toggleButtonGroupButton.py (17881B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\toggleButtonGroupButton.py 2 import trinity 3 from carbon.client.script.environment.AudioUtil import PlaySound 4 from carbonui import Density, TextColor, uiconst 5 from carbonui.button import styling 6 from carbonui.control.button import ButtonUnderlay, OPACITY_LABEL_HOVER, OPACITY_LABEL_IDLE 7 from carbonui.decorative.selectionIndicatorLine import SelectionIndicatorLine 8 from carbonui.fontconst import EVE_SMALL_FONTSIZE 9 from carbonui.loggers.buttonLogger import log_button_clicked 10 from carbonui.primitives.base import ReverseScaleDpi 11 from carbonui.primitives.container import Container 12 from carbonui.primitives.sprite import Sprite 13 from carbonui.uianimations import animations 14 from carbonui.uicore import uicore 15 from carbonui.util.color import Color 16 from eve.client.script.ui import eveColor 17 from eve.client.script.ui.control.eveLabel import EveLabelMedium 18 from eve.client.script.ui.control.glowSprite import GlowSprite 19 from eve.client.script.ui.control.themeColored import LabelThemeColored 20 from eve.client.script.ui.control.toggleButtonUnderlay import FrameType, ToggleButtonUnderlay 21 22 class BaseToggleButtonGroupButton(Container): 23 default_state = uiconst.UI_NORMAL 24 default_align = uiconst.TOLEFT_PROP 25 default_analyticID = None 26 default_iconPath = None 27 default_iconSize = None 28 default_label = None 29 default_underlayClass = ToggleButtonUnderlay 30 AUTO_HEIGHT_ENABLED_BY_DEFAULT = False 31 32 def __init__(self, colorSelected = None, density = Density.NORMAL, **kwargs): 33 self._density = density 34 self.colorSelected = colorSelected 35 super(BaseToggleButtonGroupButton, self).__init__(**kwargs) 36 37 def ApplyAttributes(self, attributes): 38 super(BaseToggleButtonGroupButton, self).ApplyAttributes(attributes) 39 self.btnID = attributes.Get('btnID', None) 40 self.panel = attributes.Get('panel', None) 41 self.isDisabled = attributes.Get('isDisabled', False) 42 self.analyticID = attributes.Get('analyticID', self.default_analyticID) 43 self.underlayClass = attributes.Get('underlayClass', self.default_underlayClass) 44 self.controller = attributes.controller 45 self.iconPath = attributes.iconPath or self.default_iconPath 46 self.iconSize = attributes.iconSize or self.default_iconSize 47 self.label = attributes.label or self.default_label 48 self.isSelected = False 49 50 @property 51 def density(self): 52 return self._density 53 54 @density.setter 55 def density(self, value): 56 if self._density != value: 57 self._density = value 58 self.FlagAlignmentDirty('{}.density'.format(self.__class__.__name__)) 59 60 def GetAutoHeight(self): 61 return None 62 63 def SetSelected(self, animate = True): 64 self.isSelected = True 65 66 def SetDeselected(self, animate = True): 67 self.isSelected = False 68 69 def IsSelected(self): 70 return self.isSelected 71 72 def OnClick(self, *args): 73 if self.isDisabled: 74 return 75 if self.IsSelected(): 76 if self.controller.IsOptional(): 77 self.controller.DeselectAll() 78 else: 79 self.controller.Select(self, playSound=True) 80 log_button_clicked(self) 81 82 def OnButtonAdded(self): 83 pass 84 85 86 class LegacyToggleButtonGroupButton(BaseToggleButtonGroupButton): 87 OPACITY_SELECTED = 1.0 88 OPACITY_HOVER = 0.125 89 TEXT_TOPMARGIN = 4 90 default_padRight = 1 91 default_iconSize = 32 92 default_colorSelected = None 93 default_iconOpacity = 1.0 94 default_showBg = True 95 default_fontSize = EVE_SMALL_FONTSIZE 96 97 def ApplyAttributes(self, attributes): 98 super(LegacyToggleButtonGroupButton, self).ApplyAttributes(attributes) 99 self.colorSelected = attributes.Get('colorSelected', self.default_colorSelected) 100 label = attributes.Get('label', None) 101 self.fontSize = attributes.Get('fontSize', self.default_fontSize) 102 iconPath = attributes.Get('iconPath', None) 103 iconSize = attributes.Get('iconSize', None) 104 iconSize = iconSize or self.default_iconSize 105 iconOpacity = attributes.get('iconOpacity', self.default_iconOpacity) 106 self.showBg = attributes.Get('showBg', self.default_showBg) 107 self.ConstructLayout(iconOpacity, iconPath, iconSize, label) 108 self.selectedBG = ButtonUnderlay(parent=self) 109 if not self.showBg: 110 self.selectedBG.display = False 111 if self.isDisabled: 112 self.SetDisabled() 113 114 def ConstructLayout(self, iconOpacity, iconPath, iconSize, label): 115 if iconPath: 116 self.AddIcon(self, iconOpacity, iconPath, iconSize) 117 self.label = None 118 else: 119 clipper = Container(parent=self, clipChildren=True, padding=(2, 0, 2, 0)) 120 self.AddLabel(clipper, label) 121 self.icon = None 122 123 def AddIcon(self, parent, iconOpacity, iconPath, iconSize): 124 self.icon = GlowSprite(parent=parent, align=uiconst.CENTER, state=uiconst.UI_DISABLED, width=iconSize, height=iconSize, texturePath=iconPath, iconOpacity=iconOpacity, color=Color.GRAY6) 125 126 def AddLabel(self, parent, label, maxWidth = None): 127 self.label = LabelThemeColored(text=label, parent=parent, align=uiconst.CENTER, fontsize=self.fontSize, maxWidth=maxWidth, autoFadeSides=20) 128 129 def GetAutoHeight(self): 130 if self.label: 131 return ReverseScaleDpi(self.label.textheight) + self.TEXT_TOPMARGIN * 2 132 if self.icon: 133 return self.icon.height 134 return 0 135 136 def SetDisabled(self): 137 self.isDisabled = True 138 if self.icon: 139 self.icon.opacity = 0.1 140 if self.label: 141 self.label.opacity = 0.1 142 if self.showBg: 143 self.selectedBG.SetDisabled() 144 145 def SetEnabled(self): 146 self.isDisabled = False 147 if self.showBg: 148 self.selectedBG.SetEnabled() 149 150 def OnMouseEnter(self, *args): 151 PlaySound(uiconst.SOUND_BUTTON_HOVER) 152 if not self.isSelected and not self.isDisabled: 153 self.selectedBG.OnMouseEnter() 154 if self.icon: 155 self.icon.OnMouseEnter() 156 else: 157 uicore.animations.FadeTo(self.label, self.label.opacity, OPACITY_LABEL_HOVER, duration=uiconst.TIME_ENTRY) 158 159 def OnMouseExit(self, *args): 160 if self.isDisabled: 161 return 162 if not self.isSelected: 163 self.selectedBG.OnMouseExit() 164 if self.icon: 165 self.icon.OnMouseExit() 166 elif not self.isSelected: 167 uicore.animations.FadeTo(self.label, self.label.opacity, OPACITY_LABEL_IDLE, duration=uiconst.TIME_EXIT) 168 169 def OnMouseDown(self, *args): 170 if self.isDisabled: 171 return 172 if self.icon: 173 self.icon.OnMouseDown() 174 self.selectedBG.OnMouseDown() 175 176 def OnMouseUp(self, *args): 177 if self.isDisabled: 178 return 179 if self.icon: 180 self.icon.OnMouseUp() 181 self.selectedBG.OnMouseUp() 182 183 def SetSelected(self, animate = True): 184 super(LegacyToggleButtonGroupButton, self).SetSelected(animate) 185 if not self.showBg: 186 self.selectedBG.display = True 187 self.selectedBG.Select() 188 if self.label: 189 self.label.opacity = OPACITY_LABEL_HOVER 190 if self.icon: 191 self.icon.OnMouseExit() 192 193 def SetDeselected(self, animate = True): 194 super(LegacyToggleButtonGroupButton, self).SetDeselected(animate) 195 if self.label: 196 self.label.opacity = OPACITY_LABEL_IDLE 197 if self.isDisabled: 198 return 199 if not self.showBg: 200 self.selectedBG.display = False 201 self.selectedBG.Deselect() 202 203 204 class ToggleButtonGroupButton(BaseToggleButtonGroupButton): 205 _hovered = False 206 _label = None 207 _pressed = False 208 _underlay = None 209 AUTO_HEIGHT_ENABLED_BY_DEFAULT = True 210 211 def __init__(self, **kwargs): 212 super(ToggleButtonGroupButton, self).__init__(**kwargs) 213 self._layout() 214 215 def GetAutoHeight(self): 216 return styling.get_height(self._density) 217 218 def _layout(self): 219 self._label = EveLabelMedium(parent=Container(parent=self, align=uiconst.TOALL, padding=(8, 0, 8, 0)), text=self.label, align=uiconst.CENTER, autoFadeSides=8, shadowSpriteEffect=trinity.TR2_SFX_GLOW, color=self._get_label_color()) 220 self._underlay = self.underlayClass(bgParent=self, selected=self.isSelected, enabled=not self.isDisabled, selected_color_override=self.colorSelected) 221 if self.isDisabled: 222 self.SetDisabled() 223 224 def OnButtonAdded(self): 225 if not self.parent: 226 return 227 idx = self.GetOrder() 228 is_first = idx == 0 229 is_last = idx == len(self.parent.children) - 1 230 if self._underlay: 231 if is_first: 232 self._underlay.frame_type = FrameType.LEFT 233 elif is_last: 234 self._underlay.frame_type = FrameType.RIGHT 235 else: 236 self._underlay.frame_type = FrameType.MIDDLE 237 if not is_first: 238 self.padLeft = 2 239 if not is_last: 240 self.padRight = 2 241 242 def SetDisabled(self): 243 self.isDisabled = True 244 if self._underlay: 245 self._underlay.enabled = False 246 self._update_label_color(duration=uiconst.TIME_EXIT) 247 248 def SetEnabled(self): 249 self.isDisabled = False 250 if self._underlay: 251 self._underlay.enabled = True 252 self._update_label_color(duration=uiconst.TIME_ENTRY) 253 254 def OnMouseEnter(self, *args): 255 self._hovered = True 256 if self._underlay: 257 self._underlay.hovered = True 258 self._update_label_color(duration=uiconst.TIME_ENTRY) 259 if not self.isSelected and not self.isDisabled: 260 PlaySound(uiconst.SOUND_BUTTON_HOVER) 261 262 def OnMouseExit(self, *args): 263 self._hovered = False 264 if self._underlay: 265 self._underlay.hovered = False 266 self._update_label_color(duration=uiconst.TIME_EXIT) 267 268 def OnMouseDown(self, button, *args): 269 if button == uiconst.MOUSELEFT: 270 self._pressed = True 271 if self._underlay: 272 self._underlay.pressed = True 273 self._update_label_color(duration=uiconst.TIME_MOUSEDOWN) 274 275 def OnMouseUp(self, button, *args): 276 if button == uiconst.MOUSELEFT: 277 self._pressed = False 278 if self._underlay: 279 self._underlay.pressed = False 280 self._update_label_color(duration=uiconst.TIME_MOUSEUP) 281 282 def SetSelected(self, animate = True): 283 super(ToggleButtonGroupButton, self).SetSelected(animate) 284 if self._underlay: 285 self._underlay.set_selected(True, animate) 286 self._update_label_color(duration=uiconst.TIME_SELECT) 287 288 def SetDeselected(self, animate = True): 289 super(ToggleButtonGroupButton, self).SetDeselected(animate) 290 if self._underlay: 291 self._underlay.set_selected(False, animate) 292 self._update_label_color(duration=uiconst.TIME_SELECT) 293 294 def UpdateAlignment(self, budgetLeft = 0, budgetTop = 0, budgetWidth = 0, budgetHeight = 0, updateChildrenOnly = False): 295 result = super(ToggleButtonGroupButton, self).UpdateAlignment(budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly) 296 if self._label: 297 label_clipper_width, _ = self._label.parent.GetCurrentAbsoluteSize() 298 if self._label.width > label_clipper_width: 299 self._label.align = uiconst.CENTERLEFT 300 else: 301 self._label.align = uiconst.CENTER 302 if self._childrenAlignmentDirty: 303 super(ToggleButtonGroupButton, self).UpdateAlignment(budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly=True) 304 return result 305 306 def _get_label_color(self): 307 if self.isDisabled: 308 return TextColor.DISABLED 309 elif self.isSelected: 310 return TextColor.HIGHLIGHT 311 elif self._pressed: 312 return eveColor.BLACK 313 elif self._hovered: 314 return TextColor.HIGHLIGHT 315 else: 316 return TextColor.NORMAL 317 318 def _update_label_color(self, duration): 319 if self._label: 320 if duration > 0.0: 321 animations.SpColorMorphTo(self._label, endColor=self._get_label_color(), duration=duration) 322 else: 323 animations.StopAnimation(self._label, 'color') 324 self._label.color = self._get_label_color() 325 326 327 class ToggleButtonGroupButtonIcon(BaseToggleButtonGroupButton): 328 _icon = None 329 _pressed = False 330 _underlay = None 331 332 def __init__(self, **kwargs): 333 super(ToggleButtonGroupButtonIcon, self).__init__(**kwargs) 334 self._layout() 335 if self.isDisabled: 336 self.SetDisabled() 337 338 def _layout(self): 339 self._icon = Sprite(parent=self, align=uiconst.CENTER, width=self.iconSize, height=self.iconSize, state=uiconst.UI_DISABLED, texturePath=self.iconPath, color=self._get_icon_color()) 340 self._underlay = self.underlayClass(bgParent=self, selected=self.isSelected, enabled=not self.isDisabled, selected_color_override=self.colorSelected) 341 342 def GetAutoHeight(self): 343 return self.iconSize + 8 344 345 def OnButtonAdded(self): 346 idx = self.GetOrder() 347 is_first = idx == 0 348 is_last = idx == len(self.parent.children) - 1 349 if self._underlay: 350 if is_first: 351 self._underlay.frame_type = FrameType.LEFT 352 elif is_last: 353 self._underlay.frame_type = FrameType.RIGHT 354 else: 355 self._underlay.frame_type = FrameType.MIDDLE 356 if not is_first: 357 self.padLeft = 2 358 if not is_last: 359 self.padRight = 2 360 361 def SetDisabled(self): 362 self.isDisabled = True 363 if self._underlay: 364 self._underlay.enabled = False 365 self._update_icon_color(duration=uiconst.TIME_SELECT) 366 367 def SetEnabled(self): 368 self.isDisabled = False 369 if self._underlay: 370 self._underlay.enabled = True 371 self._update_icon_color(duration=uiconst.TIME_DESELECT) 372 373 def OnMouseEnter(self, *args): 374 if self._underlay: 375 self._underlay.hovered = True 376 if not self.isSelected and not self.isDisabled: 377 PlaySound(uiconst.SOUND_BUTTON_HOVER) 378 379 def OnMouseExit(self, *args): 380 if self._underlay: 381 self._underlay.hovered = False 382 383 def OnMouseDown(self, button, *args): 384 if button == uiconst.MOUSELEFT: 385 self._pressed = True 386 if self._underlay: 387 self._underlay.pressed = True 388 self._update_icon_color(duration=uiconst.TIME_MOUSEDOWN) 389 390 def OnMouseUp(self, button, *args): 391 if button == uiconst.MOUSELEFT: 392 self._pressed = False 393 if self._underlay: 394 self._underlay.pressed = False 395 self._update_icon_color(duration=uiconst.TIME_MOUSEUP) 396 397 def SetSelected(self, animate = True): 398 super(ToggleButtonGroupButtonIcon, self).SetSelected(animate) 399 if self._underlay: 400 self._underlay.set_selected(True, animate) 401 self._update_icon_color(duration=uiconst.TIME_SELECT) 402 403 def SetDeselected(self, animate = True): 404 super(ToggleButtonGroupButtonIcon, self).SetDeselected(animate) 405 if self._underlay: 406 self._underlay.set_selected(False, animate) 407 self._update_icon_color(duration=uiconst.TIME_DESELECT) 408 409 def _get_icon_color(self): 410 if self.isDisabled: 411 return TextColor.DISABLED 412 elif self.isSelected: 413 return eveColor.WHITE 414 elif self._pressed: 415 return eveColor.BLACK 416 else: 417 return eveColor.WHITE 418 419 def _update_icon_color(self, duration): 420 if self._icon: 421 if duration > 0.0: 422 animations.SpColorMorphTo(self._icon, endColor=self._get_icon_color(), duration=duration) 423 else: 424 animations.StopAnimation(self._icon, 'color') 425 self._icon.color = self._get_icon_color() 426 427 428 class ToggleButtonGroupButtonIconIndicator(ToggleButtonGroupButtonIcon): 429 430 def __init__(self, **kwargs): 431 super(ToggleButtonGroupButtonIcon, self).__init__(**kwargs) 432 self._hovered = False 433 self._layout() 434 if self.isDisabled: 435 self.SetDisabled() 436 437 def _layout(self): 438 self._icon = Sprite(parent=self, align=uiconst.CENTER, width=self.iconSize, height=self.iconSize, state=uiconst.UI_DISABLED, texturePath=self.iconPath, color=self._get_icon_color()) 439 self._selectionLine = SelectionIndicatorLine(parent=self, align=uiconst.CENTERBOTTOM, width=self.iconSize / 2, height=1) 440 441 def _get_icon_color(self): 442 if self.isDisabled: 443 return TextColor.DISABLED 444 elif self.isSelected: 445 return eveColor.WHITE 446 elif self._hovered: 447 return eveColor.WHITE 448 elif self._pressed: 449 return eveColor.BLACK 450 else: 451 return eveColor.LED_GREY 452 453 def OnMouseEnter(self, *args): 454 if not self.isSelected and not self.isDisabled: 455 self._hovered = True 456 PlaySound(uiconst.SOUND_BUTTON_HOVER) 457 self._update_icon_color(duration=uiconst.TIME_ENTRY) 458 459 def OnMouseExit(self, *args): 460 self._hovered = False 461 self._update_icon_color(duration=uiconst.TIME_EXIT) 462 463 def SetSelected(self, animate = True): 464 super(ToggleButtonGroupButtonIconIndicator, self).SetSelected(animate) 465 self._selectionLine.Select() 466 467 def SetDeselected(self, animate = True): 468 super(ToggleButtonGroupButtonIconIndicator, self).SetDeselected(animate) 469 self._selectionLine.Deselect()