@@ -167,6 +167,139 @@ void onSurfaceShowCB(OH_NativeXComponent* component, void* window) {
167
167
sendMsgToWorker (cc::MessageType::WM_XCOMPONENT_SURFACE_SHOW, component, window);
168
168
}
169
169
170
+ int ohKeyCodeToCocosKeyCode (OH_NativeXComponent_KeyCode ohKeyCode){
171
+ static const int keyZeroInCocos = 48 ;
172
+ static const int keyF1InCocos = 112 ;
173
+ static const int keyAInCocos = 65 ;
174
+ static std::unordered_map<OH_NativeXComponent_KeyCode, cc::KeyCode> keyCodeMap = {
175
+ {KEY_ESCAPE, cc::KeyCode::ESCAPE},
176
+ {KEY_GRAVE, cc::KeyCode::BACKQUOTE},
177
+ {KEY_MINUS, cc::KeyCode::MINUS},
178
+ {KEY_EQUALS, cc::KeyCode::EQUAL},
179
+ {KEY_DEL, cc::KeyCode::BACKSPACE},
180
+ {KEY_TAB, cc::KeyCode::TAB},
181
+ {KEY_LEFT_BRACKET, cc::KeyCode::BRACKET_LEFT},
182
+ {KEY_RIGHT_BRACKET, cc::KeyCode::BRACKET_RIGHT},
183
+ {KEY_BACKSLASH, cc::KeyCode::BACKSLASH},
184
+ {KEY_CAPS_LOCK, cc::KeyCode::CAPS_LOCK},
185
+ {KEY_SEMICOLON, cc::KeyCode::SEMICOLON},
186
+ {KEY_APOSTROPHE, cc::KeyCode::QUOTE},
187
+ {KEY_ENTER, cc::KeyCode::ENTER},
188
+ {KEY_SHIFT_LEFT, cc::KeyCode::SHIFT_LEFT},
189
+ {KEY_COMMA, cc::KeyCode::COMMA},
190
+ {KEY_PERIOD, cc::KeyCode::PERIOD},
191
+ {KEY_SLASH, cc::KeyCode::SLASH},
192
+ {KEY_SHIFT_RIGHT, cc::KeyCode::SHIFT_RIGHT},
193
+ {KEY_CTRL_LEFT, cc::KeyCode::CONTROL_LEFT},
194
+ {KEY_ALT_LEFT, cc::KeyCode::ALT_LEFT},
195
+ {KEY_SPACE, cc::KeyCode::SPACE},
196
+ {KEY_ALT_RIGHT, cc::KeyCode::ALT_RIGHT},
197
+ {KEY_CTRL_RIGHT, cc::KeyCode::CONTROL_RIGHT},
198
+ {KEY_DPAD_LEFT, cc::KeyCode::ARROW_LEFT},
199
+ {KEY_DPAD_RIGHT, cc::KeyCode::ARROW_RIGHT},
200
+ {KEY_DPAD_DOWN, cc::KeyCode::ARROW_DOWN},
201
+ {KEY_DPAD_UP, cc::KeyCode::ARROW_UP},
202
+ {KEY_INSERT, cc::KeyCode::INSERT},
203
+ };
204
+ if (keyCodeMap.find (ohKeyCode) != keyCodeMap.end ()){
205
+ return int (keyCodeMap[ohKeyCode]);
206
+ }
207
+ if (ohKeyCode >= KEY_0 && ohKeyCode <= KEY_9){
208
+ return keyZeroInCocos + ohKeyCode - KEY_0;
209
+ }
210
+ if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z){
211
+ return keyAInCocos + ohKeyCode - KEY_A;
212
+ }
213
+ if (ohKeyCode >= KEY_F1 && ohKeyCode <= KEY_F12){
214
+ return keyF1InCocos + ohKeyCode - KEY_F1;
215
+ }
216
+ return ohKeyCode;
217
+ }
218
+
219
+ void dispatchKeyEventCB (OH_NativeXComponent* component, void * window) {
220
+ OH_NativeXComponent_KeyEvent* keyEvent;
221
+ if (OH_NativeXComponent_GetKeyEvent (component, &keyEvent) >= 0 ) {
222
+ static const int keyCodeUnknownInOH = -1 ;
223
+ static const int keyActionUnknownInOH = -1 ;
224
+ OH_NativeXComponent_KeyAction action;
225
+ OH_NativeXComponent_GetKeyEventAction (keyEvent, &action);
226
+ OH_NativeXComponent_KeyCode code;
227
+ OH_NativeXComponent_GetKeyEventCode (keyEvent, &code);
228
+ if (code == keyCodeUnknownInOH || action == keyActionUnknownInOH) {
229
+ CC_LOG_ERROR (" unknown code and action don't callback" );
230
+ return ;
231
+ }
232
+ cc::KeyboardEvent* ev = new cc::KeyboardEvent;
233
+ ev->windowId = cc::ISystemWindow::mainWindowId;
234
+ ev->action = 0 == action ? cc::KeyboardEvent::Action::PRESS : cc::KeyboardEvent::Action::RELEASE;
235
+
236
+ ev->key = ohKeyCodeToCocosKeyCode (code);
237
+ sendMsgToWorker (cc::MessageType::WM_XCOMPONENT_KEY_EVENT, reinterpret_cast <void *>(ev), window);
238
+ } else {
239
+ CC_LOG_ERROR (" OpenHarmonyPlatform::getKeyEventError" );
240
+ }
241
+ }
242
+
243
+
244
+ void dispatchMouseEventCB (OH_NativeXComponent* component, void * window) {
245
+ OH_NativeXComponent_MouseEvent mouseEvent;
246
+ int32_t ret = OH_NativeXComponent_GetMouseEvent (component, window, &mouseEvent);
247
+ if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
248
+ if (mouseEvent.action == OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_NONE)
249
+ return ;
250
+ cc::MouseEvent* ev = new cc::MouseEvent;
251
+ ev->windowId = cc::ISystemWindow::mainWindowId;
252
+ ev->x = mouseEvent.x ;
253
+ ev->y = mouseEvent.y ;
254
+ switch (mouseEvent.action ) {
255
+ case OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_PRESS:
256
+ ev->type = cc::MouseEvent::Type::DOWN;
257
+ break ;
258
+ case OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_RELEASE:
259
+ ev->type = cc::MouseEvent::Type::UP;
260
+ break ;
261
+ case OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_MOVE:
262
+ ev->type = cc::MouseEvent::Type::MOVE;
263
+ break ;
264
+ default :
265
+ ev->type = cc::MouseEvent::Type::UNKNOWN;
266
+ break ;
267
+ }
268
+ switch (mouseEvent.button ) {
269
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_LEFT_BUTTON:
270
+ ev->button = 0 ;
271
+ break ;
272
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_RIGHT_BUTTON:
273
+ ev->button = 2 ;
274
+ break ;
275
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_MIDDLE_BUTTON:
276
+ ev->button = 1 ;
277
+ break ;
278
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_BACK_BUTTON:
279
+ ev->button = 3 ;
280
+ break ;
281
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_FORWARD_BUTTON:
282
+ ev->button = 4 ;
283
+ break ;
284
+ case OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_NONE_BUTTON:
285
+ ev->button = -1 ;
286
+ break ;
287
+ }
288
+ if (mouseEvent.action == 1 && mouseEvent.button == 1 ) {
289
+ cc::OpenHarmonyPlatform::getInstance ()->isMouseLeftActive = true ;
290
+ }
291
+ if (mouseEvent.action == 2 && mouseEvent.button == 1 ) {
292
+ cc::OpenHarmonyPlatform::getInstance ()->isMouseLeftActive = false ;
293
+ }
294
+ sendMsgToWorker (cc::MessageType::WM_XCOMPONENT_MOUSE_EVENT, reinterpret_cast <void *>(ev), window);
295
+ } else {
296
+ CC_LOG_ERROR (" OpenHarmonyPlatform::getMouseEventError" );
297
+ }
298
+ }
299
+
300
+ void dispatchHoverEventCB (OH_NativeXComponent* component, bool isHover) {
301
+ // OpenharmonyPlatform::DispatchHoverEventCB
302
+ }
170
303
171
304
cc::TouchEvent::Type touchTypeTransform (OH_NativeXComponent_TouchEventType touchType) {
172
305
if (touchType == OH_NATIVEXCOMPONENT_DOWN) {
@@ -267,6 +400,12 @@ void OpenHarmonyPlatform::setNativeXComponent(OH_NativeXComponent* component) {
267
400
OH_NativeXComponent_RegisterCallback (_component, &_callback);
268
401
OH_NativeXComponent_RegisterSurfaceHideCallback (_component, onSurfaceHideCB);
269
402
OH_NativeXComponent_RegisterSurfaceShowCallback (_component, onSurfaceShowCB);
403
+ // register KeyEvent
404
+ OH_NativeXComponent_RegisterKeyEventCallback (_component, dispatchKeyEventCB);
405
+ // register mouseEvent
406
+ _mouseCallback.DispatchMouseEvent = dispatchMouseEventCB;
407
+ _mouseCallback.DispatchHoverEvent = dispatchHoverEventCB;
408
+ OH_NativeXComponent_RegisterMouseEventCallback (_component, &_mouseCallback);
270
409
}
271
410
272
411
void OpenHarmonyPlatform::enqueue (const WorkerMessageData& msg) {
@@ -338,6 +477,18 @@ void OpenHarmonyPlatform::onMessageCallback(const uv_async_t* /* req */) {
338
477
events::Touch::broadcast (*ev);
339
478
delete ev;
340
479
ev = nullptr ;
480
+ } else if (msgData.type == MessageType::WM_XCOMPONENT_KEY_EVENT) {
481
+ KeyboardEvent* ev = reinterpret_cast <KeyboardEvent*>(msgData.data );
482
+ CC_ASSERT (ev != nullptr );
483
+ events::Keyboard::broadcast (*ev);
484
+ delete ev;
485
+ ev = nullptr ;
486
+ } else if (msgData.type == MessageType::WM_XCOMPONENT_MOUSE_EVENT || msgData.type == MessageType::WM_XCOMPONENT_MOUSE_WHEEL_EVENT ) {
487
+ MouseEvent* ev = reinterpret_cast <MouseEvent*>(msgData.data );
488
+ CC_ASSERT (ev != nullptr );
489
+ events::Mouse::broadcast (*ev);
490
+ delete ev;
491
+ ev = nullptr ;
341
492
} else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_CREATED) {
342
493
CC_LOG_INFO (" onMessageCallback WM_XCOMPONENT_SURFACE_CREATED ..." );
343
494
OH_NativeXComponent* nativexcomponet = reinterpret_cast <OH_NativeXComponent*>(msgData.data );
@@ -483,6 +634,23 @@ void OpenHarmonyPlatform::onSurfaceShow(void* window) {
483
634
events::WindowRecreated::broadcast (ISystemWindow::mainWindowId);
484
635
}
485
636
637
+ void OpenHarmonyPlatform::dispatchMouseWheelCB (std::string eventType, float offsetY) {
638
+ if (isMouseLeftActive) {
639
+ return ;
640
+ }
641
+ if (eventType == " actionUpdate" ) {
642
+ float moveScrollY = offsetY - scrollDistance;
643
+ scrollDistance = offsetY;
644
+ cc::MouseEvent* ev = new cc::MouseEvent;
645
+ ev->windowId = cc::ISystemWindow::mainWindowId;
646
+ ev->type = MouseEvent::Type::WHEEL;
647
+ ev->x = 0 ;
648
+ ev->y = moveScrollY;
649
+ sendMsgToWorker (MessageType::WM_XCOMPONENT_MOUSE_WHEEL_EVENT, reinterpret_cast <void *>(ev), nullptr );
650
+ } else {
651
+ scrollDistance = 0 ;
652
+ }
653
+ }
486
654
487
655
ISystemWindow* OpenHarmonyPlatform::createNativeWindow (uint32_t windowId, void * externalHandle) {
488
656
SystemWindow* window = ccnew SystemWindow (windowId, externalHandle);
0 commit comments