diff options
-rw-r--r-- | src/gui/npcdialog.cpp | 277 | ||||
-rw-r--r-- | src/gui/npcdialog.h | 185 |
2 files changed, 462 insertions, 0 deletions
diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp new file mode 100644 index 00000000..a595467d --- /dev/null +++ b/src/gui/npcdialog.cpp @@ -0,0 +1,277 @@ +/* + * The Mana World + * Copyright (C) 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "gui/npcdialog.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/layout.h" +#include "gui/widgets/scrollarea.h" +#include "gui/widgets/textbox.h" +#include "gui/widgets/listbox.h" +#include "gui/widgets/textfield.h" +#include "gui/widgets/inttextfield.h" + +#include "npc.h" + +#include "net/net.h" +#include "net/npchandler.h" + +#include "utils/gettext.h" +#include "utils/strprintf.h" + +NpcDialog::NpcDialog() + : Window(_("NPC")), + mActionState(NPC_ACTION_NEXT), + mInputState(NPC_INPUT_NONE), + mNpcId(0), + mText("") +{ + // Basic Window Setup + setWindowName("NPCText"); + setResizable(true); + + setMinWidth(200); + setMinHeight(150); + + setDefaultSize(260, 200, ImageRect::CENTER); + + // Setup output text box + mTextBox = new TextBox; + mTextBox->setEditable(false); + mTextBox->setOpaque(false); + + mScrollArea = new ScrollArea(mTextBox); + mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + mScrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS); + + // Setup listbox + mItemList = new ListBox(this); + mItemList->setWrappingEnabled(true); + setContentSize(260, 175); + + mListScrollArea = new ScrollArea(mItemList); + mListScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + mItemList->setVisible(true); + + // Setup string input box + mTextField = new TextField(""); + mTextField->setVisible(true); + + // Setup int input box + mIntField = new IntTextField; + mIntField->setVisible(true); + + // Setup button + mButton = new Button(_("Next"), "ok", this); + + // Place widgets + buildLayout(); + + center(); + loadWindowState(); +} + + +void NpcDialog::setText(const std::string &text) +{ + mText = text; + mTextBox->setTextWrapped(mText, mScrollArea->getWidth() - 15); +} + +void NpcDialog::addText(const std::string &text) +{ + setText(mText + text + "\n"); + mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll()); + mActionState = NPC_ACTION_NEXT; + buildLayout(); +} + +void NpcDialog::showCloseButton() +{ + mActionState = NPC_ACTION_CLOSE; + buildLayout(); +} + +void NpcDialog::action(const gcn::ActionEvent &event) +{ + if (event.getId() == "ok") + { + if (mActionState == NPC_ACTION_NEXT) + { + nextDialog(); + addText("\n> Next\n"); + } + else if (mActionState == NPC_ACTION_CLOSE) + { + if (current_npc) + closeDialog(); + setVisible(false); + current_npc = 0; + NPC::isTalking = false; + } + else if (mActionState == NPC_ACTION_INPUT) + { + std::string printText = ""; // Text that will get printed in the textbox + if (mInputState == NPC_INPUT_LIST) + { + int choice = 0; + int selectedIndex = mItemList->getSelected(); + + if (selectedIndex > -1) + { + choice = selectedIndex + 1; + printText = mItems[selectedIndex]; + } + + Net::getNpcHandler()->listInput(mNpcId, choice); + } + else if (mInputState == NPC_INPUT_STRING) + { + printText = mTextField->getText(); + Net::getNpcHandler()->stringInput(mNpcId, printText); + } + else if (mInputState == NPC_INPUT_INTEGER) + { + printText = strprintf("%d", mIntField->getValue()); + Net::getNpcHandler()->integerInput(mNpcId, mIntField->getValue()); + } + // addText will auto remove the input layout + addText( strprintf("\n> \"%s\"\n", printText.c_str()) ); + } + } +} + +void NpcDialog::nextDialog() +{ + Net::getNpcHandler()->nextDialog(mNpcId); +} + +void NpcDialog::closeDialog() +{ + Net::getNpcHandler()->closeDialog(mNpcId); +} + +int NpcDialog::getNumberOfElements() +{ + return mItems.size(); +} + +std::string NpcDialog::getElementAt(int i) +{ + return mItems[i]; +} + +void NpcDialog::choiceRequest() +{ + mItems.clear(); + mActionState = NPC_ACTION_INPUT; + mInputState = NPC_INPUT_LIST; + buildLayout(); +} + +void NpcDialog::addChoice(std::string choice) +{ + mItems.push_back(choice); +} + +void NpcDialog::parseListItems(const std::string &itemString) +{ + std::istringstream iss(itemString); + + std::string tmp; + while (getline(iss, tmp, ':')) + mItems.push_back(tmp); +} + +void NpcDialog::textRequest(std::string defaultText) +{ + mActionState = NPC_ACTION_INPUT; + mInputState = NPC_INPUT_STRING; + mTextField->setText(defaultText); + buildLayout(); +} + +bool NpcDialog::isInputFocused() +{ + return mTextField->isFocused() || mIntField->isFocused(); +} + +void NpcDialog::integerRequest(int defaultValue, int min, int max) +{ + mActionState = NPC_ACTION_INPUT; + mInputState = NPC_INPUT_INTEGER; + mIntField->setRange(min, max); + mIntField->setValue(defaultValue); + buildLayout(); +} + +void NpcDialog::widgetResized(const gcn::Event &event) +{ + Window::widgetResized(event); + + setText(mText); +} + +void NpcDialog::buildLayout() +{ + clearLayout(); + + if (mActionState != NPC_ACTION_INPUT) + { + if (mActionState == NPC_ACTION_NEXT) + { + mButton->setCaption(_("Next")); + } + else if (mActionState == NPC_ACTION_CLOSE) + { + mButton->setCaption(_("Close")); + } + place(0, 0, mScrollArea, 5, 3); + place(4, 3, mButton); + } + else if (mInputState != NPC_INPUT_NONE) + { + mButton->setCaption(_("Submit")); + if (mInputState == NPC_INPUT_LIST) + { + place(0, 0, mScrollArea, 5, 3); + place(0, 3, mListScrollArea, 5, 3); + place(3, 6, mButton, 2); + } + else if(mInputState == NPC_INPUT_STRING) + { + place(0, 0, mScrollArea, 5, 3); + place(0, 3, mTextField, 3); + place(3, 4, mButton, 2); + } + else if(mInputState == NPC_INPUT_INTEGER) + { + place(0, 0, mScrollArea, 5, 3); + place(0, 3, mIntField, 3); + place(3, 4, mButton, 2); + } + } + + Layout &layout = getLayout(); + layout.setRowHeight(0, Layout::AUTO_SET); + redraw(); +} diff --git a/src/gui/npcdialog.h b/src/gui/npcdialog.h new file mode 100644 index 00000000..93ab5e54 --- /dev/null +++ b/src/gui/npcdialog.h @@ -0,0 +1,185 @@ +/* + * The Mana World + * Copyright (C) 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef NPCDIALOG_H +#define NPCDIALOG_H + +#include "gui/widgets/window.h" + +#include "npc.h" + +#include <guichan/actionlistener.hpp> +#include <guichan/listmodel.hpp> + +#include <string> +#include <vector> + +class TextBox; +class ListBox; +class TextField; +class IntTextField; +class Button; + +/** + * The npc dialog. + * + * \ingroup Interface + */ +class NpcDialog : public Window, public gcn::ActionListener, + public gcn::ListModel +{ + public: + /** + * Constructor. + * + * @see Window::Window + */ + NpcDialog(); + + /** + * Called when receiving actions from the widgets. + */ + void action(const gcn::ActionEvent &event); + + void setNpc(int npc) + { mNpcId = npc; } + + void clearText(); + + /** + * Sets the text shows in the dialog. + * + * @param string The new text. + */ + void setText(const std::string &string); + + /** + * Adds the text to the text shows in the dialog. Also adds a newline + * to the end. + * + * @param string The text to add. + */ + void addText(const std::string &string); + + /** + * When called, the widget will show a "Close" button instead of the "Next" + * And when clicked it will close the window + */ + void showCloseButton(); + + /** + * Notifies the server that client has performed a next action. + */ + void nextDialog(); + + /** + * Notifies the server that the client has performed a close action. + */ + void closeDialog(); + + /** + * Returns the number of items in the choices list. + */ + int getNumberOfElements(); + + /** + * Returns the name of item number i of the choices list. + */ + std::string getElementAt(int i); + + /** + * Makes this dialog request a choice selection from the user + */ + void choiceRequest(); + + /** + * Adds a choice to the list box + */ + void addChoice(std::string); + + /** + * Fills the options list for an NPC dialog. + * + * @param itemString A string with the options separated with colons. + */ + void parseListItems(const std::string &itemString); + + /** + * Requests a text string from the user. + */ + void textRequest(std::string defaultText = ""); + + bool isInputFocused(); + + /** + * Requests a interger from the user. + */ + void integerRequest(int defaultValue = 0, int min = 0, int max = 2000); + + /** + * Called when resizing the window. + * + * @param event The calling event + */ + void widgetResized(const gcn::Event &event); + + private: + void buildLayout(); + + int mNpcId; + + // Used for the main input area + gcn::ScrollArea *mScrollArea; + TextBox *mTextBox; + std::string mText; + + // Used for choice input + ListBox *mItemList; + gcn::ScrollArea *mListScrollArea; + std::vector<std::string> mItems; + + // Used for string and integer input + TextField *mTextField; + IntTextField *mIntField; + + // Used for the button + Button *mButton; + + enum NPCInputState { + NPC_INPUT_NONE, + NPC_INPUT_LIST, + NPC_INPUT_STRING, + NPC_INPUT_INTEGER + }; + NPCInputState mInputState; + + enum NPCActionState { + NPC_ACTION_NEXT, + NPC_ACTION_INPUT, + NPC_ACTION_CLOSE + }; + NPCActionState mActionState; +}; + +// TODO: This should be made not to be global, later. + +extern NpcDialog* npcDialog; +#endif // NPCDIALOG_H |