diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-04-29 22:17:00 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-04-29 22:17:00 +0000 |
commit | b7dfc4d660d3ad930610c4f6af35d8e916a7feb8 (patch) | |
tree | 29e3d8c42a34a113c25e995e6a82f47687ed1e97 /src/gui | |
parent | b4ddbd772ae2039ec9e23ce85f16f07f3533edd4 (diff) | |
download | mana-b7dfc4d660d3ad930610c4f6af35d8e916a7feb8.tar.gz mana-b7dfc4d660d3ad930610c4f6af35d8e916a7feb8.tar.bz2 mana-b7dfc4d660d3ad930610c4f6af35d8e916a7feb8.tar.xz mana-b7dfc4d660d3ad930610c4f6af35d8e916a7feb8.zip |
Chat command history should now be functional. Player movement will still need
to be stopped while chat box is focussed for it to work nicely though.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/chat.cpp | 101 | ||||
-rw-r--r-- | src/gui/chat.h | 16 |
2 files changed, 31 insertions, 86 deletions
diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 04a0cdfc..a193a5e7 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -28,11 +28,6 @@ #include "../main.h" #include <iostream> -/** History */ -#define HIST_LEN 10 -static char *buf[HIST_LEN]; -static int f,s,t; - ChatWindow::ChatWindow(const char *logfile, int item_num): Window("") { @@ -60,6 +55,7 @@ ChatWindow::ChatWindow(const char *logfile, int item_num): // Add key listener to chat input to be able to respond to up/down chatInput->addKeyListener(this); + curHist = history.end(); } ChatWindow::~ChatWindow() @@ -212,15 +208,19 @@ void ChatWindow::draw(gcn::Graphics *graphics) void ChatWindow::action(const std::string& eventId) { - if (eventId == "chatinput") { + if (eventId == "chatinput") + { std::string message = chatInput->getText(); if (message.length() > 0) { + if (message != history.back()) { + history.push_back(message); + } + curHist = history.end(); chat_send(char_info[0].name, message.c_str()); chatInput->setText(""); } gui->focusNone(); - } } @@ -377,79 +377,26 @@ std::string ChatWindow::cutString(std::string& value, unsigned int maximumLength return std::string(""); } -void ChatWindow::updateHistory(const char *ptr) -{ - f = t; - if(*ptr == 0) return; - - // prevent duplicates - if(f != s && strcmp(ptr, buf[(f + HIST_LEN -1) % HIST_LEN]) == 0) return; - - buf[f] = strdup(ptr); - f = ( f + 1) % HIST_LEN; - - if(f == s) { - free(buf[f]); - buf[s] = 0; - s = (s + 1) % HIST_LEN; - } - - t = f; -} - -void ChatWindow::arrowUp(void) +void ChatWindow::keyPress(const gcn::Key &key) { - printf("arrowUp\n"); - const char *ptr; - - ptr = chatInput->getText().c_str(); - - if (*ptr) { - if(t == f || strcmp(ptr, buf[t]) != 0) { - updateHistory(ptr); - t = (f + HIST_LEN - 1) % HIST_LEN; + if (key.getValue() == key.DOWN && curHist != history.end()) + { + // Move forward through the history + std::list<std::string>::iterator prevHist = curHist++; + if (curHist != history.end()) { + chatInput->setText(*curHist); + chatInput->setCaretPosition(chatInput->getText().length()); } - } - - if (t != s) - t = (t + HIST_LEN -1) % HIST_LEN; - if (buf[t]) - updateHistory(buf[t]); - else - updateHistory(""); -} - -void ChatWindow::arrowDown(void) -{ - printf("arrowDown\n"); - const char *ptr; - - ptr = chatInput->getText().c_str(); - - if (*ptr) { - if(t == f || strcmp(ptr, buf[t]) != 0) { - updateHistory(ptr); - t = (f + HIST_LEN - 1) % HIST_LEN; + else { + curHist = prevHist; } } - - if (t != f) - t = (t + 1) % HIST_LEN; - - if (buf[t]) - updateHistory(buf[t]); - else - updateHistory(""); -} - -void ChatWindow::keyPress(const gcn::Key &key) -{ - if (key.getValue() == key.DOWN) - arrowDown(); - else if (key.getValue() == key.UP) - arrowUp(); - - if (buf[t]) { - chatInput->setText(std::string(buf[t])); + else if (key.getValue() == key.UP && curHist != history.begin() && + history.size() > 0) + { + // Move backward through the history + curHist--; + chatInput->setText(*curHist); + chatInput->setCaretPosition(chatInput->getText().length()); } } diff --git a/src/gui/chat.h b/src/gui/chat.h index b078e952..4b0946f9 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -173,25 +173,20 @@ class ChatWindow : public Window, public gcn::ActionListener, */ char *chat_send(std::string nick, std::string msg); - /** History */ - void updateHistory(const char *ptr); - void arrowUp(void); - void arrowDown(void); - /** Called when key is pressed */ void keyPress(const gcn::Key& key); - private : + private: std::ofstream chatlog_file; - typedef struct CHATLOG { // list item container object + /** One item in the chat log */ + typedef struct CHATLOG { std::string nick; std::string text; int own; }; - std::list<CHATLOG> chatlog; // list object ready to accept out CHATLOG struct :) - std::list<CHATLOG>::iterator iter; + std::list<CHATLOG> chatlog; /**< Chat log */ int items; int items_keep; @@ -213,6 +208,9 @@ class ChatWindow : public Window, public gcn::ActionListener, gcn::TextField *chatInput; /**< Input box for typing chat messages */ gcn::TextBox *textOutput; /**< Text box for displaying chat history */ ScrollArea *scrollArea; /**< Scroll area around text output */ + + std::list<std::string> history; /**< Command history */ + std::list<std::string>::iterator curHist; /**< History iterator */ }; #endif |