-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_tile_repository.cc
More file actions
279 lines (240 loc) · 5.7 KB
/
command_tile_repository.cc
File metadata and controls
279 lines (240 loc) · 5.7 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
#include "command_tile_repository.h"
#include <GL/gl.h>
#include "texgen.h"
#include <iostream>
class command_tile_repository_drag final : public command_tile_drag {
public:
virtual
~command_tile_repository_drag()
{
undo();
}
inline
command_tile_repository_drag(
command_tile_repository * origin,
std::unique_ptr<command_tile> tile,
tile_display_args_t display_args)
: origin_(origin)
, cpt_(new command_point(std::move(tile)))
, display_args_(display_args)
{
extents_ = cpt_->compute_layout({}, layout_);
}
void
undo() override
{
if (cpt_) {
origin_->append(std::move(*cpt_).extract_tile());
cpt_.reset();
}
}
std::unique_ptr<command_point>
consume() override
{
return std::move(cpt_);
}
void
move(double x, double y) override
{
x_ = x;
y_ = y;
if (cpt_) {
cpt_->apply_layout(layout_, x, y, display_args_.command_point_size, true);
}
}
command_point *
content() override
{
return cpt_.get();
}
const layout_extents &
extents() const override
{
return extents_;
}
void
draw(double global_phase) const override
{
if (cpt_) {
cpt_->draw(global_phase, display_args_);
}
}
void
set_display_args(tile_display_args_t display_args) override
{
display_args_ = display_args;
if (cpt_) {
cpt_->apply_layout(layout_, x_, y_, display_args_.command_point_size, true);
}
}
private:
command_tile_repository * origin_;
std::unique_ptr<command_point> cpt_;
commands_layout layout_;
layout_extents extents_;
tile_display_args_t display_args_;
double x_;
double y_;
};
void
command_tile_repository::rearrange(bool warp)
{
std::unordered_map<command_tile *, layout_extents> layout;
double xcol[2] = {
bounds_.x1 + (bounds_.x2 - bounds_.x1) * 0.25,
bounds_.x1 + (bounds_.x2 - bounds_.x1) * 0.75
};
for (std::size_t n = command_tile::min_kind; n <= command_tile::max_kind; ++n) {
command_tile::kind_t kind = static_cast<command_tile::kind_t>(n);
auto i = piles_.find(kind);
if (i == piles_.end()) {
continue;
}
const auto & tiles = i->second;
std::size_t row = (n - command_tile::min_kind) / 2;
std::size_t col = (n - command_tile::min_kind) % 2;
double x = xcol[col];
double y = (row + .5) * tile_display_args_.command_point_size + bounds_.y1;
for (std::size_t k = 0; k < tiles.size(); ++k) {
layout_extents e;
e.x = x;
e.y = y + (tiles.size() - k - 1) * 5.0;
e.width = tile_display_args_.command_point_size;
e.top = -tile_display_args_.command_point_size * .5;
e.bottom = +tile_display_args_.command_point_size * .5;
layout[tiles[k].get()] = e;
}
}
for (const auto& pile : piles_) {
for (const auto& tile : pile.second) {
const layout_extents& e = layout[tile.get()];
if (warp) {
tile->move_to(e.x, e.y);
} else {
tile->accelerate_to(e.x, e.y);
}
}
}
}
void
command_tile_repository::reset()
{
piles_.clear();
locked_ = false;
}
std::unique_ptr<command_tile_drag>
command_tile_repository::drag_begin(double x, double y)
{
if (locked_) {
return {};
}
std::size_t column = (x - bounds_.x1) / (.5 * (bounds_.x2 - bounds_.x1));
std::size_t row = (y - bounds_.y1) / tile_display_args_.command_point_size;
if (column < 0 || column > 1) {
return {};
}
std::size_t index = column + 2 * row + command_tile::min_kind;
if (index > command_tile::max_kind) {
return {};
}
auto & pile = piles_[static_cast<command_tile::kind_t>(index)];
if (pile.empty()) {
return {};
}
std::unique_ptr<command_tile> tile = std::move(pile.back());
pile.pop_back();
rearrange(false);
std::unique_ptr<command_tile_repository_drag> drag(new command_tile_repository_drag(this, std::move(tile), tile_display_args_));
drag->move(x, y);
return std::move(drag);
}
void
command_tile_repository::drag_hover_enter(double x, double y, command_tile_drag * drag)
{
}
void
command_tile_repository::drag_hover_leave(command_tile_drag * drag)
{
}
void
command_tile_repository::drag_finish(double x, double y, std::unique_ptr<command_tile_drag> drag)
{
put_internal(drag->consume());
rearrange(false);
}
void
command_tile_repository::redraw(double global_phase)
{
glColor4f(.2, .3, .1, .2);
texture_generator::make_tex_quad2d(texid_solid,
bounds_.x1, bounds_.y1,
bounds_.x2, bounds_.y1,
bounds_.x2, bounds_.y2,
bounds_.x1, bounds_.y2);
for (const auto & pile : piles_) {
for (const auto & tile : pile.second) {
tile->draw(global_phase, tile_display_args_);
}
}
}
void
command_tile_repository::set_bounds(double x1, double y1, double x2, double y2)
{
bounds_.x1 = x1;
bounds_.y1 = y1;
bounds_.x2 = x2;
bounds_.y2 = y2;
rearrange(true);
}
void
command_tile_repository::configure(double x1, double y1, double x2, double y2, tile_display_args_t tile_display_args)
{
tile_display_args_ = tile_display_args;
set_bounds(x1, y1, x2, y2);
}
const command_tile_repository::bounds_t &
command_tile_repository::bounds() const noexcept
{
return bounds_;
}
void
command_tile_repository::animate(double now)
{
for (auto & pile : piles_) {
for (const auto & tile : pile.second) {
tile->animate(now);
}
}
}
void
command_tile_repository::append(std::unique_ptr<command_tile> tile)
{
put_internal(std::move(tile));
rearrange(false);
}
void
command_tile_repository::put_internal(std::unique_ptr<command_point> cpt)
{
for (std::size_t n = 0; n < cpt->num_branches(); ++n) {
put_internal(std::move(cpt->branch(n)));
}
put_internal(std::move(*cpt).extract_tile());
}
void
command_tile_repository::put_internal(command_sequence seq)
{
for (auto& point : seq) {
put_internal(std::move(point));
}
}
void
command_tile_repository::put_internal(std::unique_ptr<command_tile> tile)
{
auto& pile = piles_[tile->kind()];
pile.push_back(std::move(tile));
}
void
command_tile_repository::set_locked(bool locked)
{
locked_ = locked;
}