/
__init__.py
449 lines (392 loc) · 18.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import math
import random
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui, QtCore
from PyQt4.QtOpenGL import *
from Ui_mainForm import *
from GameBoard import *
from ObjectSimpleSquare import *
class PyHC(Ui_MainWindow):
''' Example class for using SpiralWidget'''
def __init__(self, ui, MainWindow):
self.mousePosition = [0, 0]
self.MainWindow = MainWindow
self.ui = ui
ui.glFrame.setTitle( " " )
self.board = GameBoard(MainWindow, ui)
ui.vLayout.addWidget(self.board)
ui.gameLog.keyPressEvent = self.test
MainWindow.keyPressEvent = self.test
self.board.wheelEvent = self.zoomMap
self.board.mouseMoveEvent = self.moveMap
self.board.mouseDoubleClickEvent = self.doubleClickMap
self.board.mousePressEvent = self.clickMap
self.board.mouseReleaseEvent = self.stopMoving
self.lastX = False
self.lastY = False
ui.cmdHeal.mousePressEvent = self.healClix
ui.cmdDamage.mousePressEvent = self.damageClix
ui.cmdTokenRed.mousePressEvent = self.tokenRed
ui.cmdTokenBlue.mousePressEvent = self.tokenBlue
ui.rollOne.mousePressEvent = self.rollOne
ui.rollTwo.mousePressEvent = self.rollTwo
ui.cmdBarrier.mousePressEvent = self.barrier
ui.cmdSmokeCloud.mousePressEvent = self.smokeCloud
ui.cmdDebris.mousePressEvent = self.debris
ui.cmdSpecial.mousePressEvent = self.special
ui.cmdLOF.mousePressEvent = self.changeLOF
ui.cmdObj.mousePressEvent = self.changeObjectMode
self.myActiveClix = False
self.theirActiveClix = False
self.activeObject = False
ui.myDial.setMouseTracking(True)
ui.myDial.mouseMoveEvent = self.hoverMyDial
def getMyActiveClix(self):
c = self.myActiveClix
if c:
x, y = c
f = self.board.boardFigures[x][y]
if f:
return f
return False
def changeLOF(self, event):
if not self.ui.cmdLOF.isChecked():
self.board.modeLOF = True
self.clearOtherToggles( self.ui.cmdLOF )
else:
self.board.modeLOF = False
self.ui.glFrame.setTitle( " " )
self.board.moving = False
self.board.updateGL()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdLOF, event)
def changeObjectMode(self, event):
if not self.ui.cmdObj.isChecked():
self.board.modeObject = True
self.clearOtherToggles( self.ui.cmdObj )
else:
self.board.modeObject = False
self.ui.glFrame.setTitle( " " )
self.board.moving = False
self.board.updateGL()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdObj, event)
def rollOne(self, event):
v = random.randint( 1, 6 )
self.board.log( "Me", "Rolled a "+str(v)+" on one die.")
return QtGui.QPushButton.mousePressEvent(self.ui.rollOne, event)
def rollTwo(self, event):
v = random.randint( 2, 12 )
s = "Rolled a "+str(v)+" on two dice."
if v == 2:
s += " <b>Crit miss!</b>"
elif v == 12:
s += " <b>Critical hit!</b>"
self.board.log( "Me", s )
return QtGui.QPushButton.mousePressEvent(self.ui.rollTwo, event)
def healClix(self, event):
f = self.getMyActiveClix()
if f:
f.heal()
self.redrawMyDial()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdHeal, event)
def damageClix(self, event):
f = self.getMyActiveClix()
if f:
f.damage()
self.redrawMyDial()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdDamage, event)
def tokenRed(self, event):
f = self.getMyActiveClix()
if f:
f.token( "red" )
self.board.updateGL()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdTokenRed, event)
def tokenBlue(self, event):
f = self.getMyActiveClix()
if f:
f.token( "blue" )
self.board.updateGL()
return QtGui.QPushButton.mousePressEvent(self.ui.cmdTokenBlue, event)
def getBoardXY(self, event):
v = self.board.viewport()
winY = v[3] - event.y()
winX = event.x()
winZ = self.board.getMouseZ(winX, winY)
posX, posY, posZ = gluUnProject(winX, winY, winZ)
x = int(math.ceil(posX)+11)
y = int(math.ceil(posZ)+11)
return [x, y]
def hoverMyDial(self, event):
if self.myActiveClix:
f = self.board.boardFigures[self.myActiveClix[0]][self.myActiveClix[1]]
if f:
t = f.getToolTip(event.pos())
if t:
QtGui.QToolTip.showText( event.globalPos(), "<p>"+t+"</p>", self.ui.myDial )
else:
QtGui.QToolTip.hideText()
def clickMap(self, event):
x, y = self.getBoardXY(event)
if ( x >= 0 and x <= 23 and y >= 0 and y <= 23 ):
if event.button() == QtCore.Qt.RightButton and self.board.boardObjects[x][y]:
#Picking up an object
o = self.board.boardObjects[x][y]
f = False
if self.myActiveClix:
f = self.board.boardFigures[self.myActiveClix[0]][self.myActiveClix[1]]
if o and o.canBePickedUp() and f:
b = f.takeObject(o)
if b:
self.board.updateGL()
elif isinstance(o, ObjectSimpleSquare):
if o.type == "barrier":
o.type = "debris"
if f:
self.board.log( "Me", f.getName() + " destroys a square of blocking terrain." )
else:
self.board.log( "Me", "Destroyed a square of blocking terrain.")
else:
self.board.boardObjects[x][y] = False
self.board.log( "Me", "Removed a square of "+o.type+" terrain.")
self.board.updateGL()
elif event.button() == QtCore.Qt.RightButton and self.board.boardFigures[x][y] and self.board.boardFigures[x][y].hasObject() and self.board.boardFigures[x][y].mine:
#Dropping an object
f = self.board.boardFigures[x][y]
f.dropObject()
self.myActiveClix = (x, y)
self.board.updateGL()
elif (self.ui.cmdBarrier.isChecked() or self.ui.cmdSmokeCloud.isChecked() or self.ui.cmdDebris.isChecked() or self.ui.cmdSpecial.isChecked()) and not self.board.boardObjects[x][y]:
if self.ui.cmdBarrier.isChecked():
type = "barrier"
elif self.ui.cmdSmokeCloud.isChecked():
type = "smoke cloud"
elif self.ui.cmdDebris.isChecked():
type = "debris"
elif self.ui.cmdSpecial.isChecked():
type = "special"
b = ObjectSimpleSquare(self.board, type)
self.board.boardObjects[x][y] = b
b.x = x
b.y = y
self.board.log( "Me", "Placed a "+type+" token." )
self.board.updateGL()
else:
if ( self.board.boardFigures[x][y] and not self.board.modeObject ):
f = self.board.boardFigures[x][y]
self.board.moving = True
self.board.moveOriginX = self.board.moveDestinationX = x
self.board.moveOriginY = self.board.moveDestinationY = y
#self.board.clearMyActive()
if f.mine:
self.board.clearActive(1)
self.myActiveClix = (x, y)
f.active = 1
self.redrawMyDial()
else:
self.board.clearActive(2)
self.theirActiveClix = (x, y)
f.active = 2
self.redrawTheirDial()
elif self.board.boardObjects[x][y]:
self.board.moving = True
self.board.modeObject = True
self.board.moveOriginX = self.board.moveDestinationX = x
self.board.moveOriginY = self.board.moveDestinationY = y
self.activeObject = (x, y)
self.redrawMyDial()
elif self.ui.cmdLOF.isChecked():
self.board.moving = True
self.board.moveOriginX = self.board.moveDestinationX = x
self.board.moveOriginY = self.board.moveDestinationY = y
def redrawMyDial(self):
if self.board.modeObject:
f = self.board.boardObjects[self.activeObject[0]][self.activeObject[1]]
else:
f = self.board.boardFigures[self.myActiveClix[0]][self.myActiveClix[1]]
if f:
png = f.drawDial()
if png:
pixmap = QtGui.QPixmap()
pixmap.loadFromData(png, "PNG")
ui.myDial.setPixmap( pixmap )
ui.myDial.update()
def redrawTheirDial(self):
f = self.board.boardFigures[self.theirActiveClix[0]][self.theirActiveClix[1]]
pixmap = QtGui.QPixmap()
pixmap.loadFromData(f.drawDial(), "PNG")
ui.theirDial.setPixmap( pixmap )
ui.theirDial.update()
def doubleClickMap(self, event):
x, y = self.getBoardXY(event)
#print "X %s Y %s aX %s aY %s vD %s Xoffset %s Yoffset %s Zoffset %s" % ( x, y, self.board.view_angle_x, self.board.view_angle_y, self.board.view_distance, self.board.x_pos, self.board.y_pos, self.board.z_pos )
# if ( x >= 0 and x <= 23 and y >= 0 and y <= 23 ):
# f = self.board.boardFigures[x][y]
# if f:
# f.damage()
# self.redrawMyDial()
return
def moveMap(self, event):
x, y = self.getBoardXY(event)
if ( x >= 0 and x <= 23 and y >= 0 and y <= 23 ):
if self.board.moving and ( (self.lastX == False and self.lastY == False ) or (self.lastX != x or self.lastY != y ) ) and event.x() > 0 and event.y() > 0:
self.board.moveDestinationX = x
self.board.moveDestinationY = y
self.lastX = x
self.lastY = y
if self.board.modeObject:
f = self.board.boardObjects[self.board.moveOriginX][self.board.moveOriginY]
else:
f = self.board.boardFigures[self.board.moveOriginX][self.board.moveOriginY]
delta = abs( x - self.board.moveOriginX )
dY = abs( y - self.board.moveOriginY )
if dY > delta:
delta = dY
if self.board.modeLOF:
self.ui.glFrame.setTitle( "LOF "+str(delta)+" space(s)..." )
else:
self.ui.glFrame.setTitle( "Moving "+f.getName()+" "+str(delta)+" space(s)..." )
#print self.board.moveDestinationX, self.board.moveDestinationY
self.board.updateGL()
def stopMoving(self, event):
if self.board.moving and not self.ui.cmdLOF.isChecked():
x, y = self.getBoardXY(event)
doMove = True
if not self.board.modeObject:
f = self.board.boardFigures[self.board.moveOriginX][self.board.moveOriginY]
if self.board.boardFigures[self.board.moveDestinationX][self.board.moveDestinationY]:
doMove = False
if f and not f.mine:
doMove = False
else:
if self.board.boardObjects[self.board.moveDestinationX][self.board.moveDestinationY]:
doMove = False
f = self.board.boardObjects[self.board.moveOriginX][self.board.moveOriginY]
if doMove:
f.x = self.board.moveDestinationX
f.y = self.board.moveDestinationY
if f and f.mine:
self.myActiveClix = (self.board.moveDestinationX, self.board.moveDestinationY)
self.board.moveSelectedFigure()
self.ui.glFrame.setTitle( " " )
self.board.moving = False
if self.board.modeObject:
if not self.ui.cmdObj.isChecked():
self.board.modeObject = False
self.board.updateGL()
def zoomMap(self, event):
if (event.orientation() == QtCore.Qt.Vertical):
#new_distance = self.board.view_distance + (float(event.delta()) / 8 / 15)
#if ( new_distance > 0.05 and new_distance < 1 ):
# self.board.view_distance = new_distance
# self.board.updateGL()
new_distance = self.board.z_pos + float(event.delta()) / 8
if new_distance >= 0 and new_distance <= 40:
self.board.z_pos = new_distance
self.board.updateGL()
return QtGui.QMainWindow.wheelEvent(self.MainWindow, event)
def test(self,event):
#print event.key()
if event.key() == QtCore.Qt.Key_Up and self.board.view_angle_x < 90:
self.board.view_angle_x += 1.0
self.board.updateGL()
elif event.key() == QtCore.Qt.Key_Down and self.board.view_angle_x > 20:
self.board.view_angle_x -= 1.0
self.board.updateGL()
elif event.key() == QtCore.Qt.Key_Left:
self.board.view_angle_y += 1.0
self.board.updateGL()
elif event.key() == QtCore.Qt.Key_Right:
self.board.view_angle_y -= 1.0
self.board.updateGL()
elif event.key() == 87:
self.board.y_pos += 1.0
self.board.updateGL()
elif event.key() == 83:
self.board.y_pos -= 1.0
self.board.updateGL()
elif event.key() == 65:
self.board.x_pos -= 1.0
self.board.updateGL()
elif event.key() == 68:
self.board.x_pos += 1.0
self.board.updateGL()
elif event.key() == 81:
self.board.z_pos -= 1.0
self.board.updateGL()
elif event.key() == 69:
self.board.z_pos += 1.0
self.board.updateGL()
elif event.key() == 48:
self.board.z_pos = 0.0
self.board.y_pos = 0.0
self.board.x_pos = 0.0
self.board.view_angle_x = 90
self.board.view_angle_y = 0
self.board.view_distance = 1
self.board.updateGL()
'''
if keystate[K_UP] and view_angle_x < 90: view_angle_x += 1.0
if keystate[K_DOWN] and view_angle_x > 0: view_angle_x -= 1.0
if keystate[K_LEFT]: view_angle_y += 1.0
if keystate[K_RIGHT]: view_angle_y -= 1.0
if keystate[K_PAGEUP] and view_distance < 2.0: view_distance += .03
if keystate[K_PAGEDOWN] and view_distance > 0.5: view_distance -= .03
if keystate[K_END]: view_distance = 1.0; view_angle_y = 0.0; view_angle_x = 90.0
'''
else:
return QtGui.QMainWindow.keyPressEvent(self.MainWindow, event)
def barrier(self, event):
if not self.ui.cmdBarrier.isChecked():
self.clearOtherToggles( self.ui.cmdBarrier )
self.ui.glFrame.setTitle( "Click a square with no figure in it to add a barrier token" )
else:
self.ui.glFrame.setTitle( " " )
return QtGui.QPushButton.mousePressEvent(self.ui.cmdBarrier, event)
def smokeCloud(self, event):
if not self.ui.cmdSmokeCloud.isChecked():
self.clearOtherToggles( self.ui.cmdSmokeCloud )
self.ui.glFrame.setTitle( "Click a square with no figure in it to add a smoke cloud token" )
else:
self.ui.glFrame.setTitle( " " )
return QtGui.QPushButton.mousePressEvent(self.ui.cmdSmokeCloud, event)
def debris(self, event):
if not self.ui.cmdDebris.isChecked():
self.clearOtherToggles( self.ui.cmdDebris )
self.ui.glFrame.setTitle( "Click a square with no figure in it to add a debris token" )
else:
self.ui.glFrame.setTitle( " " )
return QtGui.QPushButton.mousePressEvent(self.ui.cmdDebris, event)
def special(self, event):
if not self.ui.cmdSpecial.isChecked():
self.clearOtherToggles( self.ui.cmdSpecial )
self.ui.glFrame.setTitle( "Click a square with no figure in it to add a special token" )
else:
self.ui.glFrame.setTitle( " " )
return QtGui.QPushButton.mousePressEvent(self.ui.cmdSpecial, event)
#print self.board.x_pos, self.board.y_pos, self.board.z_pos, self.board.view_angle_x, self.board.view_angle_y, self.board.view_distance
def clearOtherToggles(self, button):
if button != self.ui.cmdBarrier:
self.ui.cmdBarrier.setChecked( False )
if button != self.ui.cmdSmokeCloud:
self.ui.cmdSmokeCloud.setChecked( False )
if button != self.ui.cmdDebris:
self.ui.cmdDebris.setChecked( False )
if button != self.ui.cmdSpecial:
self.ui.cmdSpecial.setChecked( False )
if button != self.ui.cmdLOF:
self.ui.cmdLOF.setChecked( False )
self.board.modeLOF = False
self.board.moving = False
self.board.updateGL()
if button != self.ui.cmdObj:
self.ui.cmdObj.setChecked( False )
self.board.modeObject = False
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
p = PyHC(ui, MainWindow)
sys.exit(app.exec_())