gradient_texture_2d_editor_plugin.cpp 11 KB

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