Otclient  14/8/2020
platformwindow.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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>
28 #include <framework/core/timer.h>
30 
31 //@bindsingleton g_window
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 
41 public:
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 maximize() = 0;
50  virtual void poll() = 0;
51  virtual void swapBuffers() = 0;
52  virtual void showMouse() = 0;
53  virtual void hideMouse() = 0;
54  virtual void displayFatalError(const std::string& message) { }
55 
56  int loadMouseCursor(const std::string& file, const Point& hotSpot);
57  virtual void setMouseCursor(int cursorId) = 0;
58  virtual void restoreMouseCursor() = 0;
59 
60  virtual void setTitle(const std::string& title) = 0;
61  virtual void setMinimumSize(const Size& minimumSize) = 0;
62  virtual void setFullscreen(bool fullscreen) = 0;
63  virtual void setVerticalSync(bool enable) = 0;
64  virtual void setIcon(const std::string& iconFile) = 0;
65  virtual void setClipboardText(const std::string& text) = 0;
66 
67  virtual Size getDisplaySize() = 0;
68  virtual std::string getClipboardText() = 0;
69  virtual std::string getPlatformType() = 0;
70 
71  int getDisplayWidth() { return getDisplaySize().width(); }
72  int getDisplayHeight() { return getDisplaySize().height(); }
73 
75  Size getSize() { return m_size; }
77  int getWidth() { return m_size.width(); }
78  int getHeight() { return m_size.height(); }
81  int getX() { return m_position.x; }
82  int getY() { return m_position.y; }
85 
86  bool isKeyPressed(Fw::Key keyCode) { return m_keysState[keyCode]; }
87  bool isMouseButtonPressed(Fw::MouseButton mouseButton) { return m_mouseButtonStates[mouseButton]; }
88  bool isVisible() { return m_visible; }
89  bool isMaximized() { return m_maximized; }
90  bool isFullscreen() { return m_fullscreen; }
91  bool hasFocus() { return m_focused; }
92 
93  void setOnClose(const std::function<void()>& onClose) { m_onClose = onClose; }
94  void setOnResize(const OnResizeCallback& onResize) { m_onResize = onResize; }
95  void setOnInputEvent(const OnInputEventCallback& onInputEvent) { m_onInputEvent = onInputEvent; }
96 
97 protected:
98  virtual int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) = 0;
99 
101 
102  void processKeyDown(Fw::Key keyCode);
103  void processKeyUp(Fw::Key keyCode);
104  void releaseAllKeys();
105  void fireKeysPress();
106 
107  std::map<int, Fw::Key> m_keyMap;
108  std::map<Fw::Key, stdext::boolean<false>> m_keysState;
109  std::map<Fw::Key, ticks_t> m_firstKeysPress;
110  std::map<Fw::Key, ticks_t> m_lastKeysPress;
112 
120 
126 
127  std::function<void()> m_onClose;
128  OnResizeCallback m_onResize;
129  OnInputEventCallback m_onInputEvent;
130 };
131 
132 extern PlatformWindow& g_window;
133 
134 #endif
PlatformWindow::m_position
Point m_position
Definition: platformwindow.h:115
PlatformWindow::restoreMouseCursor
virtual void restoreMouseCursor()=0
PlatformWindow::m_lastKeysPress
std::map< Fw::Key, ticks_t > m_lastKeysPress
Definition: platformwindow.h:110
PlatformWindow::getPosition
Point getPosition()
Definition: platformwindow.h:80
TPoint::y
T y
Definition: point.h:83
PlatformWindow::getUnmaximizedPos
Point getUnmaximizedPos()
Definition: platformwindow.h:79
PlatformWindow::setOnClose
void setOnClose(const std::function< void()> &onClose)
Definition: platformwindow.h:93
PlatformWindow::setFullscreen
virtual void setFullscreen(bool fullscreen)=0
PlatformWindow::setClipboardText
virtual void setClipboardText(const std::string &text)=0
PlatformWindow::setVerticalSync
virtual void setVerticalSync(bool enable)=0
PlatformWindow::m_focused
stdext::boolean< false > m_focused
Definition: platformwindow.h:123
PlatformWindow::getSize
Size getSize()
Definition: platformwindow.h:75
PlatformWindow::getWidth
int getWidth()
Definition: platformwindow.h:77
PlatformWindow::getPlatformType
virtual std::string getPlatformType()=0
inputevent.h
PlatformWindow::swapBuffers
virtual void swapBuffers()=0
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
PlatformWindow
Definition: platformwindow.h:32
Fw::Key
Key
Definition: const.h:57
PlatformWindow::m_unmaximizedPos
Point m_unmaximizedPos
Definition: platformwindow.h:117
PlatformWindow::setTitle
virtual void setTitle(const std::string &title)=0
PlatformWindow::getClipboardText
virtual std::string getClipboardText()=0
PlatformWindow::hasFocus
bool hasFocus()
Definition: platformwindow.h:91
PlatformWindow::setOnInputEvent
void setOnInputEvent(const OnInputEventCallback &onInputEvent)
Definition: platformwindow.h:95
PlatformWindow::updateUnmaximizedCoords
void updateUnmaximizedCoords()
Definition: platformwindow.cpp:61
stdext::boolean< false >
TSize::width
int width() const
Definition: size.h:43
PlatformWindow::m_inputEvent
InputEvent m_inputEvent
Definition: platformwindow.h:118
PlatformWindow::isFullscreen
bool isFullscreen()
Definition: platformwindow.h:90
PlatformWindow::show
virtual void show()=0
PlatformWindow::maximize
virtual void maximize()=0
PlatformWindow::m_unmaximizedSize
Size m_unmaximizedSize
Definition: platformwindow.h:116
PlatformWindow::resize
virtual void resize(const Size &size)=0
PlatformWindow::getHeight
int getHeight()
Definition: platformwindow.h:78
PlatformWindow::releaseAllKeys
void releaseAllKeys()
Definition: platformwindow.cpp:139
PlatformWindow::m_fullscreen
stdext::boolean< false > m_fullscreen
Definition: platformwindow.h:124
PlatformWindow::getY
int getY()
Definition: platformwindow.h:82
PlatformWindow::isKeyPressed
bool isKeyPressed(Fw::Key keyCode)
Definition: platformwindow.h:86
PlatformWindow::init
virtual void init()=0
InputEvent
Definition: inputevent.h:28
InputEvent::keyboardModifiers
int keyboardModifiers
Definition: inputevent.h:49
PlatformWindow::m_firstKeysPress
std::map< Fw::Key, ticks_t > m_firstKeysPress
Definition: platformwindow.h:109
PlatformWindow::m_onInputEvent
OnInputEventCallback m_onInputEvent
Definition: platformwindow.h:129
PlatformWindow::hideMouse
virtual void hideMouse()=0
timer.h
PlatformWindow::m_mouseButtonStates
stdext::boolean< false > m_mouseButtonStates[4]
Definition: platformwindow.h:119
PlatformWindow::isVisible
bool isVisible()
Definition: platformwindow.h:88
PlatformWindow::setMinimumSize
virtual void setMinimumSize(const Size &minimumSize)=0
PlatformWindow::processKeyDown
void processKeyDown(Fw::Key keyCode)
Definition: platformwindow.cpp:69
PlatformWindow::isMaximized
bool isMaximized()
Definition: platformwindow.h:89
TPoint::x
T x
Definition: point.h:83
PlatformWindow::m_created
stdext::boolean< false > m_created
Definition: platformwindow.h:121
PlatformWindow::setIcon
virtual void setIcon(const std::string &iconFile)=0
PlatformWindow::m_visible
stdext::boolean< false > m_visible
Definition: platformwindow.h:122
PlatformWindow::setMouseCursor
virtual void setMouseCursor(int cursorId)=0
PlatformWindow::getDisplayHeight
int getDisplayHeight()
Definition: platformwindow.h:72
PlatformWindow::hide
virtual void hide()=0
PlatformWindow::getKeyboardModifiers
int getKeyboardModifiers()
Definition: platformwindow.h:84
PlatformWindow::m_size
Size m_size
Definition: platformwindow.h:113
PlatformWindow::getUnmaximizedSize
Size getUnmaximizedSize()
Definition: platformwindow.h:74
PlatformWindow::m_onResize
OnResizeCallback m_onResize
Definition: platformwindow.h:128
PlatformWindow::m_onClose
std::function< void()> m_onClose
Definition: platformwindow.h:127
TSize::height
int height() const
Definition: size.h:44
declarations.h
PlatformWindow::getX
int getX()
Definition: platformwindow.h:81
stdext::shared_object_ptr
Definition: shared_object.h:39
PlatformWindow::m_keyMap
std::map< int, Fw::Key > m_keyMap
Definition: platformwindow.h:107
PlatformWindow::loadMouseCursor
int loadMouseCursor(const std::string &file, const Point &hotSpot)
Definition: platformwindow.cpp:39
PlatformWindow::setOnResize
void setOnResize(const OnResizeCallback &onResize)
Definition: platformwindow.h:94
PlatformWindow::getMousePosition
Point getMousePosition()
Definition: platformwindow.h:83
PlatformWindow::displayFatalError
virtual void displayFatalError(const std::string &message)
Definition: platformwindow.h:54
PlatformWindow::getDisplaySize
virtual Size getDisplaySize()=0
PlatformWindow::m_maximized
stdext::boolean< false > m_maximized
Definition: platformwindow.h:125
PlatformWindow::internalLoadMouseCursor
virtual int internalLoadMouseCursor(const ImagePtr &image, const Point &hotSpot)=0
Timer
Definition: timer.h:28
PlatformWindow::showMouse
virtual void showMouse()=0
InputEvent::mousePos
Point mousePos
Definition: inputevent.h:50
PlatformWindow::m_keysState
std::map< Fw::Key, stdext::boolean< false > > m_keysState
Definition: platformwindow.h:108
PlatformWindow::poll
virtual void poll()=0
PlatformWindow::m_keyPressTimer
Timer m_keyPressTimer
Definition: platformwindow.h:111
TPoint< int >
PlatformWindow::processKeyUp
void processKeyUp(Fw::Key keyCode)
Definition: platformwindow.cpp:106
global.h
TSize< int >
PlatformWindow::isMouseButtonPressed
bool isMouseButtonPressed(Fw::MouseButton mouseButton)
Definition: platformwindow.h:87
PlatformWindow::terminate
virtual void terminate()=0
PlatformWindow::fireKeysPress
void fireKeysPress()
Definition: platformwindow.cpp:157
PlatformWindow::move
virtual void move(const Point &pos)=0
PlatformWindow::m_minimumSize
Size m_minimumSize
Definition: platformwindow.h:114
Fw::MouseButton
MouseButton
Definition: const.h:246
PlatformWindow::getDisplayWidth
int getDisplayWidth()
Definition: platformwindow.h:71
PlatformWindow::getMinimumSize
Size getMinimumSize()
Definition: platformwindow.h:76