-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenubar.py
165 lines (114 loc) · 4.18 KB
/
menubar.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
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import numpy as np
from parameters import *
import map as mp
import __main__
def New():
def set_map():
"""Setting new map and closing new map window"""
MAP.WIDTH, MAP.HEIGHT = int(width.get()), int(height.get())
map_square = MAP.WIDTH * MAP.HEIGHT
# 4*map_square because we have 4 layers here on our map
MAP.MAP = np.zeros(4 * map_square, dtype = np.uint8)
#new file
MAP.file_path = None
#Checking should we fill skybox
if fill_skybox.get() and category.get() != "Category":
tile = Tiles.get(ACTIVE)
cat = category.get()
MAP.MAP[:map_square] = TILES.categories[cat][tile]["id"]
mp.Update()
#resizing canvas
__main__.cv.config(scrollregion=(0, 0, MAP.TILE_SIZE*MAP.WIDTH, MAP.TILE_SIZE*MAP.HEIGHT))
print(MAP.WIDTH, MAP.HEIGHT)
print(MAP.MAP)
new_window.destroy()
# window
new_window = Toplevel()
new_window.title("New Map")
new_window.geometry("180x300")
new_window.grab_set()
# window
# GUI
width = Entry(new_window)
l_width = Label(new_window, text = "Width:")
height = Entry(new_window)
l_height = Label(new_window, text = "Height:")
fill_skybox = IntVar()
fill = Checkbutton(new_window, text="Fill skybox", variable=fill_skybox)
def update_list(event):
"""Updating Tiles when changing category"""
k = list(TILES.categories[category.get()].keys())
k = StringVar(value=k)
Tiles.config(listvariable=k)
cat = list(TILES.categories.keys())
category = ttk.Combobox(new_window, values = cat, state = "readonly")
category.bind('<<ComboboxSelected>>', update_list)
category.set("Category")
category.grid(column=0)
Tiles = Listbox(new_window, selectmode=SINGLE)
Tiles.grid(column=0)
button = Button(new_window, text = "Create",
command = set_map)
# GUI
#Placing GUI
l_width.grid(row=0, column=0)
width.grid(row = 0, column = 1)
l_height.grid(row=1, column=0)
height.grid(row = 1, column = 1)
fill.grid(row = 2, column = 0, columnspan = 2)
category.grid(row = 3, column = 0, columnspan = 2)
Tiles.grid(row = 4, column = 0, columnspan = 2)
button.grid(row = 5, column = 0, columnspan = 2)
# Placing GUI
def Open():
"""Handles opening .map files"""
f = filedialog.askopenfile(initialdir = ".", initialfile = "New map" ,
title = "Select file",
filetypes = (("map files","*.map"),("all files","*.*")),
mode = "r", defaultextension = "*.map")
MAP.file_path = f.name
lines = f.read().splitlines()
MAP.WIDTH = int(lines[0])
MAP.HEIGHT = int(lines[1])
map_square = MAP.WIDTH * MAP.HEIGHT
# 4*map_square because we have 4 layers here on our map
MAP.MAP = np.zeros(4 * map_square, dtype=np.uint8)
#4 is number of layers
#L stands for Layer
for L in range(2,6):
for i in range(MAP.WIDTH * MAP.HEIGHT):
#TODO
MAP.MAP[i + (L - 2) * MAP.WIDTH * MAP.HEIGHT] = ord(lines[L][i])-SHIFT
#updating canvas
mp.Update()
print(MAP.WIDTH, MAP.HEIGHT)
print(MAP.MAP)
f.close()
def Save():
"""Saves MAP to file"""
if MAP.file_path is None:
Save_as()
return
f = open(MAP.file_path, mode = "w+")
f.write(str(MAP.WIDTH) + "\n")
f.write(str(MAP.HEIGHT) + "\n")
for i in range(4 * MAP.WIDTH * MAP.HEIGHT):
if i % (MAP.HEIGHT * MAP.WIDTH) == 0 and i != 0:
f.write("\n")
#TODO ASCII starts from ~32 symbol
f.write(chr(MAP.MAP[i]+SHIFT))
f.close()
def Save_as():
"""Saves MAP to file"""
f = filedialog.asksaveasfile(initialdir = ".", initialfile = "New map" ,
title = "Select directory",
filetypes = (("map files","*.map"),("all files","*.*")),
mode = "w", defaultextension = "*.map")
if f is None:
return
MAP.file_path = f.name
f.close()
Save()