-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbox.lua
executable file
·589 lines (446 loc) · 12.6 KB
/
tbox.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
snapmargin=64
--justification
jleft='l'
jright='r'
jcenter='c'
registerdrag=nil
-- the box which has the focus (only one ) for text andbutton interqctions
boxfocus=nil
hdlw=32
hdlh=32
--for text
local dfltzoom =0.3
local function applysnap(b)
if b.snap then
if b.snap.x then
b.x=b.snap.x
else
--left and right snap mutually exclusive
if b.snap.right then
b.x=b.snap.right-b.w
end
end
if b.snap.y then
b.y=b.snap.y
else
--left and right snap mutually exclusive
if b.snap.down then
b.y=b.snap.down-b.h
end
end
end
end
local function checkforsnap(b,w)
local pot=false
ret={}
if b.x < w.x+snapmargin and b.x>w.x-snapmargin and b.x~=w.x then
pot=true
ret.x=w.x
elseif b.x+b.w < w.x+w.w+snapmargin and b.x+b.w > w.x+w.w-snapmargin and b.x+b.w~=w.x+w.w then
pot=true
ret.right=w.x+w.w
end
if b.y < w.y+snapmargin and b.y>w.y-snapmargin and b.y~=w.y then
pot=true
ret.y=w.y
elseif b.y+b.h < w.y+w.h+snapmargin and b.y+b.h > w.y+w.h-snapmargin and b.x+b.w~=w.y+w.h then
pot=true
ret.down=w.y+w.h
end
if pot==true then
b.snap=ret
return true
else
b.snap=nil
end
end
-- each time we resize or move we check if there is a snap possibility within
-- margin , when mouse released, we use it if not nil
-- TODO if equals = already snapped, skip detected snap (false positive)
local function detectsnap(b)
for i,w in ipairs(widgets)
do
if w~=b and w.type=='tbox' then
found=checkforsnap(b,w)
if found==true then
break
end
end
end
end
local function drag(b,dx,dy)
if b.mode=="drag" then
b.x=b.x+dx
b.y=b.y+dy
end
if b.mode=="resize" then
b.w=b.w+dx
b.h=b.h+dy
maintainlpl(b)
end
detectsnap(b)
end
--if consumed returns true
local function click(b,mx,my)
print("scan click of id "..b.id)
if boxfocus==b then
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 and mx<b.x+b.w+hdlw and my >= b.y+b.h and my<b.y+b.h+hdlh then
b.mode="resize"
registerdrag=b
return true
end
end
if mx >= b.x and mx<b.x+b.w and my >= b.y and my<b.y+b.h then
print("click detected on id "..b.id)
--before taking focus let s check focus
if copywcb==true then
if boxfocus~=nil then
boxfocus.w=b.w
maintainlpl(boxfocus)
msg.postmsg(msg,"w copied")
copywcb=nil
return true
end
end
boxfocus=b
zoom.value=b.tzoom
return true
end
return false
end
local function addtext(b,txt)
b.buffer[b.line]=b.buffer[b.line]:sub(1,b.char)..txt..b.buffer[b.line]:sub(b.char+1)
-- b.char=b.char+string.len(txt)
b.char=b.char+1
print("char "..b.char)
-- b.char=string.len(b.buffer[b.line])
end
local function editkey(b,key)
if key=='return' then
--TODO
--split current buffer in 2 !
local cur = b.buffer[b.line]
first=cur:sub(1,b.char)
second=cur:sub(b.char+1)
b.buffer[b.line]=first
table.insert(b.buffer,b.line+1,second)
-- print('ed k')
-- table.insert(b.buffer,"")
b.line=b.line+1
b.char=0
print("char "..b.char)
end
if key=='backspace' then
if b.buffer[b.line]:len()==1 then
b.buffer[b.line]=""
b.char=1
elseif b.buffer[b.line]:len()==0 then
table.remove(b.buffer,b.line)
b.line=b.line-1
elseif b.line ==1 and b.char==0 then
--do nothing
return
elseif b.char==0 then -- begin of line, we need to merge buffer with previous
b.buffer[b.line-1]=b.buffer[b.line-1]..b.buffer[b.line]
table.remove(b.buffer,b.line)
b.line=b.line-1
else
-- b.buffer[b.line]=b.buffer[b.line]:sub(1, -2)
print("before "..b.buffer[b.line])
print("removing "..b.buffer[b.line]:sub(b.char,b.char))
nbeg=b.buffer[b.line]:sub(1,b.char-1)
print("nbeg "..nbeg)
nend=b.buffer[b.line]:sub(b.char+1)
print("nend "..nend)
b.buffer[b.line]=nbeg..nend
print("after "..b.buffer[b.line])
b.char=b.char-1
end
end
if key=='left' then
if b.char>0 then
b.char=b.char-1
print("char "..b.char)
msg.postmsg(msg,"char "..b.char)
end
end
if key=='right' then
if b.char<string.len(b.buffer[b.line]) then
b.char=b.char+1
msg.postmsg(msg,"char "..b.char)
end
end
if key=='up' then
print ("line before "..b.line)
if b.line>1 then
b.line=b.line-1
print ("line after "..b.line)
if b.char> string.len(b.buffer[b.line]) then
--end of line
b.char=string.len(b.buffer[b.line])
end
end
end
if key=='down' then
print ("line before "..b.line)
if b.line<tbllngth(b.buffer) then
b.line=b.line+1
print ("line after "..b.line)
if b.char> string.len(b.buffer[b.line]) then
--end of line
b.char=string.len(b.buffer[b.line])
end
end
end
end
-- function removeLastChar (string)
-- return string:sub(1, -2)
-- end
-- ONLY FROM LEFT
local function writeseg(seg,b,x,linetotal)
--there is an offset since we only want to draw "rtw"
-- local xof = (tw-rtw/2)
for i = 1, #(seg.val) do
local c = seg.val:sub(i,i)
if renderdecos==true then
--box of real img data
love.graphics.setColor(0.0,1.0,0.0,1.0)
love.graphics.rectangle("line",b.x+x,b.y+y,b.tzoom*tw,b.tzoom*th)
--box that we consider 'real'
love.graphics.setColor(1.0,0.0,0.0,1.0)
love.graphics.rectangle("line",b.x+x+((tw-rtw)*b.tzoom),b.y+y,b.tzoom*(tw-rtw),b.tzoom*th)
love.graphics.setColor(1.0,1.0,1.0,1.0)
end
if typo[c]~=nil then
love.graphics.draw(typo[c].pic,b.x+x,b.y+y,0,b.tzoom,b.tzoom)
elseif c==' ' then
else
love.graphics.draw(typo['unknown'].pic,b.x+x,b.y+y,0,b.tzoom,b.tzoom)
end
-- draw a cursor like square
if curline==b.line and linetotal==b.char and renderdecos==true then
love.graphics.setColor(1.0,1.0,1.0,0.2)
love.graphics.rectangle("fill",b.x+x+(tw-rtw)*b.tzoom/2,b.y+y,rtw*b.tzoom,th*b.tzoom)
love.graphics.setColor(1.0,1.0,1.0,1.0)
end
x=x+rtw*b.tzoom
linetotal=linetotal+1
end
return x,linetotal
end
--used for justif
function getrllength(b,rl)
local ret=0
for i,seg in ipairs(rl)
do
ret=ret+#(seg.val)
end
ret=ret*rtw*b.tzoom
return ret
end
local function justifiedtextrender(b)
y=0
curline=0
for i,l in ipairs(b.buffer)
do
curline=curline+1
--total of already written line on buffer line
local linetotal=0
--we get real lines
local rls=simplecprlfl(b.lpl,l)
if tbllngth(rls)==0 then
x=0
if renderdecos==true then
--we still need to blit cursor and active line ( this is an empty line )
love.graphics.polygon('fill',b.x+x,b.y+y+th*b.tzoom, b.x+x+rtw*b.tzoom,b.y+y+th*b.tzoom*0.75,b.x+x+rtw*b.tzoom,b.y+y+th*b.tzoom)
if curline == b.line then
love.graphics.line(b.x,b.y+y+th*b.tzoom,b.x+b.w,b.y+y+th*b.tzoom)
end
if curline==b.line then --edge case, we are on line begin
love.graphics.setColor(1.0,1.0,1.0,0.2)
love.graphics.rectangle("fill",b.x+x,b.y+y,rtw*b.tzoom,th*b.tzoom)
love.graphics.setColor(1.0,1.0,1.0,1.0)
end
end
--even if line empty of rls, we need to go down
y=y+th*b.tzoom
end
for j,rl in ipairs(rls)
do
--draw a line under current line
if curline == b.line and renderdecos==true then
love.graphics.line(b.x,b.y+y+th*b.tzoom,b.x+b.w,b.y+y+th*b.tzoom)
end
x=0
-- TODO calculation incorrect for more than one seg ?
--TODO for justify center calculate a different x begin
if (b.justif==jcenter)then
x=b.w/2-getrllength(b,rl)/2
end
-- TODO calculation incorrect for more than one seg ?
--TODO for justify right calculate x begin in offset from right border
if (b.justif==jright)then
x=b.w - getrllength(b,rl)
end
--we adjust first pos for real blit
--we need to start offset x to remove 'blank' on left of first pic
x=x-((tw-rtw)/2)*b.tzoom
for k,seg in ipairs(rl)
do
x,linetotal=writeseg(seg,b,x,linetotal)
end
if renderdecos==true then
if j==tbllngth(rls) then
--draw marker on real carriage return, all seg have been drawn
-- love.graphics.line(b.x+x+tw*b.tzoom,b.y+y,b.x+x+tw*b.tzoom,b.y+y+th*b.tzoom)
x=x+(tw-rtw)/2
love.graphics.polygon('fill',b.x+x,b.y+y+th*b.tzoom, b.x+x+rtw*b.tzoom,b.y+y+th*b.tzoom*0.75,b.x+x+rtw*b.tzoom,b.y+y+th*b.tzoom)
--edge case : need also to display cursor here (out of any seg)
if curline==b.line and linetotal==b.char then
love.graphics.setColor(1.0,1.0,1.0,0.2)
love.graphics.rectangle("fill",b.x+x,b.y+y,rtw*b.tzoom,th*b.tzoom)
love.graphics.setColor(1.0,1.0,1.0,1.0)
end
end
end
--real line finished ! automatic cr
x=0
-- y=y+th*b.tzoom
y=y+th*b.tzoom
end
end
end
local function simpletextrender(b)
y=0
curline=1
for i,l in ipairs(b.buffer)
do
x=0
--draw a line under current line
if curline == b.line then
love.graphics.line(b.x,b.y+y+th*b.tzoom,b.x+b.w,b.y+y+th*b.tzoom)
end
for i = 1, #l do
--TODO justification calculation : what is the line width,
--how many words do fit
--return segments of word {word} and render them
local c = l:sub(i,i)
-- do something with c
--draw a cursor like line
if curline==b.line and i==b.char then
love.graphics.line(b.x+x+tw*b.tzoom,b.y+y,b.x+x+tw*b.tzoom,b.y+y+th*b.tzoom)
end
if typo[c]~=nil then
love.graphics.draw(typo[c].pic,b.x+x,b.y+y,0,b.tzoom,b.tzoom)
elseif c==' ' then
else
love.graphics.draw(typo['unknown'].pic,b.x+x,b.y+y,0,b.tzoom,b.tzoom)
end
--draw marker on real carriage return
if i==string.len(l) then
love.graphics.line(b.x+x+tw*b.tzoom,b.y+y,b.x+x+tw*b.tzoom,b.y+y+th*b.tzoom)
end
-- prepare coords for next char in line
x=x+rtw*b.tzoom
-- if x>(b.w-(tw/2)*b.tzoom) then
if x>b.w-((tw/2)*b.tzoom) then
x=0
-- y=y+th*b.tzoom
y=y+th*b.tzoom
end
end
y=y+th*b.tzoom
curline=curline+1
-- love.graphics.print(l,b.x,b.y+b.fh*i)
-- love.graphics.print(x,y,l)
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
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.h)
love.graphics.rectangle("fill",b.x+b.w,b.y+b.h,hdlw,hdlh)
-- b.text.render(b.text,b.x,b.y)
love.graphics.setColor(0.0,0.0,1.0,1.0)
love.graphics.setLineWidth(1)
love.graphics.line(0,b.y,cvsw,b.y)
love.graphics.line(0,b.y+b.h,cvsw,b.y+b.h)
love.graphics.line(b.x,0,b.x,cvsh)
love.graphics.line(b.x+b.w,0,b.x+b.w,cvsh)
--for justify center
love.graphics.line(b.x+b.w/2,b.y,b.x+b.w/2,b.y+b.h)
--render potential snap
if b.snap then
love.graphics.setLineWidth(4)
if b.snap.x then
love.graphics.setColor(0.0,1.0,1.0,1.0)
love.graphics.line(b.snap.x,0,b.snap.x,cvsh)
end
if b.snap.right then
love.graphics.setColor(0.0,1.0,1.0,1.0)
love.graphics.line(b.snap.right,0,b.snap.right,cvsh)
end
if b.snap.y then
love.graphics.setColor(0.0,1.0,1.0,1.0)
love.graphics.line(0,b.snap.y,cvsw,b.snap.y)
end
if b.snap.down then
love.graphics.setColor(0.0,1.0,1.0,1.0)
love.graphics.line(0,b.snap.down,cvsw,b.snap.down)
end
love.graphics.setLineWidth(1)
end
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)
-- simpletextrender(b)
justifiedtextrender(b)
end
--after modifying zoom
-- prerequisite to justify text
function maintainlpl(b)
--calulate char per line
b.lpl=math.floor(b.w/(rtw*b.tzoom))
print("lpl "..b.lpl)
end
function createtbox(x,y,w,h)
ret={}
ret.type='tbox' --used to identify targets for snap
wcount=wcount+1
ret.id=wcount
ret.x=x
ret.y=y
ret.w=w
ret.h=h
ret.fh=16-- seems unused ?
ret.render=tbrender
ret.click=click
ret.drag=drag
ret.buffer={}
table.insert(ret.buffer,"")
ret.addtext=addtext
ret.editkey=editkey
--current line
ret.line=1
ret.char=0
ret.tzoom=dfltzoom
ret.justif=jleft
ret.snap=nil
ret.dragrelease=applysnap
-- ret.justif=jright
maintainlpl(ret)
return ret
end