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) 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. #pragma once
  31. #include "editor/editor_inspector.h"
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/margin_container.h"
  35. class CheckBox;
  36. class CheckButton;
  37. class EditorSelection;
  38. class GridContainer;
  39. class Label;
  40. class OptionButton;
  41. class PanelContainer;
  42. class Separator;
  43. class TextureRect;
  44. // Inspector controls.
  45. class ControlPositioningWarning : public MarginContainer {
  46. GDCLASS(ControlPositioningWarning, MarginContainer);
  47. Control *control_node = nullptr;
  48. PanelContainer *bg_panel = nullptr;
  49. GridContainer *grid = nullptr;
  50. TextureRect *title_icon = nullptr;
  51. TextureRect *hint_icon = nullptr;
  52. Label *title_label = nullptr;
  53. Label *hint_label = nullptr;
  54. Control *hint_filler_left = nullptr;
  55. Control *hint_filler_right = nullptr;
  56. void _update_warning();
  57. void _update_toggler();
  58. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  59. protected:
  60. void _notification(int p_notification);
  61. public:
  62. void set_control(Control *p_node);
  63. ControlPositioningWarning();
  64. };
  65. class EditorPropertyAnchorsPreset : public EditorProperty {
  66. GDCLASS(EditorPropertyAnchorsPreset, EditorProperty);
  67. OptionButton *options = nullptr;
  68. void _option_selected(int p_which);
  69. protected:
  70. virtual void _set_read_only(bool p_read_only) override;
  71. void _notification(int p_what);
  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. bool inside_control_category = false;
  105. public:
  106. virtual bool can_handle(Object *p_object) override;
  107. virtual void parse_category(Object *p_object, const String &p_category) override;
  108. virtual void parse_group(Object *p_object, const String &p_group) override;
  109. 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 BitField<PropertyUsageFlags> p_usage, const bool p_wide = false) override;
  110. };
  111. // Toolbar controls.
  112. class ControlEditorPopupButton : public Button {
  113. GDCLASS(ControlEditorPopupButton, Button);
  114. Ref<Texture2D> arrow_icon;
  115. PopupPanel *popup_panel = nullptr;
  116. VBoxContainer *popup_vbox = nullptr;
  117. void _popup_visibility_changed(bool p_visible);
  118. protected:
  119. void _notification(int p_what);
  120. public:
  121. virtual Size2 get_minimum_size() const override;
  122. virtual void toggled(bool p_pressed) override;
  123. VBoxContainer *get_popup_hbox() const { return popup_vbox; }
  124. ControlEditorPopupButton();
  125. };
  126. class ControlEditorPresetPicker : public MarginContainer {
  127. GDCLASS(ControlEditorPresetPicker, MarginContainer);
  128. virtual void _preset_button_pressed(const int p_preset) {}
  129. protected:
  130. static constexpr int grid_separation = 0;
  131. HashMap<int, Button *> preset_buttons;
  132. void _add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name);
  133. void _add_separator(BoxContainer *p_box, Separator *p_separator);
  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. CheckButton *expand_button = nullptr;
  147. bool vertical = false;
  148. virtual void _preset_button_pressed(const int p_preset) override;
  149. void _expand_button_pressed();
  150. protected:
  151. void _notification(int p_notification);
  152. static void _bind_methods();
  153. public:
  154. void set_allowed_flags(Vector<SizeFlags> &p_flags);
  155. void set_expand_flag(bool p_expand);
  156. SizeFlagPresetPicker(bool p_vertical);
  157. };
  158. class ControlEditorToolbar : public HBoxContainer {
  159. GDCLASS(ControlEditorToolbar, HBoxContainer);
  160. EditorSelection *editor_selection = nullptr;
  161. ControlEditorPopupButton *anchors_button = nullptr;
  162. ControlEditorPopupButton *containers_button = nullptr;
  163. Button *anchor_mode_button = nullptr;
  164. SizeFlagPresetPicker *container_h_picker = nullptr;
  165. SizeFlagPresetPicker *container_v_picker = nullptr;
  166. bool anchors_mode = false;
  167. void _anchors_preset_selected(int p_preset);
  168. void _anchors_to_current_ratio();
  169. void _anchor_mode_toggled(bool p_status);
  170. void _container_flags_selected(int p_flags, bool p_vertical);
  171. void _expand_flag_toggled(bool p_expand, bool p_vertical);
  172. Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
  173. bool _is_node_locked(const Node *p_node);
  174. List<Control *> _get_edited_controls();
  175. void _selection_changed();
  176. protected:
  177. void _notification(int p_notification);
  178. static ControlEditorToolbar *singleton;
  179. public:
  180. bool is_anchors_mode_enabled() { return anchors_mode; }
  181. static ControlEditorToolbar *get_singleton() { return singleton; }
  182. ControlEditorToolbar();
  183. };
  184. // Editor plugin.
  185. class ControlEditorPlugin : public EditorPlugin {
  186. GDCLASS(ControlEditorPlugin, EditorPlugin);
  187. ControlEditorToolbar *toolbar = nullptr;
  188. public:
  189. virtual String get_plugin_name() const override { return "Control"; }
  190. ControlEditorPlugin();
  191. };