-
Notifications
You must be signed in to change notification settings - Fork 5
/
uopengl_widgetset.pas
1562 lines (1406 loc) · 45.6 KB
/
uopengl_widgetset.pas
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
(******************************************************************************)
(* uOpenGL_WidgetSet.pas ??.??.???? *)
(* *)
(* Version : 0.13 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : <Module_description> *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: -Wird bei einem Scrollbar an stelle x geklickt so dass der *)
(* Scrollbutton nach dem Click eigentlich Rechts vom Cursor *)
(* stehen sollte, dann wird der Scrollbutton nicht nach Rechts *)
(* sondern nach Links verschoben.. *)
(* [] [] x [] *)
(* *)
(* History : 0.01 - Initial version *)
(* 0.02 = Einfügen "ColorToVector" *)
(* 0.03 = Einfügen TOpenGl_Listbox.ScrollDown *)
(* 0.04 = Fix Speicherloch Bei Freigabe *)
(* 0.05 = Erlauben OnClick für TOpenGl_Image *)
(* 0.06 = Umbau auf ueventer.pas *)
(* 0.07 = Umstellen auf smClamp => deutlich bessere Graphiken *)
(* 0.08 = Support für OpenGLASCIIFont *)
(* 0.09 = TOpenGL_Radiobutton *)
(* 0.10 = Fix Textglitch of TOpenGL_Radiobutton *)
(* 0.11 = OnChange für TOpenGL_Radiobutton *)
(* 0.12 = Fix Font colors where not per instance *)
(* 0.13 = TOpenGl_BaseClass.Hint *)
(* *)
(******************************************************************************)
Unit uopengl_widgetset;
{$MODE ObjFPC}{$H+}
Interface
Uses
ExtCtrls,
OpenGLContext,
dglOpenGL,
uvectormath,
forms, // TScrollBarKind = (sbHorizontal, sbVertical);
Graphics,
sysutils,
Controls,
Classes,
uOpenGL_ASCII_Font,
uopengl_truetype_font,
math,
LCLType,
lclintf,
uopengl_graphikengine,
uopengl_font_common,
ueventer;
Const
ShowHintTimeIntervalInMS = 1000; // Time in ms how long the cursor need to stay on the Element that the "fShowHint" flag is set
Type
{ TOpenGl_BaseClass }
TOpenGl_BaseClass = Class(TEventerClass) // Die Basisklasse, welche die ganzen Captures und Events Handled, alles im Protected Teil steht zum freien "Override" bereit.
private
fHintTimer: TTimer;
Procedure OnHintTimer(Sender: TObject);
protected
fShowHint: Boolean; // Wenn True, dann muss im OnRender der Hint gezeigt werden..
Procedure OnRender(); virtual; abstract;
Procedure MouseEnter(Sender: TObject); override;
Procedure MouseLeave(Sender: TObject); override;
public
Name: String; // Einfach nur so, wird intern eigentlich nicht benötig.
Hint: String;
IgnoreDepthtest: Boolean; // Default = true (Nur für die Abwärtskompatibilität, im Idealfall, sollte hier false sein !)
OnShowHint: TNotifyEvent;
Constructor Create(aOwner: TOpenGLControl); override;
Destructor Destroy(); override;
Procedure Render(); // Damit alles Funktionieren kann wie gewünscht muss vorher die Routine WidgetSetGo2d aufgerufen werden !!
End;
{ TOpenGL_BaseFontClass }
TOpenGl_BaseFontClass = Class(TOpenGL_BaseClass)
protected
Function fGetFontColor: TVector3; virtual;
Procedure fSetFontColor(Value: TVector3); virtual;
Function FGetFontSize: single; virtual;
Procedure FSetFontSize(value: Single); virtual;
protected
FFont: TOpenGL_Font; // Das Kontrollelement bekommt noch zusätzlich eine OpenGLFont
Property FontColor: TVector3 read fGetFontColor write fSetFontColor;
Property FontSize: Single read FGetFontSize write FSetFontSize;
public
Constructor Create(aOwner: TOpenGLControl; FontFile: String); virtual; reintroduce;
Destructor Destroy; override;
End;
{ TOpenGl_Image }
TOpenGl_Image = Class(TOpenGL_BaseClass)
protected
Fimage: TGraphikItem;
Procedure OnRender(); override;
Procedure UpdateDimension;
public
Transparent: Boolean; // Das ist noch nicht wirklich schön funktionert aber erst mal ..
Property OnClick;
Property OnDblClick;
Property OnMouseDown;
Property OnMouseMove;
Property OnMouseUp;
Procedure SetHeight(AValue: integer); override;
Procedure SetWidth(AValue: integer); override;
Procedure SetImage(Filename: String); overload;
Procedure SetImage(OpenGLIndex: integer); overload;
Constructor Create(aOwner: TOpenGLControl); override;
End;
{ TOpenGL_Label }
TOpenGl_Label = Class(TOpenGL_BaseFontClass)
protected
fcaption: String;
FFontColor: TVector3;
Procedure OnRender(); override;
Procedure Setcaption(value: String); virtual;
Function fGetFontColor: TVector3; override;
Procedure fSetFontColor(Value: TVector3); override;
public
Property Caption: String read fcaption write Setcaption;
Property FontColor;
Property FontSize;
Constructor Create(aOwner: TOpenGLControl; FontFile: String); override;
End;
{ TOpenGL_Button }
TOpenGl_Button = Class(TOpenGL_BaseClass)
private
FNormalTex: TGraphikItem;
fHoverTex: TGraphikItem;
fDownTex: TGraphikItem;
protected
Procedure SetHeight(AValue: integer); override;
Procedure SetWidth(AValue: integer); override;
Procedure OnRender(); override;
Procedure KeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState); override;
public
Caption: String;
RenderCaption: Boolean;
Property OnClick;
Constructor Create(aOwner: TOpenGLControl); override;
(*
* Lädt die 3 Texturen
* Normal = die die immer angezeigt wird
* Hover = die die angezeigt wird, solange sich dir Maus darüber bewegt
* Down = die die angezeigt wird, solange die Maus gedrückt wurde
*)
Procedure LoadTextures(Normal, Hover, Down: String); virtual;
End;
{ TOpenGL_Button_Alpha }
TOpenGl_Button_Alpha = Class(TOpenGL_Button)
private
protected
Procedure OnRender(); override;
public
(*
* Lädt die 4 Texturen
* Normal = die die immer angezeigt wird
* Hover = die die angezeigt wird, solange sich dir Maus darüber bewegt
* Down = die die angezeigt wird, solange die Maus gedrückt wurde
* Mask = Alpha Maske ( für alle Gleich )
*)
Procedure LoadTextures(Normal, Hover, Down, Mask: String); virtual; reintroduce;
End;
{ TOpenGL_Scrollbar }
TOpenGL_Scrollbar = Class(TOpenGL_BaseClass)
private
FScrollButton: TOpenGL_Button;
fUpButton: TOpenGL_Button;
fDownButton: TOpenGL_Button;
FKind: TScrollBarKind;
FMax: Integer;
FMin: integer;
FPosition: integer;
FScrolling: Boolean; // Wie FScrollButton.FMouseDown nur eben im Scrollbar verfügbar.
Procedure SetKind(AValue: TScrollBarKind);
Procedure SetMax(AValue: Integer);
Procedure SetMin(AValue: integer);
Procedure SetPosition(AValue: integer);
Procedure OnUpClick(Sender: TObject);
Procedure OnDownClick(Sender: TObject);
Procedure OnScrollMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Procedure OnScrollMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
protected
Procedure OnRender(); override;
Procedure SetLeft(AValue: integer); override;
Procedure SetTop(AValue: integer); override;
Procedure SetWidth(Value: integer); override;
Procedure SetHeight(Value: integer); override;
Procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
Procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
Procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
Procedure UpdateScrollButtonPosition();
public
SmallChange: integer;
LargeChange: integer;
OnChange: TNotifyEvent;
Property Position: integer read FPosition write SetPosition;
Property Min: integer read FMin write SetMin;
Property Max: Integer read FMax write SetMax;
Property Kind: TScrollBarKind read FKind write SetKind;
Constructor Create(aOwner: TOpenGLControl); override;
Destructor Destroy(); override;
End;
{ TOpenGL_Listbox }
TOpenGl_Listbox = Class(TOpenGL_BaseFontClass)
private
fScrollbar: TOpenGL_Scrollbar;
fTopIndex: integer;
FItems: TStringList;
FLineColors: TVector3Array;
Procedure fSetItem(Index: integer; Value: String);
Function fGetItem(Index: integer): String;
Procedure fSetLineColor(Index: integer; Value: TVector3);
Function fGetLineColor(index: integer): TVector3;
Function fgetSorted: boolean;
Procedure SetSorted(value: Boolean);
Procedure OnScrollbarChange(Sender: TObject);
Function getItemCount: integer;
protected
Procedure OnRender(); override;
Procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
Procedure KeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState); override;
Procedure SetLeft(AValue: integer); override;
Procedure SetTop(AValue: integer); override;
Procedure SetWidth(Value: integer); override;
Procedure SetHeight(Value: integer); override;
public
Color: TVector3; // HintergrundFarbe
BorderColor: TVector3; // RandFarbe
ItemIndex: integer; // Das Aktuell Ausgewählte Element ( oder -1)
Property Sorted: Boolean read fgetSorted write SetSorted; // Wenn True, dann wird beim Einfügen ( und nur da ) sortiert eingefügt.
Property OnDblClick;
Property FontColor; // Der Default Wert der Font beim Adden, hinterher muss man LineColor Nehmen
Property Items[index: integer]: String read fGetItem write fSetItem;
Property LineColor[index: integer]: TVector3 read fGetLineColor write fSetLineColor;
Property ItemCount: Integer read getItemCount;
Constructor create(aOwner: TOpenGLControl; FontFile: String); override;
Destructor destroy; override;
Function AddItem(Value: String; Color_: TVector3): integer; overload; // Jede Zeile kann ihre eigene Schriftfarbe haben
Function AddItem(Value: String): integer; overload; // Jede Zeile kann ihre eigene Schriftfarbe haben
Procedure ScrollDown;
Procedure Clear();
End;
{ TOpenGl_Edit }
TOpenGl_Edit = Class(TOpenGL_BaseFontClass)
private
FLastTimeStamp: Dword;
FDashVisible: Boolean;
FFontColor: TVector3;
protected
Procedure OnRender(); override;
Function GetHeight: Integer; override;
Procedure SetHeight(AValue: integer); override;
Function fGetFontColor: TVector3; override;
Procedure fSetFontColor(Value: TVector3); override;
Procedure KeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState); override;
public
Text: String;
Color: TVector3; // Die Hintergrundfarbe
BorderColor: TVector3; // Die "RandFarbe"
PassWordChar: Char;
Property FontColor;
Property OnKeyPress;
Constructor Create(aOwner: TOpenGLControl; FontFile: String); override;
End;
{ TOpenGL_Radiobutton }
TOpenGL_Radiobutton = Class(TOpenGL_BaseFontClass)
private
fCaption: String;
fChecked: Boolean;
Procedure setCaption(AValue: String);
Procedure setChecked(AValue: Boolean);
Procedure UncheckOthers(Sender: TObject);
protected
Procedure OnRender(); override;
Procedure Click; override;
public
CircleColor: TVector3; // Die Farbe des "Selectiert" Bobbels
CircleBackColor: TVector3; // Die Hintergrundfarbe, des "Selectiert" Bobbels
GroupIndex: Integer; // Will man mehrere Groupboxen haben die auch mehrere Selektierungen zulassen benötigt man Gruppierungen!
OnChange: TNotifyEvent;
Property Caption: String read fCaption write setCaption;
Property Checked: Boolean read fChecked write setChecked;
Constructor Create(aOwner: TOpenGLControl; FontFile: String); override;
End;
Procedure WidgetSetGo2d(Width_2D, Height_2d: Integer);
Procedure WidgetSetExit2d();
Function ColorToVector(Color: TColor): TVector3;
Implementation
Var
_2DWidth: integer = 0;
_2DHeight: integer = 0;
Procedure WidgetSetGo2d(Width_2D, Height_2d: Integer);
Begin
_2DWidth := Width_2D;
_2DHeight := Height_2d;
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0, Width_2D, Height_2d, 0, -1, 1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // Store old Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
End;
Procedure WidgetSetExit2d;
Begin
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Restore old Projection Matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // Restore old Projection Matrix
End;
Function WidgetSetTransformRoutine(x, y: integer): TPoint;
Var
dim: Array[0..3] Of Integer;
xx, yy: integer;
Begin
If (_2DWidth = 0) Or (_2DHeight = 0) Then Begin
Raise exception.create('Error 2D-Dimension not found. Did you use WidgetSetGo2d ?');
End;
glGetIntegerv(GL_VIEWPORT, @dim[0]);
xx := round(ConvertDimension(0, dim[2], x, 0, _2DWidth));
yy := round(ConvertDimension(0, dim[3], y, 0, _2DHeight));
result := Point(xx, yy);
End;
Function ColorToVector(Color: TColor): TVector3;
Begin
// TColor ist gespeichert als BGR
result.x := (color And $FF) / $FF;
result.y := ((color And $FF00) Shr 8) / $FF;
result.z := ((color And $FF0000) Shr 16) / $FF;
End;
{ TOpenGL_Scrollbar }
Constructor TOpenGL_Scrollbar.Create(aOwner: TOpenGLControl);
Begin
(*
* Dadurch, dass die Elemente zuerst erstellt werden hat das 2 Vorteile
* 1. Alle Sonderfälle auf Nil fallen weg
* 2. Im Eventer klauen sie der Komponente die OnMouse* Events -> und sind
* damit wie WYSIWYG ;)
*)
fUpButton := TOpenGL_Button.create(aOwner);
fScrollButton := TOpenGL_Button.create(aOwner);
fDownButton := TOpenGL_Button.create(aOwner);
Inherited Create(aOwner);
fUpButton.OnClick := @OnUpClick;
fDownButton.OnClick := @OnDownClick;
FScrollButton.OnMouseDown := @OnScrollMouseDown;
FScrollButton.OnMouseUp := @OnScrollMouseUp;
fkind := sbVertical;
FScrolling := false;
fMin := 0;
FMax := 100;
FPosition := 0;
SmallChange := 1;
LargeChange := 1;
OnChange := Nil;
// TODO: die 15x15 sind hardcodiert, das könnte "Besser" werden.
fUpButton.Height := 15;
fUpButton.Width := 15;
Fdownbutton.Height := 15;
Fdownbutton.Width := 15;
FScrollButton.Height := 15;
FScrollButton.Width := 15;
// Initialisieren der Button Dimensiontn
Top := 10;
Left := 10;
Width := 15;
Height := 100;
End;
Destructor TOpenGL_Scrollbar.Destroy();
Begin
FScrollButton.free;
fUpButton.free;
fDownButton.free;
Inherited Destroy;
End;
Procedure TOpenGL_Scrollbar.SetKind(AValue: TScrollBarKind);
Var
w, h: integer;
Begin
If FKind = AValue Then Exit;
w := Width;
h := Height;
FKind := AValue;
fUpButton.Width := 15;
fUpButton.Height := 15;
FScrollButton.Width := 15;
FScrollButton.Height := 15;
fDownButton.Width := 15;
fDownButton.Width := 15;
// So werden alle Unterelemente auf jeden Fall neu gesetzt...
SetWidth(h);
SetHeight(w);
SetLeft(left);
SetTop(Top);
UpdateScrollButtonPosition();
End;
Procedure TOpenGL_Scrollbar.SetMax(AValue: Integer);
Begin
If FMax = AValue Then Exit;
If avalue <= fmin Then exit;
FMax := AValue;
Position := math.min(fmax, Position);
UpdateScrollButtonPosition;
End;
Procedure TOpenGL_Scrollbar.SetMin(AValue: integer);
Begin
If FMin = AValue Then Exit;
If avalue >= fmax Then exit;
FMin := AValue;
Position := math.max(fmin, Position);
UpdateScrollButtonPosition;
End;
Procedure TOpenGL_Scrollbar.SetPosition(AValue: integer);
Begin
avalue := math.min(fmax, math.max(fmin, AValue));
If FPosition = AValue Then Exit;
FPosition := AValue;
UpdateScrollButtonPosition;
If assigned(OnChange) Then
OnChange(self);
End;
Procedure TOpenGL_Scrollbar.OnUpClick(Sender: TObject);
Begin
Position := Position - SmallChange;
End;
Procedure TOpenGL_Scrollbar.OnDownClick(Sender: TObject);
Begin
Position := Position + SmallChange;
End;
Procedure TOpenGL_Scrollbar.OnScrollMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Begin
FScrolling := ssleft In shift;
End;
Procedure TOpenGL_Scrollbar.OnScrollMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Begin
FScrolling := false;
End;
Procedure TOpenGL_Scrollbar.OnRender();
Begin
If FMouseHover Then Begin
glcolor4f(0, 1, 1, 1);
End
Else Begin
glcolor4f(1, 1, 1, 1);
End;
glbegin(GL_QUADS);
glVertex2f(left, top + Height);
glVertex2f(left + Width, top + Height);
glVertex2f(left + Width, Top);
glVertex2f(left, top);
glend;
fUpButton.Render();
fDownButton.Render();
FScrollButton.Render();
End;
Procedure TOpenGL_Scrollbar.SetLeft(AValue: integer);
Begin
Inherited SetLeft(AValue);
fUpButton.Left := AValue;
If FKind = sbVertical Then Begin
FScrollButton.Left := AValue;
fDownButton.Left := AValue;
End
Else Begin
UpdateScrollButtonPosition();
fDownButton.Left := Left + Width - 15;
End;
End;
Procedure TOpenGL_Scrollbar.SetTop(AValue: integer);
Begin
Inherited SetTop(AValue);
fUpButton.Top := AValue;
If FKind = sbVertical Then Begin
UpdateScrollButtonPosition();
fDownButton.Top := AValue + Height - 15;
End
Else Begin
FScrollButton.Top := AValue;
fDownButton.Top := AValue;
End;
End;
Procedure TOpenGL_Scrollbar.SetWidth(Value: integer);
Begin
Inherited SetWidth(Value);
If FKind = sbVertical Then Begin
fUpButton.Width := value;
FScrollButton.Width := value;
fDownButton.Width := value;
End
Else Begin
UpdateScrollButtonPosition();
fDownButton.left := left + value - fDownButton.Width;
End;
End;
Procedure TOpenGL_Scrollbar.SetHeight(Value: integer);
Begin
Inherited SetHeight(Value);
If FKind = sbVertical Then Begin
UpdateScrollButtonPosition();
fDownButton.Top := top + value - fDownButton.Height;
End
Else Begin
fUpButton.Height := value;
FScrollButton.Height := value;
fDownButton.Height := value;
End;
End;
Procedure TOpenGL_Scrollbar.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
Begin
Inherited MouseDown(Button, Shift, X, Y);
If (Not fUpButton.FMouseHover) And (Not fDownButton.FMouseHover) And (Not FScrollButton.FMouseHover) Then Begin
If FKind = sbVertical Then Begin
If FScrollButton.Top + Top < y Then Begin
Position := Position + LargeChange;
End
Else Begin
Position := Position - LargeChange;
End;
End
Else Begin
If FScrollButton.left + left < x Then Begin
Position := Position + LargeChange;
End
Else Begin
Position := Position - LargeChange;
End;
End;
UpdateScrollButtonPosition;
End;
End;
Procedure TOpenGL_Scrollbar.MouseMove(Shift: TShiftState; X, Y: Integer);
Var
P: Single;
Begin
Inherited MouseMove(Shift, X, Y);
If FScrolling Then Begin
If FKind = sbVertical Then Begin
If (y >= fUpButton.Height) And (y <= height - fdownButton.Height) Then Begin
p := ConvertDimension(fUpButton.Height, height - fdownButton.Height, y, fmin, fmax);
Position := round(p);
End;
End
Else Begin
If (x >= fUpButton.width) And (x <= width - fdownButton.width) Then Begin
p := ConvertDimension(fUpButton.width, width - fdownButton.width, x, fmin, fmax);
Position := round(p);
End;
End;
UpdateScrollButtonPosition;
End;
End;
Procedure TOpenGL_Scrollbar.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
Begin
Inherited MouseUp(Button, Shift, X, Y);
FScrolling := false;
End;
Procedure TOpenGL_Scrollbar.UpdateScrollButtonPosition();
Var
delta: integer;
s: Single;
Begin
If FKind = sbVertical Then Begin
delta := height - fUpButton.Height - fDownButton.Height - FScrollButton.Height;
s := ConvertDimension(fmin, fmax, FPosition, 0, delta);
FScrollButton.Top := top + round(s) + fUpButton.Height;
End
Else Begin
delta := width - fUpButton.width - fDownButton.width - FScrollButton.width;
s := ConvertDimension(fmin, fmax, FPosition, 0, delta);
FScrollButton.left := left + round(s) + fUpButton.width;
End;
End;
{ TOpenGL_Button_Alpha }
Procedure TOpenGL_Button_Alpha.OnRender;
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
Inherited OnRender;
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Procedure TOpenGL_Button_Alpha.LoadTextures(Normal, Hover, Down, Mask: String);
Begin
RenderCaption := false;
FNormalTex := OpenGL_GraphikEngine.LoadAlphaGraphikItem(normal, Mask, smClamp);
fHoverTex := OpenGL_GraphikEngine.LoadAlphaGraphikItem(hover, Mask, smClamp);
fDownTex := OpenGL_GraphikEngine.LoadAlphaGraphikItem(down, Mask, smClamp);
// TODO: Theoretisch müsste man hier ne AV-Werfen, wenn die Größen der Texturen unterschiedlich sind ...
Width := FNormalTex.OrigWidth;
Height := FNormalTex.OrigHeight;
End;
{ TOpenGL_Image }
Constructor TOpenGl_Image.Create(aOwner: TOpenGLControl);
Begin
Inherited Create(aOwner);
Transparent := false;
End;
Procedure TOpenGl_Image.OnRender();
Var
bool: GLboolean;
Begin
If Transparent Then Begin
RenderAlphaQuad(top, left, Fimage);
End
Else Begin
bool := glIsEnabled(gl_Blend);
If bool Then
gldisable(gl_blend);
glColor4f(1, 1, 1, 1);
RenderQuad(top, left, Fimage);
If bool Then
glenable(gl_blend);
End;
End;
Procedure TOpenGl_Image.UpdateDimension;
Begin
Width := fImage.OrigWidth;
Height := fImage.OrigHeight;
End;
Procedure TOpenGl_Image.SetHeight(AValue: integer);
Var
s: Single;
Begin
Inherited SetHeight(AValue);
(*
* Das Verhältniss von Fimage.OrigHeight / Fimage.StretchedHeight darf
* sich durch die Größenänderung nicht verändern, sonst stimmt das Rendern
* nachher nicht mehr ..
*)
If Fimage.StretchedHeight = 0 Then
s := 1
Else
s := Fimage.OrigHeight / Fimage.StretchedHeight;
Fimage.OrigHeight := AValue;
Fimage.StretchedHeight := round(Fimage.OrigHeight / s);
End;
Procedure TOpenGl_Image.SetWidth(AValue: integer);
Var
s: Single;
Begin
Inherited SetWidth(AValue);
(*
* Das Verhältniss von Fimage.OrigHeight / Fimage.StretchedHeight darf
* sich durch die Größenänderung nicht verändern, sonst stimmt das Rendern
* nachher nicht mehr ..
*)
If Fimage.StretchedWidth = 0 Then
s := 1
Else
s := Fimage.OrigWidth / Fimage.StretchedWidth;
Fimage.OrigWidth := AValue;
Fimage.StretchedWidth := round(Fimage.OrigWidth / s);
End;
Procedure TOpenGl_Image.SetImage(Filename: String);
Begin
fImage := OpenGL_GraphikEngine.LoadGraphikItem(Filename, smClamp);
UpdateDimension;
End;
Procedure TOpenGl_Image.SetImage(OpenGLIndex: integer);
Begin
fImage := OpenGL_GraphikEngine.GetInfo(OpenGLIndex);
UpdateDimension;
End;
{ TOpenGL_Listbox }
Constructor TOpenGl_Listbox.create(aOwner: TOpenGLControl; FontFile: String);
Begin
fScrollbar := TOpenGL_Scrollbar.create(aOwner);
Inherited Create(aOwner, FontFile);
fScrollbar.OnChange := @OnScrollbarChange;
FItems := TStringList.Create;
fScrollbar.top := 0;
fScrollbar.Max := 0;
fScrollbar.Visible := false;
Color := v3(0, 0, 0);
BorderColor := v3(0.5, 0.5, 0.5);
FontColor := v3(1, 1, 1);
Sorted := false;
fTopIndex := 0;
ItemIndex := -1;
Width := 150;
height := 100;
clear;
End;
Procedure TOpenGl_Listbox.SetWidth(Value: integer);
Begin
Inherited SetWidth(Value);
fScrollbar.left := left + Width - fScrollbar.Width;
End;
Procedure TOpenGl_Listbox.SetHeight(Value: integer);
Begin
Inherited SetHeight(Value);
fScrollbar.Height := value;
End;
Destructor TOpenGl_Listbox.destroy;
Begin
Clear;
fitems.free;
fScrollbar.free;
Inherited destroy;
End;
Procedure TOpenGl_Listbox.fSetItem(Index: integer; Value: String);
Begin
If (index >= 0) And (index < FItems.Count) Then
FItems[index] := value;
End;
Function TOpenGl_Listbox.fGetItem(Index: integer): String;
Begin
result := '';
If (index >= 0) And (index < FItems.Count) Then
result := Fitems[index];
End;
Procedure TOpenGl_Listbox.fSetLineColor(Index: integer; Value: TVector3);
Begin
If (index >= 0) And (index < FItems.Count) Then
FLineColors[index] := Value;
End;
Function TOpenGl_Listbox.fGetLineColor(index: integer): TVector3;
Begin
result := v3(0, 0, 0);
If (index >= 0) And (index < FItems.Count) Then
result := FLineColors[index];
End;
Function TOpenGl_Listbox.fgetSorted: boolean;
Begin
result := FItems.Sorted;
End;
Procedure TOpenGl_Listbox.SetSorted(value: Boolean);
Begin
FItems.Sorted := value;
End;
Procedure TOpenGl_Listbox.OnScrollbarChange(Sender: TObject);
Begin
fTopIndex := fScrollbar.Position;
End;
Function TOpenGl_Listbox.getItemCount: integer;
Begin
result := FItems.Count;
End;
Procedure TOpenGl_Listbox.OnRender();
Var
lw, nw, ps: Single;
dim: Array[0..3] Of Integer;
i: integer;
s: String;
ScrollbarWidth: integer;
Begin
If fScrollbar.Visible Then Begin
ScrollbarWidth := fScrollbar.Width;
End
Else Begin
ScrollbarWidth := 0;
End;
glGetIntegerv(GL_VIEWPORT, @dim[0]);
nw := max(dim[2] / _2DWidth, dim[3] / _2DHeight);
glGetFloatv(GL_POINT_SIZE, @ps);
glGetFloatv(GL_LINE_WIDTH, @lw);
glPointSize(nw); // Wegen der Font
glBindTexture(GL_TEXTURE_2D, 0);
// Erst mal nen Hintergrund malen
glColor3f(Color.x, Color.y, Color.z);
glbegin(GL_QUADS);
glvertex2f(left, top + height);
glvertex2f(left + width - ScrollbarWidth, top + height);
glvertex2f(left + width - ScrollbarWidth, top);
glvertex2f(left, top);
glend;
glLineWidth(4);
glColor3f(BorderColor.x, BorderColor.y, BorderColor.z);
glbegin(GL_LINE_LOOP);
glvertex2f(left, top + height);
glvertex2f(left + width, top + height);
glvertex2f(left + width, top);
glvertex2f(left, top);
glend;
// Rendern der Elemente
For i := 0 To trunc((height - 8) / ffont.TextHeight('8')) - 1 Do Begin
If fTopIndex + i < FItems.Count Then Begin
// Anzeigen der Selectierten zeile
If fTopIndex + i = ItemIndex Then Begin
glLineWidth(2);
glColor3f(0, 0, 1);
glbegin(GL_QUADS);
glvertex2f(left + 2, 4 + top + round(ffont.TextHeight('8') * (i + 1)));
glvertex2f(left + width - 2 - ScrollbarWidth, 4 + top + round(ffont.TextHeight('8') * (i + 1)));
glvertex2f(left + width - 2 - ScrollbarWidth, 4 + top + round(ffont.TextHeight('8') * (i + 0)));
glvertex2f(left + 2, 4 + top + round(ffont.TextHeight('8') * (i + 0)));
glend;
glLineWidth(4);
glColor3f(1, 0, 0);
glbegin(GL_LINE_LOOP);
glvertex2f(left + 2, 4 + top + round(ffont.TextHeight('8') * (i + 1)));
glvertex2f(left + width - 2 - ScrollbarWidth, 4 + top + round(ffont.TextHeight('8') * (i + 1)));
glvertex2f(left + width - 2 - ScrollbarWidth, 4 + top + round(ffont.TextHeight('8') * (i + 0)));
glvertex2f(left + 2, 4 + top + round(ffont.TextHeight('8') * (i + 0)));
glend;
End;
ffont.Colorv3 := FLineColors[fTopIndex + i];
s := FItems[i + fTopIndex];
While (ffont.TextWidth(s) > width - 4 - ScrollbarWidth) And (s <> '') Do Begin
delete(s, length(s), 1);
End;
ffont.Textout(left + 4, 4 + top + round(ffont.TextHeight('8') * i), s);
End
Else Begin
break;
End;
End;
glLineWidth(lw);
glPointSize(ps);
fScrollbar.Render();
End;
Procedure TOpenGl_Listbox.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
Begin
Inherited MouseUp(Button, Shift, X, Y);
If Not fScrollbar.FMouseHover Then Begin
ItemIndex := min(FItems.Count - 1, y Div round(ffont.TextHeight('8')) + fTopIndex);
End;
End;
Procedure TOpenGl_Listbox.KeyUp(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Var
deltaPage: integer;
Begin
Inherited KeyUp(Sender, Key, Shift);
If Not FFocus Then exit;
If key = VK_RETURN Then Begin
DblClick();
End;
If Key = vk_up Then Begin
ItemIndex := max(0, ItemIndex - 1);
If ItemIndex < fTopIndex Then fTopIndex := ItemIndex;
fScrollbar.Position := fTopIndex;
End;
If Key = vk_down Then Begin
ItemIndex := min(FItems.Count - 1, ItemIndex + 1);
If (ItemIndex + fTopIndex + 1) * ffont.TextHeight('8') > height - 8 Then Begin
fTopIndex := min(fTopIndex + 1, Fitems.count - integer(trunc((height - 8) / ffont.TextHeight('8'))));
End;
fScrollbar.Position := fTopIndex;
End;
If key = VK_NEXT Then Begin
deltaPage := trunc((height - 8) / ffont.TextHeight('8'));
ItemIndex := min(FItems.Count - 1, ItemIndex + deltaPage);
If (ItemIndex + fTopIndex + 1) * ffont.TextHeight('8') > height - 8 Then Begin
fTopIndex := min(fTopIndex + deltaPage, Fitems.count - integer(trunc((height - 8) / ffont.TextHeight('8'))));
End;
fScrollbar.Position := fTopIndex;
End;
If key = VK_PRIOR Then Begin
deltaPage := trunc((height - 8) / ffont.TextHeight('8'));
ItemIndex := max(0, ItemIndex - deltaPage);
If ItemIndex < fTopIndex Then Begin
fTopIndex := max(fTopIndex - deltaPage, 0);
End;
fScrollbar.Position := fTopIndex;
End;
End;
Procedure TOpenGl_Listbox.SetLeft(AValue: integer);
Begin
Inherited SetLeft(AValue);
fScrollbar.Left := left + Width - fScrollbar.Width;
End;
Procedure TOpenGl_Listbox.SetTop(AValue: integer);
Begin
Inherited SetTop(AValue);
fScrollbar.Top := Top;
End;
Function TOpenGl_Listbox.AddItem(Value: String; Color_: TVector3): integer;
Var
deltaPage, j, i: integer;
Begin
i := FItems.Add(value);
setlength(FLineColors, high(FLineColors) + 2);
If Sorted Then Begin
For j := high(FLineColors) Downto i + 1 Do
FLineColors[j] := FLineColors[j - 1];