Skip to content

Commit a04aa70

Browse files
committed
Add df.type.find() dialog to gm-editor
1 parent 9e3bf18 commit a04aa70

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

gui/gm-editor.lua

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ local widgets =require 'gui.widgets'
2525
local guiScript = require 'gui.script'
2626
local args={...}
2727

28+
find_funcs = find_funcs or (function()
29+
local t = {}
30+
for k in pairs(df) do
31+
pcall(function()
32+
t[k] = df[k].find
33+
end)
34+
end
35+
return t
36+
end)()
37+
2838
local keybindings={
2939
offset={key="CUSTOM_ALT_O",desc="Show current items offset"},
3040
find={key="CUSTOM_F",desc="Find a value by entering a predicate"},
41+
find_id={key="CUSTOM_I",desc="Find object with this ID"},
3142
lua_set={key="CUSTOM_ALT_S",desc="Set by using a lua function"},
3243
insert={key="CUSTOM_ALT_I",desc="Insert a new value to the vector"},
3344
delete={key="CUSTOM_ALT_D",desc="Delete selected entry"},
@@ -62,6 +73,24 @@ function getTargetFromScreens()
6273
end
6374
return my_trg
6475
end
76+
function search_relevance(search, candidate)
77+
local function clean(str)
78+
return ' ' .. str:lower():gsub('[^a-z0-9]','') .. ' '
79+
end
80+
search = clean(search)
81+
candidate = clean(candidate)
82+
local ret = 0
83+
while #search > 0 do
84+
local pos = candidate:find(search:sub(1, 1), 1, true)
85+
if pos then
86+
ret = ret + (#search - pos)
87+
candidate = candidate:sub(pos + 1)
88+
end
89+
search = search:sub(2)
90+
end
91+
return ret
92+
end
93+
6594

6695
GmEditorUi = defclass(GmEditorUi, gui.FramedScreen)
6796
GmEditorUi.ATTRS={
@@ -135,7 +164,7 @@ function GmEditorUi:find(test)
135164

136165
local e,what=load("return function(k,v) return "..test.." end")
137166
if e==nil then
138-
dialog.showMessage("Error!","function failed to compile\n"..what,COLOR_RED)
167+
dialog.showMessage("Error!","function failed to compile\n"..what,COLOR_LIGHTRED)
139168
end
140169

141170
if trg.target and trg.target._kind and trg.target._kind=="container" then
@@ -157,6 +186,29 @@ function GmEditorUi:find(test)
157186
end
158187
end
159188
end
189+
function GmEditorUi:find_id()
190+
local key = self:getSelectedKey()
191+
local id = tonumber(self:getSelectedValue())
192+
if not id then return end
193+
local opts = {}
194+
for name, func in pairs(find_funcs) do
195+
table.insert(opts, {text=name, callback=func, weight=search_relevance(key, name)})
196+
end
197+
table.sort(opts, function(a, b)
198+
return a.weight > b.weight
199+
end)
200+
guiScript.start(function()
201+
local ret,idx,choice=guiScript.showListPrompt("Choose type:",nil,3,opts,nil,true)
202+
if ret then
203+
local obj = choice.callback(id)
204+
if obj then
205+
self:pushTarget(obj)
206+
else
207+
dialog.showMessage("Error!", ('%s with ID %d not found'):format(choice.text, id), COLOR_LIGHTRED)
208+
end
209+
end
210+
end)
211+
end
160212
function GmEditorUi:insertNew(typename)
161213
local tp=typename
162214
if typename== nil then
@@ -165,7 +217,7 @@ function GmEditorUi:insertNew(typename)
165217
end
166218
local ntype=df[tp]
167219
if ntype== nil then
168-
dialog.showMessage("Error!","Type '"..tp.." not found",COLOR_RED)
220+
dialog.showMessage("Error!","Type '"..tp.." not found",COLOR_LIGHTRED)
169221
return
170222
end
171223

@@ -187,20 +239,23 @@ end
187239
function GmEditorUi:getSelectedKey()
188240
return self:currentTarget().keys[self.subviews.list_main:getSelected()]
189241
end
242+
function GmEditorUi:getSelectedValue()
243+
return self:currentTarget().target[self:getSelectedKey()]
244+
end
190245
function GmEditorUi:currentTarget()
191246
return self.stack[#self.stack]
192247
end
193248
function GmEditorUi:getSelectedEnumType()
194249
local trg=self:currentTarget()
195250
local trg_key=trg.keys[self.subviews.list_main:getSelected()]
196-
251+
197252
local ok,ret=pcall(function () --super safe way to check if the field has enum
198253
return trg.target._field==nil or trg.target:_field(trg_key)==nil
199254
end)
200255
if not ok or ret==true then
201256
return nil
202257
end
203-
258+
204259
local enum=trg.target:_field(trg_key)._type
205260
if enum._kind=="enum-type" then
206261
return enum
@@ -298,7 +353,7 @@ function GmEditorUi:set(key,input)
298353
end
299354
local e,what=load("return function(v) return "..input.." end")
300355
if e==nil then
301-
dialog.showMessage("Error!","function failed to compile\n"..what,COLOR_RED)
356+
dialog.showMessage("Error!","function failed to compile\n"..what,COLOR_LIGHTRED)
302357
return
303358
end
304359
trg.target[key]=e()(trg)
@@ -331,6 +386,8 @@ function GmEditorUi:onInput(keys)
331386
self:openOffseted(self.subviews.list_main:getSelected())
332387
elseif keys[keybindings.find.key] then
333388
self:find()
389+
elseif keys[keybindings.find_id.key] then
390+
self:find_id()
334391
elseif keys[keybindings.lua_set.key] then
335392
self:set(self:getSelectedKey())
336393
elseif keys[keybindings.insert.key] then --insert

0 commit comments

Comments
 (0)