-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.cpp
1388 lines (1132 loc) · 51.5 KB
/
Object.cpp
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
//Object.cpp
#include "Object.h"
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <sdlUtility/Collision.h>
#include <sdlUtility/Renderer.h>
#include <sdlUtility/Vector2.h>
using namespace sdlUtility;
namespace sdlObjects {
void Object::AccelerateTowards(float Direction, float Velocity) {
Acceleration = Vector2(cosf(Direction)*Velocity, sinf(Direction)*Velocity);
Interactable.Relax();
}
bool Object::AllowsAutoSize(int Axis) {
if (Axis == Axis::X) {
return AutoSize.X();
} else if (Axis == Axis::Y) {
return AutoSize.Y();
} else return (AutoSize.X() || AutoSize.Y());
}
bool Object::AllowsOtherInput() {
return AllowOtherInput;
}
void Object::AnchorAt(float AnchorX, float AnchorY) {
Anchor = Vector2(AnchorX, AnchorY);
}
int Object::AnchorX(int State) {
int AnchorX = 0;
if (BaseParent) {
if (Scaled) BaseParent->EnableScaling(true);
float ParentWidth = BaseParent->Width(State), Width = this->Width(State);
AnchorX = ParentWidth*Anchor.X()-Width/2.f;
if (Anchor.X() != 0.5) {
if (AnchorX < 0) AnchorX = 0; else if (Anchor.X() != 0 && AnchorX+Width > ParentWidth) AnchorX = ParentWidth-Width;
}
if (Anchor.X() != 0.f) AnchorX -= BaseParent->ChildrenOffsetX();
if (Scaled) BaseParent->EnableScaling(false);
}
return AnchorX;
}
int Object::AnchorY(int State) {
int AnchorY = 0;
if (BaseParent) {
if (Scaled) BaseParent->EnableScaling(true);
float ParentHeight = BaseParent->Height(State), Height = this->Height(State);
AnchorY = ParentHeight*Anchor.Y()-Height/2.f;;
if (Anchor.Y() != 0.5) {
if (AnchorY < 0) AnchorY = 0; else if (Anchor.Y() && AnchorY+Height > ParentHeight) AnchorY = ParentHeight-Height;
}
if (Anchor.Y() != 0.f) AnchorY -= BaseParent->ChildrenOffsetY();
if (Scaled) BaseParent->EnableScaling(false);
}
return AnchorY;
}
void Object::Block(int Axis) {
if (abs(Axis) == Axis::X) {
if (Axis > 0) BlockX = 1; else if (Axis < 0) BlockX = -1;
} else if (abs(Axis) == Axis::Y) {
if (Axis > 0) BlockY = 1; else if (Axis < 0) BlockY = -1;
}
}
float Object::ChildrenOffsetX() {
float Scale = Scaled?this->Scale.X():1;
return Scale*ChildrenOffset.X();
}
float Object::ChildrenOffsetY() {
float Scale = Scaled?this->Scale.Y():1;
return Scale*ChildrenOffset.Y();
}
float Object::ClosestCollisionX() {
float ClosestCollisionX = CorrectionLimits.X();
if (ClosestCollisionX != 0.f) {
if (Interactable.Shape == Shapes::Disk) {
if (ClosestCollisionX < 0) ClosestCollisionX += CollisionWidth()/2.f; else if (ClosestCollisionX > 0) ClosestCollisionX -= CollisionWidth()/2.f;
} else if (Interactable.Shape == Shapes::Rectangle) {
ClosestCollisionX += CollisionWidth();
}
}
return ClosestCollisionX;
}
float Object::ClosestCollisionY() {
float ClosestCollisionY = CorrectionLimits.Y();
if (ClosestCollisionY != 0.f) {
if (Interactable.Shape == Shapes::Disk) {
ClosestCollisionY += CollisionWidth()/2.f;
} else if (Interactable.Shape == Shapes::Rectangle) {
ClosestCollisionY += CollisionHeight();
}
}
return fabs(ClosestCollisionY);
}
float Object::CollisionHeight() {
float Scale = Scaled?this->Scale.Y():1;
return Scale*(Interactable.CollisionHeight());
}
float Object::CollisionWidth() {
float Scale = Scaled?this->Scale.X():1;
return Scale*(Interactable.CollisionWidth());
}
float Object::ClipHeight() {
return ClipSize.Y();
}
float Object::ClipWidth() {
return ClipSize.X();
}
float Object::ClipX() {
return ClipPosition.X();
}
float Object::ClipY() {
return ClipPosition.Y();
}
bool Object::DetectCollisionWith(Object *Object, int &Axis, float &AdjustX, float &AdjustY, float Interpolation) {
bool Collision = false;
if (this != Object) {
class Interactable *Interactable2 = Object->GetInteractable();
int Shape1 = Interactable.Shape;
int Shape2 = Interactable2->Shape;
if (Shape1 == Shapes::Rectangle) {
float X1 = X(Interpolation, Reference::Collision), Y1 = Y(Interpolation, Reference::Collision);
float Width1 = CollisionWidth(), Height1 = CollisionHeight();
float Direction1 = GetDirection();
if (Shape2 == Shapes::Rectangle) {
float X2 = Object->X(Interpolation, Reference::Collision), Y2 = Object->Y(Interpolation, Reference::Collision);
float Width2 = Object->CollisionWidth(), Height2 = Object->CollisionHeight();
float Direction2 = Object->GetDirection();
Collision = Collision::RectangleInRectangle(X1, Y1, Width1, Height1, Direction1, X2, Y2, Width2, Height2, Direction2, AdjustX, AdjustY);
} else if (Shape2 == Shapes::Disk) {
float X2 = Object->X(Interpolation, Reference::Origin, Position::Center), Y2 = Object->Y(Interpolation, Reference::Origin, Position::Center);
float R2 = Object->CollisionWidth()/2.f;
float r2 = Object->CollisionHeight()/2.f;
float Angle2 = Interactable2->Section()*2.f*M_PI;
float Direction2 = Object->GetDirection();
Collision = Collision::DiskInRectangle(X2, Y2, R2, r2, Angle2, Direction2, X1, Y1, Width1, Height1, Direction1, AdjustX, AdjustY);
//if (Object->GetName() == "Neotms" && GetName() == "AreaCollision") std::cout << AdjustX << " " << AdjustY << std::endl;
}
} else if (Shape1 == Shapes::Disk) {
float X1 = X(Interpolation, Reference::Origin, Position::Center), Y1 = Y(Interpolation, Reference::Origin, Position::Center);
float R1 = CollisionWidth()/2.f;
float r1 = CollisionHeight()/2.f;
float Angle1 = Interactable.Section()*2.f*M_PI;
float Direction1 = GetDirection();
if (Shape2 == Shapes::Rectangle) {
float X2 = Object->X(Interpolation, Reference::Collision), Y2 = Object->Y(Interpolation, Reference::Collision);
float Width2 = Object->CollisionWidth(), Height2 = Object->CollisionHeight();
float Direction2 = Object->GetDirection();
Collision = Collision::DiskInRectangle(X1, Y1, R1, r1, Angle1, Direction1, X2, Y2, Width2, Height2, Direction2, AdjustX, AdjustY);
//if (GetName() == "Neotms" && Object->GetName() == "AreaCollision") std::cout << AdjustX << " " << AdjustY << std::endl;
} else if (Shape2 == Shapes::Disk) {
float X2 = Object->X(Interpolation, Reference::Origin, Position::Center), Y2 = Object->Y(Interpolation, Reference::Origin, Position::Center);
float R2 = Object->CollisionWidth()/2.f;
float r2 = Object->CollisionHeight()/2.f;
float Angle2 = Interactable2->Section()*2.f*M_PI;
float Direction2 = Object->GetDirection();
Collision = Collision::DiskInDisk(X1, Y1, R1, r1, Angle1, Direction1, X2, Y2, R2, r2, Angle2, Direction2, AdjustX, AdjustY);
}
}
if (!Interactable.IsCollidable() || !Interactable2->IsCollidable()) {
AdjustX = AdjustY = 0;
}
//Determine collision axis
if (Shape1 == Shapes::Disk && Shape2 == Shapes::Disk) {
if (fabs(AdjustX) > fabs(AdjustY)) {
Axis = Axis::X;
} else if (fabs(AdjustY) > fabs(AdjustX)) {
Axis = Axis::Y;
} else {
Vector2 Vector = Vector2(X()-Object->X(), Y()-Object->Y());
float Angle = Vector.Angle();
if (Angle >= M_PI) Axis = Axis::XY; else Axis = -Axis::XY;
}
} else {
if (AdjustX && AdjustY) {
if (fabs(AdjustX) < fabs(AdjustY)) {
Axis = Axis::X;
} else if (fabs(AdjustY) < fabs(AdjustX)) {
Axis = Axis::Y;
} else {
Axis = Axis::XY;
/*
if (Object1->IsBlocked(Axis::X)) {
Axis = Axis::X;
} else if (Object1->IsBlocked(Axis::Y)) {
Axis = Axis::Y;
}
*/
}
} else {
if (!AdjustX) Axis = Axis::X;
if (!AdjustY) Axis = Axis::Y;
}
}
//if (Object1->GetName() == "Neotms" && Object2->GetName() == "TestCollision2") std::cout << "R: " << Collision << " " << AdjustX << " " << AdjustY << " " << Axis << " " << Interpolation << std::endl;
}
return Collision;
}
float Object::DistanceFrom(Object *Object) {
float Distance = 0;
if (Object) {
float CenterX = Object->X(Reference::Origin, Position::Center);
float CenterY = Object->Y(Reference::Origin, Position::Center);
Distance = sqrtf(powf(CenterX-X(Reference::Origin, Position::Center), 2)+powf(CenterY-Y(Reference::Origin, Position::Center), 2));
}
return Distance;
}
void Object::DropAt(float X, float Y) {
Interactable.DropAt(X, Y);
Interactable.Relax();
//TargetInstance.SetPosition(X, Y);
//ProcessVelocity();
}
void Object::Enable(bool State) {
this->State = State;
}
void Object::EnableAutoSize(bool State) {
AutoSize = State;
}
void Object::EnableOtherInput(bool State) {
this->AllowOtherInput = State;
}
void Object::EnableScaling(bool State) {
Scaled = State;
}
int Object::EventHandler() {
int Event = Events::Idle;
if (EventQueue.size() > 0) {
Event = EventQueue[0];
EventQueue.erase(EventQueue.begin());
switch (Event) {
case Events::Drag:
OnDrag();
if (DragRequest.X() || DragRequest.Y()) {
if (Resistance > 0) {
Scaled = true;
if ((fabs(DragRequest.X()-X()) > Resistance || fabs(DragRequest.Y()-Y()) > Resistance)) {
Scaled = false;
Resistance = -Resistance;
DropAt(DragRequest.X()/Scale.X()-ParentX()-AnchorX(), DragRequest.Y()/Scale.Y()-ParentY()-AnchorY());
EventQueue.push_back(Events::Move);
DragRequest = Vector2(0, 0);
}
} else {
MoveTo(DragRequest.X()/Scale.X(), DragRequest.Y()/Scale.Y());
DragRequest = Vector2(0, 0);
}
}
break;
case Events::Resize:
OnResize();
if (ResizeRequest.X() || ResizeRequest.Y()) {
ResizeTo(ResizeRequest.X()/Scale.X(), ResizeRequest.Y()/Scale.Y());
ResizeRequest = Vector2(0, 0);
}
break;
case Events::Hide:
OnHide();
break;
case Events::LeftButtonDown:
OnLeftButtonDown();
break;
case Events::LeftButtonUp:
OnLeftButtonUp();
DragReference = Vector2(0, 0);
ResizeReference = Vector2(0, 0);
if (Resistance < 0) Resistance = -Resistance;
DragRequest = Vector2(0, 0);
break;
case Events::Load:
OnLoad();
break;
case Events::MouseIn:
OnMouseIn();
break;
case Events::MouseOut:
OnMouseOut();
break;
case Events::Move:
OnMove();
break;
case Events::RightButtonDown:
OnRightButtonDown();
break;
case Events::RightButtonUp:
OnRightButtonUp();
break;
case Events::Show:
OnShow();
break;
case Events::Stop:
OnStop();
break;
default:
break;
}
//EventHandler
int Event = Renderable.HandleEvents();
switch(Event) {
case sdlObjects::Events::AnimationChange:
OnAnimationChange(&Renderable);
break;
case sdlObjects::Events::TextureChange:
OnTextureChange();
break;
default:
break;
}
}
return Event;
}
float Object::GetAnchor(int Axis) {
if (Axis == Axis::X) return Anchor.X(); else return Anchor.Y();
}
float Object::GetDirection() {
return Direction;
}
std::string Object::GetID() {
return Index+"."+Name;
}
std::string Object::GetIndex() {
return Index;
}
std::string Object::GetName() {
return Name;
}
Interactable *Object::GetInteractable() {
return &Interactable;
}
Renderable *Object::GetRenderable() {
return &Renderable;
}
std::string Object::GetValue() {
return Value;
}
float Object::Height(int State) {
float Scale = Scaled?this->Scale.Y():1.f;
float BaseHeight = Scale*Interactable.Height(State);
float Height = BaseHeight;
if (!BaseHeight) {
Height = InheritedHeight(State);
}
//if (Name == "Testy") std::cout << Width() << " " << Height << " " << BaseHeight << " " << AnchorX() << std::endl;
return Height;
}
void Object::Hide() {
if (Shown) {
Shown = false;
ResetInput();
EventQueue.push_back(Events::Hide);
}
}
int Object::Input(SDL_Event &Event, bool Debug) {
int Input = 0;
if ((State && IsShown(true) && Interactable.IsInteractive()) || (Debug&&BaseParent&&BaseParent->IsShown(true))) {
if (Event.type == SDL_MOUSEMOTION || Event.type == SDL_USEREVENT) {
//Mouse in and out events
if (SDL_ShowCursor(-1)) {
Scaled = true;
//Width/Height may be inherited and not defined in InputWidth
float InputWidth = Interactable.InputWidth()?Interactable.InputWidth()*Scale.X():Width();
float InputHeight = Interactable.InputHeight()?Interactable.InputHeight()*Scale.Y():Height();
bool HoverCheck = false;
int Shape = Interactable.Shape;
if (Shape == Shapes::Rectangle) {
float InputClipX = ClipPosition.X(), InputClipY = ClipPosition.Y();
float InputClipHeight = ClipSize.Y(), InputClipWidth = ClipSize.X();
if (InputClipWidth > 1) InputClipWidth = 1; else if (InputClipWidth < 0) InputClipWidth = 0;
if (InputClipHeight > 1) InputClipHeight = 1; else if (InputClipHeight < 0) InputClipHeight = 0;
if (InputClipX <= 0) InputClipX = 0; else if (InputClipX > 1) InputClipX = 1;
if (InputClipY <= 0) InputClipY = 0; else if (InputClipY > 1) InputClipY = 1;
float InputWidthClipped = InputClipWidth*(1-InputClipX)*InputWidth, InputHeightClipped = InputClipHeight*(1-InputClipY)*InputHeight;
HoverCheck = Collision::PointInRectangle(Event.motion.x, Event.motion.y, X(Reference::Origin, InputClipX), Y(Reference::Origin, InputClipY), InputWidthClipped, InputHeightClipped, Direction);
//if (Name == "MainScreenUI") std::cout << HoverCheck << " " << Event.motion.x << " " << X(Reference::Origin, InputClipX) << " " << Event.motion.y << " " << Y(Reference::Origin, InputClipY) << " " << InputWidthClipped << " " << InputHeightClipped << " " << ClipPosition.X() << " " << ClipPosition.Y() << std::endl;
} else if (Shape == Shapes::Disk) {
HoverCheck = Collision::PointInDisk(Event.motion.x, Event.motion.y, X(Reference::Origin, Position::Center), Y(Reference::Origin, Position::Center), InputWidth/2.f, InputHeight/2.f, Interactable.Section()*2.f*M_PI, Direction);
} else if (Shape == Shapes::Triangle) {
HoverCheck = Collision::PointInTriangle(Event.motion.x, Event.motion.y, X(), Y(), InputWidth, InputHeight, Direction);
}
if (HoverCheck) {
if (!Hovered) {
//if (!SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(SDL_BUTTON_LEFT) || (AllowOtherInput && Event.type == SDL_MOUSEMOTION)) {
if (true) {
EventQueue.push_back(Events::MouseIn);
Hovered = true;
Input = Events::MouseIn;
}
} else Input = -Events::MouseIn;
} else {
if (Hovered) {
if (!((Sticky||Interactable.IsMovable()) && IsClicked(SDL_BUTTON_LEFT))) {
EventQueue.push_back(Events::MouseOut);
Hovered = false;
if (IsClicked(SDL_BUTTON_LEFT) && !(Interactable.IsMovable())) Clicked = Events::Idle;
Input = Events::MouseOut;
} else {
Input = -Events::MouseIn;
}
}
}
//Drag and resize events
if (Event.type == SDL_MOUSEMOTION) {
if (IsClicked(SDL_BUTTON_LEFT)) {
if ((Interactable.IsResizable()) && Collision::PointInRectangle(DragReference.X(), DragReference.Y(), ResizeReference.X()-5, ResizeReference.Y()-5, 5, 5, Direction)) {
ResizeRequest = Vector2(Event.motion.x-X(), Event.motion.y-Y());
Input = Events::Resize;
if (EventQueue.back() != Events::Resize) EventQueue.push_back(Events::Resize);
//std::cout << Resize.X() << std::endl;
} else if (Interactable.IsMovable()) {
DragRequest = Vector2(Event.motion.x-DragReference.X(), Event.motion.y-DragReference.Y());
Input = Events::Drag;
if (EventQueue.back() != Events::Drag) EventQueue.push_back(Events::Drag);
//std::cout << Drag.X() << std::endl;
} else if (Interactable.IsMovable() || Sticky) DragReference = Vector2(Event.motion.x-X(), Event.motion.y-Y());
}
}
Scaled = false;
}
} else if (Event.type == SDL_MOUSEBUTTONDOWN) {
Scaled = true;
//Button down events
if (Hovered && Event.button.button == SDL_BUTTON_LEFT) {
if (!IsClicked(SDL_BUTTON_LEFT)) {
if (IsClicked(SDL_BUTTON_RIGHT)) EventQueue.push_back(Events::RightButtonUp);
EventQueue.push_back(Events::LeftButtonDown);
Clicked = SDL_BUTTON_LEFT;
Input = Events::LeftButtonDown;
if (Interactable.IsMovable() || Sticky || Debug) DragReference = Vector2(Event.motion.x-X(), Event.motion.y-Y());
if (Interactable.IsMovable() || Sticky || Debug) ResizeReference = Vector2(Width(), Height());
} else Clicked = 0;
} else if (Hovered && Event.button.button == SDL_BUTTON_RIGHT) {
if (!IsClicked(SDL_BUTTON_RIGHT)) {
if (IsClicked(SDL_BUTTON_LEFT)) EventQueue.push_back(Events::LeftButtonUp);
EventQueue.push_back(Events::RightButtonDown);
Clicked = SDL_BUTTON_RIGHT;
Input = Events::RightButtonDown;
} else Clicked = 0;
}
Scaled = false;
} else if (Event.type == SDL_MOUSEBUTTONUP) {
//Button up events
if (Hovered) {
if (IsClicked(SDL_BUTTON_LEFT) && Event.button.button == SDL_BUTTON_LEFT) {
EventQueue.push_back(Events::LeftButtonUp);
Clicked = 0;
Input = Events::LeftButtonUp;
} else if (IsClicked(SDL_BUTTON_RIGHT) && Event.button.button == SDL_BUTTON_RIGHT) {
EventQueue.push_back(Events::RightButtonUp);
Clicked = 0;
Input = Events::RightButtonUp;
}
} else Clicked = 0;
}
}
if (!Input) Input = OnInput(Event);
return Input;
}
float Object::InterpolateX(float Interpolation) {
float Scale = Scaled?this->Scale.X():1.f;
float SpeedX = VelocityX(true);
//float LeftOverX = X(Reference::Origin, 0, 2)-X();
//if (X(Reference::Origin, 0, 2) != 0) SpeedX = LeftOverX;
float MaxVelocity = VelocityLimit();
if (MaxVelocity && (SpeedX == Velocity.X())) {
if (fabs(MaxVelocity) < fabs(SpeedX)) {
if (SpeedX < 0) SpeedX = -MaxVelocity; else SpeedX = MaxVelocity;
}
}
float InterpolateX = Scale*Interpolation*SpeedX;
if (InterpolateX*BlockX > 0) InterpolateX = 0;
if (Interactable.IsCollidable()) {
float ClosestCollision = ClosestCollisionX();
if (Velocity.X() && ClosestCollision && fabs(InterpolateX) > fabs(ClosestCollision)) {
InterpolateX = fabs(ClosestCollision);
if (SpeedX < 0.f) InterpolateX = -InterpolateX;
}
}
//if (fabs(LeftOverX) < fabs(InterpolateX)) InterpolateX = LeftOverX;
//if (Name == "Neotms") std::cout << InterpolateX << " " << BlockX << " " << CorrectionLimits.X() << std::endl;
//if (Name == "Neotms" ) { std::cout << Name << " " << PreviousInstance.X() << " " << TargetInstance.X() << " " << InterpolateX << std::endl; } //else std::cout << "0" << std::endl; }
//if (Name == "Camera01") { std::cout << InterpolateX/SpeedX << " " << " " << Velocity.X() << " " << BaseParent->GetVelocity(Axis::X) << std::endl; }
//if (Name == "Basement" && InterpolateX) { std::cout << "Area " << SpeedX << " " << MaxVelocity << std::endl; }
//if (Name == "Camera01" && InterpolateX) { std::cout << "Camera " << SpeedX << " " << MaxVelocity << std::endl; }
//if (GetID() == "Debug.Dialog") std::cout << TargetInstance.X()-PreviousInstance.X() << " " << SpeedX << std::endl;
return InterpolateX;
}
float Object::InterpolateY(float Interpolation) {
float Scale = Scaled?this->Scale.Y():1.f;
float SpeedY = VelocityY(true);
float MaxVelocity = VelocityLimit();
if (MaxVelocity && (SpeedY == Velocity.Y())) {
if (fabs(MaxVelocity) < fabs(SpeedY)) { if (SpeedY < 0) SpeedY = -MaxVelocity; else SpeedY = MaxVelocity; }
}
float InterpolateY = Scale*Interpolation*SpeedY;
if (InterpolateY*BlockY > 0) InterpolateY = 0;
if (Interactable.IsCollidable()) {
float ClosestCollision = ClosestCollisionY();
if (Velocity.Y() && ClosestCollision && fabs(InterpolateY) > fabs(ClosestCollision)) {
InterpolateY = fabs(ClosestCollision);
if (SpeedY < 0.f) InterpolateY = -InterpolateY;
}
}
//if (Name == "Camera01") { std::cout << InterpolateY/SpeedY << " " << " " << SpeedY << " " << MaxVelocity << " " << BaseParent->GetVelocity(Axis::Y) << std::endl; }
//if (Name == "Camera01") { std::cout << SpeedY << " " << PreviousInstance.Y() << " " << TargetInstance.Y() << " " << Velocity.Y() << std::endl; }
return InterpolateY;
}
float Object::InheritedHeight(int State) {
return 0.f;
}
float Object::InheritedWidth(int State) {
return 0.f;
}
bool Object::IsActive() {
return State;
}
int Object::IsBlocked(int Axis) {
int IsBlocked = 0;
if (Axis == Axis::X) {
IsBlocked = BlockX;
} else if (Axis == Axis::Y) {
IsBlocked = BlockY;
} else if (Axis == Axis::XY) {
IsBlocked = BlockX&&BlockY;
}
return IsBlocked;
}
bool Object::IsClicked(int Button) {
if (Clicked == Button) return true; else return false;
}
bool Object::IsClipped() {
return (ClipSize != Vector2(1.f, 1.f) || ClipPosition != Vector2(0.f, 0.f));
}
bool Object::IsHovered() {
return Hovered;
}
bool Object::IsMoving(int Axis, bool State) {
bool Moving = false;
if (Axis == Axis::XY && (Velocity.X() || Velocity.Y())) {
Moving = true;
} else if (Axis == Axis::X && Velocity.X()) {
Moving = true;
} else if (Axis == Axis::Y && Velocity.Y()) {
Moving = true;
}
return (Moving or (State && BaseParent && BaseParent->IsMoving(Axis, State) && Shown));
}
bool Object::IsShown(bool State) {
if (BaseParent && State) return (Shown && BaseParent->IsShown(true)); else return Shown;
}
void Object::LimitVelocity(float MaxVelocity) {
this->MaxVelocity = MaxVelocity;
}
void Object::LookAt(Object *Object) {
if (Object) {
int CenterX = Object->X(Reference::Origin, Position::Center);
int CenterY = Object->Y(Reference::Origin, Position::Center);
float Direction = atan2(CenterY-Y(Reference::Origin, Position::Center), CenterX-X(Reference::Origin, Position::Center))+M_PI/2.f;
LookAt(Direction);
}
}
void Object::LookAt(float Direction) {
if (Direction > 2*M_PI) Direction -= 2*M_PI; else if (Direction < 0) Direction += 2*M_PI;
this->Direction = Direction;
}
void Object::MoveBy(float X, float Y) {
if (!Interactable.IsDriven()) EventQueue.push_back(Events::Move);
if (fabs(X) < 0.01) X = 0.f;
if (fabs(Y) < 0.01) Y = 0.f;
Interactable.MoveBy(X, Y);
if (Interactable.dX()*BlockX < -1) BlockX = 0;
if (Interactable.dY()*BlockY < -1) BlockY = 0;
}
void Object::MoveTo(float X, float Y) {
if (!Interactable.IsDriven()) EventQueue.push_back(Events::Move);
Interactable.MoveTo(X-(ParentX()+AnchorX()), Y-(ParentY()+AnchorY()));
if (Interactable.dX()*BlockX < -1) BlockX = 0;
if (Interactable.dY()*BlockY < -1) BlockY = 0;
}
void Object::MoveToLocal(float X, float Y) {
if (!Interactable.IsDriven()) EventQueue.push_back(Events::Move);
Interactable.MoveTo(X, Y);
if (Interactable.dX()*BlockX < -1) BlockX = 0;
if (Interactable.dY()*BlockY < -1) BlockY = 0;
}
Object::Object() {
AllowOtherInput = Hovered = false; Shown = true; Scaled = Sticky = false; State = true;
Direction = 0; MaxVelocity = 0;
ClipSize = Vector2(1.f, 1.f);
BlockX = BlockY = Clicked = TimeOfFlight = 0;
Resistance = 0;
Scale = Vector2(1.f, 1.f);
BaseParent = NULL;
}
void Object::OnCollision() {
}
void Object::OnDrag() {
}
void Object::Offset(int Axis, float Position) {
if (Axis == Axis::X) {
Interactable.DropAt(Position, Interactable.Y());
Interactable.Relax(Axis::X);
} else {
Interactable.DropAt(Interactable.X(), Position);
Interactable.Relax(Axis::Y);
}
}
void Object::OffsetChildren(int Axis, float Position) {
if (Axis == Axis::X) {
ChildrenOffset = Vector2(Position, ChildrenOffset.Y());
} else if (Axis == Axis::Y) {
ChildrenOffset = Vector2(ChildrenOffset.X(), Position);
}
}
void Object::OnAnimationChange(sdlObjects::Renderable *Renderable) {
}
void Object::OnHide() {
}
int Object::OnInput(SDL_Event &Event) {
int Input = 0;
return Input;
}
void Object::OnLeftButtonDown() {
}
void Object::OnLeftButtonUp() {
}
void Object::OnLoad() {
}
void Object::OnMouseIn() {
}
void Object::OnMouseOut() {
}
void Object::OnMove() {
}
void Object::OnProcess() {
}
void Object::OnResize() {
}
void Object::OnRightButtonDown() {
}
void Object::OnRightButtonUp() {
}
void Object::OnShow() {
}
void Object::OnStop() {
}
void Object::OnTextureChange() {
//Backup AutoSize
if (!AutoSize) {
Texture* Texture = Renderable.GetTexture();
if (!Width() && Texture && Texture->GetWidth() > 1) Resize(Axis::X, Texture->GetWidth());
if (!Height() && Texture && Texture->GetHeight() > 1) Resize(Axis::Y, Texture->GetHeight());
}
}
void Object::OnValueChange() {
}
float Object::ParentX(int State) {
if (BaseParent) {
if (Scaled) BaseParent->EnableScaling(true);
float X = (BaseParent->X(Reference::Origin, Position::None, State)+BaseParent->ChildrenOffsetX());
if (Scaled) BaseParent->EnableScaling(false);
return X;
} else return 0;
}
float Object::ParentY(int State) {
if (BaseParent) {
if (Scaled) BaseParent->EnableScaling(true);
float Y = (BaseParent->Y(Reference::Origin, Position::None, State)+BaseParent->ChildrenOffsetY());
if (Scaled) BaseParent->EnableScaling(false);
return Y;
} else return 0;
}
void Object::Process() {
if (State) {
Update();
//if (IsMoving()) std::cout << Name << " " << Velocity.X() << " " << Velocity.Y() << std::endl;
//if (Name == "Neotms") std::cout << PreviousInstance.Y() << " " << TargetInstance.Y() << std::endl;
}
OnProcess();
CorrectionLimits = Vector2(0.f, 0.f);
}
void Object::ProcessCollisions() {
bool BlockXFlag = false;
bool BlockYFlag = false;
float TotalAdjustX = 0, TotalAdjustY = 0;
float AdjustX = 0, AdjustY = 0;
int Axis = 0, Adjustment = 0;
int Step = 0;
unsigned int i = 0;
//if (Name == "Neotms") std::cout << "PC: " << CollisionQueue.size() << " " << BlockX << std::endl;
while (CollisionQueue.size() > 0) {
//Get next Adjustment
Axis = CollisionQueue[Adjustment][2];
AdjustX = CollisionQueue[Adjustment][0];
AdjustY = CollisionQueue[Adjustment][1];
//if (Name == "Neotms") std::cout << "C: " << i+1 << " " << CollisionQueue[i][0] << " " << CollisionQueue[i][1] << " " << CollisionQueue[i][2] << std::endl;
//Determine Adjustment (smallest first)
if (abs(CollisionQueue[i][2]) == Axis::X) {
BlockXFlag = true;
if (fabs(CollisionQueue[i][0]) < fabs(AdjustX)) {
Adjustment = i;
AdjustX = CollisionQueue[i][0];
AdjustY = CollisionQueue[i][1];
Axis = CollisionQueue[i][2];
}
} else if (abs(CollisionQueue[i][2]) == Axis::Y) {
BlockYFlag = true;
if (fabs(CollisionQueue[i][1]) < fabs(AdjustY)) {
Adjustment = i;
AdjustX = CollisionQueue[i][0];
AdjustY = CollisionQueue[i][1];
Axis = CollisionQueue[i][2];
}
} else if (abs(CollisionQueue[i][2]) == Axis::XY) {
BlockXFlag = true;
BlockYFlag = true;
Adjustment = i;
AdjustX = CollisionQueue[i][0];
AdjustY = CollisionQueue[i][1];
Axis = CollisionQueue[i][2];
}
i++;
if (i >= CollisionQueue.size()) {
//Apply Adjustment
if (TotalAdjustX*AdjustX > 0) AdjustX -= TotalAdjustX; else if (TotalAdjustX*AdjustX < 0 && Axis == Axis::X) {
Axis = Axis::XY;
if (AdjustY*Velocity.Y() < 0) AdjustY = -AdjustY;
//std::cout << "Conflict" << std::endl;
} else {
/*
if (AdjustX*CorrectionLimits.X() > 0 && fabs(AdjustX)+GetCollisionWidth()/2.f > fabs(CorrectionLimits.X()) && fabs(CorrectionLimits.Y()) <= GetCollisionWidth()/2.f) {
std::cout << Step << " FY " << CorrectionLimits.Y() << std::endl;
Axis = Axis::Y;
if (AdjustY*Velocity.Y() < 0) AdjustY = -AdjustY;
}
*/
}
if (TotalAdjustY*AdjustY > 0) AdjustY -= TotalAdjustY; else if (TotalAdjustY*AdjustY < 0 && Axis == Axis::Y) {
Axis = Axis::XY;
if (AdjustX*Velocity.X() < 0) AdjustX = -AdjustX;
//std::cout << "Conflict" << std::endl;
} else {
/*
if (AdjustY*CorrectionLimits.Y() > 0 && fabs(AdjustY)+GetCollisionWidth()/2.f > fabs(CorrectionLimits.Y()) && fabs(CorrectionLimits.X()) <= GetCollisionWidth()/2.f) {
std::cout << Step << " FX " << CorrectionLimits.X() << std::endl;
Axis = Axis::X;
if (AdjustX*Velocity.X() < 0) AdjustX = -AdjustX;
}
*/
}
if (Axis == Axis::X) {
if (AdjustX != 0) {
//std::cout << Step << " X " << AdjustX << " " << AdjustY << " " << CollisionQueue[Adjustment][2] << " " << CorrectionLimits.X() << std::endl;;
Interactable.NudgeBy(-AdjustX, 0);
TotalAdjustX += AdjustX;
if (AdjustX > 0) BlockX = 1; else if (AdjustX < 0) BlockX = -1;
}
} else if (Axis == Axis::Y) {
if (AdjustY != 0) {
//std::cout << Step << " Y " << AdjustY << " " << AdjustX << " " << CollisionQueue[Adjustment][2] << " " << CorrectionLimits.Y() << std::endl;;
Interactable.NudgeBy(0, -AdjustY);
TotalAdjustY += AdjustY;
if (AdjustY > 0) BlockY = 1; else if (AdjustY < 0) BlockY = -1;
}
} else if (abs(Axis) == Axis::XY) {
//std::cout << Step << " XY " << AdjustX << " " << AdjustY << " " << CollisionQueue[Adjustment][2] << " " << CorrectionLimits.X() << " " << CorrectionLimits.Y() << std::endl;;
Interactable.NudgeBy(-AdjustX, -AdjustY);
TotalAdjustX += AdjustX;
TotalAdjustY += AdjustY;
if (AdjustX > 0) BlockX = 1; else if (AdjustX < 0) BlockX = -1; else if (Axis == Axis::XY) BlockX = 1; else if (Axis == -Axis::XY) BlockX = -1;
if (AdjustY > 0) BlockY = 1; else if (AdjustY < 0) BlockY = -1; else if (Axis == Axis::XY) BlockY = 1; else if (Axis == -Axis::XY) BlockY = -1;
}
CollisionQueue.erase(CollisionQueue.begin()+Adjustment);
Step++;
Adjustment = 0;
AdjustX = AdjustY = 0;
i = 0;
}
}
if (!BlockXFlag) BlockX = 0;
if (!BlockYFlag) BlockY = 0;
//if (Name == "Neotms") std::cout << CorrectionLimits.X() << " " << CorrectionLimits.Y() << std::endl;
//if (Name == "Neotms") std::cout << "B: " << BlockX << " " << BlockY << " " << ClosestCollisionX() << std::endl << std::endl;
if (Step) OnCollision();
}
void Object::ProcessVelocity() {
float Vx = 0, Vy = 0, Angle = 0;
if (Interactable.IsCollidable() && !DragReference) {
float Vo = 0.f;
float Acceleration = 0.f;
//Guided motion
if (Interactable.IsDriven()) {
Vo = Velocity.Length();
Vector2 TargetAcceleration(Interactable.dX(), Interactable.dY());
Acceleration = TargetAcceleration.Length();
Angle = TargetAcceleration.Angle();
if (Acceleration < 1.f) { Interactable.Relax(); Vo = Acceleration = 0.f; }
//Projectile and accelerated motion
} else {
Vo = !ProjectileVelocity?Velocity.Length():ProjectileVelocity.Length();
float AccelerationFactor = this->Acceleration.Length();
if (Vo || AccelerationFactor) {
if (!!ProjectileVelocity) {
Angle = ProjectileVelocity.Angle();
ProjectileVelocity = Vector2(0.f, 0.f);
} else Angle = !this->Acceleration?Velocity.Angle():this->Acceleration.Angle();
float EffectiveArea = fabs(sin(Angle-Direction))*CollisionWidth() + fabs(cos(Angle-Direction))*CollisionHeight();
float Drag = Collision::CalculateDrag(Vo, 5e-4f*EffectiveArea, EffectiveArea);
Acceleration = AccelerationFactor-Drag;
if (Acceleration < 0 && fabs(Acceleration) > Vo) Acceleration = -Vo;
//std::cout << "Free: " << Vo << " " << Acceleration << " " << Drag << std::endl;
//if (Mass && Vector2(Vx, Vy).Length()*powf(Vo, 2) < Drag/Mass) { Vx = 0; Vy = 0; }
}