From 21c2bb116e7484e76cbcc47b9e38e7910aad3e00 Mon Sep 17 00:00:00 2001 From: nkorslund Date: Wed, 13 May 2009 07:35:23 +0000 Subject: [PATCH] Arrow keys traverses history in console git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@108 ea6a568a-9f4f-0410-981a-c910a81bb256 --- gui/cpp_console.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/gui/cpp_console.cpp b/gui/cpp_console.cpp index 3b009b51f..79f6bfd95 100644 --- a/gui/cpp_console.cpp +++ b/gui/cpp_console.cpp @@ -32,6 +32,8 @@ enum CR_EMPTY = 4 // The line had no effect }; +#include + extern "C" int32_t console_input(const char* command); extern "C" char* console_output(); @@ -41,6 +43,13 @@ public: MyGUI::EditPtr command; MyGUI::EditPtr history; + typedef std::list StringList; + + // History of previous entered commands + StringList command_history; + StringList::iterator current; + Ogre::UTFString editString; + Console() : Layout("openmw_console_layout.xml") { @@ -50,9 +59,11 @@ public: getWidget(command, "edit_Command"); getWidget(history, "list_History"); - // Set up the command line combobox + // Set up the command line box command->eventEditSelectAccept = newDelegate(this, &Console::acceptCommand); + command->eventKeyButtonPressed = + newDelegate(this, &Console::keyPress); // Set up the log window history->setOverflowToTheLeft(true); @@ -67,11 +78,53 @@ public: MyGUI::InputManager::getInstance().setKeyFocusWidget(command); } + void keyPress(MyGUI::WidgetPtr _sender, + MyGUI::KeyCode key, + MyGUI::Char _char) + { + if(command_history.empty()) return; + + // Traverse history with up and down arrows + if(key == MyGUI::KeyCode::ArrowUp) + { + // If the user was editing a string, store it for later + if(current == command_history.end()) + editString = command->getCaption(); + + if(current != command_history.begin()) + { + current--; + command->setCaption(*current); + } + } + else if(key == MyGUI::KeyCode::ArrowDown) + { + if(current != command_history.end()) + { + current++; + + if(current != command_history.end()) + command->setCaption(*current); + else + // Restore the edit string + command->setCaption(editString); + } + } + } + + void acceptCommand(MyGUI::EditPtr _sender) { const Ogre::UTFString &cm = command->getCaption(); if(cm.empty()) return; + // Add the command to the history, and set the current pointer to + // the end of the list + command_history.push_back(cm); + current = command_history.end(); + editString.clear(); + + // Log the command and result history->addText("#FFFFFF> " + cm + "\n"); int res = console_input(cm.asUTF8_c_str());