Mercurial > hg > orthanc-stone
comparison Applications/Generic/GuiAdapter.cpp @ 861:f0bf971a1e31
Keyboard event routing (handling done in app)
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 24 Jun 2019 14:57:55 +0200 |
parents | e3c56d4f863f |
children | 60a403f01c31 |
comparison
equal
deleted
inserted
replaced
859:6845a05f9526 | 861:f0bf971a1e31 |
---|---|
42 } | 42 } |
43 | 43 |
44 std::ostream& operator<<( | 44 std::ostream& operator<<( |
45 std::ostream& os, const GuiAdapterKeyboardEvent& event) | 45 std::ostream& os, const GuiAdapterKeyboardEvent& event) |
46 { | 46 { |
47 os << "ctrl: " << event.ctrlKey << ", " << | 47 os << "sym: " << event.sym << " (" << (int)(event.sym[0]) << ") ctrl: " << event.ctrlKey << ", " << |
48 "shift: " << event.shiftKey << ", " << | 48 "shift: " << event.shiftKey << ", " << |
49 "alt: " << event.altKey; | 49 "alt: " << event.altKey; |
50 return os; | 50 return os; |
51 } | 51 } |
52 | 52 |
110 //dest.canvasX = src.canvasX; | 110 //dest.canvasX = src.canvasX; |
111 //dest.canvasY = src.canvasY; | 111 //dest.canvasY = src.canvasY; |
112 //dest.padding = src.padding; | 112 //dest.padding = src.padding; |
113 } | 113 } |
114 | 114 |
115 void ConvertFromPlatform( | 115 void ConvertFromPlatform( GuiAdapterWheelEvent& dest, int eventType, const EmscriptenWheelEvent& src) |
116 GuiAdapterWheelEvent& dest, | |
117 int eventType, | |
118 const EmscriptenWheelEvent& src) | |
119 { | 116 { |
120 ConvertFromPlatform(dest.mouse, eventType, src.mouse); | 117 ConvertFromPlatform(dest.mouse, eventType, src.mouse); |
121 dest.deltaX = src.deltaX; | 118 dest.deltaX = src.deltaX; |
122 dest.deltaY = src.deltaY; | 119 dest.deltaY = src.deltaY; |
123 switch (src.deltaMode) | 120 switch (src.deltaMode) |
136 " in wheel event..."); | 133 " in wheel event..."); |
137 } | 134 } |
138 dest.deltaMode = src.deltaMode; | 135 dest.deltaMode = src.deltaMode; |
139 } | 136 } |
140 | 137 |
141 void ConvertFromPlatform( | 138 void ConvertFromPlatform(GuiAdapterKeyboardEvent& dest, const EmscriptenKeyboardEvent& src) |
142 GuiAdapterKeyboardEvent& dest, | 139 { |
143 const EmscriptenKeyboardEvent& src) | 140 dest.sym[0] = src.key[0]; |
144 { | 141 dest.sym[1] = 0; |
145 dest.ctrlKey = src.ctrlKey; | 142 dest.ctrlKey = src.ctrlKey; |
146 dest.shiftKey = src.shiftKey; | 143 dest.shiftKey = src.shiftKey; |
147 dest.altKey = src.altKey; | 144 dest.altKey = src.altKey; |
148 } | 145 } |
149 | 146 |
150 template<typename GenericFunc> | 147 template<typename GenericFunc> |
151 struct FuncAdapterPayload | 148 struct FuncAdapterPayload |
152 { | 149 { |
153 std::string canvasId; | 150 std::string canvasId; |
154 void* userData; | 151 void* userData; |
155 GenericFunc callback; | 152 GenericFunc callback; |
156 }; | 153 }; |
157 | 154 |
158 template<typename GenericFunc, | 155 template<typename GenericFunc, |
159 typename GuiAdapterEvent, | 156 typename GuiAdapterEvent, |
160 typename EmscriptenEvent> | 157 typename EmscriptenEvent> |
161 EM_BOOL OnEventAdapterFunc( | 158 EM_BOOL OnEventAdapterFunc( |
162 int eventType, const EmscriptenEvent* emEvent, void* userData) | 159 int eventType, const EmscriptenEvent* emEvent, void* userData) |
163 { | 160 { |
164 | 161 |
165 // userData is OnMouseWheelFuncAdapterPayload | 162 // userData is OnMouseWheelFuncAdapterPayload |
166 FuncAdapterPayload<GenericFunc>* payload = | 163 FuncAdapterPayload<GenericFunc>* payload = |
167 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); | 164 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); |
168 // LOG(INFO) << "OnEventAdapterFunc"; | 165 // LOG(INFO) << "OnEventAdapterFunc"; |
169 // LOG(INFO) << "------------------"; | 166 // LOG(INFO) << "------------------"; |
170 // LOG(INFO) << "eventType: " << eventType << " wheelEvent: " << | 167 // LOG(INFO) << "eventType: " << eventType << " wheelEvent: " << |
171 // (int)wheelEvent << " userData: " << userData << | 168 // (int)wheelEvent << " userData: " << userData << |
172 // " payload->userData: " << payload->userData; | 169 // " payload->userData: " << payload->userData; |
173 | 170 |
174 GuiAdapterEvent guiEvent; | 171 GuiAdapterEvent guiEvent; |
175 ConvertFromPlatform(guiEvent, eventType, *emEvent); | 172 ConvertFromPlatform(guiEvent, eventType, *emEvent); |
176 bool ret = (*(payload->callback))(payload->canvasId, &guiEvent, payload->userData); | 173 bool ret = (*(payload->callback))(payload->canvasId, &guiEvent, payload->userData); |
177 return static_cast<EM_BOOL>(ret); | 174 return static_cast<EM_BOOL>(ret); |
178 } | 175 } |
184 int /*eventType*/, const EmscriptenEvent* wheelEvent, void* userData) | 181 int /*eventType*/, const EmscriptenEvent* wheelEvent, void* userData) |
185 { | 182 { |
186 // userData is OnMouseWheelFuncAdapterPayload | 183 // userData is OnMouseWheelFuncAdapterPayload |
187 FuncAdapterPayload<GenericFunc>* payload = | 184 FuncAdapterPayload<GenericFunc>* payload = |
188 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); | 185 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); |
189 | 186 |
190 GuiAdapterEvent guiEvent; | 187 GuiAdapterEvent guiEvent; |
191 ConvertFromPlatform(guiEvent, *wheelEvent); | 188 ConvertFromPlatform(guiEvent, *wheelEvent); |
192 bool ret = (*(payload->callback))(payload->canvasId, &guiEvent, payload->userData); | 189 bool ret = (*(payload->callback))(payload->canvasId, &guiEvent, payload->userData); |
193 return static_cast<EM_BOOL>(ret); | 190 return static_cast<EM_BOOL>(ret); |
194 } | 191 } |
195 | 192 |
196 template<typename GenericFunc> | 193 template<typename GenericFunc> |
197 EM_BOOL OnEventAdapterFunc3( | 194 EM_BOOL OnEventAdapterFunc3( |
198 double time, void* userData) | 195 double time, void* userData) |
199 { | 196 { |
200 // userData is OnMouseWheelFuncAdapterPayload | 197 // userData is OnMouseWheelFuncAdapterPayload |
201 FuncAdapterPayload<GenericFunc>* payload = | 198 FuncAdapterPayload<GenericFunc>* payload = |
202 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); | 199 reinterpret_cast<FuncAdapterPayload<GenericFunc>*>(userData); |
203 //std::auto_ptr< FuncAdapterPayload<GenericFunc> > deleter(payload); | 200 //std::auto_ptr< FuncAdapterPayload<GenericFunc> > deleter(payload); |
205 return static_cast<EM_BOOL>(ret); | 202 return static_cast<EM_BOOL>(ret); |
206 } | 203 } |
207 | 204 |
208 // resize: (const char* target, void* userData, EM_BOOL useCapture, em_ui_callback_func callback) | 205 // resize: (const char* target, void* userData, EM_BOOL useCapture, em_ui_callback_func callback) |
209 template< | 206 template< |
210 typename GenericFunc, | 207 typename GenericFunc, |
211 typename GuiAdapterEvent, | 208 typename GuiAdapterEvent, |
212 typename EmscriptenEvent, | 209 typename EmscriptenEvent, |
213 typename EmscriptenSetCallbackFunc> | 210 typename EmscriptenSetCallbackFunc> |
214 static void SetCallback( | 211 static void SetCallback( |
215 EmscriptenSetCallbackFunc emFunc, | 212 EmscriptenSetCallbackFunc emFunc, |
216 std::string canvasId, void* userData, bool capture, GenericFunc func) | 213 std::string canvasId, void* userData, bool capture, GenericFunc func) |
217 { | 214 { |
218 // TODO: write RemoveCallback with an int id that gets returned from | 215 // TODO: write RemoveCallback with an int id that gets returned from |
219 // here | 216 // here |
220 FuncAdapterPayload<GenericFunc>* payload = | 217 FuncAdapterPayload<GenericFunc>* payload = |
221 new FuncAdapterPayload<GenericFunc>(); | 218 new FuncAdapterPayload<GenericFunc>(); |
222 std::auto_ptr<FuncAdapterPayload<GenericFunc> > payloadP(payload); | 219 std::auto_ptr<FuncAdapterPayload<GenericFunc> > payloadP(payload); |
223 payload->canvasId = canvasId; | 220 payload->canvasId = canvasId; |
224 payload->callback = func; | 221 payload->callback = func; |
225 payload->userData = userData; | 222 payload->userData = userData; |
395 #endif | 392 #endif |
396 | 393 |
397 | 394 |
398 #else | 395 #else |
399 | 396 |
400 // SDL ONLY | 397 // SDL ONLY |
401 void ConvertFromPlatform( | 398 void ConvertFromPlatform(GuiAdapterMouseEvent& dest, bool ctrlPressed, bool shiftPressed, bool altPressed, const SDL_Event& source) |
402 GuiAdapterMouseEvent& dest, | 399 { |
403 bool ctrlPressed, bool shiftPressed, bool altPressed, | 400 memset(&dest, 0, sizeof(GuiAdapterMouseEvent)); |
404 const SDL_Event& source) | 401 switch (source.type) |
405 { | 402 { |
406 memset(&dest, 0, sizeof(GuiAdapterMouseEvent)); | 403 case SDL_MOUSEBUTTONDOWN: |
407 switch (source.type) | 404 dest.type = GUIADAPTER_EVENT_MOUSEDOWN; |
408 { | 405 break; |
409 case SDL_MOUSEBUTTONDOWN: | 406 case SDL_MOUSEMOTION: |
410 dest.type = GUIADAPTER_EVENT_MOUSEDOWN; | 407 dest.type = GUIADAPTER_EVENT_MOUSEMOVE; |
411 break; | 408 break; |
412 case SDL_MOUSEMOTION: | 409 case SDL_MOUSEBUTTONUP: |
413 dest.type = GUIADAPTER_EVENT_MOUSEMOVE; | 410 dest.type = GUIADAPTER_EVENT_MOUSEUP; |
414 break; | 411 break; |
415 case SDL_MOUSEBUTTONUP: | 412 case SDL_MOUSEWHEEL: |
416 dest.type = GUIADAPTER_EVENT_MOUSEUP; | 413 dest.type = GUIADAPTER_EVENT_WHEEL; |
417 break; | 414 break; |
418 case SDL_MOUSEWHEEL: | 415 default: |
419 dest.type = GUIADAPTER_EVENT_WHEEL; | 416 LOG(ERROR) << "SDL event: " << source.type << " is not supported"; |
420 break; | 417 ORTHANC_ASSERT(false, "Not supported"); |
421 default: | 418 } |
422 LOG(ERROR) << "SDL event: " << source.type << " is not supported"; | 419 //dest.timestamp = src.timestamp; |
423 ORTHANC_ASSERT(false, "Not supported"); | 420 //dest.screenX = src.screenX; |
424 } | 421 //dest.screenY = src.screenY; |
425 //dest.timestamp = src.timestamp; | 422 //dest.clientX = src.clientX; |
426 //dest.screenX = src.screenX; | 423 //dest.clientY = src.clientY; |
427 //dest.screenY = src.screenY; | 424 dest.ctrlKey = ctrlPressed; |
428 //dest.clientX = src.clientX; | 425 dest.shiftKey = shiftPressed; |
429 //dest.clientY = src.clientY; | 426 dest.altKey = altPressed; |
430 dest.ctrlKey = ctrlPressed; | 427 //dest.metaKey = src.metaKey; |
431 dest.shiftKey = shiftPressed; | 428 switch (source.button.button) |
432 dest.altKey = altPressed; | 429 { |
433 //dest.metaKey = src.metaKey; | 430 case SDL_BUTTON_MIDDLE: |
434 switch (source.button.button) | 431 dest.button = 1; |
435 { | 432 break; |
436 case SDL_BUTTON_MIDDLE: | 433 |
437 dest.button = 1; | 434 case SDL_BUTTON_RIGHT: |
438 break; | 435 dest.button = 2; |
439 | 436 break; |
440 case SDL_BUTTON_RIGHT: | 437 |
441 dest.button = 2; | 438 case SDL_BUTTON_LEFT: |
442 break; | 439 dest.button = 0; |
443 | 440 break; |
444 case SDL_BUTTON_LEFT: | 441 |
445 dest.button = 0; | 442 default: |
446 break; | 443 break; |
447 | 444 } |
448 default: | 445 //dest.buttons = src.buttons; |
449 break; | 446 //dest.movementX = src.movementX; |
450 } | 447 //dest.movementY = src.movementY; |
451 //dest.buttons = src.buttons; | 448 dest.targetX = source.button.x; |
452 //dest.movementX = src.movementX; | 449 dest.targetY = source.button.y; |
453 //dest.movementY = src.movementY; | 450 //dest.canvasX = src.canvasX; |
454 dest.targetX = source.button.x; | 451 //dest.canvasY = src.canvasY; |
455 dest.targetY = source.button.y; | 452 //dest.padding = src.padding; |
456 //dest.canvasX = src.canvasX; | 453 } |
457 //dest.canvasY = src.canvasY; | 454 |
458 //dest.padding = src.padding; | 455 void ConvertFromPlatform( |
459 } | 456 GuiAdapterWheelEvent& dest, |
460 | 457 bool ctrlPressed, bool shiftPressed, bool altPressed, |
461 void ConvertFromPlatform( | 458 const SDL_Event& source) |
462 GuiAdapterWheelEvent& dest, | 459 { |
463 bool ctrlPressed, bool shiftPressed, bool altPressed, | 460 ConvertFromPlatform(dest.mouse, ctrlPressed, shiftPressed, altPressed, source); |
464 const SDL_Event& source) | 461 dest.deltaX = source.wheel.x; |
465 { | 462 dest.deltaY = source.wheel.y; |
466 ConvertFromPlatform(dest.mouse, ctrlPressed, shiftPressed, altPressed, source); | 463 } |
467 dest.deltaX = source.wheel.x; | 464 |
468 dest.deltaY = source.wheel.y; | 465 void ConvertFromPlatform(GuiAdapterKeyboardEvent& dest, const SDL_Event& src) |
469 } | 466 { |
467 memset(&dest, 0, sizeof(GuiAdapterMouseEvent)); | |
468 switch (src.type) | |
469 { | |
470 case SDL_KEYDOWN: | |
471 dest.type = GUIADAPTER_EVENT_KEYDOWN; | |
472 break; | |
473 case SDL_KEYUP: | |
474 dest.type = GUIADAPTER_EVENT_KEYUP; | |
475 break; | |
476 default: | |
477 LOG(ERROR) << "SDL event: " << src.type << " is not supported"; | |
478 ORTHANC_ASSERT(false, "Not supported"); | |
479 } | |
480 dest.sym[0] = src.key.keysym.sym; | |
481 dest.sym[1] = 0; | |
482 | |
483 if (src.key.keysym.mod & KMOD_CTRL) | |
484 dest.ctrlKey = true; | |
485 else | |
486 dest.ctrlKey = false; | |
487 | |
488 if (src.key.keysym.mod & KMOD_SHIFT) | |
489 dest.shiftKey = true; | |
490 else | |
491 dest.shiftKey = false; | |
492 | |
493 if (src.key.keysym.mod & KMOD_ALT) | |
494 dest.altKey = true; | |
495 else | |
496 dest.altKey = false; | |
497 } | |
470 | 498 |
471 | 499 |
472 | 500 |
473 // SDL ONLY | 501 // SDL ONLY |
474 void GuiAdapter::SetResizeCallback( | 502 void GuiAdapter::SetResizeCallback( |
478 } | 506 } |
479 | 507 |
480 // SDL ONLY | 508 // SDL ONLY |
481 void GuiAdapter::SetMouseDownCallback( | 509 void GuiAdapter::SetMouseDownCallback( |
482 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) | 510 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) |
483 { | 511 { |
484 mouseDownHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); | 512 mouseDownHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); |
485 } | 513 } |
486 | 514 |
487 // SDL ONLY | 515 // SDL ONLY |
488 void GuiAdapter::SetMouseMoveCallback( | 516 void GuiAdapter::SetMouseMoveCallback( |
489 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) | 517 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) |
490 { | 518 { |
491 mouseMoveHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); | 519 mouseMoveHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); |
492 } | 520 } |
493 | 521 |
494 // SDL ONLY | 522 // SDL ONLY |
495 void GuiAdapter::SetMouseUpCallback( | 523 void GuiAdapter::SetMouseUpCallback( |
496 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) | 524 std::string canvasId, void* userData, bool capture, OnMouseEventFunc func) |
497 { | 525 { |
498 mouseUpHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); | 526 mouseUpHandlers_.push_back(EventHandlerData<OnMouseEventFunc>(canvasId, func, userData)); |
499 } | 527 } |
500 | 528 |
501 // SDL ONLY | 529 // SDL ONLY |
502 void GuiAdapter::SetWheelCallback( | 530 void GuiAdapter::SetWheelCallback( |
503 std::string canvasId, void* userData, bool capture, OnMouseWheelFunc func) | 531 std::string canvasId, void* userData, bool capture, OnMouseWheelFunc func) |
504 { | 532 { |
505 mouseWheelHandlers_.push_back(EventHandlerData<OnMouseWheelFunc>(canvasId, func, userData)); | 533 mouseWheelHandlers_.push_back(EventHandlerData<OnMouseWheelFunc>(canvasId, func, userData)); |
506 } | 534 } |
507 | 535 |
508 // SDL ONLY | 536 // SDL ONLY |
509 void GuiAdapter::SetKeyDownCallback( | 537 void GuiAdapter::SetKeyDownCallback( |
510 std::string canvasId, void* userData, bool capture, OnKeyDownFunc func) | 538 std::string canvasId, void* userData, bool capture, OnKeyDownFunc func) |
511 { | 539 { |
512 } | 540 keyDownHandlers_.push_back(EventHandlerData<OnKeyDownFunc>(canvasId, func, userData)); |
541 } | |
513 | 542 |
514 // SDL ONLY | 543 // SDL ONLY |
515 void GuiAdapter::SetKeyUpCallback( | 544 void GuiAdapter::SetKeyUpCallback( |
516 std::string canvasId, void* userData, bool capture, OnKeyUpFunc func) | 545 std::string canvasId, void* userData, bool capture, OnKeyUpFunc func) |
517 { | 546 { |
518 } | 547 keyUpHandlers_.push_back(EventHandlerData<OnKeyUpFunc>(canvasId, func, userData)); |
519 | 548 } |
520 | 549 |
521 // SDL ONLY | 550 // SDL ONLY |
522 void GuiAdapter::OnAnimationFrame() | 551 void GuiAdapter::OnAnimationFrame() |
523 { | 552 { |
524 for (size_t i = 0; i < animationFrameHandlers_.size(); i++) | 553 for (size_t i = 0; i < animationFrameHandlers_.size(); i++) |
556 switch (event.mouse.type) | 585 switch (event.mouse.type) |
557 { | 586 { |
558 case GUIADAPTER_EVENT_WHEEL: | 587 case GUIADAPTER_EVENT_WHEEL: |
559 for (size_t i = 0; i < mouseWheelHandlers_.size(); i++) | 588 for (size_t i = 0; i < mouseWheelHandlers_.size(); i++) |
560 { | 589 { |
561 if(mouseWheelHandlers_[i].canvasName == windowTitle) | 590 if (mouseWheelHandlers_[i].canvasName == windowTitle) |
562 (*(mouseWheelHandlers_[i].func))(windowTitle, &event, mouseWheelHandlers_[i].userData); | 591 (*(mouseWheelHandlers_[i].func))(windowTitle, &event, mouseWheelHandlers_[i].userData); |
563 } | 592 } |
564 break; | 593 break; |
565 default: | 594 default: |
566 ORTHANC_ASSERT(false, "Wrong event.type: " << event.mouse.type << " in GuiAdapter::OnMouseWheelEvent(...)"); | 595 ORTHANC_ASSERT(false, "Wrong event.type: " << event.mouse.type << " in GuiAdapter::OnMouseWheelEvent(...)"); |
567 break; | 596 break; |
568 } | 597 } |
569 } | 598 } |
570 | 599 |
571 // SDL ONLY | 600 |
572 void GuiAdapter::OnMouseEvent(uint32_t windowID, const GuiAdapterMouseEvent& event) | 601 void GuiAdapter::OnKeyboardEvent(uint32_t windowID, const GuiAdapterKeyboardEvent& event) |
573 { | 602 { |
574 // the SDL window name IS the canvas name ("canvas" is used because this lib | 603 // only one-letter (ascii) keyboard events supported for now |
575 // is designed for Wasm | 604 ORTHANC_ASSERT(event.sym[0] != 0); |
605 ORTHANC_ASSERT(event.sym[1] == 0); | |
606 | |
576 SDL_Window* sdlWindow = SDL_GetWindowFromID(windowID); | 607 SDL_Window* sdlWindow = SDL_GetWindowFromID(windowID); |
577 ORTHANC_ASSERT(sdlWindow != NULL, "Window ID \"" << windowID << "\" is not a valid SDL window ID!"); | 608 ORTHANC_ASSERT(sdlWindow != NULL, "Window ID \"" << windowID << "\" is not a valid SDL window ID!"); |
578 | 609 |
579 const char* windowTitleSz = SDL_GetWindowTitle(sdlWindow); | 610 const char* windowTitleSz = SDL_GetWindowTitle(sdlWindow); |
580 ORTHANC_ASSERT(windowTitleSz != NULL, "Window ID \"" << windowID << "\" has a NULL window title!"); | 611 ORTHANC_ASSERT(windowTitleSz != NULL, "Window ID \"" << windowID << "\" has a NULL window title!"); |
581 | 612 |
582 std::string windowTitle(windowTitleSz); | 613 std::string windowTitle(windowTitleSz); |
583 ORTHANC_ASSERT(windowTitle != "", "Window ID \"" << windowID << "\" has an empty window title!"); | 614 ORTHANC_ASSERT(windowTitle != "", "Window ID \"" << windowID << "\" has an empty window title!"); |
584 | 615 |
585 switch (event.type) | 616 switch (event.type) |
586 { | 617 { |
587 case GUIADAPTER_EVENT_MOUSEDOWN: | 618 case GUIADAPTER_EVENT_KEYDOWN: |
588 for (size_t i = 0; i < mouseDownHandlers_.size(); i++) | 619 for (size_t i = 0; i < keyDownHandlers_.size(); i++) |
589 { | 620 { |
590 if (mouseDownHandlers_[i].canvasName == windowTitle) | 621 (*(keyDownHandlers_[i].func))(windowTitle, &event, keyDownHandlers_[i].userData); |
591 (*(mouseDownHandlers_[i].func))(windowTitle, &event, mouseDownHandlers_[i].userData); | |
592 } | 622 } |
593 break; | 623 break; |
594 case GUIADAPTER_EVENT_MOUSEMOVE: | 624 case GUIADAPTER_EVENT_KEYUP: |
595 for (size_t i = 0; i < mouseMoveHandlers_.size(); i++) | 625 for (size_t i = 0; i < keyUpHandlers_.size(); i++) |
596 { | 626 { |
597 if (mouseMoveHandlers_[i].canvasName == windowTitle) | 627 (*(keyUpHandlers_[i].func))(windowTitle, &event, keyUpHandlers_[i].userData); |
598 (*(mouseMoveHandlers_[i].func))(windowTitle, &event, mouseMoveHandlers_[i].userData); | |
599 } | 628 } |
600 break; | 629 break; |
601 case GUIADAPTER_EVENT_MOUSEUP: | 630 default: |
602 for (size_t i = 0; i < mouseUpHandlers_.size(); i++) | 631 ORTHANC_ASSERT(false, "Wrong event.type: " << event.type << " in GuiAdapter::OnKeyboardEvent(...)"); |
632 break; | |
633 } | |
634 } | |
635 | |
636 // SDL ONLY | |
637 void GuiAdapter::OnMouseEvent(uint32_t windowID, const GuiAdapterMouseEvent& event) | |
638 { | |
639 if (windowID == 0) | |
640 { | |
641 LOG(WARN) << "GuiAdapter::OnMouseEvent -- windowID == 0 and event won't be routed!"; | |
642 } | |
643 else | |
644 { | |
645 // the SDL window name IS the canvas name ("canvas" is used because this lib | |
646 // is designed for Wasm | |
647 SDL_Window* sdlWindow = SDL_GetWindowFromID(windowID); | |
648 | |
649 ORTHANC_ASSERT(sdlWindow != NULL, "Window ID \"" << windowID << "\" is not a valid SDL window ID!"); | |
650 | |
651 const char* windowTitleSz = SDL_GetWindowTitle(sdlWindow); | |
652 ORTHANC_ASSERT(windowTitleSz != NULL, "Window ID \"" << windowID << "\" has a NULL window title!"); | |
653 | |
654 std::string windowTitle(windowTitleSz); | |
655 ORTHANC_ASSERT(windowTitle != "", "Window ID \"" << windowID << "\" has an empty window title!"); | |
656 | |
657 switch (event.type) | |
603 { | 658 { |
604 if (mouseUpHandlers_[i].canvasName == windowTitle) | 659 case GUIADAPTER_EVENT_MOUSEDOWN: |
605 (*(mouseUpHandlers_[i].func))(windowTitle, &event, mouseUpHandlers_[i].userData); | 660 for (size_t i = 0; i < mouseDownHandlers_.size(); i++) |
661 { | |
662 if (mouseDownHandlers_[i].canvasName == windowTitle) | |
663 (*(mouseDownHandlers_[i].func))(windowTitle, &event, mouseDownHandlers_[i].userData); | |
664 } | |
665 break; | |
666 case GUIADAPTER_EVENT_MOUSEMOVE: | |
667 for (size_t i = 0; i < mouseMoveHandlers_.size(); i++) | |
668 { | |
669 if (mouseMoveHandlers_[i].canvasName == windowTitle) | |
670 (*(mouseMoveHandlers_[i].func))(windowTitle, &event, mouseMoveHandlers_[i].userData); | |
671 } | |
672 break; | |
673 case GUIADAPTER_EVENT_MOUSEUP: | |
674 for (size_t i = 0; i < mouseUpHandlers_.size(); i++) | |
675 { | |
676 if (mouseUpHandlers_[i].canvasName == windowTitle) | |
677 (*(mouseUpHandlers_[i].func))(windowTitle, &event, mouseUpHandlers_[i].userData); | |
678 } | |
679 break; | |
680 default: | |
681 ORTHANC_ASSERT(false, "Wrong event.type: " << event.type << " in GuiAdapter::OnMouseEvent(...)"); | |
682 break; | |
606 } | 683 } |
607 break; | 684 |
608 default: | 685 ////boost::shared_ptr<IGuiAdapterWidget> GetWidgetFromWindowId(); |
609 ORTHANC_ASSERT(false, "Wrong event.type: " << event.type << " in GuiAdapter::OnMouseEvent(...)"); | 686 //boost::shared_ptr<IGuiAdapterWidget> foundWidget; |
610 break; | 687 //VisitWidgets([foundWidget, windowID](auto widget) |
611 } | 688 // { |
612 | 689 // if (widget->GetSdlWindowID() == windowID) |
613 ////boost::shared_ptr<IGuiAdapterWidget> GetWidgetFromWindowId(); | 690 // foundWidget = widget; |
614 //boost::shared_ptr<IGuiAdapterWidget> foundWidget; | 691 // }); |
615 //VisitWidgets([foundWidget, windowID](auto widget) | 692 //ORTHANC_ASSERT(foundWidget, "WindowID " << windowID << " was not found in the registered widgets!"); |
616 // { | 693 //if(foundWidget) |
617 // if (widget->GetSdlWindowID() == windowID) | 694 // foundWidget-> |
618 // foundWidget = widget; | 695 } |
619 // }); | |
620 //ORTHANC_ASSERT(foundWidget, "WindowID " << windowID << " was not found in the registered widgets!"); | |
621 //if(foundWidget) | |
622 // foundWidget-> | |
623 } | 696 } |
624 | 697 |
625 // SDL ONLY | 698 // SDL ONLY |
626 void GuiAdapter::RequestAnimationFrame(OnAnimationFrameFunc func, void* userData) | 699 void GuiAdapter::RequestAnimationFrame(OnAnimationFrameFunc func, void* userData) |
627 { | 700 { |
635 OpenGLMessageCallback(GLenum source, | 708 OpenGLMessageCallback(GLenum source, |
636 GLenum type, | 709 GLenum type, |
637 GLuint id, | 710 GLuint id, |
638 GLenum severity, | 711 GLenum severity, |
639 GLsizei length, | 712 GLsizei length, |
640 const GLchar* message, | 713 const GLchar * message, |
641 const void* userParam) | 714 const void* userParam) |
642 { | 715 { |
643 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) | 716 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) |
644 { | 717 { |
645 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", | 718 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", |
646 (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), | 719 (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), |
647 type, severity, message); | 720 type, severity, message); |
648 } | 721 } |
649 } | 722 } |
650 # endif | 723 # endif |
651 | 724 |
652 // SDL ONLY | 725 // SDL ONLY |
653 void GuiAdapter::Run() | 726 void GuiAdapter::Run() |
654 { | 727 { |
678 { | 751 { |
679 // TODO: call exit callbacks here | 752 // TODO: call exit callbacks here |
680 stop = true; | 753 stop = true; |
681 break; | 754 break; |
682 } | 755 } |
683 else if ( (event.type == SDL_MOUSEMOTION) || | 756 else if ((event.type == SDL_MOUSEMOTION) || |
684 (event.type == SDL_MOUSEBUTTONDOWN) || | 757 (event.type == SDL_MOUSEBUTTONDOWN) || |
685 (event.type == SDL_MOUSEBUTTONUP) ) | 758 (event.type == SDL_MOUSEBUTTONUP)) |
686 { | 759 { |
687 int scancodeCount = 0; | 760 int scancodeCount = 0; |
688 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); | 761 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); |
689 bool ctrlPressed(false); | 762 bool ctrlPressed(false); |
690 bool shiftPressed(false); | 763 bool shiftPressed(false); |
691 bool altPressed(false); | 764 bool altPressed(false); |
692 | 765 |
693 if (SDL_SCANCODE_LCTRL < scancodeCount && keyboardState[SDL_SCANCODE_LCTRL]) | 766 if (SDL_SCANCODE_LCTRL < scancodeCount && keyboardState[SDL_SCANCODE_LCTRL]) |
694 ctrlPressed = true; | 767 ctrlPressed = true; |
695 if (SDL_SCANCODE_RCTRL < scancodeCount && keyboardState[SDL_SCANCODE_RCTRL]) | 768 if (SDL_SCANCODE_RCTRL < scancodeCount && keyboardState[SDL_SCANCODE_RCTRL]) |
696 ctrlPressed = true; | 769 ctrlPressed = true; |
697 if (SDL_SCANCODE_LSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_LSHIFT]) | 770 if (SDL_SCANCODE_LSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_LSHIFT]) |
698 shiftPressed = true; | 771 shiftPressed = true; |
699 if (SDL_SCANCODE_RSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_RSHIFT]) | 772 if (SDL_SCANCODE_RSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_RSHIFT]) |
700 shiftPressed = true; | 773 shiftPressed = true; |
701 if (SDL_SCANCODE_LALT < scancodeCount && keyboardState[SDL_SCANCODE_LALT]) | 774 if (SDL_SCANCODE_LALT < scancodeCount && keyboardState[SDL_SCANCODE_LALT]) |
702 altPressed = true; | 775 altPressed = true; |
703 | 776 |
704 GuiAdapterMouseEvent dest; | 777 GuiAdapterMouseEvent dest; |
705 ConvertFromPlatform(dest, ctrlPressed, shiftPressed, altPressed, event); | 778 ConvertFromPlatform(dest, ctrlPressed, shiftPressed, altPressed, event); |
706 OnMouseEvent(event.window.windowID, dest); | 779 OnMouseEvent(event.window.windowID, dest); |
707 #if 0 | 780 #if 0 |
708 // for reference, how to create trackers | 781 // for reference, how to create trackers |
766 { | 839 { |
767 case SDLK_f: | 840 case SDLK_f: |
768 // window.GetWindow().ToggleMaximize(); //TODO: move to particular handler | 841 // window.GetWindow().ToggleMaximize(); //TODO: move to particular handler |
769 break; | 842 break; |
770 | 843 |
771 case SDLK_s: | |
772 #if 0 | |
773 // TODO: re-enable at application-level!!!! | |
774 VisitWidgets( | |
775 [](auto value) | |
776 { | |
777 auto widget = boost::dynamic_pointer_cast<VolumeSlicerWidget() | |
778 value->FitContent(); | |
779 }); | |
780 #endif | |
781 break; | |
782 | |
783 case SDLK_q: | 844 case SDLK_q: |
784 stop = true; | 845 stop = true; |
785 break; | 846 break; |
786 | 847 |
787 default: | 848 default: |
849 GuiAdapterKeyboardEvent dest; | |
850 ConvertFromPlatform(dest, event); | |
851 OnKeyboardEvent(event.window.windowID, dest); | |
788 break; | 852 break; |
789 } | 853 } |
790 } | 854 } |
791 // HandleApplicationEvent(controller, compositor, event, tracker); | 855 // HandleApplicationEvent(controller, compositor, event, tracker); |
792 } | 856 } |