forked from tokenok/D2Ex2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExControlManager.cpp
525 lines (467 loc) · 12.7 KB
/
ExControlManager.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
/*==========================================================
* D2Ex2
* https://github.com/lolet/D2Ex2
* ==========================================================
* Copyright (c) 2011-2015 Bartosz Jankowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==========================================================
*/
#include "stdAfx.h"
#include "ExControlManager.h"
#include "ExTextBox.h"
#include "ExWindow.h"
#include "ExImage.h"
#include "ExButton.h"
#include "ExBuffs.h"
ExControlManager::ExControlManager() : lastControlId(0), stop_observer(false)
{
DEBUGMSG("Initialized ExControlManager!")
}
ExControlManager::~ExControlManager() {
stop();
controls.clear();
DEBUGMSG("Destroyed ExControlManager")
}
void ExControlManager::start() {
observer = thread(&ExControlManager::consume, this);
}
void ExControlManager::stop() {
if (!stop_observer) {
{
lock_guard<mutex> sync(lock);
stop_observer = true;
employee.notify_one();
}
observer.join();
}
}
exId ExControlManager::add(ExControl* control)
{
exId newId = ++lastControlId;
if (lastControlId == exnull_t) {
DEBUGMSG("Rollbacking control ids to 1 because limit has exceeded")
newId = lastControlId = 1;
}
ExMessageAddControl *msg = new ExMessageAddControl(newId, control);
sendRequest(msg);
return newId;
}
void ExControlManager::remove(const exId controlId)
{
ExMessageRemoveControl* msg = new ExMessageRemoveControl(controlId);
sendRequest(msg);
}
void ExControlManager::setHooverText(exId controlId, wstring text)
{
ExMessageHooverText* msg = new ExMessageHooverText(controlId, text);
sendRequest(msg);
}
void ExControlManager::setColor(exId controlId, unsigned int color)
{
ExMessageColor* msg = new ExMessageColor(controlId, color);
sendRequest(msg);
}
void ExControlManager::setHooverable(exId controlId, bool howset)
{
ExMessageHooverable * msg = new ExMessageHooverable(controlId, howset);
sendRequest(msg);
}
// Additional messages
void ExControlManager::setFrame(exId controlId, unsigned int frame)
{
ExMessageFrame* msg = new ExMessageFrame(controlId, frame);
sendRequest(msg);
}
void ExControlManager::setX(exId controlId, int x)
{
ExMessagePos* msg = new ExMessagePos(controlId, x, exnull_t);
sendRequest(msg);
}
void ExControlManager::setY(exId controlId, int y)
{
ExMessagePos* msg = new ExMessagePos(controlId, exnull_t, y);
sendRequest(msg);
}
void ExControlManager::move(exId controlId, int x, int y)
{
ExMessagePos* msg = new ExMessagePos(controlId, x, y);
sendRequest(msg);
}
void ExControlManager::resize(exId controlId, int width, int height)
{
ExMessageSize* msg = new ExMessageSize(controlId, width, height);
sendRequest(msg);
}
void ExControlManager::setText(exId controlId, string text)
{
ExMessageText* msg = new ExMessageText(controlId, text);
sendRequest(msg);
}
void ExControlManager::setText(exId controlId, wstring text)
{
ExMessageWideText* msg = new ExMessageWideText(controlId, text);
sendRequest(msg);
}
void ExControlManager::setMoveable(exId controlId, bool howset)
{
ExMessageMoveable * msg = new ExMessageMoveable(controlId, howset);
sendRequest(msg);
}
void ExControlManager::setState(exId controlId, ExControl::States state)
{
ExMessageState * msg = new ExMessageState(controlId, state);
sendRequest(msg);
}
void ExControlManager::setChild(exId controlId, exId childId, bool what)
{
ExMessageChildIssue * msg = new ExMessageChildIssue(controlId, childId, what);
sendRequest(msg);
}
void ExControlManager::setParent(exId controlId, exId parentId, bool what)
{
ExMessageParentIssue * msg = new ExMessageParentIssue(controlId, parentId, what);
sendRequest(msg);
}
void ExControlManager::setAlign(exId controlId, ExControl::Align x, ExControl::Align y)
{
ExMessageAlign * msg = new ExMessageAlign(controlId, x, y);
sendRequest(msg);
}
void ExControlManager::setTransparency(exId controlId, D2DrawModes transLvl)
{
ExMessageTransparency * msg = new ExMessageTransparency(controlId, transLvl);
sendRequest(msg);
}
control_it ExControlManager::find(const exId controlId)
{
for (control_it c = controls.begin(); c != controls.end(); ++c)
{
if ((*c)->id == controlId)
return c;
}
return controls.end();
}
control_const_it ExControlManager::find_ro(const exId controlId)
{
for (control_const_it c = controls.cbegin(); c != controls.cend(); ++c)
{
if ((*c)->id == controlId)
return c;
}
return controls.cend();
}
string ExControlManager::getType(exId controlId)
{
control_const_it c = find_ro(controlId);
if (c == controls.cend()) {
DEBUGMSG("Failed to find the control id %d!", controlId)
return "exnull_t";
}
return typeid(*c->get()).name();
}
int ExControlManager::getX(exId controlId)
{
control_const_it c = find_ro(controlId);
if (c == controls.cend()) {
DEBUGMSG("Failed to find the control id %d!", controlId)
return exnull_t;
}
return c->get()->GetX();
}
int ExControlManager::getY(exId controlId)
{
control_const_it c = find_ro(controlId);
if (c == controls.cend()) {
DEBUGMSG("Failed to find the control id %d!", controlId)
return exnull_t;
}
return c->get()->GetY();
}
int ExControlManager::getWidth(exId controlId)
{
control_const_it c = find_ro(controlId);
if (c == controls.cend()) {
DEBUGMSG("Failed to find the control id %d!", controlId)
return exnull_t;
}
return c->get()->GetWidth();
}
int ExControlManager::getHeight(exId controlId)
{
control_const_it c = find_ro(controlId);
if (c == controls.cend()) {
DEBUGMSG("Failed to find the control id %d!", controlId)
return exnull_t;
}
return c->get()->GetHeight();
}
void ExControlManager::sendRequest(ExMessage* msg)
{
if (msg->id == exnull_t) {
DEBUGMSG("Message %d was not send because the destination control was null", msg->type)
delete msg;
return;
}
{
lock_guard<mutex> sync(lock);
msg_queue.push(msg);
}
employee.notify_one();
}
bool ExControlManager::consume_once()
{
if (msg_queue.empty())
return false;
ExMessage *msg = msg_queue.front(); // try to consume in next iteration
switch (msg->type) {
case EXMSG_ADDCONTROL:
{
ExMessageAddControl* exmsg = (ExMessageAddControl*)msg;
exmsg->control->id = exmsg->id; // set control uuid
controls.emplace_back(unique_ptr<ExControl>(exmsg->control));
}
break;
case EXMSG_REMCONTROL:
{
ExMessageRemoveControl* exmsg = (ExMessageRemoveControl*)msg;
controls.erase(find(exmsg->id));
}
break;
case EXMSG_POS:
{
ExMessagePos* exmsg = (ExMessagePos*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to set control position %d id", exmsg->id)
break;
}
if (exmsg->x != exnull_t)
c->get()->SetX(exmsg->x);
if (exmsg->y != exnull_t)
c->get()->SetY(exmsg->y);
}
break;
case EXMSG_SIZE:
{
ExMessageSize* exmsg = (ExMessageSize*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to resize control with %d id", exmsg->id)
break;
}
if (exmsg->width != exnull_t)
c->get()->SetWidth(exmsg->width);
if (exmsg->height != exnull_t)
c->get()->SetHeight(exmsg->height);
}
break;
case EXMSG_TEXT:
{
ExMessageText* exmsg = (ExMessageText*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to set control text to %s with %d id", exmsg->text.c_str(), exmsg->id)
break;
}
ExTextBox* textbox = dynamic_cast<ExTextBox*>(c->get());
if (!textbox) {
DEBUGMSG("EXMSG_TEXT is not available for %s!", typeid(*c->get()).name())
break;
}
textbox->SetText(exmsg->text);
}
break;
case EXMSG_WIDETEXT:
{
ExMessageWideText* exmsg = (ExMessageWideText*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG(L"Failed to set control text to %s with %d id", exmsg->text.c_str(), exmsg->id)
break;
}
c->get()->SetText(exmsg->text);
}
break;
case EXMSG_CHILDISSUE:
{
ExMessageChildIssue* exmsg = (ExMessageChildIssue*)msg;
control_const_it parent = find_ro(exmsg->id); // parent
if (parent == controls.end()) {
DEBUGMSG("Failed to find parent control with %d id", exmsg->id)
break;
}
ExWindow* window = dynamic_cast<ExWindow*>(parent->get());
if (!window) {
DEBUGMSG("EXMSG_CHILDISSUE is not available for %s!", typeid(*parent->get()).name())
break;
}
control_const_it child = find_ro(exmsg->relative); // parent
if (child == controls.end()) {
DEBUGMSG("Failed to find child control with %d id", exmsg->id)
break;
}
if (exmsg->type)
window->AddChild(child->get());
else
window->DeleteChild(child->get());
//TODO: Add as parent
}
break;;
case EXMSG_MOVEABLE:
{
ExMessageMoveable* exmsg = (ExMessageMoveable*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change moveable attribute for control with %d id", exmsg->id)
break;
}
c->get()->SetMoveable(exmsg->howset);
}
break;
case EXMSG_HOOVERABLE:
{
ExMessageHooverable* exmsg = (ExMessageHooverable*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change hooverable attribute for control with %d id", exmsg->id)
break;
}
ExTextBox* textbox = dynamic_cast<ExTextBox*>(c->get());
if (!textbox) {
DEBUGMSG("EXMSG_HOOVERABLE is not available for %s!", typeid(*c->get()).name())
break;
}
textbox->SetHooverable(exmsg->howset);
}
break;
case EXMSG_STATE:
{
ExMessageState* exmsg = (ExMessageState*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change moveable attribute for control with %d id", exmsg->id)
break;
}
c->get()->SetState(exmsg->state);
}
break;
case EXMSG_ALIGN:
{
ExMessageAlign* exmsg = (ExMessageAlign*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change moveable attribute for control with %d id", exmsg->id)
break;
}
c->get()->SetAlign(exmsg->x, exmsg->y);
}
break;
case EXMSG_FRAME:
{
ExMessageFrame* exmsg = (ExMessageFrame*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change frame attribute for control with %d id", exmsg->id)
break;
}
ExButton* button = dynamic_cast<ExButton*>(c->get());
if (!button) {
DEBUGMSG("EXMSG_FRAME is not available for %s!", typeid(*c->get()).name())
break;
}
button->SetFrame(exmsg->frame);
}
break;
case EXMSG_COLOR:
{
ExMessageColor* exmsg = (ExMessageColor*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG("Failed to change frame attribute for control with %d id", exmsg->id)
break;
}
c->get()->SetColor(exmsg->color);
}
break;
case EXMSG_HOOVERTEXT:
{
ExMessageHooverText* exmsg = (ExMessageHooverText*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG(L"Failed to set control hoover text to %s with %d id", exmsg->text.c_str(), exmsg->id)
break;
}
c->get()->SetHooverText(exmsg->text);
}
break;
case EXMSG_TRANSPARENCY:
{
ExMessageTransparency* exmsg = (ExMessageTransparency*)msg;
control_const_it c = find_ro(exmsg->id);
if (c == controls.cend()) {
DEBUGMSG(L"Failed to set transparency to control with %d id", exmsg->id)
break;
}
c->get()->SetTransLvl(exmsg->nTransLvl);
}
break;
case EXMSG_DUMMY:
{
DEBUGMSG("Invalid ExMessage!")
}
break;
default:
DEBUGMSG("Unknown message to eat! (%d)", msg->type)
};
msg_queue.pop();
delete msg;
return true;
}
void ExControlManager::consume()
{
while (!stop_observer) {
unique_lock<mutex> sync(lock);
employee.wait(sync, [&]{ return stop_observer || !msg_queue.empty(); });
consume_once();
// for process()
sync.unlock();
employee.notify_one();
}
if (stop_observer)
{
while (!msg_queue.empty()) { delete msg_queue.front(); msg_queue.pop(); };
DEBUGMSG("Stopping due to stop_observer request")
return;
}
}
void ExControlManager::process()
{
unique_lock<mutex> sync(lock);
employee.wait(sync, [&]{ return msg_queue.empty(); });
}
void ExControlManager::draw()
{
for (auto &c = controls.cbegin(); c != controls.cend(); ++c) {
c->get()->Draw();
}
}
bool ExControlManager::io(UINT uMsg, WPARAM wParam)
{
bool block_msgs = false;
for (auto &c = controls.cbegin(); c != controls.cend(); ++c) {
if (c->get()->isPressed(uMsg, wParam))
block_msgs = true;
}
return block_msgs;
}