-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathApus.Engine.UIWidgets.pas
More file actions
1925 lines (1728 loc) · 57.5 KB
/
Apus.Engine.UIWidgets.pas
File metadata and controls
1925 lines (1728 loc) · 57.5 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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------
// Standard widget classes
//
// Author: 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.UIWidgets;
interface
uses Apus.Core, Apus.Lib, Apus.Engine.API, Apus.Engine.Types, Apus.Engine.UITypes,
Apus.Engine.UIShapes;
{$WRITEABLECONST ON}
{$IFDEF CPUARM} {$R-} {$ENDIF}
const
// Window defaults (writable constants, can be changed)
wcFrameBorder:integer=5; // window frame border width
wcTitleHeight:integer=24; // window title bar height
// Window area flags
wcLeftFrame = 1;
wcTopFrame = 2;
wcRightFrame = 4;
wcBottomFrame = 8;
wcHeader = 16; // area that can be used to drag and move the window
wcClient = 32; // client part of the window
// TODO: size constraints (minWidth/maxWidth etc.) to be added to TUIElement directly
type
// Horizontal or vertical separator bar that spans the full parent width (CreateH)
// or height (CreateV). The element has two visual zones:
// outer — transparent area defined by padding (marginH, marginV);
// inner — colored client area, color set via style key 'inner-fill'.
// Pass color=0 to leave the inner area transparent (spacer mode).
// canResize: intended for drag-to-resize neighboring elements (not yet implemented).
TUISplitter=class(TUIElement)
canResize:boolean; // true - allow resizing neighbour elements
// Horizontal bar: spans full parent width, height is the only meaningful dimension.
// Use .SetPaddings(marginH,marginV,marginH,marginV) for spacing around inner area.
constructor CreateH(height:single;parent:TUIElement;color:cardinal=0;name:String8='');
// Vertical bar: spans full parent height, width is the only meaningful dimension.
constructor CreateV(width:single;parent:TUIElement;color:cardinal=0;name:String8='');
end;
// Static image or custom-rendered area. src defines what to draw:
// 'file:name' — load from file
// 'event:name' — fire event to let external code draw
// 'proc:XXXXXXXX' — call render procedure by pointer (see SetRenderProc)
// '' — nothing drawn (use as transparent container)
// shape defaults to shapeEmpty so mouse events pass through by default.
// Note: file/event src variants will likely move to R-05 style attributes;
// the proc variant (SetRenderProc) is the unique feature of this class.
// Also serves as base class for TUIHint and TUIWindow.
TUIImage=class(TUIScrollable)
src:String8;
constructor Create(width,height:single;parent:TUIElement;name:String8='';source:String8='');
procedure SetRenderProc(proc:pointer); // shorthand for src:='proc:...'
end;
// Touch-screen scroll overlay: placed over another element to capture drag events and scroll it.
// Captures mouse drag events, but passes clicks through.
TUIScrollArea=class(TUIElement)
fullWidth,fullHeight:single; // full content area dimensions
direction:TUIScrollDirection;
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(fullW,fullH:single;dir:TUIScrollDirection):TUIScrollArea;
procedure onMouseMove; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onTimer; override;
protected
speedX,speedY:single;
lastTime:cardinal;
isHooked:boolean;
end;
// Tooltip popup. Usually created partially filled; creator or drawer may complete it.
TUIHint=class(TUIImage)
simpleText:String8; // plain caption text
active:boolean; // active hint: not cached, may contain child elements
created:int64; // creation timestamp in ms
adjusted:boolean; // drawer may set this after adjusting hint parameters
hiding:boolean; // hint is currently hiding
constructor Create(x,y:single;text:String8;parent_:TUIElement);
destructor Destroy; override;
procedure Hide;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onTimer; override;
end;
// Read-only text element with configurable alignment.
// autoSize lets the drawer shrink/grow the element to fit the caption text.
TUILabel=class(TUIElement)
align:TTextAlignment;
autoSize:boolean; // render should adjust element size to match caption
verticalOffset:integer; // vertical text shift (positive = up)
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(text:String8):TUILabel; // left-aligned
function Centered(text:String8):TUILabel; // centered
function Right(text:String8):TUILabel; // right-aligned
procedure CaptionWidthIs(width:single);
end;
// Push button: fires onClick/onClickAsync once on click (not latching).
// Use onClick for inline UI updates (runs on render thread).
// Use onClickAsync for slow work (runs in a new thread).
TUIButton=class(TUIElement)
default:boolean; // default button (affects rendering only)
pressed:boolean; // transient: true while mouse is held down
pending:boolean; // temporarily unavailable (ignores input)
autoPendingTime:integer; // ms to stay pending after click (0 = disabled)
onClick:TProcedure; // called inline on render thread (use for UI updates)
onClickAsync:TProcedure; // called in a new thread (use for slow work)
onClickEvent:String8;
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(caption:String8):TUIButton;
destructor Destroy; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onMouseMove; override;
function onKey(keycode:byte;pressed:boolean;shiftstate:byte):boolean; override;
function onHotKey(keycode:byte;shiftstate:byte):boolean; override;
procedure onTimer; override;
procedure SetPressed(pr:boolean); virtual;
procedure Click; virtual; // simulate click
class function Sender:TUIButton;
protected
procedure DoClick; virtual;
private
lastPressed,pendingUntil:int64;
lastOver:boolean; // was under mouse when onMouseMove was called last time
end;
// Toggle button: latches in toggled state on click.
// Radio-group behavior: if parent is TUIGroupBox, clicking untoggles
// all sibling TUIToggleButtons (only one active at a time).
// If parent is not TUIGroupBox, toggles independently.
// Renders like TUIButton by default; appearance can differ per style (e.g. iOS switch).
TUIToggleButton=class(TUIButton)
toggled:boolean; // persistent latch state (true = on)
linkedToggled:PBoolean; // optional external bool synced with toggled
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(caption:String8;toggled:boolean=false):TUIToggleButton;
procedure SetToggled(v:boolean); virtual;
class function GetSwitchIndex(parent:TUIElement):integer;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onMouseMove; override;
function onHotKey(keycode:byte;shiftstate:byte):boolean; override;
procedure SetPressed(pr:boolean); override;
protected
procedure DoClick; override;
end;
// Checkbox: a TUIToggleButton with checkbox visual rendering.
// checked is an alias for toggled.
TUICheckBox=class(TUIToggleButton)
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(caption:String8;checked:boolean=false):TUICheckBox;
private
function GetChecked:boolean;
procedure SetChecked(v:boolean);
public
property checked:boolean read GetChecked write SetChecked;
end;
// Radio button: checkbox in a group (group=1); exactly one sibling is checked at a time.
TUIRadioButton=class(TUICheckBox)
constructor Create(width,height:single;parent:TUIElement;name:String8='');
function Setup(caption:String8;checked:boolean=false):TUIRadioButton;
end;
// Decorative frame / panel border
TUIFrame=class(TUIElement)
constructor Create(width,height:single;parent_:TUIElement;depth:integer=1;style_:integer=0);
procedure SetBorderWidth(w:integer); virtual;
protected
borderWidth:integer; // frame border width in pixels
end;
// Basic window: moveable and optionally resizeable.
// When background<>nil the window operates in "skinned" mode:
// - dragRegion defines the moveable area (nil = entire window)
// - GetAreaType uses dragRegion for hit-testing instead of standard frame logic
// - rendering of background is handled externally (e.g. by CustomStyle)
TUIWindow=class(TUIImage)
header:integer; // title bar height
autoBringToFront:boolean; // bring to front on click (self or any child)
moveable:boolean; // user can drag to move
resizeable:boolean; // user can drag edges to resize
minW,minH,maxW,maxH:integer; // size constraints for resizeable windows
dragRegion:TUIShape; // skinned mode: drag area shape (nil = whole window)
background:pointer; // skinned mode: opaque ptr to background image
constructor Create(innerWidth,innerHeight:single;sizeable:boolean;parent_:TUIElement;wndName:String8='';wndCaption:String8='');
destructor Destroy; override;
// Returns area flags (wcXxx) and cursor for the given screen point.
function GetAreaType(x,y:integer;out cur:NativeInt):integer; virtual;
procedure onMouseMove; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onLostFocus; override;
procedure Resize(newWidth,newHeight:single); override;
class function IsWindow:boolean; override;
private
hooked:boolean;
area:integer; // area type under cursor (wcXxx flags)
end;
// Single-line text input. Supports Unicode, mouse selection, password masking,
// auto-complete (completion), placeholder (defaultText), and horizontal scroll.
TUIEditBox=class(TUIElement)
realText:String32; // real text value of the edit box
completion:String32; // grayed background text, if it is not empty and enter is pressed, then it is set to realText
defaultText:String32; // grayed background text, displayed if realText is empty
cursorPos:integer; // caret position in String32 (0..length, 0-based)
maxLength:integer; // max allowed length
password:boolean; // if true, all characters are displayed as '*'
selStart,selCount:integer; //
cursorTimer:int64; // time offset for cursor blinking
needPos:integer; // pixel position feedback from the drawer
msSelect:boolean; // mouse selection mode is ON
protection:byte; // xor all characters with this value
offset:integer; // shift text right by this number of pixels
constructor Create(width,height:single;parent_:TUIElement;name:String8='');
procedure onChar(ch:char;scancode:byte); override;
procedure onUniChar(ch:Char32;scancode:byte); override;
function onKey(keycode:byte;pressed:boolean;shiftstate:byte):boolean; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onMouseMove; override;
procedure SetFocus; override;
procedure onLostFocus; override;
procedure SelectAll; virtual;
private
savedText:String32;
lastClickTime:int64;
msSelStart:integer; // mouse selection anchor (0-based), -1 if not set
procedure AdjustState;
function GetText:String8;
procedure SetText(s:String8);
public
property text:String8 read GetText write SetText; // Current value in UTF-8 encoding
end;
// Horizontal or vertical scrollbar with configurable range, page size, and step.
// Link to a TUIScrollable element via Link() to control its scroll position.
// Supports smooth animation and optional auto-hide when content fits.
TUIScrollBar=class(TUIElement)
private
rValue:TAnimatedValue;
sliderRect:TRect;
function GetValue:single;
procedure SetValue(v:single);
function GetAnimating:boolean;
procedure SetPageSize(pageSize:single);
function GetStep:single;
procedure CheckAutoHide;
public
horizontal:boolean; // orientation
isInteger:boolean; // should value be always integer
min,max:single; // range
pagesize:single; // slider size (within range)
step:single; // add/subtract this amount with mouse scroll or similar events
sliderUnder:boolean; // mouse is over slider
sliderStart,sliderEnd:single; // relative position of slider (in 0..1 range)
autoHide:boolean; // hide if pagesize>=range
constructor Create(width,height:single;parent_:TUIElement;barName:String8=''); overload; // orientation from size ratio (fragile, prefer CreateH/CreateV)
constructor CreateH(width,height:single;parent_:TUIElement;barName:String8=''); // explicit horizontal
constructor CreateV(width,height:single;parent_:TUIElement;barName:String8=''); // explicit vertical
function GetScroller:IScroller;
function SetRange(newMin,newMax,newPageSize:single):TUIScrollBar;
procedure MoveTo(val:single;smooth:boolean=false); virtual;
procedure MoveRel(delta:single;smooth:boolean=false); virtual;
procedure Link(elem:TUIScrollable); virtual; // link to a scrollable element
procedure UseButtons(lessBtn,moreBtn:String8); // use button signals to move the scrollbar
procedure CalcSliderPos(minSize:single=0.5); // minimal slider size (relative to width)
procedure onTimer; override;
procedure onMouseMove; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onMouseScroll(value:integer); override;
procedure onLostFocus; override;
property value:single read GetValue write SetValue;
property isAnimating:boolean read GetAnimating;
protected
linkedControl:TUIScrollable;
delta:integer; // cursor offset from slider start (when hooked)
moving:boolean;
scroller:TObject;
end;
// Scrollable list of text items with single selection.
// Each item may carry a tag (cardinal) and a per-item hint string.
// Supports hover highlight and optional auto-select on hover.
TUIListBox=class(TUIScrollable)
lines:Strings8;
tags:array of cardinal;
hints:Strings8; // each element may have its own hint
lineHeight:single; // in self CS
selectedLine:integer; // index of selected line (or -1)
hoverLine:integer; // index of line under mouse (or -1)
autoSelectMode:boolean; // when true, hover line is automatically selected
bgColor,bgHoverColor,bgSelColor,textColor,hoverTextColor,selTextColor:cardinal; // rendering colors (R-05: move to style)
constructor Create(width,height:single;parent:TUIElement;listName:String8='';lHeight:single=0);
destructor Destroy; override;
procedure AddLine(line:String8;tag:cardinal=0;hint:String8=''); virtual;
procedure SetLine(index:integer;line:String8;tag:cardinal=0;hint:String8=''); virtual;
procedure ClearLines;
procedure SetLines(newLines:Strings8); virtual;
procedure SelectLine(line:integer); virtual;
procedure onMouseMove; override;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure UpdateScroller;
end;
// Drop-down selector: visually a button, opens a popup TUIListBox on click.
// Tracks current item by index (curItem), tag (curTag), and text (text property).
TUIComboBox=class(TUIButton)
items,hints:Strings8;
tags:IntArray;
defaultText:String8;
fCurItem,fCurTag:integer;
// pop up elements
frame:TUIFrame;
popup:TUIListBox;
maxlines:integer; // max lines to show without scrolling
constructor Create(width,height:single;parent_:TUIElement;name:String8='';list:Strings8=nil);
procedure AddItem(item:String8;tag:cardinal=0;hint:String8=''); virtual;
procedure SetItem(index:integer;item:String8;tag:cardinal=0;hint:String8=''); virtual;
procedure ClearItems;
procedure onDropDown; virtual;
procedure onMouseButtons(button:byte;state:boolean); override;
procedure onTimer; override; // tracks popup list state without using signals
procedure SetCurItem(item:integer); virtual;
procedure SetCurItemByText(value:String8); virtual;
procedure SetCurItemByTag(tag:integer); virtual;
protected
function GetText:String8;
public
property curItem:integer read fCurItem write SetCurItem;
property curTag:integer read fCurTag write SetCurItemByTag;
property text:String8 read GetText;
end;
implementation
uses SysUtils, Types, Apus.Types, Apus.Utils, Apus.EventMan, Apus.Geom2D, Apus.Clipboard,
Apus.Strings, Apus.Threads, Apus.Engine.UIRender;
type
TScrollBarInterface=class(TInterfacedObject, IScroller)
owner:TUIScrollBar;
constructor Create(owner:TUIScrollbar);
function GetElement:TUIElement;
procedure SetRange(min,max:single);
procedure SetValue(v:single);
procedure SetStep(step:single);
procedure SetPageSize(pageSize:single);
procedure MoveRel(delta:single;smooth:boolean);
function GetValue:single;
function GetStep:single;
function GetPageSize:single;
end;
var
comboPop:TUIComboBox; // если существует выпавший комбобокс (а он может быть только один) - он тут
{ TUISpacer }
constructor TUISplitter.CreateH(height:single;parent:TUIElement;color:cardinal;name:String8);
begin
inherited Create(FILL_PARENT,height,parent,name);
if color<>0 then
SetStyle('inner-fill',IntToHex(color,8));
end;
constructor TUISplitter.CreateV(width:single;parent:TUIElement;color:cardinal;name:String8);
begin
inherited Create(width,FILL_PARENT,parent,name);
if color<>0 then
SetStyle('inner-fill',IntToHex(color,8));
end;
{ TUIimage }
constructor TUIImage.Create(width,height:single;parent:TUIElement;name:String8;source:String8);
begin
inherited Create(width,height,parent,name);
src:=source;
shape:=shapeEmpty;
end;
procedure TUIImage.SetRenderProc(proc:pointer);
begin
src:='proc:'+Conv.ToHex(UIntPtr(proc));
end;
{ TUIButton }
procedure TUIButton.Click;
begin
onMouseButtons(1,true);
onMouseButtons(1,false);
end;
constructor TUIButton.Create(width,height:single;parent:TUIElement;name:String8);
begin
inherited Create(width,height,parent,name);
shape:=shapeFull;
flags.canHaveFocus:=true;
sendSignals:=ssMajor;
end;
function TUIButton.Setup(caption:String8):TUIButton;
begin
self.caption:=caption;
result:=self;
end;
destructor TUIButton.Destroy;
begin
inherited;
end;
procedure TUIButton.DoClick;
begin
TUIElement.sender:=self;
if pending then exit;
if (sendSignals<>ssNone) and (CoreTime.Ticks>lastPressed+50) then begin
Signal('UI\'+name+'\OnClick',byte(pressed));
Signal('UI\Button\Click\'+name,TTag(self));
if Assigned(onClick) then onClick;
if Assigned(onClickAsync) then Thread.Start('UIClick:'+String8(name),TThreadProc(onClickAsync));
if onClickEvent<>'' then Signal(onClickEvent,TTag(self));
lastPressed:=CoreTime.Ticks;
end;
end;
function TUIButton.onHotKey(keycode,shiftstate:byte):boolean;
begin
result:=false;
if not flags.enabled then exit;
SetPressed(true);
DoClick;
SetPressed(false);
timer:=150;
result:=true;
end;
function TUIButton.onKey(keycode:byte;pressed:boolean;shiftstate:byte):boolean;
begin
result:=inherited onKey(keycode,pressed,shiftstate);
if pressed and (TKey(keycode) in [TKey.Enter,TKey.Space]) then begin // Enter
onHotKey(keycode,shiftstate);
result:=false;
end;
end;
procedure TUIButton.onMouseButtons(button:byte;state:boolean);
begin
if not flags.enabled then begin
Signal('UI\'+name+'\ClickDisabled',button);
exit;
end;
if button=1 then begin
if not pressed and state then SetPressed(true);
if pressed and not state then begin
DoClick;
SetPressed(false);
end;
end;
inherited;
end;
procedure TUIButton.onMouseMove;
begin
inherited;
if not lastover and (undermouse=self) then
Signal('UI\Button\Over\'+name);
if lastover and (undermouse<>self) then
Signal('UI\Button\Out\'+name);
if pressed and (underMouse<>self) then
SetPressed(false);
lastover:=undermouse=self;
end;
procedure TUIButton.onTimer;
begin
SetPressed(false);
end;
class function TUIButton.Sender:TUIButton;
begin
result:=TUIElement.sender as TUIButton;
end;
procedure TUIButton.SetPressed(pr:boolean);
begin
pressed:=pr;
if sendSignals<>ssNone then begin
if pr then Signal('UI\Button\Down\'+name,UIntPtr(self))
else Signal('UI\Button\Up\'+name,UIntPtr(self));
end;
end;
{ TUIToggleButton }
constructor TUIToggleButton.Create(width,height:single;parent:TUIElement;name:String8);
begin
inherited Create(width,height,parent,name);
end;
function TUIToggleButton.Setup(caption:String8;toggled:boolean):TUIToggleButton;
begin
self.caption:=caption;
SetToggled(toggled);
result:=self;
end;
procedure TUIToggleButton.SetToggled(v:boolean);
begin
toggled:=v;
pressed:=v; // keep pressed in sync for visual rendering
if linkedToggled<>nil then linkedToggled^:=toggled;
if sendSignals<>ssNone then begin
Signal('UI\Button\Toggle\'+name,UIntPtr(self));
Signal('UI\'+name+'\Toggle');
end;
end;
procedure TUIToggleButton.SetPressed(pr:boolean);
begin
SetToggled(pr);
end;
procedure TUIToggleButton.DoClick;
var
i:integer;
begin
TUIElement.sender:=self;
if parent is TUIGroupBox then begin
// radio group: untoggle all siblings, activate self
for i:=0 to length(parent.children)-1 do
if (parent.children[i] is TUIToggleButton) and (parent.children[i]<>self) then
TUIToggleButton(parent.children[i]).SetToggled(false);
SetToggled(true);
end else
SetToggled(not toggled);
if sendSignals<>ssNone then begin
Signal('UI\'+name+'\OnClick',byte(toggled));
Signal('UI\Button\Down\'+name,TTag(self));
if Assigned(onClick) then onClick;
if Assigned(onClickAsync) then Thread.Start('UIClick:'+String8(name),TThreadProc(onClickAsync));
if onClickEvent<>'' then Signal(onClickEvent,TTag(self));
end;
end;
procedure TUIToggleButton.onMouseButtons(button:byte;state:boolean);
begin
if not flags.enabled then begin
Signal('UI\'+name+'\ClickDisabled',button);
exit;
end;
if (button=1) and state then DoClick; // fire on mouse DOWN
// don't call TUIButton.onMouseButtons (push logic)
end;
procedure TUIToggleButton.onMouseMove;
begin
inherited;
end;
function TUIToggleButton.onHotKey(keycode,shiftstate:byte):boolean;
begin
result:=false;
if not flags.enabled then exit;
// in a group box, don't click if already toggled (would have no effect)
if toggled and (parent is TUIGroupBox) then exit;
DoClick;
result:=true;
end;
class function TUIToggleButton.GetSwitchIndex(parent:TUIElement):integer;
var
e:TUIElement;
begin
result:=-1;
for e in parent.children do
if e is TUIToggleButton then begin
inc(result);
if TUIToggleButton(e).toggled then exit;
end;
result:=-1;
end;
{ TUICheckBox }
constructor TUICheckBox.Create(width,height:single;parent:TUIElement;name:String8);
begin
inherited Create(width,height,parent,name);
end;
function TUICheckBox.Setup(caption:String8;checked:boolean):TUICheckBox;
begin
self.caption:=caption;
SetToggled(checked);
result:=self;
end;
function TUICheckBox.GetChecked:boolean;
begin
result:=toggled;
end;
procedure TUICheckBox.SetChecked(v:boolean);
begin
SetToggled(v);
end;
{ TUIRadioButton }
constructor TUIRadioButton.Create(width,height:single;parent:TUIElement;name:String8);
begin
inherited Create(width,height,parent,name);
// radio behavior is implicit when parent is TUIGroupBox
end;
function TUIRadioButton.Setup(caption:String8;checked:boolean):TUIRadioButton;
begin
self.caption:=caption;
if checked then SetToggled(true);
result:=self;
end;
{ TUILabel }
procedure TUILabel.CaptionWidthIs(width:single);
var
oldW,dW:single;
begin
width:=width/globalScale;
oldW:=size.x;
ResizeClient(width,clientHeight);
dW:=size.x-oldW;
case align of
taCenter: position.x:=position.x+dW/2;
taRight: position.x:=position.x+dW;
end;
end;
constructor TUILabel.Create(width,height:single;parent:TUIElement;name:String8);
begin
inherited Create(width,height,parent,name);
shape:=shapeFull;
align:=taLeft;
sendSignals:=ssMajor;
verticalOffset:=0;
end;
function TUILabel.Setup(text:String8):TUILabel;
begin
caption:=text;
align:=taLeft;
result:=self;
end;
function TUILabel.Centered(text:String8):TUILabel;
begin
caption:=text;
align:=taCenter;
result:=self;
end;
function TUILabel.Right(text:String8):TUILabel;
begin
caption:=text;
align:=taRight;
result:=self;
end;
{ TUIWindow }
constructor TUIWindow.Create(innerWidth,innerHeight:single;sizeable:boolean;parent_:TUIElement;wndName:String8='';wndCaption:String8='');
var
deltaX,deltaY:integer;
begin
resizeable:=sizeable;
if resizeable then begin
deltaX:=wcFrameBorder; deltay:=wcFrameBorder;
end else begin
deltaX:=2; deltay:=2;
end;
inherited Create(innerWidth+deltaX*2,innerHeight+deltay+wcTitleHeight,parent_,wndName);
padding.Left:=deltaX; padding.Top:=wcTitleHeight;
padding.Right:=deltaX; padding.Bottom:=deltaY;
shape:=shapeFull;
caption:=wndCaption;
header:=wcTitleHeight;
autoBringToFront:=true;
flags.canhavefocus:=false;
moveable:=true;
minW:=32; minH:=32;
maxW:=1600; maxH:=1200;
style.SetAttr('color','$FFBCB8B0');
area:=0;
order:=100; // выше чем прочие элементы.
end;
destructor TUIWindow.Destroy;
begin
if (dragRegion<>nil) and not dragRegion.persistent then dragRegion.Free;
inherited;
end;
function TUIWindow.GetAreaType(x,y:integer;out cur:NativeInt):integer;
var
c:byte;
r:TRect;
begin
result:=0; cur:=CursorID.Default;
r:=GetPosOnScreen;
if (x<r.left) or (y<r.top) or (x>=r.Right) or (y>=r.Bottom) then exit;
dec(x,r.Left);
dec(y,r.Top);
// skinned mode: use dragRegion for hit-testing
if background<>nil then begin
if moveable then result:=wcHeader else result:=wcClient;
if (dragRegion<>nil) and not dragRegion.IsOpaque(x/r.Width,y/r.Height) then
result:=wcClient;
exit;
end;
if resizeable then begin
if x<wcFrameBorder then inc(result,wcLeftFrame);
if y<wcFrameBorder then inc(result,wcTopFrame);
if x+wcFrameBorder>=r.Width then inc(result,wcRightFrame);
if y+wcFrameBorder>=r.Height then inc(result,wcBottomFrame);
if (result=0) and (y<header) then inc(result,wcHeader);
end else begin
if y<header then inc(result,wcHeader);
end;
if result=0 then inc(result,wcClient);
c:=0;
if result and (wcLeftFrame+wcRightFrame)>0 then inc(c);
if result and (wcTopFrame+wcBottomFrame)>0 then inc(c,2);
case c of
1:cur:=CursorID.ResizeW;
2:cur:=CursorID.ResizeH;
3:cur:=CursorID.ResizeHW;
end;
end;
procedure TUIWindow.onLostFocus;
begin
hooked:=false;
end;
procedure TUIWindow.onMouseButtons(button:byte;state:boolean);
var
pnt:TPoint;
begin
inherited;
if (button=1) and not (area in [0,wcClient]) then begin
if not hooked and state then hooked:=true;
if hooked and not state then begin
hooked:=false;
// Don't allow window center to be moved outside screen
pnt:=GetPosOnScreen.CenterPoint;
/// TODO: implement action
end;
end;
end;
procedure TUIWindow.onMouseMove;
var
iScale:single;
dx,dy:single;
begin
if hooked then begin
iScale:=scale/globalScale; // pixels to parent's space scale
dx:=(curMouseX-oldMouseX)*iScale;
dy:=(curMouseY-oldMouseY)*iScale;
// Drag
if area=wcHeader then begin
position.Add(Vec2(dx,dy));
end;
// Resize
if area and wcRightFrame>0 then Resize(size.x+dx,-1);
if area and wcBottomFrame>0 then Resize(-1,size.y+dy);
if area and wcLeftFrame>0 then begin Resize(size.x-dx,-1); position.x:=position.x-dx; end;
if area and wcTopFrame>0 then begin Resize(-1,size.y-dy); position.y:=position.y-dy; end;
end;
inherited;
area:=GetAreaType(curMouseX,curMouseY,cursor);
if area in [0,wcClient] then hooked:=false;
end;
procedure TUIWindow.Resize(newWidth,newHeight:single);
begin
if newwidth<>-1 then begin
if newwidth<minW then newwidth:=minW;
if (newwidth>maxW) and (maxW>0) then newwidth:=maxW;
end;
if newheight<>-1 then begin
if newheight<minH then newheight:=minH;
if (newheight>maxH) and (maxH>0) then newheight:=maxH;
end;
inherited;
end;
class function TUIWindow.IsWindow:boolean;
begin
result:=true;
end;
{ TUIEditBox }
procedure TUIEditBox.AdjustState;
begin
if cursorpos<0 then cursorpos:=0;
if cursorpos>length(realtext) then cursorpos:=length(realtext);
if selstart<0 then selstart:=0;
if selstart>length(realtext) then selstart:=length(realtext);
if selcount<0 then selcount:=0;
if selstart+selcount>length(realtext) then selcount:=length(realtext)-selstart;
end;
constructor TUIEditBox.Create(width,height:single;parent_:TUIElement;name:String8='');
begin
inherited Create(width,height,parent_,name);
shape:=shapeFull;
cursor:=CursorID.Input;
selstart:=0;
selcount:=0;
cursorpos:=0;
maxlength:=240;
password:=false;
protection:=0;
needPos:=-1;
offset:=0;
flags.canhavefocus:=true;
sendSignals:=ssAll;
lastClickTime:=0;
msSelStart:=-1;
end;
function TUIEditBox.GetText:String8;
begin
result:=UTF8.Encode(realText);
end;
procedure TUIEditBox.SetText(s:String8);
begin
realtext:=UTF8.Decode(s);
if cursorpos>length(realtext) then cursorpos:=length(realtext);
end;
procedure TUIEditBox.onChar(ch:char;scancode:byte);
begin
inherited;
end;
procedure TUIEditBox.onUniChar(ch:Char32;scancode:byte);
var
oldText:String32;
begin
oldText:=realText;
AdjustState;
cursortimer:=CoreTime.Ticks;
TUIElement.sender:=self;
if (ch=13) and (sendSignals<>ssNone) then begin
if (not completion.IsEmpty) and (not realText.Same(completion)) then begin
realText:=completion;
completion:=[];
cursorpos:=length(realtext);
selcount:=0;
Signal('UI\'+name+'\AutoCompletion',0);
Signal('UI\Editbox\AutoCompletion\'+name,0);
end else begin
Signal('UI\'+name+'\Enter',0);
Signal('UI\Editbox\Enter\'+name,0);
end;
end;
if (ch=27) and (sendSignals<>ssNone) then Signal('UI\'+name+'\Escape',0);
if (ch>=32) and (selcount>0) then begin
delete(realtext,selstart,selcount);
insert(ch,realtext,selstart);
selcount:=0;
cursorpos:=selstart+1;
exit;
end;
if (length(realtext)<maxlength) and (ch>=32) then begin
insert(ch,realtext,cursorpos);
inc(cursorpos);
end;
if (sendSignals=ssAll) and (oldText<>realText) then begin
savedText:=oldText;
Signal('UI\'+name+'\changed',0);
end;
end;
function TUIEditBox.onKey(keycode:byte;pressed:boolean;shiftstate:byte):boolean;
procedure ClipCopy(cut:boolean=false);
var
str:String32;
begin
if password or (protection<>0) then exit;
str:=copy(realtext,selstart,selcount);
CopyStrToClipboard(Str16(UTF8.Encode(str)));
if cut then begin
delete(realtext,selstart,selcount); selcount:=0; cursorpos:=selstart;
end;
end;
procedure ClipPaste;
var
wst:String32;
begin
wst:=Str32(PasteStrFromClipboardW);
if not wst.IsEmpty then begin
if selcount>0 then begin
delete(realtext,selstart,selcount);
cursorpos:=selstart;
end else
selstart:=cursorpos;
insert(wst,realtext,cursorpos);
selcount:=length(wst);
if length(realtext)>maxlength then setLength(realtext,maxlength);
if selstart+selcount>length(realtext) then selcount:=length(realtext)-selstart;
cursorpos:=selstart+selcount;
end;
end;
function IsWordSeparator(ch:Char32):boolean;
begin
result:=(ch<=32) or (ch=Char32('.')) or (ch=Char32(',')) or (ch=Char32(';')) or
(ch=Char32(':')) or (ch=Char32('!')) or (ch=Char32('?')) or (ch=Char32('"')) or
(ch=Char32('''')) or (ch=Char32('(')) or (ch=Char32(')')) or (ch=Char32('[')) or
(ch=Char32(']')) or (ch=Char32('{')) or (ch=Char32('}')) or (ch=Char32('/')) or
(ch=Char32('\')) or (ch=Char32('|')) or (ch=Char32('+')) or (ch=Char32('-')) or
(ch=Char32('*')) or (ch=Char32('=')) or (ch=Char32('<')) or (ch=Char32('>'));
end;
function PrevWordPos(pos:integer):integer;
begin
result:=pos;
while (result>0) and IsWordSeparator(realText[result-1]) do dec(result); // skip separators
while (result>0) and not IsWordSeparator(realText[result-1]) do dec(result); // go to word start
end;
function NextWordPos(pos:integer):integer;
var
txtLen:integer;
begin
txtLen:=length(realText);
result:=pos;
while (result<txtLen) and IsWordSeparator(realText[result]) do inc(result); // skip separators
while (result<txtLen) and not IsWordSeparator(realText[result]) do inc(result); // go to word end
end;
procedure MoveCaret(newPos:integer;extendSelection:boolean);
var
anchor,oldCursor:integer;
begin
oldCursor:=cursorPos;
if newPos<0 then newPos:=0;
if newPos>length(realText) then newPos:=length(realText);
if not extendSelection then begin
cursorPos:=newPos;
selStart:=0;
selCount:=0;
exit;
end;
// Keep the fixed edge of selection as anchor and move the caret edge.
if selCount=0 then anchor:=oldCursor
else
if oldCursor=selStart then anchor:=selStart+selCount
else anchor:=selStart;
cursorPos:=newPos;
if cursorPos=anchor then begin
selStart:=cursorPos;
selCount:=0;
end else
if cursorPos<anchor then begin
selStart:=cursorPos;
selCount:=anchor-cursorPos;
end else begin
selStart:=anchor;
selCount:=cursorPos-anchor;
end;
end;
var
newPos,txtLen:integer;
useCtrl,useShift:boolean;
oldText:String32;
begin