texture_region_editor_plugin.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /**************************************************************************/
  2. /* texture_region_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 "texture_region_editor_plugin.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/2d/sprite_2d.h"
  39. #include "scene/3d/sprite_3d.h"
  40. #include "scene/gui/nine_patch_rect.h"
  41. #include "scene/gui/option_button.h"
  42. #include "scene/gui/panel_container.h"
  43. #include "scene/gui/separator.h"
  44. #include "scene/gui/spin_box.h"
  45. #include "scene/gui/view_panner.h"
  46. #include "scene/resources/atlas_texture.h"
  47. #include "scene/resources/style_box_texture.h"
  48. Transform2D TextureRegionEditor::_get_offset_transform() const {
  49. Transform2D mtx;
  50. mtx.columns[2] = -draw_ofs * draw_zoom;
  51. mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
  52. return mtx;
  53. }
  54. void TextureRegionEditor::_texture_preview_draw() {
  55. const Ref<Texture2D> object_texture = _get_edited_object_texture();
  56. if (object_texture.is_null()) {
  57. return;
  58. }
  59. Transform2D mtx = _get_offset_transform();
  60. RS::get_singleton()->canvas_item_add_set_transform(texture_preview->get_canvas_item(), mtx);
  61. texture_preview->draw_rect(Rect2(Point2(), object_texture->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
  62. texture_preview->draw_texture(object_texture, Point2());
  63. RS::get_singleton()->canvas_item_add_set_transform(texture_preview->get_canvas_item(), Transform2D());
  64. }
  65. void TextureRegionEditor::_texture_overlay_draw() {
  66. const Ref<Texture2D> object_texture = _get_edited_object_texture();
  67. if (object_texture.is_null()) {
  68. return;
  69. }
  70. Transform2D mtx = _get_offset_transform();
  71. const Color color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor));
  72. if (snap_mode == SNAP_GRID) {
  73. const Color grid_color = Color(color.r, color.g, color.b, color.a * 0.15);
  74. Size2 s = texture_overlay->get_size();
  75. int last_cell = 0;
  76. if (snap_step.x != 0) {
  77. if (snap_separation.x == 0) {
  78. for (int i = 0; i < s.width; i++) {
  79. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
  80. if (i == 0) {
  81. last_cell = cell;
  82. }
  83. if (last_cell != cell) {
  84. texture_overlay->draw_line(Point2(i, 0), Point2(i, s.height), grid_color);
  85. }
  86. last_cell = cell;
  87. }
  88. } else {
  89. for (int i = 0; i < s.width + snap_separation.x; i++) {
  90. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x)));
  91. if (i == 0) {
  92. last_cell = cell;
  93. }
  94. if (last_cell != cell) {
  95. texture_overlay->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), grid_color);
  96. }
  97. last_cell = cell;
  98. }
  99. }
  100. }
  101. if (snap_step.y != 0) {
  102. if (snap_separation.y == 0) {
  103. for (int i = 0; i < s.height; i++) {
  104. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
  105. if (i == 0) {
  106. last_cell = cell;
  107. }
  108. if (last_cell != cell) {
  109. texture_overlay->draw_line(Point2(0, i), Point2(s.width, i), grid_color);
  110. }
  111. last_cell = cell;
  112. }
  113. } else {
  114. for (int i = 0; i < s.height + snap_separation.y; i++) {
  115. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y)));
  116. if (i == 0) {
  117. last_cell = cell;
  118. }
  119. if (last_cell != cell) {
  120. texture_overlay->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), grid_color);
  121. }
  122. last_cell = cell;
  123. }
  124. }
  125. }
  126. } else if (snap_mode == SNAP_AUTOSLICE) {
  127. for (const Rect2 &r : autoslice_cache) {
  128. const Vector2 endpoints[4] = {
  129. mtx.basis_xform(r.position),
  130. mtx.basis_xform(r.position + Vector2(r.size.x, 0)),
  131. mtx.basis_xform(r.position + r.size),
  132. mtx.basis_xform(r.position + Vector2(0, r.size.y))
  133. };
  134. for (int i = 0; i < 4; i++) {
  135. int next = (i + 1) % 4;
  136. texture_overlay->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, Color(0.3, 0.7, 1, 1), 2);
  137. }
  138. }
  139. }
  140. Ref<Texture2D> select_handle = get_editor_theme_icon(SNAME("EditorHandle"));
  141. Rect2 scroll_rect(Point2(), object_texture->get_size());
  142. const Vector2 raw_endpoints[4] = {
  143. rect.position,
  144. rect.position + Vector2(rect.size.x, 0),
  145. rect.position + rect.size,
  146. rect.position + Vector2(0, rect.size.y)
  147. };
  148. const Vector2 endpoints[4] = {
  149. mtx.basis_xform(raw_endpoints[0]),
  150. mtx.basis_xform(raw_endpoints[1]),
  151. mtx.basis_xform(raw_endpoints[2]),
  152. mtx.basis_xform(raw_endpoints[3])
  153. };
  154. for (int i = 0; i < 4; i++) {
  155. int prev = (i + 3) % 4;
  156. int next = (i + 1) % 4;
  157. Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
  158. ofs *= Math_SQRT2 * (select_handle->get_size().width / 2);
  159. texture_overlay->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, color, 2);
  160. if (snap_mode != SNAP_AUTOSLICE) {
  161. texture_overlay->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
  162. }
  163. ofs = (endpoints[next] - endpoints[i]) / 2;
  164. ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (select_handle->get_size().width / 2);
  165. if (snap_mode != SNAP_AUTOSLICE) {
  166. texture_overlay->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
  167. }
  168. scroll_rect.expand_to(raw_endpoints[i]);
  169. }
  170. const Size2 scroll_margin = texture_overlay->get_size() / draw_zoom;
  171. scroll_rect.position -= scroll_margin;
  172. scroll_rect.size += scroll_margin * 2;
  173. updating_scroll = true;
  174. hscroll->set_min(scroll_rect.position.x);
  175. hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x);
  176. if (ABS(scroll_rect.position.x - (scroll_rect.position.x + scroll_rect.size.x)) <= scroll_margin.x) {
  177. hscroll->hide();
  178. } else {
  179. hscroll->show();
  180. hscroll->set_page(scroll_margin.x);
  181. hscroll->set_value(draw_ofs.x);
  182. }
  183. vscroll->set_min(scroll_rect.position.y);
  184. vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y);
  185. if (ABS(scroll_rect.position.y - (scroll_rect.position.y + scroll_rect.size.y)) <= scroll_margin.y) {
  186. vscroll->hide();
  187. draw_ofs.y = scroll_rect.position.y;
  188. } else {
  189. vscroll->show();
  190. vscroll->set_page(scroll_margin.y);
  191. vscroll->set_value(draw_ofs.y);
  192. }
  193. Size2 hmin = hscroll->get_combined_minimum_size();
  194. Size2 vmin = vscroll->get_combined_minimum_size();
  195. // Avoid scrollbar overlapping.
  196. hscroll->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, vscroll->is_visible() ? -vmin.width : 0);
  197. vscroll->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, hscroll->is_visible() ? -hmin.height : 0);
  198. updating_scroll = false;
  199. if (request_center && hscroll->get_min() < 0) {
  200. hscroll->set_value((hscroll->get_min() + hscroll->get_max() - hscroll->get_page()) / 2);
  201. vscroll->set_value((vscroll->get_min() + vscroll->get_max() - vscroll->get_page()) / 2);
  202. // This ensures that the view is updated correctly.
  203. callable_mp(this, &TextureRegionEditor::_pan_callback).call_deferred(Vector2(1, 0), Ref<InputEvent>());
  204. callable_mp(this, &TextureRegionEditor::_scroll_changed).call_deferred(0.0);
  205. request_center = false;
  206. }
  207. if (node_ninepatch || res_stylebox.is_valid()) {
  208. float margins[4] = { 0 };
  209. if (node_ninepatch) {
  210. margins[0] = node_ninepatch->get_patch_margin(SIDE_TOP);
  211. margins[1] = node_ninepatch->get_patch_margin(SIDE_BOTTOM);
  212. margins[2] = node_ninepatch->get_patch_margin(SIDE_LEFT);
  213. margins[3] = node_ninepatch->get_patch_margin(SIDE_RIGHT);
  214. } else if (res_stylebox.is_valid()) {
  215. margins[0] = res_stylebox->get_texture_margin(SIDE_TOP);
  216. margins[1] = res_stylebox->get_texture_margin(SIDE_BOTTOM);
  217. margins[2] = res_stylebox->get_texture_margin(SIDE_LEFT);
  218. margins[3] = res_stylebox->get_texture_margin(SIDE_RIGHT);
  219. }
  220. Vector2 pos[4] = {
  221. mtx.basis_xform(Vector2(0, margins[0])) + Vector2(0, endpoints[0].y - draw_ofs.y * draw_zoom),
  222. -mtx.basis_xform(Vector2(0, margins[1])) + Vector2(0, endpoints[2].y - draw_ofs.y * draw_zoom),
  223. mtx.basis_xform(Vector2(margins[2], 0)) + Vector2(endpoints[0].x - draw_ofs.x * draw_zoom, 0),
  224. -mtx.basis_xform(Vector2(margins[3], 0)) + Vector2(endpoints[2].x - draw_ofs.x * draw_zoom, 0)
  225. };
  226. _draw_margin_line(pos[0], pos[0] + Vector2(texture_overlay->get_size().x, 0));
  227. _draw_margin_line(pos[1], pos[1] + Vector2(texture_overlay->get_size().x, 0));
  228. _draw_margin_line(pos[2], pos[2] + Vector2(0, texture_overlay->get_size().y));
  229. _draw_margin_line(pos[3], pos[3] + Vector2(0, texture_overlay->get_size().y));
  230. }
  231. }
  232. void TextureRegionEditor::_draw_margin_line(Vector2 p_from, Vector2 p_to) {
  233. // Margin line is a dashed line with a normalized dash length. This method works
  234. // for both vertical and horizontal lines.
  235. Vector2 dash_size = (p_to - p_from).normalized() * 10;
  236. const int dash_thickness = Math::round(2 * EDSCALE);
  237. const Color dash_color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor));
  238. const Color dash_bg_color = dash_color.inverted() * Color(1, 1, 1, 0.5);
  239. const int line_threshold = 200;
  240. // Draw a translucent background line to make the foreground line visible on any background.
  241. texture_overlay->draw_line(p_from, p_to, dash_bg_color, dash_thickness);
  242. Vector2 dash_start = p_from;
  243. while (dash_start.distance_squared_to(p_to) > line_threshold) {
  244. texture_overlay->draw_line(dash_start, dash_start + dash_size, dash_color, dash_thickness);
  245. // Skip two size lengths, one for the drawn dash and one for the gap.
  246. dash_start += dash_size * 2;
  247. }
  248. }
  249. void TextureRegionEditor::_set_grid_parameters_clamping(bool p_enabled) {
  250. sb_off_x->set_allow_lesser(!p_enabled);
  251. sb_off_x->set_allow_greater(!p_enabled);
  252. sb_off_y->set_allow_lesser(!p_enabled);
  253. sb_off_y->set_allow_greater(!p_enabled);
  254. sb_step_x->set_allow_greater(!p_enabled);
  255. sb_step_y->set_allow_greater(!p_enabled);
  256. sb_sep_x->set_allow_greater(!p_enabled);
  257. sb_sep_y->set_allow_greater(!p_enabled);
  258. }
  259. void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input) {
  260. if (panner->gui_input(p_input)) {
  261. return;
  262. }
  263. Transform2D mtx;
  264. mtx.columns[2] = -draw_ofs * draw_zoom;
  265. mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
  266. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  267. Ref<InputEventMouseButton> mb = p_input;
  268. if (mb.is_valid()) {
  269. if (mb->get_button_index() == MouseButton::LEFT) {
  270. if (mb->is_pressed() && !panner->is_panning()) {
  271. // Check if we click on any handle first.
  272. {
  273. const real_t handle_radius = 16 * EDSCALE;
  274. const real_t handle_offset = 8 * EDSCALE;
  275. // Position of selection handles.
  276. const Vector2 endpoints[8] = {
  277. mtx.xform(rect.position) + Vector2(-handle_offset, -handle_offset),
  278. mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -handle_offset),
  279. mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(handle_offset, -handle_offset),
  280. mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(handle_offset, 0),
  281. mtx.xform(rect.position + rect.size) + Vector2(handle_offset, handle_offset),
  282. mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, handle_offset),
  283. mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-handle_offset, handle_offset),
  284. mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-handle_offset, 0)
  285. };
  286. drag_from = mtx.affine_inverse().xform(mb->get_position());
  287. if (snap_mode == SNAP_PIXEL) {
  288. drag_from = drag_from.snappedf(1);
  289. } else if (snap_mode == SNAP_GRID) {
  290. drag_from = snap_point(drag_from);
  291. }
  292. drag = true;
  293. rect_prev = _get_edited_object_region();
  294. for (int i = 0; i < 8; i++) {
  295. Vector2 tuv = endpoints[i];
  296. if (tuv.distance_to(mb->get_position()) < handle_radius) {
  297. drag_index = i;
  298. }
  299. }
  300. }
  301. // We didn't hit any handle, try other options.
  302. if (drag_index < 0) {
  303. if (node_ninepatch || res_stylebox.is_valid()) {
  304. // For ninepatchable objects check if we are clicking on margin bars.
  305. edited_margin = -1;
  306. float margins[4] = { 0 };
  307. if (node_ninepatch) {
  308. margins[0] = node_ninepatch->get_patch_margin(SIDE_TOP);
  309. margins[1] = node_ninepatch->get_patch_margin(SIDE_BOTTOM);
  310. margins[2] = node_ninepatch->get_patch_margin(SIDE_LEFT);
  311. margins[3] = node_ninepatch->get_patch_margin(SIDE_RIGHT);
  312. } else if (res_stylebox.is_valid()) {
  313. margins[0] = res_stylebox->get_texture_margin(SIDE_TOP);
  314. margins[1] = res_stylebox->get_texture_margin(SIDE_BOTTOM);
  315. margins[2] = res_stylebox->get_texture_margin(SIDE_LEFT);
  316. margins[3] = res_stylebox->get_texture_margin(SIDE_RIGHT);
  317. }
  318. Vector2 pos[4] = {
  319. mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs * draw_zoom,
  320. mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs * draw_zoom,
  321. mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs * draw_zoom,
  322. mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs * draw_zoom
  323. };
  324. if (Math::abs(mb->get_position().y - pos[0].y) < 8) {
  325. edited_margin = 0;
  326. prev_margin = margins[0];
  327. } else if (Math::abs(mb->get_position().y - pos[1].y) < 8) {
  328. edited_margin = 1;
  329. prev_margin = margins[1];
  330. } else if (Math::abs(mb->get_position().x - pos[2].x) < 8) {
  331. edited_margin = 2;
  332. prev_margin = margins[2];
  333. } else if (Math::abs(mb->get_position().x - pos[3].x) < 8) {
  334. edited_margin = 3;
  335. prev_margin = margins[3];
  336. }
  337. if (edited_margin >= 0) {
  338. drag_from = mb->get_position();
  339. drag = true;
  340. }
  341. }
  342. if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) {
  343. // We didn't hit anything, but we're in the autoslice mode. Handle it.
  344. Vector2 point = mtx.affine_inverse().xform(mb->get_position());
  345. for (const Rect2 &E : autoslice_cache) {
  346. if (E.has_point(point)) {
  347. rect = E;
  348. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !(Input::get_singleton()->is_key_pressed(Key(Key::SHIFT | Key::ALT)))) {
  349. Rect2 r;
  350. if (node_sprite_2d) {
  351. r = node_sprite_2d->get_region_rect();
  352. } else if (node_sprite_3d) {
  353. r = node_sprite_3d->get_region_rect();
  354. } else if (node_ninepatch) {
  355. r = node_ninepatch->get_region_rect();
  356. } else if (res_stylebox.is_valid()) {
  357. r = res_stylebox->get_region_rect();
  358. } else if (res_atlas_texture.is_valid()) {
  359. r = res_atlas_texture->get_region();
  360. }
  361. rect.expand_to(r.position);
  362. rect.expand_to(r.get_end());
  363. }
  364. undo_redo->create_action(TTR("Set Region Rect"));
  365. if (node_sprite_2d) {
  366. undo_redo->add_do_method(node_sprite_2d, "set_region_rect", rect);
  367. undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect());
  368. } else if (node_sprite_3d) {
  369. undo_redo->add_do_method(node_sprite_3d, "set_region_rect", rect);
  370. undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
  371. } else if (node_ninepatch) {
  372. undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect);
  373. undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
  374. } else if (res_stylebox.is_valid()) {
  375. undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", rect);
  376. undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect());
  377. } else if (res_atlas_texture.is_valid()) {
  378. undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", rect);
  379. undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
  380. }
  381. undo_redo->add_do_method(this, "_update_rect");
  382. undo_redo->add_undo_method(this, "_update_rect");
  383. undo_redo->add_do_method(texture_overlay, "queue_redraw");
  384. undo_redo->add_undo_method(texture_overlay, "queue_redraw");
  385. undo_redo->commit_action();
  386. break;
  387. }
  388. }
  389. } else if (edited_margin < 0) {
  390. // We didn't hit anything and it's not autoslice, which means we try to create a new region.
  391. if (drag_index == -1) {
  392. creating = true;
  393. rect = Rect2(drag_from, Size2());
  394. }
  395. }
  396. }
  397. } else if (!mb->is_pressed() && drag) {
  398. if (edited_margin >= 0) {
  399. undo_redo->create_action(TTR("Set Margin"));
  400. static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
  401. if (node_ninepatch) {
  402. undo_redo->add_do_method(node_ninepatch, "set_patch_margin", side[edited_margin], node_ninepatch->get_patch_margin(side[edited_margin]));
  403. undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", side[edited_margin], prev_margin);
  404. } else if (res_stylebox.is_valid()) {
  405. undo_redo->add_do_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], res_stylebox->get_texture_margin(side[edited_margin]));
  406. undo_redo->add_undo_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], prev_margin);
  407. res_stylebox->emit_changed();
  408. }
  409. edited_margin = -1;
  410. } else {
  411. undo_redo->create_action(TTR("Set Region Rect"));
  412. if (node_sprite_2d) {
  413. undo_redo->add_do_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect());
  414. undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", rect_prev);
  415. } else if (node_sprite_3d) {
  416. undo_redo->add_do_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
  417. undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", rect_prev);
  418. } else if (node_ninepatch) {
  419. undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
  420. undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev);
  421. } else if (res_stylebox.is_valid()) {
  422. undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect());
  423. undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", rect_prev);
  424. } else if (res_atlas_texture.is_valid()) {
  425. undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
  426. undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", rect_prev);
  427. }
  428. drag_index = -1;
  429. }
  430. undo_redo->add_do_method(this, "_update_rect");
  431. undo_redo->add_undo_method(this, "_update_rect");
  432. undo_redo->add_do_method(texture_overlay, "queue_redraw");
  433. undo_redo->add_undo_method(texture_overlay, "queue_redraw");
  434. undo_redo->commit_action();
  435. drag = false;
  436. creating = false;
  437. }
  438. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  439. if (drag) {
  440. drag = false;
  441. if (edited_margin >= 0) {
  442. static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
  443. if (node_ninepatch) {
  444. node_ninepatch->set_patch_margin(side[edited_margin], prev_margin);
  445. }
  446. if (res_stylebox.is_valid()) {
  447. res_stylebox->set_texture_margin(side[edited_margin], prev_margin);
  448. }
  449. edited_margin = -1;
  450. } else {
  451. _apply_rect(rect_prev);
  452. rect = rect_prev;
  453. texture_preview->queue_redraw();
  454. texture_overlay->queue_redraw();
  455. drag_index = -1;
  456. }
  457. }
  458. }
  459. }
  460. Ref<InputEventMouseMotion> mm = p_input;
  461. if (mm.is_valid()) {
  462. if (drag) {
  463. if (edited_margin >= 0) {
  464. float new_margin = 0;
  465. if (snap_mode != SNAP_GRID) {
  466. if (edited_margin == 0) {
  467. new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom;
  468. } else if (edited_margin == 1) {
  469. new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom;
  470. } else if (edited_margin == 2) {
  471. new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom;
  472. } else if (edited_margin == 3) {
  473. new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom;
  474. } else {
  475. ERR_PRINT("Unexpected edited_margin");
  476. }
  477. if (snap_mode == SNAP_PIXEL) {
  478. new_margin = Math::round(new_margin);
  479. }
  480. } else {
  481. Vector2 pos_snapped = snap_point(mtx.affine_inverse().xform(mm->get_position()));
  482. Rect2 rect_rounded = Rect2(rect.position.round(), rect.size.round());
  483. if (edited_margin == 0) {
  484. new_margin = pos_snapped.y - rect_rounded.position.y;
  485. } else if (edited_margin == 1) {
  486. new_margin = rect_rounded.size.y + rect_rounded.position.y - pos_snapped.y;
  487. } else if (edited_margin == 2) {
  488. new_margin = pos_snapped.x - rect_rounded.position.x;
  489. } else if (edited_margin == 3) {
  490. new_margin = rect_rounded.size.x + rect_rounded.position.x - pos_snapped.x;
  491. } else {
  492. ERR_PRINT("Unexpected edited_margin");
  493. }
  494. }
  495. if (new_margin < 0) {
  496. new_margin = 0;
  497. }
  498. static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
  499. if (node_ninepatch) {
  500. node_ninepatch->set_patch_margin(side[edited_margin], new_margin);
  501. }
  502. if (res_stylebox.is_valid()) {
  503. res_stylebox->set_texture_margin(side[edited_margin], new_margin);
  504. }
  505. } else {
  506. Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position());
  507. if (snap_mode == SNAP_PIXEL) {
  508. new_pos = new_pos.snappedf(1);
  509. } else if (snap_mode == SNAP_GRID) {
  510. new_pos = snap_point(new_pos);
  511. }
  512. if (creating) {
  513. rect = Rect2(drag_from, Size2());
  514. rect.expand_to(new_pos);
  515. _apply_rect(rect);
  516. texture_preview->queue_redraw();
  517. texture_overlay->queue_redraw();
  518. return;
  519. }
  520. switch (drag_index) {
  521. case 0: {
  522. Vector2 p = rect_prev.get_end();
  523. rect = Rect2(p, Size2());
  524. rect.expand_to(new_pos);
  525. _apply_rect(rect);
  526. } break;
  527. case 1: {
  528. Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
  529. rect = Rect2(p, Size2(rect_prev.size.x, 0));
  530. rect.expand_to(new_pos);
  531. _apply_rect(rect);
  532. } break;
  533. case 2: {
  534. Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
  535. rect = Rect2(p, Size2());
  536. rect.expand_to(new_pos);
  537. _apply_rect(rect);
  538. } break;
  539. case 3: {
  540. Vector2 p = rect_prev.position;
  541. rect = Rect2(p, Size2(0, rect_prev.size.y));
  542. rect.expand_to(new_pos);
  543. _apply_rect(rect);
  544. } break;
  545. case 4: {
  546. Vector2 p = rect_prev.position;
  547. rect = Rect2(p, Size2());
  548. rect.expand_to(new_pos);
  549. _apply_rect(rect);
  550. } break;
  551. case 5: {
  552. Vector2 p = rect_prev.position;
  553. rect = Rect2(p, Size2(rect_prev.size.x, 0));
  554. rect.expand_to(new_pos);
  555. _apply_rect(rect);
  556. } break;
  557. case 6: {
  558. Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
  559. rect = Rect2(p, Size2());
  560. rect.expand_to(new_pos);
  561. _apply_rect(rect);
  562. } break;
  563. case 7: {
  564. Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
  565. rect = Rect2(p, Size2(0, rect_prev.size.y));
  566. rect.expand_to(new_pos);
  567. _apply_rect(rect);
  568. } break;
  569. }
  570. }
  571. texture_preview->queue_redraw();
  572. texture_overlay->queue_redraw();
  573. }
  574. }
  575. Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
  576. if (magnify_gesture.is_valid()) {
  577. _zoom_on_position(draw_zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  578. }
  579. Ref<InputEventPanGesture> pan_gesture = p_input;
  580. if (pan_gesture.is_valid()) {
  581. hscroll->set_value(hscroll->get_value() + hscroll->get_page() * pan_gesture->get_delta().x / 8);
  582. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y / 8);
  583. }
  584. }
  585. void TextureRegionEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  586. p_scroll_vec /= draw_zoom;
  587. hscroll->set_value(hscroll->get_value() - p_scroll_vec.x);
  588. vscroll->set_value(vscroll->get_value() - p_scroll_vec.y);
  589. }
  590. void TextureRegionEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  591. _zoom_on_position(draw_zoom * p_zoom_factor, p_origin);
  592. }
  593. void TextureRegionEditor::_scroll_changed(float) {
  594. if (updating_scroll) {
  595. return;
  596. }
  597. draw_ofs.x = hscroll->get_value();
  598. draw_ofs.y = vscroll->get_value();
  599. texture_preview->queue_redraw();
  600. texture_overlay->queue_redraw();
  601. }
  602. void TextureRegionEditor::_set_snap_mode(int p_mode) {
  603. snap_mode = (SnapMode)p_mode;
  604. hb_grid->set_visible(snap_mode == SNAP_GRID);
  605. if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
  606. _update_autoslice();
  607. }
  608. texture_overlay->queue_redraw();
  609. }
  610. void TextureRegionEditor::_set_snap_off_x(float p_val) {
  611. snap_offset.x = p_val;
  612. texture_overlay->queue_redraw();
  613. }
  614. void TextureRegionEditor::_set_snap_off_y(float p_val) {
  615. snap_offset.y = p_val;
  616. texture_overlay->queue_redraw();
  617. }
  618. void TextureRegionEditor::_set_snap_step_x(float p_val) {
  619. snap_step.x = p_val;
  620. texture_overlay->queue_redraw();
  621. }
  622. void TextureRegionEditor::_set_snap_step_y(float p_val) {
  623. snap_step.y = p_val;
  624. texture_overlay->queue_redraw();
  625. }
  626. void TextureRegionEditor::_set_snap_sep_x(float p_val) {
  627. snap_separation.x = p_val;
  628. texture_overlay->queue_redraw();
  629. }
  630. void TextureRegionEditor::_set_snap_sep_y(float p_val) {
  631. snap_separation.y = p_val;
  632. texture_overlay->queue_redraw();
  633. }
  634. void TextureRegionEditor::_zoom_on_position(float p_zoom, Point2 p_position) {
  635. if (p_zoom < min_draw_zoom || p_zoom > max_draw_zoom) {
  636. return;
  637. }
  638. float prev_zoom = draw_zoom;
  639. draw_zoom = p_zoom;
  640. Point2 ofs = p_position;
  641. ofs = ofs / prev_zoom - ofs / draw_zoom;
  642. draw_ofs = (draw_ofs + ofs).round();
  643. texture_preview->queue_redraw();
  644. texture_overlay->queue_redraw();
  645. }
  646. void TextureRegionEditor::_zoom_in() {
  647. _zoom_on_position(draw_zoom * 1.5, texture_overlay->get_size() / 2.0);
  648. }
  649. void TextureRegionEditor::_zoom_reset() {
  650. _zoom_on_position(1.0, texture_overlay->get_size() / 2.0);
  651. }
  652. void TextureRegionEditor::_zoom_out() {
  653. _zoom_on_position(draw_zoom / 1.5, texture_overlay->get_size() / 2.0);
  654. }
  655. void TextureRegionEditor::_apply_rect(const Rect2 &p_rect) {
  656. if (node_sprite_2d) {
  657. node_sprite_2d->set_region_rect(p_rect);
  658. } else if (node_sprite_3d) {
  659. node_sprite_3d->set_region_rect(p_rect);
  660. } else if (node_ninepatch) {
  661. node_ninepatch->set_region_rect(p_rect);
  662. } else if (res_stylebox.is_valid()) {
  663. res_stylebox->set_region_rect(p_rect);
  664. } else if (res_atlas_texture.is_valid()) {
  665. res_atlas_texture->set_region(p_rect);
  666. }
  667. }
  668. void TextureRegionEditor::_update_rect() {
  669. rect = _get_edited_object_region();
  670. }
  671. void TextureRegionEditor::_update_autoslice() {
  672. autoslice_is_dirty = false;
  673. autoslice_cache.clear();
  674. const Ref<Texture2D> object_texture = _get_edited_object_texture();
  675. if (object_texture.is_null()) {
  676. return;
  677. }
  678. for (int y = 0; y < object_texture->get_height(); y++) {
  679. for (int x = 0; x < object_texture->get_width(); x++) {
  680. if (object_texture->is_pixel_opaque(x, y)) {
  681. bool found = false;
  682. for (Rect2 &E : autoslice_cache) {
  683. Rect2 grown = E.grow(1.5);
  684. if (grown.has_point(Point2(x, y))) {
  685. E.expand_to(Point2(x, y));
  686. E.expand_to(Point2(x + 1, y + 1));
  687. x = E.position.x + E.size.x - 1;
  688. bool merged = true;
  689. while (merged) {
  690. merged = false;
  691. bool queue_erase = false;
  692. for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) {
  693. if (queue_erase) {
  694. autoslice_cache.erase(F->prev());
  695. queue_erase = false;
  696. }
  697. if (F->get() == E) {
  698. continue;
  699. }
  700. if (E.grow(1).intersects(F->get())) {
  701. E.expand_to(F->get().position);
  702. E.expand_to(F->get().position + F->get().size);
  703. if (F->prev()) {
  704. F = F->prev();
  705. autoslice_cache.erase(F->next());
  706. } else {
  707. queue_erase = true;
  708. // Can't delete the first rect in the list.
  709. }
  710. merged = true;
  711. }
  712. }
  713. }
  714. found = true;
  715. break;
  716. }
  717. }
  718. if (!found) {
  719. Rect2 new_rect(x, y, 1, 1);
  720. autoslice_cache.push_back(new_rect);
  721. }
  722. }
  723. }
  724. }
  725. cache_map[object_texture->get_rid()] = autoslice_cache;
  726. }
  727. void TextureRegionEditor::_notification(int p_what) {
  728. switch (p_what) {
  729. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  730. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  731. break;
  732. }
  733. [[fallthrough]];
  734. }
  735. case NOTIFICATION_READY: {
  736. panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
  737. } break;
  738. case NOTIFICATION_ENTER_TREE: {
  739. get_tree()->connect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed));
  740. hb_grid->set_visible(snap_mode == SNAP_GRID);
  741. if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
  742. _update_autoslice();
  743. }
  744. } break;
  745. case NOTIFICATION_EXIT_TREE: {
  746. get_tree()->disconnect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed));
  747. } break;
  748. case NOTIFICATION_THEME_CHANGED: {
  749. texture_preview->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewBG"), EditorStringName(EditorStyles)));
  750. texture_overlay->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewFG"), EditorStringName(EditorStyles)));
  751. zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  752. zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  753. zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  754. } break;
  755. case NOTIFICATION_VISIBILITY_CHANGED: {
  756. if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
  757. _update_autoslice();
  758. }
  759. if (!is_visible()) {
  760. EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_offset", snap_offset);
  761. EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_step", snap_step);
  762. EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_separation", snap_separation);
  763. EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_mode", snap_mode);
  764. }
  765. } break;
  766. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  767. // This happens when the user leaves the Editor and returns,
  768. // they could have changed the textures, so the cache is cleared.
  769. cache_map.clear();
  770. _edit_region();
  771. } break;
  772. }
  773. }
  774. void TextureRegionEditor::_node_removed(Node *p_node) {
  775. if (p_node == node_sprite_2d || p_node == node_sprite_3d || p_node == node_ninepatch) {
  776. _clear_edited_object();
  777. hide();
  778. }
  779. }
  780. void TextureRegionEditor::_clear_edited_object() {
  781. if (node_sprite_2d) {
  782. node_sprite_2d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
  783. }
  784. if (node_sprite_3d) {
  785. node_sprite_3d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
  786. }
  787. if (node_ninepatch) {
  788. node_ninepatch->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
  789. }
  790. if (res_stylebox.is_valid()) {
  791. res_stylebox->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
  792. }
  793. if (res_atlas_texture.is_valid()) {
  794. res_atlas_texture->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
  795. }
  796. node_sprite_2d = nullptr;
  797. node_sprite_3d = nullptr;
  798. node_ninepatch = nullptr;
  799. res_stylebox = Ref<StyleBoxTexture>();
  800. res_atlas_texture = Ref<AtlasTexture>();
  801. }
  802. void TextureRegionEditor::edit(Object *p_obj) {
  803. _clear_edited_object();
  804. if (p_obj) {
  805. node_sprite_2d = Object::cast_to<Sprite2D>(p_obj);
  806. node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
  807. node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
  808. bool is_resource = false;
  809. if (Object::cast_to<StyleBoxTexture>(p_obj)) {
  810. res_stylebox = Ref<StyleBoxTexture>(p_obj);
  811. is_resource = true;
  812. }
  813. if (Object::cast_to<AtlasTexture>(p_obj)) {
  814. res_atlas_texture = Ref<AtlasTexture>(p_obj);
  815. is_resource = true;
  816. }
  817. if (is_resource) {
  818. Object::cast_to<Resource>(p_obj)->connect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
  819. } else {
  820. p_obj->connect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
  821. }
  822. _edit_region();
  823. }
  824. texture_preview->queue_redraw();
  825. texture_overlay->queue_redraw();
  826. popup_centered_ratio(0.5);
  827. request_center = true;
  828. }
  829. Ref<Texture2D> TextureRegionEditor::_get_edited_object_texture() const {
  830. if (node_sprite_2d) {
  831. return node_sprite_2d->get_texture();
  832. }
  833. if (node_sprite_3d) {
  834. return node_sprite_3d->get_texture();
  835. }
  836. if (node_ninepatch) {
  837. return node_ninepatch->get_texture();
  838. }
  839. if (res_stylebox.is_valid()) {
  840. return res_stylebox->get_texture();
  841. }
  842. if (res_atlas_texture.is_valid()) {
  843. return res_atlas_texture->get_atlas();
  844. }
  845. return Ref<Texture2D>();
  846. }
  847. Rect2 TextureRegionEditor::_get_edited_object_region() const {
  848. Rect2 region;
  849. if (node_sprite_2d) {
  850. region = node_sprite_2d->get_region_rect();
  851. } else if (node_sprite_3d) {
  852. region = node_sprite_3d->get_region_rect();
  853. } else if (node_ninepatch) {
  854. region = node_ninepatch->get_region_rect();
  855. } else if (res_stylebox.is_valid()) {
  856. region = res_stylebox->get_region_rect();
  857. } else if (res_atlas_texture.is_valid()) {
  858. region = res_atlas_texture->get_region();
  859. }
  860. const Ref<Texture2D> object_texture = _get_edited_object_texture();
  861. if (region == Rect2() && object_texture.is_valid()) {
  862. region = Rect2(Vector2(), object_texture->get_size());
  863. }
  864. return region;
  865. }
  866. void TextureRegionEditor::_texture_changed() {
  867. if (!is_visible()) {
  868. return;
  869. }
  870. _edit_region();
  871. }
  872. void TextureRegionEditor::_edit_region() {
  873. const Ref<Texture2D> object_texture = _get_edited_object_texture();
  874. if (object_texture.is_null()) {
  875. _set_grid_parameters_clamping(false);
  876. _zoom_reset();
  877. hscroll->hide();
  878. vscroll->hide();
  879. texture_preview->queue_redraw();
  880. texture_overlay->queue_redraw();
  881. return;
  882. }
  883. CanvasItem::TextureFilter filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
  884. if (node_sprite_2d) {
  885. filter = node_sprite_2d->get_texture_filter_in_tree();
  886. } else if (node_sprite_3d) {
  887. StandardMaterial3D::TextureFilter filter_3d = node_sprite_3d->get_texture_filter();
  888. switch (filter_3d) {
  889. case StandardMaterial3D::TEXTURE_FILTER_NEAREST:
  890. filter = CanvasItem::TEXTURE_FILTER_NEAREST;
  891. break;
  892. case StandardMaterial3D::TEXTURE_FILTER_LINEAR:
  893. filter = CanvasItem::TEXTURE_FILTER_LINEAR;
  894. break;
  895. case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
  896. filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
  897. break;
  898. case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
  899. filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  900. break;
  901. case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
  902. filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC;
  903. break;
  904. case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
  905. filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC;
  906. break;
  907. default:
  908. // fallback to project default
  909. filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE;
  910. break;
  911. }
  912. } else if (node_ninepatch) {
  913. filter = node_ninepatch->get_texture_filter_in_tree();
  914. }
  915. // occurs when get_texture_filter_in_tree reaches the scene root
  916. if (filter == CanvasItem::TEXTURE_FILTER_PARENT_NODE) {
  917. SubViewport *root = EditorNode::get_singleton()->get_scene_root();
  918. if (root != nullptr) {
  919. Viewport::DefaultCanvasItemTextureFilter filter_default = root->get_default_canvas_item_texture_filter();
  920. // depending on default filter, set filter to match, otherwise fall back on nearest w/ mipmaps
  921. switch (filter_default) {
  922. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
  923. filter = CanvasItem::TEXTURE_FILTER_NEAREST;
  924. break;
  925. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
  926. filter = CanvasItem::TEXTURE_FILTER_LINEAR;
  927. break;
  928. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
  929. filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  930. break;
  931. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
  932. default:
  933. filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
  934. break;
  935. }
  936. } else {
  937. filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
  938. }
  939. }
  940. texture_preview->set_texture_filter(filter);
  941. texture_preview->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_DISABLED);
  942. if (cache_map.has(object_texture->get_rid())) {
  943. autoslice_cache = cache_map[object_texture->get_rid()];
  944. autoslice_is_dirty = false;
  945. } else {
  946. if (is_visible() && snap_mode == SNAP_AUTOSLICE) {
  947. _update_autoslice();
  948. } else {
  949. autoslice_is_dirty = true;
  950. }
  951. }
  952. // Avoiding clamping with mismatched min/max.
  953. _set_grid_parameters_clamping(false);
  954. const Size2 tex_size = object_texture->get_size();
  955. sb_off_x->set_min(-tex_size.x);
  956. sb_off_x->set_max(tex_size.x);
  957. sb_off_y->set_min(-tex_size.y);
  958. sb_off_y->set_max(tex_size.y);
  959. sb_step_x->set_max(tex_size.x);
  960. sb_step_y->set_max(tex_size.y);
  961. sb_sep_x->set_max(tex_size.x);
  962. sb_sep_y->set_max(tex_size.y);
  963. _set_grid_parameters_clamping(true);
  964. sb_off_x->set_value(snap_offset.x);
  965. sb_off_y->set_value(snap_offset.y);
  966. sb_step_x->set_value(snap_step.x);
  967. sb_step_y->set_value(snap_step.y);
  968. sb_sep_x->set_value(snap_separation.x);
  969. sb_sep_y->set_value(snap_separation.y);
  970. _update_rect();
  971. texture_preview->queue_redraw();
  972. texture_overlay->queue_redraw();
  973. }
  974. Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
  975. if (snap_mode == SNAP_GRID) {
  976. p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x);
  977. p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y);
  978. }
  979. return p_target;
  980. }
  981. void TextureRegionEditor::_bind_methods() {
  982. ClassDB::bind_method(D_METHOD("_update_rect"), &TextureRegionEditor::_update_rect);
  983. }
  984. TextureRegionEditor::TextureRegionEditor() {
  985. set_title(TTR("Region Editor"));
  986. set_ok_button_text(TTR("Close"));
  987. // A power-of-two value works better as a default grid size.
  988. snap_offset = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_offset", Vector2());
  989. snap_step = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_step", Vector2(8, 8));
  990. snap_separation = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_separation", Vector2());
  991. snap_mode = (SnapMode)(int)EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_mode", SNAP_NONE);
  992. panner.instantiate();
  993. panner->set_callbacks(callable_mp(this, &TextureRegionEditor::_pan_callback), callable_mp(this, &TextureRegionEditor::_zoom_callback));
  994. VBoxContainer *vb = memnew(VBoxContainer);
  995. add_child(vb);
  996. HBoxContainer *hb_tools = memnew(HBoxContainer);
  997. vb->add_child(hb_tools);
  998. hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
  999. snap_mode_button = memnew(OptionButton);
  1000. hb_tools->add_child(snap_mode_button);
  1001. snap_mode_button->add_item(TTR("None"), 0);
  1002. snap_mode_button->add_item(TTR("Pixel Snap"), 1);
  1003. snap_mode_button->add_item(TTR("Grid Snap"), 2);
  1004. snap_mode_button->add_item(TTR("Auto Slice"), 3);
  1005. snap_mode_button->select(snap_mode);
  1006. snap_mode_button->connect(SceneStringName(item_selected), callable_mp(this, &TextureRegionEditor::_set_snap_mode));
  1007. hb_grid = memnew(HBoxContainer);
  1008. hb_tools->add_child(hb_grid);
  1009. hb_grid->add_child(memnew(VSeparator));
  1010. hb_grid->add_child(memnew(Label(TTR("Offset:"))));
  1011. sb_off_x = memnew(SpinBox);
  1012. sb_off_x->set_step(1);
  1013. sb_off_x->set_suffix("px");
  1014. sb_off_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
  1015. hb_grid->add_child(sb_off_x);
  1016. sb_off_y = memnew(SpinBox);
  1017. sb_off_y->set_step(1);
  1018. sb_off_y->set_suffix("px");
  1019. sb_off_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
  1020. hb_grid->add_child(sb_off_y);
  1021. hb_grid->add_child(memnew(VSeparator));
  1022. hb_grid->add_child(memnew(Label(TTR("Step:"))));
  1023. sb_step_x = memnew(SpinBox);
  1024. sb_step_x->set_min(0);
  1025. sb_step_x->set_step(1);
  1026. sb_step_x->set_suffix("px");
  1027. sb_step_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
  1028. hb_grid->add_child(sb_step_x);
  1029. sb_step_y = memnew(SpinBox);
  1030. sb_step_y->set_min(0);
  1031. sb_step_y->set_step(1);
  1032. sb_step_y->set_suffix("px");
  1033. sb_step_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
  1034. hb_grid->add_child(sb_step_y);
  1035. hb_grid->add_child(memnew(VSeparator));
  1036. hb_grid->add_child(memnew(Label(TTR("Separation:"))));
  1037. sb_sep_x = memnew(SpinBox);
  1038. sb_sep_x->set_min(0);
  1039. sb_sep_x->set_step(1);
  1040. sb_sep_x->set_suffix("px");
  1041. sb_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
  1042. hb_grid->add_child(sb_sep_x);
  1043. sb_sep_y = memnew(SpinBox);
  1044. sb_sep_y->set_min(0);
  1045. sb_sep_y->set_step(1);
  1046. sb_sep_y->set_suffix("px");
  1047. sb_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
  1048. hb_grid->add_child(sb_sep_y);
  1049. hb_grid->hide();
  1050. // Restore grid snap parameters.
  1051. _set_grid_parameters_clamping(false);
  1052. sb_off_x->set_value(snap_offset.x);
  1053. sb_off_y->set_value(snap_offset.y);
  1054. sb_step_x->set_value(snap_step.x);
  1055. sb_step_y->set_value(snap_step.y);
  1056. sb_sep_x->set_value(snap_separation.x);
  1057. sb_sep_y->set_value(snap_separation.y);
  1058. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  1059. draw_zoom = MAX(1.0f, EDSCALE);
  1060. max_draw_zoom = 128.0f * MAX(1.0f, EDSCALE);
  1061. min_draw_zoom = 0.01f * MAX(1.0f, EDSCALE);
  1062. texture_preview = memnew(PanelContainer);
  1063. vb->add_child(texture_preview);
  1064. texture_preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1065. texture_preview->set_clip_contents(true);
  1066. texture_preview->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_preview_draw));
  1067. texture_overlay = memnew(Panel);
  1068. texture_preview->add_child(texture_overlay);
  1069. texture_overlay->set_focus_mode(Control::FOCUS_CLICK);
  1070. texture_overlay->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_overlay_draw));
  1071. texture_overlay->connect(SceneStringName(gui_input), callable_mp(this, &TextureRegionEditor::_texture_overlay_input));
  1072. texture_overlay->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  1073. HBoxContainer *zoom_hb = memnew(HBoxContainer);
  1074. texture_overlay->add_child(zoom_hb);
  1075. zoom_hb->set_begin(Point2(5, 5));
  1076. zoom_out = memnew(Button);
  1077. zoom_out->set_flat(true);
  1078. zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1079. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_out));
  1080. zoom_hb->add_child(zoom_out);
  1081. zoom_reset = memnew(Button);
  1082. zoom_reset->set_flat(true);
  1083. zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1084. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_reset));
  1085. zoom_hb->add_child(zoom_reset);
  1086. zoom_in = memnew(Button);
  1087. zoom_in->set_flat(true);
  1088. zoom_in->set_tooltip_text(TTR("Zoom In"));
  1089. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_in));
  1090. zoom_hb->add_child(zoom_in);
  1091. vscroll = memnew(VScrollBar);
  1092. vscroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);
  1093. vscroll->set_step(0.001);
  1094. vscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed));
  1095. texture_overlay->add_child(vscroll);
  1096. hscroll = memnew(HScrollBar);
  1097. hscroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE);
  1098. hscroll->set_step(0.001);
  1099. hscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed));
  1100. texture_overlay->add_child(hscroll);
  1101. }
  1102. ////////////////////////
  1103. bool EditorInspectorPluginTextureRegion::can_handle(Object *p_object) {
  1104. return Object::cast_to<Sprite2D>(p_object) || Object::cast_to<Sprite3D>(p_object) || Object::cast_to<NinePatchRect>(p_object) || Object::cast_to<StyleBoxTexture>(p_object) || Object::cast_to<AtlasTexture>(p_object);
  1105. }
  1106. void EditorInspectorPluginTextureRegion::_region_edit(Object *p_object) {
  1107. texture_region_editor->edit(p_object);
  1108. }
  1109. bool EditorInspectorPluginTextureRegion::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) {
  1110. if ((p_type == Variant::RECT2 || p_type == Variant::RECT2I)) {
  1111. if (((Object::cast_to<Sprite2D>(p_object) || Object::cast_to<Sprite3D>(p_object) || Object::cast_to<NinePatchRect>(p_object) || Object::cast_to<StyleBoxTexture>(p_object)) && p_path == "region_rect") || (Object::cast_to<AtlasTexture>(p_object) && p_path == "region")) {
  1112. Button *button = EditorInspector::create_inspector_action_button(TTR("Edit Region"));
  1113. button->set_button_icon(texture_region_editor->get_editor_theme_icon(SNAME("RegionEdit")));
  1114. button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorPluginTextureRegion::_region_edit).bind(p_object));
  1115. add_property_editor(p_path, button, true);
  1116. }
  1117. }
  1118. return false; //not exclusive
  1119. }
  1120. EditorInspectorPluginTextureRegion::EditorInspectorPluginTextureRegion() {
  1121. texture_region_editor = memnew(TextureRegionEditor);
  1122. EditorNode::get_singleton()->get_gui_base()->add_child(texture_region_editor);
  1123. }
  1124. TextureRegionEditorPlugin::TextureRegionEditorPlugin() {
  1125. Ref<EditorInspectorPluginTextureRegion> inspector_plugin;
  1126. inspector_plugin.instantiate();
  1127. add_inspector_plugin(inspector_plugin);
  1128. }