-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrushbox.lua
executable file
·238 lines (173 loc) · 4.55 KB
/
brushbox.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
-- the box which has the focus (only one ) for text andbutton interactions
boxfocus=nil
hdlw=32
hdlh=32
local dfltzoom =1.0
--added to pass keyboard commands
bbzoomset=function(b,zoomfactor)
b.w=math.floor(b.w*zoomfactor)
b.h=math.floor(b.h*zoomfactor)
end
local function drag(b,tx,ty,dx,dy)
if b.mode=="drag" then
b.x=b.x+dx
b.y=b.y+dy
end
if b.mode=="resize" then
--TODO should behavior not be changed when
-- not keep aspect ratio?
if b.keepratio==true then
--WIP w and h should not be changed in this mode,
--just zoom
--we need to determine real dx and real dy
--when keeping ratio
local tsw= (b.w+dx)/b.w
local tsh= (b.h+dy)/b.h
--WIP distortion so far , probably loss of precision
--TODO calculate zoom and store zoom, let width and height of pic to display func
if tsw>=tsh then
b.w=math.floor(b.w*tsw)
b.h=math.floor(b.h*tsw)
else
b.w=math.floor(b.w*tsh)
b.h=math.floor(b.h*tsh)
end
b.tzoom=b.w/b.initw
print("zoom is "..b.tzoom)
else
--we change coordsm not zoom
b.w=b.w+dx
b.h=b.h+dy
end
end
end
--if consumed returns true
local function click(b,mx,my)
print("scan click of brushbox ")
if boxfocus==b then
if mx >= b.x and mx<b.x+b.w*b.tzoom and my >= b.y and my<b.y+b.h*b.tzoom then
print('drag')
b.mode="drag"
registerdrag=b
return true
end
if mx >= b.x-hdlw and mx<b.x and my >= b.y-hdlh and my<b.y then
b.mode="drag"
registerdrag=b
return true
end
end
if boxfocus==b then
if mx >= b.x+b.w*b.tzoom and mx<b.x+b.w*b.tzoom+hdlw and my >= b.y+b.h*b.tzoom and my<b.y+b.h*b.tzoom+hdlh then
b.mode="resize"
registerdrag=b
return true
end
end
--WIP resize click broken with zoom
if mx >= b.x and mx<b.x+b.w*b.tzoom and my >= b.y and my<b.y+b.h*b.tzoom then
-- print("click detected on id "..b.id)
--before taking focus let s check focus
boxfocus=b
return true
end
return false
end
local function editkey(b,key)
-- if key=='down' then
-- end
end
local function tbrender(b)
love.graphics.setColor(0.0,1.0,0.0,1.0)
if b==boxfocus then
love.graphics.setColor(1.0,0.0,0.0,1.0)
end
if renderdecos==true then
if b.texture~=nil then
if b.texquad~=nil then
--WIP are we going here ?
--not sure this is used ?
local xZoom=1.
if b.xFlip ==true then
--WIP never reached
xZoom=-xZoom
addMsg('bb xflip rdr '..xZoom)
end
--reached
addMsg('bb quad rdr')
love.graphics.draw(
b.texture,
b.texquad,
b.x,
b.y,
0,
xZoom*b.tzoom
-- *0.5
,
1*b.tzoom
-- *0.25
)
end
end
love.graphics.setLineWidth(3)
love.graphics.rectangle("fill",b.x-hdlw,b.y-hdlh,hdlw,hdlh)
love.graphics.rectangle("line",b.x,b.y,b.w*b.tzoom,b.h*b.tzoom)
love.graphics.rectangle("fill",b.x+b.w*b.tzoom,b.y+b.h*b.tzoom,hdlw,hdlh)
love.graphics.setColor(0.0,0.0,1.0,1.0)
love.graphics.setLineWidth(1)
--for justify center
love.graphics.line(b.x+b.w/2,b.y,b.x+b.w/2,b.y+b.h)
love.graphics.setColor(1.0,1.0,1.0,1.0)
-- love.graphics.print(b.id,b.x-hdlw,b.y-hdlh)
end
love.graphics.setColor(1.0,1.0,1.0,1.0)
end
--component has 2 modes, if a texture is passed,
--the selection is shown,
--then scaled scaled WIP
function bbsettexture(bb,tex,quad,xFlip)
addMsg('bb set tex xflip '..tostring(xFlip))
bb.texture=tex
--if optional quad is passed, the blit is on the quad portion
bb.texquad=quad
if xFlip~=nil then
bb.xFlip=xFLip
end
end
--brush box represents a bitmap, so when resizing we maintain its
-- zoom ratio
function createbrushbox(x,y,w,h,keepratio,xFlip)
ret={}
ret.type='tbox' --used to identify targets for snap
-- wcount=wcount+1
-- ret.id=wcount
--this is the display x y
ret.x=x
ret.y=y
--used for zoom calculation after resize
ret.initw=w --we dont change that ever
ret.w=w
ret.h=h
ret.keepratio=keepratio
ret.render=tbrender
ret.click=click
ret.drag=drag
ret.buffer={}
table.insert(ret.buffer,"")
ret.editkey=editkey
--current line
ret.line=1
ret.char=0
ret.tzoom=dfltzoom
ret.justif=jleft
if xFlip==true then
ret.xFLip=true
else
ret.xFLip=false
end
--ret.dragrelease=applysnap
-- ret.justif=jright
-- DBG for test
-- ret.texture=texture
return ret
end