From d2b804c1a817ccdd85b4b1220bf929e9d370d774 Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Sun, 18 Jan 2009 19:04:02 -0700 Subject: Fixed the NPC Integer input field, as well as cut some bull from the NPC String class. The Integer input field was rather horribly broken to the point where it could send invalid data, the increment and decrement buttons didn't work, and overall it was tripping over itself. As for the NPC String, "The Mana World" as a string is not needed to set the field to that length, but instead fills the text field with that text for starting. This is completely inappropriate for a text entry field, and a more sensible default would be to leave it empty, so that people can type in what they want without having to delete it. Signed-off-by: Ira Rice --- src/gui/inttextbox.cpp | 71 ------------------------------------- src/gui/inttextbox.h | 66 ---------------------------------- src/gui/inttextfield.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ src/gui/inttextfield.h | 72 +++++++++++++++++++++++++++++++++++++ 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/gui/textfield.cpp | 2 -- src/gui/textfield.h | 1 - 11 files changed, 200 insertions(+), 200 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 (limited to 'src/gui') diff --git a/src/gui/inttextbox.cpp b/src/gui/inttextbox.cpp deleted file mode 100644 index 4cb885c3..00000000 --- a/src/gui/inttextbox.cpp +++ /dev/null @@ -1,71 +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 d134fcd7..00000000 --- a/src/gui/inttextbox.h +++ /dev/null @@ -1,66 +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" - -#include "../guichanfwd.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..e8e6e69b --- /dev/null +++ b/src/gui/inttextfield.cpp @@ -0,0 +1,84 @@ +/* + * 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..3f9cd7ad --- /dev/null +++ b/src/gui/inttextfield.h @@ -0,0 +1,72 @@ +/* + * 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" + +#include "../guichanfwd.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 48b0e601..03b22fa9 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -20,7 +20,7 @@ */ #include "button.h" -#include "inttextbox.h" +#include "inttextfield.h" #include "item_amount.h" #include "slider.h" #include "trade.h" @@ -39,11 +39,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); @@ -61,7 +61,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); @@ -89,12 +89,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") { @@ -114,14 +114,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/gui/textfield.cpp b/src/gui/textfield.cpp index 0146fd4a..80e3bbcf 100644 --- a/src/gui/textfield.cpp +++ b/src/gui/textfield.cpp @@ -19,8 +19,6 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "textfield.h" - #include #include diff --git a/src/gui/textfield.h b/src/gui/textfield.h index 38150b83..3235e3f6 100644 --- a/src/gui/textfield.h +++ b/src/gui/textfield.h @@ -27,7 +27,6 @@ #include "../guichanfwd.h" class ImageRect; - class TextField; class TextFieldListener -- cgit v1.2.3-70-g09d2