SDK/Console: Add OnCommand signal and remove LuaState dependency

This commit is contained in:
Lynix 2019-07-07 00:28:51 +02:00
parent 3bda97a60a
commit c6d601c429
4 changed files with 21 additions and 24 deletions

View File

@ -281,6 +281,7 @@ Nazara Development Kit:
- Added TextAreaWidget::[Get|Set]TextFont - Added TextAreaWidget::[Get|Set]TextFont
- ⚠️ TextAreaWidget::OnTextAreaCursorMove signal now uses a Vector2ui* position as its second argument (instead of a std::size_t*) - ⚠️ TextAreaWidget::OnTextAreaCursorMove signal now uses a Vector2ui* position as its second argument (instead of a std::size_t*)
- Added TextAreaWidget::OnTextAreaSelection - Added TextAreaWidget::OnTextAreaSelection
- ⚠️ Console class is no longer bound to a LuaState and now has a OnCommand signal
# 0.4: # 0.4:

View File

@ -19,7 +19,6 @@
namespace Nz namespace Nz
{ {
class LuaState;
struct WindowEvent; struct WindowEvent;
} }
@ -35,7 +34,7 @@ namespace Ndk
class NDK_API Console : public BaseWidget, public Nz::HandledObject<Console> class NDK_API Console : public BaseWidget, public Nz::HandledObject<Console>
{ {
public: public:
Console(BaseWidget* parent, Nz::LuaState& state); Console(BaseWidget* parent);
Console(const Console& console) = delete; Console(const Console& console) = delete;
Console(Console&& console) = default; Console(Console&& console) = default;
~Console() = default; ~Console() = default;
@ -57,6 +56,8 @@ namespace Ndk
Console& operator=(const Console& console) = delete; Console& operator=(const Console& console) = delete;
Console& operator=(Console&& console) = default; Console& operator=(Console&& console) = default;
NazaraSignal(OnCommand, Console* /*console*/, const Nz::String& /*command*/);
private: private:
void ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction); void ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction);
void Layout() override; void Layout() override;
@ -74,7 +75,6 @@ namespace Ndk
TextAreaWidget* m_history; TextAreaWidget* m_history;
TextAreaWidget* m_input; TextAreaWidget* m_input;
Nz::FontRef m_defaultFont; Nz::FontRef m_defaultFont;
Nz::LuaState& m_state;
unsigned int m_characterSize; unsigned int m_characterSize;
unsigned int m_maxHistoryLines; unsigned int m_maxHistoryLines;
}; };

View File

@ -151,7 +151,14 @@ namespace Ndk
else else
windowDimensions.MakeZero(); windowDimensions.MakeZero();
overlay->console = info.canvas->Add<Console>(overlay->lua); Nz::LuaInstance& lua = overlay->lua;
overlay->console = info.canvas->Add<Console>();
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; Console& consoleRef = *overlay->console;
consoleRef.Resize({float(windowDimensions.x), windowDimensions.y / 4.f}); consoleRef.Resize({float(windowDimensions.x), windowDimensions.y / 4.f});
@ -163,11 +170,11 @@ namespace Ndk
consoleRef.AddLine(str); consoleRef.AddLine(str);
}); });
overlay->lua.LoadLibraries(); lua.LoadLibraries();
LuaAPI::RegisterClasses(overlay->lua); LuaAPI::RegisterClasses(lua);
// Override "print" function to add a line in the console // 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; Nz::StringStream stream;
@ -191,21 +198,15 @@ namespace Ndk
consoleRef.AddLine(stream); consoleRef.AddLine(stream);
return 0; return 0;
}); });
overlay->lua.SetGlobal("print"); lua.SetGlobal("print");
// Define a few base variables to allow our interface to interact with the application // Define a few base variables to allow our interface to interact with the application
overlay->lua.PushGlobal("Application", Ndk::Application::Instance()); lua.PushGlobal("Application", Ndk::Application::Instance());
overlay->lua.PushGlobal("Console", consoleRef.CreateHandle()); lua.PushGlobal("Console", consoleRef.CreateHandle());
// Setup a few event callback to handle the console // Setup a few event callback to handle the console
Nz::EventHandler& eventHandler = info.window->GetEventHandler(); 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) overlay->keyPressedSlot.Connect(eventHandler.OnKeyPressed, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& event)
{ {
if (event.code == Nz::Keyboard::F9) if (event.code == Nz::Keyboard::F9)

View File

@ -4,7 +4,6 @@
#include <NDK/Console.hpp> #include <NDK/Console.hpp>
#include <Nazara/Core/Unicode.hpp> #include <Nazara/Core/Unicode.hpp>
#include <Nazara/Lua/LuaState.hpp>
#include <Nazara/Platform/Event.hpp> #include <Nazara/Platform/Event.hpp>
#include <NDK/Components/GraphicsComponent.hpp> #include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp> #include <NDK/Components/NodeComponent.hpp>
@ -35,11 +34,10 @@ namespace Ndk
* \param instance Lua instance that will interact with the world * \param instance Lua instance that will interact with the world
*/ */
Console::Console(BaseWidget* parent, Nz::LuaState& state) : Console::Console(BaseWidget* parent) :
BaseWidget(parent), BaseWidget(parent),
m_historyPosition(0), m_historyPosition(0),
m_defaultFont(Nz::Font::GetDefault()), m_defaultFont(Nz::Font::GetDefault()),
m_state(state),
m_characterSize(24) m_characterSize(24)
{ {
// History // History
@ -185,8 +183,7 @@ namespace Ndk
/*! /*!
* \brief Performs this action when an input is added to the console * \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"); NazaraAssert(textArea == m_input, "Unexpected signal from an other text area");
@ -201,14 +198,12 @@ namespace Ndk
AddLine(input); //< With the input prefix AddLine(input); //< With the input prefix
if (!m_state.Execute(inputCmd)) OnCommand(this, inputCmd);
AddLine(m_state.GetLastError(), Nz::Color::Red);
} }
/*! /*!
* \brief Places the console according to its layout * \brief Places the console according to its layout
*/ */
void Console::Layout() void Console::Layout()
{ {
Nz::Vector2f origin = Nz::Vector2f(GetPosition()); Nz::Vector2f origin = Nz::Vector2f(GetPosition());