editor_dock.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* editor_dock.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 "editor_dock.h"
  31. #include "core/input/shortcut.h"
  32. #include "core/io/config_file.h"
  33. #include "editor/docks/editor_dock_manager.h"
  34. void EditorDock::_set_default_slot_bind(DockSlot p_slot) {
  35. ERR_FAIL_COND(p_slot < DOCK_SLOT_NONE || p_slot >= DOCK_SLOT_MAX);
  36. default_slot = p_slot;
  37. }
  38. void EditorDock::_emit_changed() {
  39. emit_signal(SNAME("_tab_style_changed"));
  40. }
  41. void EditorDock::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("open"), &EditorDock::open);
  43. ClassDB::bind_method(D_METHOD("make_visible"), &EditorDock::make_visible);
  44. ClassDB::bind_method(D_METHOD("close"), &EditorDock::close);
  45. ClassDB::bind_method(D_METHOD("set_title", "title"), &EditorDock::set_title);
  46. ClassDB::bind_method(D_METHOD("get_title"), &EditorDock::get_title);
  47. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  48. ClassDB::bind_method(D_METHOD("set_layout_key", "layout_key"), &EditorDock::set_layout_key);
  49. ClassDB::bind_method(D_METHOD("get_layout_key"), &EditorDock::get_layout_key);
  50. ADD_PROPERTY(PropertyInfo(Variant::STRING, "layout_key"), "set_layout_key", "get_layout_key");
  51. ClassDB::bind_method(D_METHOD("set_global", "global"), &EditorDock::set_global);
  52. ClassDB::bind_method(D_METHOD("is_global"), &EditorDock::is_global);
  53. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "global"), "set_global", "is_global");
  54. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &EditorDock::set_transient);
  55. ClassDB::bind_method(D_METHOD("is_transient"), &EditorDock::is_transient);
  56. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  57. ClassDB::bind_method(D_METHOD("set_closable", "closable"), &EditorDock::set_closable);
  58. ClassDB::bind_method(D_METHOD("is_closable"), &EditorDock::is_closable);
  59. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closable"), "set_closable", "is_closable");
  60. ClassDB::bind_method(D_METHOD("set_icon_name", "icon_name"), &EditorDock::set_icon_name);
  61. ClassDB::bind_method(D_METHOD("get_icon_name"), &EditorDock::get_icon_name);
  62. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "icon_name"), "set_icon_name", "get_icon_name");
  63. ClassDB::bind_method(D_METHOD("set_dock_icon", "icon"), &EditorDock::set_dock_icon);
  64. ClassDB::bind_method(D_METHOD("get_dock_icon"), &EditorDock::get_dock_icon);
  65. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "dock_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_dock_icon", "get_dock_icon");
  66. ClassDB::bind_method(D_METHOD("set_force_show_icon", "force"), &EditorDock::set_force_show_icon);
  67. ClassDB::bind_method(D_METHOD("get_force_show_icon"), &EditorDock::get_force_show_icon);
  68. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_show_icon"), "set_force_show_icon", "get_force_show_icon");
  69. ClassDB::bind_method(D_METHOD("set_title_color", "color"), &EditorDock::set_title_color);
  70. ClassDB::bind_method(D_METHOD("get_title_color"), &EditorDock::get_title_color);
  71. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "title_color"), "set_title_color", "get_title_color");
  72. ClassDB::bind_method(D_METHOD("set_dock_shortcut", "shortcut"), &EditorDock::set_dock_shortcut);
  73. ClassDB::bind_method(D_METHOD("get_dock_shortcut"), &EditorDock::get_dock_shortcut);
  74. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "dock_shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_dock_shortcut", "get_dock_shortcut");
  75. ClassDB::bind_method(D_METHOD("set_default_slot", "slot"), &EditorDock::_set_default_slot_bind);
  76. ClassDB::bind_method(D_METHOD("get_default_slot"), &EditorDock::_get_default_slot_bind);
  77. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_slot", PROPERTY_HINT_ENUM, "None:-1,Left Side Upper-Left,Left Side Bottom-Left,Left Side Upper-Right,Left Side Bottom-Right,Right Side Upper-Left,Right Side Bottom-Left,Right Side Upper-Right,Right Side Bottom-Right,Bottom"), "set_default_slot", "get_default_slot");
  78. ClassDB::bind_method(D_METHOD("set_available_layouts", "layouts"), &EditorDock::set_available_layouts);
  79. ClassDB::bind_method(D_METHOD("get_available_layouts"), &EditorDock::get_available_layouts);
  80. ADD_PROPERTY(PropertyInfo(Variant::INT, "available_layouts", PROPERTY_HINT_FLAGS, "Vertical:1,Horizontal:2,Floating:4"), "set_available_layouts", "get_available_layouts");
  81. ADD_SIGNAL(MethodInfo("closed"));
  82. ADD_SIGNAL(MethodInfo("_tab_style_changed"));
  83. BIND_BITFIELD_FLAG(DOCK_LAYOUT_VERTICAL);
  84. BIND_BITFIELD_FLAG(DOCK_LAYOUT_HORIZONTAL);
  85. BIND_BITFIELD_FLAG(DOCK_LAYOUT_FLOATING);
  86. BIND_BITFIELD_FLAG(DOCK_LAYOUT_ALL);
  87. BIND_ENUM_CONSTANT(DOCK_SLOT_NONE);
  88. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UL);
  89. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BL);
  90. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UR);
  91. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BR);
  92. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UL);
  93. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BL);
  94. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UR);
  95. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BR);
  96. BIND_ENUM_CONSTANT(DOCK_SLOT_BOTTOM);
  97. BIND_ENUM_CONSTANT(DOCK_SLOT_MAX);
  98. GDVIRTUAL_BIND(_update_layout, "layout");
  99. GDVIRTUAL_BIND(_save_layout_to_config, "config", "section");
  100. GDVIRTUAL_BIND(_load_layout_from_config, "config", "section");
  101. }
  102. void EditorDock::open() {
  103. if (!is_open) {
  104. EditorDockManager::get_singleton()->open_dock(this, false);
  105. }
  106. }
  107. void EditorDock::make_visible() {
  108. EditorDockManager::get_singleton()->open_dock(this, true);
  109. }
  110. void EditorDock::close() {
  111. if (is_open) {
  112. EditorDockManager::get_singleton()->close_dock(this);
  113. }
  114. }
  115. void EditorDock::set_title(const String &p_title) {
  116. if (title == p_title) {
  117. return;
  118. }
  119. title = p_title;
  120. _emit_changed();
  121. }
  122. void EditorDock::set_global(bool p_global) {
  123. if (global == p_global) {
  124. return;
  125. }
  126. global = p_global;
  127. if (is_inside_tree()) {
  128. EditorDockManager::get_singleton()->update_docks_menu();
  129. }
  130. }
  131. void EditorDock::set_icon_name(const StringName &p_name) {
  132. if (icon_name == p_name) {
  133. return;
  134. }
  135. icon_name = p_name;
  136. _emit_changed();
  137. }
  138. void EditorDock::set_dock_icon(const Ref<Texture2D> &p_icon) {
  139. if (dock_icon == p_icon) {
  140. return;
  141. }
  142. dock_icon = p_icon;
  143. _emit_changed();
  144. }
  145. void EditorDock::set_force_show_icon(bool p_force) {
  146. if (force_show_icon == p_force) {
  147. return;
  148. }
  149. force_show_icon = p_force;
  150. _emit_changed();
  151. }
  152. void EditorDock::set_title_color(const Color &p_color) {
  153. if (title_color == p_color) {
  154. return;
  155. }
  156. title_color = p_color;
  157. _emit_changed();
  158. }
  159. void EditorDock::set_dock_shortcut(const Ref<Shortcut> &p_shortcut) {
  160. if (shortcut == p_shortcut) {
  161. return;
  162. }
  163. const Callable changed_callback = callable_mp(this, &EditorDock::_emit_changed);
  164. if (shortcut.is_valid()) {
  165. shortcut->disconnect_changed(changed_callback);
  166. }
  167. shortcut = p_shortcut;
  168. if (shortcut.is_valid()) {
  169. shortcut->connect_changed(changed_callback);
  170. }
  171. _emit_changed();
  172. }
  173. void EditorDock::set_default_slot(DockSlot p_slot) {
  174. ERR_FAIL_INDEX(p_slot, DOCK_SLOT_MAX);
  175. default_slot = p_slot;
  176. }
  177. String EditorDock::get_display_title() const {
  178. if (!title.is_empty()) {
  179. return title;
  180. }
  181. const String sname = get_name();
  182. if (sname.contains_char('@')) {
  183. // Auto-generated name, try to use something better.
  184. const Node *child = get_child_count() > 0 ? get_child(0) : nullptr;
  185. if (child) {
  186. // In user plugins, the child will usually be dock's content and have a proper name.
  187. return child->get_name();
  188. }
  189. }
  190. return sname;
  191. }
  192. String EditorDock::get_effective_layout_key() const {
  193. return layout_key.is_empty() ? get_display_title() : layout_key;
  194. }