-
Notifications
You must be signed in to change notification settings - Fork 0
/
uistate.cpp
259 lines (231 loc) · 8.32 KB
/
uistate.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
#include "uistate.h"
std::unique_ptr<IState> UIState::EditIdleState::HandleInput(RobotGame* game)
{
olc::HWButton lmb = game->GetMouse(olc::Mouse::LEFT);
olc::HWButton rmb = game->GetMouse(olc::Mouse::RIGHT);
olc::vi2d pos = game->GetMousePos();
Block* block = game->GetBlockAt(pos);
Block* invBlock = game->GetBlockUnderMouseInv();
if (lmb.bPressed && block)
{
game->SelectBlock(block);
olc::vi2d gridpos = game->GetGridAt(pos);
return std::make_unique<UIState::DraggingState>(block, gridpos, gridpos - block->pos);
}
else if (rmb.bPressed && block)
{
return std::make_unique<UIState::IOSelectState>(block, ImGui::GetMousePos());
}
else if (invBlock)
{
game->SelectBlock(invBlock);
if (lmb.bPressed && game->InvCount(invBlock) > 0)
{
return std::make_unique<UIState::BlockPlaceState>(std::shared_ptr<Block>(invBlock->Clone()));
}
}
else if (game->GetKey(olc::Key::M).bPressed)
{
game->EnableLayer(game->blocksUILayer, false);
game->EnableLayer(game->connectionsLayer, false);
return std::make_unique<UIState::InteractIdleState>();
}
return nullptr;
}
std::unique_ptr<IState> UIState::DraggingState::HandleInput(RobotGame* game)
{
olc::HWButton lmb = game->GetMouse(olc::Mouse::LEFT);
if (lmb.bHeld)
{
olc::vi2d pos = game->GetMousePos();
olc::vi2d gridpos = game->GetGridAt(pos);
if (game->CanBePlaced(this->target, gridpos - this->handlepos))
this->target->pos = gridpos - this->handlepos;
return nullptr;
}
else if (lmb.bReleased)
return std::make_unique<UIState::EditIdleState>();
return nullptr;
}
std::unique_ptr<IState> UIState::IOSelectState::HandleInput(RobotGame* game)
{
ImGui::SetNextWindowSize({(float)200, (float)(35 + (this->target->ports.size() * 20) + (this->target->IsProgrammable() ? 35 : 0))});
ImGui::SetNextWindowPos({(float)this->mousepos.x, (float)this->mousepos.y});
ImGui::Begin("IO", NULL, game->popUpMenuFlags);
if (!ImGui::IsWindowFocused())
return std::make_unique<UIState::EditIdleState>();
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
auto ports = &this->target->ports;
static int openNode = -1;
int idx = 0;
for (auto it = ports->begin(); it != ports->end(); ++it, ++idx)
{
bool open = ImGui::TreeNodeEx(std::to_string(idx).c_str(), flags, it->first.c_str());
bool link = false;
if (it->second->CanConnect())
{
ImGui::SameLine(0.0f, 15.0f);
link = ImGui::SmallButton(("Link##" + std::to_string(idx)).c_str());
}
if (open)
openNode = idx;
if (open && idx == openNode)
{
auto conns = it->second->connections;
for (auto conn = conns.begin(); conn != conns.end();)
{
if ((*conn)->owner)
{
ImGui::BulletText((*conn)->owner->GetPortName(*conn).c_str());
ImGui::SameLine();
if (ImGui::SmallButton(("X##" + std::to_string(idx)).c_str()))
it->second->Disconnect(*conn);
else
++conn;
}
}
ImGui::TreePop();
}
if (link)
return std::make_unique<UIState::LinkingState>(this->target, it->second.get());
}
if (this->target->IsProgrammable() && ImGui::Button("Edit code", {80, 30}))
return std::make_unique<UIState::CodeEditState>(dynamic_cast<ProgrammableBlock*>(this->target));
if (ImGui::Button("Remove", {80, 30}))
{
game->IncrInvCount(this->target->schema, 1);
game->RemoveBlock(this->target);
return std::make_unique<UIState::EditIdleState>();
}
ImGui::End();
return nullptr;
}
void UIState::IOSelectState::OnExit(RobotGame* game)
{
ImGui::End();
}
std::unique_ptr<IState> UIState::LinkingState::HandleInput(RobotGame* game)
{
Block* block = game->GetBlockUnderMouse();
if (game->GetMouse(olc::Mouse::LEFT).bPressed && block)
return std::make_unique<UIState::IOSelectSecondState>(this->target, this->port, block, ImGui::GetMousePos());
else if (game->GetMouse(olc::Mouse::RIGHT).bPressed)
return std::make_unique<UIState::EditIdleState>();
return nullptr;
}
void UIState::LinkingState::Update(RobotGame* game)
{
game->SetDrawTarget(game->tempLayer);
game->DrawLine(game->GetBlockCenter(this->target), game->GetMousePos(), olc::GREEN);
}
std::unique_ptr<IState> UIState::IOSelectSecondState::HandleInput(RobotGame* game)
{
ImGui::SetNextWindowSize({120.0, (float)(this->target->ports.size() * 20)});
ImGui::SetNextWindowPos({(float)this->mousepos.x, (float)this->mousepos.y});
ImGui::Begin("IO", NULL, game->popUpMenuFlags);
if (!ImGui::IsWindowFocused())
{
ImGui::End();
return std::make_unique<UIState::EditIdleState>();
}
int idx = 0;
auto ports = &this->target->ports;
for (auto it = ports->begin(); it != ports->end(); ++it)
{
if (this->sourcePort->CanConnect(it->second.get()))
{
if (ImGui::Selectable((it->first + "##" + std::to_string(idx++)).c_str()))
{
this->sourcePort->Connect(it->second.get());
ImGui::End();
return std::make_unique<UIState::EditIdleState>();
}
}
else
ImGui::Selectable((it->first.c_str()), false, ImGuiSelectableFlags_Disabled);
}
ImGui::End();
return nullptr;
}
std::unique_ptr<IState> UIState::CodeEditState::HandleInput(RobotGame* game)
{
static bool open = true;
ImGui::SetNextWindowSizeConstraints({300, 200}, {800, 600});
if (open)
{
if (ImGui::Begin("Code Editor", &open, game->codeEditFlags))
{
ImVec2 textInputSize = ImGui::GetContentRegionAvail();
textInputSize.y = -40;
ImGui::InputTextMultiline("##code", &this->target->code, textInputSize, ImGuiInputTextFlags_AllowTabInput);
if (!this->target->VerifyCode())
ImGui::TextWrapped(this->target->GetError().c_str());
}
ImGui::End();
return nullptr;
}
else
{
open = true; // restore original state for next time the window is opened
return std::make_unique<UIState::EditIdleState>();
}
return nullptr;
}
std::unique_ptr<IState> UIState::BlockPlaceState::HandleInput(RobotGame* game)
{
if (game->GetMouse(olc::Mouse::RIGHT).bPressed)
return std::make_unique<UIState::EditIdleState>();
else if (game->GetMouse(olc::Mouse::LEFT).bPressed && game->CanBePlaced(this->target.get(), this->target->pos))
{
this->target->pos = game->GetGridAt(game->GetMousePos());
if (game->InvCount(this->target->schema) > 0)
{
game->PlaceBlock(this->target);
game->IncrInvCount(this->target->schema, -1);
return std::make_unique<UIState::EditIdleState>();
}
}
else if (game->GetMouse(olc::Mouse::LEFT).bPressed)
{
Block* b = game->GetBlockUnderMouseInv();
if (b)
this->target = std::shared_ptr<Block>(b->Clone());
}
return nullptr;
}
void UIState::BlockPlaceState::Update(RobotGame* game)
{
game->SetDrawTarget(game->tempLayer);
this->target->pos = game->GetGridAt(game->GetMousePos());
if (game->CanBePlaced(this->target.get(), this->target->pos))
game->DrawBlock(this->target.get());
}
std::unique_ptr<IState> UIState::InteractIdleState::HandleInput(RobotGame* game)
{
if (game->GetKey(olc::Key::M).bPressed)
{
game->EnableLayer(game->blocksUILayer, true);
game->EnableLayer(game->connectionsLayer, true);
return std::make_unique<UIState::EditIdleState>();
}
return nullptr;
}
void UIState::InteractIdleState::Update(RobotGame* game)
{
float tpsInv = 1.0f / game->tps;
this->timeCounter += game->timedelta;
if (this->timeCounter >= tpsInv)
{
this->timeCounter = this->timeCounter - tpsInv;
game->SimTick();
}
}
void UIState::InteractIdleState::OnEnter(RobotGame* game)
{
this->timeCounter = 0.0f;
game->SimStart();
}
void UIState::InteractIdleState::OnExit(RobotGame* game)
{
game->SimStop();
}