godot_menu_delegate.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**************************************************************************/
  2. /* godot_menu_delegate.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "godot_menu_delegate.h"
  31. #include "display_server_macos.h"
  32. #include "godot_menu_item.h"
  33. #include "key_mapping_macos.h"
  34. @implementation GodotMenuDelegate
  35. - (void)doNothing:(id)sender {
  36. }
  37. - (void)menuNeedsUpdate:(NSMenu *)menu {
  38. if (DisplayServer::get_singleton()) {
  39. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  40. ds->menu_open(menu);
  41. }
  42. }
  43. - (void)menuDidClose:(NSMenu *)menu {
  44. if (DisplayServer::get_singleton()) {
  45. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  46. ds->menu_close(menu);
  47. }
  48. }
  49. - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
  50. if (item) {
  51. GodotMenuItem *value = [item representedObject];
  52. if (value && value->hover_callback.is_valid()) {
  53. // If custom callback is set, use it.
  54. Variant ret;
  55. Callable::CallError ce;
  56. const Variant *args[1] = { &value->meta };
  57. value->hover_callback.callp(args, 1, ret, ce);
  58. if (ce.error != Callable::CallError::CALL_OK) {
  59. ERR_PRINT(vformat("Failed to execute menu hover callback: %s.", Variant::get_callable_error_text(value->hover_callback, args, 1, ce)));
  60. }
  61. }
  62. }
  63. }
  64. - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action {
  65. NSString *ev_key = [[event charactersIgnoringModifiers] lowercaseString];
  66. NSUInteger ev_modifiers = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
  67. for (int i = 0; i < [menu numberOfItems]; i++) {
  68. const NSMenuItem *menu_item = [menu itemAtIndex:i];
  69. if ([menu_item isEnabled] && [[menu_item keyEquivalent] compare:ev_key] == NSOrderedSame) {
  70. NSUInteger item_modifiers = [menu_item keyEquivalentModifierMask];
  71. if (ev_modifiers == item_modifiers) {
  72. GodotMenuItem *value = [menu_item representedObject];
  73. if (value) {
  74. if (value->key_callback.is_valid()) {
  75. // If custom callback is set, use it.
  76. Variant ret;
  77. Callable::CallError ce;
  78. const Variant *args[1] = { &value->meta };
  79. value->key_callback.callp(args, 1, ret, ce);
  80. if (ce.error != Callable::CallError::CALL_OK) {
  81. ERR_PRINT(vformat("Failed to execute menu key callback: %s.", Variant::get_callable_error_text(value->key_callback, args, 1, ce)));
  82. }
  83. } else {
  84. // Otherwise redirect event to the engine.
  85. if (DisplayServer::get_singleton()) {
  86. [[[NSApplication sharedApplication] keyWindow] sendEvent:event];
  87. }
  88. }
  89. // Suppress default menu action.
  90. *target = self;
  91. *action = @selector(doNothing:);
  92. }
  93. return YES;
  94. }
  95. }
  96. }
  97. return NO;
  98. }
  99. @end