-
Notifications
You must be signed in to change notification settings - Fork 2
/
EMsg.h
99 lines (85 loc) · 1.91 KB
/
EMsg.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
#ifndef _E_MSG_H_
#define _E_MSG_H_
#include <functional>
#include "ESignal.h"
#include "ETypes.h"
namespace Eg {
enum EMsgType {
EMSG_CLOSE,
EMSG_RESIZE,
EMSG_MOUSE
};
enum EMouseMsgType {
EMSG_MOUSE_MOVE
};
struct ECloseMsg;
struct EResizeMsg;
struct EMouseMoveMsg;
struct EWindowInterface {
virtual ~EWindowInterface() {}
virtual void windowClose(ECloseMsg* msg) = 0;
virtual void windowResize(EResizeMsg* msg) = 0;
virtual void mouseMove(EMouseMoveMsg* msg) = 0;
virtual EPointi pos() const = 0;
};
struct EMsgBase {
Signal<bool> answer;
std::function<void(bool)> onAnswer;
virtual ~EMsgBase() {}
void setAnswer(std::function<void(bool)> f) {
onAnswer = f;
}
void accept() {
onAnswer(true);
answer.emit(true);
}
void ignore() {
onAnswer(false);
answer.emit(false);
}
virtual void give(EWindowInterface*) = 0;
virtual EMsgType type() const = 0;
};
struct ECloseMsg : public EMsgBase {
virtual void give(EWindowInterface* w) {
w->windowClose(this);
}
virtual EMsgType type() const override {
return EMSG_CLOSE;
}
};
struct EResizeMsg : public EMsgBase {
EPointi size;
EResizeMsg(int w, int h) : size(w, h) {}
virtual void give(EWindowInterface* w) {
w->windowResize(this);
}
virtual EMsgType type() const override {
return EMSG_RESIZE;
}
};
struct EMouseMsg : public EMsgBase {
EPointi pos;
EMouseMsg(int x, int y) : pos(x, y) {}
virtual EMsgType type() const override {
return EMSG_MOUSE;
}
virtual EMouseMsgType msgType() const = 0;
void attach(EWindowInterface* w) {
pos -= w->pos();
}
void detach(EWindowInterface* w) {
pos += w->pos();
}
};
struct EMouseMoveMsg : public EMouseMsg {
EMouseMoveMsg(int x, int y) : EMouseMsg(x, y) {}
virtual void give(EWindowInterface* w) {
w->mouseMove(this);
}
virtual EMouseMsgType msgType() const override {
return EMSG_MOUSE_MOVE;
}
};
}
#endif