texture_region_editor_plugin.cpp 44 KB

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