-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapEditor.py
More file actions
149 lines (121 loc) · 5.36 KB
/
Copy pathmapEditor.py
File metadata and controls
149 lines (121 loc) · 5.36 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
import os
import tkinter as tk
from tkinter import filedialog, messagebox
MAP_WIDTH = 64
MAP_HEIGHT = 64
CELL_SIZE = 12 # Pixel size per tile cell on screen
# Color palette and labels for tile types
# TODO: entities need a starting facing/direction authored in the editor
# (e.g. per-cell facing, or a direction token appended to the entity glyph)
# so it can be exported into map.txt and carried through map2rooms.py ->
# rooms.txt -> process_room Pass 3 -> frameEntity -> entityShader.gs.
TILES = {
'0': ('#222222', '0: Floor'),
'1': ('#00ffcc', '1: Wall'),
'2': ('#ffdd00', '2: Door'),
'a': ('#ff3366', 'a: Entity A'),
'b': ('#ff8800', 'b: Entity B'),
'c': ('#aa00ff', 'c: Entity C'),
}
class MapEditor:
def __init__(self, root):
self.root = root
self.root.title("64x64 Map Visual Editor")
self.active_tile = '1'
self.grid_data = ['0'] * (MAP_WIDTH * MAP_HEIGHT)
# UI Setup
self.create_controls()
self.create_canvas()
# Load existing map.txt if present
if os.path.exists("map.txt"):
self.load_file("map.txt")
def create_controls(self):
control_frame = tk.Frame(self.root, bg="#1a1a1a")
control_frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
tk.Label(control_frame, text="Tools:", fg="white", bg="#1a1a1a").pack(side=tk.LEFT, padx=5)
self.tile_buttons = {}
for key, (color, label) in TILES.items():
btn = tk.Button(
control_frame, text=label, bg=color, fg="black" if key != '0' else "white",
command=lambda k=key: self.set_active_tile(k), relief=tk.RAISED, bd=2
)
btn.pack(side=tk.LEFT, padx=3)
self.tile_buttons[key] = btn
self.set_active_tile('1')
# File operations
tk.Button(control_frame, text="Save (map.txt)", bg="#28a745", fg="white", command=self.save_file).pack(side=tk.RIGHT, padx=5)
tk.Button(control_frame, text="Load Map", bg="#007bff", fg="white", command=self.open_file).pack(side=tk.RIGHT, padx=5)
tk.Button(control_frame, text="Clear Floor", bg="#dc3545", fg="white", command=self.clear_map).pack(side=tk.RIGHT, padx=5)
def create_canvas(self):
canvas_width = MAP_WIDTH * CELL_SIZE
canvas_height = MAP_HEIGHT * CELL_SIZE
self.canvas = tk.Canvas(self.root, width=canvas_width, height=canvas_height, bg="#000000")
self.canvas.pack(padx=10, pady=10)
# Mouse bindings for drag-drawing
self.canvas.bind("<Button-1>", self.on_click)
self.canvas.bind("<B1-Motion>", self.on_click)
def set_active_tile(self, key):
self.active_tile = key
for k, btn in self.tile_buttons.items():
if k == key:
btn.config(relief=tk.SUNKEN, bd=4)
else:
btn.config(relief=tk.RAISED, bd=2)
def on_click(self, event):
x = event.x // CELL_SIZE
y = event.y // CELL_SIZE
if 0 <= x < MAP_WIDTH and 0 <= y < MAP_HEIGHT:
idx = y * MAP_WIDTH + x
if self.grid_data[idx] != self.active_tile:
self.grid_data[idx] = self.active_tile
self.draw_cell(x, y)
def draw_cell(self, x, y):
idx = y * MAP_WIDTH + x
val = self.grid_data[idx]
color, _ = TILES.get(val, ('#222222', ''))
x1 = x * CELL_SIZE
y1 = y * CELL_SIZE
x2 = x1 + CELL_SIZE
y2 = y1 + CELL_SIZE
tag = f"cell_{x}_{y}"
self.canvas.delete(tag)
self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="#111111", tags=tag)
# Display entity letters inside the cell
if val.isalpha():
self.canvas.create_text((x1 + x2)//2, (y1 + y2)//2, text=val.upper(), fill="white", font=("Arial", 8, "bold"), tags=tag)
def redraw_all(self):
self.canvas.delete("all")
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
self.draw_cell(x, y)
def load_file(self, filepath):
try:
with open(filepath, "r") as f:
tokens = f.read().split()
if len(tokens) >= MAP_WIDTH * MAP_HEIGHT:
self.grid_data = tokens[:MAP_WIDTH * MAP_HEIGHT]
self.redraw_all()
else:
messagebox.showerror("Error", "File does not contain 4096 tokens.")
except Exception as e:
messagebox.showerror("Error", f"Failed to load file: {e}")
def open_file(self):
path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if path:
self.load_file(path)
def save_file(self):
try:
with open("map.txt", "w") as f:
f.write("\n".join(self.grid_data))
messagebox.showinfo("Success", "Saved successfully to map.txt!")
except Exception as e:
messagebox.showerror("Error", f"Failed to save map.txt: {e}")
def clear_map(self):
if messagebox.askyesno("Clear Map", "Reset all tiles to 0 (Floor)?"):
self.grid_data = ['0'] * (MAP_WIDTH * MAP_HEIGHT)
self.redraw_all()
if __name__ == "__main__":
root = tk.Tk()
root.configure(bg="#1a1a1a")
app = MapEditor(root)
root.mainloop()