gradient_texture_2d_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*************************************************************************/
  2. /* gradient_texture_2d_editor_plugin.cpp */
  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. #include "gradient_texture_2d_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/flow_container.h"
  36. #include "scene/gui/separator.h"
  37. Point2 GradientTexture2DEditorRect::_get_handle_position(const Handle p_handle) {
  38. // Get the handle's mouse position in pixels relative to offset.
  39. return (p_handle == HANDLE_FILL_FROM ? texture->get_fill_from() : texture->get_fill_to()).clamp(Vector2(), Vector2(1, 1)) * size;
  40. }
  41. void GradientTexture2DEditorRect::_update_fill_position() {
  42. if (handle == HANDLE_NONE) {
  43. return;
  44. }
  45. // Update the texture's fill_from/fill_to property based on mouse input.
  46. Vector2 percent = ((get_local_mouse_position() - offset) / size).clamp(Vector2(), Vector2(1, 1));
  47. if (snap_enabled) {
  48. percent = (percent - Vector2(0.5, 0.5)).snapped(Vector2(snap_size, snap_size)) + Vector2(0.5, 0.5);
  49. }
  50. String property_name = handle == HANDLE_FILL_FROM ? "fill_from" : "fill_to";
  51. undo_redo->create_action(vformat(TTR("Set %s"), property_name), UndoRedo::MERGE_ENDS);
  52. undo_redo->add_do_property(texture.ptr(), property_name, percent);
  53. undo_redo->add_undo_property(texture.ptr(), property_name, handle == HANDLE_FILL_FROM ? texture->get_fill_from() : texture->get_fill_to());
  54. undo_redo->commit_action();
  55. }
  56. void GradientTexture2DEditorRect::gui_input(const Ref<InputEvent> &p_event) {
  57. // Grab/release handle.
  58. const Ref<InputEventMouseButton> mb = p_event;
  59. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  60. if (mb->is_pressed()) {
  61. Point2 mouse_position = mb->get_position() - offset;
  62. if (Rect2(_get_handle_position(HANDLE_FILL_FROM).round() - handle_size / 2, handle_size).has_point(mouse_position)) {
  63. handle = HANDLE_FILL_FROM;
  64. } else if (Rect2(_get_handle_position(HANDLE_FILL_TO).round() - handle_size / 2, handle_size).has_point(mouse_position)) {
  65. handle = HANDLE_FILL_TO;
  66. } else {
  67. handle = HANDLE_NONE;
  68. }
  69. } else {
  70. _update_fill_position();
  71. handle = HANDLE_NONE;
  72. }
  73. }
  74. // Move handle.
  75. const Ref<InputEventMouseMotion> mm = p_event;
  76. if (mm.is_valid()) {
  77. _update_fill_position();
  78. }
  79. }
  80. void GradientTexture2DEditorRect::set_texture(Ref<GradientTexture2D> &p_texture) {
  81. texture = p_texture;
  82. texture->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  83. }
  84. void GradientTexture2DEditorRect::set_snap_enabled(bool p_snap_enabled) {
  85. snap_enabled = p_snap_enabled;
  86. queue_redraw();
  87. }
  88. void GradientTexture2DEditorRect::set_snap_size(float p_snap_size) {
  89. snap_size = p_snap_size;
  90. queue_redraw();
  91. }
  92. void GradientTexture2DEditorRect::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_ENTER_TREE:
  95. case NOTIFICATION_THEME_CHANGED: {
  96. checkerboard->set_texture(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")));
  97. } break;
  98. case NOTIFICATION_DRAW: {
  99. if (texture.is_null()) {
  100. return;
  101. }
  102. const Ref<Texture2D> fill_from_icon = get_theme_icon(SNAME("EditorPathSmoothHandle"), SNAME("EditorIcons"));
  103. const Ref<Texture2D> fill_to_icon = get_theme_icon(SNAME("EditorPathSharpHandle"), SNAME("EditorIcons"));
  104. handle_size = fill_from_icon->get_size();
  105. Size2 rect_size = get_size();
  106. // Get the size and position to draw the texture and handles at.
  107. size = Size2(texture->get_width() * rect_size.height / texture->get_height(), rect_size.height);
  108. if (size.width > rect_size.width) {
  109. size.width = rect_size.width;
  110. size.height = texture->get_height() * size.width / texture->get_width();
  111. }
  112. offset = ((rect_size - size + handle_size) / 2).round();
  113. size -= handle_size;
  114. checkerboard->set_rect(Rect2(offset, size));
  115. draw_set_transform(offset);
  116. draw_texture_rect(texture, Rect2(Point2(), size));
  117. // Draw grid snap lines.
  118. if (snap_enabled) {
  119. const Color primary_line_color = Color(0.5, 0.5, 0.5, 0.9);
  120. const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
  121. // Draw border and centered axis lines.
  122. draw_rect(Rect2(Point2(), size), primary_line_color, false);
  123. draw_line(Point2(size.width / 2, 0), Point2(size.width / 2, size.height), primary_line_color);
  124. draw_line(Point2(0, size.height / 2), Point2(size.width, size.height / 2), primary_line_color);
  125. // Draw vertical lines.
  126. int prev_idx = 0;
  127. for (int x = 0; x < size.width; x++) {
  128. int idx = int((x / size.width - 0.5) / snap_size);
  129. if (x > 0 && prev_idx != idx) {
  130. draw_line(Point2(x, 0), Point2(x, size.height), line_color);
  131. }
  132. prev_idx = idx;
  133. }
  134. // Draw horizontal lines.
  135. prev_idx = 0;
  136. for (int y = 0; y < size.height; y++) {
  137. int idx = int((y / size.height - 0.5) / snap_size);
  138. if (y > 0 && prev_idx != idx) {
  139. draw_line(Point2(0, y), Point2(size.width, y), line_color);
  140. }
  141. prev_idx = idx;
  142. }
  143. }
  144. // Draw handles.
  145. draw_texture(fill_from_icon, (_get_handle_position(HANDLE_FILL_FROM) - handle_size / 2).round());
  146. draw_texture(fill_to_icon, (_get_handle_position(HANDLE_FILL_TO) - handle_size / 2).round());
  147. } break;
  148. }
  149. }
  150. GradientTexture2DEditorRect::GradientTexture2DEditorRect() {
  151. undo_redo = EditorNode::get_undo_redo();
  152. checkerboard = memnew(TextureRect);
  153. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  154. checkerboard->set_draw_behind_parent(true);
  155. add_child(checkerboard);
  156. set_custom_minimum_size(Size2(0, 250 * EDSCALE));
  157. }
  158. ///////////////////////
  159. void GradientTexture2DEditor::_reverse_button_pressed() {
  160. undo_redo->create_action(TTR("Swap GradientTexture2D Fill Points"));
  161. undo_redo->add_do_property(texture.ptr(), "fill_from", texture->get_fill_to());
  162. undo_redo->add_do_property(texture.ptr(), "fill_to", texture->get_fill_from());
  163. undo_redo->add_undo_property(texture.ptr(), "fill_from", texture->get_fill_from());
  164. undo_redo->add_undo_property(texture.ptr(), "fill_to", texture->get_fill_to());
  165. undo_redo->commit_action();
  166. }
  167. void GradientTexture2DEditor::_set_snap_enabled(bool p_enabled) {
  168. texture_editor_rect->set_snap_enabled(p_enabled);
  169. snap_size_edit->set_visible(p_enabled);
  170. }
  171. void GradientTexture2DEditor::_set_snap_size(float p_snap_size) {
  172. texture_editor_rect->set_snap_size(MAX(p_snap_size, 0.01));
  173. }
  174. void GradientTexture2DEditor::set_texture(Ref<GradientTexture2D> &p_texture) {
  175. texture = p_texture;
  176. texture_editor_rect->set_texture(p_texture);
  177. }
  178. void GradientTexture2DEditor::_notification(int p_what) {
  179. switch (p_what) {
  180. case NOTIFICATION_ENTER_TREE:
  181. case NOTIFICATION_THEME_CHANGED: {
  182. reverse_button->set_icon(get_theme_icon(SNAME("ReverseGradient"), SNAME("EditorIcons")));
  183. snap_button->set_icon(get_theme_icon(SNAME("SnapGrid"), SNAME("EditorIcons")));
  184. } break;
  185. }
  186. }
  187. GradientTexture2DEditor::GradientTexture2DEditor() {
  188. undo_redo = EditorNode::get_undo_redo();
  189. HFlowContainer *toolbar = memnew(HFlowContainer);
  190. add_child(toolbar);
  191. reverse_button = memnew(Button);
  192. reverse_button->set_tooltip_text(TTR("Swap Gradient Fill Points"));
  193. toolbar->add_child(reverse_button);
  194. reverse_button->connect("pressed", callable_mp(this, &GradientTexture2DEditor::_reverse_button_pressed));
  195. toolbar->add_child(memnew(VSeparator));
  196. snap_button = memnew(Button);
  197. snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
  198. snap_button->set_toggle_mode(true);
  199. toolbar->add_child(snap_button);
  200. snap_button->connect("toggled", callable_mp(this, &GradientTexture2DEditor::_set_snap_enabled));
  201. snap_size_edit = memnew(EditorSpinSlider);
  202. snap_size_edit->set_min(0.01);
  203. snap_size_edit->set_max(0.5);
  204. snap_size_edit->set_step(0.01);
  205. snap_size_edit->set_value(0.1);
  206. snap_size_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  207. toolbar->add_child(snap_size_edit);
  208. snap_size_edit->connect("value_changed", callable_mp(this, &GradientTexture2DEditor::_set_snap_size));
  209. texture_editor_rect = memnew(GradientTexture2DEditorRect);
  210. add_child(texture_editor_rect);
  211. set_mouse_filter(MOUSE_FILTER_STOP);
  212. _set_snap_enabled(snap_button->is_pressed());
  213. _set_snap_size(snap_size_edit->get_value());
  214. }
  215. ///////////////////////
  216. bool EditorInspectorPluginGradientTexture2D::can_handle(Object *p_object) {
  217. return Object::cast_to<GradientTexture2D>(p_object) != nullptr;
  218. }
  219. void EditorInspectorPluginGradientTexture2D::parse_begin(Object *p_object) {
  220. GradientTexture2D *texture = Object::cast_to<GradientTexture2D>(p_object);
  221. if (!texture) {
  222. return;
  223. }
  224. Ref<GradientTexture2D> t(texture);
  225. GradientTexture2DEditor *editor = memnew(GradientTexture2DEditor);
  226. editor->set_texture(t);
  227. add_custom_control(editor);
  228. }
  229. ///////////////////////
  230. GradientTexture2DEditorPlugin::GradientTexture2DEditorPlugin() {
  231. Ref<EditorInspectorPluginGradientTexture2D> plugin;
  232. plugin.instantiate();
  233. add_inspector_plugin(plugin);
  234. }