Translated using Weblate (Chinese (Simplified))
[oweals/minetest.git] / src / client / event_manager.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "event.h"
23 #include <list>
24 #include <map>
25
26 class EventManager : public MtEventManager
27 {
28         static void receiverReceive(MtEvent *e, void *data)
29         {
30                 MtEventReceiver *r = (MtEventReceiver *)data;
31                 r->onEvent(e);
32         }
33         struct FuncSpec
34         {
35                 event_receive_func f;
36                 void *d;
37                 FuncSpec(event_receive_func f, void *d) : f(f), d(d) {}
38         };
39
40         struct Dest
41         {
42                 std::list<FuncSpec> funcs{};
43         };
44         std::map<MtEvent::Type, Dest> m_dest{};
45
46 public:
47         ~EventManager() override = default;
48
49         void put(MtEvent *e) override
50         {
51                 std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(e->getType());
52                 if (i != m_dest.end()) {
53                         std::list<FuncSpec> &funcs = i->second.funcs;
54                         for (FuncSpec &func : funcs) {
55                                 (*(func.f))(e, func.d);
56                         }
57                 }
58                 delete e;
59         }
60         void reg(MtEvent::Type type, event_receive_func f, void *data) override
61         {
62                 std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
63                 if (i != m_dest.end()) {
64                         i->second.funcs.emplace_back(f, data);
65                 } else {
66                         Dest dest;
67                         dest.funcs.emplace_back(f, data);
68                         m_dest[type] = dest;
69                 }
70         }
71         void dereg(MtEvent::Type type, event_receive_func f, void *data) override
72         {
73                 std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
74                 if (i != m_dest.end()) {
75                         std::list<FuncSpec> &funcs = i->second.funcs;
76                         auto j = funcs.begin();
77                         while (j != funcs.end()) {
78                                 bool remove = (j->f == f && (!data || j->d == data));
79                                 if (remove)
80                                         funcs.erase(j++);
81                                 else
82                                         ++j;
83                         }
84                 }
85         }
86 };