-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathApus.Engine.NinePatch.pas
More file actions
776 lines (742 loc) · 21.1 KB
/
Copy pathApus.Engine.NinePatch.pas
File metadata and controls
776 lines (742 loc) · 21.1 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// Nine Patch library - compatible with Android 9-patches, but supports extended features such as overlapping
//
// Copyright (C) 2021 Ivan Polyacov, Apus Software (ivan@apus-software.com)
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
unit Apus.Engine.NinePatch;
interface
uses Apus.Engine.API;
type
// 9-Patches can be created from marked images as well as from raw images
// Marked images contain color codes on the 1px border:
// * Transparent - fixed part
// * Red - fixed part (overlapped)
// * Black - resizeable part (stretched)
// * Green - resizeable part (tiled)
TCustomNinePatch=class(TNinePatch)
constructor Create(fromImage:TTexture); // create a 9-patch from a marked image
// Create a 3x3 patch from a plain (unmarked) image by specifying the fixed
// corner margins directly. Intended for procedurally generated content where
// a 1px marker border would be pointless. The whole image is content; the
// middle band is stretched (tile=false) or tiled (tile=true).
constructor CreateMargins(content:TTexture;left,top,right,bottom:integer;tile:boolean=false);
procedure Draw(x,y,width,height:single;scale:single=1.0); override;
protected
meshWidth,meshHeight:single;
// Cached patch geometry: topology+UV built once, vertex positions re-fitted on resize.
// Submitted directly via draw.IndexedMesh — the patch itself is the abstraction, no mesh class needed.
vertices:array of TVertex;
indices:array of word;
vCount,iCount:integer; // number of vertices/indices currently written
geomBuilt:boolean; // whether geometry has been built for the current patch
tex:TTexture;
procedure BuildMeshForSize(width,height:single);
private
type
TRangeType=(
rtFixed, // fixed part (may overlap) - draw above others
rtStretched,
rtTiled);
TPatchRange=record
pFrom,pTo:integer;
overlap1,overlap2:byte;
rType:TRangeType;
end;
TGridArray=array[0..9] of single;
TIntGridArray=array[0..9] of integer;
var
overlapped,tiled:boolean;
hRanges,vRanges:array of TPatchRange;
hWeights,vWeights:array of single;
//padLeft,padTop,padRight,padBottom:integer;
usedCells:array of cardinal; // bitmap of cell status (1 - cell is empty)
numCells:integer; // total number of non-empty cells
procedure BuildRanges(tex:TTexture);
procedure CheckCells;
procedure CalcWeights;
procedure ClearBorder(tex:TTexture);
procedure CalcSizes;
procedure ResizeGrid(reqWidth,reqHeight:single;var xx,yy:TGridArray);
procedure ResetGeom(vCapacity,iCapacity:integer);
function AddVertex(const v:TVertex):integer;
procedure AddTrg(a,b,c:integer);
procedure BuildSimpleMesh(nH,nW:integer;du,dv:single);
procedure BuildOverlappedMesh(nH,nW:integer;du,dv:single);
procedure BuildTiledMesh(nH,nW:integer;du,dv:single;var xx,yy:TGridArray);
procedure FillCellMesh(x1,y1,x2,y2, u1,v1,u2,v2, tileWidth,tileHeight:single);
procedure AdjustSimpleMesh(nW,nH:Integer;var xx,yy:TGridArray);
procedure AdjustOverlappedMesh(nW,nH:Integer;var xx,yy:TGridArray);
procedure BuildTiledGrid(var xx,yy:TGridArray;var x1,y1,x2,y2:TGridArray;var gridW,gridH:TIntGridArray);
function PassForCell(row,col:integer):integer; // returns 0..2
end;
implementation
uses SysUtils, Apus.Core, Apus.FastGFX, Apus.Colors, Apus.Engine.ImageTools;
type
TPatchRange=TCustomNinePatch.TPatchRange;
// Returns:
// 0 - transparent
// 1 - red
// 2 - black
// 3 - green
function ColorKey(color:cardinal):byte;
var
cVal:cardinal;
c:TARGBColor absolute cVal;
begin
cVal:=color;
if c.a<128 then result:=0
else
if c.r>128 then result:=1
else
if c.g>128 then result:=3
else
result:=2;
end;
// Returns true if overlapped
function MakeRange(img:TTexture; x,y,dx,dy:integer; var range:TPatchRange):boolean;
var
key:byte;
overlap:byte;
begin
key:=ColorKey(GetPixel(x,y));
case key of
2:range.rType:=rtStretched;
3:range.rType:=rtTiled;
else
range.rType:=rtFixed;
end;
range.overlap1:=0;
range.overlap2:=0;
if key=1 then begin
range.overlap1:=1;
overlap:=1;
end else
overlap:=2;
if dx=1 then range.pFrom:=x
else range.pFrom:=y;
range.pTo:=range.pFrom;
while (x<img.width-2) and (y<img.height-2) do begin
x:=x+dx; y:=y+dy;
key:=ColorKey(GetPixel(x,y));
case range.rType of
rtTiled:if key<>3 then break;
rtStretched:if key<>2 then break;
rtFixed:begin
case key of
0:overlap:=2;
1:if overlap=1 then inc(range.overlap1)
else inc(range.overlap2);
2,3:break;
end;
end;
end;
inc(range.pTo);
end;
result:=range.overlap1+range.overlap2>0;
end;
{ TPatchInfo }
procedure TCustomNinePatch.BuildRanges(tex:TTexture);
var
x,y,i,n:integer;
begin
// Top line
x:=1; y:=0;
while x<tex.width-1 do begin
n:=length(hRanges);
SetLength(hRanges,n+1);
MakeRange(tex,x,y,1,0,hRanges[n]);
with hRanges[n] do begin
if rType=rtTiled then tiled:=true;
if overlap1+overlap2>0 then overlapped:=true;
x:=pTo+1;
end;
end;
// Left line
x:=0; y:=1;
while y<tex.height-1 do begin
n:=length(vRanges);
SetLength(vRanges,n+1);
MakeRange(tex,x,y,0,1,vRanges[n]);
with vRanges[n] do begin
if rType=rtTiled then tiled:=true;
if overlap1+overlap2>0 then overlapped:=true;
y:=pTo+1;
end;
end;
// Too many cells?
ASSERT((length(hRanges)<8) and (length(vRanges)<8));
end;
// Check cells content and mark non-transparent cells to draw (so full transparent cells will be skipped)
procedure TCustomNinePatch.CheckCells;
var
i,j,x,y:integer;
begin
// Mark empty cells
SetLength(usedCells,length(vRanges));
numCells:=0;
for i:=0 to high(usedCells) do begin
for j:=0 to high(hRanges) do begin
// try to find non-transparent pixels (every 4-th pixel is checked)
y:=vRanges[i].pFrom;
while y<=vRanges[i].pTo do begin
x:=hRanges[j].pFrom+(y and 1);
while x<=hRanges[j].pTo do begin
if GetPixel(x,y) shr 24>4 then begin // pixels with alpha below 4 are considered transparent
Bits.SetBit(usedCells[i],j);
inc(numCells);
inc(y,10000);
break;
end;
inc(x,2);
end;
inc(y,2);
end;
end;
end;
end;
procedure TCustomNinePatch.CalcWeights;
var
i,n:integer;
begin
SetLength(hWeights,length(hRanges));
n:=0;
for i:=0 to high(hRanges) do
with hRanges[i] do
if rType<>rtFixed then begin
inc(n,1+pTo-pFrom);
hWeights[i]:=1+pTo-pFrom;
end;
ASSERT(n>0);
for i:=0 to high(hWeights) do
hWeights[i]:=hWeights[i]/n;
SetLength(vWeights,length(vRanges));
n:=0;
for i:=0 to high(vRanges) do
with vRanges[i] do
if rType<>rtFixed then begin
inc(n,1+pTo-pFrom);
vWeights[i]:=1+pTo-pFrom;
end;
ASSERT(n>0);
for i:=0 to high(vWeights) do
vWeights[i]:=vWeights[i]/n;
end;
procedure TCustomNinePatch.ClearBorder;
var
x,y:integer;
c:cardinal;
begin
y:=tex.height-1;
for x:=1 to tex.width-2 do begin
// top line
c:=GetPixel(x,1);
c:=Color.SetAlpha(c,0);
PutPixel(x,0,c);
// bottom line
c:=GetPixel(x,y-1);
c:=Color.SetAlpha(c,0);
PutPixel(x,y,c);
end;
x:=tex.width-1;
for y:=1 to tex.height-2 do begin
// left line
c:=GetPixel(1,y);
c:=Color.SetAlpha(c,0);
PutPixel(0,y,c);
// right line
c:=GetPixel(x-1,y);
c:=Color.SetAlpha(c,0);
PutPixel(x,y,c);
end;
// Corner pixels
x:=tex.width-1;
y:=tex.height-1;
c:=Color.Mix(GetPixel(0,1),GetPixel(1,0),128);
PutPixel(0,0,c);
c:=Color.Mix(GetPixel(x,1),GetPixel(x-1,0),128);
PutPixel(x,0,c);
c:=Color.Mix(GetPixel(0,y-1),GetPixel(1,y),128);
PutPixel(0,y,c);
c:=Color.Mix(GetPixel(x,y-1),GetPixel(x-1,y),128);
PutPixel(x,y,c);
end;
procedure TCustomNinePatch.CalcSizes;
var
i,v:integer;
begin
// Base dimensions
baseWidth:=0;
for i:=0 to high(hRanges) do begin
with hRanges[i] do
v:=pTo-pFrom+1-overlap1-overlap2;
inc(baseWidth,v);
end;
baseHeight:=0;
for i:=0 to high(vRanges) do begin
with vRanges[i] do
v:=pTo-pFrom+1-overlap1-overlap2;
inc(baseHeight,v);
end;
// Min dimensions
minWidth:=0;
for i:=0 to high(hRanges) do
with hRanges[i] do
if rType=rtFixed then
inc(minWidth,1+pTo-pFrom);
minHeight:=0;
for i:=0 to high(vRanges) do
with vRanges[i] do
if rType=rtFixed then
inc(minHeight,1+pTo-pFrom);
end;
procedure TCustomNinePatch.ResizeGrid(reqWidth,reqHeight:single;var xx,yy:TGridArray);
var
i:integer;
addW,addH:single;
begin
addW:=reqWidth-baseWidth;
addH:=reqHeight-baseHeight;
xx[0]:=0;
for i:=0 to high(hRanges) do
with hRanges[i] do begin
xx[i+1]:=xx[i]+(1+pTo-pFrom);
if rType<>rtFixed then begin
xx[i+1]:=xx[i+1]+addW*hWeights[i];
// overlap?
if i>0 then
xx[i+1]:=xx[i+1]-hRanges[i-1].overlap2;
if i<high(hRanges) then
xx[i+1]:=xx[i+1]-hRanges[i+1].overlap1;
end;
end;
yy[0]:=0;
for i:=0 to high(vRanges) do
with vRanges[i] do begin
yy[i+1]:=yy[i]+(1+pTo-pFrom);
if rType<>rtFixed then begin
yy[i+1]:=yy[i+1]+addH*vWeights[i];
// Overlap?
if i>0 then
yy[i+1]:=yy[i+1]-vRanges[i-1].overlap2;
if i<high(vRanges) then
yy[i+1]:=yy[i+1]-vRanges[i+1].overlap1;
end;
end;
end;
procedure TCustomNinePatch.ResetGeom(vCapacity,iCapacity:integer);
begin
SetLength(vertices,vCapacity);
SetLength(indices,iCapacity);
vCount:=0;
iCount:=0;
end;
function TCustomNinePatch.AddVertex(const v:TVertex):integer;
begin
if vCount>=length(vertices) then SetLength(vertices,vCount+16);
result:=vCount;
vertices[vCount]:=v;
inc(vCount);
end;
procedure TCustomNinePatch.AddTrg(a,b,c:integer);
begin
if iCount+3>length(indices) then SetLength(indices,iCount+6);
indices[iCount]:=a; indices[iCount+1]:=b; indices[iCount+2]:=c;
inc(iCount,3);
end;
procedure TCustomNinePatch.BuildSimpleMesh(nH,nW:integer;du,dv:single);
var
i,j,base:integer;
vrt:TVertex;
u,v:single;
begin
ResetGeom((nW+1)*(nH+1),numCells*6);
// Fill vertices. The trailing edge UV uses the last range's (pTo+1) rather
// than a hardcoded 1-du: for marked images pTo=size-2 so it equals 1-du, but
// for markup-free patches (ranges span the whole texture) pTo=size-1 -> 1.0,
// so the last texel row/column is not cropped.
for i:=0 to nH do begin
if i<nH then v:=vRanges[i].pFrom*dv
else v:=(vRanges[nH-1].pTo+1)*dv;
for j:=0 to nW do begin
if j<nW then u:=hRanges[j].pFrom*du
else u:=(hRanges[nW-1].pTo+1)*du;
vrt.Init(0,0,0,u,v,$FF808080); // position will be filled later
AddVertex(vrt);
end;
end;
// Fill indices
for i:=0 to nH-1 do
for j:=0 to nW-1 do
if Bits.Get(usedCells[i],j) then begin
base:=i*(nW+1)+j; // base index
AddTrg(base,base+1,base+2+nW);
AddTrg(base,base+2+nW,base+1+nW);
end;
end;
procedure TCustomNinePatch.BuildOverlappedMesh(nH,nW:integer;du,dv:single);
var
i,j,base,pass:integer;
vrt:TVertex;
u1,v1,u2,v2:single;
begin
ResetGeom(numCells*4,numCells*6);
base:=0;
// Fill vertices
for pass:=0 to 2 do
for i:=0 to nH-1 do begin
v1:=vRanges[i].pFrom*dv;
v2:=vRanges[i].pTo*dv+dv;
for j:=0 to nW-1 do
if Bits.Get(usedCells[i],j) then begin
if pass<>PassForCell(i,j) then continue;
u1:=hRanges[j].pFrom*du;
u2:=hRanges[j].pTo*du+du;
// Define 4 vertices for a quad
vrt.Init(0,0,0,u1,v1,$FF808080); // position will be filled later
AddVertex(vrt);
vrt.Init(0,0,0,u2,v1,$FF808080);
AddVertex(vrt);
vrt.Init(0,0,0,u2,v2,$FF808080);
AddVertex(vrt);
vrt.Init(0,0,0,u1,v2,$FF808080);
AddVertex(vrt);
// 1-st triangle
AddTrg(base,base+1,base+2);
// 2-nd triangle
AddTrg(base,base+2,base+3);
inc(base,4);
end;
end;
end;
function CalcGridSize(size,tileSize:single):integer;
begin
size:=size-1.01;
result:=1+2*trunc(0.5+size/(tileSize*2));
end;
procedure TCustomNinePatch.FillCellMesh(x1,y1,x2,y2, u1,v1,u2,v2, tileWidth,tileHeight:single);
var
vrt:TVertex;
i,j,w,h,base:integer;
x,y,xx,yy,x0,y0,k:single;
tu1,tu2,tv1,tv2:single;
begin
// Base corner of the central tile
x0:=(x1+x2-tileWidth)/2;
y0:=(y1+y2-tileHeight)/2;
// Amount of tiles
w:=CalcGridSize(x2-x1,tileWidth);
h:=CalcGridSize(y2-y1,tileHeight);
x0:=x0-(w div 2)*tileWidth;
y0:=y0-(h div 2)*tileHeight;
for i:=0 to h-1 do
for j:=0 to w-1 do begin
// temp values
x:=x0+j*tileWidth;
y:=y0+i*tileHeight;
xx:=x+tileWidth;
yy:=y+tileHeight;
tu1:=u1; tv1:=v1;
tu2:=u2; tv2:=v2;
if x<x1 then begin
k:=(x1-x+0.5)/tileWidth;
tu1:=u1+(u2-u1)*k;
x:=x1;
end;
if xx>x2 then begin
k:=(xx-x2-0.5)/tileWidth;
tu2:=u2-(u2-u1)*k;
xx:=x2;
end;
if y<y1 then begin
k:=(y1-y+0.5)/tileHeight;
tv1:=v1+(v2-v1)*k;
y:=y1;
end;
if yy>y2 then begin
k:=(yy-y2-0.5)/tileHeight;
tv2:=v2-(v2-v1)*k;
yy:=y2;
end;
// Add quad
base:=vCount;
vrt.Init(x,y,0,tu1,tv1,$FF808080);
AddVertex(vrt);
vrt.Init(xx,y,0,tu2,tv1,$FF808080);
AddVertex(vrt);
vrt.Init(xx,yy,0,tu2,tv2,$FF808080);
AddVertex(vrt);
vrt.Init(x,yy,0,tu1,tv2,$FF808080);
AddVertex(vrt);
// Add triangles
AddTrg(base,base+1,base+2);
AddTrg(base,base+2,base+3);
end;
end;
procedure TCustomNinePatch.BuildTiledMesh(nH,nW:integer;du,dv:single;var xx,yy:TGridArray);
var
i,j,pass,tileCount:integer;
gridW,gridH:TIntGridArray;
x1,y1,x2,y2:TGridArray;
u1,v1,u2,v2:single;
tileWidth,tileHeight:single;
begin
BuildTiledGrid(xx,yy, x1,y1,x2,y2,gridW,gridH);
// Calculate mesh size
tileCount:=0;
for i:=0 to nH-1 do
for j:=0 to nW-1 do
if Bits.Get(usedCells[i],j) then begin
inc(tileCount,gridW[j]*gridH[i]);
end;
ResetGeom(tileCount*4,tileCount*6);
// Fill mesh
for pass:=0 to 2 do
for i:=0 to nH-1 do begin
// Fixed rows: a single quad covering the whole cell with edge-aligned UV
// (texels pFrom..pTo). Tiled/stretched rows: tile-centered UV so repeated
// tiles don't double-sample edge texels.
if vRanges[i].rType=rtFixed then begin
v1:=vRanges[i].pFrom*dv;
v2:=(vRanges[i].pTo+1)*dv;
tileHeight:=y2[i]-y1[i];
end else begin
v1:=vRanges[i].pFrom*dv+dv/2;
v2:=vRanges[i].pTo*dv+dv/2;
tileHeight:=vRanges[i].pTo-vRanges[i].pFrom;
end;
for j:=0 to nW-1 do
if Bits.Get(usedCells[i],j) then begin
if pass<>PassForCell(i,j) then continue;
if hRanges[j].rType=rtFixed then begin
u1:=hRanges[j].pFrom*du;
u2:=(hRanges[j].pTo+1)*du;
tileWidth:=x2[j]-x1[j];
end else begin
u1:=hRanges[j].pFrom*du+du/2;
u2:=hRanges[j].pTo*du+du/2;
tileWidth:=hRanges[j].pTo-hRanges[j].pFrom;
end;
FillCellMesh(x1[j],y1[i],x2[j],y2[i], u1,v1,u2,v2, tileWidth,tileHeight);
end;
end;
end;
function TCustomNinePatch.PassForCell(row,col:integer):integer;
begin
result:=0;
if vRanges[row].rType=rtFixed then inc(result);
if hRanges[col].rType=rtFixed then inc(result);
end;
// Simple 9-patch mesh
procedure TCustomNinePatch.AdjustSimpleMesh(nW,nH:Integer;var xx,yy:TGridArray);
var
i,j:Integer;
pVrt:PVertex;
begin
pVrt:=@vertices[0];
for i:=0 to nH do
for j:=0 to nW do
begin
pVrt.x:=xx[j];
pVrt.y:=yy[i];
inc(pVrt);
end;
end;
// Overlapped 9-patch
procedure TCustomNinePatch.AdjustOverlappedMesh(nW,nH:Integer;var xx,yy:TGridArray);
var
i,j,pass:integer;
pVrt:PVertex;
x1,y1,x2,y2:single;
begin
pVrt:=@vertices[0];
for pass:=0 to 2 do
for i:=0 to nH-1 do
for j:=0 to nW-1 do
if Bits.Get(usedCells[i],j) then
if pass=PassForCell(i,j) then begin
// Calculate cell position
x1:=xx[j];
if j>0 then x1:=x1-hRanges[j-1].overlap2;
x2:=xx[j+1];
if j<nW-1 then x2:=x2+hRanges[j+1].overlap1;
y1:=yy[i];
if i>0 then y1:=y1-vRanges[i-1].overlap2;
y2:=yy[i+1];
if i<nH-1 then y2:=y2+vRanges[i+1].overlap1;
// Update vertices
pVrt.x:=x1;
pVrt.y:=y1;
inc(pVrt);
pVrt.x:=x2;
pVrt.y:=y1;
inc(pVrt);
pVrt.x:=x2;
pVrt.y:=y2;
inc(pVrt);
pVrt.x:=x1;
pVrt.y:=y2;
inc(pVrt);
end;
end;
procedure TCustomNinePatch.BuildTiledGrid(var xx,yy:TGridArray;var x1,y1,x2,y2:TGridArray;var gridW,gridH:TIntGridArray);
var
i,nW,nH:Integer;
begin
nW:=length(hRanges);
nH:=length(vRanges);
// Build overlapped grid for tiled patch
for i:=0 to nW-1 do
begin
x1[i]:=xx[i];
x2[i]:=xx[i+1];
if hRanges[i].rType<>rtFixed then begin
if i>0 then
x1[i]:=x1[i]-hRanges[i-1].overlap2;
if i<nW-1 then
x2[i]:=x2[i]+hRanges[i+1].overlap1;
gridW[i]:=CalcGridSize(x2[i]-x1[i],hRanges[i].pTo-hRanges[i].pFrom);
end else
gridW[i]:=1;
end;
for i:=0 to nH-1 do
begin
y1[i]:=yy[i];
y2[i]:=yy[i+1];
if vRanges[i].rType<>rtFixed then
begin
if i>0 then
y1[i]:=y1[i]-vRanges[i-1].overlap2;
if i<nH-1 then
y2[i]:=y2[i]+vRanges[i+1].overlap1;
gridH[i]:=CalcGridSize(y2[i]-y1[i],vRanges[i].pTo-vRanges[i].pFrom);
end
else
gridH[i]:=1;
end;
end;
{ TNinePatch }
procedure TCustomNinePatch.BuildMeshForSize(width,height:single);
var
i,j,nH,nW:integer;
du,dv:single;
xx,yy:TGridArray;
begin
if tiled then geomBuilt:=false; // Always rebuild geometry for a complex tiled patch
nW:=length(hRanges);
nH:=length(vRanges);
if not geomBuilt then begin
du:=1/tex.width;
dv:=1/tex.height;
if not (overlapped or tiled) then begin
// Indexed mesh for a regular 9-patch (simple case)
BuildSimpleMesh(nH,nW,du,dv);
end else
if not tiled then begin
// Non-indexed mesh for an overlapped 9-patch
BuildOverlappedMesh(nH,nW,du,dv);
end else begin
// Tiled patch - complex case
// Such geometry is always created for a specific size and not adjusted later
ResizeGrid(width,height,xx,yy);
BuildTiledMesh(nH,nW,du,dv,xx,yy);
end;
geomBuilt:=true;
end;
// Adjust previously created mesh
if not tiled then begin
// Adjust patch grid
ResizeGrid(width,height,xx,yy);
// Adjust vertices
if not overlapped then
AdjustSimpleMesh(nW,nH,xx,yy)
else
AdjustOverlappedMesh(nW,nH,xx,yy);
end;
// Remember new mesh dimensions for reuse
meshWidth:=width; meshHeight:=height;
end;
constructor TCustomNinePatch.Create(fromImage: TTexture);
var
x,y,n:integer;
begin
ASSERT((fromImage.width>=3) and (fromImage.height>=3));
scaleFactor:=1;
tex:=fromImage;
name:=tex.name;
overlapped:=false;
tiled:=false;
EditImage(tex);
try
BuildRanges(tex); // determine range areas
// TODO: bottom-right lines
CheckCells;
CalcWeights;
ClearBorder(tex); // Clear border pixels
finally
tex.Unlock;
end;
CalcSizes;
end;
constructor TCustomNinePatch.CreateMargins(content:TTexture;left,top,right,bottom:integer;tile:boolean);
// Build the range list for one axis: [0..lo-1] fixed, middle resizeable, [size-hi..size-1] fixed
procedure BuildAxis(var ranges:array of TPatchRange;var count:integer;size,lo,hi:integer;midType:TRangeType);
procedure Emit(pFrom,pTo:integer;rType:TRangeType);
begin
ranges[count].pFrom:=pFrom;
ranges[count].pTo:=pTo;
ranges[count].rType:=rType;
ranges[count].overlap1:=0;
ranges[count].overlap2:=0;
inc(count);
end;
begin
count:=0;
if lo>0 then Emit(0,lo-1,rtFixed);
Emit(lo,size-1-hi,midType); // middle must be non-empty (asserted by caller)
if hi>0 then Emit(size-hi,size-1,rtFixed);
end;
var
midType:TRangeType;
nH,nV:integer;
begin
ASSERT((content.width>=3) and (content.height>=3));
ASSERT((left>=0) and (right>=0) and (top>=0) and (bottom>=0));
ASSERT(left+right<content.width,'Horizontal margins leave no resizeable middle');
ASSERT(top+bottom<content.height,'Vertical margins leave no resizeable middle');
scaleFactor:=1;
tex:=content;
name:=tex.name;
overlapped:=false;
tiled:=tile;
if tile then midType:=rtTiled else midType:=rtStretched;
// ranges span the whole texture (no marker border, nothing to clear)
SetLength(hRanges,3);
SetLength(vRanges,3);
BuildAxis(hRanges,nH,content.width,left,right,midType);
BuildAxis(vRanges,nV,content.height,top,bottom,midType);
SetLength(hRanges,nH);
SetLength(vRanges,nV);
EditImage(tex);
try
CheckCells; // mark fully transparent cells to be skipped
CalcWeights;
finally
tex.Unlock;
end;
CalcSizes;
end;
procedure TCustomNinePatch.Draw(x,y,width,height:single;scale:single);
var
rWidth,rHeight:single;
begin
scale:=scale*scaleFactor;
rWidth:=width/scale;
rHeight:=height/scale;
if (meshWidth<>rWidth) or (meshHeight<>rHeight) then
BuildMeshForSize(rWidth,rHeight);
transform.SetObj(x-0.5,y-0.5,0,scale);
if vCount>0 then // qualified: bare 'draw' would resolve to this Draw method (case-insensitive)
Apus.Engine.API.draw.IndexedMesh(@vertices[0],TVertex.layoutTex,@indices[0],iCount div 3,vCount,tex);
transform.ResetObj;
end;
end.