-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathExControlManager.h
222 lines (181 loc) · 6.68 KB
/
ExControlManager.h
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
/*==========================================================
* 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.
* ==========================================================
*/
#ifndef __EXCONTROLMANAGER_H__
#define __EXCONTROLMANAGER_H__
#include "ExControl.h"
#include <list>
#include <queue>
#include <memory>
#include <mutex>
#include <thread>
#include <chrono>
#include <atomic>
#include <condition_variable>
typedef DWORD exId;
const exId exnull_t = -1337;
enum ExMsgs
{
EXMSG_DUMMY,
EXMSG_ADDCONTROL,
EXMSG_REMCONTROL,
EXMSG_POS,
EXMSG_SIZE,
EXMSG_TEXT,
EXMSG_WIDETEXT,
EXMSG_MOVEABLE,
EXMSG_STATE,
EXMSG_CHILDISSUE,
EXMSG_PARENTISSUE,
EXMSG_ALIGN,
EXMSG_FRAME,
EXMSG_HOOVERTEXT,
EXMSG_HOOVERABLE,
EXMSG_COLOR,
EXMSG_TRANSPARENCY,
};
struct ExMessage {
exId id;
exId type;
ExMessage(exId id) { type = EXMSG_DUMMY; this->id = id; };
};
struct ExMessageAddControl : ExMessage {
ExControl* control;
ExMessageAddControl(exId id, ExControl* control) : ExMessage(id) { type = EXMSG_ADDCONTROL; this->control = control;};
};
struct ExMessageRemoveControl : ExMessage {
ExMessageRemoveControl(exId id) : ExMessage(id) { type = EXMSG_REMCONTROL; };
};
struct ExMessagePos : ExMessage {
int x;
int y;
ExMessagePos(exId id, int x, int y) : ExMessage(id) { type = EXMSG_POS; this->x = x; this->y = y; };
};
struct ExMessageSize : ExMessage {
int width;
int height;
ExMessageSize(exId id, int width, int height) : ExMessage(id) { type = EXMSG_SIZE; this->width = width; this->height = height; };
};
struct ExMessageText : ExMessage {
string text;
ExMessageText(exId id, string& text) : ExMessage(id) { type = EXMSG_TEXT; this->text = text; };
};
struct ExMessageWideText : ExMessage {
wstring text;
ExMessageWideText(exId id, wstring& text) : ExMessage(id) { type = EXMSG_WIDETEXT; this->text = text; };
};
struct ExMessageHooverText : ExMessage {
wstring text;
ExMessageHooverText(exId id, wstring& text) : ExMessage(id) { type = EXMSG_HOOVERTEXT; this->text = text; };
};
struct ExMessageMoveable : ExMessage {
bool howset;
ExMessageMoveable(exId id, bool howset) : ExMessage(id) { type = EXMSG_MOVEABLE; this->howset = howset; }
};
struct ExMessageHooverable : ExMessage {
bool howset;
ExMessageHooverable(exId id, bool howset) : ExMessage(id) { type = EXMSG_HOOVERABLE; this->howset = howset; }
};
struct ExMessageState : ExMessage {
ExControl::States state;
ExMessageState(exId id, ExControl::States state) : ExMessage(id) { type = EXMSG_STATE; this->state = state; }
};
struct ExMessageChildIssue : ExMessage {
bool add; // if true then child is added
exId relative;
ExMessageChildIssue(exId id, exId relative, bool add) : ExMessage(id) { type = EXMSG_CHILDISSUE; this->add = add; this->relative = relative; }
};
struct ExMessageParentIssue : ExMessage {
bool add; // if true then parent is set
exId relative;
ExMessageParentIssue(exId id, exId relative, bool add) : ExMessage(id) { type = EXMSG_PARENTISSUE; this->add = add; this->relative = relative; }
};
struct ExMessageAlign : ExMessage {
ExControl::Align x;
ExControl::Align y;
ExMessageAlign(exId id, ExControl::Align x, ExControl::Align y) : ExMessage(id) { type = EXMSG_ALIGN; this->x = x; this->y = y; };
};
struct ExMessageFrame: ExMessage {
unsigned int frame;
ExMessageFrame(exId id, unsigned int frame) : ExMessage(id) { type = EXMSG_FRAME; this->frame = frame; }
};
struct ExMessageColor : ExMessage {
unsigned int color;
ExMessageColor(exId id, unsigned int color) : ExMessage(id) { type = EXMSG_COLOR; this->color = color; }
};
struct ExMessageTransparency : ExMessage {
D2DrawModes nTransLvl;
ExMessageTransparency(exId id, D2DrawModes nTransLvl) : ExMessage(id) { type = EXMSG_TRANSPARENCY; this->nTransLvl = nTransLvl; }
};
typedef list<unique_ptr<ExControl>> control_t;
typedef control_t::iterator control_it;
typedef control_t::const_iterator control_const_it;
typedef queue<ExMessage*> exmsgqueue_t;
class ExControlManager {
public:
ExControlManager();
~ExControlManager();
void start(); // Start the observer thread
void stop();
// Creation/deletion
exId add(ExControl* control);
void remove(const exId controlId);
// Modify parameters of controls
void setX(exId controlId, int x);
void setY(exId controlId, int y);
void move(exId controlId, int x, int y);
void resize(exId controlId, int width, int height);
void setText(exId controlId, string text);
void setText(exId controlId, wstring text);
void setHooverText(exId controlId, wstring text);
void setColor(exId controlId, unsigned int color);
// Additional messages
void setFrame(exId controlId, unsigned int frame);
void setAlign(exId controlId, ExControl::Align xAlign, ExControl::Align yAlign);
void setMoveable(exId controlId, bool howset);
void setHooverable(exId controlId, bool howset);
void setState(exId controlId, ExControl::States state);
void setChild(exId controlId, exId childId, bool what); // if what is true then child is added, otherwise it's removed
void setParent(exId controlId, exId parentId, bool what); // if what is true then parent is added, otherwise it's removed
void setTransparency(exId controlId, D2DrawModes transLvl);
// Getters (block observer thread)
string getType(exId controlId); // return typeid raw_string with control class name
int getX(exId controlId);
int getY(exId controlId);
int getWidth(exId controlId);
int getHeight(exId controlId);
void process(); // similar to WINAPI's ProcessMessages()
// Drawing and input processing are done in the same thread, so no need to lock!
void draw(); // Execute Draw() on each control
bool io(UINT uMsg, WPARAM wParam); // Execute isPressed() on each control, return true is event happened
private:
bool consume_once();
void consume(); // Take the messages from the queue and parses them
void sendRequest(ExMessage* msg); // Add a message to the queue, will be consumed by consume()
control_it find(const exId controlId);
control_const_it find_ro(const exId controlId);
mutex lock;
thread observer;
control_t controls;
exmsgqueue_t msg_queue;
atomic<exId> lastControlId;
condition_variable employee;
atomic<bool> stop_observer;
};
#endif