Can expand and reduce canvass with lctrl and lshift. (Bug: all tiles get erased :c)

This commit is contained in:
lustlion 2022-01-31 01:22:15 +01:00
parent 727592ac55
commit e1bfd4f7f5
3 changed files with 106 additions and 18 deletions

View File

@ -115,34 +115,35 @@ function EditorDoEdit()
and LevelTiles[vertical][horizontal] ~= nil
and love.keyboard.isDown("lshift") ~= true
and love.keyboard.isDown("lctrl") ~= true
then
if Keybind:CheckDown(Keybind.generic.lclick)
and selecting_tile ~= nil
then
if Keybind:CheckDown(Keybind.generic.lclick) then
InstanceTile(vertical,horizontal,selecting_tile)
SetTile(vertical,horizontal,selecting_tile)
elseif Keybind:CheckDown(Keybind.generic.rclick) then
InstanceTile(vertical,horizontal,0)
SetTile(vertical,horizontal,0)
end
LevelReloadTiles()
--[[
elseif love.keyboard.isDown("lshift") then
--ExpandCanvas(math.sign(expand_h),math.sign(expand_v))
elseif Keybind:HasPressed(Keybind.generic.lshift) then
LevelExpandCanvas(math.sign(expand_h),math.sign(expand_v))
if expand_h < 0 then
Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width
Camera.pos.x = Camera.pos.x - math.sign(expand_h)*tileProperties.scale*tileProperties.width
end
if expand_v < 0 then
Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height
Camera.pos.y = Camera.pos.y - math.sign(expand_v)*tileProperties.scale*tileProperties.height
end
elseif love.keyboard.isDown("lctrl") then
--ReduceCanvas(math.sign(expand_h),math.sign(expand_v))
elseif Keybind:HasPressed(Keybind.generic.lctrl) then
LevelReduceCanvas(math.sign(expand_h),math.sign(expand_v))
if expand_h < 0 then
Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width
Camera.pos.x = Camera.pos.x - math.sign(expand_h)*tileProperties.scale*tileProperties.width
end
if expand_v < 0 then
Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height
end]]
Camera.pos.y = Camera.pos.y - math.sign(expand_v)*tileProperties.scale*tileProperties.height
end
end
end
end

View File

@ -71,6 +71,8 @@ function Keybind:Default()
-- Generic
Keybind.generic.lclick = { keys = {1}}
Keybind.generic.rclick = { keys = {2}}
Keybind.generic.lshift = { keys = {"lshift"}}
Keybind.generic.lctrl = { keys = {"lctrl"}}
end
-- Set default values at start

View File

@ -19,6 +19,86 @@ function LevelLoadTiles()
TileCreateObjects()
end
function LevelExpandCanvas(horizontal,vertical)
local h = LevelGetTileWidth()
local v = LevelGetTileHeight()
-- get new canvas size
local newCanvasH = h + math.abs(horizontal)
local newCanvasV = v + math.abs(vertical)
-- lets make a new temporal canvas
local ExpandedLevel = {}
for i = 1, newCanvasV do
ExpandedLevel[i] = {}
for j = 1, newCanvasH do
ExpandedLevel[i][j] = InstanceTile(0)
end
end
-- lets guess how the new canvas and positions are offset
local expand_h = 0
if horizontal < 0 then
expand_h = -horizontal
end
local expand_v = 0
if vertical < 0 then
expand_v = -vertical
end
-- get data from old canvas to new canvas
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
ExpandedLevel[i+expand_v][j+expand_h] = InstanceTile(LevelTiles[i][j])
end
end
-- use new canvas
LevelTiles = ExpandedLevel
end
function LevelReduceCanvas(horizontal,vertical)
local h = LevelGetTileWidth()
local v = LevelGetTileHeight()
-- get new canvas size
local newCanvasH = h - math.abs(horizontal)
local newCanvasV = v - math.abs(vertical)
-- lets make a new temporal canvas
local ExpandedLevel = {}
for i = 1, newCanvasV do
ExpandedLevel[i] = {}
for j = 1, newCanvasH do
ExpandedLevel[i][j] = InstanceTile(0)
end
end
-- lets guess how the new canvas and positions are offset
local expand_h = 0
if horizontal < 0 then
expand_h = -horizontal
end
local expand_v = 0
if vertical < 0 then
expand_v = -vertical
end
-- get data from old canvas to new canvas
for i = 1, #ExpandedLevel do
for j = 1, #ExpandedLevel[i] do
ExpandedLevel[i][j] = InstanceTile(LevelTiles[i+expand_v][j+expand_h])
end
end
-- use new canvas
LevelTiles = ExpandedLevel
end
function LevelGetTileData()
for k, v in pairs(tileset) do
if v == LevelData.tileset then
@ -79,7 +159,7 @@ function LevelIndexTiles()
-- instance level tiles according to the Properties
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
InstanceTile(i,j,LevelTiles[i][j])
SetTile(i,j,LevelTiles[i][j])
end
end
end
@ -117,9 +197,8 @@ function TileDataInitialize()
end
end
function InstanceTile(i,j,id)
LevelTiles[i][j] = {}
local tile = LevelTiles[i][j]
function InstanceTile(id)
local tile = {}
tile.id = id
local Properties = TileData[tile.id]
@ -137,6 +216,12 @@ function InstanceTile(i,j,id)
tile.display = Properties.force
end
end
return tile
end
function SetTile(i,j,id)
LevelTiles[i][j] = InstanceTile(id)
end
function TileGetType(tile)