Skip to content

Commit

Permalink
Room window can now use tile position and size
Browse files Browse the repository at this point in the history
Shows in pixels if `editor.itemAllowPixelPerfect` is enabled
  • Loading branch information
Cruor committed Dec 19, 2021
1 parent 3a913ca commit 55fb138
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/ui/windows/room_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local viewportHandler = require("viewport_handler")
local mapItemUtils = require("map_item_utils")
local sceneHandler = require("scene_handler")
local entityStruct = require("structs.entity")
local configs = require("configs")

local roomWindow = {}

Expand Down Expand Up @@ -151,10 +152,19 @@ local function handleCheckpoint(room, hasCheckpoint)
end
end

local function saveRoomCallback(room, editing)
local function saveRoomCallback(room, editing, usingPixels)
return function(formFields)
local newRoomData = form.getFormData(formFields)

-- Restore tile to pixel values
if not usingPixels then
newRoomData.x = newRoomData.x * 8
newRoomData.y = newRoomData.y * 8

newRoomData.width = newRoomData.width * 8
newRoomData.height = newRoomData.height * 8
end

if editing then
local targetRoom = loadedState.getRoomByName(room.name)
local before = utils.deepcopy(targetRoom)
Expand Down Expand Up @@ -247,6 +257,18 @@ function roomWindow.createRoomWindow(room, editing)
return
end

local usingPixels = configs.editor.itemAllowPixelPerfect

-- Floor position and size attributes in room data to convert to tiles
-- Multiplied back to pixel values in the save callback
if not usingPixels then
room.x = math.floor(room.x / 8)
room.y = math.floor(room.y / 8)

room.width = math.floor(room.width / 8)
room.height = math.floor(room.height / 8)
end

-- Not a actual attribute of the room
-- Used to add a checkpoint entity
room.checkpoint = checkCheckpointEntity(room)
Expand Down Expand Up @@ -290,7 +312,7 @@ function roomWindow.createRoomWindow(room, editing)
{
text = tostring(language.ui.room_window.save_changes),
formMustBeValid = true,
callback = saveRoomCallback(room, editing)
callback = saveRoomCallback(room, editing, usingPixels)
},
{
text = tostring(language.ui.room_window.close_window),
Expand Down

0 comments on commit 55fb138

Please sign in to comment.