resize_handle.py (10011B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\decorative\resize_handle.py 2 import math 3 import signals 4 import trinity 5 from carbonui import Axis, AxisAlignment, uiconst 6 from carbonui.decorative.divider_line import DividerLine 7 from carbonui.primitives.container import Container 8 from carbonui.primitives.sprite import Sprite 9 from carbonui.uianimations import animations 10 from carbonui.uicore import uicore 11 from carbonui.util.dpi import reverse_scale_dpi 12 from eve.client.script.ui import eveThemeColor 13 14 class ResizeHandle(Container): 15 HANDLE_THICKNESS = 1 16 MASK_SIZE = 128 17 _dragging = False 18 _handle = None 19 _last_mouse_position = (0, 0) 20 _line = None 21 _on_drag_end = None 22 _on_drag_move = None 23 _on_drag_start = None 24 25 def __init__(self, orientation, on_drag_start = None, on_drag_move = None, on_drag_end = None, show_line = False, cross_axis_alignment = AxisAlignment.CENTER, size = 8, state = uiconst.UI_NORMAL, **kwargs): 26 self._cross_axis_alignment = cross_axis_alignment 27 self._orientation = orientation 28 self._size = size 29 self._show_line = show_line 30 width, height = self._get_dimensions() 31 super(ResizeHandle, self).__init__(width=width, height=height, cursor=self._get_cursor(), state=state, **kwargs) 32 self._create_handle() 33 if self._show_line: 34 self._create_line() 35 if on_drag_start: 36 self.on_drag_start.connect(on_drag_start) 37 if on_drag_move: 38 self.on_drag_move.connect(on_drag_move) 39 if on_drag_end: 40 self.on_drag_end.connect(on_drag_end) 41 42 @property 43 def cross_axis_alignment(self): 44 return self._cross_axis_alignment 45 46 @cross_axis_alignment.setter 47 def cross_axis_alignment(self, value): 48 if self._cross_axis_alignment != value: 49 self._cross_axis_alignment = value 50 self.FlagAlignmentDirty() 51 52 @property 53 def orientation(self): 54 return self._orientation 55 56 @orientation.setter 57 def orientation(self, value): 58 if self._orientation != value: 59 self._orientation = value 60 self._update_orientation() 61 62 @property 63 def show_line(self): 64 return self._show_line 65 66 @show_line.setter 67 def show_line(self, value): 68 if self._show_line != value: 69 self._show_line = value 70 self._update_show_line() 71 72 @property 73 def size(self): 74 return self._size 75 76 @size.setter 77 def size(self, value): 78 if self._size != value: 79 self._size = value 80 self.FlagAlignmentDirty() 81 82 @property 83 def on_drag_start(self): 84 if self._on_drag_start is None: 85 self._on_drag_start = signals.Signal('{}.on_drag_start'.format(self.__class__.__name__)) 86 return self._on_drag_start 87 88 @property 89 def on_drag_move(self): 90 if self._on_drag_move is None: 91 self._on_drag_move = signals.Signal('{}.on_drag_move'.format(self.__class__.__name__)) 92 return self._on_drag_move 93 94 @property 95 def on_drag_end(self): 96 if self._on_drag_end is None: 97 self._on_drag_end = signals.Signal('{}.on_drag_end'.format(self.__class__.__name__)) 98 return self._on_drag_end 99 100 def _get_cursor(self): 101 if self._orientation == Axis.HORIZONTAL: 102 return uiconst.UICORSOR_VERTICAL_RESIZE 103 if self._orientation == Axis.VERTICAL: 104 return uiconst.UICORSOR_HORIZONTAL_RESIZE 105 106 def _update_cursor(self): 107 self.cursor = self._get_cursor() 108 109 def _update_axis(self): 110 self._update_cursor() 111 if self._handle: 112 self._handle.align = self._get_handle_align() 113 if self._line: 114 self._line.align = self._get_handle_align() 115 116 def _update_orientation(self): 117 self._update_cursor() 118 if self._handle: 119 self._handle.align = self._get_handle_align() 120 if self._line: 121 self._line.align = self._get_handle_align() 122 123 def _create_handle(self): 124 self._handle = Sprite(parent=self, align=self._get_handle_align(), state=uiconst.UI_DISABLED, texturePath='res:/UI/Texture/classes/ResizeHandle/fill.png', textureSecondaryPath='res:/UI/Texture/classes/ResizeHandle/mask.png', color=self._get_color(), spriteEffect=trinity.TR2_SFX_MODULATE, outputMode=uiconst.OUTPUT_COLOR_AND_GLOW, glowBrightness=0.5, opacity=0.0) 125 self._update_mask() 126 127 def _update_mask(self): 128 x, y = self._handle.GetCurrentAbsolutePosition() 129 w, h = self._handle.GetCurrentAbsoluteSize() 130 px = x + w / 2.0 - uicore.uilib.x 131 py = y + h / 2.0 - uicore.uilib.y 132 self._handle.scaleSecondary = (w / float(self.MASK_SIZE), h / float(self.MASK_SIZE)) 133 self._handle.translationSecondary = (px / float(self.MASK_SIZE), py / float(self.MASK_SIZE)) 134 135 def _get_handle_align(self): 136 if self._orientation == Axis.HORIZONTAL: 137 return uiconst.TOTOP_NOPUSH 138 if self._orientation == Axis.VERTICAL: 139 return uiconst.TOLEFT_NOPUSH 140 141 @staticmethod 142 def _get_color(): 143 return eveThemeColor.THEME_ACCENT[:3] 144 145 def _create_line(self, opacity_override = None): 146 if opacity_override is not None: 147 opacity = opacity_override 148 else: 149 opacity = self._get_line_opacity() 150 self._line = DividerLine(parent=self, align=self._get_handle_align(), opacity=opacity) 151 152 def _get_line_opacity(self): 153 if self._show_line: 154 return 1.0 155 else: 156 return 0.0 157 158 def _update_line_opacity(self): 159 if self._line: 160 animations.FadeTo(self._line, startVal=self._line.opacity, endVal=self._get_line_opacity(), duration=0.3) 161 162 def _update_show_line(self): 163 if self._show_line and self._line is None: 164 self._create_line(opacity_override=0.0) 165 self._update_line_opacity() 166 167 def OnColorThemeChanged(self): 168 if self._handle: 169 self._handle.color = self._get_color() 170 171 def _get_dimensions(self): 172 if self.align == uiconst.TOALL: 173 return (0, 0) 174 if self._orientation == Axis.HORIZONTAL: 175 height = self._size 176 if self.align in uiconst.ALIGNMENTS_WITH_INCLUDED_VERTICAL_PADDING: 177 height += self.padTop + self.padBottom 178 return (self.width, height) 179 if self._orientation == Axis.VERTICAL: 180 width = self._size 181 if self.align in uiconst.ALIGNMENTS_WITH_INCLUDED_HORIZONTAL_PADDING: 182 width += self.padLeft + self.padRight 183 return (width, self.height) 184 185 def _update_dimensions(self): 186 self.width, self.height = self._get_dimensions() 187 188 def _get_handle_dimensions(self): 189 if self._orientation == Axis.HORIZONTAL: 190 return (0, self.HANDLE_THICKNESS) 191 if self._orientation == Axis.VERTICAL: 192 return (self.HANDLE_THICKNESS, 0) 193 194 def _update_handle_dimensions(self): 195 width, height = self._get_handle_dimensions() 196 if self._handle: 197 self._handle.width = width 198 self._handle.height = height 199 if self._line: 200 self._line.width = width 201 self._line.height = height 202 203 def _update_handle_position(self, budget_width, budget_height): 204 left = 0 205 top = 0 206 if self._orientation == Axis.HORIZONTAL: 207 if self._cross_axis_alignment == AxisAlignment.START: 208 top = 0 209 elif self._cross_axis_alignment == AxisAlignment.CENTER: 210 top = int(math.floor((budget_height - self.HANDLE_THICKNESS) / 2.0)) 211 elif self._cross_axis_alignment == AxisAlignment.END: 212 top = budget_height - self.HANDLE_THICKNESS 213 elif self._cross_axis_alignment == AxisAlignment.START: 214 left = 0 215 elif self._cross_axis_alignment == AxisAlignment.CENTER: 216 left = int(math.floor((budget_width - self.HANDLE_THICKNESS) / 2.0)) 217 elif self._cross_axis_alignment == AxisAlignment.END: 218 left = budget_width - self.HANDLE_THICKNESS 219 if self._handle: 220 self._handle.left = left 221 self._handle.top = top 222 if self._line: 223 self._line.left = left 224 self._line.top = top 225 226 def UpdateAlignment(self, budgetLeft = 0, budgetTop = 0, budgetWidth = 0, budgetHeight = 0, updateChildrenOnly = False): 227 self._update_dimensions() 228 self._update_handle_dimensions() 229 self._update_handle_position(budget_width=reverse_scale_dpi(budgetWidth), budget_height=reverse_scale_dpi(budgetHeight)) 230 return super(ResizeHandle, self).UpdateAlignment(budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly) 231 232 def OnMouseDown(self, button): 233 if not self._dragging and button == uiconst.MOUSELEFT: 234 self._dragging = True 235 self._last_mouse_position = (uicore.uilib.x, uicore.uilib.y) 236 if self._on_drag_start is not None: 237 self._on_drag_start() 238 239 def OnMouseUp(self, button): 240 if self._dragging and button == uiconst.MOUSELEFT: 241 self._dragging = False 242 if self._on_drag_end is not None: 243 self._on_drag_end() 244 245 def OnMouseMove(self, *args): 246 self._update_mask() 247 if self._dragging: 248 if self._on_drag_move is not None: 249 if self._orientation == Axis.VERTICAL: 250 self._on_drag_move(uicore.uilib.x - self._last_mouse_position[0]) 251 elif self._orientation == Axis.HORIZONTAL: 252 self._on_drag_move(uicore.uilib.y - self._last_mouse_position[1]) 253 self._last_mouse_position = (uicore.uilib.x, uicore.uilib.y) 254 255 def OnMouseEnter(self, *args): 256 self._update_mask() 257 animations.FadeIn(self._handle, duration=0.15) 258 259 def OnMouseExit(self, *args): 260 animations.FadeOut(self._handle, duration=0.3)