hexCell.py (13220B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\shared\mapView\hexagonal\hexCell.py 2 from __future__ import absolute_import 3 import math 4 import random 5 from carbon.common.script.util.timerstuff import AutoTimer 6 from carbonui.primitives.base import Base, ReverseScaleDpi 7 from carbonui.primitives.container import Container 8 from carbonui.primitives.sprite import Sprite 9 from carbonui.primitives.vectorlinetrace import VectorLineTrace 10 from eve.client.script.ui.control.eveLabel import EveLabelSmall 11 import carbonui.const as uiconst 12 from eve.client.script.ui.shared.mapView.hexagonal.hexMap import HexMap 13 from . import hexUtil 14 from carbonui.uicore import uicore 15 16 class HexCell(Container): 17 default_pickRadius = -1 18 default_opacity = 1.0 19 default_align = uiconst.CENTER 20 default_state = uiconst.UI_PICKCHILDREN 21 isFlatTop = True 22 editMode = False 23 hexGrid = None 24 hexSize = 16.0 25 hexGridSize = 32.0 26 gridPosition = (0, 0) 27 outline = None 28 positionLabel = None 29 objectID = None 30 outlineColor = (1, 1, 1, 1) 31 subMap = None 32 lazyLoadThread = None 33 OnDragCallback = None 34 OnDragEndCallback = None 35 OnMouseDownCallback = None 36 OnMouseUpCallback = None 37 OnMouseWheelCallback = None 38 globalScaling = 1.0 39 localScaling = 1.0 40 mapsize = None 41 42 def ApplyAttributes(self, attributes): 43 Container.ApplyAttributes(self, attributes) 44 self.editMode = attributes.editMode 45 self.hexGrid = attributes.hexGrid 46 self.hexSize = attributes.hexSize or self.hexSize 47 self.hexGridSize = attributes.hexGridSize or self.hexGridSize 48 self.isFlatTop = attributes.isFlatTop or self.isFlatTop 49 self.sizeUnscaled = hexUtil.hex_slot_size(self.isFlatTop, self.hexSize) 50 self.width, self.height = self.sizeUnscaled 51 if settings.user.ui.Get('mapDebugShowIDs', 0): 52 self.positionLabel = EveLabelSmall(parent=self, align=uiconst.CENTERTOP) 53 self.backgroundSprite = Sprite(bgParent=self, texturePath='res:/UI/Texture/classes/HexMap/baseHexUnderlaySmall.png', color=self.hexGrid.mainColor) 54 if self.editMode: 55 self.state = uiconst.UI_NORMAL 56 57 def Close(self, *args): 58 Container.Close(self, *args) 59 self.hexGrid = None 60 self.OnMouseDownCallback = None 61 self.OnMouseUpCallback = None 62 self.OnMouseWheelCallback = None 63 64 def SetMapGridSize(self, mapsize): 65 self.mapsize = mapsize 66 67 def UpdateAlignment(self, budgetLeft = 0, budgetTop = 0, budgetWidth = 0, budgetHeight = 0, updateChildrenOnly = False): 68 ret = Container.UpdateAlignment(self, budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly) 69 return ret 70 71 def TraverseHexCell(self, viewportRect, scaling): 72 self.globalScaling = scaling 73 self._latestViewportData = viewportRect 74 w, h = self.sizeUnscaled 75 self.width = w * self.globalScaling * self.localScaling 76 self.height = h * self.globalScaling * self.localScaling 77 if self.gridPosition: 78 self.MoveToCR(*self.gridPosition) 79 vX, vY, vW, vH = viewportRect 80 l = vX + self.left - self.width / 2 + self.parent.width / 2 81 r = l + self.width 82 t = vY + self.top - self.height / 2 + self.parent.height / 2 83 b = t + self.height 84 if self.width < 50: 85 self.backgroundSprite.texturePath = 'res:/UI/Texture/classes/HexMap/baseHexUnderlaySmall.png' 86 elif self.width < 100: 87 self.backgroundSprite.texturePath = 'res:/UI/Texture/classes/HexMap/baseHexUnderlayMedium.png' 88 else: 89 self.backgroundSprite.texturePath = 'res:/UI/Texture/classes/HexMap/baseHexUnderlay.png' 90 if (r >= 0 and l <= vW or r >= vW and l <= 0) and (b >= 0 and t <= vH or b >= vH and t <= 0): 91 self.display = True 92 if self.height < self.hexSize / 2: 93 if self.positionLabel: 94 self.positionLabel.display = False 95 elif self.positionLabel: 96 self.positionLabel.display = True 97 if self.height >= 96: 98 if not self.editMode and not self.lazyLoadThread and not self.subMap: 99 self.lazyLoadThread = AutoTimer(50 + random.randint(1, 500), self.LoadContent) 100 if self.subMap: 101 width, height = hexUtil.hex_slot_size(self.isFlatTop, self.hexSize * 1000.0) 102 self.subMap.localScaling = height / float(self.subMap.sizeUnscaled[1]) / 1000.0 103 self.subMap.width = self.subMap.sizeUnscaled[0] * self.globalScaling * self.subMap.localScaling 104 self.subMap.height = self.subMap.sizeUnscaled[1] * self.globalScaling * self.subMap.localScaling 105 left = (self.parent.width - self.width) / 2 + self.left 106 top = (self.parent.height - self.height) / 2 + self.top 107 self.subMap.TraverseHexMap((vX + left, 108 vY + top, 109 vW, 110 vH), scaling=self.globalScaling * self.localScaling) 111 else: 112 self.UnloadContent() 113 else: 114 self.display = False 115 self.UnloadContent() 116 117 def GetMap(self): 118 return self.subMap 119 120 def LoadContent(self): 121 self.lazyLoadThread = None 122 if self.objectID is None or self.objectID not in uicore.mapObjectDataByID: 123 return 124 if not self.display or not self.parent: 125 return 126 if self.subMap is None: 127 from eve.client.script.ui.shared.mapView.hexagonal.hexMap import HexMap 128 self.subMap = HexMap(parent=self, parentMap=self.hexGrid, align=uiconst.CENTER, setMapSize=self.mapsize) 129 self.subMap.opacity = 0 130 self.subMap.LoadMapData(self.objectID) 131 self.subMap.state = uiconst.UI_DISABLED 132 self.state = uiconst.UI_DISABLED 133 width, height = hexUtil.hex_slot_size(self.isFlatTop, self.hexSize * 1000.0) 134 self.subMap.localScaling = height / float(self.subMap.sizeUnscaled[1]) / 1000.0 135 self.subMap.width = self.subMap.sizeUnscaled[0] * self.globalScaling * self.subMap.localScaling 136 self.subMap.height = self.subMap.sizeUnscaled[1] * self.globalScaling * self.subMap.localScaling 137 vX, vY, vW, vH = self._latestViewportData 138 left = (self.parent.width - self.width) / 2 + self.left 139 top = (self.parent.height - self.height) / 2 + self.top 140 self.subMap.TraverseHexMap((vX + left, 141 vY + top, 142 vW, 143 vH), scaling=self.globalScaling * self.localScaling) 144 uicore.animations.FadeTo(self.subMap, startVal=0.0, endVal=1.0) 145 self.subMap.display = True 146 147 def UnloadContent(self): 148 self.lazyLoadThread = None 149 if self.subMap: 150 self.subMap.Close() 151 self.subMap = None 152 153 def MoveToXYZ(self, x, y, z): 154 column, row = hexUtil.cube_to_axial_coordinate(x, y, z) 155 self.MoveToCR(column, row) 156 157 def OffsetCR(self, offsetColumn, offsetRow): 158 column, row = self.gridPosition 159 oddColumnOffset = offsetColumn & 1 160 if oddColumnOffset and not column & 1: 161 offsetRow -= 1 162 self.MoveToCR(column + offsetColumn, row + offsetRow) 163 164 def MoveToCR(self, column, row): 165 cX, cY = hexUtil.hex_slot_center_position(column, row, self.isFlatTop, self.hexGridSize) 166 self.left = cX * self.globalScaling * self.localScaling 167 self.top = cY * self.globalScaling * self.localScaling 168 self.gridPosition = (column, row) 169 if self.positionLabel: 170 parentID = uicore.mapObjectParentByID.get(self.objectID, None) 171 self.positionLabel.text = '%s<br>%s<br>%s/%s' % (self.objectID, 172 parentID, 173 column, 174 row) 175 176 def GetGridPosition(self): 177 return self.gridPosition 178 179 def GetCenterPosition(self): 180 return (self.left + self.width / 2, self.top + self.height / 2) 181 182 def GetPosition(self): 183 return (self.left, self.top) 184 185 def GetNeighborsInRange(self, startRange = 0, endRange = 1): 186 startpos = self.gridPosition 187 for i in xrange(startRange): 188 startpos = hexUtil.neighbour_axial(startpos, 4, self.isFlatTop) 189 190 ret = [] 191 for i in xrange(startRange, endRange): 192 startpos = hexUtil.neighbour_axial(startpos, 4, self.isFlatTop) 193 cr = startpos 194 for direction in xrange(6): 195 for length in xrange(i + 1): 196 cr = hexUtil.neighbour_axial(cr, direction, self.isFlatTop) 197 ret.append(cr) 198 199 return ret 200 201 def GetSlotNeighborsInDirection(self, column_row, directions = (0, 1, 2, 3, 4, 5), startRange = 0, endRange = 10): 202 ret = [] 203 for direction in directions: 204 cr = column_row 205 for i in xrange(startRange): 206 cr = hexUtil.neighbour_axial(cr, direction, self.isFlatTop) 207 208 for length in xrange(startRange, endRange): 209 cr = hexUtil.neighbour_axial(cr, direction, self.isFlatTop) 210 ret.append(cr) 211 212 return ret 213 214 def OnMouseDown(self, mouseButton, *args): 215 if self.editMode: 216 self.dragThread = AutoTimer(1, self.DragHexCell) 217 218 def OnMouseUp(self, mouseButton, *args): 219 if self.OnMouseUpCallback: 220 self.OnMouseUpCallback(mouseButton, self) 221 222 def OnMouseWheel(self, dz, *args): 223 if self.OnMouseWheelCallback: 224 self.OnMouseWheelCallback(dz, self) 225 226 def OnDblClick(self, *args): 227 if self.objectID and not self.editMode: 228 mapRoot = self.FindTopLevelMap() 229 if mapRoot: 230 mapRoot.LoadMapData(self.objectID) 231 232 def DragHexCell(self): 233 if not uicore.uilib.leftbtn: 234 self.dragThread = None 235 if self.OnDragEndCallback: 236 self.OnDragEndCallback(self) 237 return 238 pl, pt = self.parent.GetAbsolutePosition() 239 self.left = uicore.uilib.x - pl - self.parent.width / 2 240 self.top = uicore.uilib.y - pt - self.parent.height / 2 241 if self.OnDragCallback: 242 self.OnDragCallback() 243 244 def FindTopLevelMap(self): 245 check = self 246 while check: 247 if isinstance(check, HexMap) and not check.isChild: 248 return check 249 check = check.parent 250 251 def DrawOutline(self, margin = 0): 252 if self.outline: 253 outline = self.outline 254 else: 255 outline = VectorLineTrace(parent=self, lineWidth=1, idx=0) 256 outline.isLoop = True 257 self.outline = outline 258 outline.Flush() 259 self.cornerPoints = [] 260 colors = [(1, 0, 0, 1), 261 (1, 1, 1, 1), 262 (0, 1, 0, 1), 263 (1, 1, 1, 1), 264 (0, 0, 1, 1), 265 (1, 1, 1, 1)] 266 for i in xrange(6): 267 if self.isFlatTop: 268 outlineRad = self.displayWidth * 0.5 - margin 269 angle = 2.0 * math.pi / 6.0 * i 270 else: 271 outlineRad = self.displayHeight * 0.5 - margin 272 angle = 2.0 * math.pi / 6.0 * (i + 0.5) 273 x_i = ReverseScaleDpi(self.displayWidth * 0.5 + outlineRad * math.cos(angle)) 274 y_i = ReverseScaleDpi(self.displayHeight * 0.5 + outlineRad * math.sin(angle)) 275 outline.AddPoint((x_i, y_i), colors[i]) 276 self.cornerPoints.append((x_i, y_i)) 277 278 def Hilite(self): 279 self.outline.lineWidth = 3 280 281 @apply 282 def displayRect(): 283 fget = Base.displayRect.fget 284 285 def fset(self, value): 286 pW, pH = self._displayWidth, self._displayHeight 287 displayX, displayY, displayWidth, displayHeight = value 288 self._displayX = int(round(displayX)) 289 self._displayY = int(round(displayY)) 290 self._displayWidth = int(round(displayX + displayWidth)) - self._displayX 291 self._displayHeight = int(round(displayY + displayHeight)) - self._displayY 292 ro = self.renderObject 293 if ro: 294 ro.displayX = self._displayX 295 ro.displayY = self._displayY 296 ro.displayWidth = self._displayWidth 297 ro.displayHeight = self._displayHeight 298 if self._displayWidth != pW or self._displayHeight != pH: 299 if self._backgroundlist and len(self.background): 300 self.UpdateBackgrounds() 301 if self.outline: 302 self.DrawOutline() 303 304 return property(**locals()) 305 306 @apply 307 def position(): 308 doc = '(ui) position of hexMap' 309 310 def fget(self): 311 return (self._left, self._top) 312 313 def fset(self, value): 314 self.left = value[0] 315 self.top = value[1] 316 317 return property(**locals()) 318 319 @apply 320 def size(): 321 doc = '(ui) size of hexMap' 322 323 def fget(self): 324 return (self._width, self._height) 325 326 def fset(self, value): 327 self.width = value[0] 328 self.height = value[1] 329 330 return property(**locals())