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. #ifndef CONTROL_EDITOR_PLUGIN_H
  31. #define CONTROL_EDITOR_PLUGIN_H
  32. #include "editor/editor_inspector.h"
  33. #include "editor/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 GridContainer;
  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. public:
  74. void setup(const Vector<String> &p_options);
  75. virtual void update_property() override;
  76. EditorPropertyAnchorsPreset();
  77. };
  78. class EditorPropertySizeFlags : public EditorProperty {
  79. GDCLASS(EditorPropertySizeFlags, EditorProperty);
  80. enum FlagPreset {
  81. SIZE_FLAGS_PRESET_FILL,
  82. SIZE_FLAGS_PRESET_SHRINK_BEGIN,
  83. SIZE_FLAGS_PRESET_SHRINK_CENTER,
  84. SIZE_FLAGS_PRESET_SHRINK_END,
  85. SIZE_FLAGS_PRESET_CUSTOM,
  86. };
  87. OptionButton *flag_presets = nullptr;
  88. CheckBox *flag_expand = nullptr;
  89. VBoxContainer *flag_options = nullptr;
  90. Vector<CheckBox *> flag_checks;
  91. bool vertical = false;
  92. bool keep_selected_preset = false;
  93. void _preset_selected(int p_which);
  94. void _expand_toggled();
  95. void _flag_toggled();
  96. protected:
  97. virtual void _set_read_only(bool p_read_only) override;
  98. public:
  99. void setup(const Vector<String> &p_options, bool p_vertical);
  100. virtual void update_property() override;
  101. EditorPropertySizeFlags();
  102. };
  103. class EditorInspectorPluginControl : public EditorInspectorPlugin {
  104. GDCLASS(EditorInspectorPluginControl, EditorInspectorPlugin);
  105. public:
  106. virtual bool can_handle(Object *p_object) override;
  107. virtual void parse_group(Object *p_object, const String &p_group) override;
  108. 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;
  109. };
  110. // Toolbar controls.
  111. class ControlEditorPopupButton : public Button {
  112. GDCLASS(ControlEditorPopupButton, Button);
  113. Ref<Texture2D> arrow_icon;
  114. PopupPanel *popup_panel = nullptr;
  115. VBoxContainer *popup_vbox = nullptr;
  116. void _popup_visibility_changed(bool p_visible);
  117. protected:
  118. void _notification(int p_what);
  119. public:
  120. virtual Size2 get_minimum_size() const override;
  121. virtual void toggled(bool p_pressed) override;
  122. VBoxContainer *get_popup_hbox() const { return popup_vbox; }
  123. ControlEditorPopupButton();
  124. };
  125. class ControlEditorPresetPicker : public MarginContainer {
  126. GDCLASS(ControlEditorPresetPicker, MarginContainer);
  127. virtual void _preset_button_pressed(const int p_preset) {}
  128. protected:
  129. static constexpr int grid_separation = 0;
  130. HashMap<int, Button *> preset_buttons;
  131. void _add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name);
  132. void _add_separator(BoxContainer *p_box, Separator *p_separator);
  133. public:
  134. ControlEditorPresetPicker() {}
  135. };
  136. class AnchorPresetPicker : public ControlEditorPresetPicker {
  137. GDCLASS(AnchorPresetPicker, ControlEditorPresetPicker);
  138. virtual void _preset_button_pressed(const int p_preset) override;
  139. protected:
  140. void _notification(int p_notification);
  141. static void _bind_methods();
  142. public:
  143. AnchorPresetPicker();
  144. };
  145. class SizeFlagPresetPicker : public ControlEditorPresetPicker {
  146. GDCLASS(SizeFlagPresetPicker, ControlEditorPresetPicker);
  147. CheckBox *expand_button = nullptr;
  148. bool vertical = false;
  149. virtual void _preset_button_pressed(const int p_preset) override;
  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. SizeFlagPresetPicker(bool p_vertical);
  156. };
  157. class ControlEditorToolbar : public HBoxContainer {
  158. GDCLASS(ControlEditorToolbar, HBoxContainer);
  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 _position_to_anchor(const Control *p_control, Vector2 position);
  171. bool _is_node_locked(const Node *p_node);
  172. List<Control *> _get_edited_controls();
  173. void _selection_changed();
  174. protected:
  175. void _notification(int p_notification);
  176. static ControlEditorToolbar *singleton;
  177. public:
  178. bool is_anchors_mode_enabled() { return anchors_mode; };
  179. static ControlEditorToolbar *get_singleton() { return singleton; }
  180. ControlEditorToolbar();
  181. };
  182. // Editor plugin.
  183. class ControlEditorPlugin : public EditorPlugin {
  184. GDCLASS(ControlEditorPlugin, EditorPlugin);
  185. ControlEditorToolbar *toolbar = nullptr;
  186. public:
  187. virtual String get_name() const override { return "Control"; }
  188. ControlEditorPlugin();
  189. };
  190. #endif // CONTROL_EDITOR_PLUGIN_H