-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmap_editor.py
More file actions
281 lines (229 loc) · 10.2 KB
/
map_editor.py
File metadata and controls
281 lines (229 loc) · 10.2 KB
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
# Editor to make customized TravelField maps.
import tkinter as tk
from tkinter import ttk, filedialog
import torch
# Terrain constants
GRASS = 1
SWAMP = 2
ROCK = 3
ROAD = 4
GOAL = 5
char_dict = {
".": GRASS,
"□": ROCK,
"+": ROAD,
"-": SWAMP,
"O": GOAL,
}
inv_char_dict = {v: k for k, v in char_dict.items()}
class MapEditor:
def __init__(self, root):
self.root = root
self.root.title("Map Editor")
self.grid_size = 32
self.cell_size = 15
self.terrain_types = {
GRASS: {"name": "Grass", "color": (0, 255, 0)},
SWAMP: {"name": "Swamp", "color": (139, 69, 19)},
ROCK: {"name": "Rock", "color": (100, 100, 100)},
ROAD: {"name": "Road", "color": (255, 255, 155)},
GOAL: {"name": "Goal", "color": (255, 0, 0)},
}
self.grid = torch.zeros((8, self.grid_size, self.grid_size))
self.undo_stack = []
self.is_painting = False
self.is_erasing = False
self.setup_gui()
self.initialize_borders()
self.update_window_size()
self.draw_grid()
# ------------------- GUI -------------------
def setup_gui(self):
self.main_frame = ttk.Frame(self.root)
self.main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Row 1: Terrain, Map size, Apply, Pan buttons
control_frame1 = ttk.Frame(self.main_frame)
control_frame1.pack(fill=tk.X, pady=(0, 5))
ttk.Label(control_frame1, text="Terrain:").pack(side=tk.LEFT)
self.terrain_var = tk.StringVar()
self.terrain_dropdown = ttk.Combobox(
control_frame1,
textvariable=self.terrain_var,
values=[f"{k}: {v['name']}" for k, v in self.terrain_types.items()],
state="readonly",
width=14
)
self.terrain_dropdown.set("1: Grass")
self.terrain_dropdown.pack(side=tk.LEFT, padx=(5, 10))
ttk.Label(control_frame1, text="Map Size:").pack(side=tk.LEFT)
self.size_var = tk.IntVar(value=self.grid_size)
ttk.Spinbox(control_frame1, from_=4, to=256, textvariable=self.size_var, width=5).pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame1, text="Apply", command=self.apply_size).pack(side=tk.LEFT, padx=(5, 20))
# Pan buttons
pan_frame = ttk.Frame(control_frame1)
pan_frame.pack(side=tk.LEFT)
ttk.Button(pan_frame, text="▲", width=3, command=lambda: self.pan_canvas(0, -5)).pack()
mid = ttk.Frame(pan_frame)
mid.pack()
ttk.Button(mid, text="◀", width=3, command=lambda: self.pan_canvas(-5, 0)).pack(side=tk.LEFT)
ttk.Button(mid, text="▶", width=3, command=lambda: self.pan_canvas(5, 0)).pack(side=tk.LEFT)
ttk.Button(pan_frame, text="▼", width=3, command=lambda: self.pan_canvas(0, 5)).pack()
# Row 2: Undo, Restart, Save, Load
control_frame2 = ttk.Frame(self.main_frame)
control_frame2.pack(fill=tk.X, pady=(0, 10))
ttk.Button(control_frame2, text="Undo", command=self.undo_last).pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame2, text="Restart", command=self.restart_map).pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame2, text="Save", command=self.save_map).pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame2, text="Load", command=self.load_map).pack(side=tk.LEFT, padx=5)
# Canvas frame
self.canvas_frame = ttk.Frame(self.main_frame)
self.canvas_frame.pack(fill=tk.BOTH, expand=True)
self.canvas = tk.Canvas(self.canvas_frame, bg="white")
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Scrollbars (created once, updated dynamically)
self.v_scroll = ttk.Scrollbar(self.canvas_frame, orient=tk.VERTICAL, command=self.canvas.yview)
self.h_scroll = ttk.Scrollbar(self.canvas_frame, orient=tk.HORIZONTAL, command=self.canvas.xview)
self.canvas.configure(xscrollcommand=self.h_scroll.set, yscrollcommand=self.v_scroll.set)
# Mouse bindings
self.canvas.bind("<Button-1>", self.on_mouse_down)
self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_mouse_up)
self.canvas.bind("<Button-3>", self.on_right_mouse_down)
self.canvas.bind("<B3-Motion>", self.on_right_mouse_drag)
self.canvas.bind("<ButtonRelease-3>", self.on_right_mouse_up)
self.canvas.bind("<Button-2>", self.on_middle_mouse_down)
self.canvas.bind("<B2-Motion>", self.on_middle_mouse_drag)
# Status bar
self.status_var = tk.StringVar(value="Ready")
ttk.Label(self.main_frame, textvariable=self.status_var, relief=tk.SUNKEN).pack(fill=tk.X)
# ------------------- Map Logic -------------------
def initialize_borders(self):
self.grid.zero_()
self.grid[GRASS, :, :] = 1.0
def apply_size(self):
n = int(self.size_var.get())
self.grid_size = n
self.grid = torch.zeros((8, n, n))
self.initialize_borders()
self.update_window_size()
self.draw_grid()
def update_window_size(self):
"""Resize window and canvas to fit map + controls, add scrollbars only if needed."""
# Canvas size
max_width = self.root.winfo_screenwidth() - 100
max_height = self.root.winfo_screenheight() - 150
# Minimum canvas size so buttons always visible
min_canvas_width = 300
min_canvas_height = 200
canvas_width = max(min(self.grid_size * self.cell_size, max_width), min_canvas_width)
canvas_height = max(min(self.grid_size * self.cell_size, max_height), min_canvas_height)
self.canvas.config(width=canvas_width, height=canvas_height)
# Scrollbars if needed
if self.grid_size * self.cell_size > canvas_width:
self.h_scroll.pack(side=tk.BOTTOM, fill=tk.X)
else:
self.h_scroll.pack_forget()
if self.grid_size * self.cell_size > canvas_height:
self.v_scroll.pack(side=tk.RIGHT, fill=tk.Y)
else:
self.v_scroll.pack_forget()
# Measure actual control rows + status bar height
self.root.update_idletasks() # ensures sizes are computed
control_height = (
self.main_frame.winfo_children()[0].winfo_reqheight() + # first row
self.main_frame.winfo_children()[1].winfo_reqheight() + # second row
self.main_frame.winfo_children()[-1].winfo_reqheight() + 10 # status bar + small padding
)
# Set window geometry
window_width = canvas_width + 40
window_height = canvas_height + control_height
self.root.geometry(f"{window_width}x{window_height}")
def draw_grid(self):
self.canvas.delete("all")
for i in range(self.grid_size):
for j in range(self.grid_size):
t = torch.argmax(self.grid[:, i, j]).item()
rgb = self.terrain_types[t]["color"]
color = "#%02x%02x%02x" % rgb
x1, y1 = j * self.cell_size, i * self.cell_size
x2, y2 = x1 + self.cell_size, y1 + self.cell_size
self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
# ------------------- Undo / Paint -------------------
def push_undo(self):
self.undo_stack.append(self.grid.clone())
def paint_cell(self, event, terrain_type):
x = int(self.canvas.canvasx(event.x) // self.cell_size)
y = int(self.canvas.canvasy(event.y) // self.cell_size)
if 0 <= x < self.grid_size and 0 <= y < self.grid_size:
self.grid[:, y, x] = 0
self.grid[terrain_type, y, x] = 1
self.draw_grid()
# ------------------- Mouse events -------------------
def on_mouse_down(self, event):
self.push_undo()
self.is_painting = True
terrain_type = int(self.terrain_var.get().split(":")[0])
self.paint_cell(event, terrain_type)
def on_mouse_drag(self, event):
if self.is_painting:
terrain_type = int(self.terrain_var.get().split(":")[0])
self.paint_cell(event, terrain_type)
def on_mouse_up(self, event):
self.is_painting = False
def on_right_mouse_down(self, event):
self.push_undo()
self.is_erasing = True
self.paint_cell(event, GRASS)
def on_right_mouse_drag(self, event):
if self.is_erasing:
self.paint_cell(event, GRASS)
def on_right_mouse_up(self, event):
self.is_erasing = False
def on_middle_mouse_down(self, event):
self.canvas.scan_mark(event.x, event.y)
def on_middle_mouse_drag(self, event):
self.canvas.scan_dragto(event.x, event.y, gain=1)
def pan_canvas(self, dx, dy):
self.canvas.xview_scroll(dx, "units")
self.canvas.yview_scroll(dy, "units")
# ------------------- Actions -------------------
def undo_last(self):
if self.undo_stack:
self.grid = self.undo_stack.pop()
self.draw_grid()
def restart_map(self):
self.initialize_borders()
self.draw_grid()
def save_map(self):
filename = filedialog.asksaveasfilename(defaultextension=".txt")
if not filename:
return
with open(filename, "w", encoding="utf-8") as f:
for i in range(self.grid_size):
line = ""
for j in range(self.grid_size):
t = torch.argmax(self.grid[:, i, j]).item()
line += inv_char_dict.get(t, ".")
f.write(line + "\n")
def load_map(self):
filename = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
if not filename:
return
with open(filename, "r", encoding="utf-8") as f:
lines = [l.rstrip() for l in f if l.strip()]
n = len(lines)
self.grid_size = n
self.size_var.set(n)
self.grid = torch.zeros((8, n, n))
for i, line in enumerate(lines):
for j, ch in enumerate(line):
self.grid[char_dict.get(ch, GRASS), i, j] = 1
self.update_window_size()
self.draw_grid()
def main():
root = tk.Tk()
MapEditor(root)
root.mainloop()
if __name__ == "__main__":
main()