From c702285ab5c288fc65dacdf81b583890d3b99c63 Mon Sep 17 00:00:00 2001 From: José Ávila Date: Fri, 17 Jun 2005 07:20:03 +0000 Subject: new BrowserBox widget with colors/links support --- src/gui/chat.cpp | 170 ++++++++++++++++++++++++------------------------------- src/gui/chat.h | 8 ++- src/gui/help.cpp | 85 ++++++++-------------------- src/gui/help.h | 21 ++++--- 4 files changed, 116 insertions(+), 168 deletions(-) (limited to 'src/gui') diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index a0b027ae..8a11969a 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -25,6 +25,7 @@ #include "textfield.h" #include "textbox.h" #include "chatinput.h" +#include "gui.h" #include "../graphics.h" #include "../main.h" #include @@ -37,21 +38,20 @@ ChatWindow::ChatWindow(const std::string &logfile): items_keep = 20; setContentSize(600, 100); - textOutput = new TextBox(); + setResizable(true); + chatInput = new ChatInput(); - textOutput->setEditable(false); - scrollArea = new ScrollArea(textOutput); - scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - scrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS); - scrollArea->setDimension(gcn::Rectangle( - 2, 0, 596, 98 - chatInput->getHeight() - 5)); - scrollArea->setOpaque(false); - chatInput->setPosition( - chatInput->getBorderSize(), - 100 - chatInput->getHeight() - chatInput->getBorderSize()); - chatInput->setWidth(600 - 2 * chatInput->getBorderSize()); chatInput->setEventId("chatinput"); chatInput->addActionListener(this); + + textOutput = new BrowserBox(BrowserBox::AUTO_WRAP); + textOutput->setOpaque(false); + scrollArea = new ScrollArea(textOutput); + scrollArea->setPosition( + scrollArea->getBorderSize(), scrollArea->getBorderSize()); + scrollArea->setScrollPolicy( + gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_ALWAYS); + //scrollArea->setOpaque(false); add(scrollArea); add(chatInput); @@ -69,44 +69,79 @@ ChatWindow::~ChatWindow() chatlog_file.close(); } -void ChatWindow::chat_log(std::string line, int own) +void ChatWindow::logic() { - int pos; - CHATLOG tmp; + chatInput->setPosition( + chatInput->getBorderSize(), + getContent()->getHeight() - chatInput->getHeight() - + chatInput->getBorderSize()); + chatInput->setWidth( + getContent()->getWidth() - 2 * chatInput->getBorderSize()); + + scrollArea->setWidth( + getContent()->getWidth() - 2 * scrollArea->getBorderSize()); + scrollArea->setHeight( + getContent()->getHeight() - 2 * scrollArea->getBorderSize() - + chatInput->getHeight() - 5); + scrollArea->logic(); +} +void ChatWindow::chat_log(std::string line, int own) +{ // Delete overhead from the end of the list while ((int)chatlog.size() > items_keep) { chatlog.pop_back(); } - pos = 0; - pos = (int)line.find(" : ", 0); + CHATLOG tmp; + tmp.own = own; + tmp.nick = ""; + + // Fix the owner of welcome message. + if (line.substr(0, 7) == "Welcome") + { + own = BY_SERVER; + } + + int pos = line.find(" : "); if (pos > 0) { tmp.nick = line.substr(0, pos); - switch (own) { - case ACT_IS: - tmp.nick += CAT_IS; - break; - case ACT_WHISPER: - tmp.nick += CAT_WHISPER; - break; - case BY_GM: - tmp.nick += std::string("Global announcement: "); - default: - tmp.nick += CAT_NORMAL; - } line.erase(0, pos + 3); - } else { - tmp.nick = ""; } - tmp.own = own; - - line = tmp.nick + line; + + std::string lineColor = "##0"; // Equiv. to BrowserBox::BLACK + switch (own) { + case BY_GM: + tmp.nick += std::string("Global announcement: "); + lineColor = "##1"; // Equiv. to BrowserBox::RED + break; + case BY_PLAYER: + tmp.nick += CAT_NORMAL; + lineColor = "##2"; // Equiv. to BrowserBox::GREEN + break; + case BY_OTHER: + tmp.nick += CAT_NORMAL; + lineColor = "##4"; // Equiv. to BrowserBox::ORANGE + break; + case BY_SERVER: + tmp.nick += std::string("Server: "); + lineColor = "##7"; // Equiv. to BrowserBox::PINK + break; + case ACT_WHISPER: + tmp.nick += CAT_WHISPER; + lineColor = "##3"; // Equiv. to BrowserBox::BLUE + break; + case ACT_IS: + tmp.nick += CAT_IS; + lineColor = "##5"; // Equiv. to BrowserBox::YELLOW + break; + } + + line = lineColor + tmp.nick + line; - textOutput->setText( - textOutput->getText() + std::string("\n") + line); - scrollArea->setVerticalScrollAmount( - scrollArea->getVerticalMaxScroll()); + textOutput->addRow(line); + textOutput->draw(gui->getGraphics()); + scrollArea->setVerticalScrollAmount(scrollArea->getVerticalMaxScroll()); } void ChatWindow::chat_log(CHATSKILL action) @@ -114,67 +149,10 @@ void ChatWindow::chat_log(CHATSKILL action) chat_log(const_msg(action), BY_SERVER); } - void ChatWindow::draw(gcn::Graphics *graphics) { // Draw the window border/background and children - Window::draw(graphics); - - // Draw the chat log - /* - int x, y; - int n = 8; - int texty = getHeight() - 5 - chatInput->getHeight() - - 2 * chatInput->getBorderSize(); - int i = 0; - CHATLOG line; - n -= 1; - - graphics->setColor(gcn::Color(203, 203, 203)); - graphics->drawLine(95, 5, 95, texty); - - getAbsolutePosition(x, y); - - for (iter = chatlog.begin(); iter != chatlog.end(); iter++) - { - line = *iter; - - texty -= getFont()->getHeight() - 2; - - switch (line.own) { - case BY_GM: - graphics->setColor(gcn::Color(97, 156, 236)); // GM Bue - //graphics->drawText("Global announcement: ", 5, texty); - addOutput(std::string("Global announcement: ")); - break; - case BY_PLAYER: - graphics->setColor(gcn::Color(255, 246, 98)); // Yellow - break; - case BY_OTHER: - graphics->setColor(gcn::Color(97, 156, 236)); // GM Bue - break; - } - - switch (line.own) { - case BY_GM: - graphics->setColor(gcn::Color(39, 197, 39)); // Green - break; - case BY_PLAYER: - graphics->setColor(gcn::Color(255, 255, 255)); // White - break; - case BY_OTHER: - graphics->setColor(gcn::Color(39, 197, 39)); // Green - break; - default: - graphics->setColor(gcn::Color(83, 233, 246)); // Light blue - } - - if (i >= n) { - return; - } - i++; - } - */ + Window::draw(graphics); } void ChatWindow::action(const std::string& eventId) diff --git a/src/gui/chat.h b/src/gui/chat.h index 28955ecf..dfb0ba85 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -30,6 +30,7 @@ #include "../net/network.h" #include "window.h" #include "scrollarea.h" +#include "browserbox.h" #include #include #include @@ -116,6 +117,11 @@ class ChatWindow : public Window, public gcn::ActionListener, */ ~ChatWindow(); + /** + * Logic (updates components' size) + */ + void logic(); + /* * Adds a line of text to our message list. Parameters: * @@ -197,7 +203,7 @@ class ChatWindow : public Window, public gcn::ActionListener, std::string const_msg(CHATSKILL); gcn::TextField *chatInput; /**< Input box for typing chat messages */ - gcn::TextBox *textOutput; /**< Text box for displaying chat history */ + BrowserBox *textOutput; /**< Text box for displaying chat history */ ScrollArea *scrollArea; /**< Scroll area around text output */ std::list history; /**< Command history */ diff --git a/src/gui/help.cpp b/src/gui/help.cpp index 515ff96a..a842d42e 100644 --- a/src/gui/help.cpp +++ b/src/gui/help.cpp @@ -33,9 +33,9 @@ HelpWindow::HelpWindow(): { setContentSize(455, 350); - textBox = new TextBox(); - textBox->setEditable(false); - scrollArea = new ScrollArea(textBox); + browserBox = new BrowserBox(); + browserBox->setOpaque(false); + scrollArea = new ScrollArea(browserBox); okButton = new Button("Close"); scrollArea->setDimension(gcn::Rectangle( @@ -46,20 +46,20 @@ HelpWindow::HelpWindow(): okButton->setEventId("close"); okButton->addActionListener(this); + + browserBox->setLinkHandler(this); add(scrollArea); add(okButton); setLocationRelativeTo(getParent()); - textBox->requestFocus(); } HelpWindow::~HelpWindow() { + delete browserBox; delete okButton; - delete textBox; delete scrollArea; - links.clear(); } void HelpWindow::action(const std::string& eventId) @@ -67,77 +67,42 @@ void HelpWindow::action(const std::string& eventId) setVisible(false); } -void HelpWindow::mousePress(int mx, int my, int button) +void HelpWindow::handleLink(const std::string& link) { - getParent()->moveToTop(this); - - if (button == gcn::MouseInput::LEFT) - { - int x1 = scrollArea->getX() + 10; - int y1 = scrollArea->getY() + 15; - int x2 = x1 + scrollArea->getWidth() - 25; - int y2 = y1 + scrollArea->getHeight(); - - if ((mx >= x1) && (my >= y1) && (mx <= x2) && (my <= y2)) - { - for (unsigned int i = 0; i < links.size(); i++) - { - int y1 = links[i].yPos * textBox->getFont()->getHeight() + 5 - - scrollArea->getVerticalScrollAmount(); - int y2 = y1 + textBox->getFont()->getHeight(); - - if ((my > y1) && (my < y2)) - { - loadHelp(links[i].file); - } - } - } - else if (hasMouse() && my < (int)(getTitleBarHeight() + getPadding())) - { - mMouseDrag = true; - mMouseXOffset = mx; - mMouseYOffset = my; - } - } + std::string helpFile = link; + loadHelp(helpFile); } void HelpWindow::loadHelp(const std::string &helpFile) { - std::string helpPath = TMW_DATADIR "data/help/" + helpFile + ".txt"; + browserBox->clearRows(); + + loadFile("header"); + loadFile(helpFile); + + scrollArea->setVerticalScrollAmount(0); + setVisible(true); +} + +void HelpWindow::loadFile(const std::string &file) +{ + std::string filePath = TMW_DATADIR "data/help/" + file + ".txt"; std::ifstream fin; - fin.open(helpPath.c_str()); + fin.open(filePath.c_str()); + if (fin.fail()) { - logger->log("Couldn't load help file: %s", helpPath.c_str()); + logger->log("Couldn't load help file: %s", filePath.c_str()); return; } - links.clear(); - textBox->setText(""); - while (!fin.eof()) { std::string line = ""; getline(fin, line); - - // Check for links - if (line.substr(0, 1) == "@") - { - int idx = line.find("->"); - HELP_LINK hlink; - hlink.yPos = textBox->getNumberOfRows() + 1; - hlink.file = line.substr(1, idx - 1); - links.push_back(hlink); - - line = " " + line.erase(0, idx); - } - - textBox->addRow(line); + browserBox->addRow(line); } - scrollArea->setVerticalScrollAmount(0); - setVisible(true); - fin.close(); } diff --git a/src/gui/help.h b/src/gui/help.h index 366bd418..3c5d95a1 100644 --- a/src/gui/help.h +++ b/src/gui/help.h @@ -26,16 +26,14 @@ #include #include "window.h" - -struct HELP_LINK { - int yPos; - std::string file; -}; +#include "linkhandler.h" +#include "browserbox.h" /** * The help dialog. */ -class HelpWindow : public Window, public gcn::ActionListener +class HelpWindow : public Window, public LinkHandler, + public gcn::ActionListener { public: /** @@ -54,19 +52,20 @@ class HelpWindow : public Window, public gcn::ActionListener void action(const std::string& eventId); /** - * Handles mouse click. + * Handles link action. */ - void mousePress(int mx, int my, int button); + void handleLink(const std::string& link); /** - * Load help in the dialog. + * Loads help in the dialog. */ void loadHelp(const std::string &helpFile); private: - std::vector links; + void loadFile(const std::string &file); + + BrowserBox* browserBox; gcn::Button *okButton; - gcn::TextBox *textBox; gcn::ScrollArea *scrollArea; }; -- cgit v1.2.3-60-g2f50