-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit_Cursor.pas
158 lines (119 loc) · 3.41 KB
/
Unit_Cursor.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
unit Unit_Cursor;
interface
uses Classes, Controls, dglOpenGL,
Unit_ColorCoder, Unit_Controls, Unit_Pieces, Unit_Vector;
type
TMouseArea = (maIngot, maDeck);
//Ingame cursor that can grap pieces and move them around and across Ingot
TPCursor = class
private
fPosX: Integer;
fPosY: Integer;
fMouseArea: TMouseArea;
fPrevX, fPrevY: Single;
fPickedPiece: TPiece;
fIngotPoint: TVector3f;
fIngotNormal: TVector3f;
public
CodeBelow: TColorCodeId;
constructor Create;
destructor Destroy; override;
property PosX: Integer read fPosX;
property PosY: Integer read fPosY;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure MouseMove(Shift: TShiftState; X, Y: Integer);
procedure Render;
end;
var
fCursor: TPCursor;
implementation
uses Unit_Defaults, Unit_Deck, Unit_Ingot, Unit_Session, Unit_Render;
{ TCursor }
constructor TPCursor.Create;
begin
inherited;
end;
destructor TPCursor.Destroy;
begin
inherited;
end;
procedure TPCursor.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
fPosX := X;
fPosY := Y;
fPrevX := fPosX;
fPrevY := fPosY;
if fPosY < fRender.Height - DECK_HEIGHT then
fMouseArea := maIngot
else
fMouseArea := maDeck;
case fMouseArea of
maIngot: ;//fIngot.MouseDown;
maDeck: if CodeBelow.Code = ccPiece then
begin
fPickedPiece := fPieces.PieceById(CodeBelow.Id);
fPickedPiece.Location := plCursor;
fDeck.PiecePick(fPickedPiece);
end;
end;
end;
procedure TPCursor.MouseMove(Shift: TShiftState; X, Y: Integer);
var
RayStart, RayEnd: TVector3f;
begin
fPosX := X;
fPosY := Y;
CodeBelow := fRender.CodeBelow(fPosX, fPosY);
if not (ssLeft in Shift) then
if fPosY < fRender.Height - DECK_HEIGHT then
fMouseArea := maIngot
else
fMouseArea := maDeck;
case fMouseArea of
maIngot: begin
if (ssLeft in Shift) and (CodeBelow.Code in [ccNone, ccIngot]) then
fIngot.Rotate(-(fPrevX - fPosX)/1, -(fPrevY - fPosY)/1);
if (CodeBelow.Code = ccIngot) then
begin
fRender.GetRay(fPosX, fPosY, RayStart, RayEnd);
fIngot.RayIntersect(RayStart, RayEnd, fIngotPoint, fIngotNormal);
end;
end;
maDeck: if Shift = [] then
begin
if CodeBelow.Code = ccPiece then
fPieces.Selected := CodeBelow.Id
else
fPieces.Selected := -1;
end;
end;
fPrevX := fPosX;
fPrevY := fPosY;
end;
procedure TPCursor.Render;
var
V: TVector3f;
begin
if (fMouseArea = maDeck)
or ((fMouseArea = maIngot) and (CodeBelow.Code = ccNone)) then
begin
if fPickedPiece = nil then Exit;
fRender.Switch(rm2D);
glPushMatrix;
glTranslatef(fPosX, fPosY, 0);
glScalef(150, 150, 150);
fPickedPiece.Render2D;
glPopMatrix;
end;
if fIngotNormal.Y <> 0 then
begin
fRender.Switch(rm3D);
V := VectorAdd(fIngotPoint, VectorScale(fIngotNormal, 0.25));
glColor4f(1,1,0,1);
glBegin(GL_LINES);
glVertex3fv(@fIngotPoint);
glVertex3fv(@V);
glEnd;
end;
end;
end.