-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexmap.py
executable file
·380 lines (318 loc) · 13.9 KB
/
hexmap.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
#!/usr/bin/python
"""
A simple interactive HexMap. My kids like playing with this as a game in itself, but the real
intention is that you can use this as a basis for whatever else you are making. Requires Pygame.
Controls are mouse-based.
Left-click creates a new hex or changes the color of one.
Left-drag pans the map.
Right-drag rotates the map.
Scroll-wheel zooms.
Scroll-wheel-click resets zoom and re-centeres the map.
Credit for "the hard parts" in this file goes to Amit at Red Blob Games:
https://www.redblobgames.com/grids/hexagons/
"""
import sys
import pygame
import random
import math
import collections
Point = collections.namedtuple("Point", ["x", "y"])
Orientation = collections.namedtuple("Orientation", ["f0", "f1", "f2", "f3", "b0", "b1", "b2", "b3", "start_angle"])
Layout = collections.namedtuple("Layout", ["orientation", "size", "origin"])
layout_pointy = Orientation(math.sqrt(3.0), math.sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, math.sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5)
layout_flat = Orientation(3.0 / 2.0, 0.0, math.sqrt(3.0) / 2.0, math.sqrt(3.0), 2.0 / 3.0, 0.0, -1.0 / 3.0, math.sqrt(3.0) / 3.0, 0.0)
PI_OVER_THREE = math.pi / 3.0
BLACK = (0, 0, 0)
TRANSPARENT = (25, 175, 82) # A random color that we'll use for easy transparency
class Position(tuple):
# Using Cube/Axial coordinates
def __new__(cls, q, r, s=None):
if s == None:
s = -1 * q - r
return tuple.__new__(Position, (q, r, s))
@property
def q(self):
return self[0]
@property
def r(self):
return self[1]
@property
def s(self):
return self[2]
def round(self):
"""Convert fractional coordinates into the actual position of the containing hex."""
qi = int(round(self.q))
ri = int(round(self.r))
si = int(round(self.s))
q_diff = abs(qi - self.q)
r_diff = abs(ri - self.r)
s_diff = abs(si - self.s)
if q_diff > r_diff and q_diff > s_diff:
qi = -ri - si
else:
if r_diff > s_diff:
ri = -qi - si
else:
si = -qi - ri
return Position(qi, ri, si)
class HexMap:
ONE_THIRD = 1. / 3
TWO_THIRDS = 2. / 3
SQRT_3_OVER_3 = 3**0.5 / 3
STARTING_HEX_SIZE = Point(100.0, 100.0)
def __init__(self, size):
self.size = size
self.map_surface = pygame.Surface(size)
self.layout = Layout(layout_pointy, self.STARTING_HEX_SIZE, Point(size[0] / 2, size[1] / 2))
self.hexes = {}
origin = Position(0, 0)
self.hexes[origin] = Hex(self.map_surface, origin, self.layout)
def click(self, location):
pos = self.pixel_to_hex(Point(location[0], location[1]))
if pos in self.hexes:
self.hexes[pos].click()
else:
hex = Hex(self.map_surface, pos, self.layout)
self.hexes[pos] = hex
def _recalculate_hex_corners(self):
for hex in self.hexes.values():
hex.recalculate_corners(self.layout)
def zoom(self, change):
size = Point(self.layout.size[0] * change, self.layout.size[1] * change)
self.layout = Layout(self.layout.orientation, size, self.layout.origin)
self._recalculate_hex_corners()
def reset(self, size):
self.layout = Layout(self.layout.orientation, self.STARTING_HEX_SIZE, Point(size[0] / 2, size[1] / 2))
self._recalculate_hex_corners()
def pan(self, x_travel, y_travel):
self.layout = Layout(self.layout.orientation, self.layout.size, Point(self.layout.origin[0] + x_travel,
self.layout.origin[1] + y_travel))
self._recalculate_hex_corners()
def rotate_right(self):
if self.layout.orientation == layout_pointy:
self.layout = Layout(layout_flat, self.layout.size, self.layout.origin)
new_hexes = {}
for position in self.hexes:
x, y, z = position
new_position = Position(-z, -x, -y)
hex = self.hexes[position]
hex.position = new_position
new_hexes[new_position] = hex
self.hexes = new_hexes
else:
self.layout = Layout(layout_pointy, self.layout.size, self.layout.origin)
self._recalculate_hex_corners()
def rotate_left(self):
if self.layout.orientation == layout_flat:
self.layout = Layout(layout_pointy, self.layout.size, self.layout.origin)
new_hexes = {}
for position in self.hexes:
x, y, z = position
new_position = Position(-y, -z, -x)
hex = self.hexes[position]
hex.position = new_position
new_hexes[new_position] = hex
self.hexes = new_hexes
else:
self.layout = Layout(layout_flat, self.layout.size, self.layout.origin)
self._recalculate_hex_corners()
def pixel_to_hex(self, p):
M = self.layout.orientation
size = self.layout.size
origin = self.layout.origin
pt = Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y)
q = M.b0 * pt.x + M.b1 * pt.y
r = M.b2 * pt.x + M.b3 * pt.y
return Position(q, r).round()
def draw(self, screen):
self.map_surface.fill(BLACK)
for hex in self.hexes.values():
hex.draw()
screen.blit(self.map_surface, (0, 0))
class Hex:
DIRECTIONS = [(1, 0, -1), (1, -1, 0), (0, -1, 1), (-1, 0, 1), (-1, 1, 0), (0, 1, -1)]
def __init__(self, map, position, layout):
self.position = position
self.map = map
# Calculate the points that define the vertexes.
self.points = self.corners(layout, position)
self.color = self.random_color()
@staticmethod
def random_color():
return (random.randint(0,255), random.randint(0,255), random.randint(0,255))
def recalculate_corners(self, layout):
self.points = self.corners(layout, self.position)
@staticmethod
def hex_to_pixel(layout, h):
M = layout.orientation
size = layout.size
origin = layout.origin
x = (M.f0 * h.q + M.f1 * h.r) * size.x
y = (M.f2 * h.q + M.f3 * h.r) * size.y
return Point(x + origin.x, y + origin.y)
@staticmethod
def corner_offset(layout, corner):
M = layout.orientation
size = layout.size
angle = PI_OVER_THREE * (M.start_angle - corner)
return Point(size.x * math.cos(angle), size.y * math.sin(angle))
@staticmethod
def corners(layout, h):
corners = []
center = Hex.hex_to_pixel(layout, h)
for i in range(0, 6):
offset = Hex.corner_offset(layout, i)
corners.append(Point(center.x + offset.x, center.y + offset.y))
return corners
def neighbors(self):
neighbors = []
for direction in self.DIRECTIONS:
neighbors.append(Position(self.position[0] + direction[0],
self.position[1] + direction[1],
self.position[2] + direction[2]))
return neighbors
def click(self):
self.color = self.random_color()
def draw(self):
pygame.draw.polygon(self.map, self.color, self.points, 1)
class Tab:
def __init__(self, name, pane):
self.name = name
self.pane = pane
self.name_display = self.pane.font.render(name, True, (0, 0, 255), BLACK)
self.name_display = pygame.transform.rotate(self.name_display, 90)
self.text = self.pane.font.render("This is what " + self.name + " would look like", True, (0, 0, 255), BLACK)
self.text_width = self.text.get_size()[0]
self.content = pygame.Surface((self.text_width, self.pane.screen_size[1]))
self.content.blit(self.text, (0, self.pane.screen_size[1]/2))
def get_size(self):
return self.name_display.get_size()
def click(self, pos):
pass
def draw(self, screen):
# The expanded content only. Return the width.
screen.blit(self.content, (self.pane.screen_size[0] - self.text_width, 0))
return self.text_width
class TabPane:
WIDTH_BUFFER = 5
HEIGHT_BUFFER = 10
def __init__(self, size):
self.expanded = False
self.active_tab = None
self.tabs = []
self.font = pygame.font.SysFont(pygame.font.get_default_font(), 30)
tmp_surface = self.font.render('Ig', True, (0, 0, 255), BLACK)
tmp_surface = pygame.transform.rotate(tmp_surface, 90)
self.screen_size = size
map_width, map_height = size
self.tab_width = tmp_surface.get_size()[0] + self.WIDTH_BUFFER * 2
self.tab_row = pygame.Surface((self.tab_width, self.screen_size[1]))
self.tab_row.set_colorkey(TRANSPARENT)
self.content_width = 0
def create_tab(self, name):
tab = Tab(name, self)
self.tabs.append(tab)
def collidepoint(self, pos):
return pos[0] >= self.screen_size[0] - self.content_width - self.tab_width
def click(self, pos):
left = self.screen_size[0] - self.content_width - self.tab_width
right = self.screen_size[0] - self.content_width
if pos[0] >= right:
self.active_tab.click(pos)
elif pos[0] >= left:
# they clicked on a tab maybe?
height = 0
for tab in self.tabs:
tab_height = tab.get_size()[1] + self.HEIGHT_BUFFER * 2
rect = pygame.Rect(left, height, self.tab_width, tab_height)
height += tab_height
if rect.collidepoint(pos[0], pos[1]):
if self.active_tab == tab:
# minimize
self.active_tab = None
self.content_width = 0
else:
self.active_tab = tab
break
def draw(self, screen):
height = 0
self.tab_row.fill(TRANSPARENT)
self.screen_size= screen.get_size()
for tab in self.tabs:
if tab == self.active_tab:
self.content_width = tab.draw(screen)
tab_height = tab.get_size()[1]
top_right = (self.tab_width - 1, height)
top_left = (0, height + self.HEIGHT_BUFFER)
bottom_left = (0, height + self.HEIGHT_BUFFER + tab_height)
bottom_right = (self.tab_width - 1, height + 2 * self.HEIGHT_BUFFER + tab_height)
pygame.draw.polygon(self.tab_row, BLACK, (top_right, top_left, bottom_left, bottom_right))
pygame.draw.polygon(self.tab_row, (255, 255, 255), (top_right, top_left, bottom_left, bottom_right), 1)
self.tab_row.blit(tab.name_display, (0 + self.WIDTH_BUFFER, height + self.HEIGHT_BUFFER))
height += tab_height + 2 * self.HEIGHT_BUFFER
screen.blit(self.tab_row, (self.screen_size[0] - self.tab_width - self.content_width, 0))
if __name__ == '__main__':
pygame.init()
size = width, height = 1200, 1000
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
LEFT_BUTTON, MIDDLE_BUTTON, RIGHT_BUTTON, SCROLL_UP, SCROLL_DOWN = 1, 2, 3, 4, 5
hex_map = HexMap(size)
tab_pane = TabPane(size)
tab_pane.create_tab('Tab 1')
tab_pane.create_tab('This is tab 2')
tab_pane.create_tab('Tab 3')
mouse_drag_orig_angle = None
mouse_drag_orig_pos = None
was_moved = False
while True:
# max the busy-wait loop at 60 FPS
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == SCROLL_DOWN:
hex_map.zoom(.95)
elif event.button == SCROLL_UP:
hex_map.zoom(1.05)
elif event.button == RIGHT_BUTTON:
mouse_drag_orig_angle = None
elif event.button == LEFT_BUTTON:
mouse_drag_orig_pos = None
if tab_pane.collidepoint(event.pos):
tab_pane.click(event.pos)
else:
if not was_moved:
hex_map.click(event.pos)
was_moved = False
elif event.button == MIDDLE_BUTTON:
hex_map.reset(size)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == RIGHT_BUTTON:
mouse_drag_orig_angle = math.atan2(event.pos[1] - hex_map.layout.origin.y, event.pos[0] - hex_map.layout.origin.x)
elif event.button == LEFT_BUTTON:
mouse_drag_orig_pos = event.pos
elif event.type == pygame.MOUSEMOTION:
if mouse_drag_orig_angle: # if we're rotating it
new_angle = math.atan2(event.pos[1] - hex_map.layout.origin.y, event.pos[0] - hex_map.layout.origin.x)
difference = math.degrees(mouse_drag_orig_angle - new_angle)
if difference > 180:
difference -= 360
elif difference < -180:
difference += 360
if abs(difference) > 30:
if difference > 0:
hex_map.rotate_right()
else:
hex_map.rotate_left()
mouse_drag_orig_angle = new_angle
elif mouse_drag_orig_pos: # if we're dragging it
if (abs(event.pos[0] - mouse_drag_orig_pos[0]) + abs(event.pos[1] - mouse_drag_orig_pos[1])) > 3:
hex_map.pan(event.pos[0] - mouse_drag_orig_pos[0], event.pos[1] - mouse_drag_orig_pos[1])
mouse_drag_orig_pos = event.pos
was_moved = True
screen.fill(BLACK)
hex_map.draw(screen)
tab_pane.draw(screen)
pygame.display.flip()