control_editor_plugin.h 8.4 KB

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