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