-
Notifications
You must be signed in to change notification settings - Fork 17
/
regiondrag.lua
180 lines (156 loc) · 4.75 KB
/
regiondrag.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
regiondrag = class("regiondrag")
regiondragboxwidth = 3
function regiondrag:init(x, y, width, height)
self.x = x-1
self.y = y-1
self.width = width or 1
self.height = height or 1
self.grabbed = 0
end
function regiondrag:update(dt)
if self.grabbed ~= 0 then
local mx, my = mouse.getPosition()
local oldx, oldy, oldwidth, oldheight, oldgrabbed = self.x, self.y, self.width, self.height, self.grabbed
local tx, ty = getMouseTile(mx-.5*16*scale, my)
--X
if self.grabbed == 1 or self.grabbed == 3 then
if tx < self.x then
self.width = self.x-tx+self.width
self.x = tx
elseif tx > self.x then
if tx > self.x+self.width then
self.grabbed = self.grabbed + 1
self.x = self.x+self.width
self.width = tx-self.x
else
self.width = self.x-tx+self.width
self.x = tx
end
end
elseif self.grabbed == 2 or self.grabbed == 4 then
if tx > self.x+self.width then
self.width = tx-self.x
elseif tx < self.x+self.width then
if tx <= self.x then
self.grabbed = self.grabbed - 1
self.width = self.x-tx
self.x = tx
else
self.width = tx-self.x
end
end
end
--Y
if self.grabbed == 1 or self.grabbed == 2 then
if ty < self.y then
self.height = self.y-ty+self.height
self.y = ty
elseif ty > self.y then
if ty > self.y+self.height then
self.grabbed = self.grabbed + 2
self.y = self.y+self.height
self.height = ty-self.y
else
self.height = self.y-ty+self.height
self.y = ty
end
end
elseif self.grabbed == 3 or self.grabbed == 4 then
if ty > self.y+self.height then
self.height = ty-self.y
elseif ty < self.y+self.height then
if ty <= self.y then
self.grabbed = self.grabbed - 2
self.height = self.y-ty
self.y = ty
else
self.height = ty-self.y
end
end
end
if self.grabbed == 5 then
self.x, self.y = self.movestartx+round((mx-self.movex)/16/scale), self.movestarty+round((my-self.movey)/16/scale)
end
if self.width == 0 or self.height == 0 then
self.x, self.y, self.width, self.height, self.grabbed = oldx, oldy, oldwidth, oldheight, oldgrabbed
end
end
end
function regiondrag:draw()
local high = 0
if self:inhighlight(mouse.getX(), mouse.getY(), 5) and self.grabbed == 0 then
high = 5
end
for i = 1, 4 do
if self:inhighlight(mouse.getX(), mouse.getY(), i) then
high = i
break
end
end
if high == 5 or self.grabbed == 5 then
love.graphics.setColor(1, 1, 1, 0.4)
else
love.graphics.setColor(1, 0.5, 0.15, 0.4)
end
love.graphics.rectangle("fill", (self.x-xscroll)*16*scale, (self.y-yscroll-.5)*16*scale, self.width*16*scale, self.height*16*scale)
--edges
for i = 1, 4 do
local x, y
if i == self.grabbed or (self.grabbed == 0 and high == i) then
love.graphics.setColor(1, 1, 1, 0.8)
else
love.graphics.setColor(0.87, 0.4, 0.11, 0.8)
end
if i == 1 then
x = (self.x-xscroll)*16*scale
y = (self.y-yscroll-.5)*16*scale
elseif i == 2 then
x = (self.x-xscroll+self.width)*16*scale
y = (self.y-yscroll-.5)*16*scale
elseif i == 3 then
x = (self.x-xscroll)*16*scale
y = (self.y-yscroll-.5+self.height)*16*scale
elseif i == 4 then
x = (self.x-xscroll+self.width)*16*scale
y = (self.y-yscroll-.5+self.height)*16*scale
end
love.graphics.rectangle("fill", x-regiondragboxwidth*scale, y-regiondragboxwidth*scale, regiondragboxwidth*2*scale, regiondragboxwidth*2*scale)
end
end
function regiondrag:mousepressed(x, y, button)
for i = 1, 5 do
if self:inhighlight(mouse.getX(), mouse.getY(), i) then
self.grabbed = i
break
end
end
if self.grabbed == 5 then
self.movex = x
self.movey = y
self.movestartx = self.x
self.movestarty = self.y
elseif self.grabbed == 0 then
return true
end
end
function regiondrag:mousereleased(x, y, button)
self.grabbed = 0
end
function regiondrag:inhighlight(x, y, i)
if i == 1 then
vx = (self.x-xscroll)*16*scale
vy = (self.y-yscroll-.5)*16*scale
elseif i == 2 then
vx = (self.x-xscroll+self.width)*16*scale
vy = (self.y-yscroll-.5)*16*scale
elseif i == 3 then
vx = (self.x-xscroll)*16*scale
vy = (self.y-yscroll-.5+self.height)*16*scale
elseif i == 4 then
vx = (self.x-xscroll+self.width)*16*scale
vy = (self.y-yscroll-.5+self.height)*16*scale
elseif i == 5 then
return x >= (self.x-xscroll)*16*scale and x < (self.x-xscroll+self.width)*16*scale and y >= (self.y-yscroll-.5)*16*scale and y < (self.y-yscroll-.5+self.height)*16*scale
end
return x >= vx-regiondragboxwidth*scale and x < vx+regiondragboxwidth*scale and y >= vy-regiondragboxwidth*scale and y < vy+regiondragboxwidth*scale
end