buttons.py (13149B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\buttons.py 2 import logging 3 from carbonui import uiconst 4 from carbonui.fontconst import STYLE_DEFAULT, EVE_SMALL_FONTSIZE 5 from carbonui.primitives.container import Container 6 from carbonui.primitives.frame import Frame 7 from carbonui.uicore import uicore 8 from eve.client.script.ui.control.eveLabel import Label 9 log = logging.getLogger(__name__) 10 OPACITY_LABEL_IDLE = 1.0 11 12 class ButtonTextBoldness(object): 13 NEVER_BOLD = 0 14 ALWAYS_BOLD = 1 15 BOLD_ON_MOUSEOVER = 2 16 17 18 class TextButtonWithBackgrounds(Container): 19 __guid__ = 'uicontrols.TextButtonWithBackgrounds' 20 default_state = uiconst.UI_NORMAL 21 default_text = '' 22 default_fontsize = EVE_SMALL_FONTSIZE 23 default_fontStyle = STYLE_DEFAULT 24 default_boldText = ButtonTextBoldness.NEVER_BOLD 25 default_mouseUpBGTexture = None 26 default_mouseEnterBGTexture = None 27 default_mouseDownBGTexture = None 28 default_disabledBGTexture = None 29 default_mouseUpBGOpacity = 1.0 30 default_mouseEnterBGOpacity = 1.0 31 default_mouseDownBGOpacity = 1.0 32 default_disabledBGOpacity = 1.0 33 default_mouseUpBGColor = None 34 default_mouseEnterBGColor = None 35 default_mouseDownBGColor = None 36 default_disabledBGColor = None 37 default_mouseUpTextColor = (1.0, 1.0, 1.0, 1.0) 38 default_mouseEnterTextColor = (0.0, 0.0, 0.0, 1.0) 39 default_mouseDownTextColor = (0.0, 0.0, 0.0, 1.0) 40 default_disabledTextColor = (1.0, 1.0, 1.0, 1.0) 41 default_bgFadeInDuration = 0.1 42 default_bgFadeOutDuration = 0.1 43 default_frameCornerSize = 0 44 default_bgRotation = 0.0 45 default_hoverSound = None 46 default_selectedSound = None 47 default_func = None 48 default_args = None 49 default_textContainerOpacity = 1.0 50 default_isCapitalized = False 51 default_letterspace = 0 52 53 def ApplyAttributes(self, attributes): 54 self.text = attributes.Get('text', self.default_text) 55 self.fontsize = attributes.Get('fontsize', self.default_fontsize) 56 self.fontStyle = attributes.Get('fontStyle', self.default_fontStyle) 57 self.boldText = attributes.Get('boldText', self.default_boldText) 58 self.isCapitalized = attributes.Get('isCapitalized', self.default_isCapitalized) 59 self.letterspace = attributes.Get('letterspace', self.default_letterspace) 60 self.mouseUpBGTexture = attributes.Get('mouseUpBGTexture', self.default_mouseUpBGTexture) 61 self.mouseEnterBGTexture = attributes.Get('mouseEnterBGTexture', self.default_mouseEnterBGTexture) 62 self.mouseDownBGTexture = attributes.Get('mouseDownBGTexture', self.default_mouseDownBGTexture) 63 self.disabledBGTexture = attributes.Get('disabledBGTexture', self.default_disabledBGTexture) 64 self.mouseUpBGOpacity = attributes.Get('mouseUpBGOpacity', self.default_mouseUpBGOpacity) 65 self.mouseEnterBGOpacity = attributes.Get('mouseEnterBGOpacity', self.default_mouseEnterBGOpacity) 66 self.mouseDownBGOpacity = attributes.Get('mouseDownBGOpacity', self.default_mouseDownBGOpacity) 67 self.disabledBGOpacity = attributes.Get('disabledBGOpacity', self.default_disabledBGOpacity) 68 self.mouseUpBGColor = attributes.Get('mouseUpBGColor', self.default_mouseUpBGColor) 69 self.mouseEnterBGColor = attributes.Get('mouseEnterBGColor', self.default_mouseEnterBGColor) 70 self.mouseDownBGColor = attributes.Get('mouseDownBGColor', self.default_mouseDownBGColor) 71 self.disabledBGColor = attributes.Get('disabledBGColor', self.default_disabledBGColor) 72 self.mouseUpTextColor = attributes.Get('mouseUpTextColor', self.default_mouseUpTextColor) 73 self.mouseEnterTextColor = attributes.Get('mouseEnterTextColor', self.default_mouseEnterTextColor) 74 self.mouseDownTextColor = attributes.Get('mouseDownTextColor', self.default_mouseDownTextColor) 75 self.disabledTextColor = attributes.Get('disabledTextColor', self.default_disabledTextColor) 76 self.bgFadeInDuration = attributes.Get('bgFadeInDuration', self.default_bgFadeInDuration) 77 self.bgFadeOutDuration = attributes.Get('bgFadeOutDuration', self.default_bgFadeOutDuration) 78 self.frameCornerSize = attributes.Get('frameCornerSize', self.default_frameCornerSize) 79 self.bgRotation = attributes.Get('bgRotation', self.default_bgRotation) 80 self.audio = sm.GetService('audio') 81 self.hoverSound = attributes.Get('hoverSound', self.default_hoverSound) 82 self.selectedSound = attributes.Get('selectedSound', self.default_selectedSound) 83 self.func = attributes.get('func', self.default_func) 84 self.args = attributes.get('args', self.default_args) 85 textContainerOpacity = attributes.get('textContainerOpacity', self.default_textContainerOpacity) 86 self.mouseUpBG = None 87 self.mouseEnterBG = None 88 self.mouseDownBG = None 89 self.disabledBG = None 90 Container.ApplyAttributes(self, attributes) 91 self.BuildText(textContainerOpacity) 92 self.BuildBackground() 93 self.BuildMouseUpBackground() 94 self.BuildMouseEnterBackground() 95 self.BuildMouseDownBackground() 96 self.BuildDisabledBackground() 97 self.UpdateEnabledState() 98 99 def BuildBackground(self): 100 self.bgContainer = Container(name='bgContainer', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, state=uiconst.UI_DISABLED) 101 102 def BuildMouseUpBackground(self): 103 if self.mouseUpBGTexture: 104 self.mouseUpBG = Frame(name='mouseUpBG', texturePath=self.mouseUpBGTexture, parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, rotation=self.bgRotation, cornerSize=self.frameCornerSize, color=self.mouseUpBGColor) 105 else: 106 self.mouseUpBG = Container(name='mouseUpBG', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, bgColor=self.mouseUpBGColor) 107 108 def BuildMouseEnterBackground(self): 109 if self.mouseEnterBGTexture: 110 self.mouseEnterBG = Frame(name='mouseEnterBG', parent=self, align=uiconst.TOTOP_NOPUSH, texturePath=self.mouseEnterBGTexture, width=self.width, height=self.height, opacity=0.0, rotation=self.bgRotation, cornerSize=self.frameCornerSize, color=self.mouseEnterBGColor) 111 else: 112 self.mouseEnterBG = Container(name='mouseEnterBG', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, bgColor=self.mouseEnterBGColor, opacity=0.0) 113 114 def BuildMouseDownBackground(self): 115 if self.mouseDownBGTexture: 116 self.mouseDownBG = Frame(name='mouseDownBG', parent=self, align=uiconst.TOTOP_NOPUSH, texturePath=self.mouseDownBGTexture, width=self.width, height=self.height, opacity=0.0, rotation=self.bgRotation, cornerSize=self.frameCornerSize, color=self.mouseDownBGColor) 117 else: 118 self.mouseDownBG = Container(name='mouseDownBG', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, bgColor=self.mouseDownBGColor, opacity=0.0) 119 120 def BuildDisabledBackground(self): 121 if self.disabledBGTexture: 122 self.disabledBG = Frame(name='disabledBG', parent=self, align=uiconst.TOTOP_NOPUSH, texturePath=self.disabledBGTexture, width=self.width, height=self.height, opacity=0.0, rotation=self.bgRotation, cornerSize=self.frameCornerSize, color=self.disabledBGColor) 123 else: 124 self.disabledBG = Container(name='disabledBG', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, bgColor=self.disabledBGColor, opacity=0.0) 125 126 def BuildText(self, textContainerOpacity): 127 self.textContainer = Container(name='textContainer', parent=self, align=uiconst.TOTOP_NOPUSH, width=self.width, height=self.height, opacity=textContainerOpacity) 128 self.label = Label(name='text', parent=self.textContainer, align=uiconst.CENTER, fontsize=self.fontsize, fontstyle=self.fontStyle, letterspace=self.letterspace, shadowOffset=(0, 0)) 129 self.SetText(self.text) 130 self.SetTextColor(self.mouseUpTextColor) 131 if self.boldText == ButtonTextBoldness.ALWAYS_BOLD: 132 self.label.bold = True 133 134 def StopBackgroundAnimations(self): 135 uicore.animations.StopAllAnimations(self.mouseUpBG) 136 uicore.animations.StopAllAnimations(self.mouseDownBG) 137 uicore.animations.StopAllAnimations(self.mouseEnterBG) 138 uicore.animations.StopAllAnimations(self.disabledBG) 139 140 def SetText(self, text): 141 self.text = text 142 self.label.text = self.text.upper() if self.isCapitalized else self.text 143 144 def SetTextColor(self, color): 145 self.label.SetTextColor(color) 146 _, _, _, opacity = color 147 self.label.opacity = opacity 148 149 def UpdateEnabledState(self): 150 if self.disabledBG: 151 self.StopBackgroundAnimations() 152 isDisabled = self.IsDisabled() 153 self.mouseUpBG.opacity = 0.0 if isDisabled else self.mouseUpBGOpacity 154 self.disabledBG.opacity = self.disabledBGOpacity if isDisabled else 0.0 155 textColor = self.disabledTextColor if isDisabled else self.mouseUpTextColor 156 self.SetTextColor(textColor) 157 158 def SetState(self, state): 159 super(TextButtonWithBackgrounds, self).SetState(state) 160 self.UpdateEnabledState() 161 162 def IsDisabled(self): 163 return self.pickState == uiconst.TR2_SPS_OFF 164 165 def Enable(self, *args): 166 super(TextButtonWithBackgrounds, self).Enable(*args) 167 self.UpdateEnabledState() 168 169 def Disable(self, *args): 170 super(TextButtonWithBackgrounds, self).Disable(*args) 171 self.UpdateEnabledState() 172 173 def OnMouseDown(self, *args): 174 self.StopBackgroundAnimations() 175 self.mouseUpBG.opacity = 0.0 176 self.mouseEnterBG.opacity = 0.0 177 self.mouseDownBG.opacity = self.mouseDownBGOpacity 178 self.SetTextColor(self.mouseDownTextColor) 179 if self.boldText == ButtonTextBoldness.BOLD_ON_MOUSEOVER: 180 self.label.bold = True 181 182 def OnMouseUp(self, *args): 183 self.StopBackgroundAnimations() 184 uicore.animations.FadeOut(self.mouseDownBG, duration=self.bgFadeOutDuration) 185 if uicore.uilib.mouseOver == self: 186 startOpacity = self.mouseEnterBG.opacity 187 endOpacity = self.mouseEnterBGOpacity 188 uicore.animations.FadeTo(self.mouseEnterBG, startOpacity, endOpacity, duration=self.bgFadeInDuration) 189 self.SetTextColor(self.mouseEnterTextColor) 190 if self.boldText == ButtonTextBoldness.BOLD_ON_MOUSEOVER: 191 self.label.bold = True 192 else: 193 startOpacity = self.mouseUpBG.opacity 194 endOpacity = self.mouseUpBGOpacity 195 uicore.animations.FadeTo(self.mouseUpBG, startOpacity, endOpacity, duration=self.bgFadeInDuration) 196 self.SetTextColor(self.mouseUpTextColor) 197 if self.boldText == ButtonTextBoldness.BOLD_ON_MOUSEOVER: 198 self.label.bold = False 199 200 def OnMouseEnter(self, *args): 201 self.StopBackgroundAnimations() 202 uicore.animations.FadeOut(self.mouseUpBG, duration=self.bgFadeOutDuration) 203 uicore.animations.FadeOut(self.mouseDownBG, duration=self.bgFadeOutDuration) 204 startOpacity = self.mouseEnterBG.opacity 205 endOpacity = self.mouseEnterBGOpacity 206 uicore.animations.FadeTo(self.mouseEnterBG, startOpacity, endOpacity, duration=self.bgFadeInDuration) 207 self.SetTextColor(self.mouseEnterTextColor) 208 if self.boldText == ButtonTextBoldness.BOLD_ON_MOUSEOVER: 209 self.label.bold = True 210 if self.hoverSound: 211 self.audio.SendUIEvent(self.hoverSound) 212 213 def OnMouseExit(self, *args): 214 self.StopBackgroundAnimations() 215 uicore.animations.FadeOut(self.mouseEnterBG, duration=self.bgFadeOutDuration) 216 uicore.animations.FadeOut(self.mouseDownBG, duration=self.bgFadeOutDuration) 217 startOpacity = self.mouseUpBG.opacity 218 endOpacity = self.mouseUpBGOpacity 219 uicore.animations.FadeTo(self.mouseUpBG, startOpacity, endOpacity, duration=self.bgFadeInDuration) 220 self.SetTextColor(self.mouseUpTextColor) 221 if self.boldText == ButtonTextBoldness.BOLD_ON_MOUSEOVER: 222 self.label.bold = False 223 224 def OnClick(self, *args): 225 if self.selectedSound: 226 self.audio.SendUIEvent(self.selectedSound) 227 if self.func: 228 self.ExecuteFunction() 229 230 def ExecuteFunction(self): 231 if type(self.args) == tuple: 232 self.func(*self.args) 233 elif self.args: 234 self.func(self.args) 235 else: 236 self.func() 237 238 def _OnResize(self, *args): 239 if self.mouseUpBG: 240 self.mouseUpBG.width = self.width 241 self.mouseUpBG.height = self.height 242 if self.mouseEnterBG: 243 self.mouseUpBG.width = self.width 244 self.mouseUpBG.height = self.height 245 if self.mouseDownBG: 246 self.mouseUpBG.width = self.width 247 self.mouseUpBG.height = self.height 248 if self.disabledBG: 249 self.disabledBG.width = self.width 250 self.disabledBG.height = self.height