Fixed file selection

This commit is contained in:
David Lannan 2024-11-22 10:29:42 +10:30
parent 910f94561a
commit 743c7ff809
3 changed files with 91 additions and 34 deletions

View File

@ -2,6 +2,7 @@
local ffi = require("ffi")
local json = require("lua.json")
-- ------------------------------------------------------------------------------------------------------
@ -10,14 +11,12 @@ local json = require("lua.json")
local settings = {}
-- Default settings if none are loaded or found
local default_settings = [[
local platforms = { "Win64", "MacOS", "Linux", "IOS64" }
local resolutions = { "1920x1080", "1680x1050", "1600x900", "1440x900", "1376x768" }
local arch = { "x86", "x64", "arm", "arm64", "arm64be", "ppc", "mips", "mipsel", "mips64", "mips64el", "mips64r6", "mips64r6el" }
local oss = { "Windows", "Linux", "OSX", "BSD", "POSIX", "Other" }
local default = {
local default_settings = {
rbuilder = {
appname = { index = 1, value = "rbuild", slen = 7 },
@ -73,9 +72,6 @@ local default = {
},
}
return default
]]
-- ------------------------------------------------------------------------------------------------------
local function loadconfig(projectpath)
@ -85,7 +81,12 @@ local function loadconfig(projectpath)
if(fh) then
res = fh:read("*a")
fh:close()
res = load(res)()
if(res == nil or #res == 0) then
print("[Error] loadconfig - cannot read settings file: "..projectpath)
res = nil
else
res = json.decode(res)
end
print(res)
else
print("[Error] settings.load - unable to load config: "..projectpath)
@ -107,7 +108,7 @@ settings.load = function( projectpath )
if( projectpath == nil ) then
print("[Error] settings.load invalid projectpath nil, using default settings.")
settings.config = load(default_settings)()
settings.config = default_settings
else
-- Load the project settings into the local config
@ -121,6 +122,20 @@ end
-- ------------------------------------------------------------------------------------------------------
settings.save = function( projectpath )
-- Grab the recents file list too.
settings.recent = nil
print(projectpath)
local fh = io.open(projectpath, "w")
if(fh) then
fh:write( json.encode(settings.config) )
fh:close()
end
end
-- ------------------------------------------------------------------------------------------------------
return settings
-- ------------------------------------------------------------------------------------------------------

View File

@ -39,11 +39,7 @@ file_selector.create_file_selector = function( name, rect, start_folder, folders
local flags = bit.bor(nk.NK_WINDOW_BORDER, nk.NK_WINDOW_TITLE)
flags = bit.bor(flags, nk.NK_WINDOW_NO_SCROLLBAR)
local full_path = udata.file_selected
if(folders_only) then full_path = udata.folder_path end
local long_name = string.format("%s: %s", name, full_path)
if (nk.nk_group_begin(ctx, long_name, flags) == true) then
if (nk.nk_group_begin(ctx, name, flags) == true) then
nk.nk_layout_row_begin(ctx, nk.NK_DYNAMIC, 25, 2)
nk.nk_layout_row_push(ctx, 0.2)
@ -58,7 +54,7 @@ file_selector.create_file_selector = function( name, rect, start_folder, folders
nk.nk_layout_row_end(ctx)
if(udata.fs_type) then
nk.nk_layout_row_dynamic(ctx, dim.h-110, 1)
nk.nk_layout_row_dynamic(ctx, dim.h-135, 1)
local files = dirtools.get_folderslist(udata.folder_path)
local bhit = wdgts.widget_list_buttons(ctx, "folder_files", nil, files, dim.w -40 )
@ -69,9 +65,13 @@ file_selector.create_file_selector = function( name, rect, start_folder, folders
else
udata.folder_path = dirtools.change_folder(udata.folder_path, udata.hit[2])
end
local full_path = udata.file_selected
if(folders_only) then full_path = udata.folder_path end
ffi.copy(udata.prop.ffi, ffi.string(full_path))
udata.prop.len_ffi[0] = #full_path
end
else
nk.nk_layout_row_dynamic(ctx, dim.h-110, 1)
nk.nk_layout_row_dynamic(ctx, dim.h-135, 1)
local files = dirtools.get_dirlist(udata.folder_path)
local bhit = wdgts.widget_list_buttons(ctx, "select_file", nil, files, dim.w -40 )
@ -87,8 +87,24 @@ file_selector.create_file_selector = function( name, rect, start_folder, folders
udata.file_selected = dirtools.change_folder(udata.folder_path, udata.hit[2])
end
end
local full_path = udata.file_selected
if(folders_only) then full_path = udata.folder_path end
ffi.copy(udata.prop.ffi, ffi.string(full_path))
udata.prop.len_ffi[0] = #full_path
end
end
nk.nk_layout_row_dynamic(ctx, 25, 1)
local edit_evt = nk.nk_edit_string(ctx, nk.NK_EDIT_SIMPLE, udata.prop.ffi, udata.prop.len_ffi, udata.prop.slen, nk.nk_filter_default)
if(edit_evt == 1 or edit_evt == 10) then
if(folders_only) then
udata.folder_path = ffi.string(udata.prop.ffi)
else
udata.file_selected = ffi.string(udata.prop.ffi)
end
end
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "Cancel") == true) then
udata.popup_active = 0
@ -98,17 +114,6 @@ file_selector.create_file_selector = function( name, rect, start_folder, folders
end
if (nk.nk_button_label(ctx, "OK") == true) then
udata.popup_active = 0
if(udata.prop) then
if(folders_only) then
udata.prop.value = udata.folder_path
else
udata.prop.value = udata.file_selected
end
udata.prop.ffi = ffi.new("char[?]", udata.prop.slen )
ffi.fill(udata.prop.ffi, udata.prop.slen, 0)
ffi.copy(udata.prop.ffi, ffi.string(udata.prop.value))
udata.prop.len_ffi = ffi.new("int[1]", {string.len(udata.prop.value)})
end
if(udata.callback) then
udata.callback(udata, true)
end
@ -125,7 +130,21 @@ end
-- --------------------------------------------------------------------------------------
file_selector.open = function(select_obj, initial_path, prop, callback)
if(prop == nil) then
prop = { ffi = ffi.new("char[?]", 256 ), slen = 256, value = initial_path }
ffi.fill(prop.ffi, #initial_path, 0)
ffi.copy(prop.ffi, ffi.string(initial_path))
prop.len_ffi = ffi.new("int[1]", {#initial_path})
end
select_obj.popup_active = 1
select_obj.prop = prop
select_obj.folder_path = dirtools.get_folder(prop.value)
select_obj.drives = dirtools.get_drives()
if(callback) then select_obj.callback = callback end
end
-- --------------------------------------------------------------------------------------

View File

@ -194,10 +194,7 @@ local function display_section(ctx, sectionname)
nk.nk_style_set_font(ctx, myfonts[1].handle)
nk.nk_layout_row_push(ctx, 30)
if(nk.nk_button_label(ctx, "") == true) then
folder_select.popup_active = 1
folder_select.prop = v
folder_select.folder_path = v.value
folder_select.drives = dirtools.get_drives()
fsel.open(folder_select, v.value, v)
end
nk.nk_style_set_font(ctx, myfonts[3].handle)
elseif(v.ptype == "file") then
@ -206,10 +203,7 @@ local function display_section(ctx, sectionname)
nk.nk_style_set_font(ctx, myfonts[1].handle)
nk.nk_layout_row_push(ctx, 30)
if(nk.nk_button_label(ctx, "") == true) then
file_select.popup_active = 1
file_select.prop = v
file_select.folder_path = dirtools.get_folder(v.value)
file_select.drives = dirtools.get_drives()
fsel.open(file_select, v.value, v)
end
nk.nk_style_set_font(ctx, myfonts[3].handle)
elseif(v.ptype == "combo") then
@ -352,12 +346,15 @@ panel.main_ui = function(ctx)
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Open", nk.NK_TEXT_LEFT)) then
panel.loadconfig()
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Save", nk.NK_TEXT_LEFT)) then
panel.saveconfig(true)
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "SaveAs", nk.NK_TEXT_LEFT)) then
panel.saveconfig(true)
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Quit", nk.NK_TEXT_LEFT)) then
@ -446,6 +443,32 @@ end
-- --------------------------------------------------------------------------------------
panel.loadconfig = function()
fsel.open(file_select, ".", nil,
function(udata, res)
if(res == true) then
local name=ffi.string(udata.file_selected)
settings.load(name)
end
end)
end
-- --------------------------------------------------------------------------------------
panel.saveconfig = function(saveas)
fsel.open(file_select, ".", nil,
function(udata, res)
if(res == true) then
local name=ffi.string(udata.file_selected)
settings.save(name)
end
end)
end
-- --------------------------------------------------------------------------------------
return panel
-- --------------------------------------------------------------------------------------