[MOD 0.11.x] Planning-Tool-Mod v0.3.0

Topics and discussion about specific mods
User avatar
Cheata
Long Handed Inserter
Long Handed Inserter
Posts: 78
Joined: Sun Aug 17, 2014 6:46 am
Contact:

Re: [MOD 0.11.x] Planning-Tool-Mod v0.3.0

Post by Cheata »

Planning tool seems to be having issues with the lack of game.player.cursorposition, game.players[0].cursorposition doesn't seem to work either so iv made a few tweaks to get the rectangle feature working again in 0.11.18

all my changes are in the control.lua but there are a lot of them so i'm just going to post the whole file in a code segment below

instead of poling off the cursor position every few ticks iv set it up so that the first click defines the start of the band box and every click thereafter will define or redefine the size of the box and displays a preview. to apply the preview you need to click the finish button in the top left or the cancel button to discard the band box.

your first click will appear to do nothing but the "selection Tool" box should appear in the top left to let you know your in selection mode. after clicking finish or cancel the "selection Tool box" will disappear

Code: Select all

require "defines"
require "util"

game.oninit(function()
	initTables()
end)


game.onload(function()
	initTables()
	if game.player.gui.left.planningTool ~= nil then
		game.player.gui.left.planningTool.destroy()
	end
	if glob.planningTool.visible == true then
		showGUI()
	end
end)


game.onsave(function()
	if game.player.gui.left.planningTool ~= nil then
		game.player.gui.left.planningTool.destroy()
	end
end)


game.onevent(defines.events.onputitem, function(event)
	if (game.player.cursorstack ~= nil) and (game.player.cursorstack.name == "planning-tool") then
		
		if game.player.gui.left.planningTool == nil then
			showGUI()
			return
		else
			if glob.planningTool.hideFlag == true then
				game.player.print({"drawingNotPossible"})
				return
			end

			if glob.planningTool.modeFlag == false then
				drawFreehand(event)
			else
				if glob.planningTool.armed == false then
          showSelect()
					glob.planningTool.startPos = event.position
					glob.planningTool.armed = true
				else
					glob.planningTool.endPos = event.position
					drawPreview()
				end
			end
		end
	end
end)

function applyselection()
  for _, preObj in ipairs(glob.planningTool.previewObjects) do
    local tmpPos = preObj.position
    local color = glob.planningTool.color
    local EntName = "pt-overlay-"..color
    if glob.planningTool.checkerboardFlag then
      EntName = EntName ..((math.floor(tmpPos.x) + math.floor(tmpPos.y)) % 2)
    else
      EntName = EntName .."0"
    end

    local tmpValid = true
    for index, obj in ipairs(glob.planningTool.objects) do
      if obj.valid == true then
        if (obj.position.x == tmpPos.x) and (obj.position.y == tmpPos.y) then
          if (glob.planningTool.overwriteFlag == true) or (glob.planningTool.color == "remove") then
            obj.destroy()
            table.remove(glob.planningTool.objects, index)
            if glob.planningTool.color ~= "remove" then
              table.insert(glob.planningTool.objects, game.createentity{name = EntName, position = tmpPos})
            end
            tmpValid = false
            break
          else
            tmpValid = false
            break
          end
        end
      end
    end
    if (tmpValid == true) and (glob.planningTool.color ~= "remove") then
      table.insert(glob.planningTool.objects, game.createentity{name = EntName, position = tmpPos})
    end
  end
end

function clearPreview ()
  while (#glob.planningTool.previewObjects > 0) do
    if glob.planningTool.previewObjects[#glob.planningTool.previewObjects].valid == true then
      glob.planningTool.previewObjects[#glob.planningTool.previewObjects].destroy()
    end
    table.remove(glob.planningTool.previewObjects)
  end
  glob.planningTool.armed = false
  glob.planningTool.size = {x = 0, y = 0}
  game.player.gui.left.planningTool.destroy()
  showGUI()
end

game.onevent(defines.events.onguiclick, function(event)
	if event.element.name == "ptColorButton" then
		glob.planningTool.colorIndex = glob.planningTool.colorIndex + 1
		if glob.planningTool.colorIndex > 7 then
			glob.planningTool.colorIndex = 1
		end
		glob.planningTool.color = glob.planningTool.colors[glob.planningTool.colorIndex]
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name =="ptHideButton" then
		if glob.planningTool.hideFlag == false then
			glob.planningTool.hideFlag = true
			hideObjects(true)
		else
			glob.planningTool.hideFlag = false
			hideObjects(false)
		end
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name =="ptRemoveAllButton" then
		removeAll()
	elseif event.element.name == "ptCloseButton" then
		glob.planningTool.visible = false
		game.player.gui.left.planningTool.destroy()
		return
	elseif event.element.name == "ptModeCheck" then
		if glob.planningTool.modeFlag == false then
			glob.planningTool.modeFlag = true
		else
			glob.planningTool.modeFlag = false
		end
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name == "ptFilledCheck" then
		if glob.planningTool.filledFlag == false then
			glob.planningTool.filledFlag = true
		else
			glob.planningTool.filledFlag = false
		end
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name == "ptOverwriteCheck" then
		if glob.planningTool.overwriteFlag == false then
			glob.planningTool.overwriteFlag = true
		else
			glob.planningTool.overwriteFlag = false
		end
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name == "ptCheckerboardCheck" then
		if glob.planningTool.checkerboardFlag == false then
			glob.planningTool.checkerboardFlag = true
		else
			glob.planningTool.checkerboardFlag = false
		end
		game.player.gui.left.planningTool.destroy()
		showGUI()
	elseif event.element.name == "stFinish" then
    applyselection()
    clearPreview()
    glob.selectTool.visible = false
    game.player.gui.top.selectTool.destroy()
	elseif event.element.name == "stCancel" then
    clearPreview()
    glob.selectTool.visible = false
    game.player.gui.top.selectTool.destroy()
	end
end)



function initTables()


	if glob.planningTool == nil then
		glob.planningTool = {}
	end
	
	if glob.selectTool == nil then
		glob.selectTool = {}
	end

	if glob.planningTool.colors == nil then
		glob.planningTool.colors = {"blue", "cyan", "green", "magenta", "red", "yellow", "remove"}
	end

	if glob.planningTool.colorIndex == nil then
		glob.planningTool.colorIndex = 1
	end

	if glob.planningTool.color == nil then
		glob.planningTool.color = glob.planningTool.colors[glob.planningTool.colorIndex]
	end

	if glob.planningTool.objects == nil then
		glob.planningTool.objects = {}
	end

	if glob.planningTool.hiddenObjects == nil then
		glob.planningTool.hiddenObjects = {}
	end

  if glob.selectTool.visible == nil then
    glob.selectTool.visible = false
  end
  
	if glob.planningTool.visible == nil then
		glob.planningTool.visible = false
	end

	if glob.planningTool.overwriteFlag == nil then
		glob.planningTool.overwriteFlag = false
	end

	if glob.planningTool.checkerboardFlag == nil then
		glob.planningTool.checkerboardFlag = false
	end

	if glob.planningTool.modeFlag == nil then
		glob.planningTool.modeFlag = false --"freehand" == false; "rect" == true
	end

	if glob.planningTool.filledFlag == nil then
		glob.planningTool.filledFlag = false
	end

	if glob.planningTool.armed == nil then
		glob.planningTool.armed = false
	end

	if glob.planningTool.startPos == nil then
		glob.planningTool.startPos = {}
	end
	
	if glob.planningTool.endPos == nil then
		glob.planningTool.endPos = {}
	end

	if glob.planningTool.previewObjects == nil then
		glob.planningTool.previewObjects = {}
	end

	if glob.planningTool.hideFlag == nil then
		glob.planningTool.hideFlag = false
	end

	if glob.planningTool.size == nil then
		glob.planningTool.size = {x = 0, y = 0} 
	end
end

function showSelect()
  if game.player.gui.top.selectTool == nil then
    glob.selectTool.visible=true
    local rootFrame = game.player.gui.top.add{type="frame", name="selectTool", caption="Selection Tool", direction = "vertical"}
    local table = rootFrame.add{type="table", name = "selectToolTable", colspan = 2}
    table.add{type = "button", name = "stFinish", caption="Finish"}
    table.add{type = "button", name = "stCancel", caption="Cancel"}
  end 
end

function showGUI()
	if game.player.gui.left.planningTool == nil then
		glob.planningTool.visible = true
		local rootFrame = game.player.gui.left.add{type = "frame", name = "planningTool", caption = {"planningTool"}, direction = "vertical"}
		local table = rootFrame.add{type ="table", name = "planningToolTable", colspan = 2}
			table.add{type = "label", name = "ptColorLabel", caption = {"colorLabel"}}
			table.add{type = "button", name = "ptColorButton", caption = getColorName(), style = "smallerButtonFont"}
			table.add{type = "label", name = "ptModeLabel", caption = {"modeLabel"}}
			table.add{type = "checkbox", name = "ptModeCheck", state = glob.planningTool.modeFlag}
			if glob.planningTool.modeFlag == true then
				table.add{type = "label", name = "ptFilledLabel", caption = {"filledLabel"}}
				table.add{type = "checkbox", name = "ptFilledCheck", state = glob.planningTool.filledFlag}
			end
			table.add{type = "label", name = "ptOverwriteLabel", caption = {"overwriteLabel"}}
			table.add{type = "checkbox", name = "ptOverwriteCheck", state = glob.planningTool.overwriteFlag}
			table.add{type = "label", name = "ptCheckerboardLabel", caption = {"checkerboardLabel"}}
			table.add{type = "checkbox", name = "ptCheckerboardCheck", state = glob.planningTool.checkerboardFlag}
		if glob.planningTool.modeFlag == true then
			local size = glob.planningTool.size
			if (size.x ~= 0) and (size.y ~= 0) then
				table.add{type = "label", name = "ptSizeLabel", caption = {"sizeLabel"}}
				table.add{type = "label", name = "ptSizeLabel2", caption = size.x.."x"..size.y}
			end
		end
		local hideCaption
		if glob.planningTool.hideFlag == false then
			hideCaption = {"hideHideButton"}
		else
			hideCaption = {"hideShowButton"}
		end
		rootFrame.add{type ="button", name = "ptHideButton", caption = hideCaption, style = "smallerButtonFont"}
		rootFrame.add{type ="button", name = "ptRemoveAllButton", caption = {"removeAllButton"}, style = "smallerButtonFont"}
		rootFrame.add{type ="button", name = "ptCloseButton", caption = {"closeButton"}, style = "smallerButtonFont"}
	end
end

function getColorName()
	for _, color in ipairs(glob.planningTool.colors) do
		if glob.planningTool.color == color then
			return {color}
		end
	end
end

function removeAll()
	while #glob.planningTool.objects > 0 do
		if glob.planningTool.objects[#glob.planningTool.objects].valid then
			glob.planningTool.objects[#glob.planningTool.objects].destroy()
		end
		table.remove(glob.planningTool.objects)
	end
end






function drawFreehand(event)
	local color = glob.planningTool.color
	local EntName = "pt-overlay-"..color
	
	if glob.planningTool.checkerboardFlag then
		EntName = EntName ..((math.floor(event.position.x) + math.floor(event.position.y)) % 2)
	else
		EntName = EntName .."0"
	end
			
	if glob.planningTool.colorIndex ~= 7 then
		if (game.canplaceentity{name = EntName, position = event.position}) then
			table.insert(glob.planningTool.objects, game.createentity{name = EntName, position = event.position})
		elseif glob.planningTool.overwriteFlag == true then
			local PosB = {x = math.floor(event.position.x), y = math.floor(event.position.y)}
			local PosE = {x = math.ceil(event.position.x), y = math.ceil(event.position.y)}
			local obstacles = game.findentitiesfiltered{area = {PosB, PosE}, type="decorative"}
			for _, object in ipairs(obstacles) do
				local tmp1, tmp2 = string.find(object.name, "pt-overlay", 1, true)
				if (tmp1 ~= nil) and (tmp2 ~= nil) then
					if object.valid == true then
						object.destroy()
					end
				end
			end
			table.insert(glob.planningTool.objects, game.createentity{name = EntName, position = event.position})
		end
	else -- color == "remove"
		if not game.canplaceentity{name = EntName, position = event.position} then
			local PosB = {x = math.floor(event.position.x), y = math.floor(event.position.y)}
			local PosE = {x = math.ceil(event.position.x), y = math.ceil(event.position.y)}
			local obstacles = game.findentitiesfiltered{area = {PosB, PosE}, type="decorative"}
			for _, object in ipairs(obstacles) do
				local tmp1, tmp2 = string.find(object.name, "pt-overlay", 1, true)
				if (tmp1 ~= nil) and (tmp2 ~= nil) then
					if object.valid == true then
						object.destroy()
					end
				end
			end
		end
	end
end


function drawPreview()
	while (#glob.planningTool.previewObjects > 0) do
		if glob.planningTool.previewObjects[#glob.planningTool.previewObjects].valid == true then
			glob.planningTool.previewObjects[#glob.planningTool.previewObjects].destroy()
		end
		table.remove(glob.planningTool.previewObjects)
	end

	local color = glob.planningTool.color
	local EntName = "pt-overlay-"..color.."1"
	
	local cursorPos = glob.planningTool.endPos
	cursorPos.x = math.floor(cursorPos.x)
	cursorPos.y = math.floor(cursorPos.y)

	local startPos = glob.planningTool.startPos
	startPos.x = math.floor(startPos.x)
	startPos.y = math.floor(startPos.y)

	glob.planningTool.size.x = math.abs(startPos.x - cursorPos.x) + 1
	glob.planningTool.size.y = math.abs(startPos.y - cursorPos.y) + 1
	if game.player.gui.left.planningTool ~= nil then game.player.gui.left.planningTool.destroy() end
	showGUI()

	local stepX, stepY
	if startPos.x >= cursorPos.x then
		stepX = -1
	else
		stepX = 1
	end
	if startPos.y >= cursorPos.y then
		stepY = -1
	else
		stepY = 1
	end

	if glob.planningTool.filledFlag == true then
		for i = startPos.x, cursorPos.x, stepX do
			for j = startPos.y, cursorPos.y, stepY do
				table.insert(glob.planningTool.previewObjects, game.createentity{name = EntName, position = {x = i, y = j}})
			end
		end
	else
		for i = startPos.x, cursorPos.x, stepX do
			table.insert(glob.planningTool.previewObjects, game.createentity{name = EntName, position = {x = i, y = startPos.y}})
			table.insert(glob.planningTool.previewObjects, game.createentity{name = EntName, position = {x = i, y = cursorPos.y}})
		end

		for j = startPos.y + stepY, cursorPos.y - stepY, stepY do
			table.insert(glob.planningTool.previewObjects, game.createentity{name = EntName, position = {x = startPos.x, y = j}})
			table.insert(glob.planningTool.previewObjects, game.createentity{name = EntName, position = {x = cursorPos.x, y = j}})
		end
	end
end

function hideObjects(shouldHide)
	if shouldHide == true then
		while (#glob.planningTool.objects > 0) do
			local tmpObj = glob.planningTool.objects[#glob.planningTool.objects]
			table.insert(glob.planningTool.hiddenObjects, {name = tmpObj.name, position = tmpObj.position})
			if glob.planningTool.objects[#glob.planningTool.objects].valid == true then
				glob.planningTool.objects[#glob.planningTool.objects].destroy()
			end
			table.remove(glob.planningTool.objects)
		end
	else
		while (#glob.planningTool.hiddenObjects > 0) do
			local tmpObj = glob.planningTool.hiddenObjects[#glob.planningTool.hiddenObjects]
			table.insert(glob.planningTool.objects, game.createentity{name = tmpObj.name, position = tmpObj.position})
			table.remove(glob.planningTool.hiddenObjects)
		end
	end
end
please note i am not trying to take over this mod or take credit for it. I have just done a little work allow it to run in my current environment and wish to share it in case anybody else is having the same issues while waiting for a new release

drs9999 please feel free to use or discard any changes i have made as you wish

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: [MOD 0.11.x] Planning-Tool-Mod v0.3.0

Post by FishSandwich »

This needs updating to work in multiplayer. :D

Vorgon
Manual Inserter
Manual Inserter
Posts: 2
Joined: Wed Mar 02, 2016 1:25 pm
Contact:

Re: [MOD 0.11.x] Planning-Tool-Mod v0.3.0

Post by Vorgon »

Nice mod!

I've updated it to make it compatible with 0.12.24.

The thread is here: viewtopic.php?f=120&t=20745.

It's not my intention to take over something that isn't mine. I saw that the mod was incompatible with 0.12.x and that nobody had posted about it for almost a year, and thought it would be useful to make it compatible.

Post Reply

Return to “Mods”