-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxMapeditor.py
186 lines (138 loc) · 4.41 KB
/
wxMapeditor.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# wxMapeditor.py
import wx
import os, sys
import pygame
from utils import KeyHandler
import thread
import mapeditor
# warning, in wx, a frame is a window, a window is a frame.
class wxMapEditor(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "ariclient map editor", size=(800,600))
self.__init_menu__()
self.CreateStatusBar()
self.__init_controls__()
def __init_menu__(self):
#Creation of the Menu with two items, about ans quit
menubar = wx.MenuBar()
fileMenu = wx.Menu()
OpenMn = fileMenu.Append(wx.ID_OPEN, '&Open', 'Open a map')
SaveMn = fileMenu.Append(wx.ID_SAVE, '&Save', 'Save a map')
fileMenu.AppendSeparator()
QuitMn = fileMenu.Append(wx.ID_EXIT, '&Quit', 'Quit application')
aboutMenu = wx.Menu()
AboutMn = aboutMenu.Append(wx.ID_ABOUT, '&About', 'About the application')
menubar.Append(fileMenu, '&File')
menubar.Append(aboutMenu, '&?')
self.SetMenuBar(menubar)
#Binds the two items to the correct funcion, AboutBox and Quit application
self.Bind(wx.EVT_MENU, self.OnOpen, OpenMn)
self.Bind(wx.EVT_MENU, self.OnSave, SaveMn)
self.Bind(wx.EVT_MENU, self.OnQuit, QuitMn)
self.Bind(wx.EVT_MENU, self.OnAbout, AboutMn)
def __init_controls__(self):
self.brushes = wx.TreeCtrl(self, -1, size = wx.Size(100,600))
self.brushesRoot = self.brushes.AddRoot("Brushes")
self.brushes.AppendItem(self.brushesRoot, "Grass")
self.brushes.AppendItem(self.brushesRoot, "Water")
self.panel = PygamePanel(self)
sizer = wx.BoxSizer( wx.HORIZONTAL )
sizer.Add( self.brushes, 0, wx.ALL, 5 )
sizer.Add( self.panel, 0, wx.ALL, 5 )
self.SetSizer( sizer )
def OnOpen(self, event):
self.dirname = ''
dlg = wx.FileDialog(self, "Open a file...", self.dirname, "", "*.txt", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
filePath = os.path.join(self.dirname, self.filename)
print filePath
self.panel.pygamethread.LoadMap(filePath)
dlg.Destroy()
def OnSave(self, event):
self.dirname = ''
dlg = wx.FileDialog(self, "Save file as...", self.dirname, "", "*.txt", wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
filePath = os.path.join(self.dirname, self.filename)
print filePath
self.panel.pygamethread.SaveMap(filePath)
dlg.Destroy()
def OnQuit(self, event):
self.Close()
def OnAbout(self, event):
about = wx.AboutDialogInfo()
about.Name = "ariclient map editor"
wx.AboutBox(about)
class PygameThread:
def __init__(self, screen):
self.running = False
self._wait = False
self.screen = screen
def Start(self):
self.running = True
thread.start_new_thread(self.Run, ())
def Stop(self):
self.running = False
def IsRunning(self):
return self.running
def Run(self):
m = self.map
kh = KeyHandler()
while self.running:
while self._wait: #lock the update
pass
events = kh.getEvents()
m.update(events)
self.running = False
def LockUpdate(self):
self._wait = True
def ReleaseUpdate(self):
self._wait = False
def SetMap(self, map):
self.LockUpdate()
self.map = map
self.ReleaseUpdate()
def NewMap(self, size):
#self.map = mapeditor.MapEditor(self.screen)
self.LockUpdate()
self.map.new(*size)
self.ReleaseUpdate()
def LoadMap(self, filename):
#self.map = mapeditor.MapEditor(self.screen)
self.LockUpdate()
self.map.open(filename)
self.ReleaseUpdate()
def SaveMap(self, filename):
self.LockUpdate()
self.map.save(filename)
self.ReleaseUpdate()
class PygamePanel(wx.Panel):
draw = False
dragOriginX = 0
dragOriginY = 0
def __init__(self, parent, wsize = wx.Size(700,500)):
wx.Panel.__init__(self, parent, -1, size = wsize)
self.hwnd = self.GetHandle()
if sys.platform == "win32":
os.environ['SDL_VIDEODRIVER'] = 'windib'
os.environ['SDL_WINDOWID'] = str(self.hwnd) #must be before pygame init
pygame.init()
self.screen = pygame.display.set_mode(wsize)
self.pygamethread = PygameThread(self.screen)
#workaround to use the mapeditor in the thread
self.map = mapeditor.MapEditor(self.screen)
self.map.open("maps/testmap.txt")
self.pygamethread.SetMap(self.map)
self.pygamethread.Start()
def __del__(self):
self.pygamethread.Stop()
if __name__ == "__main__":
app = wx.App()
frame = wxMapEditor()
frame.Show()
app.MainLoop()