From 2bddfe68bcbf6a9b01a44d8f059ab04f49e6755d Mon Sep 17 00:00:00 2001 From: Eric Mehl Date: Fri, 2 Aug 2024 11:31:45 -0400 Subject: [PATCH] fixup! ColorChooser : Add `_ColorField` widget --- python/GafferUI/ColorChooser.py | 56 +++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/python/GafferUI/ColorChooser.py b/python/GafferUI/ColorChooser.py index ade3cf2a93..44b9ff5b76 100644 --- a/python/GafferUI/ColorChooser.py +++ b/python/GafferUI/ColorChooser.py @@ -387,44 +387,60 @@ def __drawValue( self, painter ) : painter.setBrush( QtGui.QBrush( color ) ) size = self.size() + + # Use a dot when both axes are a valid value. if position.x >= 0 and position.y >= 0 and position.x <= size.x and position.y <= size.y : painter.drawEllipse( QtCore.QPoint( position.x, position.y ), 4.5, 4.5 ) return - # Draw a triangle pointing out of the field. Prioritize corners to avoid cutting - # off part of the triangle out of the field when one axis is right at 0 or size. - # |\ <-- We don't want this overhang. - # -------|-> - # |/| - # | - # | - if position.x >= size.x and position.y <= 0 : + triangleWidth = 5.0 + triangleSpacing = 2.0 + positionClamped = imath.V2f( + min( max( 0.0, position.x ), size.x ), + min( max( 0.0, position.y ), size.y ) + ) + offset = imath.V2f( 0 ) + # Use a corner triangle if both axes are invalid values. + if position.x > size.x and position.y < 0 : rotation = -45.0 # Triangle pointing to the top-right - elif position.x <= 0 and position.y <= 0 : + offset = imath.V2f( -triangleSpacing, triangleSpacing ) + elif position.x < 0 and position.y < 0 : rotation = -135.0 # Top-left - elif position.x <= 0 and position.y >= size.y : + offset = imath.V2f( triangleSpacing, triangleSpacing ) + elif position.x < 0 and position.y > size.y : rotation = -225.0 # Bottom-left - elif position.x >= size.x and position.y >= size.y : + offset = imath.V2f( triangleSpacing, -triangleSpacing ) + elif position.x > size.x and position.y > size.y : rotation = -315.0 # Bottom-right + offset = imath.V2f( -triangleSpacing, -triangleSpacing ) - elif position.x <= size.x and position.x >= 0 and position.y <= 0 : + # Finally, use a top / left / bottom / right triangle if one axis is an invalid value. + elif position.y < 0 : rotation = -90.0 # Top - elif position.x <= 0 and position.y >= 0 and position.y <= size.y : + offset = imath.V2f( 0, triangleSpacing ) + # Clamp it in more to account for the triangle size + positionClamped.x = min( max( triangleWidth + triangleSpacing, positionClamped.x ), size.x - triangleWidth - triangleSpacing ) + elif position.x < 0 : rotation = -180.0 # Left - elif position.x >= 0 and position.x <= size.x and position.y >= size.y : + offset = imath.V2f( triangleSpacing, 0 ) + positionClamped.y = min( max( triangleWidth + triangleSpacing, positionClamped.y ), size.y - triangleWidth - triangleSpacing ) + elif position.y > size.y : rotation = -270.0 # Bottom + offset = imath.V2f( 0, -triangleSpacing ) + positionClamped.x = min( max( triangleWidth + triangleSpacing, positionClamped.x ), size.x - triangleWidth - triangleSpacing ) else : rotation = 0.0 # Right + offset = imath.V2f( -triangleSpacing, 0 ) + positionClamped.y = min( max( triangleWidth + triangleSpacing, positionClamped.y ), size.y - triangleWidth - triangleSpacing ) - rightPoints = [ imath.V2f( 0, 0 ), imath.V2f( -6, 5 ), imath.V2f( -6, -5 ) ] + rightPoints = [ imath.V2f( 0, 0 ), imath.V2f( -6, triangleWidth ), imath.V2f( -6, -triangleWidth ) ] xform = imath.M33f().rotate( math.radians( rotation ) ) * imath.M33f().translate( - imath.V2f( - min( max( 0.0, position.x ), size.x ), - min( max( 0.0, position.y ), size.y ) - ) + positionClamped + offset ) points = [ p * xform for p in rightPoints ] - points = [ QtCore.QPoint( p.x, p.y ) for p in points ] + # Transforming the points introduces slight precision errors which can be noticeable + # when drawing polygons. Round the values to compensate. + points = [ QtCore.QPoint( round( p.x ), round( p.y ) ) for p in points ] painter.drawConvexPolygon( points )