-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplacer.lua
More file actions
101 lines (86 loc) · 3.15 KB
/
placer.lua
File metadata and controls
101 lines (86 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Placer = class('Placer')
local editor = editor
-- this line is OK as long as Editor requires Placer.
function Placer:initialize(x,y)
self.pos = vector(x,y) --The position to display the placer
--Class specific variables
self.size = vector(1240,600) -- The size of the placer's viewport
self.canvas = love.graphics.newCanvas(self.size.x,self.size.y)
self.camera = camera(0,0)
self.drawing = false
end
function Placer:snapToGrid(wx,wy)
gx = utility.round(wx / editor.settings.gridSize,0) * editor.settings.gridSize
gy = utility.round(wy / editor.settings.gridSize,0) * editor.settings.gridSize
return gx,gy
end
function Placer:mousepressed(mx,my,btn)
if utility.point2Box(mx,my,self.pos.x,self.pos.y,self.size.x,self.size.y) then
wx,wy = self.camera:worldCoords(mx,my)
gx,gy = self:snapToGrid(wx,wy)
--HAHA MAGIC NUMBERS! YOU LOVE IT CHEESE
if editor.settings.drawMode == 1 then
--select
editor:selectShapeAt(gx,gy)
end
if editor.settings.drawMode == 2 then
--new shape
editor:newShape(true)
editor:addVertexAt(gx,gy)
editor.settings.drawMode = 4
end
if editor.settings.drawMode == 3 then
--delete shape
editor:deleteShapeAt(gx,gy)
end
if editor.settings.drawMode == 4 then
--add vertex
editor:addVertexAt(gx,gy)
end
if editor.settings.drawMode == 5 then
--delete vertex
editor:deleteVertexAt(gx,gy)
end
end
end
function Placer:update(dt)
local cameraSpeed = 200
--KEYBOARD CAMERA MOVEMENT
if love.keyboard.isDown('w') then self.camera:move(0,-cameraSpeed*dt) end
if love.keyboard.isDown('s') then self.camera:move(0,cameraSpeed*dt) end
if love.keyboard.isDown('a') then self.camera:move(-cameraSpeed*dt,0) end
if love.keyboard.isDown('d') then self.camera:move(cameraSpeed*dt,0) end
end
function Placer:draw()
local mx,my = love.mouse.getX(),love.mouse.getY()
local mgx,mgy = self:snapToGrid(self.camera:worldCoords(mx,my))
--SET canvas
love.graphics.setCanvas(self.canvas)
self.canvas:clear(0,0,0,0)
--ATTACH CAMERA
self.camera:attach()
--Draw grid
--Draw shapes
for i,v in ipairs(editor.shapes) do
v:draw()
if i == editor.settings.selectedShape then
love.graphics.setColor(50,255,50,50)
love.graphics.rectangle('fill',v.bounds.pos.x,v.bounds.pos.y,v.bounds.size.x,v.bounds.size.y)
end
end
--Draw grid-snapped cursor
love.graphics.circle('fill',mgx,mgy,2)
--UNSET CANVAS
love.graphics.setCanvas()
--DETACH CAMERA
self.camera.detach()
--UI
love.graphics.print("mouse (Screen): "..love.mouse.getX()..", "..love.mouse.getY(),0,70)
love.graphics.print("mouse (Grid): "..mgx..", "..mgy,0,90)
--draw canvas
love.graphics.setColor(255,255,255,255)
love.graphics.draw(self.canvas,self.pos.x,self.pos.y)
--Draw bounds of canvas
love.graphics.setColor(70,70,70,255)
love.graphics.rectangle('line',self.pos.x,self.pos.y,self.size.x,self.size.y)
end