Added file selection. Working ok.

This commit is contained in:
David Lannan 2024-11-19 00:57:04 +10:30
parent 4af9000c40
commit de2fdb1747
4 changed files with 130 additions and 9 deletions

View File

@ -21,7 +21,7 @@ local default = {
project = {
project_name = { index = 1, value = "default", ptype = "string", slen = 64 },
project_path = { index = 2, value = "F:\\dev\\luajit\\Thunc\\sokol-luajit", ptype = "path", slen = 256 }, -- Always starts empty. Will support a project file.
project_file = { index = 3, value = "default.slp", ptype = "file", slen = 64 }, -- Sokol luajit project file
project_file = { index = 3, value = "F:\\dev\\luajit\\Thunc\\sokol-luajit\\default.slp", ptype = "file", slen = 64 }, -- Sokol luajit project file
project_start = { index = 4, value = "example1.lua", ptype = "file", slen = 128 },
},
@ -40,7 +40,7 @@ local default = {
},
sokol = {
sokol_path = { index = 1, value = "../../../../", ptype = "path", slen = 256 },
sokol_path = { index = 1, value = "F:\\dev\\luajit\\Thunc\\sokol-luajit", ptype = "path", slen = 256 },
sokol_bin = { index = 2, value = "bin/", ptype = "path", slen = 256 },
sokol_ffi = { index = 3, value = "ffi/", ptype = "path", slen = 256 },
sokol_lua = { index = 4, value = "lua/", ptype = "path", slen = 256 },

View File

@ -74,8 +74,68 @@ local tabs = {
-- --------------------------------------------------------------------------------------
local file_select = {
popup_active = 0,
prop = nil,
file_selected = "",
popup_dim = ffi.new("struct nk_rect",{20, 100, 220, 90}),
folder_path = ".",
hit = {0, "", 0},
ui_func = function(ctx, dim, udata)
nk.nk_layout_row_dynamic(ctx, dim.h-20, 1)
local flags = bit.bor(nk.NK_WINDOW_BORDER, nk.NK_WINDOW_TITLE)
flags = bit.bor(flags, nk.NK_WINDOW_NO_SCROLLBAR)
if (nk.nk_group_begin(ctx, "Select File", flags) == true) then
nk.nk_layout_row_dynamic(ctx, dim.h-90, 1)
local files = dirtools.get_dirlist(udata.folder_path)
if(#files == 0 or files[1].name ~= "..") then table.insert(files, 1, { name = ".." }) end
local bhit = wdgts.widget_list_buttons(ctx, "select_file", nil, files, dim.w -40 )
if(bhit) then
udata.hit = bhit
if(bhit[2] == "..") then
udata.folder_path = dirtools.get_folder(udata.folder_path)
if(udata.folder_path == nil or udata.folder_path == "") then udata.folder_path = "." end
else
local isfolder = dirtools.is_folder(udata.folder_path.."\\"..udata.hit[2])
print(isfolder)
if(isfolder == true) then
udata.folder_path = udata.folder_path.."\\"..udata.hit[2]
else
udata.file_selected = udata.folder_path.."\\"..udata.hit[2]
end
end
end
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "Cancel") == true) then
udata.popup_active = 0
end
if (nk.nk_button_label(ctx, "OK") == true) then
udata.popup_active = 0
if(udata.prop) then
udata.prop.value = udata.file_selected
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
end
nk.nk_group_end(ctx)
end
if(udata.popup_active == 0) then nk.nk_popup_close(ctx) end
return udata.popup_active
end,
}
-- --------------------------------------------------------------------------------------
local folder_select = {
popup_active = 0,
prop = nil,
popup_dim = ffi.new("struct nk_rect",{20, 100, 220, 90}),
folder_path = ".",
hit = {0, "", 0},
@ -100,7 +160,6 @@ local folder_select = {
else
udata.folder_path = udata.folder_path.."\\"..udata.hit[2]
end
print(udata.folder_path)
end
nk.nk_layout_row_dynamic(ctx, 25, 2)
@ -109,6 +168,13 @@ local folder_select = {
end
if (nk.nk_button_label(ctx, "OK") == true) then
udata.popup_active = 0
if(udata.prop) then
udata.prop.value = udata.folder_path
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
end
nk.nk_group_end(ctx)
end
@ -136,13 +202,11 @@ for sectionname, section in pairs(config) do
elseif(prop.ptype == "float") then
prop.ffi = ffi.new("float[1]", prop.value)
elseif(prop.ptype == "path") then
print(prop.value)
prop.ffi = ffi.new("char[?]", prop.slen )
ffi.fill(prop.ffi, prop.slen, 0)
ffi.copy(prop.ffi, ffi.string(prop.value))
prop.len_ffi = ffi.new("int[1]", {string.len(prop.value)})
elseif(prop.ptype == "file") then
print(prop.value)
prop.ffi = ffi.new("char[?]", prop.slen )
ffi.fill(prop.ffi, prop.slen, 0)
ffi.copy(prop.ffi, ffi.string(prop.value))
@ -193,6 +257,7 @@ local function display_section(ctx, sectionname)
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
end
nk.nk_style_set_font(ctx, myfonts[3].handle)
@ -201,7 +266,11 @@ local function display_section(ctx, sectionname)
nk.nk_edit_string(ctx, nk.NK_EDIT_SIMPLE, v.ffi, v.len_ffi, v.slen, nk.nk_filter_default)
nk.nk_style_set_font(ctx, myfonts[1].handle)
nk.nk_layout_row_push(ctx, 30)
nk.nk_button_label(ctx, "")
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)
end
nk.nk_style_set_font(ctx, myfonts[3].handle)
elseif(v.ptype == "combo") then
nk.nk_layout_row_push(ctx, value_col)
@ -279,6 +348,7 @@ local function main_ui(ctx)
wdgts.widget_panel_fixed(ctx, "WinMain", 0, 0, sapp.sapp_width(), sapp.sapp_height(), 0, function(data)
folder_select.popup_active = wdgts.widget_popup_panel(ctx, "popup", folder_select.popup_dim, folder_select.ui_func, folder_select, folder_select.popup_active)
file_select.popup_active = wdgts.widget_popup_panel(ctx, "popup", file_select.popup_dim, file_select.ui_func, file_select, file_select.popup_active)
nk.nk_style_set_font(ctx, myfonts[4].handle)
@ -340,6 +410,11 @@ local function init()
folder_select.popup_dim.h = sapp.sapp_height()* 0.7
folder_select.popup_dim.x = sapp.sapp_width()/2-8 - folder_select.popup_dim.w/2
folder_select.popup_dim.y = sapp.sapp_height()/2-40 - folder_select.popup_dim.h/2
file_select.popup_dim.w = sapp.sapp_width()* 0.4
file_select.popup_dim.h = sapp.sapp_height()* 0.7
file_select.popup_dim.x = sapp.sapp_width()/2-8 - file_select.popup_dim.w/2
file_select.popup_dim.y = sapp.sapp_height()/2-40 - file_select.popup_dim.h/2
end
-- --------------------------------------------------------------------------------------

View File

@ -258,16 +258,30 @@ widgets.widget_list_buttons = function(ctx, title, flags, items, width)
local rounding = ctx.style.button.rounding
ctx.style.button.rounding = 0
ctx.style.button.text_alignment = nk.NK_TEXT_LEFT
local col = ctx.style.button.text_normal
local hcol = ctx.style.button.text_hover
local tcolor = nk.nk_rgb(col.r, col.g, col.b)
local thcolor = nk.nk_rgb(hcol.r, hcol.g, hcol.b)
flags = flags or nk.NK_WINDOW_BORDER -- default setup
if (nk.nk_group_begin(ctx, title, flags) == true) then
for i, item in ipairs(items) do
nk.nk_layout_row_static(ctx, 22, width, 1)
if(item.folder == true) then
ctx.style.button.text_normal = nk.nk_rgb(100, 100, 255)
ctx.style.button.text_hover = nk.nk_rgb(160, 160, 255)
end
if(nk.nk_button_label(ctx, item.name) == true) then
pressed = {i, item.name}
end
ctx.style.button.text_normal = tcolor
end
nk.nk_group_end(ctx)
end
ctx.style.button.text_hover = thcolor
ctx.style.button.text_normal = tcolor
ctx.style.button.rounding = rounding
ctx.style.button.text_alignment = button_align
return pressed

View File

@ -25,19 +25,51 @@ dirtools.add_default_paths = function(path)
package.path = package.path..";"..path.."/?.lua"
end
local sep = "\\"
if(ffi.os ~= "Windows") then sep = "/" end
---------------------------------------------------------------------------------------
-- Much safer way to build folder than pattern match (very unstable)
dirtools.get_folder = function(path)
local parts = {}
local sep = "\\"
local patt = "(.-)[\\]"
if(ffi.os ~= "Windows") then patt = "(.-)[/]"; sep = "/" end
if(ffi.os ~= "Windows") then patt = "(.-)[/]" end
for pseg in string.gmatch(path, patt) do
tinsert(parts, pseg)
end
if(#parts < 1) then return path end
return tconcat(parts, sep)
end
---------------------------------------------------------------------------------------
-- Check if the path is folder or file
if(ffi.os == "Windows") then
dirtools.is_folder = function(path)
local fh = io.popen("attrib "..path, "r")
if(fh) then
local res = fh:read("*a")
local fileattr = string.sub(res, 1, 20)
fileattr = string.gsub(fileattr, " ", "")
fh:close()
return (#fileattr == 0)
end
return false
end
else
dirtools.is_folder = function(path)
local fh = io.popen("file "..path, "r")
if(fh) then
local res = fh:read("*a")
local folder = string.match(res, ".-: directory$")
fh:close()
return (folder ~= nil)
end
return false
end
end
---------------------------------------------------------------------------------------
dirtools.get_app_path = function( expected_root_folder )
@ -90,7 +122,7 @@ dirtools.get_dirlist = function(path, cache_update)
end
for f in string.gmatch(res, "(.-)\n") do
local newfile = { name = ffi.string(f)}
local newfile = { name = ffi.string(f), folder = dirtools.is_folder(path..sep..f) }
newfile.select = ffi.new("int[1]")
newfile.select[0] = 0
table.insert(files, newfile)