status_indicator.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************/
  2. /* status_indicator.cpp */
  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 "status_indicator.h"
  31. #include "scene/gui/popup_menu.h"
  32. void StatusIndicator::_notification(int p_what) {
  33. ERR_MAIN_THREAD_GUARD;
  34. #ifdef TOOLS_ENABLED
  35. if (is_part_of_edited_scene()) {
  36. return;
  37. }
  38. #endif
  39. switch (p_what) {
  40. case NOTIFICATION_ENTER_TREE: {
  41. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_STATUS_INDICATOR)) {
  42. if (visible && iid == DisplayServer::INVALID_INDICATOR_ID) {
  43. iid = DisplayServer::get_singleton()->create_status_indicator(icon, tooltip, callable_mp(this, &StatusIndicator::_callback));
  44. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  45. if (pm) {
  46. RID menu_rid = pm->bind_global_menu();
  47. DisplayServer::get_singleton()->status_indicator_set_menu(iid, menu_rid);
  48. }
  49. }
  50. }
  51. } break;
  52. case NOTIFICATION_EXIT_TREE: {
  53. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_STATUS_INDICATOR)) {
  54. if (iid != DisplayServer::INVALID_INDICATOR_ID) {
  55. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  56. if (pm) {
  57. pm->unbind_global_menu();
  58. DisplayServer::get_singleton()->status_indicator_set_menu(iid, RID());
  59. }
  60. DisplayServer::get_singleton()->delete_status_indicator(iid);
  61. iid = DisplayServer::INVALID_INDICATOR_ID;
  62. }
  63. }
  64. } break;
  65. default:
  66. break;
  67. }
  68. }
  69. void StatusIndicator::_bind_methods() {
  70. ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &StatusIndicator::set_tooltip);
  71. ClassDB::bind_method(D_METHOD("get_tooltip"), &StatusIndicator::get_tooltip);
  72. ClassDB::bind_method(D_METHOD("set_icon", "texture"), &StatusIndicator::set_icon);
  73. ClassDB::bind_method(D_METHOD("get_icon"), &StatusIndicator::get_icon);
  74. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &StatusIndicator::set_visible);
  75. ClassDB::bind_method(D_METHOD("is_visible"), &StatusIndicator::is_visible);
  76. ClassDB::bind_method(D_METHOD("set_menu", "menu"), &StatusIndicator::set_menu);
  77. ClassDB::bind_method(D_METHOD("get_menu"), &StatusIndicator::get_menu);
  78. ClassDB::bind_method(D_METHOD("get_rect"), &StatusIndicator::get_rect);
  79. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::INT, "mouse_button"), PropertyInfo(Variant::VECTOR2I, "mouse_position")));
  80. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip", "get_tooltip");
  81. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_icon", "get_icon");
  82. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "menu", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PopupMenu"), "set_menu", "get_menu");
  83. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  84. }
  85. void StatusIndicator::_callback(MouseButton p_index, const Point2i &p_pos) {
  86. emit_signal(SceneStringName(pressed), p_index, p_pos);
  87. }
  88. void StatusIndicator::set_icon(const Ref<Texture2D> &p_icon) {
  89. ERR_MAIN_THREAD_GUARD;
  90. icon = p_icon;
  91. if (iid != DisplayServer::INVALID_INDICATOR_ID) {
  92. DisplayServer::get_singleton()->status_indicator_set_icon(iid, icon);
  93. }
  94. }
  95. Ref<Texture2D> StatusIndicator::get_icon() const {
  96. return icon;
  97. }
  98. void StatusIndicator::set_tooltip(const String &p_tooltip) {
  99. ERR_MAIN_THREAD_GUARD;
  100. tooltip = p_tooltip;
  101. if (iid != DisplayServer::INVALID_INDICATOR_ID) {
  102. DisplayServer::get_singleton()->status_indicator_set_tooltip(iid, tooltip);
  103. }
  104. }
  105. String StatusIndicator::get_tooltip() const {
  106. return tooltip;
  107. }
  108. void StatusIndicator::set_menu(const NodePath &p_menu) {
  109. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  110. if (pm) {
  111. pm->unbind_global_menu();
  112. if (iid != DisplayServer::INVALID_INDICATOR_ID) {
  113. DisplayServer::get_singleton()->status_indicator_set_menu(iid, RID());
  114. }
  115. }
  116. menu = p_menu;
  117. pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  118. if (pm) {
  119. if (iid != DisplayServer::INVALID_INDICATOR_ID) {
  120. RID menu_rid = pm->bind_global_menu();
  121. DisplayServer::get_singleton()->status_indicator_set_menu(iid, menu_rid);
  122. }
  123. }
  124. }
  125. NodePath StatusIndicator::get_menu() const {
  126. return menu;
  127. }
  128. void StatusIndicator::set_visible(bool p_visible) {
  129. ERR_MAIN_THREAD_GUARD;
  130. if (visible == p_visible) {
  131. return;
  132. }
  133. visible = p_visible;
  134. if (!is_inside_tree()) {
  135. return;
  136. }
  137. #ifdef TOOLS_ENABLED
  138. if (is_part_of_edited_scene()) {
  139. return;
  140. }
  141. #endif
  142. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_STATUS_INDICATOR)) {
  143. if (visible && iid == DisplayServer::INVALID_INDICATOR_ID) {
  144. iid = DisplayServer::get_singleton()->create_status_indicator(icon, tooltip, callable_mp(this, &StatusIndicator::_callback));
  145. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  146. if (pm) {
  147. RID menu_rid = pm->bind_global_menu();
  148. DisplayServer::get_singleton()->status_indicator_set_menu(iid, menu_rid);
  149. }
  150. }
  151. if (!visible && iid != DisplayServer::INVALID_INDICATOR_ID) {
  152. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(menu));
  153. if (pm) {
  154. pm->unbind_global_menu();
  155. DisplayServer::get_singleton()->status_indicator_set_menu(iid, RID());
  156. }
  157. DisplayServer::get_singleton()->delete_status_indicator(iid);
  158. iid = DisplayServer::INVALID_INDICATOR_ID;
  159. }
  160. }
  161. }
  162. bool StatusIndicator::is_visible() const {
  163. return visible;
  164. }
  165. Rect2 StatusIndicator::get_rect() const {
  166. if (iid == DisplayServer::INVALID_INDICATOR_ID) {
  167. return Rect2();
  168. }
  169. return DisplayServer::get_singleton()->status_indicator_get_rect(iid);
  170. }