/
PickEntrySide.py
executable file
·336 lines (291 loc) · 11.1 KB
/
PickEntrySide.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
#!/usr/bin/env python3
from __future__ import annotations
import logging
import math
from sys import maxsize
from typing import Any, Optional, Set, Tuple
import cairo
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, GObject, Gtk
from twisted.internet import gtk3reactor
try:
gtk3reactor.install() # type: ignore
except AssertionError:
pass
from twisted.internet import defer, reactor
from slugathon.game import BattleMap, MasterBoard, MasterHex
from slugathon.gui import GUIBattleHex, icon
from slugathon.util import guiutils, prefs
__copyright__ = "Copyright (c) 2005-2021 David Ripton"
__license__ = "GNU GPL v2"
SQRT3 = math.sqrt(3.0)
hexlabel_to_entry_side = {
"F1": 1,
"F2": 1,
"F3": 1,
"F4": 1,
"A3": 3,
"B4": 3,
"C5": 3,
"D6": 3,
"A1": 5,
"B1": 5,
"C1": 5,
"D1": 5,
}
def new(
board: MasterBoard.MasterBoard,
masterhex: MasterHex.MasterHex,
entry_sides: Set[int],
parent: Gtk.Window,
playername: Optional[str] = None,
scale: Optional[int] = None,
) -> Tuple[PickEntrySide, defer.Deferred]:
"""Create a PickEntrySide dialog and return it and a Deferred."""
def1 = defer.Deferred() # type: defer.Deferred
pick_entry_side = PickEntrySide(
board, masterhex, entry_sides, def1, parent, playername, scale
)
return pick_entry_side, def1
class PickEntrySide(Gtk.Dialog):
"""Dialog to pick a masterhex entry side."""
def __init__(
self,
board: MasterBoard.MasterBoard,
masterhex: MasterHex.MasterHex,
entry_sides: Set[int],
def1: defer.Deferred,
parent: Gtk.Window,
playername: Optional[str] = None,
scale: Optional[int] = None,
):
GObject.GObject.__init__(
self, title=f"Pick Entry Side - {playername}", parent=parent
)
terrain = masterhex.terrain
# We always orient the map as if for entry side 5.
self.battlemap = BattleMap.BattleMap(terrain, 5)
self.entry_sides = entry_sides
self.deferred = def1
self.playername = playername
self.board = board
self.set_icon(icon.pixbuf)
self.set_transient_for(parent)
self.set_destroy_with_parent(True)
self.set_title(f"PickEntrySide - Slugathon - {self.playername}")
self.hbox1 = Gtk.HBox(homogeneous=True)
self.hbox2 = Gtk.HBox()
self.hbox3 = Gtk.HBox()
self.vbox.pack_start(self.hbox1, False, True, 0)
self.vbox.pack_start(self.hbox2, True, True, 0)
self.vbox.pack_start(self.hbox3, False, True, 0)
if scale is None:
self.scale = self.compute_scale()
else:
self.scale = scale
self.area = Gtk.DrawingArea()
self.area.set_size_request(self.compute_width(), self.compute_height())
if self.playername:
tup = prefs.load_window_position(
self.playername, self.__class__.__name__
)
if tup:
x, y = tup
self.move(x, y)
tup = prefs.load_window_size(
self.playername, self.__class__.__name__
)
if tup:
width, height = tup
self.resize(width, height)
own_hex_label = self.masterhex_label(masterhex)
left_hex = masterhex.neighbors[4] or masterhex.neighbors[5]
left_hex_label = self.masterhex_label(left_hex)
top_hex = masterhex.neighbors[0] or masterhex.neighbors[1]
top_hex_label = self.masterhex_label(top_hex)
bottom_hex = masterhex.neighbors[2] or masterhex.neighbors[3]
bottom_hex_label = self.masterhex_label(bottom_hex)
spacer_label = self.masterhex_label(None)
self.hbox1.pack_start(own_hex_label, True, True, 0)
self.hbox1.pack_start(top_hex_label, True, True, 0)
self.hbox2.pack_start(left_hex_label, True, True, 0)
self.hbox2.pack_start(self.area, True, True, 0)
self.hbox3.pack_start(spacer_label, True, True, 0)
self.hbox3.pack_start(bottom_hex_label, True, True, 0)
self.guihexes = {}
for hex1 in self.battlemap.hexes.values():
# Don't show entrances.
if hex1.label not in ["ATTACKER", "DEFENDER"]:
self.guihexes[hex1.label] = GUIBattleHex.GUIBattleHex(
hex1, self
)
self.repaint_hexlabels = set() # type: Set[str]
# Hexes that need their bounding rectangles cleared, too.
# This fixes chits.
self.clear_hexlabels = set() # type: Set[str]
self.area.connect("draw", self.cb_area_expose)
self.area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.area.connect("button-press-event", self.cb_click)
self.connect("destroy", self.callback_with_none)
for hexlabel, entry_side in hexlabel_to_entry_side.items():
if entry_side in self.entry_sides:
self.guihexes[hexlabel].selected = True
self.show_all()
def compute_scale(self) -> int:
"""Return the approximate maximum scale that lets the map fit on the
screen."""
width = Gdk.Screen.width()
height = Gdk.Screen.height()
# Fudge factor to leave room on the sides.
xscale = math.floor(width / (2 * self.battlemap.hex_width)) - 5
# Fudge factor for menus and toolbars.
yscale = (
math.floor(height / (2 * SQRT3 * self.battlemap.hex_height)) - 11
)
return int(min(xscale, yscale))
def compute_width(self) -> int:
"""Return the width of the map in pixels."""
return int(math.ceil(self.scale * self.battlemap.hex_width * 3.2))
def compute_height(self) -> int:
"""Return the height of the map in pixels."""
return int(
math.ceil(self.scale * self.battlemap.hex_height * 2 * SQRT3)
)
def masterhex_label(
self, masterhex: Optional[MasterHex.MasterHex]
) -> Gtk.EventBox:
"""Return a Gtk.Label describing masterhex, inside a white
Gtk.EventBox."""
eventbox = Gtk.EventBox()
if masterhex:
text = (
f"<span size='large' weight='bold'>{masterhex.terrain} "
f"hex {masterhex.label}</span>"
)
else:
text = ""
label = Gtk.Label()
label.set_markup(text)
eventbox.add(label)
gtkcolor = Gdk.color_parse("white")
eventbox.modify_bg(Gtk.StateType.NORMAL, gtkcolor)
return eventbox
def cb_area_expose(
self, area: Gtk.DrawingArea, event: cairo.Context
) -> bool:
self.update_gui(event=event)
return True
def cb_click(self, area: Gtk.DrawingArea, event: Gdk.EventButton) -> bool:
for guihex in self.guihexes.values():
if guiutils.point_in_polygon((event.x, event.y), guihex.points):
self.clicked_on_hex(area, event, guihex)
return True
return True
def clicked_on_hex(
self,
area: Gtk.DrawingArea,
event: Gdk.EventButton,
guihex: GUIBattleHex.GUIBattleHex,
) -> None:
if guihex.selected:
entry_side = hexlabel_to_entry_side[guihex.battlehex.label]
self.deferred.callback(entry_side)
self.destroy()
def bounding_rect_for_hexlabels(
self, hexlabels: Set[str]
) -> Tuple[float, float, float, float]:
"""Return the minimum bounding rectangle that encloses all
GUIMasterHexes whose hexlabels are given, as a tuple
(x, y, width, height)
"""
min_x = float(maxsize)
max_x = float(-maxsize)
min_y = float(maxsize)
max_y = float(-maxsize)
for hexlabel in hexlabels:
try:
guihex = self.guihexes[hexlabel]
x, y, width, height = guihex.bounding_rect
min_x = min(min_x, x)
min_y = min(min_y, y)
max_x = max(max_x, x + width)
max_y = max(max_y, y + height)
except KeyError: # None check
pass
width = max_x - min_x
height = max_y - min_y
return min_x, min_y, width, height
def update_gui(self, event: Optional[cairo.Context] = None) -> None:
"""Repaint the amount of the GUI that needs repainting.
Compute the dirty rectangle from the union of
self.repaint_hexlabels and the event's area.
"""
if not self.area or not self.area.get_window():
return
if event is None:
if not self.repaint_hexlabels:
return
else:
clip_rect = self.bounding_rect_for_hexlabels(
self.repaint_hexlabels
)
else:
if self.repaint_hexlabels:
clip_rect = guiutils.combine_rectangles(
event.clip_extents(),
self.bounding_rect_for_hexlabels(self.repaint_hexlabels),
)
else:
clip_rect = event.clip_extents()
ctx = self.area.get_window().cairo_create()
ctx.set_line_width(round(0.2 * self.scale))
ctx.rectangle(*clip_rect)
ctx.clip()
# white background, only when we get an event
if event is not None:
ctx.set_source_rgb(1, 1, 1)
requisition = self.area.get_size_request()
width = requisition.width
height = requisition.height
ctx.rectangle(0, 0, width, height)
ctx.fill()
for hexlabel in self.clear_hexlabels:
ctx.set_source_rgb(1, 1, 1)
guihex = self.guihexes[hexlabel]
x, y, width, height = guihex.bounding_rect
ctx.rectangle(x, y, width, height)
ctx.fill()
for guihex in self.guihexes.values():
if guiutils.rectangles_intersect(clip_rect, guihex.bounding_rect):
guihex.update_gui(ctx)
self.repaint_hexlabels.clear()
self.clear_hexlabels.clear()
def repaint(self, hexlabels: Set[str] = None) -> None:
if hexlabels:
self.repaint_hexlabels.update(hexlabels)
reactor.callLater(0, self.update_gui) # type: ignore
def callback_with_none(self, *args: Any) -> None:
"""Called if the window is destroyed."""
logging.debug(f"{args=}")
if not self.deferred.called:
self.deferred.callback(None)
if __name__ == "__main__":
import random
def my_callback(choice: int) -> None:
logging.info(f"chose entry side {choice}")
reactor.stop() # type: ignore
board = MasterBoard.MasterBoard()
masterhex = random.choice(list(board.hexes.values()))
logging.info(f"masterhex {masterhex}")
entry_sides = set()
for side, neighbor in enumerate(masterhex.neighbors):
if neighbor is not None:
if side & 1:
entry_sides.add(side)
else:
entry_sides.add(side + 1)
pick_entry_side, def1 = new(board, masterhex, {1, 3, 5}, None)
def1.addCallback(my_callback)
reactor.run() # type: ignore[attr-defined]