Sdk/Console: Add command history

Former-commit-id: e5b437988a3f81b2724c0f1a45da0bbfc85b14b0
This commit is contained in:
Lynix 2016-03-30 18:36:54 +02:00
parent b331d66c70
commit 2b51a8b4b6
2 changed files with 51 additions and 6 deletions

View File

@ -9,6 +9,7 @@
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Utility/Event.hpp>
#include <Nazara/Utility/Node.hpp>
#include <Nazara/Utility/SimpleTextDrawer.hpp>
#include <NDK/EntityOwner.hpp>
@ -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<Nz::String> m_commandHistory;
std::vector<Line> m_historyLines;
EntityOwner m_historyBackground;
EntityOwner m_history;

View File

@ -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<Ndk::NodeComponent>();
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<std::size_t>(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'));
}