00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/minimap.h"
00023
00024 #include "being.h"
00025 #include "beingmanager.h"
00026 #include "configuration.h"
00027 #include "graphics.h"
00028 #include "localplayer.h"
00029
00030 #include "resources/image.h"
00031
00032 #include "utils/gettext.h"
00033
00034 #include <guichan/font.hpp>
00035
00036 bool Minimap::mShow = true;
00037
00038 Minimap::Minimap():
00039 Window(_("MiniMap")),
00040 mMapImage(NULL),
00041 mProportion(0.5)
00042 {
00043 setWindowName("MiniMap");
00044 mShow = config.getValue(getWindowName() + "Show", true);
00045 setDefaultSize(5, 25, 100, 100);
00046 setResizable(true);
00047 setDefaultVisible(true);
00048 setSaveVisible(true);
00049
00050 setStickyButton(true);
00051 setSticky(false);
00052
00053 loadWindowState();
00054 }
00055
00056 Minimap::~Minimap()
00057 {
00058 if (mMapImage)
00059 mMapImage->decRef();
00060
00061 config.setValue(getWindowName() + "Show", mShow);
00062 }
00063
00064 void Minimap::setMapImage(Image *img)
00065 {
00066 if (mMapImage)
00067 mMapImage->decRef();
00068
00069 mMapImage = img;
00070
00071 if (mMapImage)
00072 {
00073 const int offsetX = 2 * getPadding();
00074 const int offsetY = getTitleBarHeight() + getPadding();
00075 const int titleWidth = getFont()->getWidth(getCaption()) + 15;
00076 const int mapWidth = mMapImage->getWidth() < 100 ?
00077 mMapImage->getWidth() + offsetX : 100;
00078 const int mapHeight = mMapImage->getHeight() < 100 ?
00079 mMapImage->getHeight() + offsetY : 100;
00080
00081 setMinWidth(mapWidth > titleWidth ? mapWidth : titleWidth);
00082 setMinHeight(mapHeight);
00083 setMaxWidth(mMapImage->getWidth() > titleWidth ?
00084 mMapImage->getWidth() + offsetX : titleWidth);
00085 setMaxHeight(mMapImage->getHeight() + offsetY);
00086
00087 setDefaultSize(getX(), getY(), getWidth(), getHeight());
00088 resetToDefaultSize();
00089
00090 setVisible(mShow);
00091 }
00092 else
00093 {
00094 setVisible(false);
00095 }
00096 }
00097
00098 void Minimap::toggle()
00099 {
00100 setVisible(!isVisible(), true);
00101 }
00102
00103 void Minimap::draw(gcn::Graphics *graphics)
00104 {
00105 Window::draw(graphics);
00106
00107 const gcn::Rectangle a = getChildrenArea();
00108
00109 graphics->pushClipArea(a);
00110
00111 int mapOriginX = 0;
00112 int mapOriginY = 0;
00113
00114 if (mMapImage)
00115 {
00116 if (mMapImage->getWidth() > a.width ||
00117 mMapImage->getHeight() > a.height)
00118 {
00119 const Vector &p = player_node->getPosition();
00120 mapOriginX = (int) (((a.width) / 2) - (int) (p.x * mProportion) / 32);
00121 mapOriginY = (int) (((a.height) / 2) - (int) (p.y * mProportion) / 32);
00122
00123 const int minOriginX = a.width - mMapImage->getWidth();
00124 const int minOriginY = a.height - mMapImage->getHeight();
00125
00126 if (mapOriginX < minOriginX)
00127 mapOriginX = minOriginX;
00128 if (mapOriginY < minOriginY)
00129 mapOriginY = minOriginY;
00130 if (mapOriginX > 0)
00131 mapOriginX = 0;
00132 if (mapOriginY > 0)
00133 mapOriginY = 0;
00134 }
00135
00136 static_cast<Graphics*>(graphics)->
00137 drawImage(mMapImage, mapOriginX, mapOriginY);
00138 }
00139
00140 const Beings &beings = beingManager->getAll();
00141
00142 for (Beings::const_iterator bi = beings.begin(), bi_end = beings.end();
00143 bi != bi_end; ++bi)
00144 {
00145 const Being *being = (*bi);
00146 int dotSize = 2;
00147
00148 switch (being->getType()) {
00149 case Being::PLAYER:
00150 if (being == player_node)
00151 {
00152 dotSize = 3;
00153 graphics->setColor(gcn::Color(61, 209, 52));
00154 break;
00155 }
00156 graphics->setColor(gcn::Color(61, 52, 209));
00157 break;
00158
00159 case Being::MONSTER:
00160 graphics->setColor(gcn::Color(209, 52, 61));
00161 break;
00162
00163 case Being::NPC:
00164 graphics->setColor(gcn::Color(255, 255, 0));
00165 break;
00166
00167 default:
00168 continue;
00169 }
00170
00171 const int offset = (int) ((dotSize - 1) * mProportion);
00172 const Vector &pos = being->getPosition();
00173
00174 graphics->fillRectangle(gcn::Rectangle(
00175 (int) (pos.x * mProportion) / 32 + mapOriginX - offset,
00176 (int) (pos.y * mProportion) / 32 + mapOriginY - offset,
00177 dotSize, dotSize));
00178 }
00179
00180 graphics->popClipArea();
00181 }