1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef UIMANAGER_H
24#define UIMANAGER_H
25
26#include "declarations.h"
27#include "uiwidget.h"
28#include <framework/core/inputevent.h>
29#include <framework/otml/declarations.h>
30
31//@bindsingleton g_ui
32class UIManager
33{
34public:
35 void init();
36 void terminate();
37
38 void render(Fw::DrawPane drawPane);
39 void resize(const Size& size);
40 void inputEvent(const InputEvent& event);
41
42 void updatePressedWidget(const Fw::MouseButton button, const UIWidgetPtr& newPressedWidget, const Point& clickedPos = Point(), bool fireClicks = true);
43 bool updateDraggingWidget(const UIWidgetPtr& draggingWidget, const Point& clickedPos = Point());
44 void updateHoveredWidget(bool now = false);
45
46 void clearStyles();
47 bool importStyle(std::string file);
48 bool importStyleFromString(std::string data);
49 void importStyleFromOTML(const OTMLNodePtr& styleNode);
50 OTMLNodePtr getStyle(const std::string& styleName);
51 std::string getStyleClass(const std::string& styleName);
52
53 UIWidgetPtr loadUIFromString(const std::string& data, const UIWidgetPtr& parent);
54 UIWidgetPtr loadUI(std::string file, const UIWidgetPtr& parent);
55 UIWidgetPtr displayUI(const std::string& file) { return loadUI(file, m_rootWidget); }
56 UIWidgetPtr createWidget(const std::string& styleName, const UIWidgetPtr& parent);
57 UIWidgetPtr createWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent);
58
59 void setMouseReceiver(const UIWidgetPtr& widget) { m_mouseReceiver = widget; }
60 void setKeyboardReceiver(const UIWidgetPtr& widget) { m_keyboardReceiver = widget; }
61 void setDebugBoxesDrawing(bool enabled) { m_drawDebugBoxes = enabled; }
62 void resetMouseReceiver() { m_mouseReceiver = m_rootWidget; }
63 void resetKeyboardReceiver() { m_keyboardReceiver = m_rootWidget; }
64 UIWidgetPtr getMouseReceiver() { return m_mouseReceiver; }
65 UIWidgetPtr getKeyboardReceiver() { return m_keyboardReceiver; }
66 UIWidgetPtr getDraggingWidget() { return m_draggingWidget; }
67 UIWidgetPtr getHoveredWidget() { return m_hoveredWidget; }
68 UIWidgetPtr getPressedWidget() { return m_pressedWidget[Fw::MouseLeftButton]; }
69 UIWidgetPtr getRootWidget() { return m_rootWidget; }
70 bool isMouseGrabbed() { return m_mouseReceiver != m_rootWidget; }
71 bool isKeyboardGrabbed() { return m_keyboardReceiver != m_rootWidget; }
72
73 bool isDrawingDebugBoxes() { return m_drawDebugBoxes; }
74
75protected:
76 void onWidgetAppear(const UIWidgetPtr& widget);
77 void onWidgetDisappear(const UIWidgetPtr& widget);
78 void onWidgetDestroy(const UIWidgetPtr& widget);
79
80 friend class UIWidget;
81
82private:
83 UIWidgetPtr m_rootWidget;
84 UIWidgetPtr m_mouseReceiver;
85 UIWidgetPtr m_keyboardReceiver;
86 UIWidgetPtr m_draggingWidget;
87 UIWidgetPtr m_hoveredWidget;
88 UIWidgetPtr m_pressedWidget[Fw::MouseButtonLast + 1] = { nullptr };
89 stdext::boolean<false> m_hoverUpdateScheduled;
90 stdext::boolean<false> m_drawDebugBoxes;
91 std::unordered_map<std::string, OTMLNodePtr> m_styles;
92 UIWidgetList m_destroyedWidgets;
93 ScheduledEventPtr m_checkEvent;
94 stdext::timer m_moveTimer;
95};
96
97extern UIManager g_ui;
98
99#endif
100