control_editor_plugin.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*************************************************************************/
  2. /* control_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef CONTROL_EDITOR_PLUGIN_H
  31. #define CONTROL_EDITOR_PLUGIN_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/check_box.h"
  36. #include "scene/gui/control.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "scene/gui/option_button.h"
  40. #include "scene/gui/panel_container.h"
  41. #include "scene/gui/popup.h"
  42. #include "scene/gui/separator.h"
  43. #include "scene/gui/texture_rect.h"
  44. class EditorUndoRedoManager;
  45. // Inspector controls.
  46. class ControlPositioningWarning : public MarginContainer {
  47. GDCLASS(ControlPositioningWarning, MarginContainer);
  48. Control *control_node = nullptr;
  49. PanelContainer *bg_panel = nullptr;
  50. GridContainer *grid = nullptr;
  51. TextureRect *title_icon = nullptr;
  52. TextureRect *hint_icon = nullptr;
  53. Label *title_label = nullptr;
  54. Label *hint_label = nullptr;
  55. Control *hint_filler_left = nullptr;
  56. Control *hint_filler_right = nullptr;
  57. void _update_warning();
  58. void _update_toggler();
  59. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  60. protected:
  61. void _notification(int p_notification);
  62. public:
  63. void set_control(Control *p_node);
  64. ControlPositioningWarning();
  65. };
  66. class EditorPropertyAnchorsPreset : public EditorProperty {
  67. GDCLASS(EditorPropertyAnchorsPreset, EditorProperty);
  68. OptionButton *options = nullptr;
  69. void _option_selected(int p_which);
  70. protected:
  71. virtual void _set_read_only(bool p_read_only) override;
  72. public:
  73. void setup(const Vector<String> &p_options);
  74. virtual void update_property() override;
  75. EditorPropertyAnchorsPreset();
  76. };
  77. class EditorPropertySizeFlags : public EditorProperty {
  78. GDCLASS(EditorPropertySizeFlags, EditorProperty);
  79. enum FlagPreset {
  80. SIZE_FLAGS_PRESET_FILL,
  81. SIZE_FLAGS_PRESET_SHRINK_BEGIN,
  82. SIZE_FLAGS_PRESET_SHRINK_CENTER,
  83. SIZE_FLAGS_PRESET_SHRINK_END,
  84. SIZE_FLAGS_PRESET_CUSTOM,
  85. };
  86. OptionButton *flag_presets = nullptr;
  87. CheckBox *flag_expand = nullptr;
  88. VBoxContainer *flag_options = nullptr;
  89. Vector<CheckBox *> flag_checks;
  90. bool vertical = false;
  91. bool keep_selected_preset = false;
  92. void _preset_selected(int p_which);
  93. void _expand_toggled();
  94. void _flag_toggled();
  95. protected:
  96. virtual void _set_read_only(bool p_read_only) override;
  97. public:
  98. void setup(const Vector<String> &p_options, bool p_vertical);
  99. virtual void update_property() override;
  100. EditorPropertySizeFlags();
  101. };
  102. class EditorInspectorPluginControl : public EditorInspectorPlugin {
  103. GDCLASS(EditorInspectorPluginControl, EditorInspectorPlugin);
  104. public:
  105. virtual bool can_handle(Object *p_object) override;
  106. virtual void parse_group(Object *p_object, const String &p_group) override;
  107. virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide = false) override;
  108. };
  109. // Toolbar controls.
  110. class ControlEditorPopupButton : public Button {
  111. GDCLASS(ControlEditorPopupButton, Button);
  112. Ref<Texture2D> arrow_icon;
  113. PopupPanel *popup_panel = nullptr;
  114. VBoxContainer *popup_vbox = nullptr;
  115. void _popup_visibility_changed(bool p_visible);
  116. protected:
  117. void _notification(int p_what);
  118. public:
  119. virtual Size2 get_minimum_size() const override;
  120. virtual void toggled(bool p_pressed) override;
  121. VBoxContainer *get_popup_hbox() const { return popup_vbox; }
  122. ControlEditorPopupButton();
  123. };
  124. class ControlEditorPresetPicker : public MarginContainer {
  125. GDCLASS(ControlEditorPresetPicker, MarginContainer);
  126. virtual void _preset_button_pressed(const int p_preset) {}
  127. protected:
  128. static constexpr int grid_separation = 0;
  129. HashMap<int, Button *> preset_buttons;
  130. void _add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name);
  131. void _add_separator(BoxContainer *p_box, Separator *p_separator);
  132. public:
  133. ControlEditorPresetPicker() {}
  134. };
  135. class AnchorPresetPicker : public ControlEditorPresetPicker {
  136. GDCLASS(AnchorPresetPicker, ControlEditorPresetPicker);
  137. virtual void _preset_button_pressed(const int p_preset) override;
  138. protected:
  139. void _notification(int p_notification);
  140. static void _bind_methods();
  141. public:
  142. AnchorPresetPicker();
  143. };
  144. class SizeFlagPresetPicker : public ControlEditorPresetPicker {
  145. GDCLASS(SizeFlagPresetPicker, ControlEditorPresetPicker);
  146. CheckBox *expand_button;
  147. bool vertical = false;
  148. virtual void _preset_button_pressed(const int p_preset) override;
  149. protected:
  150. void _notification(int p_notification);
  151. static void _bind_methods();
  152. public:
  153. void set_allowed_flags(Vector<SizeFlags> &p_flags);
  154. SizeFlagPresetPicker(bool p_vertical);
  155. };
  156. class ControlEditorToolbar : public HBoxContainer {
  157. GDCLASS(ControlEditorToolbar, HBoxContainer);
  158. Ref<EditorUndoRedoManager> undo_redo;
  159. EditorSelection *editor_selection = nullptr;
  160. ControlEditorPopupButton *anchors_button = nullptr;
  161. ControlEditorPopupButton *containers_button = nullptr;
  162. Button *anchor_mode_button = nullptr;
  163. SizeFlagPresetPicker *container_h_picker = nullptr;
  164. SizeFlagPresetPicker *container_v_picker = nullptr;
  165. bool anchors_mode = false;
  166. void _anchors_preset_selected(int p_preset);
  167. void _anchors_to_current_ratio();
  168. void _anchor_mode_toggled(bool p_status);
  169. void _container_flags_selected(int p_flags, bool p_vertical);
  170. Vector2 _anchor_to_position(const Control *p_control, Vector2 anchor);
  171. Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
  172. bool _is_node_locked(const Node *p_node);
  173. List<Control *> _get_edited_controls();
  174. void _selection_changed();
  175. protected:
  176. void _notification(int p_notification);
  177. static ControlEditorToolbar *singleton;
  178. public:
  179. bool is_anchors_mode_enabled() { return anchors_mode; };
  180. static ControlEditorToolbar *get_singleton() { return singleton; }
  181. ControlEditorToolbar();
  182. };
  183. // Editor plugin.
  184. class ControlEditorPlugin : public EditorPlugin {
  185. GDCLASS(ControlEditorPlugin, EditorPlugin);
  186. ControlEditorToolbar *toolbar = nullptr;
  187. public:
  188. virtual String get_name() const override { return "Control"; }
  189. ControlEditorPlugin();
  190. };
  191. #endif // CONTROL_EDITOR_PLUGIN_H