added function to cancel active prompt added function to add new spawns from editor added keybind for that

This commit is contained in:
lustlion 2022-03-11 18:39:27 +01:00
parent 3d41699d8f
commit 61b8aa883b
4 changed files with 117 additions and 50 deletions

View File

@ -7,39 +7,15 @@ editor = {
function stepEditor() function stepEditor()
editor.palette_mode = editor.palette_mode or false editor.palette_mode = editor.palette_mode or false
animateTiles() animateTiles()
if Keybind:checkPressed(Keybind.editor.room_mode) then if Prompt.active_prompt == nil then
if love.keyboard.isDown("lshift") then if Keybind:checkPressed(Keybind.editor.room_mode) then
editor.room_mode = "delete" if love.keyboard.isDown("lshift") then
else editor.room_mode = "delete"
editor.room_mode = not editor.room_mode else
editor.room_mode = not editor.room_mode
end
editor.room_points = {}
end 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 Keybind:checkPressed(Keybind.editor.palette_mode) then
if editor.palette_mode then if editor.palette_mode then
@ -53,6 +29,31 @@ function stepEditor()
end end
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: -- TODO:
-- i changed this but i dont know what was to do here. -- i changed this but i dont know what was to do here.
-- - made specific action keybinds -- - made specific action keybinds
@ -237,6 +238,10 @@ function doEditorEdit()
promptSpawnArchetype() promptSpawnArchetype()
elseif Keybind:checkDown(Keybind.editor.entity_modify_data) then elseif Keybind:checkDown(Keybind.editor.entity_modify_data) then
promptSpawnArgs() promptSpawnArgs()
elseif Keybind:checkDown(Keybind.editor.entity_remove) then
deleteSpawn()
elseif Keybind:checkDown(Keybind.editor.entity_new) then
promptSpawnNew()
end end
end end
end end

View File

@ -129,6 +129,8 @@ function Keybind:default()
Keybind.editor.entity_move = { keys = {2}} Keybind.editor.entity_move = { keys = {2}}
Keybind.editor.entity_modify_archetype = { keys = {"t"}} Keybind.editor.entity_modify_archetype = { keys = {"t"}}
Keybind.editor.entity_modify_data = { keys = {"g"}} Keybind.editor.entity_modify_data = { keys = {"g"}}
Keybind.editor.entity_remove = { keys = {"delete"}}
Keybind.editor.entity_new = { keys = {"n"}}
-- Generic -- Generic
Keybind.generic.lclick = { keys = {1}} Keybind.generic.lclick = { keys = {1}}
Keybind.generic.rclick = { keys = {2}} Keybind.generic.rclick = { keys = {2}}

View File

@ -55,10 +55,64 @@ function moveSpawns(x,y)
end end
end end
function promptSpawnArchetype()
if Prompt.active_prompt then function promptSpawnNew()
Prompt.active_prompt.canceled = true 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
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 for _, spawn in pairs(LoadedObjects.Spawns) do
if spawn.selected then if spawn.selected then
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset() local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
@ -79,8 +133,7 @@ function promptSpawnArchetype()
print("archetype changed --",succ) print("archetype changed --",succ)
print("from: ", spawn.archetype.type) print("from: ", spawn.archetype.type)
if not succ if not succ
or type(arch) ~= "table" or checkArchetypeInvalid(arch)
or type(arch.type) ~= "string"
then then
arch = spawn.archetype arch = spawn.archetype
end end
@ -90,25 +143,27 @@ function promptSpawnArchetype()
print("invalid input for prompt "..prompt.name) print("invalid input for prompt "..prompt.name)
end end
end, end,
}) })
prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x 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.y = (spawn.args[2]-20)*game.scale - Camera.pos.y - offset_y
prompt:activate() prompt:activate()
end end
end end
end end
function checkArgsAreInvalid(args) function checkArgsInvalid(args)
for _, arg in pairs(args) do 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
end end
function promptSpawnArgs() function promptSpawnArgs()
if Prompt.active_prompt then Prompt:cancelActive()
Prompt.active_prompt.canceled = true
end
for _, spawn in pairs(LoadedObjects.Spawns) do for _, spawn in pairs(LoadedObjects.Spawns) do
if spawn.selected then if spawn.selected then
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset() local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
@ -127,7 +182,7 @@ function promptSpawnArgs()
print("args changed --",succ) print("args changed --",succ)
print("from: ", unpack(args)) print("from: ", unpack(args))
if not succ if not succ
or checkArgsAreInvalid(args) or checkArgsInvalid(args)
then then
args = spawn.args args = spawn.args
end end
@ -137,9 +192,9 @@ function promptSpawnArgs()
print("invalid input for prompt "..prompt.name) print("invalid input for prompt "..prompt.name)
end end
end, end,
}) })
prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x 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.y = (spawn.args[2]-4)*game.scale - Camera.pos.y - offset_y
prompt:activate() prompt:activate()
end end
end end

View File

@ -24,6 +24,11 @@ Prompt = {
background_color = {0,0,0,1}, background_color = {0,0,0,1},
active_prompt = nil, active_prompt = nil,
} }
function Prompt:cancelActive()
if Prompt.active_prompt then
Prompt.active_prompt.canceled = true
end
end
function Prompt:new(o) function Prompt:new(o)
o = o or {} o = o or {}