diff --git a/data/editor.lua b/data/editor.lua index 640cb21..cf95e59 100644 --- a/data/editor.lua +++ b/data/editor.lua @@ -1,5 +1,6 @@ require "menu" require "msgbox" +require "items" editor = { @@ -8,6 +9,7 @@ editor = { selection=nil, ants=0xc936, modified = false, + item_selected=nil, enable = function() app.update = editor.update @@ -32,40 +34,58 @@ editor = { 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.layer==LAYER_BACKGROUND or editor.layer==LAYER_FOREGROUND then if editor.selection then - editor.selection.x2=tx - editor.selection.y2=ty + 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 - editor.selection={} - editor.selection.x1=tx - editor.selection.y1=ty - editor.selection.x2=tx - editor.selection.y2=ty + 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 else - if editor.selection then - editor.create_stamp() - editor.selection=nil + if mouse.press(mouse.LEFT) then + if editor.item_selected then + surf.target(rooms.surf_items) + surf.pixel(tx,ty,editor.item_selected) + editor.modified = true + surf.target(0) + end + end + if mouse.press(mouse.RIGHT) then + editor.item_selected = nil + end + if editor.item_selected then + local k = items[editor.item_selected] + draw.surf(k.visual.x, k.visual.y, k.visual.w, k.visual.h, mx, my) end end end @@ -85,7 +105,11 @@ editor = { 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() + if editor.layer==LAYER_BACKGROUND or editor.layer==LAYER_FOREGROUND then + editor.tilepicker.show() + else + editor.itempicker.show() + end elseif key.press(key.P) then sprites.hero.pos.x = tx*8-4 sprites.hero.pos.y = ty*8-9 @@ -166,10 +190,10 @@ editor = { end end, - picker = { + tilepicker = { show = function() - app.push(editor.picker.update_tiles) + app.push(editor.tilepicker.update_tiles) end, update_tiles = function() @@ -201,5 +225,39 @@ editor = { end end + }, + + itempicker = { + + show = function() + app.push(editor.itempicker.update_items) + end, + + update_items = function() + view.origin(0,0) + view.clip() + surf.source(surf_sprites) + surf.cls(1) + local mx, my = mouse.pos() + local x,y = 0,0 + for i,k in pairs(items) do + local w = math.max(k.visual.w, #k.name*4) + local ox = (w-k.visual.w)/2 + draw.text(k.name, x, y, 15) + draw.surf(k.visual.x, k.visual.y, k.visual.w, k.visual.h, x+ox, y+7) + if mx>x and mxy and my=v.pos.x and mx<=v.pos.x+v.size.w and my>=v.pos.y and my<=v.pos.y+v.size.h then + sprites.draw_sprite_selected(v) + else + sprites.draw_sprite(v) + end end sprites.draw_sprite(sprites.hero) end, @@ -305,5 +324,24 @@ sprites = { draw.surf(frame.frame.x, frame.frame.y, frame.frame.w, frame.frame.h, sprite.pos.x, sprite.pos.y, frame.frame.w, frame.frame.h, (not reversed) ~= (not sprite.flipped)) local x,y,w,h = util.aabb(sprite) --draw.rect(x,y,w,h,8) + end, + + draw_sprite_selected = function(sprite) + pal.subpal(0,32,28) + + local cycle = animations[sprite.animation].cycle[sprite.current_frame] + local frame = animations[sprite.animation].frames[cycle] + local reversed = frame.reversed or false + local x, y, w, h, sx, sy, f = sprite.pos.x, sprite.pos.y, frame.frame.w, frame.frame.h, frame.frame.x, frame.frame.y, (not reversed) ~= (not sprite.flipped) + draw.surf(sx, sy, w, h, x-1, y-1, w, h, f) + draw.surf(sx, sy, w, h, x, y-1, w, h, f) + draw.surf(sx, sy, w, h, x+1, y-1, w, h, f) + draw.surf(sx, sy, w, h, x-1, y, w, h, f) + draw.surf(sx, sy, w, h, x+1, y, w, h, f) + draw.surf(sx, sy, w, h, x-1, y+1, w, h, f) + draw.surf(sx, sy, w, h, x, y, w, h, f) + draw.surf(sx, sy, w, h, x+1, y+1, w, h, f) + pal.subpal() + draw.surf(sx, sy, w, h, x, y, w, h, f) end } \ No newline at end of file diff --git a/data/templates.lua b/data/templates.lua index 0d07c87..2ba945f 100644 --- a/data/templates.lua +++ b/data/templates.lua @@ -35,6 +35,7 @@ templates = { else error("Template not recognized") end + sprite.room = rooms.current() print("creat sprite de tipus "..type) return sprite end,