From c6d601c42977969f1d1e9ed2d852f93c76f486e3 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 7 Jul 2019 00:28:51 +0200 Subject: [PATCH] SDK/Console: Add OnCommand signal and remove LuaState dependency --- ChangeLog.md | 1 + SDK/include/NDK/Console.hpp | 6 +++--- SDK/src/NDK/Application.cpp | 27 ++++++++++++++------------- SDK/src/NDK/Console.cpp | 11 +++-------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 7100c7027..587e763bd 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -281,6 +281,7 @@ Nazara Development Kit: - Added TextAreaWidget::[Get|Set]TextFont - ⚠️ TextAreaWidget::OnTextAreaCursorMove signal now uses a Vector2ui* position as its second argument (instead of a std::size_t*) - Added TextAreaWidget::OnTextAreaSelection +- ⚠️ Console class is no longer bound to a LuaState and now has a OnCommand signal # 0.4: diff --git a/SDK/include/NDK/Console.hpp b/SDK/include/NDK/Console.hpp index cb79fcbee..32d0cd76a 100644 --- a/SDK/include/NDK/Console.hpp +++ b/SDK/include/NDK/Console.hpp @@ -19,7 +19,6 @@ namespace Nz { - class LuaState; struct WindowEvent; } @@ -35,7 +34,7 @@ namespace Ndk class NDK_API Console : public BaseWidget, public Nz::HandledObject { public: - Console(BaseWidget* parent, Nz::LuaState& state); + Console(BaseWidget* parent); Console(const Console& console) = delete; Console(Console&& console) = default; ~Console() = default; @@ -57,6 +56,8 @@ namespace Ndk Console& operator=(const Console& console) = delete; Console& operator=(Console&& console) = default; + NazaraSignal(OnCommand, Console* /*console*/, const Nz::String& /*command*/); + private: void ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction); void Layout() override; @@ -74,7 +75,6 @@ namespace Ndk TextAreaWidget* m_history; TextAreaWidget* m_input; Nz::FontRef m_defaultFont; - Nz::LuaState& m_state; unsigned int m_characterSize; unsigned int m_maxHistoryLines; }; diff --git a/SDK/src/NDK/Application.cpp b/SDK/src/NDK/Application.cpp index c26b1e7d2..cf6fcdbb5 100644 --- a/SDK/src/NDK/Application.cpp +++ b/SDK/src/NDK/Application.cpp @@ -151,7 +151,14 @@ namespace Ndk else windowDimensions.MakeZero(); - overlay->console = info.canvas->Add(overlay->lua); + Nz::LuaInstance& lua = overlay->lua; + + overlay->console = info.canvas->Add(); + overlay->console->OnCommand.Connect([&lua](Ndk::Console* console, const Nz::String& command) + { + if (!lua.Execute(command)) + console->AddLine(lua.GetLastError(), Nz::Color::Red); + }); Console& consoleRef = *overlay->console; consoleRef.Resize({float(windowDimensions.x), windowDimensions.y / 4.f}); @@ -163,11 +170,11 @@ namespace Ndk consoleRef.AddLine(str); }); - overlay->lua.LoadLibraries(); - LuaAPI::RegisterClasses(overlay->lua); + lua.LoadLibraries(); + LuaAPI::RegisterClasses(lua); // Override "print" function to add a line in the console - overlay->lua.PushFunction([&consoleRef] (Nz::LuaState& state) + lua.PushFunction([&consoleRef] (Nz::LuaState& state) { Nz::StringStream stream; @@ -191,21 +198,15 @@ namespace Ndk consoleRef.AddLine(stream); return 0; }); - overlay->lua.SetGlobal("print"); + lua.SetGlobal("print"); // Define a few base variables to allow our interface to interact with the application - overlay->lua.PushGlobal("Application", Ndk::Application::Instance()); - overlay->lua.PushGlobal("Console", consoleRef.CreateHandle()); + lua.PushGlobal("Application", Ndk::Application::Instance()); + lua.PushGlobal("Console", consoleRef.CreateHandle()); // Setup a few event callback to handle the console Nz::EventHandler& eventHandler = info.window->GetEventHandler(); - /*overlay->eventSlot.Connect(eventHandler.OnEvent, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent& event) - { - if (consoleRef.IsVisible()) - consoleRef.SendEvent(event); - });*/ - overlay->keyPressedSlot.Connect(eventHandler.OnKeyPressed, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& event) { if (event.code == Nz::Keyboard::F9) diff --git a/SDK/src/NDK/Console.cpp b/SDK/src/NDK/Console.cpp index 088c29444..964bb6b9c 100644 --- a/SDK/src/NDK/Console.cpp +++ b/SDK/src/NDK/Console.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -35,11 +34,10 @@ namespace Ndk * \param instance Lua instance that will interact with the world */ - Console::Console(BaseWidget* parent, Nz::LuaState& state) : + Console::Console(BaseWidget* parent) : BaseWidget(parent), m_historyPosition(0), m_defaultFont(Nz::Font::GetDefault()), - m_state(state), m_characterSize(24) { // History @@ -185,8 +183,7 @@ namespace Ndk /*! * \brief Performs this action when an input is added to the console */ - - void Console::ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction) + void Console::ExecuteInput(const TextAreaWidget* textArea, bool* /*ignoreDefaultAction*/) { NazaraAssert(textArea == m_input, "Unexpected signal from an other text area"); @@ -201,14 +198,12 @@ namespace Ndk AddLine(input); //< With the input prefix - if (!m_state.Execute(inputCmd)) - AddLine(m_state.GetLastError(), Nz::Color::Red); + OnCommand(this, inputCmd); } /*! * \brief Places the console according to its layout */ - void Console::Layout() { Nz::Vector2f origin = Nz::Vector2f(GetPosition());