control_editor_plugin.h 8.1 KB

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