forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_field.lua
More file actions
222 lines (184 loc) · 5.42 KB
/
Copy pathedit_field.lua
File metadata and controls
222 lines (184 loc) · 5.42 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
local gui = require('gui')
local utils = require('utils')
local Widget = require('gui.widgets.widget')
local HotkeyLabel = require('gui.widgets.labels.hotkey_label')
local TextArea = require('gui.widgets.text_area')
local WrappedText = require('gui.widgets.text_area.wrapped_text')
local getval = utils.getval
OneLineWrappedText = defclass(OneLineWrappedText, WrappedText)
function OneLineWrappedText:update(text)
self.lines = {text}
end
TextFieldArea = defclass(TextFieldArea, TextArea)
TextFieldArea.ATTRS{
on_char = DEFAULT_NIL,
key = DEFAULT_NIL,
on_submit = DEFAULT_NIL,
on_submit2 = DEFAULT_NIL,
modal = false,
}
function TextFieldArea:onInput(keys)
if self.on_char and keys._STRING and keys._STRING ~= 0 then
if not self.on_char(string.char(keys._STRING), self:getText()) then
return self.modal
end
end
if keys.SELECT or keys.SELECT_ALL then
if self.key then
self:setFocus(false)
end
if keys.SELECT_ALL then
if self.on_submit2 then
self.on_submit2(self:getText())
return true
end
else
if self.on_submit then
self.on_submit(self:getText())
return true
end
end
return not not self.key
end
return TextFieldArea.super.onInput(self, keys)
end
function TextFieldArea:getPreferredFocusState()
-- allow EditField to manage focus
return false
end
function TextFieldArea:hasFocus()
return self.parent_view.focus
end
function TextFieldArea:setFocus(focus)
return self.parent_view:setFocus(focus)
end
----------------
-- Edit field --
----------------
---@class widgets.EditField.attrs: widgets.Widget.attrs
---@field label_text? string
---@field text string
---@field text_pen? dfhack.color|dfhack.pen
---@field on_char? function
---@field on_change? function
---@field on_submit? function
---@field on_submit2? function
---@field key? string
---@field key_sep? string
---@field modal boolean
---@field ignore_keys? string[]
---@class widgets.EditField.attrs.partial: widgets.EditField.attrs
---@class widgets.EditField: widgets.Widget, widgets.EditField.attrs
---@field super widgets.Widget
---@field ATTRS widgets.EditField.attrs|fun(attributes: widgets.EditField.attrs.partial)
---@overload fun(init_table: widgets.EditField.attrs.partial): self
EditField = defclass(EditField, Widget)
EditField.ATTRS{
label_text = DEFAULT_NIL,
text = '',
text_pen = DEFAULT_NIL,
on_char = DEFAULT_NIL,
on_change = DEFAULT_NIL,
on_submit = DEFAULT_NIL,
on_submit2 = DEFAULT_NIL,
key = DEFAULT_NIL,
key_sep = DEFAULT_NIL,
modal = false,
ignore_keys = DEFAULT_NIL,
}
---@param init_table widgets.EditField.attrs
function EditField:preinit(init_table)
init_table.frame = init_table.frame or {}
init_table.frame.h = init_table.frame.h or 1
end
function EditField:init()
local function on_activate()
self:setFocus(true)
end
self.cursor = #self.text + 1
self.ignore_keys = self.ignore_keys or {}
self.label = HotkeyLabel{
frame={t=0,l=0},
key=self.key,
key_sep=self.key_sep,
label=self.label_text,
on_activate=self.key and on_activate or nil
}
self.text_area = TextFieldArea{
one_line_mode=true,
frame={t=0,r=0},
init_text=self.text or '',
text_pen=self.text_pen or COLOR_LIGHTCYAN,
modal=self.modal,
on_char=self.on_char,
key = self.key,
on_submit = self.on_submit,
on_submit2 = self.on_submit2,
ignore_keys={
table.unpack(self.ignore_keys)
},
on_text_change=self:callback('onTextAreaTextChange'),
on_cursor_change=function(cursor) self.cursor = cursor end
}
self:addviews{self.label, self.text_area}
self.text_area.frame.l = self.label:getTextWidth()
end
function EditField:setFocus(focus)
self.saved_text = self.text
return EditField.super.setFocus(self, focus)
end
function EditField:getPreferredFocusState()
return not self.key
end
function EditField:setCursor(cursor)
if not cursor then
cursor = #self.text + 1
end
self.cursor = cursor
self.text_area:setCursor(cursor)
self.cursor = self.text_area:getCursor()
end
function EditField:setText(text, cursor)
text = text or ''
local old = self.text
self.text = text
self.text_area:setText(text)
self:setCursor(cursor)
if self.on_change and text ~= old then
self.on_change(self.text, old)
end
end
function EditField:onTextAreaTextChange(text)
if self.text ~= text then
local old = self.text
self.text = text
if self.on_change then
self.on_change(self.text, old)
end
end
end
function EditField:insert(text)
local old = self.text
self:setText(
old:sub(1,self.cursor-1)..text..old:sub(self.cursor),
self.cursor + #text
)
end
function EditField:onInput(keys)
if keys._MOUSE_L and self:getMousePos() then
self:setFocus(true)
end
if not self.focus then
return self.label:onInput(keys)
end
if self.key and (keys.LEAVESCREEN or keys._MOUSE_R) then
self:setFocus(false)
return true
end
if self.text_area:onInput(keys) then
return true
end
-- if we're modal, then unconditionally eat all the input
return self.modal
end
return EditField