mirror of
https://github.com/Mestima/GodotHook.git
synced 2025-05-03 14:50:27 +00:00
Initial commit
This commit is contained in:
parent
911cd75586
commit
f1651856a8
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/__pycache__
|
||||
*.obj
|
5
SCsub
Normal file
5
SCsub
Normal file
@ -0,0 +1,5 @@
|
||||
# SCsub
|
||||
|
||||
Import('env')
|
||||
|
||||
env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
|
7
config.py
Normal file
7
config.py
Normal file
@ -0,0 +1,7 @@
|
||||
# config.py
|
||||
|
||||
def can_build(env, platform):
|
||||
return True
|
||||
|
||||
def configure(env):
|
||||
pass
|
58
hook.cpp
Normal file
58
hook.cpp
Normal file
@ -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<FuncRef> 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<FuncRef> 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();
|
||||
}
|
29
hook.h
Normal file
29
hook.h
Normal file
@ -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<FuncRef> function);
|
||||
void Call(String event, Array args);
|
||||
void Remove(String event, String uid);
|
||||
Hook();
|
||||
};
|
||||
|
||||
#endif // HOOK_H
|
14
register_types.cpp
Normal file
14
register_types.cpp
Normal file
@ -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<Hook>();
|
||||
}
|
||||
|
||||
void unregister_hook_types() {
|
||||
// Nothing to do here in this example.
|
||||
}
|
5
register_types.h
Normal file
5
register_types.h
Normal file
@ -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 */
|
Loading…
x
Reference in New Issue
Block a user