Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
Window.android.cpp
1
2#include "stdafx.h"
3#include <Covellite/Os/Window.hpp>
4#include <alicorn/platform/environment.hpp>
5#include <Covellite/Events.hpp>
6#include <Covellite/App/IApplication.hpp>
7#include <Covellite/Os/Events.hpp>
8
9using AppInfo_t = ::alicorn::system::platform::AppInfo;
10
11namespace covellite
12{
13
14namespace os
15{
16
17Window::Window(const ::covellite::app::IApplication & _Application) :
18 m_Events(_Application),
19 m_Handle(AppInfo_t::Get<android_app>().window),
20 m_LastTypeSizeMessage(0)
21{
22 ActivateResizeEvents();
23 ActivateMouseEvents();
24 ActivateKeyEvents();
25}
26
27Window::~Window(void) = default;
28
49Rect Window::GetClientRect(void) const /*override*/
50{
51 return
52 {
53 0,
54 ::alicorn::system::platform::Environment{}.GetStatusBarHeight(),
55 ANativeWindow_getWidth(::covellite::any_cast<ANativeWindow *>(m_Handle)),
56 ANativeWindow_getHeight(::covellite::any_cast<ANativeWindow *>(m_Handle))
57 };
58}
59
60void Window::ActivateApplicationEvents(void)
61{
62
63}
64
65void Window::ActivateResizeEvents(void)
66{
67 m_Events[(int32_t)APP_CMD_WINDOW_REDRAW_NEEDED].Connect([=](void)
68 {
69 m_Events[events::Window.Resize]();
70 });
71}
72
73void Window::ActivateMouseEvents(void)
74{
75 using AInputEvent_t = AInputEvent *;
76
77 auto BuildMessage = [](int32_t _Type, int32_t _Value) -> int32_t
78 {
79 return (_Type << 8) | _Value;
80 };
81
82 const auto Motion =
83 BuildMessage(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE);
84
85 m_Events[Motion].Connect([=](const AInputEvent_t & _pEvent)
86 {
87 const auto X = static_cast<int32_t>(AMotionEvent_getX(_pEvent, 0));
88 const auto Y = static_cast<int32_t>(AMotionEvent_getY(_pEvent, 0));
89
90 m_Events[events::Cursor.Motion](events::Cursor_t::Position{ X, Y });
91 });
92
93 const auto Touch =
94 BuildMessage(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN);
95
96 m_Events[Touch].Connect([=](const AInputEvent_t & _pEvent)
97 {
98 const auto X = static_cast<int32_t>(AMotionEvent_getX(_pEvent, 0));
99 const auto Y = static_cast<int32_t>(AMotionEvent_getY(_pEvent, 0));
100
101 // Отдельный Motion необходим, т.к. Android не генерирует отдельное
102 // сообщение при касании пальцем экрана.
103 m_Events[events::Cursor.Motion](events::Cursor_t::Position{ X, Y });
104 m_Events[events::Cursor.Touch]();
105 });
106
107 const auto Release =
108 BuildMessage(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP);
109
110 m_Events[Release].Connect([=](const AInputEvent_t & _pEvent)
111 {
112 const auto X = static_cast<int32_t>(AMotionEvent_getX(_pEvent, 0));
113 const auto Y = static_cast<int32_t>(AMotionEvent_getY(_pEvent, 0));
114
115 // Отдельный Motion необходим, т.к. Android не генерирует отдельное
116 // сообщение при отпускании пальцем экрана.
117 m_Events[events::Cursor.Motion](events::Cursor_t::Position{ X, Y });
118 m_Events[events::Cursor.Release]();
119 });
120}
121
122void Window::ActivateKeyEvents(void)
123{
124 using AInputEvent_t = AInputEvent *;
125
126 auto BuildMessage = [](int32_t _Type, int32_t _Value) -> int32_t
127 {
128 return (_Type << 8) | _Value;
129 };
130
131 const auto Pressed =
132 BuildMessage(AINPUT_EVENT_TYPE_KEY, events::Key_t::APP_CMD_KEY_PRESSED);
133
134 m_Events[Pressed].Connect([=](const AInputEvent_t & _pEvent)
135 {
136 const events::Key_t::Code Code = AKeyEvent_getKeyCode(_pEvent);
137
138 m_Events[events::Key.Pressed](Code);
139 });
140
141 const auto Down =
142 BuildMessage(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN);
143
144 m_Events[Down].Connect([=](const AInputEvent_t & _pEvent)
145 {
146 const events::Key_t::Code Code = AKeyEvent_getKeyCode(_pEvent);
147
148 m_Events[events::Key.Down](Code);
149 });
150
151 const auto Up =
152 BuildMessage(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP);
153
154 m_Events[Up].Connect([=](const AInputEvent_t & _pEvent)
155 {
156 const events::Key_t::Code Code = AKeyEvent_getKeyCode(_pEvent);
157
158 if (Code == AKEYCODE_BACK)
159 {
160 m_Events[events::Key.Back]();
161 }
162 else if (Code == AKEYCODE_MENU)
163 {
164 m_Events[events::Key.Menu]();
165 }
166 else
167 {
168 m_Events[events::Key.Up](Code);
169 }
170 });
171}
172
173} // namespace os
174
175} // namespace covellite
Definition android_native_app_glue.h:111