diff options
author | José Ávila <linux@javila.net> | 2005-06-17 07:20:03 +0000 |
---|---|---|
committer | José Ávila <linux@javila.net> | 2005-06-17 07:20:03 +0000 |
commit | c702285ab5c288fc65dacdf81b583890d3b99c63 (patch) | |
tree | 589cffa9c16d15f18df4c988bbe300806e6f6425 /src | |
parent | 50b0f0e7ea4508e1ee30f0a0ea8741af6d3212d4 (diff) | |
download | mana-c702285ab5c288fc65dacdf81b583890d3b99c63.tar.gz mana-c702285ab5c288fc65dacdf81b583890d3b99c63.tar.bz2 mana-c702285ab5c288fc65dacdf81b583890d3b99c63.tar.xz mana-c702285ab5c288fc65dacdf81b583890d3b99c63.zip |
new BrowserBox widget with colors/links support
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/engine.cpp | 6 | ||||
-rw-r--r-- | src/engine.h | 2 | ||||
-rw-r--r-- | src/game.cpp | 18 | ||||
-rw-r--r-- | src/gui/chat.cpp | 170 | ||||
-rw-r--r-- | src/gui/chat.h | 8 | ||||
-rw-r--r-- | src/gui/help.cpp | 85 | ||||
-rw-r--r-- | src/gui/help.h | 21 |
8 files changed, 145 insertions, 170 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 2c579825..5f615055 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,8 @@ bin_PROGRAMS = tmw tmw_SOURCES = graphic/spriteset.cpp \ graphic/spriteset.h \ + gui/browserbox.cpp \ + gui/browserbox.h \ gui/buddywindow.cpp \ gui/buddywindow.h \ gui/button.cpp \ @@ -37,6 +39,7 @@ tmw_SOURCES = graphic/spriteset.cpp \ gui/itemcontainer.h \ gui/item_amount.cpp \ gui/item_amount.h \ + gui/linkhandler.h \ gui/listbox.cpp \ gui/listbox.h \ gui/login.cpp \ @@ -59,6 +62,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ gui/passwordfield.h \ gui/playerbox.cpp \ gui/playerbox.h \ + gui/popupmenu.cpp \ + gui/popupmenu.h \ gui/progressbar.cpp \ gui/progressbar.h \ gui/radiobutton.cpp \ diff --git a/src/engine.cpp b/src/engine.cpp index 64015153..8bede976 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -65,6 +65,7 @@ TradeWindow *tradeWindow; BuddyWindow *buddyWindow; Menu *menu; HelpWindow *helpWindow; +PopupMenu *popupMenu; std::map<int, Spriteset*> monsterset; char hairtable[16][4][2] = { @@ -195,6 +196,7 @@ Engine::Engine(): tradeWindow = new TradeWindow(); buddyWindow = new BuddyWindow(); helpWindow = new HelpWindow(); + popupMenu = new PopupMenu(); /** * Menu items @@ -249,6 +251,7 @@ Engine::Engine(): buddyWindow->setVisible(false); menu->setVisible(false); helpWindow->setVisible(false); + popupMenu->setVisible(false); // Do not focus any text field gui->focusNone(); @@ -297,6 +300,7 @@ Engine::~Engine() delete buddyWindow; delete menu; delete helpWindow; + delete popupMenu; // Delete sprite sets //delete monsterset; @@ -409,7 +413,7 @@ void Engine::draw() sx * 32 - 8 - offset_x, sy * 32 - 52 - offset_y); } - else if (being->job < 10) { // Draw a player + else if ((being->job < 10) && (being->name != "")) { // Draw a player being->text_x = sx * 32 + get_x_offset(being) - offset_x; being->text_y = sy * 32 + get_y_offset(being) - offset_y; diff --git a/src/engine.h b/src/engine.h index f5b24869..722cb287 100644 --- a/src/engine.h +++ b/src/engine.h @@ -44,6 +44,7 @@ #include "gui/menu.h" #include "gui/trade.h" #include "gui/help.h" +#include "gui/popupmenu.h" #include "resources/resourcemanager.h" #include "map.h" #include "graphic/spriteset.h" @@ -74,6 +75,7 @@ extern TradeWindow *tradeWindow; extern BuddyWindow *buddyWindow; extern Menu *menu; extern HelpWindow *helpWindow; +extern PopupMenu *popupMenu; extern std::map<int, Spriteset*> monsterset; char get_x_offset(char, char); char get_y_offset(char, char); diff --git a/src/game.cpp b/src/game.cpp index 95a74568..1023be99 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -38,6 +38,7 @@ #include "gui/confirm_dialog.h" #include "gui/requesttrade.h" #include "gui/help.h" +#include "gui/browserbox.h" #include "net/protocol.h" #include "resources/mapreader.h" @@ -390,6 +391,21 @@ void do_input() int mx = event.button.x / 32 + camera_x; int my = event.button.y / 32 + camera_y; +//================================ By Javila ===============================// + /** + * NOTES: + * - added here just for testing purposes. I think it's better + * add to right mouse button, and put the actual right mouse + * button function as "default" actions in left mouse button. + * - the popup menu can't handle keyboard action/navigation. For + * keyboard input I think we can add slash (/) commands. + */ + if (event.button.button == 2) + { + popupMenu->showPopup(mx, my); + } +//==========================================================================// + if (event.button.button == 3) { Being *target = findNode(mx, my); @@ -971,7 +987,7 @@ void do_parse() // Can I use the item? case 0x00a8: if (RFIFOB(6) == 0) { - chatWindow->chat_log("Failed to use item", BY_OTHER); + chatWindow->chat_log("Failed to use item", BY_SERVER); } else { inventoryWindow->changeQuantity(RFIFOW(2), RFIFOW(4)); } 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 <iostream> @@ -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 <SDL.h> #include <list> #include <string> @@ -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<std::string> 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 <guichan.hpp> #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<HELP_LINK> links; + void loadFile(const std::string &file); + + BrowserBox* browserBox; gcn::Button *okButton; - gcn::TextBox *textBox; gcn::ScrollArea *scrollArea; }; |