forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventHandler.h
More file actions
116 lines (91 loc) · 3.33 KB
/
eventHandler.h
File metadata and controls
116 lines (91 loc) · 3.33 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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file eventHandler.h
* @author drose
* @date 1999-02-08
*/
#ifndef EVENTHANDLER_H
#define EVENTHANDLER_H
#include "pandabase.h"
#include "event.h"
#include "pt_Event.h"
#include "asyncFuture.h"
#include "pset.h"
#include "pmap.h"
class EventQueue;
/**
* A class to monitor events from the C++ side of things. It maintains a set
* of "hooks", function pointers assigned to event names, and calls the
* appropriate hooks when the matching event is detected.
*
* This class is not necessary when the hooks are detected and processed
* entirely by the scripting language, e.g. via Scheme hooks or the messenger
* in Python.
*/
class EXPCL_PANDA_EVENT EventHandler : public TypedObject {
public:
// Define a function type suitable for receiving events.
typedef void EventFunction(const Event *);
typedef void EventCallbackFunction(const Event *, void *);
PUBLISHED:
explicit EventHandler(EventQueue *ev_queue);
~EventHandler() {}
AsyncFuture *get_future(const std::string &event_name);
void process_events();
virtual void dispatch_event(const Event *event);
void write(std::ostream &out) const;
INLINE static EventHandler *get_global_event_handler(EventQueue *queue = nullptr);
public:
bool add_hook(const std::string &event_name, EventFunction *function);
bool add_hook(const std::string &event_name, EventCallbackFunction *function,
void *data);
bool has_hook(const std::string &event_name) const;
bool has_hook(const std::string &event_name, EventFunction *function) const;
bool has_hook(const std::string &event_name, EventCallbackFunction *function,
void *data) const;
bool remove_hook(const std::string &event_name, EventFunction *function);
bool remove_hook(const std::string &event_name, EventCallbackFunction *function,
void *data);
bool remove_hooks(const std::string &event_name);
bool remove_hooks_with(void *data);
void remove_all_hooks();
protected:
typedef pset<EventFunction *> Functions;
typedef pmap<std::string, Functions> Hooks;
typedef std::pair<EventCallbackFunction*, void*> CallbackFunction;
typedef pset<CallbackFunction> CallbackFunctions;
typedef pmap<std::string, CallbackFunctions> CallbackHooks;
typedef pmap<std::string, PT(AsyncFuture)> Futures;
Hooks _hooks;
CallbackHooks _cbhooks;
Futures _futures;
EventQueue &_queue;
static EventHandler *_global_event_handler;
static void make_global_event_handler();
private:
void write_hook(std::ostream &out, const Hooks::value_type &hook) const;
void write_cbhook(std::ostream &out, const CallbackHooks::value_type &hook) const;
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
TypedObject::init_type();
register_type(_type_handle, "EventHandler",
TypedObject::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
};
#include "eventHandler.I"
#endif