diff --git a/src/Scripting/NasalCanvas.cxx b/src/Scripting/NasalCanvas.cxx index 2d53a8a12..b6888a87b 100644 --- a/src/Scripting/NasalCanvas.cxx +++ b/src/Scripting/NasalCanvas.cxx @@ -24,6 +24,7 @@ #include #include #include
+#include // for NasalBinding #include #include @@ -34,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +64,7 @@ typedef nasal::Ghost NasalCustomEvent; typedef nasal::Ghost NasalDeviceEvent; typedef nasal::Ghost NasalKeyboardEvent; typedef nasal::Ghost NasalMouseEvent; +typedef nasal::Ghost NasalKeyBinding; struct CustomEventDetailWrapper; typedef SGSharedPtr CustomEventDetailPtr; @@ -210,6 +213,20 @@ static sc::ElementPtr f_groupGetChild(sc::Group& group, SGPropertyNode* node) return group.getChild(node); } +static naRef f_groupAddKeyBinding(sc::Group& group, const nasal::CallContext& ctx) +{ + auto keyBinding = ctx.requireArg(0); + group.getOrCreateFocusScope()->addKeyBinding(keyBinding); + return naNil(); +} + +static naRef f_windowAddKeyBinding(sc::Window& window, const nasal::CallContext& ctx) +{ + auto keyBinding = ctx.requireArg(0); + window.focusScope()->addKeyBinding(keyBinding); + return naNil(); +} + static void propElementSetData( simgear::PropertyBasedElement& el, const std::string& name, const nasal::ContextWrapper& ctx, @@ -478,6 +495,18 @@ static naRef f_newSpacerItem(const nasal::CallContext& ctx) return ctx.to_nasal(new sc::SpacerItem); } +static naRef f_keyBindingAddBinding(sc::KeyBinding& keyBinding, const nasal::CallContext& ctx) +{ + auto cb = ctx.requireArg(0); + keyBinding.addBinding(new NasalBinding{cb}); + return naNil(); +} + +static naRef f_newKeyBinding(const nasal::CallContext& ctx) +{ + return ctx.to_nasal(new sc::KeyBinding); +} + naRef initNasalCanvas(naRef globals, naContext c) { nasal::Hash globals_module(globals, c), @@ -537,6 +566,15 @@ naRef initNasalCanvas(naRef globals, naContext c) .member("buttons", &sc::MouseEvent::getButtonMask) .member("click_count", &sc::MouseEvent::getCurrentClickCount); + + NasalKeyBinding::init("canvas.KeyBinding") + .member("key", &sc::KeyBinding::key, &sc::KeyBinding::setKey) + .member("keyCode", &sc::KeyBinding::keyCode, &sc::KeyBinding::setKeyCode) + .member("modifiers", &sc::KeyBinding::modifiers, &sc::KeyBinding::setModifiers) + .method("addBinding", &f_keyBindingAddBinding); + canvas_module.createHash("KeyBinding") + .set("new", &f_newKeyBinding); + //---------------------------------------------------------------------------- // Canvas & elements @@ -578,10 +616,11 @@ naRef initNasalCanvas(naRef globals, naContext c) .method("localToCanvas", &sc::Element::localToCanvas); NasalGroup::init("canvas.Group") - .bases() - .method("_createChild", &f_groupCreateChild) - .method( "_getChild", &f_groupGetChild) - .method("_getElementById", &sc::Group::getElementById); + .bases() + .method("_createChild", &f_groupCreateChild) + .method("_getChild", &f_groupGetChild) + .method("_getElementById", &sc::Group::getElementById) + .method("addKeyBinding", &f_groupAddKeyBinding); NasalText::init("canvas.Text") .bases() .method("heightForWidth", &sc::Text::heightForWidth) @@ -685,7 +724,8 @@ naRef initNasalCanvas(naRef globals, naContext c) .member("_node_ghost", &elementGetNode) .method("_getCanvasDecoration", &sc::Window::getCanvasDecoration) .method("setLayout", &sc::Window::setLayout) - .method("toScreenPosition", &sc::Window::toScreenPosition); + .method("toScreenPosition", &sc::Window::toScreenPosition) + .method("addKeyBinding", &f_windowAddKeyBinding); canvas_module.set("_newWindowGhost", f_createWindow); canvas_module.set("_getDesktopGhost", f_getDesktop);