gaugeCircular.py (11695B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\gaugeCircular.py 2 import math 3 import carbonui.const as uiconst 4 from carbon.common.script.util.mathCommon import FloatCloseEnough 5 from carbonui.primitives.container import Container 6 from carbonui.primitives.fill import Fill 7 from carbonui.primitives.sprite import Sprite 8 from carbonui.primitives.transform import Transform 9 from math import pi, cos, sin, atan2 10 from carbonui.uianimations import animations 11 from carbonui.util.color import Color 12 import geo2 13 import uthread 14 import blue 15 from carbonui.uicore import uicore 16 from eve.client.script.ui import eveColor 17 from eve.client.script.ui.control.donutSegment import DonutSegment 18 19 class GaugeCircular(Container): 20 default_name = 'GaugeCircular' 21 default_radius = 20 22 default_lineWidth = 3.0 23 default_align = uiconst.TOPLEFT 24 default_state = uiconst.UI_NORMAL 25 default_colorStart = (1.0, 1.0, 1.0, 1.0) 26 default_colorEnd = (0.8, 0.8, 0.8, 1.0) 27 default_colorBg = None 28 default_colorMarker = None 29 default_startAngle = -pi / 2 30 default_value = 0.0 31 default_callback = None 32 default_hoverMarkerEnabled = False 33 default_clockwise = True 34 default_bgPortion = 1.0 35 default_showMarker = True 36 default_autoUpdate = True 37 default_moveMarker = False 38 default_animateInRealTime = True 39 default_angle = 2 * pi 40 default_glow = False 41 default_useMiddleGauge = False 42 43 def ApplyAttributes(self, attributes): 44 Container.ApplyAttributes(self, attributes) 45 self.radius = attributes.Get('radius', self.default_radius) 46 self.lineWidth = attributes.Get('lineWidth', self.default_lineWidth) 47 self.startAngle = attributes.Get('startAngle', self.default_startAngle) 48 self.value = attributes.Get('value', self.default_value) 49 self.colorStart = attributes.Get('colorStart', self.default_colorStart) 50 self.colorEnd = attributes.Get('colorEnd', self.default_colorEnd) 51 self.colorBg = attributes.Get('colorBg', self.default_colorBg) 52 colorMarker = attributes.Get('colorMarker', self.default_colorMarker) 53 self.callback = attributes.Get('callback', self.default_callback) 54 self.isHoverMarkerEnabled = attributes.Get('hoverMarkerEnabled', self.default_hoverMarkerEnabled) 55 self.isClockwise = attributes.Get('clockwise', self.default_clockwise) 56 self.bgPortion = attributes.Get('bgPortion', self.default_bgPortion) 57 self.showMarker = attributes.get('showMarker', self.default_showMarker) 58 self.autoUpdate = attributes.get('autoUpdate', self.default_autoUpdate) 59 self.moveMarker = attributes.get('moveMarker', self.default_moveMarker) 60 self.useRealTime = attributes.get('useRealTime', self.default_animateInRealTime) 61 self.angle = attributes.get('angle', self.default_angle) 62 self.glow = attributes.get('glow', self.default_glow) 63 self.glowBrightness = attributes.get('glowBrightness', 0) 64 self.useMiddleGauge = attributes.get('useMiddleGauge', self.default_useMiddleGauge) 65 if colorMarker is None: 66 colorMarker = self.colorStart 67 self.width = self.height = self.radius * 2 68 self.pickRadius = self.radius + self.lineWidth 69 self._angle = 0.0 70 hoverMarkerSize = self.lineWidth * 1.7 71 self.hoverMarker = Sprite(name='hoverMarker', parent=self, color=colorMarker, width=hoverMarkerSize, height=hoverMarkerSize, state=uiconst.UI_DISABLED, opacity=0.0, texturePath='res:/UI/Texture/Classes/Gauge/circle.png') 72 if self.isClockwise: 73 rotation = -self.startAngle - pi / 2 74 else: 75 rotation = self.startAngle + pi / 2 76 state = uiconst.UI_DISABLED if self.showMarker else uiconst.UI_HIDDEN 77 self.markerTransform = Transform(name='markerTransform', parent=self, align=uiconst.TOALL, rotationCenter=(0.5, 0.5), rotation=rotation, state=state) 78 height = self.lineWidth + 5 79 self.marker = Fill(name='marker', parent=self.markerTransform, color=colorMarker, align=uiconst.CENTERTOP, pos=(0, 80 -(height - self.lineWidth), 81 2, 82 height)) 83 self.gauge = None 84 self.middleGauge = None 85 self.bgGauge = None 86 self.Reconstruct() 87 if self.autoUpdate: 88 uthread.new(self.UpdateThread) 89 90 def UpdateThread(self): 91 while not self.destroyed: 92 diff = self.gauge.end - self.value 93 diff = 3.0 * diff / blue.os.fps 94 self.gauge.end -= diff 95 blue.synchro.Yield() 96 97 def Reconstruct(self): 98 self._ConstructGauge() 99 if self.useMiddleGauge: 100 self._ConstructMiddleGauge() 101 self._ConstructBGGauge() 102 103 def _ConstructBGGauge(self): 104 if self.bgPortion is None: 105 return 106 if self.colorBg is None: 107 self.colorBg = Color(*self.colorStart).SetBrightness(0.2).GetRGBA() 108 if self.bgGauge: 109 self.bgGauge.Close() 110 self.bgGauge = self.ConstructLine(self.colorBg, self.colorBg) 111 self.bgGauge.end = self.bgPortion 112 self.bgGauge.name = 'backgroundLine' 113 114 def _ConstructMiddleGauge(self): 115 if self.middleGauge: 116 self.middleGauge.Close() 117 self.middleGauge = self.ConstructLine(eveColor.LEAFY_GREEN, eveColor.LEAFY_GREEN) 118 self.middleGauge.end = self.value 119 self.middleGauge.name = 'middleLine' 120 121 def _ConstructGauge(self): 122 if self.gauge: 123 self.gauge.Close() 124 self.gauge = self.ConstructLine(self.colorStart, self.colorEnd) 125 self.gauge.end = self.value 126 self.gauge.name = 'gaugeLine' 127 if self.glow: 128 self.gauge.outputMode = uiconst.OUTPUT_COLOR_AND_GLOW 129 self.gauge.glowBrightness = self.glowBrightness 130 131 def ConstructLine(self, colorStart, colorEnd): 132 return DonutSegment(parent=self, colorEnd=colorEnd, colorStart=colorStart, lineWidth=self.lineWidth, startAngle=self.startAngle, radius=self.radius, isClockwise=self.isClockwise, angle=self.angle) 133 134 def SetValue(self, value, animate = True): 135 if FloatCloseEnough(value, self.value): 136 return 137 if not animate: 138 self.gauge.end = value 139 self.value = value 140 if animate: 141 glow = self.glowBrightness 142 animations.MorphScalar(self.gauge, 'glowBrightness', glow, 4 * glow, curveType=uiconst.ANIM_WAVE, duration=1.0) 143 144 def SetMarkerValue(self, value): 145 self.markerTransform.rotation = self.GetOffsetMarkerValue(value) 146 147 def SetValueAndMarker(self, value, animated = True): 148 self.SetValue(value, animated) 149 self.SetMarkerValue(value) 150 151 def SetValueAndMarkerTimed(self, value, duration): 152 self.SetMarkerValueTimed(value, duration) 153 self.SetValueTimed(value, duration) 154 155 def SetValueTimed(self, value, duration = 0.3, timeOffset = 0.0): 156 uicore.animations.MorphScalar(self.gauge, 'end', self.gauge.end, value, duration=duration, curveType=uiconst.ANIM_LINEAR, curveSet=self._GetCorrectCurveSet(), timeOffset=timeOffset) 157 self.value = value 158 159 def SetValueWithMiddleGauge(self, value, lingerDurationSeconds, animationSpeedSeconds = 0.75, callback = None): 160 uicore.animations.MorphScalar(self.middleGauge, 'end', self.gauge.end, value, duration=animationSpeedSeconds, curveType=uiconst.ANIM_LINEAR, curveSet=self._GetCorrectCurveSet()) 161 uicore.animations.MorphScalar(self.gauge, 'end', self.gauge.end, value, duration=animationSpeedSeconds, curveType=uiconst.ANIM_LINEAR, curveSet=self._GetCorrectCurveSet(), timeOffset=lingerDurationSeconds, callback=callback) 162 self.value = value 163 164 def _GetCorrectCurveSet(self): 165 simTimeCurveSet = uicore.animations.CreateCurveSet(useRealTime=self.useRealTime) 166 simTimeCurveSet.name = 'gaugeCircularCurveSet' 167 return simTimeCurveSet 168 169 def SetMarkerValueTimed(self, value, duration): 170 newRotation = self.GetOffsetMarkerValue(value) 171 uicore.animations.MorphScalar(self.markerTransform, 'rotation', self.markerTransform.rotation, newRotation, duration=duration, curveType=uiconst.ANIM_LINEAR, curveSet=self._GetCorrectCurveSet()) 172 173 def StopGaugeAnimations(self): 174 self.gauge.StopAnimations() 175 self.markerTransform.StopAnimations() 176 177 def GetOffsetMarkerValue(self, value): 178 offSet = math.pi * 2 * (1.0 - value) 179 newRotation = math.pi * 1.5 - self.startAngle + offSet 180 return newRotation 181 182 def SetColor(self, colorStart, colorEnd = None): 183 self.colorStart = colorStart 184 self.colorEnd = colorEnd 185 self.gauge.SetColor(colorStart, colorEnd) 186 187 def SetColorMarker(self, colorMarker): 188 self.marker.SetRGBA(*colorMarker) 189 190 def SetColorBg(self, colorBg): 191 self.colorBg = colorBg 192 self._ConstructBGGauge() 193 194 def GetMousePositionAngle(self): 195 x, y = self.GetAbsolutePosition() 196 x = uicore.uilib.x - x - self.radius 197 y = uicore.uilib.y - y - self.radius 198 v1 = (x, y) 199 if geo2.Vec2Length(v1) < 5: 200 return self._angle 201 v2 = (0.0, 1.0) 202 dot = geo2.Vec2Dot(v1, v2) 203 cross = v1[0] * v2[1] - v1[1] * v2[0] 204 angle = atan2(dot, cross) 205 angle -= self.startAngle 206 if angle < 0: 207 angle += 2 * pi 208 self._angle = angle 209 return angle 210 211 def TriggerCallback(self): 212 if not self.callback: 213 return 214 angle = self.GetMousePositionAngle() 215 value = angle / (2.0 * pi) 216 self.callback(value) 217 218 def OnMouseDown(self, *args): 219 if not uicore.uilib.leftbtn: 220 return 221 self.TriggerCallback() 222 223 def OnMouseEnter(self, *args): 224 if not self.callback: 225 return 226 for obj in (self.gauge, self.bgGauge, self.marker): 227 uicore.animations.FadeTo(obj, obj.opacity, 1.5, duration=0.3) 228 229 if self.isHoverMarkerEnabled: 230 uicore.animations.FadeIn(self.hoverMarker, 1.0, duration=0.3) 231 232 def OnMouseExit(self, *args): 233 if not self.callback: 234 return 235 for obj in (self.gauge, self.bgGauge, self.marker): 236 uicore.animations.FadeTo(obj, obj.opacity, 1.0, duration=0.6) 237 238 if self.isHoverMarkerEnabled: 239 uicore.animations.FadeOut(self.hoverMarker, duration=0.3) 240 currAngle = self.hoverMarkerAngle 241 endAngle = 2 * pi if currAngle > pi else 0.0 242 uicore.animations.MorphScalar(self, 'hoverMarkerAngle', currAngle, endAngle, duration=0.3) 243 244 def OnMouseMove(self, *args): 245 if uicore.uilib.leftbtn: 246 self.TriggerCallback() 247 if not self.isHoverMarkerEnabled: 248 return 249 self.hoverMarkerAngle = self.GetMousePositionAngle() 250 251 def GetHoverMarkerAngle(self): 252 if not self.isHoverMarkerEnabled: 253 return 0.0 254 return self.GetMousePositionAngle() 255 256 def SetHoverMarkerAngle(self, angle): 257 angle += self.startAngle 258 w = self.hoverMarker.height / 2.0 259 r = self.radius + self.lineWidth 260 x = (r + w) * cos(angle) 261 y = (r + w) * sin(angle) 262 offset = r - w - self.lineWidth / 2.0 263 self.hoverMarker.left = x + offset 264 self.hoverMarker.top = y + offset 265 266 hoverMarkerAngle = property(GetHoverMarkerAngle, SetHoverMarkerAngle) 267 268 def EnableHoverMarker(self): 269 self.isHoverMarkerEnabled = True 270 271 def DisableHoverMarker(self): 272 self.isHoverMarkerEnabled = False 273 274 def SetRadius(self, radius): 275 self.radius = radius 276 self.width = self.height = radius * 2 277 self.Reconstruct()