sokol-luajit/tools/build/rbuilder/rbuilder.lua
2024-11-13 19:41:06 +10:30

472 lines
16 KiB
Lua

package.path = package.path..";../../../?.lua"
local dirtools = require("tools.dirtools")
local base_path = dirtools.get_app_path("sokol%-luajit")
package.cpath = package.cpath..";"..base_path.."bin/win64/?.dll"
package.path = package.path..";"..base_path.."ffi/sokol/?.lua"
package.path = package.path..";"..base_path.."?.lua"
--_G.SOKOL_DLL = "sokol_debug_dll"
local sapp = require("sokol_app")
sg = require("sokol_nuklear")
local nk = sg
local slib = require("sokol_libs") -- Warn - always after gfx!!
local hmm = require("hmm")
local hutils = require("hmm_utils")
local stb = require("stb")
local ffi = require("ffi")
-- --------------------------------------------------------------------------------------
-- For win - sleep so as to not consume all proc cycles for ui
ffi.cdef[[
void Sleep(uint32_t ms);
]]
-- --------------------------------------------------------------------------------------
local config = require("config.settings")
local wdgts = require("utils.widgets")
local fonts = require("utils.fonts")
local icons = ffi.new("struct nk_image [?]", 10)
-- --------------------------------------------------------------------------------------
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 folder_select = {
popup_active = 0,
popup_dim = ffi.new("struct nk_rect",{20, 100, 220, 90}),
folder_path = ".",
ui_func = function(ctx, dim, udata)
nk.nk_layout_row_dynamic(ctx, 25, 1)
nk.nk_label(ctx, "A terrible error as occured", nk.NK_TEXT_LEFT)
nk.nk_layout_row_dynamic(ctx, 25, 2)
if (nk.nk_button_label(ctx, "OK") == true) then
udata.popup_active = 0
nk.nk_popup_close(ctx)
end
if (nk.nk_button_label(ctx, "WTF") == true) then
udata.popup_active = 0
nk.nk_popup_close(ctx)
end
return udata.popup_active
end,
}
-- --------------------------------------------------------------------------------------
local visited = {}
local function tdump(o, level)
local level = level or 1
if(level == 0) then visited = {} end
if type(o) == 'table' then
local s = ' {\n'
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..tostring(k)..'"' end
if(visited[v] == nil) then
visited[v] = true
s = s .. string.rep(" ", level)..'['..k..'] = ' .. tdump(v, level+1)
end
end
return s .. string.rep(" ", level-1)..'}\n'
else
return tostring(o).."\n"
end
end
-- --------------------------------------------------------------------------------------
-- 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 pix = {}
local function init(void)
-- // setup sokol-gfx, sokol-time and sokol-imgui
local sg_desc = ffi.new("sg_desc[1]")
sg_desc[0].environment = slib.sglue_environment()
sg_desc[0].logger.func = slib.slog_func
sg.sg_setup( sg_desc )
-- // use sokol-nuklear with all default-options (we're not doing
-- // multi-sampled rendering or using non-default pixel formats)
local snk = ffi.new("snk_desc_t[1]")
snk[0].dpi_scale = sapp.sapp_dpi_scale()
snk[0].logger.func = slib.slog_func
nk.snk_setup(snk)
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")
sapp.sapp_show_mouse(false)
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
end
-- --------------------------------------------------------------------------------------
local function cleanup()
sg.sg_shutdown()
end
-- --------------------------------------------------------------------------------------
local current_ctx = nil
local function input(event)
if(event.type == sapp.SAPP_EVENTTYPE_RESIZED) then
nk.snk_handle_event(event)
folder_select.popup_dim.x = sapp.sapp_width()/2 - folder_select.popup_dim.w/2
folder_select.popup_dim.y = sapp.sapp_height()/2 - folder_select.popup_dim.h
elseif(event.type == sapp.SAPP_EVENTTYPE_MOUSE_ENTER) then
nk.nk_style_show_cursor(current_ctx)
sapp.sapp_show_mouse(false)
elseif(event.type == sapp.SAPP_EVENTTYPE_MOUSE_LEAVE) then
nk.nk_style_hide_cursor(current_ctx)
sapp.sapp_show_mouse(true)
else
nk.snk_handle_event(event)
end
end
-- --------------------------------------------------------------------------------------
-- Extract config and build ffi objects for them
-- Each config property shall have a shadow ffi property labeled with _ffi
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 == nil or 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
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))
prop.len_ffi = ffi.new("int[1]", {string.len(prop.value)})
end
end
end
local group_width = ffi.new("int[1]", {320})
local group_height = ffi.new("int[1]", {200})
-- --------------------------------------------------------------------------------------
local range_float_value = ffi.new("float[1]")
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
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)
nk.nk_button_label(ctx, "")
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"
}
}
-- --------------------------------------------------------------------------------------
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")
-- Awesome little radial popup.
local res = wdgts.make_pie_popup(ctx, icons, 100, 6)
end
-- --------------------------------------------------------------------------------------
local curr_tab = 1
local files = {}
-- Fill with temp file list of dir /b
local fh = io.popen("dir /b", "r")
local d = fh:read("*a")
fh:close()
for f in string.gmatch(d, "(.-)\n") do
local newfile = { name = ffi.string(f)}
newfile.select = ffi.new("nk_bool[1]")
newfile.select[0] = 0
table.insert(files, newfile)
end
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)
wdgts.widget_list_selectable(ctx, "source_files", nk.NK_WINDOW_BORDER, files, bounds.w -40 )
end,
},
{
name = "Images"
},
{
name = "Data"
}
}
-- --------------------------------------------------------------------------------------
local function assets_panel(ctx)
nk.nk_style_set_font(ctx, myfonts[3].handle)
curr_tab = wdgts.widget_notebook(ctx, "assets", tabs, curr_tab, 500, 120)
nk.nk_layout_row_begin(ctx, nk.NK_STATIC, 22, 3)
nk.nk_layout_row_push(ctx, 50)
nk.nk_label(ctx, "size:", nk.NK_TEXT_LEFT)
nk.nk_layout_row_push(ctx, 130)
nk.nk_property_int(ctx, "#Width:", 100, group_width, 500, 10, 1)
nk.nk_layout_row_push(ctx, 130)
nk.nk_property_int(ctx, "#Height:", 100, group_height, 500, 10, 1)
nk.nk_layout_row_end(ctx)
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)
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
local padding = ctx[0].style.window.padding
ctx[0].style.window.padding = nk.nk_vec2(10,10)
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
-- --------------------------------------------------------------------------------------
local function frame(void)
local ctx = nk.snk_new_frame()
current_ctx = ctx
main_ui(ctx)
-- // the sokol_gfx draw pass
local pass = ffi.new("sg_pass[1]")
pass[0].action.colors[0].load_action = sg.SG_LOADACTION_CLEAR
pass[0].action.colors[0].clear_value = { 0.25, 0.5, 0.7, 1.0 }
pass[0].swapchain = slib.sglue_swapchain()
sg.sg_begin_pass(pass)
nk.snk_render(sapp.sapp_width(), sapp.sapp_height())
sg.sg_end_pass()
sg.sg_commit()
ffi.C.Sleep(3)
end
-- --------------------------------------------------------------------------------------
local app_desc = ffi.new("sapp_desc[1]")
app_desc[0].init_cb = init
app_desc[0].frame_cb = frame
app_desc[0].cleanup_cb = cleanup
app_desc[0].event_cb = input
app_desc[0].width = 1280
app_desc[0].height = 800
app_desc[0].window_title = config.rbuilder.title.value
app_desc[0].icon.sokol_default = true
app_desc[0].logger.func = slib.slog_func
app_desc[0].enable_clipboard = true
app_desc[0].ios_keyboard_resizes_canvas = false
sapp.sapp_run( app_desc )
-- --------------------------------------------------------------------------------------
local default_libs = {
["os"] = true,
["_G"] = true,
["jit"] = true,
["table"] = true,
["jit.opt"] = true,
["ffi"] = true,
["string"] = true,
["io"] = true,
["coroutine"] = true,
["package"] = true,
["math"] = true,
["debug"] = true,
["bit"] = true,
}
for k,v in pairs(package.loaded) do
if(type(v) ~= "userdata" and default_libs[tostring(k)] == nil) then
print( "["..tostring(k).."] = "..tostring(v))
end
end
-- --------------------------------------------------------------------------------------