gauge.py (13412B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\gauge.py 2 import math 3 from math import pi 4 import carbonui.const as uiconst 5 import uthread 6 from carbon.common.script.util.mathCommon import FloatCloseEnough 7 from carbonui.const import TOLEFT_PROP, TOPRIGHT, TOPLEFT 8 from carbonui.primitives.container import Container 9 from carbonui.primitives.containerAutoSize import ContainerAutoSize 10 from carbonui.primitives.fill import Fill 11 from carbonui.primitives.gradientSprite import GradientSprite 12 from carbonui.primitives.sprite import Sprite 13 from carbonui.uiconst import OutputMode 14 from carbonui.uicore import uicore 15 from carbonui.util.color import Color 16 from eve.client.script.ui.control.eveLabel import EveLabelSmall 17 from eve.client.script.ui.control.themeColored import GradientThemeColored, FillThemeColored 18 CLOSE_ENOUGH_TOLERANCE = 1e-07 19 20 class _GaugeBase(ContainerAutoSize): 21 default_state = uiconst.UI_NORMAL 22 default_align = uiconst.TOPLEFT 23 default_alignMode = uiconst.TOTOP 24 default_width = 100 25 default_height = 30 26 default_gaugeHeight = 6 27 default_label = '' 28 default_subText = '' 29 default_gradientBrightnessFactor = 2.0 30 default_gaugeAlign = TOLEFT_PROP 31 default_animateInRealTime = True 32 default_labelClass = EveLabelSmall 33 default_textPadding = (0, 0, 0, 1) 34 default_glowBrightness = 0.5 35 36 def ApplyAttributes(self, attributes): 37 ContainerAutoSize.ApplyAttributes(self, attributes) 38 labelTxt = attributes.Get('label', self.default_label) 39 subTxt = attributes.Get('subText', self.default_subText) 40 gaugeHeight = attributes.Get('gaugeHeight', self.default_gaugeHeight) 41 self.gradientBrightnessFactor = attributes.Get('gradientBrightnessFactor', self.default_gradientBrightnessFactor) 42 self.gaugeAlign = attributes.Get('gaugeAlign', self.default_gaugeAlign) 43 self.useRealTime = attributes.get('useRealTime', self.default_animateInRealTime) 44 self.labelClass = attributes.get('labelClass', self.default_labelClass) 45 self.textPadding = attributes.get('textPadding', self.default_textPadding) 46 self.glowBrightness = attributes.get('glowBrightness', self.default_glowBrightness) 47 self.markers = {} 48 self.arrows = {} 49 self.gaugeCont = Container(parent=self, name='gaugeCont', height=gaugeHeight, align=uiconst.TOTOP, clipChildren=True, state=uiconst.UI_DISABLED) 50 self.label = None 51 if labelTxt: 52 self.SetText(labelTxt) 53 self.subText = None 54 if subTxt: 55 self.SetSubText(subTxt) 56 57 def _SetValue(self, gauge, value, animate, duration = 0.6, timeOffset = 0.0): 58 if animate: 59 if self.IsHorizontalGauge(): 60 attrName = 'width' 61 currValue = gauge.width 62 else: 63 attrName = 'height' 64 currValue = gauge.height 65 uicore.animations.MorphScalar(gauge, attrName, currValue, value, duration=duration, curveSet=self._GetCorrectCurveSet(), timeOffset=timeOffset) 66 elif self.IsHorizontalGauge(): 67 gauge.width = value 68 else: 69 gauge.height = value 70 self.value = value 71 72 def _GetCorrectCurveSet(self): 73 simTimeCurveSet = uicore.animations.CreateCurveSet(useRealTime=self.useRealTime) 74 simTimeCurveSet.name = 'gaugeCurveSet' 75 return simTimeCurveSet 76 77 def ShowMarker(self, value, width = 1, color = Color.WHITE, **kwargs): 78 self.HideMarker(value) 79 marker = self._CreateMarker(color, value, width, **kwargs) 80 self.markers[value] = marker 81 82 def _CreateMarker(self, color, value, width, **kwargs): 83 marker = Fill(parent=self.gaugeCont, name='marker', color=color, align=uiconst.TOPLEFT_PROP, pos=(value, 84 0, 85 width, 86 self.gaugeCont.height), state=uiconst.UI_DISABLED, idx=0) 87 return marker 88 89 def ShowArrow(self, value): 90 self.HideArrow(value) 91 arrow = Sprite(parent=self, name='arrow', align=uiconst.TOPLEFT_PROP, pos=(value, 92 self.gaugeCont.height + 16, 93 6, 94 6), state=uiconst.UI_DISABLED, idx=0, texturePath='res:/UI/Texture/Shared/arrows/arrowRight.png', rotation=math.pi / 2) 95 self.arrows[value] = arrow 96 97 def ShowMarkers(self, values, width = 1, color = Color.WHITE): 98 for value in values: 99 self.ShowMarker(value, width, color) 100 101 def HideMarker(self, value): 102 if value in self.markers: 103 self.markers[value].Close() 104 self.markers.pop(value) 105 106 def HideArrow(self, value): 107 if value in self.arrows: 108 self.arrows[value].Close() 109 self.arrows.pop(value) 110 111 def HideAllArrows(self): 112 for arrow in self.arrows.values(): 113 arrow.Close() 114 115 self.arrows.clear() 116 117 def HideAllMarkers(self): 118 for marker in self.markers.values(): 119 marker.Close() 120 121 self.markers.clear() 122 123 def SetSubText(self, text): 124 if not self.subText: 125 self.subText = EveLabelSmall(parent=self, align=uiconst.TOTOP, state=uiconst.UI_DISABLED, maxLines=1, padTop=1) 126 self.subText.text = text 127 128 def SetText(self, text): 129 if not self.label: 130 self.label = self.labelClass(parent=self, align=uiconst.TOTOP, state=uiconst.UI_DISABLED, maxLines=1, padding=self.textPadding, idx=0) 131 self.label.text = text 132 133 def _CreateGradient(self, parent, color): 134 if len(color) == 3: 135 color = color + (1.0,) 136 return GradientSprite(align=uiconst.TOALL, parent=parent, rotation=-pi / 2, rgbData=[(0.0, color[:3])], alphaData=[(0, color[-1])], outputMode=OutputMode.COLOR_AND_GLOW, glowBrightness=self.glowBrightness) 137 138 def IsHorizontalGauge(self): 139 return self.gaugeAlign in (uiconst.TOLEFT_PROP, uiconst.TORIGHT_PROP) 140 141 142 class Gauge(_GaugeBase): 143 __guid__ = 'uicls.Gauge' 144 default_name = 'Gauge' 145 default_value = 0.0 146 default_backgroundColor = None 147 default_cyclic = False 148 default_color = Color.WHITE 149 150 def ApplyAttributes(self, attributes): 151 _GaugeBase.ApplyAttributes(self, attributes) 152 self.color = attributes.Get('color', self.default_color) 153 backgroundColor = attributes.Get('backgroundColor', self.default_backgroundColor) 154 self.value = attributes.Get('value', self.default_value) 155 self.cyclic = attributes.Get('cyclic', self.default_cyclic) 156 self.gauge = Container(parent=self.gaugeCont, name='gauge', align=self.gaugeAlign, clipChildren=True, width=0.0, state=uiconst.UI_DISABLED, values=(0, 0, 0, 0)) 157 self.gaugeGradient = self._CreateGradient(parent=self.gauge, color=self.color) 158 self.flashGradient = None 159 if backgroundColor is None: 160 backgroundColor = Color(*self.color).SetAlpha(0.2).SetSaturation(0.2).GetRGBA() 161 self._CreateBackgroundFill(backgroundColor) 162 self.SetValueInstantly(self.value) 163 164 def _CreateBackgroundFill(self, backgroundColor): 165 self.sr.backgroundFill = Fill(bgParent=self.gaugeCont, name='background', color=backgroundColor) 166 167 def SetGaugeAlign(self, align): 168 self.gauge.align = align 169 170 def HasValue(self, value): 171 return FloatCloseEnough(self.value, value, CLOSE_ENOUGH_TOLERANCE) 172 173 def SetValue(self, value, frequency = 10.0, animate = True, timeOffset = 0.0, duration = 0.6, flash = True): 174 if self.HasValue(value): 175 return 176 if self.cyclic and self.value > value: 177 self.SetValueInstantly(value) 178 else: 179 if animate and flash: 180 self.AnimFlash(value - self.value, timeOffset=timeOffset) 181 self._SetValue(self.gauge, value, animate, duration, timeOffset) 182 183 def SetValueTimed(self, value, duration, callback = None): 184 if self.IsHorizontalGauge(): 185 attrName = 'width' 186 self.value = self.gauge.width 187 else: 188 attrName = 'height' 189 self.value = self.gauge.height 190 uicore.animations.MorphScalar(self.gauge, attrName, self.value, value, duration=duration, curveType=uiconst.ANIM_LINEAR, callback=callback, curveSet=self._GetCorrectCurveSet()) 191 192 def SetValueText(self, text): 193 if getattr(self, 'valueText', None) is None: 194 self.valueText = EveLabelSmall(parent=self.gaugeCont, align=uiconst.CENTER, state=uiconst.UI_DISABLED, idx=0) 195 if self.valueText.text != text: 196 self.valueText.text = text 197 198 def SetColor(self, color, animDuration = None): 199 if color == self.color: 200 return 201 self.color = color 202 if self.gaugeGradient: 203 if animDuration: 204 uicore.animations.FadeOut(self.gaugeGradient, duration=animDuration, callback=self.gaugeGradient.Close) 205 else: 206 self.gaugeGradient.Close() 207 self.gaugeGradient = self._CreateGradient(self.gauge, color) 208 if animDuration: 209 uicore.animations.FadeTo(self.gaugeGradient, 0.0, self.gaugeGradient.opacity, duration=animDuration) 210 211 def SetBackgroundColor(self, color): 212 adjustedColor = Color(*color).SetAlpha(0.2).GetRGBA() 213 self.sr.backgroundFill.color = adjustedColor 214 215 def SetLabelColor(self, color): 216 if self.label is not None: 217 self.label.color = color 218 219 def SetValueInstantly(self, value): 220 self.value = value 221 if self.IsHorizontalGauge(): 222 self.gauge.width = value 223 else: 224 self.gauge.height = value 225 self.gauge.StopAnimations() 226 227 def AnimFlash(self, diff, duration = 1.6, timeOffset = 0.0): 228 uthread.new(self._AnimFlash, diff, duration, timeOffset) 229 230 def _AnimFlash(self, diff, duration, timeOffset): 231 w, h = self.gaugeCont.GetAbsoluteSize() 232 align = TOPLEFT if self.gauge.align == TOLEFT_PROP else TOPRIGHT 233 if not self.flashGradient: 234 self._CreateFlashGradient(align, h, w) 235 self.flashGradient.opacity = 1.0 236 if self.IsHorizontalGauge(): 237 if diff > 0: 238 self.flashGradient.rotation = 0 239 uicore.animations.MorphScalar(self.flashGradient, 'left', -w, w, duration - 0.4, timeOffset=timeOffset) 240 else: 241 self.flashGradient.rotation = pi 242 uicore.animations.MorphScalar(self.flashGradient, 'left', w, -w, duration - 0.4, timeOffset=timeOffset) 243 elif diff > 0: 244 self.flashGradient.rotation = -pi / 2 245 uicore.animations.MorphScalar(self.flashGradient, 'top', -h, h, duration - 0.4, timeOffset=timeOffset) 246 else: 247 self.flashGradient.rotation = pi / 2.0 248 uicore.animations.MorphScalar(self.flashGradient, 'top', h, -h, duration - 0.4, timeOffset=timeOffset) 249 uicore.animations.FadeOut(self.flashGradient, duration=duration, timeOffset=timeOffset) 250 251 def _CreateFlashGradient(self, align, h, w): 252 self.flashGradient = GradientSprite(parent=self.gauge, idx=0, name='flashGradient', align=align, width=w, height=h, rgbData=[(0, (1.0, 1.0, 1.0))], alphaData=[(0, 0.0), (0.9, 0.5), (1.0, 0.0)]) 253 254 def HideBackground(self): 255 self.sr.backgroundFill.Hide() 256 257 258 class GaugeThemeColored(Gauge): 259 260 def _CreateGradient(self, parent, color): 261 GradientThemeColored(align=uiconst.TOALL, colorType=uiconst.COLORTYPE_UIHILIGHT, parent=parent, rotation=-pi / 2, alphaData=[(0.0, 1.0)], rgbData=[(0, (1.0, 1.0, 1.0))]) 262 263 def _CreateBackgroundFill(self, *args): 264 self.sr.backgroundFill = FillThemeColored(bgParent=self.gaugeCont, colorType=uiconst.COLORTYPE_UIHILIGHT) 265 266 def _CreateFlashGradient(self, align, h, w): 267 self.flashGradient = GradientThemeColored(parent=self.gauge, colorType=uiconst.COLORTYPE_UIHILIGHTGLOW, idx=0, name='flashGradient', align=align, width=w, height=h, rgbData=[(0, (1.0, 1.0, 1.0))], alphaData=[(0, 0.0), (0.9, 0.5), (1.0, 0.0)]) 268 269 270 class GaugeMultiValue(_GaugeBase): 271 default_name = 'GaugeMultiValue' 272 default_backgroundColor = (1.0, 1.0, 1.0, 0.2) 273 default_colors = [] 274 275 def ApplyAttributes(self, attributes): 276 _GaugeBase.ApplyAttributes(self, attributes) 277 colors = attributes.Get('colors', self.default_colors) 278 values = attributes.Get('values', []) 279 backgroundColor = attributes.Get('backgroundColor', self.default_backgroundColor) 280 self.gauges = [] 281 self.gradients = [] 282 self._construct_color_gradients(colors) 283 Fill(bgParent=self.gaugeCont, name='background', color=backgroundColor) 284 self.UpdateValues(values) 285 286 def UpdateValues(self, values): 287 for gaugeNum, value in enumerate(values): 288 self.SetValue(gaugeNum, value, animate=False) 289 290 def _construct_color_gradients(self, colors): 291 numGauges = len(colors) 292 for gaugeNum in xrange(numGauges): 293 layer = Container(parent=self.gaugeCont, name='layer') 294 gauge = Container(parent=layer, name='gaugeCont%s' % gaugeNum, align=uiconst.TOLEFT_PROP) 295 self.gradients.append(self._CreateGradient(gauge, color=colors[gaugeNum])) 296 self.gauges.append(gauge) 297 298 def SetValue(self, gaugeNum, value, animate = True): 299 gauge = self.gauges[gaugeNum] 300 if animate: 301 self._SetValue(gauge, value, animate) 302 else: 303 gauge.StopAnimations() 304 gauge.width = value