From f1651856a8342cf680e22075cbffecc22e58049a Mon Sep 17 00:00:00 2001 From: Mestima Date: Sat, 14 May 2022 05:39:04 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ SCsub | 5 ++++ config.py | 7 ++++++ hook.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ hook.h | 29 +++++++++++++++++++++++ register_types.cpp | 14 +++++++++++ register_types.h | 5 ++++ 7 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 SCsub create mode 100644 config.py create mode 100644 hook.cpp create mode 100644 hook.h create mode 100644 register_types.cpp create mode 100644 register_types.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36fb54a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/__pycache__ +*.obj \ No newline at end of file diff --git a/SCsub b/SCsub new file mode 100644 index 0000000..3c12e73 --- /dev/null +++ b/SCsub @@ -0,0 +1,5 @@ +# SCsub + +Import('env') + +env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..c1c0259 --- /dev/null +++ b/config.py @@ -0,0 +1,7 @@ +# config.py + +def can_build(env, platform): + return True + +def configure(env): + pass \ No newline at end of file diff --git a/hook.cpp b/hook.cpp new file mode 100644 index 0000000..1d9b601 --- /dev/null +++ b/hook.cpp @@ -0,0 +1,58 @@ +/* hook.cpp */ + +#include "hook.h" + +Dictionary Hook::GetTable() { + return table; +} + +void Hook::Create(String name) { + Dictionary newHook = Dictionary(); + table[name] = newHook; +} + +void Hook::Add(String event, String uid, Ref function) { + Dictionary tmp = table[event]; + tmp[uid] = function; + table[event] = tmp; +} + +void Hook::Call(String event, Array args = Array()) { + for (int i = 0; i < table.size(); i++) { + Dictionary tmp = table.get(event, Dictionary()); + if (!tmp.empty()) { + Array keys = tmp.keys(); + for (int key_i = 0; key_i < keys.size(); key_i++) { + Ref function = tmp[keys[key_i]]; + function->call_funcv(args); + } + } else { + ERR_PRINT("Hook event '" + event + "' cannot be found or empty."); + } + } +} + +void Hook::Remove(String event, String uid) { + Dictionary tmp = table.get(event, Dictionary()); + if (!tmp.empty()) { + if (tmp.erase(uid)) { + table[event] = tmp; + } else { + ERR_PRINT("Hook '" + uid + "' cannot be found."); + } + } else { + ERR_PRINT("Hook event '" + event + "' cannot be found or empty."); + } +} + +void Hook::_bind_methods() { + ClassDB::bind_method(D_METHOD("GetTable"), &Hook::GetTable); + ClassDB::bind_method(D_METHOD("Create", "name"), &Hook::Create); + ClassDB::bind_method(D_METHOD("Add", "event", "uid", "function"), &Hook::Add); + ClassDB::bind_method(D_METHOD("Call", "event", "args"), &Hook::Call); + ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove); +}; + +Hook::Hook() { + table = Dictionary(); +} \ No newline at end of file diff --git a/hook.h b/hook.h new file mode 100644 index 0000000..89edd40 --- /dev/null +++ b/hook.h @@ -0,0 +1,29 @@ +/* hook.h */ + +#ifndef HOOK_H +#define HOOK_H + +#include "core/reference.h" +#include "core/func_ref.h" +#include "core/ustring.h" +#include "core/dictionary.h" +#include "core/array.h" + +class Hook : public Reference { + GDCLASS(Hook, Reference); + + Dictionary table; + +protected: + static void _bind_methods(); + +public: + Dictionary GetTable(); + void Create(String name); + void Add(String event, String uid, Ref function); + void Call(String event, Array args); + void Remove(String event, String uid); + Hook(); +}; + +#endif // HOOK_H \ No newline at end of file diff --git a/register_types.cpp b/register_types.cpp new file mode 100644 index 0000000..9ef1c1e --- /dev/null +++ b/register_types.cpp @@ -0,0 +1,14 @@ +/* register_types.cpp */ + +#include "register_types.h" + +#include "core/class_db.h" +#include "hook.h" + +void register_hook_types() { + ClassDB::register_class(); +} + +void unregister_hook_types() { + // Nothing to do here in this example. +} \ No newline at end of file diff --git a/register_types.h b/register_types.h new file mode 100644 index 0000000..d6b73f6 --- /dev/null +++ b/register_types.h @@ -0,0 +1,5 @@ +/* register_types.h */ + +void register_hook_types(); +void unregister_hook_types(); +/* yes, the word in the middle must be the same as the module folder name */ \ No newline at end of file