-
Notifications
You must be signed in to change notification settings - Fork 26
/
event_manager.h
112 lines (92 loc) · 3.68 KB
/
event_manager.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
#pragma once
#include "dfhack_shared.h"
#include <functional>
#include "df/interface_key.h"
// from dfplex
struct Client;
struct ClientUpdateInfo;
struct OnupdateCallback
{
std::function<bool(color_ostream &)> callback;
int32_t ticklimit;
int32_t minyear;
int32_t minyeartick;
std::string description;
bool hasTickLimit;
OnupdateCallback(const std::string & descr, std::function<bool(color_ostream &)> cb);
OnupdateCallback(const std::string & descr, std::function<bool(color_ostream &)> cb, int32_t tl, int32_t initdelay = 0);
bool check_run(color_ostream & out, int32_t year, int32_t yeartick);
};
struct OnstatechangeCallback
{
std::function<bool(color_ostream &, state_change_event)> cb;
std::string description;
OnstatechangeCallback(const std::string & descr, std::function<bool(color_ostream &, state_change_event)> cb);
};
class ExclusiveCallback;
struct EventManager
{
public:
EventManager();
~EventManager();
OnupdateCallback *onupdate_register(const std::string & descr, int32_t ticklimit, int32_t initialtickdelay, std::function<void(color_ostream &)> b);
OnupdateCallback *onupdate_register_once(const std::string & descr, int32_t ticklimit, int32_t initialtickdelay, std::function<bool(color_ostream &)> b);
OnupdateCallback *onupdate_register_once(const std::string & descr, int32_t ticklimit, std::function<bool(color_ostream &)> b);
OnupdateCallback *onupdate_register_once(const std::string & descr, std::function<bool(color_ostream &)> b);
void onupdate_unregister(OnupdateCallback *&b);
OnstatechangeCallback *onstatechange_register(const std::string & descr, std::function<void(color_ostream &, state_change_event)> b);
OnstatechangeCallback *onstatechange_register_once(const std::string & descr, std::function<bool(color_ostream &, state_change_event)> b);
void onstatechange_unregister(OnstatechangeCallback *&b);
void create_dfplex_client();
void remove_dfplex_client();
bool register_exclusive(std::unique_ptr<ExclusiveCallback> && cb, bool force = false);
void queue_exclusive(std::unique_ptr<ExclusiveCallback> && cb);
inline bool has_exclusive() const { return exclusive != nullptr; }
template<typename E>
inline bool each_exclusive(std::function<bool(const E *)> fn) const
{
if (auto e = dynamic_cast<E *>(exclusive.get()))
{
if (fn(e))
{
return true;
}
}
for (auto & queued : exclusive_queue)
{
if (auto e = dynamic_cast<E *>(queued.get()))
{
if (fn(e))
{
return true;
}
}
}
return false;
}
template<typename E>
inline bool has_exclusive(bool allow_queued = false) const
{
if (!allow_queued)
{
return dynamic_cast<E *>(exclusive.get()) != nullptr;
}
return each_exclusive<E>([](const E *) -> bool { return true; });
}
std::string status();
void report(std::ostream & out, bool html);
void onstatechange(color_ostream & out, state_change_event event);
void onupdate(color_ostream & out, const std::function<void(std::vector<df::interface_key> &)> & send_keys);
bool is_client();
private:
friend class AI;
friend class Camera;
void clear();
std::unique_ptr<ExclusiveCallback> exclusive;
std::unique_ptr<ExclusiveCallback> delay_delete_exclusive;
std::list<std::unique_ptr<ExclusiveCallback>> exclusive_queue;
std::vector<OnupdateCallback *> onupdate_list;
std::vector<OnstatechangeCallback *> onstatechange_list;
Client *dfplex_client;
};
extern EventManager events;