-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.py
268 lines (168 loc) · 5.45 KB
/
Window.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
import sys
import os
import libtcodpy as libtcod
import weakref
#TODO handle more colors, easier acces
class Palette:
def __init__(self,data=None):
if not data:
data=[(0,0,0),(0,0,128),(0,128,0),(0,128,128),(128,0,0),(128,0,128),(128,128,0),(192,192,192),(128,128,128),(0,0,255),(0,255,0),(0,255,255),(255,0,0),(255,0,255),(255,255,0),(255,255,255)]
print data[0]
self.BLACK=libtcod.Color(*data[0])
self.BLUE=libtcod.Color(*data[1])
self.GREEN=libtcod.Color(*data[2])
self.CYAN=libtcod.Color(*data[3])
self.RED=libtcod.Color(*data[4])
self.MAGENTA=libtcod.Color(*data[5])
self.BROWN=libtcod.Color(*data[6])
self.LGREY=libtcod.Color(*data[7])
self.DGREY=libtcod.Color(*data[8])
self.LBLUE=libtcod.Color(*data[9])
self.LGREEN=libtcod.Color(*data[10])
self.LCYAN=libtcod.Color(*data[11])
self.LRED=libtcod.Color(*data[12])
self.LMAGENTA=libtcod.Color(*data[13])
self.YELLOW=libtcod.Color(*data[14])
self.WHITE=libtcod.Color(*data[15])
class W_Window:
def __init__(self,parent,width,height):
self.parent = weakref.ref(parent)
self.width=width
self.height=height
self.console=libtcod.console_new(width,height)
def get_palette(self):
pal=self.parent().get_palette()
return pal
class Main_Window(W_Window):
def __init__(self,parent,width,height,palette=False):
W_Window.__init__(self,parent,width,height)
#self.window=libtcod.console_new(width,height)
self.sub_windows=list()
if not palette:
palette=Palette()
self.color=palette
def get_palette(self):
return self.color
def create_window(self,width,height,rx,ry,id):
wind=Simple_Window(self,width,height,rx,ry,id)
self.sub_windows.append(wind)
return wind
def build(self):
for window in self.sub_windows:
window.build()
def get_elem_by_mouse(self):
'''
return the Wui_elem/Window under the mouse
'''
potList=list()
x=self.parent().mouse.cx
y=self.parent().mouse.cy
for window in self.sub_windows:
if window.is_in(x,y):
potList.append(window)
if len(potList) != 1:
print 'Unimplemented yet, see Z-Levels for windows'
target=potList[len(potList)-1]
else:
target=potList[0]
result=target.get_elem_by_mouse(x-target.rx,y-target.ry)
return result
def render(self):
for window in self.sub_windows:
#window.render()
libtcod.console_blit(window.console,0,0,window.width,window.height,self.console,window.rx,window.ry)
libtcod.console_blit(self.console,0,0,self.width,self.height,0,0,0)
libtcod.console_flush()
class Sub_Window(W_Window):
def __init__(self,parent,width,height,rx,ry,id):
W_Window.__init__(self,parent,width,height)
self.rx=rx
self.ry=ry
self.id=id
def get_elem_by_mouse(self,x,y):
'''
parent method to get an elem by mouse position
:param x : mouse x pos relative to window
:param y : mouse y pos relative to window
'''
return self
def is_in(self,x,y):
'''Return true if the x,y tile is in the window
note: window store its x,y relative to parent
'''
return (self.rx <= x and
self.rx + self.width > x and
self.ry <= y and
self.ry +self.height > y)
class Simple_Window(Sub_Window):
def __init__(self,parent,width,height,rx,ry,id):
Sub_Window.__init__(self,parent,width,height,rx,ry,id)
self.content=None
self.bk_color=False
self.color=False
def set_content(self,elem):
self.content=elem
def add_elem(self,elem):
self.set_content(elem)
def set_bk_color(self,color):
self.bk_color=color
def set_color(self,color):
self.color=color
def get_bk_color(self):
if self.bk_color:
return self.bk_color
else:
return self.get_palette().DGREY
def get_color(self):
if self.color:
return self.color
else:
return self.get_palette().BLUE
def get_elem_by_mouse(self,x,y):
'''
method to get an elem by mouse position
chain to content
:param x : mouse x pos relative to window
:param y : mouse y pos relative to window
'''
if not self.content:
ret= self
else:
ret= self.content.get_elem_by_mouse(x-1,y-1)
return ret
def build(self):
print 'built!'
libtcod.console_set_default_foreground(self.console,self.get_color())
libtcod.console_set_default_background(self.console,self.get_bk_color())
libtcod.console_print_frame(self.console,0,0,self.width,self.height,True,libtcod.BKGND_SET,self.id)
temp=libtcod.console_new(self.width-2,self.height-2)
y=1
if not self.content:
print 'fenetre vide'
else:
#TODO: redo this, exeptions
self.content.build(temp)
libtcod.console_blit(temp,0,0,self.content.width,self.content.height,self.console,1,y)
class Folding_Window(Sub_Window):
def __init__(self,parent,width,height,rx,ry,id):
Sub_Window.__init__(self,parent,width,height,rx,ry,id)
self.content=list()
#self.mode=
def add_elem(self,elem):
self.content.append(elem)
def build(self):
print 'built!'
libtcod.console_set_default_foreground(self.console,self.get_palette().LBLUE)
libtcod.console_set_default_background(self.console,self.get_palette().DGREY)
libtcod.console_print_frame(self.console,0,0,self.width,self.height,True,libtcod.BKGND_SET,'fenetre1')
temp=libtcod.console_new(self.width-2,self.height-2)
libtcod.console_set_default_foreground(temp,self.get_palette().LBLUE)
libtcod.console_set_default_background(temp,self.get_palette().DGREY)
y=1
for elem in self.content:
libtcod.console_clear(temp)
height=elem.build(temp)
libtcod.console_blit(temp,0,0,elem.width,elem.height,self.console,1,y)
y+=height
def render(self):
pass