Debian Codegraph @

src/ui/castleuicontrols_touchlist.inc

raw
1 {%MainUnit castleuicontrols.pas}
2 {
3 Copyright 2010-2021 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 //type
20
21 { Tracking of a touch by a single finger, used by TTouchList. }
22 TTouch = record
23 public
24 { Index of the finger/mouse. Always simply zero on traditional desktops with
25 just a single mouse device. For devices with multi-touch
26 (and/or possible multiple mouse pointers) this is actually useful
27 to connect touches from different frames into a single move action.
28 In other words: the FinderIndex stays constant while user moves
29 a finger over a touch device.
30
31 All the touches on a TCastleContainer.Touches array always have different
32 FingerIndex value.
33
34 Note that the index of TTouch structure in TCastleContainer.Touches array is
35 @italic(not) necessarily equal to the FingerIndex. It cannot be ---
36 imagine you press 1st finger, then press 2nd finger, then let go of
37 the 1st finger. The FingerIndex of the 2nd finger cannot change
38 (to keep events sensibly reporting the same touch),
39 so there has to be a temporary "hole" in FinderIndex numeration. }
40 FingerIndex: TFingerIndex;
41
42 { Position of the touch over a device.
43
44 Position (0, 0) is the window's bottom-left corner.
45 This is consistent with how our 2D controls (TCastleUserInterface)
46 treat all positions.
47
48 The position is expressed as a float value, to support backends
49 that can report positions with sub-pixel accuracy.
50 For example GTK and Android can do it, although it depends on
51 underlying hardware capabilities as well.
52 The top-right corner or the top-right pixel has the coordinates
53 (Width, Height).
54 Note that if you want to actually draw something at the window's
55 edge (for example, paint the top-right pixel of the window with some
56 color), then the pixel coordinates are (Width - 1, Height - 1).
57 The idea is that the whole top-right pixel is an area starting
58 in (Width - 1, Height - 1) and ending in (Width, Height).
59
60 Note that we have mouse capturing (when user presses and holds
61 the mouse button, all the following mouse events are reported to this
62 window, even when user moves the mouse outside of the window).
63 This is typical of all window libraries (GTK, LCL etc.).
64 This implicates that mouse positions are sometimes tracked also
65 when mouse is outside the window, which means that mouse position
66 may be outside the rectangle (0, 0) - (Width, Height),
67 so it may even be negative. }
68 Position: TVector2;
69 end;
70 PTouch = ^TTouch;
71
72 { Tracking of multi-touch, a position of each finger on the screen. }
73 TTouchList = class({$ifdef FPC}specialize{$endif} TStructList<TTouch>)
74 private
75 { Find an item with given FingerIndex, or -1 if not found. }
76 function FindFingerIndex(const FingerIndex: TFingerIndex): Integer;
77 function GetFingerIndexPosition(const FingerIndex: TFingerIndex): TVector2;
78 procedure SetFingerIndexPosition(const FingerIndex: TFingerIndex;
79 const Value: TVector2);
80 public
81 { Gets or sets a position corresponding to given FingerIndex.
82 If there is no information for given FingerIndex on the list,
83 the getter will return zero, and the setter will automatically create
84 and add appropriate information. }
85 property FingerIndexPosition[const FingerIndex: TFingerIndex]: TVector2
86 read GetFingerIndexPosition write SetFingerIndexPosition;
87 { Remove a touch item for given FingerIndex. }
88 procedure RemoveFingerIndex(const FingerIndex: TFingerIndex);
89 end;
90
91 {$endif read_interface}
92
93 {$ifdef read_implementation}
94
95 { TTouchList ----------------------------------------------------------------- }
96
97 function TTouchList.FindFingerIndex(const FingerIndex: TFingerIndex): Integer;
98 begin
99 for Result := 0 to Count - 1 do
100 if L[Result].FingerIndex = FingerIndex then
101 Exit;
102 Result := -1;
103 end;
104
105 function TTouchList.GetFingerIndexPosition(const FingerIndex: TFingerIndex): TVector2;
106 var
107 Index: Integer;
108 begin
109 Index := FindFingerIndex(FingerIndex);
110 if Index <> -1 then
111 Result := L[Index].Position
112 else
113 Result := TVector2.Zero;
114 end;
115
116 procedure TTouchList.SetFingerIndexPosition(const FingerIndex: TFingerIndex;
117 const Value: TVector2);
118 var
119 Index: Integer;
120 NewTouch: PTouch;
121 begin
122 Index := FindFingerIndex(FingerIndex);
123 if Index <> -1 then
124 L[Index].Position := Value else
125 begin
126 NewTouch := PTouch(Add);
127 NewTouch^.FingerIndex := FingerIndex;
128 NewTouch^.Position := Value;
129 end;
130 end;
131
132 procedure TTouchList.RemoveFingerIndex(const FingerIndex: TFingerIndex);
133 var
134 Index: Integer;
135 begin
136 Index := FindFingerIndex(FingerIndex);
137 if Index <> -1 then
138 Delete(Index);
139 end;
140
141 {$endif read_implementation}