-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorLogic.cs
577 lines (495 loc) · 21.2 KB
/
EditorLogic.cs
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
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Caravel.Core;
using Caravel.Core.Entity;
using static Caravel.Core.Entity.Cv_Entity;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Caravel.Core.Input;
using System.Windows.Forms;
using Caravel.Core.Process;
using Caravel;
using System.Linq;
using static Caravel.Core.Cv_SceneManager;
namespace CaravelEditor
{
public class EditorLogic : Cv_GameLogic
{
public string ProjectDirectory
{
get; private set;
}
public int NumEntities
{
get
{
return Entities.Count;
}
}
public bool IsRunning
{
get
{
return State == Cv_GameState.Running;
}
}
public EditorView EditorView
{
get; private set;
}
public Dictionary<Cv_EntityID, Cv_Entity> EntitiesMap
{
get
{
return Entities;
}
}
public Dictionary<string, Cv_Entity> EntityPathsMap
{
get
{
return EntitiesByPath;
}
}
public string[]EntityNames
{
get
{
List<string> entityNames = new List<string>();
foreach (var e in EntitiesByPath.Values)
{
entityNames.Add(e.EntityName);
}
return entityNames.ToArray();
}
}
public List<string> PhysicsMaterials
{
get
{
var materials = new List<string>();
materials.AddRange(GamePhysics.GetMaterials());
return materials;
}
}
public int EntityDragStepX
{
get; set;
}
public int EntityDragStepY
{
get; set;
}
public int EntityDragStepRot
{
get; set;
}
public string CurrentScenePreLoadScript
{
get; set;
}
public string CurrentScenePostLoadScript
{
get; set;
}
public string CurrentSceneUnLoadScript
{
get; set;
}
private Vector2 m_PrevMousePos;
private int m_iPreviousScrollValue;
private bool m_bMovingEntity = false;
private bool m_bRotatingEntity = false;
private Cv_Entity m_EntityBeingMoved;
private Vector2 m_DeltaBuffer;
private int m_iIdleTime = 0;
private Dictionary<string, int> m_LastIDsUsed = new Dictionary<string, int>();
public EditorLogic(CaravelApp app) : base(app)
{
m_iPreviousScrollValue = 0;
m_PrevMousePos = new Vector2(-1, -1);
ProjectDirectory = Directory.GetCurrentDirectory();
CurrentScenePostLoadScript = "";
CurrentScenePreLoadScript = "";
CurrentSceneUnLoadScript = "";
}
public Vector2 GetNewEntityWorldPos(int screenX, int screenY, int stepX, int stepY)
{
var editorApp = ((EditorApp)Caravel);
var parentPosition = Vector3.Zero;
var parentEntity = GetEntity(editorApp.EForm.CurrentEntity);
if (parentEntity != null)
{
if (parentEntity.GetComponent<Cv_TransformComponent>() != null)
{
parentPosition = parentEntity.GetComponent<Cv_TransformComponent>().WorldPosition;
}
}
var worldPos = EditorView.GetWorldCoords(new Vector2(screenX, screenY));
Vector2 pos = Vector2.Zero;
if (worldPos != null)
{
int numStepsX = (int)(worldPos.Value.X) / stepX;
int numStepsY = (int)(worldPos.Value.Y) / stepY;
int signX = worldPos.Value.X < 0 ? -1 : 1;
int signY = worldPos.Value.Y < 0 ? -1 : 1;
pos = new Vector2((numStepsX + signX * 0.5f) * stepX, (numStepsY + signY * 0.5f) * stepY);
pos -= new Vector2(parentPosition.X, parentPosition.Y);
}
return pos;
}
protected override bool VGameOnPreLoadScene(XmlElement sceneData, string sceneID)
{
return true;
}
protected override bool VGameOnLoadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
{
if (sceneName == "Root")
{
var scriptElement = sceneData.SelectNodes("Script").Item(0);
if (scriptElement != null)
{
CurrentScenePreLoadScript = scriptElement.Attributes["preLoad"].Value;
CurrentScenePostLoadScript = scriptElement.Attributes["postLoad"].Value;
CurrentSceneUnLoadScript = scriptElement.Attributes["unLoad"].Value;
}
}
return true;
}
protected override void VGameOnUpdate(float time, float timeElapsed)
{
var cam = GetEntity(EditorView.EditorCamera);
if (cam == null)
{
return;
}
var camTransf = cam.GetComponent<Cv_TransformComponent>();
var camSettings = cam.GetComponent<Cv_CameraComponent>();
var mouseState = Mouse.GetState();
var keyboardState = Keyboard.GetState();
var editorApp = ((EditorApp)Caravel);
var mousePos = Cv_InputManager.Instance.GetMouseValues().MousePos;
var mouseScroll = Cv_InputManager.Instance.GetMouseValues().MouseWheelVal;
//MOUSE ACTIONS
if (Cv_InputManager.Instance.CommandActive("mouseLeftClick", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EWindow != null
&& editorApp.EWindow.Focused
&& editorApp.EWindow.EditorForm.CanSelectEntities
&& mousePos.X > 0 && mousePos.Y > 0)
{
Cv_EntityID[] entities;
EditorView.Pick(mousePos, out entities);
if (Cv_InputManager.Instance.CommandActivated("mouseLeftClick", Cv_Player.One))
{
if (!Cv_InputManager.Instance.CommandActive("alternateMode", Cv_Player.One))
{
if (entities.Length > 0)
{
if (entities[0] != editorApp.EForm.CurrentEntity)
{
editorApp.EForm.SetSelectedEntity(entities[0]);
}
if (!m_bMovingEntity)
{
m_EntityBeingMoved = GetEntity(editorApp.EForm.CurrentEntity);
}
}
else
{
var timer = new Cv_TimerProcess(100, DeselectEntity);
Caravel.ProcessManager.AttachProcess(timer);
}
}
else if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
if (!m_bMovingEntity)
{
m_EntityBeingMoved = GetEntity(editorApp.EForm.CurrentEntity);
}
}
}
if (Cv_InputManager.Instance.CommandActive("mouseMove", Cv_Player.One) && m_EntityBeingMoved != null)
{
m_bMovingEntity = true;
var trasnfComp = m_EntityBeingMoved.GetComponent<Cv_TransformComponent>();
if (trasnfComp == null)
{
m_bMovingEntity = false;
m_EntityBeingMoved = null;
}
else
{
var delta = m_DeltaBuffer + (mousePos - m_PrevMousePos);
var deltaXscale = delta / (float)EditorView.EditorRenderer.Scale;
var entityDelta = deltaXscale / camSettings.Zoom;
var numStepsX = ((int) entityDelta.X) / EntityDragStepX;
var numStepsY = ((int) entityDelta.Y) / EntityDragStepY;
var finalDelta = new Vector3(numStepsX * EntityDragStepX, numStepsY * EntityDragStepY, 0);
m_DeltaBuffer = delta;
if (finalDelta != Vector3.Zero)
{
trasnfComp.SetPosition(trasnfComp.Position + finalDelta);
if (finalDelta.X != 0)
{
m_DeltaBuffer = new Vector2(0, m_DeltaBuffer.Y);
}
if (finalDelta.Y != 0)
{
m_DeltaBuffer = new Vector2(m_DeltaBuffer.X, 0);
}
}
}
}
}
}
else if (editorApp.Mode == EditorApp.EditorMode.CAMERA
&& editorApp.EWindow != null
&& editorApp.EWindow.Focused && m_PrevMousePos.X != -1)
{
var delta = m_PrevMousePos - mousePos;
camTransf.SetPosition(camTransf.Position + new Vector3(delta, 0) / (camSettings.Zoom * (float)EditorView.EditorRenderer.Scale));
}
else if (editorApp.Mode == EditorApp.EditorMode.CREATE)
{
if (Cv_InputManager.Instance.CommandActivated("mouseLeftClick", Cv_Player.One))
{
var timer = new Cv_TimerProcess(100, PaintEntity);
Caravel.ProcessManager.AttachProcess(timer);
}
}
}
else
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (m_bMovingEntity)
{
m_bMovingEntity = false;
m_EntityBeingMoved = null;
m_DeltaBuffer = Vector2.Zero;
editorApp.EForm.UpdateEntityXml();
}
}
}
if (Cv_InputManager.Instance.CommandActive("mouseWheelUp", Cv_Player.One) || Cv_InputManager.Instance.CommandActive("mouseWheelDown", Cv_Player.One))
{
m_iIdleTime = 0;
var delta = mouseScroll - m_iPreviousScrollValue;
if (editorApp.Mode == EditorApp.EditorMode.CAMERA)
{
camSettings.Zoom += delta / 3000f;
editorApp.EForm.UpdateTools();
}
else if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EWindow != null
&& editorApp.EWindow.Focused
&& editorApp.EWindow.EditorForm.CanSelectEntities)
{
var entity = GetEntity(editorApp.EForm.CurrentEntity);
if (entity != null)
{
var trasnfComp = entity.GetComponent<Cv_TransformComponent>();
if (trasnfComp != null)
{
m_bRotatingEntity = true;
trasnfComp.SetRotation(trasnfComp.Rotation + delta / 3000f);
}
}
}
}
}
else if (m_bRotatingEntity)
{
if (m_iIdleTime > 1000)
{
m_bRotatingEntity = false;
editorApp.EForm.UpdateEntityXml();
m_iIdleTime = 0;
}
else
{
m_iIdleTime += (int)(timeElapsed);
}
}
//SAVE SHORTCUT
if (Cv_InputManager.Instance.CommandActive("alternateMode", Cv_Player.One) && Cv_InputManager.Instance.CommandDeactivated("save", Cv_Player.One))
{
if (editorApp.EForm.CurrentSceneFile != null && editorApp.EForm.CurrentSceneFile != "")
{
editorApp.EForm.SaveScene();
}
}
//MOVE ENTITIES WITH ARROWS
if (Cv_InputManager.Instance.CommandActivated("Left", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
var e = GetEntity(editorApp.EForm.CurrentEntity);
if (e != null && e.GetComponent<Cv_TransformComponent>() != null)
{
var pos = e.GetComponent<Cv_TransformComponent>().Position;
e.GetComponent<Cv_TransformComponent>().SetPosition(new Vector3(pos.X - 1, pos.Y, pos.Z));
}
}
}
}
if (Cv_InputManager.Instance.CommandActivated("Right", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
var e = GetEntity(editorApp.EForm.CurrentEntity);
if (e != null && e.GetComponent<Cv_TransformComponent>() != null)
{
var pos = e.GetComponent<Cv_TransformComponent>().Position;
e.GetComponent<Cv_TransformComponent>().SetPosition(new Vector3(pos.X + 1, pos.Y, pos.Z));
}
}
}
}
if (Cv_InputManager.Instance.CommandActivated("Up", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
var e = GetEntity(editorApp.EForm.CurrentEntity);
if (e != null && e.GetComponent<Cv_TransformComponent>() != null)
{
var pos = e.GetComponent<Cv_TransformComponent>().Position;
e.GetComponent<Cv_TransformComponent>().SetPosition(new Vector3(pos.X, pos.Y - 1, pos.Z));
}
}
}
}
if (Cv_InputManager.Instance.CommandActivated("Down", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
var e = GetEntity(editorApp.EForm.CurrentEntity);
if (e != null && e.GetComponent<Cv_TransformComponent>() != null)
{
var pos = e.GetComponent<Cv_TransformComponent>().Position;
e.GetComponent<Cv_TransformComponent>().SetPosition(new Vector3(pos.X, pos.Y + 1, pos.Z));
}
}
}
}
//DELETE KEY
if (Cv_InputManager.Instance.CommandActivated("Delete", Cv_Player.One))
{
if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM)
{
if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY)
{
editorApp.EForm.RemoveEntityFromEditor(editorApp.EForm.CurrentEntity);
editorApp.EForm.SetSelectedEntity(Cv_EntityID.INVALID_ENTITY);
}
}
}
m_PrevMousePos = mousePos;
m_iPreviousScrollValue = mouseScroll;
}
protected override void VGameOnAddView(Cv_GameView view, Cv_EntityID entityId)
{
EditorView = (EditorView) view;
}
protected override Cv_EntityFactory VCreateEntityFactory()
{
return new EditorEntityFactory();
}
private void DeselectEntity()
{
var editorApp = ((EditorApp)Caravel);
if (editorApp.EWindow == null
|| editorApp.Mode != EditorApp.EditorMode.TRANSFORM
|| !editorApp.EWindow.Focused
|| !editorApp.EWindow.EditorForm.CanSelectEntities
|| editorApp.CurrentResourceBundle == null || editorApp.CurrentResourceBundle == "")
{
return;
}
editorApp.EForm.SetSelectedEntity(Cv_EntityID.INVALID_ENTITY);
}
private void PaintEntity()
{
var editorApp = ((EditorApp)Caravel);
var mousePos = Cv_InputManager.Instance.GetMouseValues().MousePos;
if (editorApp.EWindow == null
|| editorApp.Mode != EditorApp.EditorMode.CREATE
|| !editorApp.EWindow.Focused
|| !editorApp.EWindow.EditorForm.CanSelectEntities
|| editorApp.CurrentResourceBundle == null || editorApp.CurrentResourceBundle == "")
{
return;
}
if (editorApp.EForm.CurrentEntityType != null && editorApp.EForm.CurrentEntityType != "")
{
Cv_EntityID[] entities;
EditorView.Pick(mousePos, out entities);
bool entityExists = false;
foreach (var e in entities)
{
var entity = GetEntity(e);
if (entity != null && entity.EntityTypeResource == editorApp.EForm.CurrentEntityType)
{
entityExists = true;
break;
}
}
if (!entityExists)
{
var transform = Cv_Transform.Identity;
var typePos = editorApp.EForm.GetTypePos(editorApp.EForm.CurrentEntityType);
var entityNames = EntityNames;
var parentEntity = GetEntity(editorApp.EForm.CurrentEntity);
if (parentEntity != null)
{
entityNames = parentEntity.Children.Select(e => e.EntityName).ToArray();
if (!m_LastIDsUsed.ContainsKey(parentEntity.EntityPath))
{
m_LastIDsUsed.Add(parentEntity.EntityPath, 0);
}
}
else if (!m_LastIDsUsed.ContainsKey("Root"))
{
m_LastIDsUsed.Add("Root", 0);
}
Vector2 pos = GetNewEntityWorldPos((int) mousePos.X, (int) mousePos.Y, EntityDragStepX, EntityDragStepY);
var entityName = "";
var parentKey = parentEntity != null ? parentEntity.EntityPath : "Root";
do
{
entityName = editorApp.EForm.EntityTypeItems[editorApp.EForm.CurrentEntityType].Type + "_" + m_LastIDsUsed[parentKey];
m_LastIDsUsed[parentKey] = m_LastIDsUsed[parentKey]+1;
}
while (entityNames.Contains(entityName));
var entity = CreateEntity(editorApp.EForm.CurrentEntityType, entityName, editorApp.CurrentResourceBundle, true, editorApp.EForm.CurrentEntity, null, null);
var transformComp = entity.GetComponent<Cv_TransformComponent>();
if (transformComp)
{
transformComp.SetPosition(new Vector3(pos, typePos.Z));
}
if (entity != null)
{
editorApp.EForm.AddNewEntityToEditor(entity, true);
}
}
}
else
{
MessageBox.Show("You must select an entity type before using the 'Create' tool.");
}
}
}
}