gradient_texture_2d_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_undo_redo_manager.h"
  33. #include "editor/gui/editor_spin_slider.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/flow_container.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/resources/gradient_texture.h"
  40. Point2 GradientTexture2DEdit::_get_handle_pos(const Handle p_handle) {
  41. // Get the handle's mouse position in pixels relative to offset.
  42. return (p_handle == HANDLE_FROM ? texture->get_fill_from() : texture->get_fill_to()).clampf(0, 1) * size;
  43. }
  44. GradientTexture2DEdit::Handle GradientTexture2DEdit::get_handle_at(const Vector2 &p_pos) {
  45. Point2 from_pos = _get_handle_pos(HANDLE_FROM);
  46. Point2 to_pos = _get_handle_pos(HANDLE_TO);
  47. // If both handles are at the position, grab the one that's closer.
  48. if (p_pos.distance_squared_to(from_pos) < p_pos.distance_squared_to(to_pos)) {
  49. return Rect2(from_pos.round() - handle_size / 2, handle_size).has_point(p_pos) ? HANDLE_FROM : HANDLE_NONE;
  50. } else {
  51. return Rect2(to_pos.round() - handle_size / 2, handle_size).has_point(p_pos) ? HANDLE_TO : HANDLE_NONE;
  52. }
  53. }
  54. void GradientTexture2DEdit::set_fill_pos(const Vector2 &p_pos) {
  55. if (p_pos.is_equal_approx(initial_grab_pos)) {
  56. return;
  57. }
  58. const StringName property_name = (grabbed == HANDLE_FROM) ? "fill_from" : "fill_to";
  59. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  60. undo_redo->create_action(TTR("Move GradientTexture2D Fill Point"));
  61. undo_redo->add_do_property(texture.ptr(), property_name, p_pos);
  62. undo_redo->add_undo_property(texture.ptr(), property_name, initial_grab_pos);
  63. undo_redo->commit_action();
  64. }
  65. void GradientTexture2DEdit::gui_input(const Ref<InputEvent> &p_event) {
  66. const Ref<InputEventMouseButton> mb = p_event;
  67. if (mb.is_valid()) {
  68. if (mb->get_button_index() == MouseButton::LEFT) {
  69. if (mb->is_pressed()) {
  70. grabbed = get_handle_at(mb->get_position() - offset);
  71. if (grabbed != HANDLE_NONE) {
  72. initial_grab_pos = _get_handle_pos(grabbed) / size;
  73. queue_redraw();
  74. }
  75. } else {
  76. // Release the handle.
  77. if (grabbed != HANDLE_NONE) {
  78. set_fill_pos(_get_handle_pos(grabbed) / size);
  79. grabbed = HANDLE_NONE;
  80. queue_redraw();
  81. }
  82. }
  83. }
  84. if (grabbed != HANDLE_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  85. texture->set((grabbed == HANDLE_FROM) ? SNAME("fill_from") : SNAME("fill_to"), initial_grab_pos);
  86. grabbed = HANDLE_NONE;
  87. queue_redraw();
  88. }
  89. }
  90. // Move handle.
  91. const Ref<InputEventMouseMotion> mm = p_event;
  92. if (mm.is_valid()) {
  93. Vector2 mpos = mm->get_position() - offset;
  94. Handle handle_at_mpos = get_handle_at(mpos);
  95. if (hovered != handle_at_mpos) {
  96. hovered = handle_at_mpos;
  97. queue_redraw();
  98. }
  99. if (grabbed == HANDLE_NONE) {
  100. return;
  101. }
  102. Vector2 new_pos = (mpos / size).clampf(0, 1);
  103. if (snap_enabled || mm->is_command_or_control_pressed()) {
  104. new_pos = new_pos.snappedf(1.0 / snap_count);
  105. }
  106. // Allow to snap to an axis with Shift.
  107. if (mm->is_shift_pressed()) {
  108. Vector2 initial_mpos = initial_grab_pos * size;
  109. if (Math::abs(mpos.x - initial_mpos.x) > Math::abs(mpos.y - initial_mpos.y)) {
  110. new_pos.y = initial_grab_pos.y;
  111. } else {
  112. new_pos.x = initial_grab_pos.x;
  113. }
  114. }
  115. // Do it directly from the texture so there's no undo/redo until the handle is released.
  116. texture->set((grabbed == HANDLE_FROM) ? SNAME("fill_from") : SNAME("fill_to"), new_pos);
  117. }
  118. }
  119. void GradientTexture2DEdit::set_texture(Ref<GradientTexture2D> &p_texture) {
  120. texture = p_texture;
  121. texture->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  122. }
  123. void GradientTexture2DEdit::set_snap_enabled(bool p_snap_enabled) {
  124. snap_enabled = p_snap_enabled;
  125. queue_redraw();
  126. if (texture.is_valid()) {
  127. if (snap_enabled) {
  128. texture->set_meta(SNAME("_snap_enabled"), true);
  129. } else {
  130. texture->remove_meta(SNAME("_snap_enabled"));
  131. }
  132. }
  133. }
  134. void GradientTexture2DEdit::set_snap_count(int p_snap_count) {
  135. snap_count = p_snap_count;
  136. queue_redraw();
  137. if (texture.is_valid()) {
  138. if (snap_count != GradientTexture2DEditor::DEFAULT_SNAP) {
  139. texture->set_meta(SNAME("_snap_count"), snap_count);
  140. } else {
  141. texture->remove_meta(SNAME("_snap_count"));
  142. }
  143. }
  144. }
  145. void GradientTexture2DEdit::_notification(int p_what) {
  146. switch (p_what) {
  147. case NOTIFICATION_MOUSE_EXIT: {
  148. if (hovered != HANDLE_NONE) {
  149. hovered = HANDLE_NONE;
  150. queue_redraw();
  151. }
  152. } break;
  153. case NOTIFICATION_THEME_CHANGED: {
  154. checkerboard->set_texture(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")));
  155. } break;
  156. case NOTIFICATION_DRAW: {
  157. _draw();
  158. } break;
  159. }
  160. }
  161. void GradientTexture2DEdit::_draw() {
  162. if (texture.is_null()) {
  163. return;
  164. }
  165. const Ref<Texture2D> fill_from_icon = get_editor_theme_icon(SNAME("EditorPathSmoothHandle"));
  166. const Ref<Texture2D> fill_to_icon = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));
  167. handle_size = fill_from_icon->get_size();
  168. Size2 rect_size = get_size();
  169. // Get the size and position to draw the texture and handles at.
  170. // Subtract handle sizes so they stay inside the preview, but keep the texture's aspect ratio.
  171. Size2 available_size = rect_size - handle_size;
  172. Size2 ratio = available_size / texture->get_size();
  173. size = MIN(ratio.x, ratio.y) * texture->get_size();
  174. offset = ((rect_size - size) / 2).round();
  175. checkerboard->set_rect(Rect2(offset, size));
  176. draw_set_transform(offset);
  177. draw_texture_rect(texture, Rect2(Point2(), size));
  178. // Draw grid snap lines.
  179. if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && grabbed != HANDLE_NONE)) {
  180. const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
  181. for (int idx = 0; idx < snap_count + 1; idx++) {
  182. float x = float(idx * size.width) / snap_count;
  183. float y = float(idx * size.height) / snap_count;
  184. draw_line(Point2(x, 0), Point2(x, size.height), line_color);
  185. draw_line(Point2(0, y), Point2(size.width, y), line_color);
  186. }
  187. }
  188. // Draw handles.
  189. const Color focus_modulate = Color(0.5, 1, 2);
  190. bool modulate_handle_from = grabbed == HANDLE_FROM || hovered == HANDLE_FROM;
  191. bool modulate_handle_to = grabbed == HANDLE_TO || hovered == HANDLE_TO;
  192. draw_texture(fill_from_icon, (_get_handle_pos(HANDLE_FROM) - handle_size / 2).round(), modulate_handle_from ? focus_modulate : Color(1, 1, 1));
  193. draw_texture(fill_to_icon, (_get_handle_pos(HANDLE_TO) - handle_size / 2).round(), modulate_handle_to ? focus_modulate : Color(1, 1, 1));
  194. }
  195. GradientTexture2DEdit::GradientTexture2DEdit() {
  196. checkerboard = memnew(TextureRect);
  197. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  198. checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  199. checkerboard->set_draw_behind_parent(true);
  200. add_child(checkerboard, false, INTERNAL_MODE_FRONT);
  201. set_custom_minimum_size(Size2(0, 250 * EDSCALE));
  202. }
  203. ///////////////////////
  204. const int GradientTexture2DEditor::DEFAULT_SNAP = 10;
  205. void GradientTexture2DEditor::_reverse_button_pressed() {
  206. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  207. undo_redo->create_action(TTR("Swap GradientTexture2D Fill Points"));
  208. undo_redo->add_do_property(texture.ptr(), "fill_from", texture->get_fill_to());
  209. undo_redo->add_do_property(texture.ptr(), "fill_to", texture->get_fill_from());
  210. undo_redo->add_undo_property(texture.ptr(), "fill_from", texture->get_fill_from());
  211. undo_redo->add_undo_property(texture.ptr(), "fill_to", texture->get_fill_to());
  212. undo_redo->commit_action();
  213. }
  214. void GradientTexture2DEditor::_set_snap_enabled(bool p_enabled) {
  215. texture_editor_rect->set_snap_enabled(p_enabled);
  216. snap_count_edit->set_visible(p_enabled);
  217. }
  218. void GradientTexture2DEditor::_set_snap_count(int p_snap_count) {
  219. texture_editor_rect->set_snap_count(p_snap_count);
  220. }
  221. void GradientTexture2DEditor::set_texture(Ref<GradientTexture2D> &p_texture) {
  222. texture = p_texture;
  223. texture_editor_rect->set_texture(p_texture);
  224. }
  225. void GradientTexture2DEditor::_notification(int p_what) {
  226. switch (p_what) {
  227. case NOTIFICATION_ENTER_TREE:
  228. case NOTIFICATION_THEME_CHANGED: {
  229. reverse_button->set_icon(get_editor_theme_icon(SNAME("ReverseGradient")));
  230. snap_button->set_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  231. } break;
  232. case NOTIFICATION_READY: {
  233. if (texture.is_valid()) {
  234. // Set snapping settings based on the texture's meta.
  235. snap_button->set_pressed(texture->get_meta("_snap_enabled", false));
  236. snap_count_edit->set_value(texture->get_meta("_snap_count", DEFAULT_SNAP));
  237. }
  238. } break;
  239. }
  240. }
  241. GradientTexture2DEditor::GradientTexture2DEditor() {
  242. HFlowContainer *toolbar = memnew(HFlowContainer);
  243. add_child(toolbar);
  244. reverse_button = memnew(Button);
  245. reverse_button->set_tooltip_text(TTR("Swap Gradient Fill Points"));
  246. toolbar->add_child(reverse_button);
  247. reverse_button->connect(SceneStringName(pressed), callable_mp(this, &GradientTexture2DEditor::_reverse_button_pressed));
  248. toolbar->add_child(memnew(VSeparator));
  249. snap_button = memnew(Button);
  250. snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
  251. snap_button->set_toggle_mode(true);
  252. toolbar->add_child(snap_button);
  253. snap_button->connect("toggled", callable_mp(this, &GradientTexture2DEditor::_set_snap_enabled));
  254. snap_count_edit = memnew(EditorSpinSlider);
  255. snap_count_edit->set_min(2);
  256. snap_count_edit->set_max(100);
  257. snap_count_edit->set_value(DEFAULT_SNAP);
  258. snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  259. toolbar->add_child(snap_count_edit);
  260. snap_count_edit->connect("value_changed", callable_mp(this, &GradientTexture2DEditor::_set_snap_count));
  261. texture_editor_rect = memnew(GradientTexture2DEdit);
  262. add_child(texture_editor_rect);
  263. set_mouse_filter(MOUSE_FILTER_STOP);
  264. _set_snap_enabled(snap_button->is_pressed());
  265. _set_snap_count(snap_count_edit->get_value());
  266. }
  267. ///////////////////////
  268. bool EditorInspectorPluginGradientTexture2D::can_handle(Object *p_object) {
  269. return Object::cast_to<GradientTexture2D>(p_object) != nullptr;
  270. }
  271. void EditorInspectorPluginGradientTexture2D::parse_begin(Object *p_object) {
  272. GradientTexture2D *texture = Object::cast_to<GradientTexture2D>(p_object);
  273. if (!texture) {
  274. return;
  275. }
  276. Ref<GradientTexture2D> t(texture);
  277. GradientTexture2DEditor *editor = memnew(GradientTexture2DEditor);
  278. editor->set_texture(t);
  279. add_custom_control(editor);
  280. }
  281. ///////////////////////
  282. GradientTexture2DEditorPlugin::GradientTexture2DEditorPlugin() {
  283. Ref<EditorInspectorPluginGradientTexture2D> plugin;
  284. plugin.instantiate();
  285. add_inspector_plugin(plugin);
  286. }