diff --git a/lua.cpp b/lua.cpp index 5ad4b9c..7c5ad9b 100644 --- a/lua.cpp +++ b/lua.cpp @@ -4,6 +4,9 @@ #include "jfile.h" #include "log.h" #include +#include +#include + namespace fs = std::filesystem; extern "C" { @@ -723,23 +726,38 @@ extern "C" { std::string path = "./data"; if (lua_gettop(L) > 0) path = luaL_checkstring(L, 1); - lua_newtable(L); - int i=1; - + // Collect entries + std::vector entries; for (const auto& entry : fs::directory_iterator(path)) { - lua_newtable(L); // create subtable for this entry + entries.push_back(entry); + } - // name field - lua_pushstring(L, entry.path().u8string().c_str()); + // Sort: directories first, then files; both alphabetically + std::sort(entries.begin(), entries.end(), + [](const fs::directory_entry& a, const fs::directory_entry& b) { + bool adir = a.is_directory(); + bool bdir = b.is_directory(); + if (adir != bdir) return adir > bdir; // directories before files + return a.path().filename().string() < b.path().filename().string(); + }); + + // Build Lua table + lua_newtable(L); + int i = 1; + for (const auto& entry : entries) { + lua_newtable(L); + + // name field (canonical absolute path) + lua_pushstring(L, fs::canonical(entry.path()).u8string().c_str()); lua_setfield(L, -2, "name"); - // dir field (true if directory, false otherwise) + // dir field lua_pushboolean(L, entry.is_directory()); lua_setfield(L, -2, "dir"); - // insert subtable into main table at index i lua_rawseti(L, -2, i++); } + return 1; } @@ -864,6 +882,11 @@ extern "C" { return 1; } + static int cpp_mouse_dblclick(lua_State *L) { + lua_pushboolean(L, doubleclick()); + return 1; + } + static int cpp_mouse_discard(lua_State *L) { mdiscard(); return 0; @@ -1047,6 +1070,7 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_mouse_wheel); lua_setfield(L, -2, "wheel"); lua_pushcfunction(L,cpp_mouse_down); lua_setfield(L, -2, "down"); lua_pushcfunction(L,cpp_mouse_press); lua_setfield(L, -2, "press"); + lua_pushcfunction(L,cpp_mouse_dblclick); lua_setfield(L, -2, "dblclick"); lua_pushcfunction(L,cpp_mouse_discard); lua_setfield(L, -2, "discard"); lua_pushinteger(L, 1); lua_setfield(L, -2, "LEFT"); diff --git a/mini.cpp b/mini.cpp index e6d0a5e..084a822 100644 --- a/mini.cpp +++ b/mini.cpp @@ -112,6 +112,7 @@ Uint8 key_just_pressed = 0; int mouse_x, mouse_y, mouse_wheel; Uint32 mouse_buttons; uint8_t mouse_just_pressed = 0; +bool double_click = false; bool mouse_discard = false; SDL_Gamepad *gamepad = NULL; @@ -479,8 +480,9 @@ int main(int argc,char*argv[]){ key_just_pressed = 0; pad_just_pressed = SDL_GAMEPAD_BUTTON_INVALID; mouse_just_pressed = 0; + mouse_wheel = 0; + double_click = false; while(!should_exit) { - mouse_wheel = 0; if (update_mode==UPDATE_WAIT) SDL_WaitEvent(NULL); else if (update_mode==UPDATE_TIMEOUT) SDL_WaitEventTimeout(NULL, timeout); while(SDL_PollEvent(&mini_eve)) { @@ -522,7 +524,11 @@ int main(int argc,char*argv[]){ if (mouse_discard) mouse_discard = false; else - mouse_just_pressed = mini_eve.button.button; + if (mini_eve.button.clicks==2 && mini_eve.button.button==SDL_BUTTON_LEFT) { + double_click = true; + } else { + mouse_just_pressed = mini_eve.button.button; + } } if (mini_eve.type == SDL_EVENT_MOUSE_WHEEL) { mouse_wheel = mini_eve.wheel.y; @@ -558,6 +564,8 @@ int main(int argc,char*argv[]){ if (beats>0)beats--; key_just_pressed = 0; mouse_just_pressed = 0; + double_click = false; + mouse_wheel = 0; pad_just_pressed = SDL_GAMEPAD_BUTTON_INVALID; } SDL_SetRenderTarget(mini_ren, mini_shadertex); @@ -1313,6 +1321,10 @@ bool mbtnp(uint8_t i) { return mouse_just_pressed == i; } +bool doubleclick() { + return double_click; +} + void mdiscard() { mouse_discard = true; } diff --git a/mini.h b/mini.h index 16f388d..e9a3028 100644 --- a/mini.h +++ b/mini.h @@ -225,6 +225,7 @@ int mousey(); int mwheel(); bool mbtn(uint8_t i); bool mbtnp(uint8_t i); +bool doubleclick(); void mdiscard(); float time(); diff --git a/tools/fonted/data/game.ini b/tools/fonted/data/game.ini new file mode 100644 index 0000000..f2c739a --- /dev/null +++ b/tools/fonted/data/game.ini @@ -0,0 +1,5 @@ +title=NEW MINI PROJECT +config=newminiproject +width=320 +height=240 +zoom=2 diff --git a/tools/fonted/data/main.lua b/tools/fonted/data/main.lua new file mode 100644 index 0000000..0b3887c --- /dev/null +++ b/tools/fonted/data/main.lua @@ -0,0 +1,138 @@ +function loadDir(path) + files = sys.dir(path) + if #files > 0 then + dir = files[1].name:match("(.+)/[^/]+$") + else + dir = path + end + table.insert(files, 1, { dir=true, name=".." }) + selected = 0 + offset = 0 + lift_height=-1 + lift_top=0 + if #files>23 then + lift_height=23*161/#files + end +end + +function goBack() + dir = dir:match("(.+)/[^/]+$") + loadDir(dir) +end + +function mini.init() + surf.cls(0) + folder = surf.new(5,4) + surf.target(folder) + for i=0,4 do for j=0,3 do surf.pixel(i,j,0) end end + for i=2,4 do surf.pixel(i,0,255) end + surf.target(0) + loadDir(".") +end + +function mini.update() + + local mx,my=mouse.pos() + + view.origin(0,0) + for i=0,15 do + draw.rectf(i*8, 0, 8, 8, i) + end + pal.trans(255) + view.origin(20, 20) + draw.rrectf(0,0,280,200,3,15) + draw.rrectf(4,4,272,9,2,12) + draw.rrectf(4,17,272,163,2,12) + if lift_height>0 then draw.rrectf(271,18+lift_top,4,lift_height,2,14) end + + draw.rrectf(192,185,40,11,2,9) + draw.rrectf(192,184,40,11,2,10) + draw.text("OK",209,187,9) + + draw.rrectf(236,185,40,11,2,1) + draw.rrectf(236,184,40,11,2,2) + draw.text("CANCEL",245,187,1) + + local y = 19 + draw.text(dir, 6, 6, 0) + + surf.source(folder) + +-- if mx>4 and mx<268 and my>=y-1 and my4 and mx<268 and my>=y-1 and my=23 then break end + end + + if key.press(key.RETURN) and selected~=-1 then + if files[selected].dir then + local filename = files[selected].name:match("([^/]+)$") + loadDir(dir.."/"..filename) + else + -- do nothing for now + end + elseif key.press(key.DOWN) and selected<#files then + selected=selected+1 + elseif key.press(key.UP) then + if selected>1 then + selected=selected-1 + else + selected = 1 + end + end + + local scroll = -mouse.wheel() + if scroll~=0 then + offset=offset+scroll + if offset<0 then offset=0 end + if offset+23 > #files then offset = #files-23 end + lift_top=offset*161/#files + end + +end diff --git a/tools/fonted/data/subatomic.gif b/tools/fonted/data/subatomic.gif new file mode 100644 index 0000000..b060009 Binary files /dev/null and b/tools/fonted/data/subatomic.gif differ diff --git a/vscode/library.lua b/vscode/library.lua index a53710e..d512192 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -518,6 +518,10 @@ function mouse.down(btn) end ---Returns whether the specified mouse button has just been pressed function mouse.press(btn) end +---@return boolean +---Returns whether the user performed a double click +function mouse.dblclick() end + ---Ignores current down button, effectively not raising the next "press" event function mouse.discard() end