| 92 | } |
| 93 | |
| 94 | LuaCallbacks LuaBindings::makeWidgetCallbacks(Widget* parentWidget, GuiReaderPtr reader) { |
| 95 | if (!reader) |
| 96 | reader = make_shared<GuiReader>(); |
| 97 | |
| 98 | LuaCallbacks callbacks; |
| 99 | |
| 100 | // a bit miscellaneous, but put this here since widgets have access to gui context |
| 101 | |
| 102 | callbacks.registerCallback("playSound", |
| 103 | [parentWidget](String const& audio, Maybe<int> loops, Maybe<float> volume) { |
| 104 | parentWidget->context()->playAudio(audio, loops.value(0), volume.value(1.0f)); |
| 105 | }); |
| 106 | |
| 107 | // widget userdata methods |
| 108 | |
| 109 | callbacks.registerCallback("bindCanvas", [parentWidget](String const& widgetName) -> Maybe<CanvasWidgetPtr> { |
| 110 | if (auto canvas = parentWidget->fetchChild<CanvasWidget>(widgetName)) |
| 111 | return canvas; |
| 112 | return {}; |
| 113 | }); |
| 114 | |
| 115 | // generic widget callbacks |
| 116 | |
| 117 | callbacks.registerCallback("getPosition", [parentWidget](String const& widgetName) -> Maybe<Vec2I> { |
| 118 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 119 | return widget->relativePosition(); |
| 120 | return {}; |
| 121 | }); |
| 122 | callbacks.registerCallback("setPosition", [parentWidget](String const& widgetName, Vec2I const& position) { |
| 123 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 124 | widget->setPosition(position); |
| 125 | }); |
| 126 | |
| 127 | callbacks.registerCallback("getSize", [parentWidget](String const& widgetName) -> Maybe<Vec2I> { |
| 128 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 129 | return widget->size(); |
| 130 | return {}; |
| 131 | }); |
| 132 | callbacks.registerCallback("setSize", [parentWidget](String const& widgetName, Vec2I const& size) { |
| 133 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 134 | widget->setSize(size); |
| 135 | }); |
| 136 | |
| 137 | callbacks.registerCallback("setVisible", [parentWidget](String const& widgetName, bool visible) { |
| 138 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 139 | widget->setVisibility(visible); |
| 140 | }); |
| 141 | |
| 142 | callbacks.registerCallback("active", [parentWidget](String const& widgetName) -> Maybe<bool> { |
| 143 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 144 | return widget->active(); |
| 145 | return {}; |
| 146 | }); |
| 147 | |
| 148 | callbacks.registerCallback("focus", [parentWidget](String const& widgetName) { |
| 149 | if (auto widget = parentWidget->fetchChild<Widget>(widgetName)) |
| 150 | widget->focus(); |
| 151 | }); |
nothing calls this directly
no test coverage detected