forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoverFragment.cs
More file actions
379 lines (330 loc) · 14.1 KB
/
Copy pathRoverFragment.cs
File metadata and controls
379 lines (330 loc) · 14.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
using System;
using UnityEngine;
using RemoteTech.FlightComputer.Commands;
namespace RemoteTech.UI
{
public class RoverFragment : IFragment
{
private FlightComputer.FlightComputer mFlightComputer;
private Action mOnClickQueue;
private bool distDefault = true;
private float
mSteering = 0,
mSteerClamp = 1,
prevTurn = 0,
prevDist = 0;
private string
mTurn = "",
mSpeed = "5",
mDist = "",
mLatitude = "",
mLongditude = "",
mHeading = "";
public RoverFragment(FlightComputer.FlightComputer fc, Action queue)
{
mFlightComputer = fc;
mOnClickQueue = queue;
}
private float Turn
{
get
{
float turn;
if (!Single.TryParse(mTurn, out turn)) {
turn = 0;
}
return turn;
}
set { mTurn = value.ToString(); }
}
private float Heading
{
get
{
float heading;
if (!Single.TryParse(mHeading, out heading)) {
heading = 0;
}
return heading;
}
set { mHeading = value.ToString(); }
}
private float Speed
{
get
{
float speed;
if (!Single.TryParse(mSpeed, out speed)) {
speed = 0;
}
return speed;
}
set { mSpeed = value.ToString(); }
}
private float Dist
{
get
{
float dist;
if (!Single.TryParse(mDist, out dist)) {
dist = 0;
}
return dist;
}
set { mDist = value.ToString(); }
}
private float Latitude
{
get
{
float latitude;
if (!Single.TryParse(mLatitude, out latitude)) {
latitude = 0;
}
return latitude;
}
set { mLatitude = value.ToString(); }
}
private float Longitude
{
get
{
float longitude;
if (!Single.TryParse(mLongditude, out longitude)) {
longitude = 0;
}
return longitude;
}
set { mLongditude = value.ToString(); }
}
private void EnqueueTurn()
{
mFlightComputer.Enqueue(DriveCommand.Turn(mSteering, Turn, Speed));
}
private void EnqueueDist()
{
mFlightComputer.Enqueue(DriveCommand.Distance(Dist, 0, Speed));
}
private int selectedModeIndex = 0;
private bool MouseClick = false;
private readonly GUIContent[] Tabs = { new GUIContent("TGT", "Drive to the latitude and longitude of a body or towards vessel target."),
new GUIContent("HDG", "Drive with specific heading and distance."),
new GUIContent("FINE", "Drive with specific turning or distance.") };
private enum RoverModes { TargetMode = 0,
HeadingMode = 1,
FineMode = 2 };
public void Draw()
{
float width3 = 156 / 3 - GUI.skin.button.margin.right * 2.0f / 3.0f;
GUILayout.BeginVertical();
{
selectedModeIndex = GUILayout.Toolbar(selectedModeIndex, Tabs, GUILayout.Width(width3*3 + GUI.skin.button.margin.right * 2.0f));
GUILayout.Space(5);
switch (selectedModeIndex) {
case (int) RoverModes.TargetMode:
DrawTargetContent();
break;
case (int) RoverModes.HeadingMode:
DrawHDGContent();
break;
case (int) RoverModes.FineMode:
DrawFineContent();
break;
}
GUILayout.Space(5);
GUILayout.BeginHorizontal();
{
RTUtil.Button(new GUIContent("DRIVE", "Starts the automatic driving."),
delegate { OnExecClick(selectedModeIndex); }, GUILayout.Width(width3));
GUILayout.FlexibleSpace();
RTUtil.Button(new GUIContent(">>", "Toggles the queue and delay functionality."),
mOnClickQueue, GUILayout.Width(width3));
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
private void OnExecClick(int modeIndex)
{
//FINE
if (modeIndex == (int)RoverModes.FineMode && Speed != 0)
{
if (mSteering != 0 && Turn != 0)
EnqueueTurn();
else if (Dist != 0)
EnqueueDist();
else
{
if (!distDefault && mSteering != 0 && Turn != 0)
EnqueueTurn();
else if (distDefault && Dist != 0)
EnqueueDist();
}
}
//HDG
else if (modeIndex == (int)RoverModes.HeadingMode)
{
mFlightComputer.Enqueue(DriveCommand.DistanceHeading(Dist, Heading, mSteerClamp, Speed));
}
//TGT
else if (modeIndex == (int)RoverModes.TargetMode)
{
mFlightComputer.Enqueue(DriveCommand.Coord(mSteerClamp, Latitude, Longitude, Speed));
}
}
private void DrawFineContent()
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Steer: ", "How much to turn"));
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(Math.Abs(mSteering).ToString("P"), ""));
if (mSteering != 0) {
if (mSteering < 0)
GUILayout.Label(new GUIContent("right", ""), GUILayout.Width(40));
else
GUILayout.Label(new GUIContent("left", ""), GUILayout.Width(40));
} else
GUILayout.Label(new GUIContent("", ""), GUILayout.Width(40));
}
GUILayout.EndHorizontal();
RTUtil.HorizontalSlider(ref mSteering, 1, -1);
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Turn", "How many degrees to turn"), GUILayout.Width(50));
GUI.SetNextControlName("RC1");
RTUtil.TextField(ref mTurn, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(°)", "How many degrees to turn"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Dist", "Distance to drive"), GUILayout.Width(50));
GUI.SetNextControlName("RC2");
RTUtil.TextField(ref mDist, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(m)", "Distance to drive"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Speed", "Speed to maintain, negative for reverse"), GUILayout.Width(50));
GUI.SetNextControlName("RC3");
RTUtil.TextField(ref mSpeed, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(m/s)", "Speed to maintain, negative for reverse"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
mTurn = RTUtil.ConstrictNum(mTurn, 90);
mDist = RTUtil.ConstrictNum(mDist, false);
mSpeed = RTUtil.ConstrictNum(mSpeed);
if (prevTurn != Turn)
distDefault = false;
else if (prevDist != Dist)
distDefault = true;
prevTurn = Turn;
prevDist = Dist;
}
private void DrawHDGContent()
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Steer: ", "How sharp to turn at max"));
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mSteerClamp.ToString("P"), "How sharp to turn at max"));
GUILayout.Label(new GUIContent("max", "How sharp to turn at max"), GUILayout.Width(40));
}
GUILayout.EndHorizontal();
RTUtil.HorizontalSlider(ref mSteerClamp, 0, 1);
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Hdg", "Heading to maintain"), GUILayout.Width(50));
GUI.SetNextControlName("RC1");
RTUtil.TextField(ref mHeading, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label("(°)", GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Dist", "Distance to drive"), GUILayout.Width(50));
GUI.SetNextControlName("RC2");
RTUtil.TextField(ref mDist, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label("(m)", GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Speed", "Speed to maintain"), GUILayout.Width(50));
GUI.SetNextControlName("RC3");
RTUtil.TextField(ref mSpeed, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(m/s)", "Speed to maintain"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
mHeading = RTUtil.ConstrictNum(mHeading, 360);
mDist = RTUtil.ConstrictNum(mDist, false);
mSpeed = RTUtil.ConstrictNum(mSpeed, false);
}
private void DrawTargetContent()
{
string targetTypeString = "Body coordinations";
string tooltip = "Hold " + GameSettings.MODIFIER_KEY.name + " and click on ground to input coordinates";
ITargetable Target = mFlightComputer.Vessel.targetObject;
if (GameSettings.MODIFIER_KEY.GetKey() && ((Input.GetMouseButton(0) || Input.GetMouseButton(1)) != MouseClick)) // on lookout for mouse click on body
{
MouseClick = Input.GetMouseButton(0) || Input.GetMouseButton(1);
Vector2 latlon;
if (MouseClick && RTUtil.CBhit(mFlightComputer.Vessel.mainBody, out latlon))
{
Latitude = latlon.x;
Longitude = latlon.y;
}
}
else if (Target != null) // only if target is vessel not world coord
{
if (Target.GetType().ToString().Equals("Vessel"))
{
Vessel TargetVessel = Target as Vessel;
Latitude = (float) TargetVessel.latitude;
Longitude = (float) TargetVessel.longitude;
targetTypeString = "Designated Vessel";
tooltip = "Drive to this vessel";
}
}
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Steer: ", "How sharp to turn at max"));
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mSteerClamp.ToString("P"), "How sharp to turn at max"));
GUILayout.Label(new GUIContent("max", "How sharp to turn at max"), GUILayout.Width(40));
}
GUILayout.EndHorizontal();
RTUtil.HorizontalSlider(ref mSteerClamp, 0, 1);
GUILayout.Label(new GUIContent("Mode: "+targetTypeString, tooltip));
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("LAT", "Latitude to drive to"), GUILayout.Width(50));
GUI.SetNextControlName("RC1");
RTUtil.TextField(ref mLatitude, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(°)", "Hold " + GameSettings.MODIFIER_KEY.name + " and click on ground to input coordinates"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("LON", "Longitude to drive to"), GUILayout.Width(50));
GUI.SetNextControlName("RC2");
RTUtil.TextField(ref mLongditude, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(°)", "Hold " + GameSettings.MODIFIER_KEY.name + " and click on ground to input coordinates"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent("Speed", "Speed to maintain"), GUILayout.Width(50));
GUI.SetNextControlName("RC3");
RTUtil.TextField(ref mSpeed, GUILayout.Width(50), GUILayout.ExpandWidth(false));
GUILayout.Label(new GUIContent("(m/s)", "Speed to maintain"), GUI.skin.textField, GUILayout.Width(40));
}
GUILayout.EndHorizontal();
mLatitude = RTUtil.ConstrictNum(mLatitude);
mLongditude = RTUtil.ConstrictNum(mLongditude);
mSpeed = RTUtil.ConstrictNum(mSpeed, false);
}
}
}