I need some help with making a save button for my MS paint clone project, because I don't know where to start with this thing:
1. I have a image (SaveIcon.png) that I'll need to be clickable.
2. The save function needs to "screenshot"/save where you have drawn your image on your "canvas" (800x480).
3. I want the save icon to be placed to the right of the UI because on the left I have a box that sows colours that you're using.
4. I also want to be able to name the file before saving the image.
I really need some help with this because I don't really know if I can do it myself..
Code: Select all
function love.load()
squares = {}
Lines = {}
currentLine = {0, 0, 0, 0}
lineSize = 16
mouseIsDown = false
-- Colors
daColors =
--[[White]]--
{{255, 255, 255},
--[[Blue]]--
{50, 130, 185},
--[[Brown]]--
{143, 114, 30},
--[[Green]]--
{50, 158, 30},
--[[Orange]]--
{199, 129, 68},
--[[Pink]]--
{226, 131, 239},
--[[Purple]]--
{143, 83, 185},
--[[Red]]--
{171, 91, 75},
--[[Turquoise]]--
{50, 137, 151},
--[[Black]]--
{0, 0, 0}}
curColor = 1
-- User Interface
ui = love.graphics.newImage("heliumUI.png")
-- White Background
love.graphics.setBackgroundColor(255, 255, 255)
end
function love.update( dt )
-- The Brush
local x, y = love.mouse.getPosition()
if love.mouse.isDown( "l" ) and not love.keyboard.isDown("lshift") then
local newPosition = {}
newPosition.x = x - 8
newPosition.y = y - 8
newPosition.color = daColors[curColor]
table.insert( squares, newPosition )
end
if mouseIsDown then
currentLine.x2 = x
currentLine.y2 = y
end
end
-- Draws All The Graphics --
function love.draw()
local x, y = love.mouse.getPosition()
love.graphics.setLineWidth(1)
for key, square in ipairs( squares ) do
love.graphics.setColor(square.color)
love.graphics.rectangle( "fill", square.x, square.y, 16, 16 )
end
-- Draws Current Colour
love.graphics.setColor(daColors[curColor])
love.graphics.rectangle("fill", 47, 515, 100, 50)
-- Draws A 16x16 Square That Follows The Mouse
love.graphics.rectangle("line", x - 8, y - 8, 16, 16)
love.graphics.setLineWidth(lineSize)
for i = 1, #Lines do
love.graphics.setColor(Lines[i].color)
love.graphics.line(Lines[i].x1, Lines[i].y1, Lines[i].x2, Lines[i].y2)
end
love.graphics.setColor(daColors[curColor])
love.graphics.line(currentLine.x1, currentLine.y1, currentLine.x2, currentLine.y2)
-- User Interface
love.graphics.setColor(255, 255, 255)
love.graphics.draw(ui, 0, 480)
end
function love.mousepressed(x, y, button)
-- Drop Tool -- I'll leave this here in case you ever need it.
-- if button == "l" and love.keyboard.isDown("lshift") then
-- local screenshot = love.graphics.newScreenshot()
-- local r, g, b = screenshot: getPixel(x - 1, y - 1)
-- daColors[curColor] = {r, g, b}
-- end
if button == "r" then
mouseIsDown = true
currentLine = {x1 = x, y1 = y, x2 = x, y2 = y}
end
end
function love.mousereleased(x, y, button)
if button == "r" and mouseIsDown then
mouseIsDown = false
currentLine.color = daColors[curColor]
table.insert(Lines, currentLine)
currentLine = {0, 0, 0, 0}
end
end
function love.keypressed(key)
-- Select Your Color By Pressing The Number Keys (1 to 0)
if key == "1" then
curColor = 1 -- White
elseif key == "2" then
curColor = 2 -- Blue
elseif key == "3" then
curColor = 3 -- Brown
elseif key == "4" then
curColor = 4 -- Green
elseif key == "5" then
curColor = 5 -- Orange
elseif key == "6" then
curColor = 6 -- Pink
elseif key == "7" then
curColor = 7 -- Purple
elseif key == "8" then
curColor = 8 -- Red
elseif key == "9" then
curColor = 9 -- Turquoise
elseif key == "0" then
curColor = 10 -- Black
elseif key == "f" then
squares = {}
Lines = {}
love.graphics.setBackgroundColor(daColors[curColor])
end
end