[colobot] 39/100: Command history for cheat console, closes #316 (PR #869)

Didier Raboud odyx at moszumanska.debian.org
Thu Jun 1 18:10:17 UTC 2017


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch debian/master
in repository colobot.

commit 8a0c7279dc518438fcfd3b8543140edc66386f9c
Author: Smok94 <tor94 at o2.pl>
Date:   Mon Jan 2 20:23:19 2017 +0100

    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 80e9ce0..2797e3d 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<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 &&
         event.GetData<KeyEventData>()->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<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];
+}
diff --git a/src/level/robotmain.h b/src/level/robotmain.h
index 346e8fc..dad1bed 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<CObject*> m_selectionHistory;
     bool            m_debugCrashSpheres;
+
+    //! Cheat console command history
+    std::deque<std::string> m_commandHistory;
+    //! Index of currently selected element in command history
+    int             m_commandHistoryIndex;
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list