00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/gui.h"
00023
00024 #include "gui/focushandler.h"
00025 #include "gui/palette.h"
00026 #include "gui/sdlinput.h"
00027 #include "gui/skin.h"
00028 #include "gui/truetypefont.h"
00029 #include "gui/viewport.h"
00030
00031 #include "gui/widgets/window.h"
00032 #include "gui/widgets/windowcontainer.h"
00033
00034 #include "configlistener.h"
00035 #include "configuration.h"
00036 #include "graphics.h"
00037 #include "log.h"
00038
00039 #include "resources/image.h"
00040 #include "resources/imageset.h"
00041 #include "resources/imageloader.h"
00042 #include "resources/resourcemanager.h"
00043
00044 #include <guichan/exception.hpp>
00045 #include <guichan/image.hpp>
00046
00047
00048 Gui *gui = 0;
00049 Viewport *viewport = 0;
00050 SDLInput *guiInput = 0;
00051
00052
00053 gcn::Font *boldFont = 0;
00054
00055 class GuiConfigListener : public ConfigListener
00056 {
00057 public:
00058 GuiConfigListener(Gui *g):
00059 mGui(g)
00060 {}
00061
00062 void optionChanged(const std::string &name)
00063 {
00064 if (name == "customcursor")
00065 {
00066 bool bCustomCursor = config.getValue("customcursor", 1) == 1;
00067 mGui->setUseCustomCursor(bCustomCursor);
00068 }
00069 }
00070 private:
00071 Gui *mGui;
00072 };
00073
00074 Gui::Gui(Graphics *graphics):
00075 mCustomCursor(false),
00076 mMouseCursors(NULL),
00077 mMouseCursorAlpha(1.0f),
00078 mMouseInactivityTimer(0),
00079 mCursorType(CURSOR_POINTER)
00080 {
00081 logger->log("Initializing GUI...");
00082
00083 setGraphics(graphics);
00084
00085
00086 static ImageLoader imageLoader;
00087 gcn::Image::setImageLoader(&imageLoader);
00088
00089
00090 guiInput = new SDLInput;
00091 setInput(guiInput);
00092
00093
00094 delete mFocusHandler;
00095 mFocusHandler = new FocusHandler;
00096
00097
00098 WindowContainer *guiTop = new WindowContainer;
00099 guiTop->setDimension(gcn::Rectangle(0, 0,
00100 graphics->getWidth(), graphics->getHeight()));
00101 guiTop->setOpaque(false);
00102 Window::setWindowContainer(guiTop);
00103 setTop(guiTop);
00104
00105 ResourceManager *resman = ResourceManager::getInstance();
00106
00107
00108 const int fontSize = (int) config.getValue("fontSize", 11);
00109 std::string fontFile = branding.getValue("font", "fonts/dejavusans.ttf");
00110 std::string path = resman->getPath(fontFile);
00111 try
00112 {
00113 mGuiFont = new TrueTypeFont(path, fontSize);
00114 mInfoParticleFont = new TrueTypeFont(path, fontSize, TTF_STYLE_BOLD);
00115 }
00116 catch (gcn::Exception e)
00117 {
00118 logger->error(std::string("Unable to load '") + fontFile +
00119 std::string("': ") + e.getMessage());
00120 }
00121
00122
00123 fontFile = branding.getValue("boldFont", "fonts/dejavusans-bold.ttf");
00124 path = resman->getPath(fontFile);
00125 try
00126 {
00127 boldFont = new TrueTypeFont(path, fontSize);
00128 }
00129 catch (gcn::Exception e)
00130 {
00131 logger->error(std::string("Unable to load '") + fontFile +
00132 std::string("': ") + e.getMessage());
00133 }
00134
00135 gcn::Widget::setGlobalFont(mGuiFont);
00136
00137
00138 setUseCustomCursor(config.getValue("customcursor", 1) == 1);
00139 mConfigListener = new GuiConfigListener(this);
00140 config.addListener("customcursor", mConfigListener);
00141
00142
00143 viewport = new Viewport;
00144 viewport->setDimension(gcn::Rectangle(0, 0,
00145 graphics->getWidth(), graphics->getHeight()));
00146 guiTop->add(viewport);
00147 }
00148
00149 Gui::~Gui()
00150 {
00151 config.removeListener("customcursor", mConfigListener);
00152 delete mConfigListener;
00153
00154 if (mMouseCursors)
00155 mMouseCursors->decRef();
00156
00157 delete mGuiFont;
00158 delete boldFont;
00159 delete mInfoParticleFont;
00160 delete viewport;
00161 delete getTop();
00162
00163 delete guiInput;
00164
00165 SkinLoader::deleteInstance();
00166 }
00167
00168 void Gui::logic()
00169 {
00170
00171 if (mMouseInactivityTimer < 100 * 15)
00172 {
00173 ++mMouseInactivityTimer;
00174 mMouseCursorAlpha = std::min(1.0f, mMouseCursorAlpha + 0.05f);
00175 }
00176 else
00177 mMouseCursorAlpha = std::max(0.0f, mMouseCursorAlpha - 0.005f);
00178
00179 guiPalette->advanceGradient();
00180
00181 gcn::Gui::logic();
00182 }
00183
00184 void Gui::draw()
00185 {
00186 mGraphics->pushClipArea(getTop()->getDimension());
00187 getTop()->draw(mGraphics);
00188
00189 int mouseX, mouseY;
00190 Uint8 button = SDL_GetMouseState(&mouseX, &mouseY);
00191
00192 if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1))
00193 && mCustomCursor
00194 && mMouseCursorAlpha > 0.0f)
00195 {
00196 Image *mouseCursor = mMouseCursors->get(mCursorType);
00197 mouseCursor->setAlpha(mMouseCursorAlpha);
00198
00199 static_cast<Graphics*>(mGraphics)->drawImage(
00200 mouseCursor,
00201 mouseX - 15,
00202 mouseY - 17);
00203 }
00204
00205 mGraphics->popClipArea();
00206 }
00207
00208 void Gui::setUseCustomCursor(bool customCursor)
00209 {
00210 if (customCursor != mCustomCursor)
00211 {
00212 mCustomCursor = customCursor;
00213
00214 if (mCustomCursor)
00215 {
00216
00217 SDL_ShowCursor(SDL_DISABLE);
00218
00219
00220 ResourceManager *resman = ResourceManager::getInstance();
00221 mMouseCursors =
00222 resman->getImageSet("graphics/gui/mouse.png", 40, 40);
00223
00224 if (!mMouseCursors)
00225 logger->error("Unable to load mouse cursors.");
00226 }
00227 else
00228 {
00229
00230 SDL_ShowCursor(SDL_ENABLE);
00231
00232
00233 if (mMouseCursors)
00234 {
00235 mMouseCursors->decRef();
00236 mMouseCursors = NULL;
00237 }
00238 }
00239 }
00240 }
00241
00242 void Gui::handleMouseMoved(const gcn::MouseInput &mouseInput)
00243 {
00244 gcn::Gui::handleMouseMoved(mouseInput);
00245 mMouseInactivityTimer = 0;
00246 }