diff --git a/SDK/include/NDK/Console.hpp b/SDK/include/NDK/Console.hpp index 1f0d36930..0f6c08d43 100644 --- a/SDK/include/NDK/Console.hpp +++ b/SDK/include/NDK/Console.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,7 @@ namespace Ndk inline bool IsVisible() const; void SendCharacter(char32_t character); + void SendEvent(Nz::WindowEvent event); void SetCharacterSize(unsigned int size); void SetSize(const Nz::Vector2f& size); @@ -65,6 +67,8 @@ namespace Ndk Nz::String text; }; + std::size_t m_historyPosition; + std::vector m_commandHistory; std::vector m_historyLines; EntityOwner m_historyBackground; EntityOwner m_history; diff --git a/SDK/src/NDK/Console.cpp b/SDK/src/NDK/Console.cpp index da16e3aa2..976bb7fd1 100644 --- a/SDK/src/NDK/Console.cpp +++ b/SDK/src/NDK/Console.cpp @@ -20,6 +20,7 @@ namespace Ndk } Console::Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance) : + m_historyPosition(0), m_defaultFont(Nz::Font::GetDefault()), m_instance(instance), m_size(size), @@ -81,10 +82,10 @@ namespace Ndk Ndk::NodeComponent& inputNode = m_input->AddComponent(); inputNode.SetParent(this); - + Layout(); } - + void Console::AddLine(const Nz::String& text, const Nz::Color& color) { AddLineInternal(text, color); @@ -124,6 +125,40 @@ namespace Ndk m_inputTextSprite->Update(m_inputDrawer); } + void Console::SendEvent(Nz::WindowEvent event) + { + switch (event.type) + { + case Nz::WindowEventType_TextEntered: + SendCharacter(event.text.character); + break; + + case Nz::WindowEventType_KeyPressed: + { + switch (event.key.code) + { + case Nz::Keyboard::Down: + case Nz::Keyboard::Up: + if (event.key.code == Nz::Keyboard::Up) + m_historyPosition = std::min(m_commandHistory.size(), m_historyPosition + 1); + else + { + if (m_historyPosition > 1) + m_historyPosition--; + else if (m_historyPosition == 0) + m_historyPosition = 1; + } + + Nz::String text = m_commandHistory[m_commandHistory.size() - m_historyPosition]; + m_inputDrawer.SetText(s_inputPrefix + text); + m_inputTextSprite->Update(m_inputDrawer); + break; + } + break; + } + } + } + void Console::SetCharacterSize(unsigned int size) { m_characterSize = size; @@ -162,12 +197,18 @@ namespace Ndk void Console::ExecuteInput() { - Nz::String input = m_inputDrawer.GetText().SubString(s_inputPrefixSize); + Nz::String input = m_inputDrawer.GetText(); + Nz::String inputCmd = input.SubString(s_inputPrefixSize);; m_inputDrawer.SetText(s_inputPrefix); - AddLineInternal(input); + if (m_commandHistory.empty() || m_commandHistory.back() != inputCmd) + m_commandHistory.push_back(inputCmd); - if (!m_instance.Execute(input)) + m_historyPosition = 0; + + AddLineInternal(input); //< With the input prefix + + if (!m_instance.Execute(inputCmd)) AddLineInternal(m_instance.GetLastError(), Nz::Color::Red); RefreshHistory(); @@ -210,7 +251,7 @@ namespace Ndk m_historyDrawer.AppendText(it->text); ++it; } - + m_historyDrawer.AppendText(Nz::String('\n')); }