From 61b8aa883b686f23601b152c895f6b5b8a5d2c9a Mon Sep 17 00:00:00 2001 From: lustlion Date: Fri, 11 Mar 2022 18:39:27 +0100 Subject: [PATCH] added function to cancel active prompt added function to add new spawns from editor added keybind for that --- code/editor.lua | 69 +++++++++++++++++++---------------- code/keybind.lua | 2 + code/spawn.lua | 91 +++++++++++++++++++++++++++++++++++++--------- code/ui/prompt.lua | 5 +++ 4 files changed, 117 insertions(+), 50 deletions(-) diff --git a/code/editor.lua b/code/editor.lua index 0e33556..1736731 100644 --- a/code/editor.lua +++ b/code/editor.lua @@ -7,39 +7,15 @@ editor = { function stepEditor() editor.palette_mode = editor.palette_mode or false animateTiles() - if Keybind:checkPressed(Keybind.editor.room_mode) then - if love.keyboard.isDown("lshift") then - editor.room_mode = "delete" - else - editor.room_mode = not editor.room_mode + if Prompt.active_prompt == nil then + if Keybind:checkPressed(Keybind.editor.room_mode) then + if love.keyboard.isDown("lshift") then + editor.room_mode = "delete" + else + editor.room_mode = not editor.room_mode + end + editor.room_points = {} end - editor.room_points = {} - end - - if Keybind:checkPressed(Keybind.editor.palette_mode) then - if editor.palette_mode then - editor.palette_mode = false - palette_scroll_x = nil - palette_scroll_y = nil - else - editor.palette_mode = true - palette_scroll_x = 0 - palette_scroll_y = 0 - end - end - - if Keybind:checkPressed(Keybind.editor.palette_mode) then - if editor.palette_mode then - editor.palette_mode = false - palette_scroll_x = nil - palette_scroll_y = nil - else - editor.palette_mode = true - palette_scroll_x = 0 - palette_scroll_y = 0 - end - end - if Keybind:checkPressed(Keybind.editor.palette_mode) then if editor.palette_mode then @@ -53,6 +29,31 @@ function stepEditor() end end + if Keybind:checkPressed(Keybind.editor.palette_mode) then + if editor.palette_mode then + editor.palette_mode = false + palette_scroll_x = nil + palette_scroll_y = nil + else + editor.palette_mode = true + palette_scroll_x = 0 + palette_scroll_y = 0 + end + end + + if Keybind:checkPressed(Keybind.editor.palette_mode) then + if editor.palette_mode then + editor.palette_mode = false + palette_scroll_x = nil + palette_scroll_y = nil + else + editor.palette_mode = true + palette_scroll_x = 0 + palette_scroll_y = 0 + end + end + end + -- TODO: -- i changed this but i dont know what was to do here. -- - made specific action keybinds @@ -237,6 +238,10 @@ function doEditorEdit() promptSpawnArchetype() elseif Keybind:checkDown(Keybind.editor.entity_modify_data) then promptSpawnArgs() + elseif Keybind:checkDown(Keybind.editor.entity_remove) then + deleteSpawn() + elseif Keybind:checkDown(Keybind.editor.entity_new) then + promptSpawnNew() end end end diff --git a/code/keybind.lua b/code/keybind.lua index f8abdeb..3c4105f 100644 --- a/code/keybind.lua +++ b/code/keybind.lua @@ -129,6 +129,8 @@ function Keybind:default() Keybind.editor.entity_move = { keys = {2}} Keybind.editor.entity_modify_archetype = { keys = {"t"}} Keybind.editor.entity_modify_data = { keys = {"g"}} + Keybind.editor.entity_remove = { keys = {"delete"}} + Keybind.editor.entity_new = { keys = {"n"}} -- Generic Keybind.generic.lclick = { keys = {1}} Keybind.generic.rclick = { keys = {2}} diff --git a/code/spawn.lua b/code/spawn.lua index caa4921..aa0722e 100644 --- a/code/spawn.lua +++ b/code/spawn.lua @@ -55,10 +55,64 @@ function moveSpawns(x,y) end end -function promptSpawnArchetype() - if Prompt.active_prompt then - Prompt.active_prompt.canceled = true + +function promptSpawnNew() + Prompt:cancelActive() + local text = "" + local prompt = Prompt:new({ + name = "new spawn", + input = text, + func = function(prompt) + local f = loadstring("return {"..prompt.input.."}") + if f ~= nil then + local succ, result = pcall(f) + local arch = result[1] + local args = {} + if #result > 1 then + for i=2, #result+1 do + print("arg #"..i-1) + args[i-1] = result[i] + end + else + args = {0,0} + end + + print("new entity spawn --",succ) + if not succ + or checkArchetypeInvalid(arch) + then + print("invalid input for prompt "..prompt.name) + else + print("archetype: ",arch.type) + print("args: ",unpack(args)) + addSpawn(arch, unpack(args)) + end + else + print("invalid input for prompt "..prompt.name) + end + end, + }) + prompt.pos.x = 0 + prompt.pos.y = 10 + prompt:activate() +end + +function deleteSpawn() + for i=1, #LoadedObjects.Spawns do + if LoadedObjects.Spawns[i] + and LoadedObjects.Spawns[i].selected then + table.remove(LoadedObjects.Spawns,i) + break + end end +end + +function checkArchetypeInvalid(arch) + return type(arch) ~= "table" or type(arch.type) ~= "string" +end + +function promptSpawnArchetype() + Prompt:cancelActive() for _, spawn in pairs(LoadedObjects.Spawns) do if spawn.selected then local offset_x, offset_y = spawn.archetype.display:getCenteredOffset() @@ -79,8 +133,7 @@ function promptSpawnArchetype() print("archetype changed --",succ) print("from: ", spawn.archetype.type) if not succ - or type(arch) ~= "table" - or type(arch.type) ~= "string" + or checkArchetypeInvalid(arch) then arch = spawn.archetype end @@ -90,25 +143,27 @@ function promptSpawnArchetype() print("invalid input for prompt "..prompt.name) end end, - }) - prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x - prompt.pos.y = (spawn.args[2]-20)*game.scale - Camera.pos.y - offset_y - + }) + prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x + prompt.pos.y = (spawn.args[2]-20)*game.scale - Camera.pos.y - offset_y prompt:activate() end end end -function checkArgsAreInvalid(args) +function checkArgsInvalid(args) for _, arg in pairs(args) do - if arg == nil then return true end + -- this is checking the args are not nil variables + if arg == nil + or arg == "" + then + return true + end end end function promptSpawnArgs() - if Prompt.active_prompt then - Prompt.active_prompt.canceled = true - end + Prompt:cancelActive() for _, spawn in pairs(LoadedObjects.Spawns) do if spawn.selected then local offset_x, offset_y = spawn.archetype.display:getCenteredOffset() @@ -127,7 +182,7 @@ function promptSpawnArgs() print("args changed --",succ) print("from: ", unpack(args)) if not succ - or checkArgsAreInvalid(args) + or checkArgsInvalid(args) then args = spawn.args end @@ -137,9 +192,9 @@ function promptSpawnArgs() print("invalid input for prompt "..prompt.name) end end, - }) - prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x - prompt.pos.y = (spawn.args[2]-4)*game.scale - Camera.pos.y - offset_y + }) + prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x + prompt.pos.y = (spawn.args[2]-4)*game.scale - Camera.pos.y - offset_y prompt:activate() end end diff --git a/code/ui/prompt.lua b/code/ui/prompt.lua index 36ea8b1..f905c6c 100644 --- a/code/ui/prompt.lua +++ b/code/ui/prompt.lua @@ -24,6 +24,11 @@ Prompt = { background_color = {0,0,0,1}, active_prompt = nil, } +function Prompt:cancelActive() + if Prompt.active_prompt then + Prompt.active_prompt.canceled = true + end +end function Prompt:new(o) o = o or {}