Mana
Loading...
Searching...
No Matches
gui.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/gui.h"
23
24#include "gui/focushandler.h"
25#include "gui/palette.h"
26#include "gui/sdlinput.h"
27#include "gui/truetypefont.h"
28
30#include "gui/widgets/window.h"
32
33#include "client.h"
34#include "configuration.h"
35#include "graphics.h"
36#include "log.h"
37
39#include "resources/theme.h"
40#include "utils/filesystem.h"
41
42#include <guichan/exception.hpp>
43#include <guichan/image.hpp>
44
45#include <algorithm>
46
47#include <SDL_image.h>
48
49// Guichan stuff
50Gui *gui = nullptr;
51SDLInput *guiInput = nullptr;
52
53// Bolded font
54gcn::Font *boldFont = nullptr;
55
56// Mono font
57gcn::Font *monoFont = nullptr;
58
60
61Gui::Gui(Graphics *graphics, const std::string &themePath)
62 : mAvailableThemes(Theme::getAvailableThemes())
63 , mCustomCursorScale(Client::getVideo().settings().scale())
64{
65 // Try to find the requested theme, using the first one as fallback
66 auto themeIt = std::find_if(mAvailableThemes.begin(),
67 mAvailableThemes.end(),
68 [&themePath](const ThemeInfo &theme) {
69 return theme.getPath() == themePath;
70 });
71
72 setTheme(themeIt != mAvailableThemes.end() ? *themeIt : mAvailableThemes.front());
73
74 Log::info("Initializing GUI...");
75 // Set graphics
76 setGraphics(graphics);
77
78 // Set input
79 guiInput = new SDLInput;
80 setInput(guiInput);
81
82 // Replace focus handler
83 delete mFocusHandler;
84 mFocusHandler = new FocusHandler;
85
86 // Initialize top GUI widget
87 auto *guiTop = new WindowContainer;
88 guiTop->setFocusable(true);
89 guiTop->setSize(graphics->getWidth(), graphics->getHeight());
90 guiTop->setOpaque(false);
92 setTop(guiTop);
93
94 // Set global font
95 const int fontSize = config.fontSize;
96 std::string fontFile = branding.getValue("font", "fonts/dejavusans.ttf");
97 std::string path = ResourceManager::getPath(fontFile);
98
99 // Initialize the font scale before creating the fonts
101
102 try
103 {
104 mGuiFont = new TrueTypeFont(path, fontSize);
105 mInfoParticleFont = new TrueTypeFont(path, fontSize, TTF_STYLE_BOLD);
106 }
107 catch (gcn::Exception e)
108 {
109 Log::critical(std::string("Unable to load '") + fontFile +
110 "': " + e.getMessage());
111 }
112
113 // Set bold font
114 fontFile = branding.getValue("boldFont", "fonts/dejavusans-bold.ttf");
115 path = ResourceManager::getPath(fontFile);
116 try
117 {
118 boldFont = new TrueTypeFont(path, fontSize);
119 }
120 catch (gcn::Exception e)
121 {
122 Log::critical(std::string("Unable to load '") + fontFile +
123 "': " + e.getMessage());
124 }
125
126 // Set mono font
127 fontFile = branding.getValue("monoFont", "fonts/dejavusans-mono.ttf");
128 path = ResourceManager::getPath(fontFile);
129 try
130 {
131 monoFont = new TrueTypeFont(path, fontSize);
132 }
133 catch (gcn::Exception e)
134 {
135 Log::critical(std::string("Unable to load '") + fontFile +
136 "': " + e.getMessage());
137 }
138
141
142 gcn::Widget::setGlobalFont(mGuiFont);
143
144 // Initialize mouse cursor and listen for changes to the option
146
148}
149
151{
152 for (auto cursor : mSystemMouseCursors)
153 SDL_FreeCursor(cursor);
154
155 for (auto cursor : mCustomMouseCursors)
156 SDL_FreeCursor(cursor);
157
158 delete mGuiFont;
159 delete boldFont;
160 delete monoFont;
161 delete mInfoParticleFont;
162 delete getTop();
163
164 delete guiInput;
165}
166
168{
169 // Hide mouse cursor after extended inactivity
171 SDL_ShowCursor(SDL_DISABLE);
172
174
175 gcn::Gui::logic();
176
177 while (!guiInput->isTextQueueEmpty())
178 {
179 TextInput textInput = guiInput->dequeueTextInput();
180 handleTextInput(textInput);
181 }
182}
183
184void Gui::event(Event::Channel channel, const Event &event)
185{
186 if (channel == Event::ConfigChannel)
187 {
188 if (event.getType() == Event::ConfigOptionChanged &&
189 event.hasValue(&Config::customCursor))
190 {
192 }
193 }
194}
195
196bool Gui::videoResized(int width, int height)
197{
198 const float graphicsScale = static_cast<Graphics*>(mGraphics)->getScale();
199 const float userScale = Client::getVideo().settings().scale();
200
201 TrueTypeFont::updateFontScale(graphicsScale);
202
203 if (mCustomCursorScale != userScale)
204 {
205 mCustomCursorScale = userScale;
207 updateCursor();
208 }
209
210 auto *top = static_cast<WindowContainer*>(getTop());
211
212 int oldWidth = top->getWidth();
213 int oldHeight = top->getHeight();
214 if (oldWidth == width && oldHeight == height)
215 return false;
216
217 top->setSize(width, height);
218 top->adjustAfterResize(oldWidth, oldHeight);
219 return true;
220}
221
222void Gui::setUseCustomCursor(bool customCursor)
223{
224 if (mCustomCursor == customCursor)
225 return;
226
227 mCustomCursor = customCursor;
228 updateCursor();
229}
230
232{
233 if (mCursorType == cursor)
234 return;
235
236 mCursorType = cursor;
237 updateCursor();
238}
239
240void Gui::setTheme(const ThemeInfo &theme)
241{
242 mTheme = std::make_unique<Theme>(theme);
243}
244
246{
247 if (mCustomCursor && !mCustomMouseCursors.empty())
248 SDL_SetCursor(mCustomMouseCursors[static_cast<int>(mCursorType)]);
249 else
250 SDL_SetCursor(mSystemMouseCursors[static_cast<int>(mCursorType)]);
251}
252
253void Gui::handleMouseMoved(const gcn::MouseInput &mouseInput)
254{
255 gcn::Gui::handleMouseMoved(mouseInput);
257
258 // Make sure the cursor is visible
259 SDL_ShowCursor(SDL_ENABLE);
260}
261
262void Gui::handleTextInput(const TextInput &textInput)
263{
264 if (auto focused = mFocusHandler->getFocused())
265 {
266 if (auto textField = dynamic_cast<TextField*>(focused))
267 {
268 textField->textInput(textInput);
269 }
270 }
271}
272
273static SDL_Surface *loadSurface(const std::string &path)
274{
275 if (SDL_RWops *file = FS::openRWops(path))
276 return IMG_Load_RW(file, 1);
277 return nullptr;
278}
279
281{
282 for (auto cursor : mCustomMouseCursors)
283 SDL_FreeCursor(cursor);
284
285 mCustomMouseCursors.clear();
286
287 const std::string cursorPath = mTheme->resolvePath("mouse.png");
288 SDL_Surface *mouseSurface = loadSurface(cursorPath);
289 if (!mouseSurface)
290 {
291 Log::warn("Unable to load mouse cursor file (%s): %s",
292 cursorPath.c_str(), SDL_GetError());
293 return;
294 }
295
296 SDL_SetSurfaceBlendMode(mouseSurface, SDL_BLENDMODE_NONE);
297
298 constexpr int cursorSize = 40;
299 const int targetCursorSize = cursorSize * mCustomCursorScale;
300 const int columns = mouseSurface->w / cursorSize;
301
302 SDL_Surface *cursorSurface = SDL_CreateRGBSurfaceWithFormat(
303 0, targetCursorSize, targetCursorSize, 32,
304 SDL_PIXELFORMAT_RGBA32);
305
306 for (int i = 0; i < static_cast<int>(Cursor::Count); ++i)
307 {
308 int x = i % columns * cursorSize;
309 int y = i / columns * cursorSize;
310
311 SDL_Rect srcrect = { x, y, cursorSize, cursorSize };
312 SDL_Rect dstrect = { 0, 0, targetCursorSize, targetCursorSize };
313 SDL_BlitScaled(mouseSurface, &srcrect, cursorSurface, &dstrect);
314
315 SDL_Cursor *cursor = SDL_CreateColorCursor(cursorSurface,
317 17 * mCustomCursorScale);
318 if (!cursor)
319 {
320 Log::warn("Unable to create cursor: %s", SDL_GetError());
321 }
322
323 mCustomMouseCursors.push_back(cursor);
324 }
325
326 SDL_FreeSurface(cursorSurface);
327 SDL_FreeSurface(mouseSurface);
328}
329
331{
332 constexpr struct {
333 Cursor cursor;
334 SDL_SystemCursor systemCursor;
335 } cursors[static_cast<int>(Cursor::Count)] = {
336 { Cursor::Pointer, SDL_SYSTEM_CURSOR_ARROW },
337 { Cursor::ResizeAcross, SDL_SYSTEM_CURSOR_SIZEWE },
338 { Cursor::ResizeDown, SDL_SYSTEM_CURSOR_SIZENS },
339 { Cursor::ResizeDownLeft, SDL_SYSTEM_CURSOR_SIZENESW },
340 { Cursor::ResizeDownRight, SDL_SYSTEM_CURSOR_SIZENWSE },
341 { Cursor::Fight, SDL_SYSTEM_CURSOR_HAND },
342 { Cursor::PickUp, SDL_SYSTEM_CURSOR_HAND },
343 { Cursor::Talk, SDL_SYSTEM_CURSOR_HAND },
344 { Cursor::Action, SDL_SYSTEM_CURSOR_HAND },
345 { Cursor::Left, SDL_SYSTEM_CURSOR_ARROW },
346 { Cursor::Up, SDL_SYSTEM_CURSOR_ARROW },
347 { Cursor::Right, SDL_SYSTEM_CURSOR_ARROW },
348 { Cursor::Down, SDL_SYSTEM_CURSOR_ARROW },
349 { Cursor::Drag, SDL_SYSTEM_CURSOR_SIZEALL },
350 { Cursor::Hand, SDL_SYSTEM_CURSOR_HAND },
351 };
352
353 for (auto cursor : cursors)
354 mSystemMouseCursors.push_back(SDL_CreateSystemCursor(cursor.systemCursor));
355}
The core part of the client.
Definition client.h:113
static Video & getVideo()
Definition client.h:187
std::string getValue(const std::string &key, const std::string &deflt) const
Gets a value as string.
void listen(Event::Channel channel)
Definition event.h:42
@ ConfigOptionChanged
Definition event.h:68
Channel
Definition event.h:45
@ ConfigChannel
Definition event.h:51
The focus handler.
A central point of control for graphics.
Definition graphics.h:78
float getScale() const
Returns the graphics scale.
Definition graphics.h:231
int getHeight() const
Returns the logical height of the screen.
Definition graphics.h:226
int getWidth() const
Returns the logical width of the screen.
Definition graphics.h:221
Main GUI class.
Definition gui.h:80
Timer mMouseActivityTimer
Definition gui.h:161
float mCustomCursorScale
Definition gui.h:158
void handleTextInput(const TextInput &textInput)
Definition gui.cpp:262
bool mCustomCursor
Show custom cursor.
Definition gui.h:157
Gui(Graphics *screen, const std::string &themePath)
Definition gui.cpp:61
void loadSystemCursors()
Definition gui.cpp:330
void handleMouseMoved(const gcn::MouseInput &mouseInput) override
Definition gui.cpp:253
static bool debugDraw
Definition gui.h:141
gcn::Font * mInfoParticleFont
Font for Info Particles.
Definition gui.h:156
void loadCustomCursors()
Definition gui.cpp:280
std::vector< ThemeInfo > mAvailableThemes
Definition gui.h:153
bool videoResized(int width, int height)
Called when the application window has been resized.
Definition gui.cpp:196
std::unique_ptr< Theme > mTheme
The global GUI theme.
Definition gui.h:154
std::vector< SDL_Cursor * > mCustomMouseCursors
Definition gui.h:160
gcn::Font * mGuiFont
The global GUI font.
Definition gui.h:155
Cursor mCursorType
Definition gui.h:162
void updateCursor()
Definition gui.cpp:245
void setUseCustomCursor(bool customCursor)
Sets whether a custom cursor should be rendered.
Definition gui.cpp:222
std::vector< SDL_Cursor * > mSystemMouseCursors
Definition gui.h:159
void event(Event::Channel channel, const Event &event) override
Definition gui.cpp:184
~Gui() override
Definition gui.cpp:150
void setTheme(const ThemeInfo &theme)
Sets the global GUI theme.
Definition gui.cpp:240
void setCursorType(Cursor cursor)
Sets which cursor should be used.
Definition gui.cpp:231
void logic() override
Performs logic of the GUI.
Definition gui.cpp:167
static void advanceGradients()
Updates all colors, that are non-static.
Definition palette.cpp:82
static std::string getPath(const std::string &file)
Returns the real path to a file.
SDL implementation of Input.
Definition sdlinput.h:143
bool isTextQueueEmpty() const
Definition sdlinput.cpp:106
TextInput dequeueTextInput()
Definition sdlinput.cpp:111
A text field.
Definition textfield.h:72
Definition theme.h:196
bool passed() const
Returns whether the timer has passed.
Definition time.h:88
void set(uint32_t ms=0)
Sets the timer with an optional duration in milliseconds.
Definition time.h:69
A wrapper around SDL_ttf for allowing the use of TrueType fonts.
static void updateFontScale(float scale)
const VideoSettings & settings() const
Definition video.h:77
A window container.
void adjustAfterResize(int oldScreenWidth, int oldScreenHeight)
Ensures that all visible windows are on the screen after the screen has been resized.
static void setWindowContainer(WindowContainer *windowContainer)
Sets the window container to be used by new windows.
Definition window.cpp:98
Config config
Global settings (config.xml)
Definition client.cpp:97
Graphics * graphics
Definition client.cpp:104
Configuration branding
XML branding information reader.
Definition client.cpp:98
SDLInput * guiInput
GUI input.
Definition gui.cpp:51
Gui * gui
The GUI system.
Definition gui.cpp:50
gcn::Font * monoFont
Monospaced text font.
Definition gui.cpp:57
gcn::Font * boldFont
Bolded text font.
Definition gui.cpp:54
SDLInput * guiInput
GUI input.
Definition gui.cpp:51
gcn::Font * monoFont
Monospaced text font.
Definition gui.cpp:57
Cursor
Cursors are in graphic order from left to right.
Definition gui.h:55
@ ResizeDownRight
@ ResizeDown
@ ResizeAcross
@ ResizeDownLeft
gcn::Font * boldFont
Bolded text font.
Definition gui.cpp:54
SDL_RWops * openRWops(const std::string &path)
Definition filesystem.h:261
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
bool customCursor
int scale() const
Definition video.cpp:37