-
Notifications
You must be signed in to change notification settings - Fork 0
/
eraser.lua
executable file
·132 lines (102 loc) · 2.61 KB
/
eraser.lua
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'rect.lua'
require 'assets.lua'
require 'image.lua'
local g = love.graphics
local m = love.mouse
local a = love.audio
Eraser = class('Eraser')
function Eraser:initialize(position, size)
self.rect = Rect:new(position, size, 200,200,200,255)
self.pos = position
local img = Assets.LoadImage('texture01.png')
self.image = Image:new(img, 7, 431, 122, 91)
-- self.prevPos = position
-- self.prevDir = Vector:new(0,0)
-- self.dir = Vector:new(0,0)
self.erasing = false
self.p1 = nil
self.p2 = nil
eraserSound = eraserSound or a.newSource('assets/sounds/rubber.wav')
eraserSound:setVolume(0.3)
end
function Eraser:setPosition(position)
self.rect.position = position
self.pos = position
--[[
if self.pos:distance(position) > 0 then
self.prevPos = self.pos
self.pos = position
self.prevDir = self.dir
self.dir = (self.pos - self.prevPos):normalize()
end
]]--
end
function Eraser:update(dt)
local mouseDown = m.isDown('l') or m.isDown('r')
if mouseDown and not self.erasing then
self.p1 = self.pos
self.erasing = true
eraserSound:stop()
eraserSound:play()
elseif not mouseDown and self.erasing then
self.p2 = self.pos
self.erasing = false
end
--[[
if mouseDown then
if not self.erasing then
self.points = {}
self.points[1] = self.pos
self.points.index = 1
self.erasing = true
else
if (self.dir.y < 0 and self.prevDir.y > 0) or (self.dir.y > 0 and self.prevDir.y < 0) then
self.points[self.points.index+1] = self.pos
self.points.index = self.points.index+1
end
end
else
if self.erasing then
self.points[self.points.index+1] = self.pos
self.points.index = self.points.index+1
self.erasing = false
end
end
]]--
end
function Eraser:hasCutLine()
return self.p1 and self.p2
end
function Eraser:getAndClearCutLine()
local p1 = self.p1
local p2 = self.p2
assert(p1)
assert(p2)
self.p1 = nil
self.p2 = nil
self.cp1 = p1
self.cp2 = p2
-- eraserSound:reset()
return Line:new(p1, p2)
end
function Eraser:draw()
g.setColor(0,0,0,255)
-- self.rect:draw()
-- g.rectangle("fill", self.rect.position.x - self.rect.size.x, self.rect.position.y - self.rect.size.y, self.rect.size.x, self.rect.size.y)
self.image:draw(self.pos.x + 40, self.pos.y - 40)
-- g.setColor(255,0,0,255)
-- g.circle("fill", self.pos.x, self.pos.y, 4, 32)
if self.cp1 and self.cp2 then
g.setColor(255,0,0,255)
-- g.line(self.cp1.x, self.cp1.y, self.cp2.x, self.cp2.y)
end
if self.points then
local p = self.points[1]
g.setColor(255,0,0,255)
for i = 2, self.points.index-1 do
local q = self.points[i]
g.line(p.x, p.y, q.x, q.y)
p = q
end
end
end