summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-12-16 23:18:55 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-12-16 23:18:55 +0000
commitdc53baaaf65943c6768e3ad209129609334133e8 (patch)
treecfd4ba52d94e2f2238195addaa53f80f4013f36a /src
parent35afca876ece89be5b282c73a200d0d3c5d27fe0 (diff)
downloadmana-dc53baaaf65943c6768e3ad209129609334133e8.tar.gz
mana-dc53baaaf65943c6768e3ad209129609334133e8.tar.bz2
mana-dc53baaaf65943c6768e3ad209129609334133e8.tar.xz
mana-dc53baaaf65943c6768e3ad209129609334133e8.zip
Subclassed ShopListBox from ListBox to get rid of some duplicated code, and
fixed a problem with scrolling to the current selection. Also aligned the text a bit better and put the price in parenthesis.
Diffstat (limited to 'src')
-rw-r--r--src/gui/buy.cpp57
-rw-r--r--src/gui/listbox.cpp7
-rw-r--r--src/gui/listbox.h4
-rw-r--r--src/gui/sell.cpp3
-rw-r--r--src/gui/shop.cpp2
-rw-r--r--src/gui/shoplistbox.cpp138
-rw-r--r--src/gui/shoplistbox.h20
-rw-r--r--src/resources/openglsdlimageloader.cpp3
-rw-r--r--src/resources/openglsdlimageloader.h3
9 files changed, 92 insertions, 145 deletions
diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp
index 91488595..c701499c 100644
--- a/src/gui/buy.cpp
+++ b/src/gui/buy.cpp
@@ -88,7 +88,6 @@ BuyDialog::BuyDialog(Network *network):
mShopItemList->setEventId("item");
mSlider->setEventId("slider");
- mShopItemList->addActionListener(this);
mShopItemList->addSelectionListener(this);
mSlider->addActionListener(this);
@@ -115,7 +114,7 @@ void BuyDialog::setMoney(int amount)
{
mMoney = amount;
mShopItemList->setPlayersMoney(amount);
- mMoneyLabel->setCaption("Price : 0 GP / " + toString(mMoney) + " GP");
+ mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
mMoneyLabel->adjustSize();
}
@@ -132,7 +131,7 @@ void BuyDialog::reset()
mDecreaseButton->setEnabled(false);
mQuantityLabel->setCaption("0");
mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Price : 0 GP / " + toString(mMoney) + " GP");
+ mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
mMoneyLabel->adjustSize();
mItemDescLabel->setCaption("");
mItemEffectLabel->setCaption("");
@@ -148,31 +147,7 @@ void BuyDialog::action(const std::string& eventId, gcn::Widget* widget)
{
int selectedItem = mShopItemList->getSelected();
- if (eventId == "item")
- {
- // Reset amount of items and update labels
- mAmountItems = 0;
- mSlider->setValue(0);
- mQuantityLabel->setCaption("0");
- mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Price : 0 GP / " + toString(mMoney) + " GP");
- mMoneyLabel->adjustSize();
-
- // Disable buttons for buying and decreasing
- mBuyButton->setEnabled(false);
- mDecreaseButton->setEnabled(false);
-
- // If no item was selected, none can be bought, otherwise
- // calculate how many the player can afford
- mMaxItems = (mShopItemList->getSelected() == -1) ? 0 :
- mMoney / mShopItems->at(selectedItem).price;
-
- // When at least one item can be bought, enable the slider and the
- // increase button
- mIncreaseButton->setEnabled(mMaxItems > 0);
- mSlider->setEnabled(mMaxItems > 0);
- }
- else if (eventId == "quit")
+ if (eventId == "quit")
{
setVisible(false);
current_npc = 0;
@@ -254,7 +229,7 @@ void BuyDialog::action(const std::string& eventId, gcn::Widget* widget)
mQuantityLabel->adjustSize();
int price = mAmountItems * mShopItems->at(selectedItem).price;
- mMoneyLabel->setCaption("Price : " + toString(price) + " GP / "
+ mMoneyLabel->setCaption("Price: " + toString(price) + " GP / "
+ toString(mMoney) + " GP" );
mMoneyLabel->adjustSize();
}
@@ -262,19 +237,39 @@ void BuyDialog::action(const std::string& eventId, gcn::Widget* widget)
void BuyDialog::selectionChanged(const SelectionEvent &event)
{
+ // Reset amount of items and update labels
+ mAmountItems = 0;
+ mSlider->setValue(0);
+ mQuantityLabel->setCaption("0");
+ mQuantityLabel->adjustSize();
+ mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
+ mMoneyLabel->adjustSize();
+
+ // Disable buttons for buying and decreasing
+ mBuyButton->setEnabled(false);
+ mDecreaseButton->setEnabled(false);
+
int selectedItem = mShopItemList->getSelected();
if (selectedItem > -1)
{
- const ItemInfo &info =
- ItemDB::get(mShopItems->at(selectedItem).id);
+ const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id);
mItemDescLabel->setCaption("Description: " + info.getDescription());
mItemEffectLabel->setCaption("Effect: " + info.getEffect());
+
+ // Calculate how many the player can afford
+ mMaxItems = mMoney / mShopItems->at(selectedItem).price;
}
else
{
mItemDescLabel->setCaption("Description:");
mItemEffectLabel->setCaption("Effect:");
+ mMaxItems = 0;
}
+
+ // When at least one item can be bought, enable the slider and the
+ // increase button
+ mIncreaseButton->setEnabled(mMaxItems > 0);
+ mSlider->setEnabled(mMaxItems > 0);
}
diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp
index 14626d06..d4a2c6cb 100644
--- a/src/gui/listbox.cpp
+++ b/src/gui/listbox.cpp
@@ -38,9 +38,8 @@ ListBox::ListBox(gcn::ListModel *listModel):
void ListBox::draw(gcn::Graphics *graphics)
{
- if (mListModel == NULL) {
+ if (!mListModel)
return;
- }
graphics->setColor(gcn::Color(110, 160, 255));
graphics->setFont(getFont());
@@ -54,7 +53,9 @@ void ListBox::draw(gcn::Graphics *graphics)
}
// Draw the list elements
- for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); ++i, y += fontHeight)
+ for (int i = 0, y = 0;
+ i < mListModel->getNumberOfElements();
+ ++i, y += fontHeight)
{
graphics->drawText(mListModel->getElementAt(i), 1, y);
}
diff --git a/src/gui/listbox.h b/src/gui/listbox.h
index c1932f54..deca07cf 100644
--- a/src/gui/listbox.h
+++ b/src/gui/listbox.h
@@ -73,9 +73,9 @@ class ListBox : public gcn::ListBox
/**
* Sets the index of the selected element.
*/
- void setSelected(int selected);
+ virtual void setSelected(int selected);
- private:
+ protected:
/**
* Sends out selection events to the list of selection listeners.
*/
diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp
index 0137fdde..24556567 100644
--- a/src/gui/sell.cpp
+++ b/src/gui/sell.cpp
@@ -146,7 +146,8 @@ void SellDialog::addItem(Item *item, int price)
ITEM_SHOP item_shop;
- item_shop.name = item->getInfo().getName() + " " + toString(price) + " GP";
+ item_shop.name = item->getInfo().getName()
+ + " (" + toString(price) + " GP)";
item_shop.price = price;
item_shop.index = item->getInvIndex();
item_shop.id = item->getId();
diff --git a/src/gui/shop.cpp b/src/gui/shop.cpp
index 2d33e8a8..3d972bc2 100644
--- a/src/gui/shop.cpp
+++ b/src/gui/shop.cpp
@@ -45,7 +45,7 @@ void ShopItems::addItem(short id, int price)
ITEM_SHOP item_shop;
item_shop.name = ItemDB::get(id).getName()
- + " " + toString(price) + " GP";
+ + " (" + toString(price) + " GP)";
item_shop.price = price;
item_shop.id = id;
item_shop.image = ItemDB::get(id).getImage();
diff --git a/src/gui/shoplistbox.cpp b/src/gui/shoplistbox.cpp
index 61abff35..f5581323 100644
--- a/src/gui/shoplistbox.cpp
+++ b/src/gui/shoplistbox.cpp
@@ -34,11 +34,10 @@
#include "../graphics.h"
-const int ITEM_SPRITE_HEIGHT = 32;
+const int ITEM_ICON_SIZE = 32;
ShopListBox::ShopListBox(gcn::ListModel *listModel):
- gcn::ListBox(listModel),
- mMousePressed(false),
+ ListBox(listModel),
mPlayerMoney(0)
{
mRowHeight = getFont()->getHeight();
@@ -46,105 +45,97 @@ ShopListBox::ShopListBox(gcn::ListModel *listModel):
}
ShopListBox::ShopListBox(gcn::ListModel *listModel, ShopItems *shopListModel):
- gcn::ListBox(listModel),
- mMousePressed(false),
+ ListBox(listModel),
mPlayerMoney(0),
mShopItems(shopListModel)
{
- mRowHeight = (getFont()->getHeight() > ITEM_SPRITE_HEIGHT ?
- getFont()->getHeight() : ITEM_SPRITE_HEIGHT);
+ mRowHeight = std::max(getFont()->getHeight(), ITEM_ICON_SIZE);
mPriceCheck = true;
}
-
void ShopListBox::setPlayersMoney(int money)
{
mPlayerMoney = money;
}
-void ShopListBox::draw(gcn::Graphics *graphics)
+void ShopListBox::draw(gcn::Graphics *gcnGraphics)
{
- if (mListModel == NULL) {
+ if (!mListModel)
return;
- }
+
+ Graphics *graphics = static_cast<Graphics*>(gcnGraphics);
graphics->setFont(getFont());
// Draw the list elements
- for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); ++i, y += mRowHeight)
+ for (int i = 0, y = 0;
+ i < mListModel->getNumberOfElements();
+ ++i, y += mRowHeight)
{
- graphics->setColor(gcn::Color(0xffffff));
- if (mShopItems != NULL)
+ gcn::Color backgroundColor = gcn::Color(0xffffff);
+
+ if (i == mSelected)
+ {
+ backgroundColor = gcn::Color(110, 160, 255);
+ }
+ else if (mShopItems &&
+ mPlayerMoney < mShopItems->at(i).price && mPriceCheck)
{
- if(mPlayerMoney < mShopItems->at(i).price && mPriceCheck)
- {
- graphics->setColor(gcn::Color(0x919191));
- }
+ backgroundColor = gcn::Color(0x919191);
}
+
+ graphics->setColor(backgroundColor);
graphics->fillRectangle(gcn::Rectangle(0, y, getWidth(), mRowHeight));
- if (mShopItems)
- dynamic_cast<Graphics*>(graphics)->drawImage(mShopItems->at(i).image, 1, y);
- graphics->drawText(mListModel->getElementAt(i), ITEM_SPRITE_HEIGHT, y);
- }
- // Draw rectangle below the selected list element and the list element
- // not shown.
- if (mSelected >= 0) {
- graphics->setColor(gcn::Color(110, 160, 255));
- graphics->fillRectangle(gcn::Rectangle(0, mRowHeight * mSelected,
- getWidth(), mRowHeight));
if (mShopItems)
- dynamic_cast<Graphics*>(graphics)->drawImage(
- mShopItems->at(mSelected).image, 1, mRowHeight * mSelected);
- graphics->drawText(mListModel->getElementAt(mSelected),
- ITEM_SPRITE_HEIGHT, mRowHeight * mSelected);
+ {
+ graphics->drawImage(mShopItems->at(i).image, 1, y);
+ }
+ graphics->drawText(mListModel->getElementAt(i), ITEM_ICON_SIZE + 5,
+ y + (ITEM_ICON_SIZE - getFont()->getHeight()) / 2);
}
}
void ShopListBox::setSelected(int selected)
{
- gcn::ListBox::setSelected(selected);
- if (mListModel != NULL)
+ if (!mListModel)
{
- gcn::BasicContainer *par = getParent();
- if (par == NULL)
- {
- return;
- }
-
- gcn::Rectangle scroll;
+ mSelected = -1;
+ }
+ else
+ {
+ // Update mSelected with bounds checking
+ mSelected = std::min(mListModel->getNumberOfElements() - 1,
+ std::max(-1, selected));
- if (mSelected < 0)
+ gcn::BasicContainer *parent = getParent();
+ if (parent)
{
- scroll.y = 0;
+ gcn::Rectangle scroll;
+ scroll.y = (mSelected < 0) ? 0 : mRowHeight * mSelected;
+ scroll.height = mRowHeight;
+ parent->showWidgetPart(this, scroll);
}
- else
- {
- scroll.y = mRowHeight * mSelected;
- }
-
- scroll.height = mRowHeight;
- par->showWidgetPart(this, scroll);
}
+
fireSelectionChangedEvent();
}
void ShopListBox::mousePress(int x, int y, int button)
{
-
- bool enoughMoney = false;
if (button == gcn::MouseInput::LEFT && hasMouse())
{
- if (mShopItems)
+ bool enoughMoney = false;
+
+ if (mShopItems && mPriceCheck)
{
- if(mPlayerMoney >= mShopItems->at(y / mRowHeight).price)
+ if (mPlayerMoney >= mShopItems->at(y / mRowHeight).price)
enoughMoney = true;
}
else // Old Behaviour
+ {
enoughMoney = true;
-
- if (!mPriceCheck)
- enoughMoney = true;
+ }
if (enoughMoney)
{
@@ -155,40 +146,9 @@ void ShopListBox::mousePress(int x, int y, int button)
}
}
-void ShopListBox::mouseRelease(int x, int y, int button)
-{
- gcn::ListBox::mouseRelease(x, y, button);
-
- mMousePressed = false;
-}
-
-void ShopListBox::mouseMotion(int x, int y)
-{
- gcn::ListBox::mouseMotion(x, y);
-
- // Pretend mouse is pressed continuously while dragged. Causes list
- // selection to be updated as is default in many GUIs.
- if (mMousePressed)
- {
- mousePress(x, y, gcn::MouseInput::LEFT);
- }
-}
-
-void ShopListBox::fireSelectionChangedEvent()
-{
- SelectionEvent event(this);
- SelectionListeners::iterator i_end = mListeners.end();
- SelectionListeners::iterator i;
-
- for (i = mListeners.begin(); i != i_end; ++i)
- {
- (*i)->selectionChanged(event);
- }
-}
-
void ShopListBox::adjustSize()
{
- if (mListModel != NULL)
+ if (mListModel)
{
setHeight(mRowHeight * mListModel->getNumberOfElements());
}
diff --git a/src/gui/shoplistbox.h b/src/gui/shoplistbox.h
index 2dff8977..476564b2 100644
--- a/src/gui/shoplistbox.h
+++ b/src/gui/shoplistbox.h
@@ -21,10 +21,10 @@
* $Id: listbox.h 2655 2006-09-03 21:25:02Z b_lindeijer $
*/
-#ifndef _TMW_LISTBOX_H
-#define _TMW_LISTBOX_H
+#ifndef _TMW_SHOPLISTBOX_H
+#define _TMW_SHOPLISTBOX_H
-#include <guichan/widgets/listbox.hpp>
+#include "listbox.h"
#include "shop.h"
class SelectionListener;
@@ -36,7 +36,7 @@ class SelectionListener;
*
* \ingroup GUI
*/
-class ShopListBox : public gcn::ListBox
+class ShopListBox : public ListBox
{
public:
/**
@@ -55,8 +55,6 @@ class ShopListBox : public gcn::ListBox
void draw(gcn::Graphics *graphics);
void mousePress(int x, int y, int button);
- void mouseRelease(int x, int y, int button);
- void mouseMotion(int x, int y);
/**
* Adds a listener to the list that's notified each time a change to
@@ -98,15 +96,6 @@ class ShopListBox : public gcn::ListBox
void setPriceCheck(bool check);
private:
- /**
- * Sends out selection events to the list of selection listeners.
- */
- void fireSelectionChangedEvent();
-
- bool mMousePressed; /**< Keeps track of mouse pressed status. */
-
- std::list<SelectionListener*> mListeners;
-
int mPlayerMoney;
/**
@@ -118,7 +107,6 @@ class ShopListBox : public gcn::ListBox
int mRowHeight; /**< Row Height */
bool mPriceCheck;
-
};
#endif
diff --git a/src/resources/openglsdlimageloader.cpp b/src/resources/openglsdlimageloader.cpp
index c7ddec74..5915fb49 100644
--- a/src/resources/openglsdlimageloader.cpp
+++ b/src/resources/openglsdlimageloader.cpp
@@ -29,7 +29,8 @@
#include "resourcemanager.h"
-SDL_Surface* OpenGLSDLImageLoader::loadSDLSurface(const std::string& filename)
+SDL_Surface*
+OpenGLSDLImageLoader::loadSDLSurface(const std::string &filename)
{
ResourceManager *resman = ResourceManager::getInstance();
return resman->loadSDLSurface(filename);
diff --git a/src/resources/openglsdlimageloader.h b/src/resources/openglsdlimageloader.h
index 29be294c..b79dde15 100644
--- a/src/resources/openglsdlimageloader.h
+++ b/src/resources/openglsdlimageloader.h
@@ -31,7 +31,8 @@
class OpenGLSDLImageLoader : public gcn::OpenGLSDLImageLoader
{
protected:
- SDL_Surface* loadSDLSurface(const std::string& filename);
+ SDL_Surface*
+ loadSDLSurface(const std::string &filename);
};
#endif