Command history for cheat console, closes #316 (PR #869)

Adds console command history. Browsable by up and down arrow keys.
dev-buzzingcars
Smok94 2017-01-02 20:23:19 +01:00 committed by krzys_h
parent 6ec13017eb
commit 8a0c7279dc
2 changed files with 61 additions and 0 deletions

View File

@ -763,6 +763,7 @@ bool CRobotMain::ProcessEvent(Event &event)
m_interface->SetFocus(pe); m_interface->SetFocus(pe);
if (m_phase == PHASE_SIMUL) m_cmdEditPause = m_pause->ActivatePause(PAUSE_ENGINE); if (m_phase == PHASE_SIMUL) m_cmdEditPause = m_pause->ActivatePause(PAUSE_ENGINE);
m_cmdEdit = true; m_cmdEdit = true;
m_commandHistoryIndex = -1; // no element selected in command history
} }
return false; 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<KeyEventData>()->key == KEY(UP) && m_cmdEdit)
{
Ui::CEdit* pe = static_cast<Ui::CEdit*>(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<KeyEventData>()->key == KEY(DOWN) && m_cmdEdit)
{
Ui::CEdit* pe = static_cast<Ui::CEdit*>(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 && if (event.type == EVENT_KEY_DOWN &&
event.GetData<KeyEventData>()->key == KEY(RETURN) && m_cmdEdit) event.GetData<KeyEventData>()->key == KEY(RETURN) && m_cmdEdit)
{ {
@ -793,6 +816,7 @@ bool CRobotMain::ProcessEvent(Event &event)
m_cmdEditPause = nullptr; m_cmdEditPause = nullptr;
} }
ExecuteCmd(cmd); ExecuteCmd(cmd);
PushToCommandHistory(cmd);
m_cmdEdit = false; m_cmdEdit = false;
return false; return false;
} }
@ -5768,3 +5792,28 @@ bool CRobotMain::GetDebugCrashSpheres()
{ {
return m_debugCrashSpheres; 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<int>(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];
}

View File

@ -513,6 +513,13 @@ protected:
void UpdateDebugCrashSpheres(); 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: protected:
CApplication* m_app = nullptr; CApplication* m_app = nullptr;
@ -685,4 +692,9 @@ protected:
std::deque<CObject*> m_selectionHistory; std::deque<CObject*> m_selectionHistory;
bool m_debugCrashSpheres; bool m_debugCrashSpheres;
//! Cheat console command history
std::deque<std::string> m_commandHistory;
//! Index of currently selected element in command history
int m_commandHistoryIndex;
}; };