forked from kivutar/permadev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
157 lines (133 loc) · 4.08 KB
/
ui.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
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 Editor:
def __init__(self, x, y, entity):
self.x = x
self.y = y
self.width = 40
self.height = 24
self.entity = entity
self.text = self.entity.ai_text
self.cursorPos = len(self.text)
self.saveBtn = Button(self.x, self.y+self.height-1, "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.draw_frame(self.x, self.y, self.width, self.height, self.entity.name, True, tcod.Color(168,168,168), tcod.Color(0,0,168))
con.print(self.x+1, self.y+2, self.text, tcod.Color(168,168,168), tcod.Color(0,0,168))
tcod.console_set_char_background(con, self.x+1+px, self.y+2+py, tcod.Color(0,168,168))
self.saveBtn.draw(con, mouse)