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
|
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// scriptdelegate -- manages function delegate
#include "../qcommon/listener.h"
#include "../qcommon/delegate.h"
#include "gamescript.h"
class ScriptRegisteredDelegate
{
public:
void Execute(Listener *object, const Event& ev);
};
/**
* Registered delegate, for scripts.
* It contains a ScriptThreadLabel with the game script and the label to execute.
*/
class ScriptRegisteredDelegate_Script : public ScriptRegisteredDelegate
{
public:
ScriptRegisteredDelegate_Script(const ScriptThreadLabel& inLabel);
ScriptVariable Execute(Listener *object, const Event& ev);
bool operator==(const ScriptRegisteredDelegate_Script& registeredDelegate) const;
private:
ScriptThreadLabel label;
};
/**
* Registered delegate, for code use.
* It contains the function to execute.
*/
class ScriptRegisteredDelegate_Code : public ScriptRegisteredDelegate
{
public:
using DelegateResponse = void (*)(Listener *object, const Event& ev);
public:
ScriptRegisteredDelegate_Code(DelegateResponse inResponse);
void Execute(Listener *object, const Event& ev);
bool operator==(const ScriptRegisteredDelegate_Code& registeredDelegate) const;
private:
DelegateResponse response;
};
/**
* Registered delegate, for code use.
* It contains the object along the member function to execute.
* The function will not be executed if the object is NULL.
*/
class ScriptRegisteredDelegate_CodeMember : public ScriptRegisteredDelegate
{
public:
using DelegateClassResponse = void (Class::*)(Listener *object, const Event& ev);
public:
ScriptRegisteredDelegate_CodeMember(Class *inObject, DelegateClassResponse inResponse);
void Execute(Listener *object, const Event& ev);
bool operator==(const ScriptRegisteredDelegate_CodeMember& registeredDelegate) const;
private:
SafePtr<Class> object;
DelegateClassResponse response;
};
/**
* A script delegate provides a way for code to subscribe for events.
* Scripts and code can register for a delegate and have their function executed
* when the delegate gets triggered.
*/
class ScriptDelegate
{
public:
ScriptDelegate(const char *name, const char *description);
~ScriptDelegate();
static const ScriptDelegate *GetRoot();
const ScriptDelegate *GetNext() const;
/**
* Register a script label.
*
* @param label The label to be executed
*/
void Register(const ScriptThreadLabel& label);
/**
* Unregistered the label.
*
* @param label The label to unregister
*/
void Unregister(const ScriptThreadLabel& label);
/**
* Register a function.
*
* @param response The function to be executed
*/
void Register(ScriptRegisteredDelegate_Code::DelegateResponse response);
/**
* Unregistered the function.
*
* @param response the function to unregister
*/
void Unregister(ScriptRegisteredDelegate_Code::DelegateResponse response);
/**
* Register with an object and a member function.
*
* @param object The object to notify
* @param response The member function of the object to be executed
*/
void Register(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response);
/**
* Unregistered the member function.
*
* @param object The object where the member function is
* @param response The member function to unregister
*/
void Unregister(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response);
/**
* Executes all registered delegates with the specified event.
*
* @param ev Parameter list
*/
ScriptVariable Trigger(const Event& ev = Event()) const;
/**
* Executes all registered delegates with the specified event.
*
* @param ev Parameter list
*/
ScriptVariable Trigger(Listener *object, const Event& ev = Event()) const;
/**
* Reset the delegate, unregister callbacks.
*/
void Reset();
/**
* Search and return the specified script delegate by name.
*
* @param name The name to search for
*/
static ScriptDelegate *GetScriptDelegate(const char *name);
static void ResetAllDelegates();
// non-movable and non-copyable
ScriptDelegate(ScriptDelegate&& other) = delete;
ScriptDelegate& operator=(ScriptDelegate&& other) = delete;
ScriptDelegate(const ScriptDelegate& other) = delete;
ScriptDelegate& operator=(const ScriptDelegate& other) = delete;
private:
// Linked-list
ScriptDelegate *next;
ScriptDelegate *prev;
static ScriptDelegate *root;
const char *name;
const char *description;
Container<ScriptRegisteredDelegate_Script> list_script;
Container<ScriptRegisteredDelegate_Code> list_code;
Container<ScriptRegisteredDelegate_CodeMember> list_codeMember;
};
|