-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.lua
More file actions
113 lines (101 loc) · 2.94 KB
/
editor.lua
File metadata and controls
113 lines (101 loc) · 2.94 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
102
103
104
105
106
107
108
109
110
111
112
113
--Gamestate declaration
editor = {}
--Class requirements
require ('shape')
require('combo')
require('placer')
--Logger
local l = require('logger')
--First-time state initialization
function editor:init()
self.settings = {
drawMode = 1,
selectedShape =1,
gridSize = 32
}
self.shapes = {}
self.states = {}
--Widgets come after all declarations since we pass our table to it
self.widgets = {
modeSelector = Combo:new(10,650,5),
placer = Placer:new(10,10)
}
end
--Gets & Sets
function editor:getSelectedShape()
local s = self.shapes[self.settings.selectedShape]
if s ~= nil then return s else l.log("There is no selection!",4) end
end
function editor:selectShapeByIndex(index)
if self.shapes[index] then
self.settings.selectedShape = index
else l.log("Select: Shape at index "..index.." does not exist!",4) end
end
function editor:selectShapeAt(x,y)
--This is lazy right now. If two shapes have the same bounds it only grabs the oldest.
for i,v in ipairs(self.shapes) do
if utility.point2Box(x,y,v.bounds.pos.x,v.bounds.pos.y,v.bounds.size.x,v.bounds.size.y)
then
self:selectShapeByIndex(i)
break
end
end
end
function editor:newShape(select)
local select = select or true
--if select, select the new shape immediately
table.insert(self.shapes,Shape:new())
if select then self:selectShapeByIndex(table.getn(self.shapes)) end
end
function editor:deleteShape(index)
index = index or self.settings.selectedShape
table.remove(self.shapes,index)
self:selectShapeByIndex(table.getn(self.shapes))
end
function editor:deleteShapeAt(x,y)
self:selectShapeAt(x,y)
self:deleteShape()
end
function editor:addVertexAt(x,y)
if self:getSelectedShape() then self:getSelectedShape():addVertex(x,y) end
end
function editor:deleteVertexAt(x,y)
--Searches vertices of selected shape to attempt a removal at specified coords
local shape = self:getSelectedShape()
local found = false
if shape then
for i,v in ipairs(shape.vertices) do
if v.pos.x == x and v.pos.y == y then
found = true
shape:deleteVertexByIndex(i)
break
end
end
if not found then l.log("Vertex at: "..x..", "..y.." not found!",2) end
end
end
---CALLBACKS
--Update callback
function editor:update(dt)
for i,v in pairs(self.widgets) do
if v.update then v:update(dt) end
end
end
--Draw callback
function editor:draw()
for i,v in pairs(self.widgets) do
if v.draw then v:draw() end
end
end
--Mousepressed
function editor:mousepressed(mx,my,btn)
for i,v in pairs(self.widgets) do
if v.mousepressed then v:mousepressed(mx,my,btn) end
end
end
--Mousereleased
function editor:mousereleased(mx,my,btn)
for i,v in pairs(self.widgets) do
if v.mousereleased then v:mousepressed(mx,my,btn) end
end
end