From 3fcd3755f5c5d23af31e081c59275ef94cb4e036 Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Mon, 19 Jan 2009 21:59:59 +0100 Subject: Fixes to the NPC Integer input field Also don't set "The Mana World" to start with. Signed-off-by: Ira Rice (cherry picked from Aethyra commit d2b804c1a817ccdd85b4b1220bf929e9d370d774) Conflicts: src/game.cpp src/gui/inttextfield.cpp src/gui/item_amount.cpp src/gui/textfield.h --- src/CMakeLists.txt | 4 +-- src/Makefile.am | 4 +-- src/game.cpp | 22 +++++++++++- src/gui/inttextbox.cpp | 73 ------------------------------------- src/gui/inttextbox.h | 65 --------------------------------- src/gui/inttextfield.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++ src/gui/inttextfield.h | 71 ++++++++++++++++++++++++++++++++++++ src/gui/item_amount.cpp | 24 ++++++------- src/gui/item_amount.h | 4 +-- src/gui/npcintegerdialog.cpp | 62 +++++++++++++------------------- src/gui/npcintegerdialog.h | 12 +++---- src/gui/npcstringdialog.cpp | 2 +- src/net/npchandler.cpp | 2 +- tmw.cbp | 4 +-- 14 files changed, 228 insertions(+), 206 deletions(-) delete mode 100644 src/gui/inttextbox.cpp delete mode 100644 src/gui/inttextbox.h create mode 100644 src/gui/inttextfield.cpp create mode 100644 src/gui/inttextfield.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d7127801..70a58272 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -95,8 +95,8 @@ SET(SRCS gui/gui.h gui/help.cpp gui/help.h - gui/inttextbox.cpp - gui/inttextbox.h + gui/inttextfield.cpp + gui/inttextfield.h gui/inventorywindow.cpp gui/inventorywindow.h gui/itemcontainer.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 97b62dd9..358b0b61 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,8 +47,8 @@ tmw_SOURCES = gui/widgets/layout.cpp \ gui/gui.h \ gui/help.cpp \ gui/help.h \ - gui/inttextbox.h \ - gui/inttextbox.cpp \ + gui/inttextfield.h \ + gui/inttextfield.cpp \ gui/inventorywindow.cpp \ gui/inventorywindow.h \ gui/itemcontainer.cpp \ diff --git a/src/game.cpp b/src/game.cpp index 1b6f2d5c..6cd94760 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -627,6 +627,26 @@ void Game::handleInput() { setupWindow->action(gcn::ActionEvent(NULL, "cancel")); } + // Submits the text and proceeds to the next dialog + else if (npcStringDialog->isVisible()) + { + npcStringDialog->action(gcn::ActionEvent(NULL, "ok")); + } + // Proceed to the next dialog option, or close the window + else if (npcTextDialog->isVisible()) + { + npcTextDialog->action(gcn::ActionEvent(NULL, "ok")); + } + // Choose the currently highlighted dialogue option + else if (npcListDialog->isVisible()) + { + npcListDialog->action(gcn::ActionEvent(NULL, "ok")); + } + // Submits the text and proceeds to the next dialog + else if (npcIntegerDialog->isVisible()) + { + npcIntegerDialog->action(gcn::ActionEvent(NULL, "ok")); + } // Else, open the chat edit box else { @@ -634,7 +654,7 @@ void Game::handleInput() used = true; } break; - // Quitting confirmation dialog + // Quitting confirmation dialog case SDLK_ESCAPE: if (!exitConfirm) { exitConfirm = new ConfirmDialog( diff --git a/src/gui/inttextbox.cpp b/src/gui/inttextbox.cpp deleted file mode 100644 index a995f084..00000000 --- a/src/gui/inttextbox.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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. - * - * The Mana World 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 The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "inttextbox.h" - -#include "sdlinput.h" - -#include "../utils/tostring.h" - -IntTextBox::IntTextBox(int i): - mValue(i) -{ -} - -void -IntTextBox::keyPressed(gcn::KeyEvent &event) -{ - const gcn::Key &key = event.getKey(); - - if (key.getValue() == Key::BACKSPACE || - key.getValue() == Key::DELETE) - { - setText(std::string()); - event.consume(); - } - - if (!key.isNumber()) return; - TextField::keyPressed(event); - - std::istringstream s(getText()); - int i; - s >> i; - setInt(i); -} - -void IntTextBox::setRange(int min, int max) -{ - mMin = min; - mMax = max; -} - -int IntTextBox::getInt() -{ - return getText().empty() ? mMin : mValue; -} - -void IntTextBox::setInt(int i) -{ - if (i >= mMin && i <= mMax) - mValue = i; - - const std::string valStr = toString(mValue); - setText(valStr); - setCaretPosition(valStr.length() + 1); -} diff --git a/src/gui/inttextbox.h b/src/gui/inttextbox.h deleted file mode 100644 index 8dad0c39..00000000 --- a/src/gui/inttextbox.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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. - * - * The Mana World 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 The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef INTTEXTBOX_H -#define INTTEXTBOX_H - -#include "textfield.h" - -/** - * TextBox which only accepts numbers as input. - */ -class IntTextBox : public TextField -{ - public: - /** - * Constructor, sets initial value. - */ - IntTextBox(int value=0); - - /** - * Sets the minimum and maximum values of the text box. - */ - void setRange(int minimum, int maximum); - - /** - * Returns the value in the text box. - */ - int getInt(); - - /** - * Set the value of the text box to the specified value. - */ - void setInt(int value); - - /** - * Responds to key presses. - */ - void - keyPressed(gcn::KeyEvent &event); - - private: - int mMin; /**< Minimum value */ - int mMax; /**< Maximum value */ - int mValue; /**< Current value */ -}; - -#endif diff --git a/src/gui/inttextfield.cpp b/src/gui/inttextfield.cpp new file mode 100644 index 00000000..5d668ccc --- /dev/null +++ b/src/gui/inttextfield.cpp @@ -0,0 +1,85 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "inttextfield.h" + +#include "sdlinput.h" + +#include "../utils/tostring.h" + +IntTextField::IntTextField(int def): + TextField(toString(def)), + mDefault(def), + mValue(def) +{ +} + +void IntTextField::keyPressed(gcn::KeyEvent &event) +{ + const gcn::Key &key = event.getKey(); + + if (key.getValue() == Key::BACKSPACE || + key.getValue() == Key::DELETE) + { + setText(std::string()); + event.consume(); + } + + if (!key.isNumber()) + return; + + TextField::keyPressed(event); + + std::istringstream s(getText()); + int i; + s >> i; + setValue(i); +} + +void IntTextField::setRange(int min, int max) +{ + mMin = min; + mMax = max; +} + +int IntTextField::getValue() +{ + return getText().empty() ? mMin : mValue; +} + +void IntTextField::setValue(int i) +{ + if (i >= mMin && i <= mMax) + mValue = i; + else if (i < mMin) + mValue = mMin; + else if (i > mMax) + mValue = mMax; + + const std::string valStr = toString(mValue); + setText(valStr); + setCaretPosition(valStr.length() + 1); +} + +void IntTextField::reset() +{ + setValue(mDefault); +} diff --git a/src/gui/inttextfield.h b/src/gui/inttextfield.h new file mode 100644 index 00000000..ab301141 --- /dev/null +++ b/src/gui/inttextfield.h @@ -0,0 +1,71 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef INTTEXTFIELD_H +#define INTTEXTFIELD_H + +#include "textfield.h" + +/** + * TextBox which only accepts numbers as input. + */ +class IntTextField : public TextField +{ + public: + /** + * Constructor, sets default value. + */ + IntTextField(int def = 0); + + /** + * Sets the minimum and maximum values of the text box. + */ + void setRange(int minimum, int maximum); + + /** + * Returns the value in the text box. + */ + int getValue(); + + /** + * Reset the field to the default value. + */ + void reset(); + + /** + * Set the value of the text box to the specified value. + */ + void setValue(int value); + + /** + * Responds to key presses. + */ + void + keyPressed(gcn::KeyEvent &event); + + private: + int mMin; /**< Minimum value */ + int mMax; /**< Maximum value */ + int mDefault; /**< Default value */ + int mValue; /**< Current value */ +}; + +#endif diff --git a/src/gui/item_amount.cpp b/src/gui/item_amount.cpp index 17c3243a..cd6c6937 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -22,7 +22,7 @@ #include "item_amount.h" #include "button.h" -#include "inttextbox.h" +#include "inttextfield.h" #include "slider.h" #include "trade.h" @@ -40,11 +40,11 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent, Item *item): const int maxRange = mItem->getQuantity(); // Integer field - mItemAmountTextBox = new IntTextBox(1); - mItemAmountTextBox->setRange(1, maxRange); - mItemAmountTextBox->setWidth(30); - mItemAmountTextBox->setActionEventId("Dummy"); - mItemAmountTextBox->addActionListener(this); + mItemAmountTextField = new IntTextField(1); + mItemAmountTextField->setRange(1, maxRange); + mItemAmountTextField->setWidth(30); + mItemAmountTextField->setActionEventId("Dummy"); + mItemAmountTextField->addActionListener(this); // Slider mItemAmountSlide = new Slider(1.0, maxRange); @@ -62,7 +62,7 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent, Item *item): // Set positions place(0, 0, minusButton); - place(1, 0, mItemAmountTextBox).setPadding(2); + place(1, 0, mItemAmountTextField).setPadding(2); place(2, 0, plusButton); place(0, 1, mItemAmountSlide, 6); place(4, 2, okButton); @@ -90,12 +90,12 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent, Item *item): void ItemAmountWindow::resetAmount() { - mItemAmountTextBox->setInt(1); + mItemAmountTextField->setValue(1); } void ItemAmountWindow::action(const gcn::ActionEvent &event) { - int amount = mItemAmountTextBox->getInt(); + int amount = mItemAmountTextField->getValue(); if (event.getId() == "Cancel") { @@ -115,14 +115,14 @@ void ItemAmountWindow::action(const gcn::ActionEvent &event) } else if (event.getId() == "Drop") { - player_node->dropItem(mItem, mItemAmountTextBox->getInt()); + player_node->dropItem(mItem, mItemAmountTextField->getValue()); scheduleDelete(); } else if (event.getId() == "AddTrade") { - tradeWindow->tradeItem(mItem, mItemAmountTextBox->getInt()); + tradeWindow->tradeItem(mItem, mItemAmountTextField->getValue()); scheduleDelete(); } - mItemAmountTextBox->setInt(amount); + mItemAmountTextField->setValue(amount); mItemAmountSlide->setValue(amount); } diff --git a/src/gui/item_amount.h b/src/gui/item_amount.h index 2005094d..08852c8f 100644 --- a/src/gui/item_amount.h +++ b/src/gui/item_amount.h @@ -30,7 +30,7 @@ #include "../guichanfwd.h" -class IntTextBox; +class IntTextField; class Item; #define AMOUNT_TRADE_ADD 1 @@ -60,7 +60,7 @@ class ItemAmountWindow : public Window, public gcn::ActionListener void resetAmount(); private: - IntTextBox *mItemAmountTextBox; /**< Item amount caption. */ + IntTextField *mItemAmountTextField; /**< Item amount caption. */ Item *mItem; /** diff --git a/src/gui/npcintegerdialog.cpp b/src/gui/npcintegerdialog.cpp index f5b6ac5b..75dd52c8 100644 --- a/src/gui/npcintegerdialog.cpp +++ b/src/gui/npcintegerdialog.cpp @@ -25,7 +25,7 @@ #include #include "button.h" -#include "textfield.h" +#include "inttextfield.h" #include "../npc.h" @@ -39,18 +39,25 @@ NpcIntegerDialog::NpcIntegerDialog(): { mDecButton = new Button("-", "decvalue", this); mIncButton = new Button("+", "incvalue", this); - mValueField = new TextField(); + mValueField = new IntTextField(); okButton = new Button(_("OK"), "ok", this); cancelButton = new Button(_("Cancel"), "cancel", this); + resetButton = new Button(_("Reset"), "reset", this); mDecButton->setSize(20, 20); mIncButton->setSize(20, 20); + ContainerPlacer place; + place = getPlacer(0, 0); + place(0, 0, mDecButton); place(1, 0, mValueField, 3); place(4, 0, mIncButton); - place(2, 1, okButton); - place(3, 1, cancelButton, 2); + place.getCell().matchColWidth(1, 0); + place = getPlacer(0, 1); + place(0, 0, resetButton); + place(2, 0, cancelButton); + place(3, 0, okButton); reflowLayout(175, 0); setLocationRelativeTo(getParent()); @@ -59,19 +66,14 @@ NpcIntegerDialog::NpcIntegerDialog(): mValueField->addKeyListener(this); } -void NpcIntegerDialog::prepDialog(const int min, const int def, const int max) +void NpcIntegerDialog::setRange(const int min, const int max) { - mMin = min; - mMax = max; - mDefault = def; - mValue = def; - - mValueField->setText(toString(mValue)); + mValueField->setRange(min, max); } int NpcIntegerDialog::getValue() { - return mValue; + return mValueField->getValue(); } void NpcIntegerDialog::action(const gcn::ActionEvent &event) @@ -85,41 +87,25 @@ void NpcIntegerDialog::action(const gcn::ActionEvent &event) else if (event.getId() == "cancel") { finish = 1; - mValue = mDefault; + mValueField->reset(); } - else if (event.getId() == "decvalue" && mValue < mMin) + else if (event.getId() == "decvalue") { - mValue--; + mValueField->setValue(mValueField->getValue() - 1); } - else if (event.getId() == "incvalue" && mValue > mMax) + else if (event.getId() == "incvalue") { - mValue++; + mValueField->setValue(mValueField->getValue() + 1); + } + else if (event.getId() == "reset") + { + mValueField->reset(); } - - mValueField->setText(toString(mValue)); if (finish) { setVisible(false); - current_npc->integerInput(mValue); + current_npc->integerInput(mValueField->getValue()); current_npc = 0; } } - -void NpcIntegerDialog::keyPressed(gcn::KeyEvent &event) -{ - std::stringstream tempValue(mValueField->getText()); - int value; - tempValue >> value; - if (value < mMin) - { - value = mMin; - } - if (value > mMax) - { - value = mMax; - } - - mValue = value; - mValueField->setText(toString(value)); -} diff --git a/src/gui/npcintegerdialog.h b/src/gui/npcintegerdialog.h index a45d57c4..c1bdffe1 100644 --- a/src/gui/npcintegerdialog.h +++ b/src/gui/npcintegerdialog.h @@ -32,6 +32,8 @@ #include "../guichanfwd.h" +class IntTextField; + /** * The npc integer input dialog. * @@ -53,9 +55,6 @@ class NpcIntegerDialog : public Window, public gcn::ActionListener, */ void action(const gcn::ActionEvent &event); - /** Called when key is pressed */ - void keyPressed(gcn::KeyEvent &event); - /** * Returns the current value. */ @@ -65,18 +64,17 @@ class NpcIntegerDialog : public Window, public gcn::ActionListener, * Prepares the NPC dialog. * * @param min The minimum value to allow - * @param def The default value * @param max The maximum value to allow */ - void prepDialog(const int min, const int def, const int max); + void setRange(const int min, const int max); private: - int mMin, mMax, mDefault, mValue; gcn::Button *mDecButton; gcn::Button *mIncButton; - gcn::TextField *mValueField; + IntTextField *mValueField; gcn::Button *okButton; gcn::Button *cancelButton; + gcn::Button *resetButton; }; #endif // _TMW_GUI_NPCINTEGERDIALOG_H diff --git a/src/gui/npcstringdialog.cpp b/src/gui/npcstringdialog.cpp index 6bca961c..53f200b0 100644 --- a/src/gui/npcstringdialog.cpp +++ b/src/gui/npcstringdialog.cpp @@ -37,7 +37,7 @@ NpcStringDialog::NpcStringDialog(): Window(_("NPC Text Request")) { - mValueField = new TextField("The Mana World"); // Just a sizing value :) + mValueField = new TextField(""); okButton = new Button(_("OK"), "ok", this); cancelButton = new Button(_("Cancel"), "cancel", this); diff --git a/src/net/npchandler.cpp b/src/net/npchandler.cpp index d8763f43..6355df7a 100644 --- a/src/net/npchandler.cpp +++ b/src/net/npchandler.cpp @@ -89,7 +89,7 @@ void NPCHandler::handleMessage(MessageIn *msg) // Request for an integer id = msg->readInt32(); current_npc = dynamic_cast(beingManager->findBeing(id)); - npcIntegerDialog->prepDialog(0, 0, 2147483647); + npcIntegerDialog->setRange(0, 2147483647); npcIntegerDialog->setVisible(true); break; diff --git a/tmw.cbp b/tmw.cbp index c7d37951..7275627b 100644 --- a/tmw.cbp +++ b/tmw.cbp @@ -136,8 +136,8 @@ - - + + -- cgit v1.2.3-70-g09d2