-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.hpp
133 lines (118 loc) · 3.02 KB
/
State.hpp
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
#pragma once
#include "Gates.hpp"
#include <raylib.h>
#include <vector>
class State {
// Drawables
std::vector<Object *> Objects;
// Gates
std::vector<Gate *> Gates;
// Points
std::vector<Point *> Points;
public:
State() {
this->Out = new Output;
this->Out->SetPosition(this->InilitOutPos());
}
inline void SetOutput(Output *o) { this->Out = o; }
inline void AddObject(Object *ob) { this->Objects.push_back(ob); }
inline void AddGate(Gate *ob) {
AddPoint(ob);
this->Gates.push_back(ob);
}
inline void AddPoint(Point *p) {
AddObject(p);
this->Points.push_back(p);
}
inline void DrawAll() {
for (auto object : this->Objects) {
object->Draw();
}
this->Out->Draw();
}
inline void Solve() {
if (this->Out->IsConnectedTo()) {
this->Out->Solve();
}
}
inline void CycleAll() {
for (auto object : this->Objects) {
object->Cycle();
object->CheckMouse();
object->DragMove();
}
this->Out->Cycle();
this->Out->CheckMouse();
this->Out->DragMove();
for (auto point : this->Points) {
point->DragToConnect();
}
for (auto point : this->Points) {
point->DragToConnect();
if (point->IsConnDragging()) {
this->DraggedConnection = point;
}
}
for (auto gate : this->Gates) {
if (this->DraggedConnection != nullptr && gate->IsMouseOnThis()) {
if (!gate->IsConnDragging() && gate->IsMouseOnThis() &&
gate != this->DraggedConnection) {
gate->ConnectToThis(this->DraggedConnection);
this->DraggedConnection = nullptr;
}
}
}
if (this->Out != nullptr && this->DraggedConnection != nullptr) {
if (this->Out->IsMouseOnThis() && !this->Out->IsConnectedTo()) {
this->Out->ConnectToThis(this->DraggedConnection);
this->DraggedConnection->ConnectThis(this->Out);
this->DraggedConnection = nullptr;
}
}
}
inline Vector2 InilitOutPos() {
return Vector2{(float)GetScreenWidth() / 2 - 300, (float)GetScreenHeight() / 2};
}
inline void Reset() {
this->Objects.clear();
this->Gates.clear();
this->Points.clear();
this->DraggedConnection = nullptr;
this->MovingObject = false;
this->Out = nullptr;
this->Out = new Output;
this->Out->SetPosition(this->InilitOutPos());
}
// If an object is currently being moved
bool MovingObject;
Point *DraggedConnection = nullptr;
Output *Out = nullptr;
inline void Controls() {
switch (GetKeyPressed()) {
case KEY_DELETE:
this->Reset();
break;
case KEY_ONE:
this->AddPoint(new Switch);
break;
case KEY_TWO:
this->AddGate(new AndGate);
break;
case KEY_THREE:
this->AddGate(new OrGate);
break;
case KEY_FOUR:
this->AddGate(new NandGate);
break;
case KEY_FIVE:
this->AddGate(new NorGate);
break;
case KEY_SIX:
this->AddGate(new XorGate);
break;
case KEY_SEVEN:
this->AddGate(new XnorGate);
break;
}
}
};