Skip to content

Commit ac2aad6

Browse files
committed
Connect keyboard events to Lua
1 parent 4c30698 commit ac2aad6

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/core/src/scripting/scripting_api.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,27 @@ Error ScriptingAPI::mountGrowlScripts(API& api) {
7171
return err;
7272
}
7373

74+
auto input_keyboard_event_res = createClass("InputKeyboardEvent", false);
75+
if (!input_keyboard_event_res) {
76+
return std::move(input_keyboard_event_res.error());
77+
}
78+
auto& input_keyboard_event_cls = *input_keyboard_event_res;
79+
80+
if (auto err =
81+
input_keyboard_event_cls->addConstructor<const InputKeyboardEvent*>(
82+
[](ClassSelf* self, void* ctx,
83+
const std::vector<ScriptingParam>& args)
84+
-> Result<ScriptingParam> {
85+
auto event = static_cast<const InputKeyboardEvent*>(
86+
std::get<const void*>(args.at(0)));
87+
self->setField("type", (int)event->type);
88+
self->setField("key", (int)event->key);
89+
return ScriptingParam();
90+
},
91+
nullptr);
92+
err) {
93+
return err;
94+
}
95+
7496
return nullptr;
7597
}

src/scene/include/growl/scene/node.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Node : public InputProcessor {
5252
// Input
5353
virtual bool onMouseEvent(const InputMouseEvent& event) override;
5454
bool onMouseEventRaw(const InputMouseEvent& event);
55+
virtual bool onKeyboardEvent(const InputKeyboardEvent& event) override;
56+
bool onKeyboardEventRaw(const InputKeyboardEvent& event);
5557

5658
private:
5759
API* api;

src/scene/src/node.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,39 @@ bool Node::onMouseEvent(const InputMouseEvent& event) {
177177
bool Node::onMouseEventRaw(const InputMouseEvent& event) {
178178
return InputProcessor::onMouseEvent(event);
179179
}
180+
181+
bool Node::onKeyboardEvent(const InputKeyboardEvent& event) {
182+
if (!bound_script_obj) {
183+
return onKeyboardEventRaw(event);
184+
}
185+
186+
std::vector<ScriptingParam> ctor_args;
187+
ctor_args.push_back(&event);
188+
auto ctor_result =
189+
api->scripting().executeConstructor<const InputKeyboardEvent*>(
190+
"InputKeyboardEvent", ctor_args);
191+
if (!ctor_result) {
192+
api->system().log(
193+
LogLevel::Warn, "Node::onKeyboardEvent",
194+
"Failed to execute constructor: {}",
195+
ctor_result.error()->message());
196+
return false;
197+
}
198+
199+
std::vector<ScriptingParam> v;
200+
v.push_back(std::move(ctor_result.get()));
201+
auto exec_res =
202+
api->scripting().executeMethod<bool, std::unique_ptr<ScriptingRef>>(
203+
bound_script_obj.get(), "onKeyboardEvent", v);
204+
if (!exec_res) {
205+
api->system().log(
206+
LogLevel::Warn, "Node::onKeyboardEvent",
207+
"Failed to execute method: {}", exec_res.error()->message());
208+
return false;
209+
}
210+
return std::get<bool>(*exec_res);
211+
}
212+
213+
bool Node::onKeyboardEventRaw(const InputKeyboardEvent& event) {
214+
return InputProcessor::onKeyboardEvent(event);
215+
}

src/scene/src/scene.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,37 @@ Error Growl::initSceneGraph(API& api) {
268268
return err;
269269
}
270270

271+
if (auto err = node_cls->addMethod<bool, std::unique_ptr<ScriptingRef>>(
272+
"onKeyboardEvent",
273+
[](ClassSelf* self, void* ctx,
274+
const std::vector<ScriptingParam>& args)
275+
-> Result<ScriptingParam> {
276+
ScriptingAPI* scripting = static_cast<ScriptingAPI*>(ctx);
277+
Node* n =
278+
static_cast<Node*>(const_cast<void*>(std::get<const void*>(
279+
self->getField("__ptr", ScriptingType::Ptr))));
280+
auto& script_event =
281+
std::get<std::unique_ptr<ScriptingRef>>(args.at(0));
282+
InputKeyboardEvent event;
283+
if (auto res = scripting->getField(
284+
script_event.get(), "type", ScriptingType::Int);
285+
!res) {
286+
return std::move(res.error());
287+
} else {
288+
event.type = KeyEventType(std::get<int>(res.get()));
289+
}
290+
if (auto res = scripting->getField(
291+
script_event.get(), "key", ScriptingType::Int);
292+
!res) {
293+
return std::move(res.error());
294+
} else {
295+
event.key = Key(std::get<int>(res.get()));
296+
}
297+
return ScriptingParam(n->onKeyboardEventRaw(event));
298+
},
299+
&(api.scripting()))) {
300+
return err;
301+
}
302+
271303
return nullptr;
272304
}

0 commit comments

Comments
 (0)