00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "gui/itempopup.h"
00024
00025 #include "gui/gui.h"
00026 #include "gui/palette.h"
00027
00028 #include "gui/widgets/textbox.h"
00029
00030 #include "graphics.h"
00031 #include "units.h"
00032
00033 #include "utils/gettext.h"
00034 #include "utils/stringutils.h"
00035
00036 #include <guichan/font.hpp>
00037 #include <guichan/widgets/label.hpp>
00038
00039 ItemPopup::ItemPopup():
00040 Popup("ItemPopup")
00041 {
00042
00043 mItemName = new gcn::Label;
00044 mItemName->setFont(boldFont);
00045 mItemName->setPosition(getPadding(), getPadding());
00046
00047 const int fontHeight = getFont()->getHeight();
00048
00049
00050 mItemDesc = new TextBox;
00051 mItemDesc->setEditable(false);
00052 mItemDesc->setPosition(getPadding(), fontHeight);
00053
00054
00055 mItemEffect = new TextBox;
00056 mItemEffect->setEditable(false);
00057 mItemEffect->setPosition(getPadding(), 2 * fontHeight + 2 * getPadding());
00058
00059
00060 mItemWeight = new TextBox;
00061 mItemWeight->setEditable(false);
00062 mItemWeight->setPosition(getPadding(), 3 * fontHeight + 4 * getPadding());
00063
00064 add(mItemName);
00065 add(mItemDesc);
00066 add(mItemEffect);
00067 add(mItemWeight);
00068
00069 loadPopupConfiguration();
00070 }
00071
00072 ItemPopup::~ItemPopup()
00073 {
00074 }
00075
00076 void ItemPopup::setItem(const ItemInfo &item)
00077 {
00078 if (item.getName() == mItemName->getCaption())
00079 return;
00080
00081 mItemType = item.getType();
00082
00083 mItemName->setCaption(item.getName());
00084 mItemName->adjustSize();
00085 mItemName->setForegroundColor(getColor(mItemType));
00086
00087 mItemDesc->setTextWrapped(item.getDescription(), 196);
00088 mItemEffect->setTextWrapped(item.getEffect(), 196);
00089 mItemWeight->setTextWrapped(_("Weight: ") +
00090 Units::formatWeight(item.getWeight()), 196);
00091
00092 int minWidth = mItemName->getWidth();
00093
00094 if (mItemDesc->getMinWidth() > minWidth)
00095 minWidth = mItemDesc->getMinWidth();
00096 if (mItemEffect->getMinWidth() > minWidth)
00097 minWidth = mItemEffect->getMinWidth();
00098 if (mItemWeight->getMinWidth() > minWidth)
00099 minWidth = mItemWeight->getMinWidth();
00100
00101 minWidth += 8;
00102 setWidth(minWidth);
00103
00104 const int numRowsDesc = mItemDesc->getNumberOfRows();
00105 const int numRowsEffect = mItemEffect->getNumberOfRows();
00106
00107 const int height = getFont()->getHeight();
00108
00109 if (item.getEffect().empty())
00110 {
00111 setContentSize(minWidth, (numRowsDesc + 3) * height);
00112
00113 mItemWeight->setPosition(getPadding(), (numRowsDesc + getPadding()) *
00114 height);
00115 }
00116 else
00117 {
00118 setContentSize(minWidth, (numRowsDesc + numRowsEffect + getPadding()) *
00119 height);
00120
00121 mItemWeight->setPosition(getPadding(), (numRowsDesc + numRowsEffect +
00122 getPadding()) * height);
00123 }
00124
00125 mItemDesc->setPosition(getPadding(), 2 * height);
00126 mItemEffect->setPosition(getPadding(), (numRowsDesc + getPadding()) * height);
00127 }
00128
00129 gcn::Color ItemPopup::getColor(ItemType type)
00130 {
00131 switch (type)
00132 {
00133 case ITEM_UNUSABLE:
00134 return guiPalette->getColor(Palette::GENERIC);
00135 case ITEM_USABLE:
00136 return guiPalette->getColor(Palette::USABLE);
00137 case ITEM_EQUIPMENT_ONE_HAND_WEAPON:
00138 return guiPalette->getColor(Palette::ONEHAND);
00139 case ITEM_EQUIPMENT_TWO_HANDS_WEAPON:
00140 return guiPalette->getColor(Palette::TWOHAND);
00141 case ITEM_EQUIPMENT_TORSO:
00142 return guiPalette->getColor(Palette::TORSO);
00143 case ITEM_EQUIPMENT_ARMS:
00144 return guiPalette->getColor(Palette::ARMS);
00145 case ITEM_EQUIPMENT_HEAD:
00146 return guiPalette->getColor(Palette::HEAD);
00147 case ITEM_EQUIPMENT_LEGS:
00148 return guiPalette->getColor(Palette::LEGS);
00149 case ITEM_EQUIPMENT_SHIELD:
00150 return guiPalette->getColor(Palette::SHIELD);
00151 case ITEM_EQUIPMENT_RING:
00152 return guiPalette->getColor(Palette::RING);
00153 case ITEM_EQUIPMENT_NECKLACE:
00154 return guiPalette->getColor(Palette::NECKLACE);
00155 case ITEM_EQUIPMENT_FEET:
00156 return guiPalette->getColor(Palette::FEET);
00157 case ITEM_EQUIPMENT_AMMO:
00158 return guiPalette->getColor(Palette::AMMO);
00159 default:
00160 return guiPalette->getColor(Palette::UNKNOWN_ITEM);
00161 }
00162 }
00163
00164 void ItemPopup::view(int x, int y)
00165 {
00166 const int distance = 20;
00167
00168 int posX = std::max(0, x - getWidth() / 2);
00169 int posY = y + distance;
00170
00171 if (posX > graphics->getWidth() - getWidth())
00172 posX = graphics->getWidth() - getWidth();
00173 if (posY > graphics->getHeight() - getHeight())
00174 posY = y - getHeight() - distance;
00175
00176 setPosition(posX, posY);
00177 setVisible(true);
00178 requestMoveToTop();
00179 }