From 8a0c7279dc518438fcfd3b8543140edc66386f9c Mon Sep 17 00:00:00 2001 From: Smok94 Date: Mon, 2 Jan 2017 20:23:19 +0100 Subject: [PATCH] Command history for cheat console, closes #316 (PR #869) Adds console command history. Browsable by up and down arrow keys. --- src/level/robotmain.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ src/level/robotmain.h | 12 ++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 80e9ce06..2797e3d3 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -763,6 +763,7 @@ bool CRobotMain::ProcessEvent(Event &event) m_interface->SetFocus(pe); if (m_phase == PHASE_SIMUL) m_cmdEditPause = m_pause->ActivatePause(PAUSE_ENGINE); m_cmdEdit = true; + m_commandHistoryIndex = -1; // no element selected in command history } return false; } @@ -777,6 +778,28 @@ bool CRobotMain::ProcessEvent(Event &event) } } + // Browse forward command history with UP key + if (event.type == EVENT_KEY_DOWN && + event.GetData()->key == KEY(UP) && m_cmdEdit) + { + Ui::CEdit* pe = static_cast(m_interface->SearchControl(EVENT_CMD)); + if (pe == nullptr) return false; + std::string cmd = GetNextFromCommandHistory(); + if (!cmd.empty()) pe->SetText(cmd); + return false; + } + + // Browse backward command history with DOWN key + if (event.type == EVENT_KEY_DOWN && + event.GetData()->key == KEY(DOWN) && m_cmdEdit) + { + Ui::CEdit* pe = static_cast(m_interface->SearchControl(EVENT_CMD)); + if (pe == nullptr) return false; + std::string cmd = GetPreviousFromCommandHistory(); + if (!cmd.empty()) pe->SetText(cmd); + return false; + } + if (event.type == EVENT_KEY_DOWN && event.GetData()->key == KEY(RETURN) && m_cmdEdit) { @@ -793,6 +816,7 @@ bool CRobotMain::ProcessEvent(Event &event) m_cmdEditPause = nullptr; } ExecuteCmd(cmd); + PushToCommandHistory(cmd); m_cmdEdit = false; return false; } @@ -5768,3 +5792,28 @@ bool CRobotMain::GetDebugCrashSpheres() { return m_debugCrashSpheres; } + +void CRobotMain::PushToCommandHistory(std::string str) +{ + if (!m_commandHistory.empty() && m_commandHistory.front() == str) // already in history + return; + + m_commandHistory.push_front(str); + + if (m_commandHistory.size() > 50) // to avoid infinite growth + m_commandHistory.pop_back(); +} + +std::string CRobotMain::GetNextFromCommandHistory() +{ + if (m_commandHistory.empty() || static_cast(m_commandHistory.size()) <= m_commandHistoryIndex + 1) // no next element + return ""; + return m_commandHistory[++m_commandHistoryIndex]; +} + +std::string CRobotMain::GetPreviousFromCommandHistory() +{ + if (m_commandHistory.empty() || m_commandHistoryIndex < 1) // first or none element selected + return ""; + return m_commandHistory[--m_commandHistoryIndex]; +} diff --git a/src/level/robotmain.h b/src/level/robotmain.h index 346e8fc4..dad1bed0 100644 --- a/src/level/robotmain.h +++ b/src/level/robotmain.h @@ -513,6 +513,13 @@ protected: void UpdateDebugCrashSpheres(); + //! Adds element to the beginning of command history + void PushToCommandHistory(std::string obj); + //! Returns next/previous element from command history and updates index + //@{ + std::string GetNextFromCommandHistory(); + std::string GetPreviousFromCommandHistory(); + //@} protected: CApplication* m_app = nullptr; @@ -685,4 +692,9 @@ protected: std::deque m_selectionHistory; bool m_debugCrashSpheres; + + //! Cheat console command history + std::deque m_commandHistory; + //! Index of currently selected element in command history + int m_commandHistoryIndex; };