-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.py
More file actions
265 lines (215 loc) · 6.45 KB
/
Copy pathui.py
File metadata and controls
265 lines (215 loc) · 6.45 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
import tcod
import glo
class MenuBar:
def __init__(self, x, y, items):
self.x = x
self.y = y
self.width = 0
self.height = 1
self.items = items
def update(self, mouse, key):
if mouse.lbutton_pressed:
x = 0
for item in self.items:
w = len(item.get("name"))
if mouse.cy == self.y and mouse.cx >= self.x+x and mouse.cx <= self.x+x+w:
item.get("cb")()
def draw(self, con, mouse):
x = 0
for item in self.items:
bg = tcod.black
w = len(item.get("name"))
if mouse.cy == self.y and mouse.cx >= self.x+x and mouse.cx <= self.x+x+w:
bg = tcod.green
con.print(self.x+x, self.y, item.get("name"), tcod.white, bg)
x += w + 1
class Dropdown:
def __init__(self, x, y, items):
self.x = x
self.y = y
self.width = 12
self.height = len(items)+2
self.items = items
def update(self, mouse, key):
if mouse.lbutton_pressed:
for i, item in enumerate(self.items):
if mouse.cy == self.y+1+i and mouse.cx >= self.x and mouse.cx <= self.x+self.width:
item.get("cb")()
def draw(self, con, mouse):
con.draw_frame(self.x, self.y, self.width, self.height, "", True, tcod.white, tcod.black)
for i, item in enumerate(self.items):
bg = tcod.black
if mouse.cy == self.y+1+i and mouse.cx >= self.x and mouse.cx <= self.x+self.width:
bg = tcod.green
con.print(self.x+1, self.y+1+i, item.get("name"), tcod.white, bg)
class Button:
def __init__(self, x, y, text, cb):
self.x = x
self.y = y
self.text = text
self.width = len(self.text)
self.height = 1
self.cb = cb
def update(self, mouse, key):
if mouse.lbutton_pressed:
if mouse.cy == self.y and mouse.cx >= self.x and mouse.cx <= self.x+self.width:
self.cb()
def draw(self, con, mouse):
bg = tcod.black
if mouse.cy == self.y and mouse.cx >= self.x and mouse.cx <= self.x+self.width:
bg = tcod.green
con.print(self.x, self.y, self.text, tcod.white, bg)
class UnitDetails:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.entity = entity
self.width = 50
self.height = 24
self.active_tab = 0
self.tabs = [
{
"Button": Button(self.x+1, self.y+1, "AI ", lambda: self.setTab(0)),
"Pane": Editor(self.x+11, self.y+1, entity),
},
{
"Button": Button(self.x+1, self.y+2, "Logs ", lambda: self.setTab(1)),
"Pane": Logs(self.x+11, self.y+1, entity),
},
{
"Button": Button(self.x+1, self.y+3, "Equipment", lambda: self.setTab(2)),
"Pane": Equipments(self.x+11, self.y+1, entity),
},
{
"Button": Button(self.x+1, self.y+4, "Storage ", lambda: self.setTab(3)),
"Pane": Storage(self.x+11, self.y+1, entity),
},
]
def setTab(self, i):
self.active_tab = i
def update(self, mouse, key):
for t in self.tabs:
t.get("Button").update(mouse, key)
t.get("Pane").update(mouse, key)
def draw(self, con, mouse):
con.draw_frame(self.x, self.y, self.width, self.height, self.entity.name, True, tcod.Color(168,168,168), tcod.Color(0,0,168))
tcod.console_set_default_foreground(con, tcod.Color(168,168,168))
con.vline(self.x+10, self.y+1, self.height-2)
for t in self.tabs:
t.get("Button").draw(con, mouse)
tab = self.tabs[self.active_tab]
tab.get("Pane").draw(con, mouse)
class Editor:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.width = 40
self.height = 22
self.entity = entity
self.text = self.entity.ai_text
self.cursorPos = len(self.text)
self.saveBtn = Button(self.x, self.y+self.height, "SAVE", lambda: self.saveAndQuit())
def charToPos(self, c):
x = 0
y = 0
for i in range(c):
x += 1
if self.text[i] == '\n':
y += 1
x = 0
return (x, y)
def lines(self):
lines = 0
for i in range(len(self.text)):
if self.text[i] == '\n':
lines += 1
return lines
def clamp(self):
self.cursorPos = max(0, self.cursorPos)
self.cursorPos = min(len(self.text), self.cursorPos)
def saveAndQuit(self):
self.entity.ai_text = self.text
#glo.uis.remove(self)
def update(self, mouse, key):
if key.vk == tcod.KEY_LEFT:
self.cursorPos -= 1
self.clamp()
elif key.vk == tcod.KEY_RIGHT:
self.cursorPos += 1
self.clamp()
elif key.vk == tcod.KEY_DOWN:
px, py = self.charToPos(self.cursorPos)
for i in range(self.cursorPos, len(self.text)):
if self.text[i] == '\n':
self.cursorPos = i+1+px
self.clamp()
break
elif key.vk == tcod.KEY_UP:
px, py = self.charToPos(self.cursorPos)
for i in reversed(range(self.cursorPos)):
if self.text[i] == '\n':
self.cursorPos = i-1
self.clamp()
break
elif key.vk == tcod.KEY_BACKSPACE:
if self.cursorPos > 0:
self.cursorPos -= 1
self.clamp()
self.text = self.text[:self.cursorPos] + self.text[self.cursorPos+1:]
elif key.vk == tcod.KEY_TEXT:
self.text = self.text[:self.cursorPos] + key.text + self.text[self.cursorPos:]
self.cursorPos += 1
self.clamp()
elif key.vk == tcod.KEY_ENTER:
self.text = self.text[:self.cursorPos] + '\n' + self.text[self.cursorPos:]
self.cursorPos += 1
self.clamp()
self.saveBtn.update(mouse, key)
def draw(self, con, mouse):
px, py = self.charToPos(self.cursorPos)
con.print(self.x, self.y, self.text, tcod.Color(168,168,168), tcod.Color(0,0,168))
tcod.console_set_char_background(con, self.x+px, self.y+py, tcod.Color(0,168,168))
self.saveBtn.draw(con, mouse)
class Logs:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.width = 40
self.height = 22
self.entity = entity
def update(self, mouse, key):
pass
def draw(self, con, mouse):
for i, line in enumerate(self.entity.log):
if i >= self.height:
return
con.print(self.x, self.y+i, line, tcod.Color(168,168,168), tcod.Color(0,0,168))
class Equipments:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.width = 40
self.height = 24
self.entity = entity
def update(self, mouse, key):
pass
def draw(self, con, mouse):
for i, eq in enumerate(self.entity.equipments):
xx = 0
con.print(self.x, self.y+i, eq.name, tcod.Color(168,168,168), tcod.Color(0,0,168))
xx += len(eq.name)+1
for fn in eq.funcs:
con.print(self.x+xx, self.y+i, fn.__name__, tcod.Color(0,168,168), tcod.Color(0,0,168))
xx += len(fn.__name__)+1
class Storage:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.width = 40
self.height = 24
self.entity = entity
def update(self, mouse, key):
pass
def draw(self, con, mouse):
for i, item in enumerate(self.entity.items):
con.print(self.x, self.y+i, item.name, tcod.Color(168,168,168), tcod.Color(0,0,168))