clickableboxbar.py (10422B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\clickableboxbar.py 2 import localization 3 from carbonui import uiconst 4 from carbonui.primitives.container import Container 5 from carbonui.primitives.fill import Fill 6 7 class ClickableBoxBar(Container): 8 __guid__ = 'uicls.ClickableBoxBar' 9 __nonpersistvars__ = ['boxes', 10 'boxValues', 11 'value', 12 'boxWidth', 13 'boxHeight', 14 'boxMargin', 15 'boxSpacing', 16 'minimumIndex', 17 'maximumIndex', 18 'isReadOnly', 19 'colors', 20 'hintFormat'] 21 COLOR_BELOWMINIMUM = 0 22 COLOR_SELECTED = 1 23 COLOR_UNSELECTED = 2 24 COLOR_ABOVEMAXIMUM = 3 25 default_name = 'ClickableBoxBar' 26 27 def ApplyAttributes(self, attributes): 28 Container.ApplyAttributes(self, attributes) 29 self.isTabStop = 0 30 self.boxes = [] 31 self.value = None 32 self.minimumIndex = None 33 self.maximumIndex = None 34 self.isReadOnly = False 35 self.colors = {} 36 self.hintFormat = None 37 numBoxes = attributes.get('numBoxes') 38 boxValues = attributes.get('boxValues', []) 39 initialValue = attributes.get('initialValue') 40 boxWidth = attributes.get('boxWidth', 20) 41 boxHeight = attributes.get('boxHeight', 20) 42 boxMargin = attributes.get('boxMargin', 2) 43 boxSpacing = attributes.get('boxSpacing', 1) 44 minimumValue = attributes.get('minimumValue') 45 maximumValue = attributes.get('maximumValue') 46 readonly = attributes.get('readonly', False) 47 colorDict = attributes.get('colorDict') 48 backgroundColor = attributes.get('backgroundColor') 49 hintFormat = attributes.get('hintFormat') 50 subHint = attributes.get('subHint') 51 aboveMaxHint = attributes.get('aboveMaxHint') 52 belowMaxHint = attributes.get('belowMaxHint') 53 if boxValues is not None and len(boxValues) > 0 and len(boxValues) != numBoxes: 54 raise RuntimeError('Unable to create a ClickableBoxBar with a defined boxValues array of a different size than the number of boxes') 55 if boxValues is None or len(boxValues) == 0: 56 self.boxValues = range(1, numBoxes + 1) 57 else: 58 self.boxValues = boxValues 59 if initialValue is not None and initialValue not in self.boxValues: 60 raise RuntimeError('Unable to create a ClickableBoxBar with an initialValue outside its range of values') 61 if minimumValue is not None and minimumValue - 1 not in self.boxValues: 62 raise RuntimeError('Unable to create a ClickableBoxBar with a minimumValue outside its range of values') 63 self.boxWidth = boxWidth 64 self.boxHeight = boxHeight 65 self.boxMargin = boxMargin 66 self.boxSpacing = boxSpacing 67 self.isReadOnly = readonly 68 self.colors = colorDict 69 self.hintFormat = hintFormat 70 self.aboveMaxHint = aboveMaxHint 71 self.belowMaxHint = belowMaxHint 72 if colorDict is None: 73 self.colors = {self.COLOR_BELOWMINIMUM: (0.95, 0.95, 0.95, 0.75), 74 self.COLOR_SELECTED: (0.5, 1.0, 0.5, 0.75), 75 self.COLOR_UNSELECTED: (0.4, 0.4, 0.4, 0.75), 76 self.COLOR_ABOVEMAXIMUM: (0.1, 0.1, 0.1, 0.75)} 77 if minimumValue is not None: 78 if minimumValue in self.boxValues: 79 self.minimumIndex = self.boxValues.index(minimumValue) 80 else: 81 self.minimumIndex = len(boxValues) 82 if maximumValue is not None: 83 self.maximumIndex = self.boxValues.index(maximumValue) 84 i = 0 85 while i < numBoxes: 86 clickable = not self.isReadOnly 87 initialState = self.COLOR_UNSELECTED 88 if self.minimumIndex is not None: 89 clickable = clickable and i >= self.minimumIndex 90 if i < self.minimumIndex: 91 initialState = self.COLOR_BELOWMINIMUM 92 if self.maximumIndex is not None: 93 clickable = clickable and i <= self.maximumIndex 94 if initialState == self.COLOR_UNSELECTED and i > self.maximumIndex: 95 initialState = self.COLOR_ABOVEMAXIMUM 96 self.boxes.append(self.CreateBox(i, self.colors, clickable, initialState, subHint)) 97 i += 1 98 99 bgColor = backgroundColor 100 if backgroundColor is None: 101 bgColor = (0.0, 0.0, 0.0, 0.9) 102 Fill(parent=self, color=bgColor) 103 if initialValue is not None: 104 self.SetValue(initialValue) 105 106 def Increment(self): 107 if self.value not in self.boxValues: 108 idx = -1 109 else: 110 idx = self.boxValues.index(self.value) 111 if idx + 1 >= len(self.boxValues) or self.maximumIndex is not None and idx + 1 > self.maximumIndex: 112 return False 113 self._OnBoxClicked(idx + 1) 114 return True 115 116 def Decrement(self): 117 if self.value not in self.boxValues: 118 return False 119 idx = self.boxValues.index(self.value) 120 if idx == 0: 121 oldValue = self.value 122 self.value = 0 123 for x in xrange(0, len(self.boxValues)): 124 self.boxes[x].SetColor(self.COLOR_UNSELECTED) 125 126 self.OnValueChanged(oldValue, self.value) 127 return True 128 if idx - 1 < 0 or self.minimumIndex is not None and idx - 1 < self.minimumIndex: 129 return False 130 self._OnBoxClicked(idx - 1) 131 return True 132 133 def GetValue(self): 134 return self.value 135 136 def SetValue(self, value): 137 if value == self.value: 138 return 139 if value not in self.boxValues: 140 self.value = 0 141 for x in xrange(0, len(self.boxValues)): 142 self.boxes[x].SetColor(self.COLOR_UNSELECTED) 143 144 return 145 idx = self.boxValues.index(value) 146 self._OnBoxClicked(idx) 147 148 def CreateBox(self, boxIndex, colors, clickable, initialState, subHint): 149 boxLeft = self.boxMargin + boxIndex * (self.boxSpacing + self.boxWidth) 150 newBox = ClickableBoxBarBox(name='box', parent=self, align=uiconst.TOPLEFT, left=boxLeft, top=self.boxMargin, height=self.boxHeight, width=self.boxWidth, state=uiconst.UI_NORMAL) 151 newBox.Startup(boxIndex, defaultColor=colors[initialState], stateColors=colors, clickable=clickable, subHint=subHint) 152 return newBox 153 154 def _OnBoxClicked(self, boxIndex): 155 if self.value is not None and not self.OnAttemptBoxClicked(self.value, self.boxValues[boxIndex]): 156 return 157 startIdx = 0 if self.minimumIndex is None else self.minimumIndex 158 finishIdx = self.maximumIndex + 1 if self.maximumIndex is not None else len(self.boxValues) 159 if self.minimumIndex is not None: 160 for x in xrange(0, min(startIdx, boxIndex + 1)): 161 self.boxes[x].SetColor(self.COLOR_BELOWMINIMUM) 162 163 for x in xrange(startIdx, boxIndex + 1): 164 self.boxes[x].SetColor(self.COLOR_SELECTED) 165 166 for x in xrange(boxIndex + 1, finishIdx): 167 self.boxes[x].SetColor(self.COLOR_UNSELECTED) 168 169 oldValue = self.value 170 self.value = self.boxValues[boxIndex] 171 self.OnBoxClicked(boxIndex, self.value) 172 if oldValue != self.value: 173 self.OnValueChanged(oldValue, self.value) 174 175 def _OnBoxDoubleClicked(self, boxIndex): 176 self.OnBoxDoubleClicked(boxIndex, self.boxValues[boxIndex]) 177 178 def _OnClose(self, *args): 179 self.boxes = None 180 self.boxValues = None 181 182 def OnBoxClicked(self, boxIndex, boxValue): 183 pass 184 185 def OnBoxDoubleClicked(self, boxIndex, boxValue): 186 pass 187 188 def OnValueChanged(self, oldValue, newValue): 189 pass 190 191 def OnAttemptBoxClicked(self, oldValue, newValue): 192 return True 193 194 def GetHint(self, box, *args): 195 hintText = '' 196 if self.hintFormat is not None: 197 hintText = localization.GetByLabel(self.hintFormat, value=self.value) 198 if box.identifier < self.minimumIndex and self.belowMaxHint is not None: 199 hintText += self.belowMaxHint % self.GetIndexValue(box.identifier) 200 if box.identifier > self.maximumIndex and self.aboveMaxHint is not None: 201 hintText += self.aboveMaxHint % self.GetIndexValue(box.identifier) 202 if hintText == '': 203 return 204 return hintText 205 206 def GetIndexValue(self, index): 207 if index < 0 or index >= len(self.boxValues): 208 return None 209 else: 210 return self.boxValues[index] 211 212 213 class ClickableBoxBarBox(Container): 214 __guid__ = 'uicls.ClickableBoxBarBox' 215 __nonpersistvars__ = ['identifier', 216 'defaultColor', 217 'colors', 218 'fill', 219 'subHint'] 220 identifier = None 221 defaultColor = None 222 colors = None 223 fill = None 224 subHint = None 225 226 def Startup(self, boxIdentifier, defaultColor = (1.0, 1.0, 1.0, 0.5), stateColors = {}, clickable = True, subHint = None): 227 self.identifier = boxIdentifier 228 self.defaultColor = defaultColor 229 self.colors = stateColors 230 self.clickable = clickable 231 self.subHint = subHint 232 self.SetupUIElements() 233 234 def SetupUIElements(self): 235 self.sr.highlight = Fill(parent=self, color=(1.0, 1.0, 1.0, 0.4)) 236 self.sr.highlight.state = uiconst.UI_HIDDEN 237 self.fill = Fill(parent=self, color=self.defaultColor) 238 239 def SetColor(self, state): 240 if state in self.colors: 241 newColor = self.colors[state] 242 else: 243 newColor = self.defaultColor 244 self.fill.SetRGBA(*newColor) 245 246 def OnClick(self, *args): 247 if self.clickable: 248 self.parent._OnBoxClicked(self.identifier) 249 250 def OnDoubleClick(self, *args): 251 if self.clickable: 252 self.parent._OnDoubleClick(self.identifier) 253 254 def OnMouseEnter(self, *args): 255 if self.clickable: 256 self.sr.highlight.state = uiconst.UI_DISABLED 257 258 def OnMouseExit(self, *args): 259 if self.clickable: 260 self.sr.highlight.state = uiconst.UI_HIDDEN 261 262 def GetHint(self, *args): 263 if self.subHint is not None and self.clickable: 264 hintText = self.parent.GetHint(self, *args) 265 if hintText is None: 266 hintText = '' 267 hintText += self.subHint % self.parent.GetIndexValue(self.identifier) 268 return hintText 269 else: 270 return self.parent.GetHint(self, *args)