sokol-luajit/tools/build/rbuilder/ui/panels.lua

379 lines
14 KiB
Lua
Raw Normal View History

2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
local ffi = require("ffi")
local dirtools = require("tools.dirtools")
local sapp = require("sokol_app")
sg = require("sokol_nuklear")
local nk = sg
local slib = require("sokol_libs") -- Warn - always after gfx!!
-- --------------------------------------------------------------------------------------
local settings = require("config.settings")
local wdgts = require("utils.widgets")
local fonts = require("utils.fonts")
2024-11-21 08:01:24 +08:00
local fsel = require("ui.file_selector")
2024-11-17 21:00:11 +08:00
local icons = ffi.new("struct nk_image [?]", 10)
2024-11-21 14:25:15 +08:00
local themes = require("lua.themes")
2024-11-22 20:09:25 +08:00
local logging = require("utils.logging")
2024-11-21 14:25:15 +08:00
2024-12-09 22:30:21 +08:00
local build = require("ui.panels_build")
local assets = require("ui.panels_assets")
-- --------------------------------------------------------------------------------------
2024-11-21 18:35:34 +08:00
local panel = {
2024-11-22 20:09:25 +08:00
config = nil,
current_path = ".",
2024-12-04 19:55:30 +08:00
2024-12-09 22:30:21 +08:00
build = build,
assets = assets,
2024-11-21 18:35:34 +08:00
}
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
-- Static vars
--
-- Note: Many vars are locally set or globals. This was from a direct C conversion.
-- To make static vars workaround then place them below.
local winrect = ffi.new("struct nk_rect[1]", {{10, 25, 1000, 600}})
-- --------------------------------------------------------------------------------------
local myfonts = nil
local font_list = {
{ font_file = "fontawesome-webfont.ttf", font_size = 16.0, range = nk.nk_font_awesome_glyph_ranges() },
{ font_file = "Rubik-Light.ttf", font_size = 16.0 },
{ font_file = "Rubik-Regular.ttf", font_size = 20.0 },
{ font_file = "Rubik-Bold.ttf", font_size = 21.0 },
}
2024-11-21 08:01:24 +08:00
local folder_select = nil
local file_select = nil
2024-11-22 20:09:25 +08:00
local recent_files = nil
2024-11-21 08:01:24 +08:00
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
-- Extract config and build ffi objects for them
-- Each config property shall have a shadow ffi property labeled with _ffi
local function setup_config()
2024-11-21 18:35:34 +08:00
for sectionname, section in pairs(panel.config) do
for key, prop in pairs(section) do
-- If ptype is nill or string its a string
if(prop.ptype == "string") then
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 == "combo") then
prop.ffi = ffi.new("int[1]", prop.value)
2024-12-09 22:30:21 +08:00
elseif(prop.ptype == "check") then
prop.ffi = ffi.new("bool[1]", prop.value)
elseif(prop.ptype == "int") then
prop.ffi = ffi.new("int[1]", prop.value)
elseif(prop.ptype == "float") then
prop.ffi = ffi.new("float[1]", prop.value)
elseif(prop.ptype == "path") then
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
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)})
end
2024-11-17 21:00:11 +08:00
end
end
2024-12-09 22:30:21 +08:00
assets.config = panel.config
assets.folder_select = folder_select
assets.file_select = file_select
build.config = panel.config
build.folder_select = folder_select
build.file_select = file_select
end
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
local pix = {}
2024-11-17 21:00:11 +08:00
2024-11-21 18:35:34 +08:00
panel.init = function()
local base_path = "./"
pix["baboon"] = wdgts.icon_load(base_path.."images/baboon.png")
-- pix["copy"] = icon_load(base_path.."icon/copy.png")
-- pix["del"] = icon_load(base_path.."icon/delete.png")
-- pix["rocket"] = icon_load(base_path.."icon/rocket.png")
-- pix["edit"] = icon_load(base_path.."icon/edit.png")
winrect[0].x = 0
winrect[0].y = 0
winrect[0].w = sapp.sapp_width()
winrect[0].h = sapp.sapp_height()
icons[0] = wdgts.icon_load("./icon/home.png")
icons[1] = wdgts.icon_load("./icon/phone.png")
icons[2] = wdgts.icon_load("./icon/plane.png")
icons[3] = wdgts.icon_load("./icon/wifi.png")
icons[4] = wdgts.icon_load("./icon/settings.png")
icons[5] = wdgts.icon_load("./icon/volume.png")
2024-11-21 08:01:24 +08:00
local popup_wide = sapp.sapp_width()* 0.4
local popup_high = sapp.sapp_height()* 0.7
local dim = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
folder_select = fsel.create_file_selector("Select Folder", dim, ".", true)
2024-11-21 08:01:24 +08:00
local dim2 = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
file_select = fsel.create_file_selector("Select File", dim2, ".")
2024-11-21 18:35:34 +08:00
panel.config = settings.load()
setup_config()
2024-11-22 20:09:25 +08:00
settings.load_recents()
2024-11-23 08:33:02 +08:00
popup_wide = sapp.sapp_width()* 0.8
local dim3 = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
recent_files = fsel.create_file_list("Choose Recent", dim3, {})
end
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
2024-11-22 20:09:25 +08:00
panel.show_recents = function(ctx)
panel.recent_list = {}
for i,v in ipairs(settings.recents) do
local newfile = { name = ffi.string(v) }
newfile.select = ffi.new("int[1]")
newfile.select[0] = 0
table.insert(panel.recent_list, newfile)
end
recent_files.recent_files = panel.recent_list
fsel.show(recent_files, function(udata, res)
2024-11-23 08:33:02 +08:00
if(res == true) then
panel.current_path = udata.file_selected
panel.config = settings.load(udata.file_selected)
setup_config()
2024-11-23 08:33:02 +08:00
settings.add_recents(udata.file_selected)
end
2024-11-22 20:09:25 +08:00
end)
end
-- --------------------------------------------------------------------------------------
2024-11-21 18:35:34 +08:00
panel.main_ui = function(ctx)
2024-11-17 21:00:11 +08:00
if(myfonts == nil) then
myfonts = fonts.setup_font(ctx, font_list)
2024-11-21 14:25:15 +08:00
wdgts.myfonts = myfonts
themes.tech(ctx)
2024-12-09 22:30:21 +08:00
assets.font = myfonts
build.font = myfonts
2024-11-17 21:00:11 +08:00
end
wdgts.widget_panel_fixed(ctx, "WinMain", 0, 0, sapp.sapp_width(), sapp.sapp_height(), 0, function(data)
2024-12-04 19:55:30 +08:00
panel.build.active = wdgts.widget_popup_panel(ctx, "popup_build", folder_select.popup_dim, panel.build.ui_func, panel.build, panel.build.active)
folder_select.popup_active = wdgts.widget_popup_panel(ctx, "popup_folder", folder_select.popup_dim, folder_select.ui_func, folder_select, folder_select.popup_active)
file_select.popup_active = wdgts.widget_popup_panel(ctx, "popup_fileselect", file_select.popup_dim, file_select.ui_func, file_select, file_select.popup_active)
recent_files.popup_active = wdgts.widget_popup_panel(ctx, "popup_recentfiles", recent_files.popup_dim, recent_files.ui_func, recent_files, recent_files.popup_active)
2024-11-17 21:00:11 +08:00
-- /* menubar */
local menu_states = { MENU_DEFAULT = 0, MENU_WINDOWS = 1}
nk.nk_menubar_begin(ctx)
-- /* menu #1 */
nk.nk_layout_row_begin(ctx, nk.NK_STATIC, 25, 2)
nk.nk_layout_row_push(ctx, 140)
2024-11-22 20:09:25 +08:00
if (nk.nk_menu_begin_label(ctx, "File", nk.NK_TEXT_LEFT, nk.nk_vec2(150, 240))) then
nk.nk_layout_row_dynamic(ctx, 35, 1)
2024-11-22 20:09:25 +08:00
if (nk.nk_menu_item_label(ctx, "New", nk.NK_TEXT_LEFT)== true) then
panel.newconfig()
end
2024-11-22 20:09:25 +08:00
nk.nk_layout_row_dynamic(ctx, 3, 1)
nk.nk_menu_item_label(ctx, "------------", nk.NK_TEXT_LEFT)
nk.nk_layout_row_dynamic(ctx, 35, 1)
2024-11-22 20:09:25 +08:00
if (nk.nk_menu_item_label(ctx, "Open", nk.NK_TEXT_LEFT)== true) then
2024-11-22 07:59:42 +08:00
panel.loadconfig()
end
2024-11-22 20:09:25 +08:00
nk.nk_layout_row_dynamic(ctx, 35, 1)
2024-11-22 20:09:25 +08:00
if(nk.nk_menu_item_label(ctx, "Open Recent", nk.NK_TEXT_LEFT) == true) then
panel.show_recents()
end
nk.nk_layout_row_dynamic(ctx, 3, 1)
nk.nk_menu_item_label(ctx, "------------", nk.NK_TEXT_LEFT)
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Save", nk.NK_TEXT_LEFT)== true) then
panel.saveconfig()
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
2024-11-22 20:09:25 +08:00
if (nk.nk_menu_item_label(ctx, "SaveAs", nk.NK_TEXT_LEFT)== true) then
2024-11-22 07:59:42 +08:00
panel.saveconfig(true)
end
2024-11-22 20:09:25 +08:00
nk.nk_layout_row_dynamic(ctx, 3, 1)
nk.nk_menu_item_label(ctx, "------------", nk.NK_TEXT_LEFT)
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Quit", nk.NK_TEXT_LEFT)) then
2024-11-22 20:09:25 +08:00
sapp.sapp_request_quit()
end
nk.nk_menu_end(ctx)
end
-- /* menu #2 */
nk.nk_layout_row_push(ctx, 60)
if (nk.nk_menu_begin_label(ctx, "Tools", nk.NK_TEXT_LEFT, nk.nk_vec2(200, 200))) then
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Build", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Build All", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Clean All", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Test Build", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Profile", nk.NK_TEXT_LEFT)) then
end
nk.nk_menu_end(ctx)
end
nk.nk_menubar_end(ctx)
nk.nk_layout_row_begin(ctx, nk.NK_STATIC, 15, 1)
nk.nk_layout_row_push(ctx, 140)
2024-11-17 21:00:11 +08:00
nk.nk_style_set_font(ctx, myfonts[4].handle)
local flags = nk.NK_WINDOW_BORDER
local height = sapp.sapp_height()
nk.nk_layout_row_dynamic(ctx, height-20, 2)
2024-11-21 08:13:37 +08:00
if (nk.nk_group_begin(ctx, "Settings", flags) == true) then
2024-11-17 21:00:11 +08:00
local padding = ctx[0].style.window.padding
ctx[0].style.window.padding = nk.nk_vec2(10,10)
2024-12-09 22:30:21 +08:00
build.panel(data.ctx)
if(build.run_config) then setup_config() end
2024-11-17 21:00:11 +08:00
nk.nk_group_end(ctx)
end
nk.nk_style_set_font(ctx, myfonts[4].handle)
if (nk.nk_group_begin(ctx, "Assets", flags) == true) then
2024-12-09 22:30:21 +08:00
assets.panel(data.ctx)
2024-11-17 21:00:11 +08:00
nk.nk_group_end(ctx)
end
end, {ctx=ctx})
2024-11-21 18:35:34 +08:00
-- return not nk.nk_window_is_closed(ctx, "Overview")
2024-11-17 21:00:11 +08:00
end
-- --------------------------------------------------------------------------------------
2024-11-21 18:35:34 +08:00
panel.input = function(event)
2024-11-17 21:00:11 +08:00
2024-11-21 18:35:34 +08:00
if(event.type == sapp.SAPP_EVENTTYPE_RESIZED) then
local popup_wide = sapp.sapp_width()* 0.4
local popup_high = sapp.sapp_height()* 0.7
local dim = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
folder_select.popup_dim = dim
local dim2 = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
file_select.popup_dim = dim2
2024-11-22 20:09:25 +08:00
2024-11-23 08:33:02 +08:00
popup_wide = sapp.sapp_width()* 0.8
local dim3 = nk.nk_rect( sapp.sapp_width()/2-8 - popup_wide/2,
sapp.sapp_height()/2-40 - popup_high/2, popup_wide, popup_high)
recent_files.popup_dim = dim3
2024-11-21 18:35:34 +08:00
end
end
-- --------------------------------------------------------------------------------------
2024-11-17 21:00:11 +08:00
panel.newconfig = function()
panel.config = settings.load(nil)
setup_config()
end
-- --------------------------------------------------------------------------------------
2024-11-22 07:59:42 +08:00
panel.loadconfig = function()
fsel.open(file_select, ".", nil,
function(udata, res)
if(res == true) then
local name=ffi.string(udata.file_selected)
2024-11-22 20:09:25 +08:00
panel.current_path = name
logging.info(" panel.loadconfig: Loaded path - "..name)
panel.config = settings.load(name)
setup_config()
2024-11-22 20:09:25 +08:00
settings.add_recents(name)
2024-11-22 07:59:42 +08:00
end
end)
end
-- --------------------------------------------------------------------------------------
panel.saveconfig = function(saveas)
2024-11-22 20:09:25 +08:00
local init_path = panel.current_path or "."
print("Init Path: "..panel.current_path)
if(saveas) then
fsel.open(file_select, init_path, nil,
function(udata, res)
if(res == true) then
local name=ffi.string(udata.file_selected)
logging.info(" panel.saveconfig: Saved path - "..name)
settings.save(name)
end
end)
else
settings.save(panel.current_path)
logging.info(" panel.saveconfig: Saved path - "..panel.current_path)
end
end
-- --------------------------------------------------------------------------------------
panel.cleanup = function()
settings.save_recents()
2024-12-04 19:55:30 +08:00
logging.cleanup()
2024-11-22 07:59:42 +08:00
end
-- --------------------------------------------------------------------------------------
2024-11-21 18:35:34 +08:00
return panel
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------