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

564 lines
22 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")
local icons = ffi.new("struct nk_image [?]", 10)
local config = settings.load()
-- --------------------------------------------------------------------------------------
-- 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 },
}
-- --------------------------------------------------------------------------------------
local curr_tab = 1
local tabs = {
{
name = "Lua Source",
func = function(ctx)
local bounds = nk.nk_window_get_content_region(ctx)
nk.nk_layout_row_dynamic(ctx, bounds.h, 1)
local files = config.assets.lua
wdgts.widget_list_selectable(ctx, "source_files", nk.NK_WINDOW_BORDER, files, bounds.w -40 )
end,
2024-11-20 07:21:56 +08:00
asset_name = "lua",
2024-11-17 21:00:11 +08:00
},
{
name = "Images",
func = function(ctx)
local bounds = nk.nk_window_get_content_region(ctx)
nk.nk_layout_row_dynamic(ctx, bounds.h, 1)
local files = config.assets.images
wdgts.widget_list_selectable(ctx, "source_files", nk.NK_WINDOW_BORDER, files, bounds.w -40 )
end,
2024-11-20 07:21:56 +08:00
asset_name = "images",
2024-11-17 21:00:11 +08:00
},
{
name = "Data",
func = function(ctx)
local bounds = nk.nk_window_get_content_region(ctx)
nk.nk_layout_row_dynamic(ctx, bounds.h, 1)
local files = config.assets.data
wdgts.widget_list_selectable(ctx, "source_files", nk.NK_WINDOW_BORDER, files, bounds.w -40 )
end,
2024-11-20 07:21:56 +08:00
asset_name = "data",
2024-11-17 21:00:11 +08:00
}
}
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
2024-11-18 22:27:04 +08:00
local file_select = {
popup_active = 0,
prop = nil,
drives = {},
2024-11-18 22:27:04 +08:00
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_begin(ctx, nk.NK_DYNAMIC, 25, 2)
nk.nk_layout_row_push(ctx, 0.2)
nk.nk_label(ctx, "Drives: ", 1)
for k,v in ipairs(udata.drives) do
nk.nk_layout_row_push(ctx, 0.1)
if(nk.nk_button_label(ctx, v) == true) then
udata.folder_path = v
end
end
nk.nk_layout_row_end(ctx)
nk.nk_layout_row_dynamic(ctx, dim.h-110, 1)
2024-11-18 22:27:04 +08:00
local files = dirtools.get_dirlist(udata.folder_path)
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_parent(udata.folder_path)
2024-11-18 22:27:04 +08:00
else
local isfolder = files[udata.hit[1]].folder ~= nil
2024-11-18 22:27:04 +08:00
if(isfolder == true) then
udata.folder_path = dirtools.change_folder(udata.folder_path, udata.hit[2])
2024-11-18 22:27:04 +08:00
else
udata.file_selected = dirtools.change_folder(udata.folder_path, udata.hit[2])
2024-11-18 22:27:04 +08:00
end
end
end
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "Cancel") == true) then
udata.popup_active = 0
2024-11-20 07:21:56 +08:00
if(udata.callback) then
udata.callback(udata, false)
end
2024-11-18 22:27:04 +08:00
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
2024-11-20 07:21:56 +08:00
if(udata.callback) then
udata.callback(udata, true)
end
2024-11-18 22:27:04 +08:00
end
nk.nk_group_end(ctx)
end
if(udata.popup_active == 0) then nk.nk_popup_close(ctx) end
return udata.popup_active
end,
}
-- --------------------------------------------------------------------------------------
2024-11-17 21:00:11 +08:00
local folder_select = {
popup_active = 0,
2024-11-18 22:27:04 +08:00
prop = nil,
2024-11-17 21:00:11 +08:00
popup_dim = ffi.new("struct nk_rect",{20, 100, 220, 90}),
folder_path = ".",
drives = {},
2024-11-17 21:00:11 +08:00
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 Folder", flags) == true) then
nk.nk_layout_row_begin(ctx, nk.NK_DYNAMIC, 25, 2)
nk.nk_layout_row_push(ctx, 0.2)
nk.nk_label(ctx, "Drives: ", 1)
for k,v in ipairs(udata.drives) do
nk.nk_layout_row_push(ctx, 0.1)
if(nk.nk_button_label(ctx, v) == true) then
udata.folder_path = v
end
end
nk.nk_layout_row_end(ctx)
nk.nk_layout_row_dynamic(ctx, dim.h-110, 1)
2024-11-17 21:00:11 +08:00
local files = dirtools.get_folderslist(udata.folder_path)
local bhit = wdgts.widget_list_buttons(ctx, "folder_files", nil, files, dim.w -40 )
if(bhit) then
udata.hit = bhit
if(bhit[2] == "..") then
udata.folder_path = dirtools.get_parent(udata.folder_path)
2024-11-17 21:00:11 +08:00
else
udata.folder_path = dirtools.change_folder(udata.folder_path, udata.hit[2])
2024-11-17 21:00:11 +08:00
end
end
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "Cancel") == true) then
udata.popup_active = 0
2024-11-20 07:21:56 +08:00
if(udata.callback) then
udata.callback(udata, false)
udata.callback = nil
end
2024-11-17 21:00:11 +08:00
end
if (nk.nk_button_label(ctx, "OK") == true) then
udata.popup_active = 0
2024-11-18 22:27:04 +08:00
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
2024-11-20 07:21:56 +08:00
if(udata.callback) then
udata.callback(udata, true)
udata.callback = nil
end
2024-11-17 21:00:11 +08:00
end
nk.nk_group_end(ctx)
end
if(udata.popup_active == 0) then nk.nk_popup_close(ctx) end
return udata.popup_active
end,
}
-- --------------------------------------------------------------------------------------
-- Extract config and build ffi objects for them
-- Each config property shall have a shadow ffi property labeled with _ffi
local function setup_config()
for sectionname, section in pairs(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)
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
end
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
local pix = {}
2024-11-17 21:00:11 +08:00
local function init()
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")
folder_select.popup_dim.w = sapp.sapp_width()* 0.4
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
setup_config()
end
2024-11-17 21:00:11 +08:00
-- --------------------------------------------------------------------------------------
local function display_section(ctx, sectionname)
local section = config[sectionname]
-- Collect the number of properties in the section
local count = 0
local sorted = {}
for k,v in pairs(section) do
count = count + 1
v.key = k
sorted[v.index] = v
end
nk.nk_layout_row_dynamic(ctx, 28 * count + 60, 1)
local bounds = nk.nk_window_get_content_region(ctx)
local prop_col = bounds.w * 0.23
local value_col = bounds.w * 0.73
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, sectionname, flags) == true) then
for k,v in ipairs(sorted) do
nk.nk_layout_row_begin(ctx, nk.NK_STATIC, 28, 3)
nk.nk_layout_row_push(ctx, prop_col)
nk.nk_label(ctx, v.key..":", nk.NK_TEXT_LEFT)
if(v.ptype == "string" or v.ptype == nil) then
nk.nk_layout_row_push(ctx, value_col)
nk.nk_edit_string(ctx, nk.NK_EDIT_SIMPLE, v.ffi, v.len_ffi, v.slen, nk.nk_filter_default)
elseif(v.ptype == "path") then
nk.nk_layout_row_push(ctx, value_col - 34)
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)
if(nk.nk_button_label(ctx, "") == true) then
folder_select.popup_active = 1
2024-11-18 22:27:04 +08:00
folder_select.prop = v
2024-11-17 22:08:48 +08:00
folder_select.folder_path = v.value
folder_select.drives = dirtools.get_drives()
2024-11-17 21:00:11 +08:00
end
nk.nk_style_set_font(ctx, myfonts[3].handle)
elseif(v.ptype == "file") then
nk.nk_layout_row_push(ctx, value_col - 34)
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)
2024-11-18 22:27:04 +08:00
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()
2024-11-18 22:27:04 +08:00
end
2024-11-17 21:00:11 +08:00
nk.nk_style_set_font(ctx, myfonts[3].handle)
elseif(v.ptype == "combo") then
nk.nk_layout_row_push(ctx, value_col)
v.value = wdgts.widget_combo_box(ctx, v.plist, v.value, 200)
elseif(v.ptype == "int") then
nk.nk_layout_row_push(ctx, value_col)
nk.nk_property_int(ctx, "", v.vmin, v.ffi, v.vmax, v.vstep, v.vinc)
elseif(v.ptype == "float") then
nk.nk_layout_row_push(ctx, value_col)
nk.nk_property_float(ctx, "", v.vmin, v.ffi, v.vmax, v.vstep, v.vinc)
end
nk.nk_layout_row_end(ctx)
end
nk.nk_group_end(ctx)
end
end
-- --------------------------------------------------------------------------------------
local project_curr_tab = 1
local project_tabs = {
{
name = "Project",
func = function(ctx)
display_section(ctx, "project")
display_section(ctx, "sokol")
end,
},
{
name = "Build",
func = function(ctx)
display_section(ctx, "platform")
end,
},
{
name = "Logs",
2024-11-17 21:00:11 +08:00
}
}
-- --------------------------------------------------------------------------------------
local function project_panel(ctx)
nk.nk_style_set_font(ctx, myfonts[3].handle)
project_curr_tab = wdgts.widget_notebook(ctx, "assets", project_tabs, project_curr_tab, 500, 120)
-- display_section(ctx, "graphics")
-- display_section(ctx, "audio")
nk.nk_layout_row_dynamic(ctx, 25, 2)
local add_folder = false
if (nk.nk_button_label(ctx, "Build Release") == true) then
add_folder = true
end
local add_file = false
if (nk.nk_button_label(ctx, "Clean All") == true) then
add_file = true
end
2024-11-17 21:00:11 +08:00
-- Awesome little radial popup.
local res = wdgts.make_pie_popup(ctx, icons, 100, 6)
end
-- --------------------------------------------------------------------------------------
local function assets_panel(ctx)
nk.nk_style_set_font(ctx, myfonts[3].handle)
2024-11-20 07:21:56 +08:00
curr_tab, named_tab = wdgts.widget_notebook(ctx, "assets", tabs, curr_tab, 500, 120)
2024-11-17 21:00:11 +08:00
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "Add Folder") == true) then
folder_select.popup_active = 1
folder_select.prop = nil
folder_select.folder_path = "."
folder_select.drives = dirtools.get_drives()
2024-11-20 07:21:56 +08:00
folder_select.callback = function(udata, res)
print(udata.folder_path, named_tab, res)
if(res == true) then
print(named_tab)
table.insert(config.assets[named_tab], udata.folder_path)
end
end
end
if (nk.nk_button_label(ctx, "Add File") == true) then
file_select.popup_active = 1
file_select.prop = nil
file_select.folder_path = dirtools.get_folder(".")
file_select.drives = dirtools.get_drives()
2024-11-20 07:21:56 +08:00
file_select.callback = function(udata, res)
if(res == true) then
print(named_tab)
table.insert(config.assets[named_tab], udata.file_selected)
end
end
end
2024-11-17 21:00:11 +08:00
end
-- --------------------------------------------------------------------------------------
local function main_ui(ctx)
if(myfonts == nil) then
myfonts = fonts.setup_font(ctx, font_list)
end
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)
2024-11-18 22:27:04 +08:00
file_select.popup_active = wdgts.widget_popup_panel(ctx, "popup", file_select.popup_dim, file_select.ui_func, file_select, file_select.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)
if (nk.nk_menu_begin_label(ctx, "Project", nk.NK_TEXT_LEFT, nk.nk_vec2(120, 200))) then
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "New", nk.NK_TEXT_LEFT)) then
init()
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Open", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Save", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "SaveAs", nk.NK_TEXT_LEFT)) then
end
nk.nk_layout_row_dynamic(ctx, 35, 1)
if (nk.nk_menu_item_label(ctx, "Quit", nk.NK_TEXT_LEFT)) then
os.exit()
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)
-- wdgts.widget_panel_fixed(ctx, "Project", 10, 10, width, height, flags, function(data)
if (nk.nk_group_begin(ctx, "Project", flags) == true) then
local padding = ctx[0].style.window.padding
ctx[0].style.window.padding = nk.nk_vec2(10,10)
project_panel(data.ctx)
-- end, {ctx=ctx})
nk.nk_group_end(ctx)
end
nk.nk_style_set_font(ctx, myfonts[4].handle)
-- wdgts.widget_panel_fixed(ctx, "Assets", 10+width+10, 10, width, height, flags, function(data)
if (nk.nk_group_begin(ctx, "Assets", flags) == true) then
assets_panel(data.ctx)
-- end, {ctx=ctx})
nk.nk_group_end(ctx)
end
end, {ctx=ctx})
return not nk.nk_window_is_closed(ctx, "Overview")
end
-- --------------------------------------------------------------------------------------
return {
config = config,
init = init,
main_ui = main_ui,
}
-- --------------------------------------------------------------------------------------