- [NEW] 'app' module

- [NEW] 'editor' module
- [NEW] 'msgbox' module
- [NEW] 'score' module
- [NEW] 'ui' module
- [NEW] 'util' module
This commit is contained in:
2025-06-20 13:50:07 +02:00
parent bbbf9d0c50
commit ddebcfa47a
11 changed files with 400 additions and 48 deletions

16
data/app.lua Normal file
View File

@@ -0,0 +1,16 @@
app = {
update = nil,
stack = {},
push = function(func)
table.insert(app.stack, mini.update)
mini.update = func
end,
pop = function()
if #app.stack > 0 then
mini.update = table.remove(app.stack)
end
end,
}

175
data/editor.lua Normal file
View File

@@ -0,0 +1,175 @@
require "menu"
require "msgbox"
editor = {
layer = LAYER_FOREGROUND,
brush={w=2,h=1,tiles={16,17}},
selection=nil,
ants=0xc936,
modified = false,
enable = function()
menu.show()
app.update = editor.update
sys.beat(2)
end,
update = function()
view.origin(0,0)
surf.target(0)
-- Pintar el menú (el marcador serà en el modul game.lua)
--score.draw()
menu.draw()
-- Pintar el mapa i sprites
rooms.draw()
--view.origin(0,0)
local mx, my = mouse.pos()
local tx, ty = (mx>>3), (my>>3)
mx, my = tx<<3, ty<<3
if my>=0 then
if editor.selection then
local rx1,ry1,rx2,ry2=editor.selection.x1<<3,editor.selection.y1<<3,editor.selection.x2<<3,editor.selection.y2<<3
if rx1>rx2 then rx1,rx2=rx2,rx1 end
if ry1>ry2 then ry1,ry2=ry2,ry1 end
draw.pattern(editor.ants)
draw.rect(rx1-1, ry1-1, rx2-rx1+10, ry2-ry1+10, 28)
draw.pattern(0xffff)
if sys.beat() then
editor.ants = (editor.ants<<12) | (editor.ants>>4)
end
else
draw.rect(mx-1, my-1, editor.brush.w*8+2, editor.brush.h*8+2, 28)
draw.rect(mx, my, editor.brush.w*8, editor.brush.h*8, 1)
end
if mouse.down(mouse.LEFT) then
editor.stamp(tx,ty)
editor.modified = true
--map.tile(tx,ty,editor.brush.tiles[1])
end
if mouse.down(mouse.RIGHT) then
if editor.selection then
editor.selection.x2=tx
editor.selection.y2=ty
else
editor.selection={}
editor.selection.x1=tx
editor.selection.y1=ty
editor.selection.x2=tx
editor.selection.y2=ty
end
else
if editor.selection then
editor.create_stamp()
editor.selection=nil
end
end
end
view.origin(0,0)
draw.text(rooms.pos.x//20,1,96,28)
draw.text(rooms.pos.y//12,5,96,28)
if key.press(key.ESCAPE) then
editor.quit()
elseif key.press(key.RIGHT) and rooms.pos.x < 20*7 then
rooms.pos.x = rooms.pos.x + 20
elseif key.press(key.LEFT) and rooms.pos.x > 0 then
rooms.pos.x = rooms.pos.x - 20
elseif key.press(key.DOWN) and rooms.pos.y < 12*7 then
rooms.pos.y = rooms.pos.y + 12
elseif key.press(key.UP) and rooms.pos.y > 0 then
rooms.pos.y = rooms.pos.y - 12
elseif key.press(key.TAB) or key.press(key.ESCAPE) then
editor.picker.show()
elseif key.press(key.Q) then
msgbox.show("IE MEN!", {"Hi ha canvis sense guardar.", "Vols guardar-los abans d'eixir?"}, { {"Cancel", app.pop}, {"No", function() sys.quit() end}, {"Yes", sys.quit} } )
end
end,
create_stamp=function()
local tx1,ty1,tx2,ty2=editor.selection.x1,editor.selection.y1,editor.selection.x2,editor.selection.y2
if tx1>tx2 then tx1,tx2=tx2,tx1 end
if ty1>ty2 then ty1,ty2=ty2,ty1 end
editor.brush.w=tx2-tx1+1
editor.brush.h=ty2-ty1+1
local w,h=editor.brush.w,editor.brush.h
local p=1
for y=1,h do
for x=1,w do
editor.brush.tiles[p]=map.tile(tx1+x-1,ty1+y-1)
--map.tile(tx+x-1,ty+y-1,editor.brush.tiles[p])
p=p+1
end
end
end,
stamp=function(tx,ty)
local w,h=editor.brush.w,editor.brush.h
local p=1
for y=1,h do
for x=1,w do
local fx, fy = tx+x-1, ty+y-1
if fx < rooms.pos.x+20 and fy < rooms.pos.y+12 then
map.tile(fx,fy,editor.brush.tiles[p])
end
p=p+1
end
end
end,
quit=function()
if editor.modified then
msgbox.show("IE MEN!",
{"Hi ha canvis sense guardar.", "Vols guardar-los abans d'eixir?"},
{
{"Cancel", app.pop},
{"No", sys.quit},
{"Yes", function() rooms.save() sys.quit() end}
} )
else
sys.quit()
end
end,
picker = {
show = function()
app.push(editor.picker.update_tiles)
end,
update_tiles = function()
view.origin(0,0)
view.clip()
surf.source(tiles)
surf.cls(1)
draw.surf(0,0,128,128,0,0)
local mx, my = mouse.pos()
local tx, ty = mx>>3, my>>3
mx, my = tx<<3, ty<<3
draw.rect(mx-1, my-1, 10, 10, 28)
draw.rect(mx, my, 8, 8, 1)
if mouse.press(mouse.LEFT) then
if tx<16 and ty<16 then
editor.brush.w=1
editor.brush.h=1
editor.brush.tiles={}
editor.brush.tiles[1]=ty*16+tx
end
app.pop()
end
if key.press(key.TAB) or key.press(key.ESCAPE) then
app.pop()
end
end
}
}

View File

@@ -1,39 +1,19 @@
require "app"
require "score"
require "rooms"
require "menu"
x=0
require "editor"
function mini.init()
sprites = surf.load("sprites.gif")
tiles = surf.load("tiles.gif")
surf.source(tiles)
pal.set(pal.load("tiles.gif"))
pal.trans(0)
rooms.init()
menu.show()
editor.enable()
end
function mini.update()
view.origin(0,0)
surf.cls(1)
surf.target(0)
menu.draw()
-- Pintar el marcador
-- [TODO]
-- Pintar el mapa i sprites
rooms.draw()
view.origin(0,0)
local mx, my = mouse.pos()
mx, my = math.floor(mx/8)*8, math.floor(my/8)*8
if my>=8 then
draw.rect(mx-1, my-1, 10, 10, 28)
draw.rect(mx, my, 8, 8, 1)
end
if key.press(key.ESCAPE) then
sys.quit()
end
app.update()
end

View File

@@ -21,22 +21,39 @@ menu = {
draw.rectf(0,0,160,7,23)
draw.hline(0,7,160,16)
menu.current_x = 1
menu.option("FILE")
popup.create("FILE", 1, 8)
popup.addOption("FILE", "New", function() sys.quit() end)
popup.addOption("FILE", "Load", function() sys.quit() end)
popup.addOption("FILE", "Save", function() sys.quit() end)
popup.addOption("FILE", "Save As...", function() sys.quit() end)
popup.addOption("FILE", "Save", function() rooms.save() editor.modified=false end)
popup.addOption("FILE", "Quit", editor.quit)
menu.option("VIEW")
popup.create("VIEW", 1, 8)
popup.addOption("VIEW", "Background", function() rooms.toggle_visibility(LAYER_BACKGROUND) end)
popup.addOption("VIEW", "Shadows", function() rooms.toggle_visibility(LAYER_SHADOWS) end)
popup.addOption("VIEW", "Foreground", function() rooms.toggle_visibility(LAYER_FOREGROUND) end)
popup.addOption("VIEW", "Sprites", function() rooms.toggle_visibility(LAYER_SPRITES) end)
menu.option("EDIT")
popup.create("EDIT", 1, 8)
popup.addOption("EDIT", "Background", function() editor.layer=LAYER_BACKGROUND end)
popup.addOption("EDIT", "Foreground", function() editor.layer=LAYER_FOREGROUND end)
--popup.addOption("EDIT", "Sprites", function() rooms.toggle_visibility(LAYER_SPRITES) end)
menu.option("TOOLS")
if editor.modified then
draw.text("*",160-5,1,8)
end
end,
option = function(label)
local next_x = menu.current_x + (#label + 2)*4
local mx, my = mouse.pos()
if my < 8 and mx >= menu.current_x and mx < next_x then
draw.rectf(menu.current_x, 0, next_x-menu.current_x, 8, 21)
draw.rectf(menu.current_x, 0, next_x-menu.current_x, 7, 21)
if mouse.down(mouse.LEFT) then
mouse.discard()
popup.show(label)
end
end

73
data/msgbox.lua Normal file
View File

@@ -0,0 +1,73 @@
require "ui"
require "util"
msgbox = {
title = "TITOL",
text = { "Este es el missatge.", "Pot estar en varies linies" },
buttons = { {"YES", sys.quit}, {"NO", app.pop} },
selected = 0,
w = 100,
h = 50,
show = function(title, text, buttons, default)
msgbox.selected = default or 1
msgbox.title = title
msgbox.text = text
msgbox.buttons = buttons
msgbox.w = 0
for i,v in ipairs(msgbox.text) do
local width = #v*4+8
if width > msgbox.w then msgbox.w = width end
end
msgbox.h = #msgbox.text*6+35
app.push(msgbox.update)
end,
update = function()
local top = (160-msgbox.w)//2
local left = (104-msgbox.h)//2
view.clip(top, left, msgbox.w, msgbox.h)
view.origin(top, left)
draw.outset(0, 0, msgbox.w, msgbox.h)
draw.rectf(1,1,msgbox.w-2, 7, 21)
draw.text(msgbox.title, 2, 2, 28)
local y = 12
for i,v in ipairs(msgbox.text) do
draw.text(v, 5, y, 1)
y = y + 6
end
local mx, my = mouse.pos()
local x = msgbox.w - 35
y = msgbox.h - 12
for i,v in ipairs(msgbox.buttons) do
local inside = util.inside(mx, my, {x, y, 32, 9})
draw.outset(x, y, 32, 9)
if inside then
draw.rectf(x+1,y+1,30,7,27)
if mouse.down(mouse.LEFT) then draw.inset(x, y, 32, 9) end
end
local offset = (32-#v[1]*4)//2
draw.text(v[1], x+1+offset, y+2, 1)
if (i==msgbox.selected) then
draw.rect(x-1, y-1, 34, 11, 1)
end
if mouse.press(mouse.LEFT) then
if inside then
v[2]()
end
end
x = x - 34
end
if key.press(key.ESCAPE) then
app.pop()
end
end
}

View File

@@ -4,7 +4,7 @@ popup={
current = nil,
create = function(label,x,y)
popup.list[label] = {x=x, y=y, width=0, options={}}
popup.list[label] = {x=x, y=y, width=50, options={}}
end,
addOption = function(parent, label, action)
@@ -16,23 +16,36 @@ popup={
end,
show = function(label)
popup.old_update = mini.update
popup.current = label
mini.update = popup.update
app.push(popup.update)
end,
update = function()
view.origin(0,0)
local mx, my = mouse.pos()
local p = popup.list[popup.current]
draw.rectf(p.x, p.y, p.width, #p.options*7+2, 23)
draw.rect(p.x, p.y, p.width, #p.options*7+2, 16)
draw.outset(p.x, p.y, p.width, #p.options*7+2)
local y = p.y+2
for k,v in ipairs(p.options) do
local inside = util.inside(mx, my, {p.x, y-2, p.width, 7})
if inside then
draw.rectf(p.x+1, y-1, p.width-2, 7, 21)
if mouse.press(mouse.LEFT) then
app.pop()
v.action()
end
end
draw.text(v.label, p.x+2, y, 28)
y = y + 7
end
local inside = util.inside(mx, my, {p.x, p.y, p.width, #p.options*7+2})
if not inside and mouse.down(mouse.LEFT) then
mouse.discard()
app.pop()
end
if key.press(key.ESCAPE) then
mini.update = popup.old_update
app.pop()
end
end

View File

@@ -1,10 +1,17 @@
LAYER_FOREGROUND = 1
LAYER_BACKGROUND = 2
LAYER_ITEMS = 4
LAYER_SPRITES = 8
LAYER_SHADOWS = 16
LAYER_ALL = 31
rooms = {
surf_background = nil,
surf_foreground = nil,
surf_items = nil,
surf_original_items = nil,
pos = {x=10*8, y=0},
visibility = LAYER_ALL,
pos = {x=0, y=0},
init = function()
rooms.pos.x, rooms.pos.y = 0,0
@@ -42,51 +49,85 @@ rooms = {
--shader.enable();
end,
save = function()
surf.save(rooms.surf_background, "data/rooms_background.bin")
surf.save(rooms.surf_foreground, "data/rooms_foreground.bin")
surf.save(rooms.surf_items, "data/rooms_items.bin")
editor.modified = false
end,
draw = function()
-- Retallem la pantalla a la zona de joc
view.clip(0,8,160,96)
-- Movem la càmara a l'habitació on estem
view.origin(rooms.pos.x*8,rooms.pos.y*8+8)
view.origin(-rooms.pos.x*8,-rooms.pos.y*8+8)
-- Pintem el background
surf.source(tiles)
map.surf(rooms.surf_background)
map.draw()
if rooms.is_visible(LAYER_BACKGROUND) then
map.draw()
else
draw.rectf(0,0,160,96,1)
end
-- Movem 4x4 pixels la càmara per a pintar les sombres dels sprites i el foreground
view.origin(rooms.pos.x*8+4,rooms.pos.y*8+12)
view.origin(-rooms.pos.x*8+4,-rooms.pos.y*8+12)
-- Pintem el foreground de negre
map.surf(rooms.surf_foreground)
pal.subpal(0,32,1)
map.draw()
if rooms.is_visible(LAYER_FOREGROUND | LAYER_SHADOWS) then map.draw() end
-- Pintem els sprites de negre
-- [TODO]
surf.source(sprites)
draw.surf(0, 0, 16, 17, 10, 10, 16, 17)
if rooms.is_visible(LAYER_SPRITES | LAYER_SHADOWS) then
draw.surf(0, 0, 16, 17, 10, 10, 16, 17)
end
-- Movem la càmara al lloc que toca de nou, i tornem la paleta normal
view.origin(rooms.pos.x*8,rooms.pos.y*8+8)
view.origin(-rooms.pos.x*8,-rooms.pos.y*8+8)
pal.subpal()
-- Pintem el foreground
surf.source(tiles)
map.surf(rooms.surf_foreground)
map.draw()
if rooms.is_visible(LAYER_FOREGROUND) then map.draw() end
-- Pintem els sprites
-- [TODO]
surf.source(sprites)
draw.surf(0, 0, 16, 17, 10, 10, 16, 17)
if rooms.is_visible(LAYER_SPRITES) then
draw.surf(0, 0, 16, 17, 10, 10, 16, 17)
end
-- Pintem la rejilla
--for y=0,12 do draw.line(0,y*8, 160, y*8, 27) end
--for x=0,20 do draw.line(x*8, 0, x*8, 104, 27) end
end,
is_visible = function(layer)
return rooms.visibility & layer == layer
end,
set_visibility = function(layer, visibility)
if visibility then
rooms.visibility = rooms.visibility | layer
else
rooms.visibility = rooms.visibility & ~layer
end
end,
toggle_visibility = function(layer)
if rooms.visibility & layer == layer then
rooms.visibility = rooms.visibility & ~layer
else
rooms.visibility = rooms.visibility | layer
end
end,
peiv = function()
pal.color(1, 1, 1, 1)
return "HOLA OTHER UNIT"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 229 B

After

Width:  |  Height:  |  Size: 263 B

6
data/score.lua Normal file
View File

@@ -0,0 +1,6 @@
score = {
draw = function()
draw.rectf(0,0,160,7,1)
end
}

18
data/ui.lua Normal file
View File

@@ -0,0 +1,18 @@
function draw.outset(x, y, w, h)
draw.rectf(x, y, w, h, 23)
w,h = w+x,h+y
draw.hline(x, h-1, w-1,16)
draw.vline(w-1, y, h-1,16)
draw.hline(x, y, w-1,28)
draw.vline(x, y, h-1,28)
end
function draw.inset(x, y, w, h)
draw.rectf(x, y, w, h, 23)
w,h = w+x,h+y
draw.hline(x, y, w-1,16)
draw.vline(x, y, h-1,16)
draw.hline(x, h-1, w-1,28)
draw.vline(w-1, y, h-1,28)
end

13
data/util.lua Normal file
View File

@@ -0,0 +1,13 @@
util={
inside = function(x, y, rectangle)
if x >= rectangle[1] and
y >= rectangle[2] and
x < rectangle[3]+rectangle[1] and
y < rectangle[4]+rectangle[2] then
return true
else
return false
end
end
}