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 PLATFORMWINDOW_H
24#define PLATFORMWINDOW_H
25
26#include <framework/global.h>
27#include <framework/core/inputevent.h>
28#include <framework/core/timer.h>
29#include <framework/graphics/declarations.h>
30
31//@bindsingleton g_window
32class PlatformWindow
33{
34 enum {
35 KEY_PRESS_REPEAT_INTERVAL = 30,
36 };
37
38 typedef std::function<void(const Size&)> OnResizeCallback;
39 typedef std::function<void(const InputEvent&)> OnInputEventCallback;
40
41public:
42 virtual void init() = 0;
43 virtual void terminate() = 0;
44
45 virtual void move(const Point& pos) = 0;
46 virtual void resize(const Size& size) = 0;
47 virtual void show() = 0;
48 virtual void hide() = 0;
49 virtual void minimize() = 0;
50 virtual void maximize() = 0;
51 virtual void poll() = 0;
52 virtual void swapBuffers() = 0;
53 virtual void showMouse() = 0;
54 virtual void hideMouse() = 0;
55 virtual void displayFatalError(const std::string& message) { }
56
57 int loadMouseCursor(const std::string& file, const Point& hotSpot);
58 virtual void setMouseCursor(int cursorId) = 0;
59 virtual void restoreMouseCursor() = 0;
60
61 virtual void setTitle(const std::string& title) = 0;
62 virtual void setMinimumSize(const Size& minimumSize) = 0;
63 virtual void setFullscreen(bool fullscreen) = 0;
64 virtual void setVerticalSync(bool enable) = 0;
65 virtual void setIcon(const std::string& iconFile) = 0;
66 virtual void setClipboardText(const std::string& text) = 0;
67
68 bool hasVerticalSync() { return m_verticalSync; }
69
70 virtual Size getDisplaySize() = 0;
71 virtual std::string getClipboardText() = 0;
72 virtual std::string getPlatformType() = 0;
73
74 int getDisplayWidth() { return getDisplaySize().width(); }
75 int getDisplayHeight() { return getDisplaySize().height(); }
76
77 Size getUnmaximizedSize() { return m_unmaximizedSize; }
78 Size getSize() { return m_size; }
79 Size getMinimumSize() { return m_minimumSize; }
80 int getWidth() { return m_size.width(); }
81 int getHeight() { return m_size.height(); }
82 Point getUnmaximizedPos() { return m_unmaximizedPos; }
83 Point getPosition() { return m_position; }
84 int getX() { return m_position.x; }
85 int getY() { return m_position.y; }
86 Point getMousePosition() { return m_inputEvent.mousePos; }
87 int getKeyboardModifiers() { return m_inputEvent.keyboardModifiers; }
88
89 bool isKeyPressed(Fw::Key keyCode) { return m_keysState[keyCode]; }
90 bool isMouseButtonPressed(Fw::MouseButton mouseButton) { return m_mouseButtonStates[mouseButton]; }
91 bool isVisible() { return m_visible; }
92 bool isMaximized() { return m_maximized; }
93 bool isFullscreen() { return m_fullscreen; }
94 bool hasFocus() { return m_focused; }
95
96 void setOnClose(const std::function<void()>& onClose) { m_onClose = onClose; }
97 void setOnResize(const OnResizeCallback& onResize) { m_onResize = onResize; }
98 void setOnInputEvent(const OnInputEventCallback& onInputEvent) { m_onInputEvent = onInputEvent; }
99
100 virtual void showTextEditor(const std::string& title, const std::string& description, const std::string& text, int flags) {}
101 virtual void handleTextInput(std::string text) {} // for android
102
103 void setScaling(float scaling) { m_scaling = scaling; }
104
105 virtual void flash();
106
107protected:
108 virtual int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) = 0;
109
110 void updateUnmaximizedCoords();
111
112 void processKeyDown(Fw::Key keyCode);
113 void processKeyUp(Fw::Key keyCode);
114 void releaseAllKeys();
115 void fireKeysPress();
116
117 std::map<int, Fw::Key> m_keyMap;
118 std::map<Fw::Key, stdext::boolean<false>> m_keysState;
119 std::map<Fw::Key, ticks_t> m_firstKeysPress;
120 std::map<Fw::Key, ticks_t> m_lastKeysPress;
121 Timer m_keyPressTimer;
122
123 Size m_size;
124 Size m_minimumSize;
125 Point m_position;
126 Size m_unmaximizedSize;
127 Point m_unmaximizedPos;
128 InputEvent m_inputEvent;
129 stdext::boolean<false> m_mouseButtonStates[Fw::MouseButtonLast + 1];
130
131 stdext::boolean<false> m_created;
132 stdext::boolean<false> m_visible;
133 stdext::boolean<false> m_focused;
134 stdext::boolean<false> m_fullscreen;
135 stdext::boolean<false> m_maximized;
136 bool m_verticalSync = false;
137 float m_scaling = 1.0;
138
139 std::function<void()> m_onClose;
140 OnResizeCallback m_onResize;
141 OnInputEventCallback m_onInputEvent;
142};
143
144extern PlatformWindow& g_window;
145
146#endif
147