buttonIcon.py (12085B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\buttonIcon.py 2 import telemetry 3 import trinity 4 from carbon.client.script.environment.AudioUtil import PlaySound 5 from carbonui import uiconst 6 from carbonui.loggers.buttonLogger import log_button_clicked 7 from carbonui.primitives.container import Container 8 from carbonui.primitives.sprite import Sprite 9 from carbonui.text.color import TextColor 10 from carbonui.uicore import uicore 11 from carbonui.util.color import Color 12 from eve.client.script.ui import eveColor, eveThemeColor 13 from eve.client.script.ui.control.themeColored import SpriteThemeColored 14 COLOR_ACTIVE = eveColor.WHITE 15 COLOR_DEFAULT = TextColor.NORMAL 16 COLOR_INACTIVE = eveColor.GUNMETAL_GREY 17 _ICON_COLOR_HOVERED = TextColor.HIGHLIGHT 18 _ICON_COLOR_IDLE = TextColor.NORMAL 19 _ICON_COLOR_DISABLED = TextColor.DISABLED 20 _ICON_COLOR_SELECTED = eveThemeColor.THEME_ACCENT 21 22 class ButtonIcon(Container): 23 __guid__ = 'uicontrols.ButtonIcon' 24 OPACITY_IDLE = 0.0 25 GLOWAMOUNT_IDLE = 0.0 26 GLOWAMOUNT_MOUSEHOVER = 0.35 27 GLOWAMOUNT_MOUSECLICK = 1.0 28 OPACITY_SELECTED = 0.5 29 OPACITY_GLOW_IDLE = 0.0 30 default_func = None 31 default_args = None 32 default_width = 32 33 default_height = 32 34 default_align = uiconst.TOPLEFT 35 default_state = uiconst.UI_NORMAL 36 default_texturePath = None 37 default_isActive = True 38 default_iconSize = 16 39 default_rotation = 0 40 default_noBgSize = 1 41 default_iconColor = COLOR_DEFAULT 42 default_iconEnabledOpacity = 1.0 43 default_iconDisabledOpacity = 0.5 44 default_colorSelected = None 45 default_isHoverBGUsed = None 46 default_isSelectedBgUsed = False 47 default_showGlow = True 48 default_glowColor = None 49 default_soundClick = uiconst.SOUND_BUTTON_CLICK 50 default_iconClass = SpriteThemeColored 51 default_useThemeColor = True 52 default_analyticID = '' 53 default_iconBlendMode = trinity.TR2_SBM_ADD 54 default_blendMode = trinity.TR2_SBM_BLEND 55 default_playClickSound = True 56 57 def ApplyAttributes(self, attributes): 58 super(ButtonIcon, self).ApplyAttributes(attributes) 59 self.func = attributes.get('func', self.default_func) 60 self.args = attributes.get('args', self.default_args) 61 self.isActive = attributes.get('isActive', True) 62 self.texturePath = attributes.get('texturePath', self.default_texturePath) 63 self.iconSize = attributes.get('iconSize', self.default_iconSize) 64 self.iconColor = attributes.get('iconColor', self.default_iconColor) 65 self.iconEnabledOpacity = attributes.get('iconEnabledOpacity', self.default_iconEnabledOpacity) 66 self.iconDisabledOpacity = attributes.get('iconDisabledOpacity', self.default_iconDisabledOpacity) 67 self.isHoverBGUsed = attributes.Get('isHoverBGUsed', self.default_isHoverBGUsed) 68 self.isSelectedBgUsed = attributes.Get('isSelectedBgUsed', self.default_isSelectedBgUsed) 69 self.colorSelected = attributes.Get('colorSelected', self.default_colorSelected) 70 self.rotation = attributes.Get('rotation', self.default_rotation) 71 self.showGlow = attributes.Get('showGlow', self.default_showGlow) 72 self.glowColor = attributes.Get('glowColor', self.default_glowColor) 73 self.soundClick = attributes.Get('soundClick', self.default_soundClick) 74 self.playClickSound = attributes.Get('playClickSound', self.default_playClickSound) 75 self.iconBlendMode = attributes.Get('iconBlendMode', self.default_iconBlendMode) 76 self.blendMode = attributes.Get('blendMode', self.default_blendMode) 77 self.iconClass = attributes.Get('iconClass', self.default_iconClass) or self.default_iconClass 78 self.useThemeColor = attributes.Get('useThemeColor', self.default_useThemeColor) 79 self.analyticID = attributes.Get('analyticID', self.default_analyticID) 80 if self.isHoverBGUsed is None: 81 if self.iconSize < self.default_noBgSize: 82 self.isHoverBGUsed = True 83 else: 84 self.isHoverBGUsed = False 85 self.isSelected = False 86 self.enabled = True 87 self.ConstructIcon() 88 self.bgContainer = Container(name='bgCont', parent=self, align=uiconst.TOALL, state=uiconst.UI_DISABLED) 89 self.selectedBG = None 90 self.ConstructBackground() 91 self.blinkBg = None 92 enabled = attributes.get('enabled', True) 93 if not enabled: 94 self.Disable() 95 self.UpdateIconState(animate=False) 96 97 def SetIconSize(self, iconSize): 98 self.iconSize = iconSize 99 self.icon.width = self.icon.height = iconSize 100 101 def ConstructBackground(self): 102 if self.useThemeColor: 103 spriteClass = SpriteThemeColored 104 else: 105 spriteClass = Sprite 106 self.mouseEnterBG = spriteClass(name='mouseEnterBG', bgParent=self.bgContainer, texturePath='res:/UI/Texture/classes/ButtonIcon/mouseEnter.png', opacity=0.0, color=self.colorSelected) 107 self.mouseDownBG = spriteClass(name='mouseDownBG', bgParent=self.bgContainer, texturePath='res:/UI/Texture/classes/ButtonIcon/mouseDown.png', opacity=0.0, color=self.colorSelected) 108 109 def ConstructIcon(self): 110 self.icon = Sprite(name='icon', parent=self, align=uiconst.CENTER, width=self.iconSize, height=self.iconSize, texturePath=self.texturePath, state=uiconst.UI_DISABLED, rotation=self.rotation, outputMode=uiconst.OUTPUT_COLOR_AND_GLOW, glowBrightness=0.0, blendMode=self.blendMode) 111 112 def SetTexturePath(self, texturePath): 113 self.texturePath = texturePath 114 self.icon.SetTexturePath(texturePath) 115 116 def SetIconColor(self, color): 117 self.iconColor = color 118 self.icon.SetRGBA(*color) 119 120 def SetColor(self, color): 121 self.mouseEnterBG.SetFixedColor(color) 122 self.mouseDownBG.SetFixedColor(color) 123 124 def ConstructBlinkBackground(self): 125 if self.blinkBg: 126 return 127 if self.useThemeColor: 128 spriteClass = SpriteThemeColored 129 else: 130 spriteClass = Sprite 131 self.blinkBg = spriteClass(name='blinkBG', bgParent=self.bgContainer, texturePath='res:/UI/Texture/classes/ButtonIcon/mouseEnter.png', opacity=0.0) 132 133 def ConstructSelectedBackground(self): 134 if self.selectedBG: 135 return 136 if self.useThemeColor: 137 frameClass = SpriteThemeColored 138 else: 139 frameClass = Sprite 140 self.selectedBG = frameClass(name='selectedBG', bgParent=self.bgContainer, colorType=uiconst.COLORTYPE_UIHILIGHT, idx=0, color=self.colorSelected) 141 self.UpdateSelectedColor() 142 143 def Disable(self, opacity = None): 144 self.enabled = 0 145 if self.mouseEnterBG: 146 self.mouseEnterBG.StopAnimations() 147 self.mouseEnterBG.opacity = 0.0 148 self.UpdateIconState(False) 149 150 def Enable(self): 151 self.enabled = 1 152 self.UpdateIconState(False) 153 154 def SetRotation(self, value): 155 self.icon.rotation = value 156 157 def UpdateIconState(self, animate = True): 158 glowAmount = self._GetGlowAmount() 159 if animate: 160 uicore.animations.MorphScalar(self.icon, 'glowBrightness', self.icon.glowBrightness, glowAmount, duration=uiconst.TIME_ENTRY) 161 else: 162 uicore.animations.StopAnimation(self.icon, 'glowBrightness') 163 self.icon.glowBrightness = glowAmount 164 iconColor = self._GetIconColor() 165 if animate: 166 uicore.animations.SpColorMorphTo(self.icon, endColor=iconColor, duration=uiconst.TIME_ENTRY) 167 else: 168 uicore.animations.StopAnimation(self.icon, 'color') 169 self.icon.SetRGBA(*iconColor) 170 self.icon.blendMode = trinity.TR2_SBM_ADD if self.enabled else trinity.TR2_SBM_BLEND 171 172 def _GetIconColor(self): 173 if not self.enabled: 174 return _ICON_COLOR_DISABLED 175 elif uicore.uilib.mouseOver == self: 176 return _ICON_COLOR_HOVERED 177 elif self.isSelected: 178 return self.colorSelected or _ICON_COLOR_SELECTED 179 else: 180 return self.iconColor or _ICON_COLOR_IDLE 181 182 def _GetIconHoverColor(self): 183 return COLOR_ACTIVE 184 185 def _GetGlowAmount(self): 186 if not self.enabled: 187 return self.GLOWAMOUNT_IDLE 188 if uicore.uilib.mouseOver == self or self.isSelected: 189 if uicore.uilib.leftbtn: 190 return self.GLOWAMOUNT_MOUSECLICK 191 else: 192 return self.GLOWAMOUNT_MOUSEHOVER 193 else: 194 return self.GLOWAMOUNT_IDLE 195 196 def SetActive(self, isActive): 197 pass 198 199 def ToggleSelected(self): 200 if self.isSelected: 201 self.SetDeselected() 202 else: 203 self.SetSelected() 204 205 @telemetry.ZONE_METHOD 206 def SetSelected(self, force = False): 207 if self.isSelected and not force: 208 return 209 self.isSelected = True 210 self.UpdateIconState() 211 self.UpdateSelectedColor() 212 213 @telemetry.ZONE_METHOD 214 def SetDeselected(self, force = False): 215 if not self.isSelected and not force: 216 return 217 self.isSelected = False 218 self.UpdateIconState() 219 self.UpdateSelectedColor() 220 221 def UpdateSelectedColor(self): 222 if self.isSelected: 223 if self.selectedBG: 224 self.selectedBG.opacity = self.OPACITY_SELECTED 225 iconColor = Color(*self.colorSelected).SetBrightness(1.0).SetSaturation(0.3).GetRGBA() if self.colorSelected else None 226 else: 227 iconColor = self.colorSelected 228 else: 229 if self.selectedBG: 230 self.selectedBG.opacity = 0.0 231 iconColor = self.iconColor 232 if iconColor: 233 self.icon.SetRGBA(*iconColor) 234 235 def Blink(self, duration = 0.8, loops = 1): 236 self.ConstructBlinkBackground() 237 uicore.animations.FadeTo(self.blinkBg, 0.0, 0.9, duration=duration, curveType=uiconst.ANIM_WAVE, loops=loops) 238 239 def StopBlink(self): 240 if self.blinkBg: 241 uicore.animations.FadeOut(self.blinkBg, 0.3) 242 243 def OnClick(self, *args): 244 if not self._IsClickable(): 245 return 246 if self.playClickSound: 247 PlaySound(self.soundClick) 248 log_button_clicked(self) 249 self._ExecuteFunction() 250 251 def _IsClickable(self): 252 return self.enabled 253 254 def _ExecuteFunction(self): 255 if not self.func: 256 return 257 if type(self.args) == tuple: 258 self.func(*self.args) 259 elif self.args: 260 self.func(self.args) 261 else: 262 self.func() 263 264 def OnMouseEnter(self, *args): 265 self.StopBlink() 266 if not self.enabled: 267 return 268 self.UpdateIconState() 269 if self.isHoverBGUsed: 270 uicore.animations.FadeIn(self.mouseEnterBG, 0.5, duration=uiconst.TIME_ENTRY) 271 PlaySound(uiconst.SOUND_BUTTON_HOVER) 272 273 def OnMouseExit(self, *args): 274 self.UpdateIconState() 275 if self.isHoverBGUsed: 276 uicore.animations.FadeOut(self.mouseEnterBG, duration=uiconst.TIME_EXIT) 277 278 def OnMouseDown(self, *args): 279 if not self.enabled: 280 return 281 if self.isHoverBGUsed: 282 uicore.animations.FadeTo(self.mouseDownBG, self.mouseDownBG.opacity, 1.0, duration=0.05) 283 uicore.animations.FadeOut(self.mouseEnterBG, duration=0.05) 284 self.UpdateIconState() 285 286 def OnMouseUp(self, *args): 287 if self.isHoverBGUsed: 288 uicore.animations.FadeOut(self.mouseDownBG, duration=0.05) 289 if not self.enabled: 290 return 291 self.UpdateIconState() 292 if uicore.uilib.mouseOver == self: 293 if self.isHoverBGUsed: 294 uicore.animations.FadeIn(self.mouseEnterBG, 0.5, duration=0.05) 295 296 def OnEndDrag(self, *args): 297 if uicore.uilib.mouseOver != self: 298 uicore.animations.FadeOut(self.mouseEnterBG, duration=0.2) 299 elif self.isHoverBGUsed: 300 uicore.animations.FadeIn(self.mouseEnterBG, duration=0.1) 301 uicore.animations.FadeOut(self.mouseDownBG, duration=0.1) 302 self.UpdateIconState()