gradient_texture_2d_editor_plugin.cpp 12 KB

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