- [NEW] mouse.inside(x,y,w,h)

- [WIP] fonted tool
This commit is contained in:
2025-12-09 14:05:12 +01:00
parent eac20bbbe0
commit 6e15fe7231
6 changed files with 232 additions and 126 deletions

10
lua.cpp
View File

@@ -892,6 +892,15 @@ extern "C" {
return 0;
}
static int cpp_mouse_inside(lua_State *L) {
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
int h = luaL_checkinteger(L, 4);
lua_pushboolean(L, minside(x,y,w,h));
return 1;
}
// key
// ===============================================
@@ -1072,6 +1081,7 @@ void push_lua_funcs() {
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_pushcfunction(L,cpp_mouse_inside); lua_setfield(L, -2, "inside");
lua_pushinteger(L, 1); lua_setfield(L, -2, "LEFT");
lua_pushinteger(L, 2); lua_setfield(L, -2, "MIDDLE");

View File

@@ -1329,6 +1329,12 @@ void mdiscard() {
mouse_discard = true;
}
bool minside(int x, int y, int w, int h) {
const int mx = mouse_x-ds::origin[0];
const int my = mouse_y-ds::origin[1];
return (mx>=x) && (my>=y) && (mx<x+w) && (my<y+h);
}
float time() {
return float(SDL_GetTicks())/1000.0f;
}

1
mini.h
View File

@@ -227,6 +227,7 @@ bool mbtn(uint8_t i);
bool mbtnp(uint8_t i);
bool doubleclick();
void mdiscard();
bool minside(int x, int y, int w, int h);
float time();
bool beat(int16_t i);

View File

@@ -1,138 +1,68 @@
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
require "ui"
function goBack()
dir = dir:match("(.+)/[^/]+$")
loadDir(dir)
end
update = nil
protofont_surface = nil
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(".")
update = any_update
ui.filedialog.show(select_file);
end
function select_file(filename)
if protofont_surface then surf.free(protofont_surface) end
protofont_surface = surf.loadex(filename)
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 my<y+6 then
-- draw.rectf(4,y-1,272,7,13)
-- if mouse.dblclick() then
-- goBack();
-- end
-- end
-- draw.surf(0,0,5,4,7,y+1,5,4)
-- pal.subpal(0,3)
-- draw.surf(0,0,5,4,6,y,5,4)
-- pal.subpal(0)
-- draw.text("..", 13, y, 0) y=y+7
local count = 0
for k,file in ipairs(files) do
local filename = file.name:match("([^/]+)$")
if count < offset then goto continue end
--if v:match("%.gif$") then
if mx>4 and mx<268 and my>=y-1 and my<y+6 then
draw.rectf(4,y-1,272,7,13)
if mouse.dblclick() then
if file.dir then
if k==1 then
goBack()
else
loadDir(dir.."/"..filename)
end
else
--selected = k
end
elseif mouse.press(mouse.LEFT) then
selected = k
end
end
if selected == k then
draw.rectf(4,y-1,272,7,10)
end
if file.dir then
draw.surf(0,0,5,4,7,y+1,5,4)
pal.subpal(0,3)
draw.surf(0,0,5,4,6,y,5,4)
pal.subpal(0)
draw.text(filename, 13, y, 0)
else
draw.text(filename, 6, y, 0)
end
y=y+7
::continue::
count=count+1
if count-offset>=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
update()
end
function any_update()
surf.cls(0)
view.origin(4,4)
draw.rrectf(0,0,136,132,3,15)
if protofont_surface then
local w,h = surf.size(protofont_surface)
local x,y = (136-w)/2,(132-h)/2
surf.source(protofont_surface)
draw.surf(0,0,w,h,x,y,w,h)
draw.text(tostring(w)..","..tostring(h), 10, 100, 3)
end
view.origin(4,140)
draw.rrectf(0,0,136,68,3,15)
view.origin(8,144)
local x,y=0,0
for c=32,127 do
draw.rectf(x,y,7,9,12)
draw.rect(x,y,7,9,0)
draw.text(string.char(c),x+2,y+2,0)
x=x+8
if x>=128 then
x=0
y=y+10
end
end
draw.text("ÁÄÉÍÓÚáéíóúÀÈÌÒÙàèìòùñÑçÇ", x, y+8, 12)
view.origin(144,4)
draw.rrectf(0,0,21,12,3,14)
draw.text("BASE",3,2,12)
draw.rrectf(22,0,45,12,3,15)
draw.text("DIACRITICS",25,2,0)
-- acute(Á), grave(À); diaeresis(Ä), circumflex(Â); cedilla(Ç), tilde(Ñ)
draw.rrectf(68,0,49,12,3,15)
draw.text("PRECOMPOSED",71,2,0)
draw.rrectf(0,8,140,140,3,14)
end

145
tools/fonted/data/ui.lua Normal file
View File

@@ -0,0 +1,145 @@
ui = {
button = function(text, x, y, w, h, color)
local text_x = (w-(#text*4))/2
draw.rrectf(x,y+1,w,h,2,color[1])
if mouse.inside(x,y,w,h) then
if mouse.down(mouse.LEFT) then y=y+1 end
draw.rrectf(x,y,w,h,2,color[3])
if mouse.press(mouse.LEFT) then return true end
else
draw.rrectf(x,y,w,h,2,color[2])
end
draw.text(text,x+text_x+1,y+3,color[1])
return false
end,
filedialog = {
folder = nil,
files = nil,
dir = ".",
selected = 0,
offset = 0,
lift_height = -1,
lift_top = 0,
old_update = nil,
callback = nil,
show = function(callback)
if not ui.filedialog.folder then
ui.filedialog.folder=surf.new(5,4)
surf.target(ui.filedialog.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)
end
ui.filedialog.loadDir(".")
ui.filedialog.callback = callback
ui.filedialog.old_update = update
update = ui.filedialog.update
end,
loadDir = function(path)
ui.filedialog.files = sys.dir(path)
if #ui.filedialog.files > 0 then
ui.filedialog.dir = ui.filedialog.files[1].name:match("(.+)/[^/]+$")
else
ui.filedialog.dir = path
end
table.insert(ui.filedialog.files, 1, { dir=true, name=".." })
ui.filedialog.selected = 0
ui.filedialog.offset = 0
ui.filedialog.lift_height=-1
ui.filedialog.lift_top=0
if #ui.filedialog.files>23 then
ui.filedialog.lift_height=23*161/#ui.filedialog.files
end
end,
goBack = function()
ui.filedialog.dir = ui.filedialog.dir:match("(.+)/[^/]+$")
ui.filedialog.loadDir(ui.filedialog.dir)
end,
select = function()
if ui.filedialog.files[ui.filedialog.selected].dir then
local filename = ui.filedialog.files[ui.filedialog.selected].name:match("([^/]+)$")
ui.filedialog.loadDir(ui.filedialog.dir.."/"..filename)
else
update = ui.filedialog.old_update
ui.filedialog.callback(ui.filedialog.files[ui.filedialog.selected].name)
end
end,
update = function()
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 ui.filedialog.lift_height>0 then draw.rrectf(271,18+ui.filedialog.lift_top,4,ui.filedialog.lift_height,2,14) end
if ui.button("OK",192,184,40,11,{9,10,11}) then ui.filedialog.select() end
if ui.button("CANCEL",236,184,40,11,{1,2,3}) then update = ui.filedialog.old_update end
local y = 19
draw.text(ui.filedialog.dir, 6, 6, 0)
surf.source(ui.filedialog.folder)
local count = 0
for k,file in ipairs(ui.filedialog.files) do
local filename = file.name:match("([^/]+)$")
if count < ui.filedialog.offset then goto continue end
--if not ui.filedialog.filter or filename:match("%."..ui.filedialog.filter.."$") then
if mouse.inside(4,y-1,272,7) then
--if mx>4 and mx<268 and my>=y-1 and my<y+6 then
draw.rectf(4,y-1,272,7,13)
if mouse.dblclick() then
ui.filedialog.select()
elseif mouse.press(mouse.LEFT) then
ui.filedialog.selected = k
end
end
if ui.filedialog.selected == k then
draw.rectf(4,y-1,272,7,10)
end
if file.dir then
draw.surf(0,0,5,4,7,y+1,5,4)
pal.subpal(0,3)
draw.surf(0,0,5,4,6,y,5,4)
pal.subpal(0)
draw.text(filename, 13, y, 0)
else
draw.text(filename, 6, y, 0)
end
y=y+7
::continue::
count=count+1
if count-ui.filedialog.offset>=23 then break end
end
if key.press(key.RETURN) and ui.filedialog.selected~=-1 then
ui.filedialog.select()
elseif key.press(key.DOWN) and ui.filedialog.selected<#ui.filedialog.files then
ui.filedialog.selected=ui.filedialog.selected+1
elseif key.press(key.UP) then
if ui.filedialog.selected>1 then
ui.filedialog.selected=ui.filedialog.selected-1
else
ui.filedialog.selected = 1
end
end
local scroll = -mouse.wheel()
if scroll~=0 then
ui.filedialog.offset=ui.filedialog.offset+scroll
if ui.filedialog.offset<0 then ui.filedialog.offset=0 end
if ui.filedialog.offset+23 > #ui.filedialog.files then ui.filedialog.offset = #ui.filedialog.files-23 end
ui.filedialog.lift_top=ui.filedialog.offset*161/#ui.filedialog.files
end
end
}
}

View File

@@ -17,6 +17,11 @@ function surf.new(w, h) end
---Load GIF file and return surface
function surf.load(filename) end
---@param filename string
---@return number surface
---Load GIF from external file and return surface
function surf.loadex(filename) end
---@param surface number
---@param filename string
---@optional palette table
@@ -525,6 +530,15 @@ function mouse.dblclick() end
---Ignores current down button, effectively not raising the next "press" event
function mouse.discard() end
---@param x number
---@param y number
---@param w number
---@param h number
---@return boolean
---Returns whether the mouse is inside the rectangle specified
function mouse.inside(x,y,w,h) end
mouse.LEFT = 1
mouse.MIDDLE = 2
mouse.RIGHT = 3