texture_region_editor_plugin.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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/core_string_names.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_scale.h"
  35. #include "scene/gui/check_box.h"
  36. /**
  37. @author Mariano Suligoy
  38. */
  39. void draw_margin_line(Control *edit_draw, Vector2 from, Vector2 to) {
  40. Vector2 line = (to - from).normalized() * 10;
  41. // Draw a translucent background line to make the foreground line visible on any background.
  42. edit_draw->draw_line(
  43. from,
  44. to,
  45. EditorNode::get_singleton()->get_theme_base()->get_color("mono_color", "Editor").inverted() * Color(1, 1, 1, 0.5),
  46. Math::round(2 * EDSCALE));
  47. while ((to - from).length_squared() > 200) {
  48. edit_draw->draw_line(
  49. from,
  50. from + line,
  51. EditorNode::get_singleton()->get_theme_base()->get_color("mono_color", "Editor"),
  52. Math::round(2 * EDSCALE));
  53. from += line * 2;
  54. }
  55. }
  56. void TextureRegionEditor::_region_draw() {
  57. Ref<Texture> base_tex = nullptr;
  58. if (node_sprite) {
  59. base_tex = node_sprite->get_texture();
  60. } else if (node_sprite_3d) {
  61. base_tex = node_sprite_3d->get_texture();
  62. } else if (node_ninepatch) {
  63. base_tex = node_ninepatch->get_texture();
  64. } else if (obj_styleBox.is_valid()) {
  65. base_tex = obj_styleBox->get_texture();
  66. } else if (atlas_tex.is_valid()) {
  67. base_tex = atlas_tex->get_atlas();
  68. }
  69. if (base_tex.is_null()) {
  70. return;
  71. }
  72. Transform2D mtx;
  73. mtx.elements[2] = -draw_ofs * draw_zoom;
  74. mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
  75. VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
  76. edit_draw->draw_texture(base_tex, Point2());
  77. VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());
  78. const Color color = get_color("mono_color", "Editor");
  79. if (snap_mode == SNAP_GRID) {
  80. const Color grid_color = Color(color.r, color.g, color.b, color.a * 0.15);
  81. Size2 s = edit_draw->get_size();
  82. int last_cell = 0;
  83. if (snap_step.x != 0) {
  84. if (snap_separation.x == 0) {
  85. for (int i = 0; i < s.width; i++) {
  86. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
  87. if (i == 0) {
  88. last_cell = cell;
  89. }
  90. if (last_cell != cell) {
  91. edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), grid_color);
  92. }
  93. last_cell = cell;
  94. }
  95. } else {
  96. for (int i = 0; i < s.width; i++) {
  97. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x)));
  98. if (i == 0) {
  99. last_cell = cell;
  100. }
  101. if (last_cell != cell) {
  102. edit_draw->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), grid_color);
  103. }
  104. last_cell = cell;
  105. }
  106. }
  107. }
  108. if (snap_step.y != 0) {
  109. if (snap_separation.y == 0) {
  110. for (int i = 0; i < s.height; i++) {
  111. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
  112. if (i == 0) {
  113. last_cell = cell;
  114. }
  115. if (last_cell != cell) {
  116. edit_draw->draw_line(Point2(0, i), Point2(s.width, i), grid_color);
  117. }
  118. last_cell = cell;
  119. }
  120. } else {
  121. for (int i = 0; i < s.height; i++) {
  122. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y)));
  123. if (i == 0) {
  124. last_cell = cell;
  125. }
  126. if (last_cell != cell) {
  127. edit_draw->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), grid_color);
  128. }
  129. last_cell = cell;
  130. }
  131. }
  132. }
  133. } else if (snap_mode == SNAP_AUTOSLICE) {
  134. for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
  135. Rect2 r = E->get();
  136. Vector2 endpoints[4] = {
  137. mtx.basis_xform(r.position),
  138. mtx.basis_xform(r.position + Vector2(r.size.x, 0)),
  139. mtx.basis_xform(r.position + r.size),
  140. mtx.basis_xform(r.position + Vector2(0, r.size.y))
  141. };
  142. for (int i = 0; i < 4; i++) {
  143. int next = (i + 1) % 4;
  144. edit_draw->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, Color(0.3, 0.7, 1, 1), 2);
  145. }
  146. }
  147. }
  148. Ref<Texture> select_handle = get_icon("EditorHandle", "EditorIcons");
  149. Rect2 scroll_rect(Point2(), base_tex->get_size());
  150. const Vector2 raw_endpoints[4] = {
  151. rect.position,
  152. rect.position + Vector2(rect.size.x, 0),
  153. rect.position + rect.size,
  154. rect.position + Vector2(0, rect.size.y)
  155. };
  156. const Vector2 endpoints[4] = {
  157. mtx.basis_xform(raw_endpoints[0]),
  158. mtx.basis_xform(raw_endpoints[1]),
  159. mtx.basis_xform(raw_endpoints[2]),
  160. mtx.basis_xform(raw_endpoints[3])
  161. };
  162. for (int i = 0; i < 4; i++) {
  163. int prev = (i + 3) % 4;
  164. int next = (i + 1) % 4;
  165. Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
  166. ofs *= Math_SQRT2 * (select_handle->get_size().width / 2);
  167. edit_draw->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, color, 2);
  168. if (snap_mode != SNAP_AUTOSLICE) {
  169. edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
  170. }
  171. ofs = (endpoints[next] - endpoints[i]) / 2;
  172. ofs += (endpoints[next] - endpoints[i]).tangent().normalized() * (select_handle->get_size().width / 2);
  173. if (snap_mode != SNAP_AUTOSLICE) {
  174. edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
  175. }
  176. scroll_rect.expand_to(raw_endpoints[i]);
  177. }
  178. const Size2 scroll_margin = edit_draw->get_size() / draw_zoom;
  179. scroll_rect.position -= scroll_margin;
  180. scroll_rect.size += scroll_margin * 2;
  181. updating_scroll = true;
  182. hscroll->set_min(scroll_rect.position.x);
  183. hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x);
  184. if (ABS(scroll_rect.position.x - (scroll_rect.position.x + scroll_rect.size.x)) <= scroll_margin.x) {
  185. hscroll->hide();
  186. } else {
  187. hscroll->show();
  188. hscroll->set_page(scroll_margin.x);
  189. hscroll->set_value(draw_ofs.x);
  190. }
  191. vscroll->set_min(scroll_rect.position.y);
  192. vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y);
  193. if (ABS(scroll_rect.position.y - (scroll_rect.position.y + scroll_rect.size.y)) <= scroll_margin.y) {
  194. vscroll->hide();
  195. draw_ofs.y = scroll_rect.position.y;
  196. } else {
  197. vscroll->show();
  198. vscroll->set_page(scroll_margin.y);
  199. vscroll->set_value(draw_ofs.y);
  200. }
  201. Size2 hmin = hscroll->get_combined_minimum_size();
  202. Size2 vmin = vscroll->get_combined_minimum_size();
  203. // Avoid scrollbar overlapping.
  204. hscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, vscroll->is_visible() ? -vmin.width : 0);
  205. vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, hscroll->is_visible() ? -hmin.height : 0);
  206. updating_scroll = false;
  207. if (node_ninepatch || obj_styleBox.is_valid()) {
  208. float margins[4] = { 0 };
  209. if (node_ninepatch) {
  210. margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP);
  211. margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM);
  212. margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT);
  213. margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT);
  214. } else if (obj_styleBox.is_valid()) {
  215. margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP);
  216. margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM);
  217. margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT);
  218. margins[3] = obj_styleBox->get_margin_size(MARGIN_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(edit_draw, pos[0], pos[0] + Vector2(edit_draw->get_size().x, 0));
  227. draw_margin_line(edit_draw, pos[1], pos[1] + Vector2(edit_draw->get_size().x, 0));
  228. draw_margin_line(edit_draw, pos[2], pos[2] + Vector2(0, edit_draw->get_size().y));
  229. draw_margin_line(edit_draw, pos[3], pos[3] + Vector2(0, edit_draw->get_size().y));
  230. }
  231. }
  232. void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
  233. Transform2D mtx;
  234. mtx.elements[2] = -draw_ofs * draw_zoom;
  235. mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
  236. const real_t handle_radius = 8 * EDSCALE;
  237. const real_t handle_offset = 4 * EDSCALE;
  238. // Position of selection handles.
  239. const Vector2 endpoints[8] = {
  240. mtx.xform(rect.position) + Vector2(-handle_offset, -handle_offset),
  241. mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -handle_offset),
  242. mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(handle_offset, -handle_offset),
  243. mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(handle_offset, 0),
  244. mtx.xform(rect.position + rect.size) + Vector2(handle_offset, handle_offset),
  245. mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, handle_offset),
  246. mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-handle_offset, handle_offset),
  247. mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-handle_offset, 0)
  248. };
  249. Ref<InputEventMouseButton> mb = p_input;
  250. if (mb.is_valid()) {
  251. if (mb->get_button_index() == BUTTON_LEFT) {
  252. if (mb->is_pressed()) {
  253. if (node_ninepatch || obj_styleBox.is_valid()) {
  254. edited_margin = -1;
  255. float margins[4] = { 0 };
  256. if (node_ninepatch) {
  257. margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP);
  258. margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM);
  259. margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT);
  260. margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT);
  261. } else if (obj_styleBox.is_valid()) {
  262. margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP);
  263. margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM);
  264. margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT);
  265. margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT);
  266. }
  267. Vector2 pos[4] = {
  268. mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs * draw_zoom,
  269. mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs * draw_zoom,
  270. mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs * draw_zoom,
  271. mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs * draw_zoom
  272. };
  273. if (Math::abs(mb->get_position().y - pos[0].y) < 8) {
  274. edited_margin = 0;
  275. prev_margin = margins[0];
  276. } else if (Math::abs(mb->get_position().y - pos[1].y) < 8) {
  277. edited_margin = 1;
  278. prev_margin = margins[1];
  279. } else if (Math::abs(mb->get_position().x - pos[2].x) < 8) {
  280. edited_margin = 2;
  281. prev_margin = margins[2];
  282. } else if (Math::abs(mb->get_position().x - pos[3].x) < 8) {
  283. edited_margin = 3;
  284. prev_margin = margins[3];
  285. }
  286. if (edited_margin >= 0) {
  287. drag_from = Vector2(mb->get_position().x, mb->get_position().y);
  288. drag = true;
  289. }
  290. }
  291. if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) {
  292. Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
  293. for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
  294. if (E->get().has_point(point)) {
  295. rect = E->get();
  296. if (Input::get_singleton()->is_key_pressed(KEY_CONTROL) && !(Input::get_singleton()->is_key_pressed(KEY_SHIFT | KEY_ALT))) {
  297. Rect2 r;
  298. if (node_sprite) {
  299. r = node_sprite->get_region_rect();
  300. } else if (node_sprite_3d) {
  301. r = node_sprite_3d->get_region_rect();
  302. } else if (node_ninepatch) {
  303. r = node_ninepatch->get_region_rect();
  304. } else if (obj_styleBox.is_valid()) {
  305. r = obj_styleBox->get_region_rect();
  306. } else if (atlas_tex.is_valid()) {
  307. r = atlas_tex->get_region();
  308. }
  309. rect.expand_to(r.position);
  310. rect.expand_to(r.position + r.size);
  311. }
  312. undo_redo->create_action(TTR("Set Region Rect"));
  313. if (node_sprite) {
  314. undo_redo->add_do_method(node_sprite, "set_region_rect", rect);
  315. undo_redo->add_undo_method(node_sprite, "set_region_rect", node_sprite->get_region_rect());
  316. } else if (node_sprite_3d) {
  317. undo_redo->add_do_method(node_sprite_3d, "set_region_rect", rect);
  318. undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
  319. } else if (node_ninepatch) {
  320. undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect);
  321. undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
  322. } else if (obj_styleBox.is_valid()) {
  323. undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", rect);
  324. undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect());
  325. } else if (atlas_tex.is_valid()) {
  326. undo_redo->add_do_method(atlas_tex.ptr(), "set_region", rect);
  327. undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region());
  328. }
  329. undo_redo->add_do_method(this, "_update_rect");
  330. undo_redo->add_undo_method(this, "_update_rect");
  331. undo_redo->add_do_method(edit_draw, "update");
  332. undo_redo->add_undo_method(edit_draw, "update");
  333. undo_redo->commit_action();
  334. break;
  335. }
  336. }
  337. } else if (edited_margin < 0) {
  338. drag_from = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
  339. if (snap_mode == SNAP_PIXEL) {
  340. drag_from = drag_from.snapped(Vector2(1, 1));
  341. } else if (snap_mode == SNAP_GRID) {
  342. drag_from = snap_point(drag_from);
  343. }
  344. drag = true;
  345. if (node_sprite) {
  346. rect_prev = node_sprite->get_region_rect();
  347. } else if (node_sprite_3d) {
  348. rect_prev = node_sprite_3d->get_region_rect();
  349. } else if (node_ninepatch) {
  350. rect_prev = node_ninepatch->get_region_rect();
  351. } else if (obj_styleBox.is_valid()) {
  352. rect_prev = obj_styleBox->get_region_rect();
  353. } else if (atlas_tex.is_valid()) {
  354. rect_prev = atlas_tex->get_region();
  355. }
  356. for (int i = 0; i < 8; i++) {
  357. Vector2 tuv = endpoints[i];
  358. if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < handle_radius) {
  359. drag_index = i;
  360. }
  361. }
  362. if (drag_index == -1) {
  363. creating = true;
  364. rect = Rect2(drag_from, Size2());
  365. }
  366. }
  367. } else if (drag) {
  368. if (edited_margin >= 0) {
  369. undo_redo->create_action(TTR("Set Margin"));
  370. static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
  371. if (node_ninepatch) {
  372. undo_redo->add_do_method(node_ninepatch, "set_patch_margin", m[edited_margin], node_ninepatch->get_patch_margin(m[edited_margin]));
  373. undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", m[edited_margin], prev_margin);
  374. } else if (obj_styleBox.is_valid()) {
  375. undo_redo->add_do_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], obj_styleBox->get_margin_size(m[edited_margin]));
  376. undo_redo->add_undo_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], prev_margin);
  377. obj_styleBox->emit_signal(CoreStringNames::get_singleton()->changed);
  378. }
  379. edited_margin = -1;
  380. } else {
  381. undo_redo->create_action(TTR("Set Region Rect"));
  382. if (node_sprite) {
  383. undo_redo->add_do_method(node_sprite, "set_region_rect", node_sprite->get_region_rect());
  384. undo_redo->add_undo_method(node_sprite, "set_region_rect", rect_prev);
  385. } else if (node_sprite_3d) {
  386. undo_redo->add_do_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
  387. undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", rect_prev);
  388. } else if (atlas_tex.is_valid()) {
  389. undo_redo->add_do_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region());
  390. undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", rect_prev);
  391. } else if (node_ninepatch) {
  392. undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
  393. undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev);
  394. } else if (obj_styleBox.is_valid()) {
  395. undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect());
  396. undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", rect_prev);
  397. }
  398. drag_index = -1;
  399. }
  400. undo_redo->add_do_method(this, "_update_rect");
  401. undo_redo->add_undo_method(this, "_update_rect");
  402. undo_redo->add_do_method(edit_draw, "update");
  403. undo_redo->add_undo_method(edit_draw, "update");
  404. undo_redo->commit_action();
  405. drag = false;
  406. creating = false;
  407. }
  408. } else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  409. if (drag) {
  410. drag = false;
  411. if (edited_margin >= 0) {
  412. static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
  413. if (node_ninepatch) {
  414. node_ninepatch->set_patch_margin(m[edited_margin], prev_margin);
  415. }
  416. if (obj_styleBox.is_valid()) {
  417. obj_styleBox->set_margin_size(m[edited_margin], prev_margin);
  418. }
  419. edited_margin = -1;
  420. } else {
  421. apply_rect(rect_prev);
  422. rect = rect_prev;
  423. edit_draw->update();
  424. drag_index = -1;
  425. }
  426. }
  427. } else if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  428. _zoom_on_position(draw_zoom * ((0.95 + (0.05 * mb->get_factor())) / 0.95), mb->get_position());
  429. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  430. _zoom_on_position(draw_zoom * (1 - (0.05 * mb->get_factor())), mb->get_position());
  431. }
  432. }
  433. Ref<InputEventMouseMotion> mm = p_input;
  434. if (mm.is_valid()) {
  435. if (mm->get_button_mask() & BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  436. Vector2 dragged(mm->get_relative().x / draw_zoom, mm->get_relative().y / draw_zoom);
  437. hscroll->set_value(hscroll->get_value() - dragged.x);
  438. vscroll->set_value(vscroll->get_value() - dragged.y);
  439. } else if (drag) {
  440. if (edited_margin >= 0) {
  441. float new_margin = 0;
  442. if (snap_mode != SNAP_GRID) {
  443. if (edited_margin == 0) {
  444. new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom;
  445. } else if (edited_margin == 1) {
  446. new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom;
  447. } else if (edited_margin == 2) {
  448. new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom;
  449. } else if (edited_margin == 3) {
  450. new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom;
  451. } else {
  452. ERR_PRINT("Unexpected edited_margin");
  453. }
  454. if (snap_mode == SNAP_PIXEL) {
  455. new_margin = Math::round(new_margin);
  456. }
  457. } else {
  458. Vector2 pos_snapped = snap_point(mtx.affine_inverse().xform(mm->get_position()));
  459. Rect2 rect_rounded = Rect2(rect.position.round(), rect.size.round());
  460. if (edited_margin == 0) {
  461. new_margin = pos_snapped.y - rect_rounded.position.y;
  462. } else if (edited_margin == 1) {
  463. new_margin = rect_rounded.size.y + rect_rounded.position.y - pos_snapped.y;
  464. } else if (edited_margin == 2) {
  465. new_margin = pos_snapped.x - rect_rounded.position.x;
  466. } else if (edited_margin == 3) {
  467. new_margin = rect_rounded.size.x + rect_rounded.position.x - pos_snapped.x;
  468. } else {
  469. ERR_PRINT("Unexpected edited_margin");
  470. }
  471. }
  472. if (new_margin < 0) {
  473. new_margin = 0;
  474. }
  475. static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
  476. if (node_ninepatch) {
  477. node_ninepatch->set_patch_margin(m[edited_margin], new_margin);
  478. }
  479. if (obj_styleBox.is_valid()) {
  480. obj_styleBox->set_margin_size(m[edited_margin], new_margin);
  481. }
  482. } else {
  483. Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position());
  484. if (snap_mode == SNAP_PIXEL) {
  485. new_pos = new_pos.snapped(Vector2(1, 1));
  486. } else if (snap_mode == SNAP_GRID) {
  487. new_pos = snap_point(new_pos);
  488. }
  489. if (creating) {
  490. rect = Rect2(drag_from, Size2());
  491. rect.expand_to(new_pos);
  492. apply_rect(rect);
  493. edit_draw->update();
  494. return;
  495. }
  496. switch (drag_index) {
  497. case 0: {
  498. Vector2 p = rect_prev.position + rect_prev.size;
  499. rect = Rect2(p, Size2());
  500. rect.expand_to(new_pos);
  501. apply_rect(rect);
  502. } break;
  503. case 1: {
  504. Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
  505. rect = Rect2(p, Size2(rect_prev.size.x, 0));
  506. rect.expand_to(new_pos);
  507. apply_rect(rect);
  508. } break;
  509. case 2: {
  510. Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
  511. rect = Rect2(p, Size2());
  512. rect.expand_to(new_pos);
  513. apply_rect(rect);
  514. } break;
  515. case 3: {
  516. Vector2 p = rect_prev.position;
  517. rect = Rect2(p, Size2(0, rect_prev.size.y));
  518. rect.expand_to(new_pos);
  519. apply_rect(rect);
  520. } break;
  521. case 4: {
  522. Vector2 p = rect_prev.position;
  523. rect = Rect2(p, Size2());
  524. rect.expand_to(new_pos);
  525. apply_rect(rect);
  526. } break;
  527. case 5: {
  528. Vector2 p = rect_prev.position;
  529. rect = Rect2(p, Size2(rect_prev.size.x, 0));
  530. rect.expand_to(new_pos);
  531. apply_rect(rect);
  532. } break;
  533. case 6: {
  534. Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
  535. rect = Rect2(p, Size2());
  536. rect.expand_to(new_pos);
  537. apply_rect(rect);
  538. } break;
  539. case 7: {
  540. Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
  541. rect = Rect2(p, Size2(0, rect_prev.size.y));
  542. rect.expand_to(new_pos);
  543. apply_rect(rect);
  544. } break;
  545. }
  546. }
  547. edit_draw->update();
  548. }
  549. }
  550. Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
  551. if (magnify_gesture.is_valid()) {
  552. _zoom_on_position(draw_zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  553. }
  554. Ref<InputEventPanGesture> pan_gesture = p_input;
  555. if (pan_gesture.is_valid()) {
  556. hscroll->set_value(hscroll->get_value() + hscroll->get_page() * pan_gesture->get_delta().x / 8);
  557. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y / 8);
  558. }
  559. }
  560. void TextureRegionEditor::_scroll_changed(float) {
  561. if (updating_scroll) {
  562. return;
  563. }
  564. draw_ofs.x = hscroll->get_value();
  565. draw_ofs.y = vscroll->get_value();
  566. edit_draw->update();
  567. }
  568. void TextureRegionEditor::_set_snap_mode(int p_mode) {
  569. snap_mode = p_mode;
  570. if (snap_mode == SNAP_GRID) {
  571. hb_grid->show();
  572. } else {
  573. hb_grid->hide();
  574. }
  575. if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
  576. _update_autoslice();
  577. }
  578. edit_draw->update();
  579. }
  580. void TextureRegionEditor::_set_snap_off_x(float p_val) {
  581. snap_offset.x = p_val;
  582. edit_draw->update();
  583. }
  584. void TextureRegionEditor::_set_snap_off_y(float p_val) {
  585. snap_offset.y = p_val;
  586. edit_draw->update();
  587. }
  588. void TextureRegionEditor::_set_snap_step_x(float p_val) {
  589. snap_step.x = p_val;
  590. edit_draw->update();
  591. }
  592. void TextureRegionEditor::_set_snap_step_y(float p_val) {
  593. snap_step.y = p_val;
  594. edit_draw->update();
  595. }
  596. void TextureRegionEditor::_set_snap_sep_x(float p_val) {
  597. snap_separation.x = p_val;
  598. edit_draw->update();
  599. }
  600. void TextureRegionEditor::_set_snap_sep_y(float p_val) {
  601. snap_separation.y = p_val;
  602. edit_draw->update();
  603. }
  604. void TextureRegionEditor::_zoom_on_position(float p_zoom, Point2 p_position) {
  605. if (p_zoom < 0.25 || p_zoom > 8) {
  606. return;
  607. }
  608. float prev_zoom = draw_zoom;
  609. draw_zoom = p_zoom;
  610. Point2 ofs = p_position;
  611. ofs = ofs / prev_zoom - ofs / draw_zoom;
  612. draw_ofs.x = Math::round(draw_ofs.x + ofs.x);
  613. draw_ofs.y = Math::round(draw_ofs.y + ofs.y);
  614. edit_draw->update();
  615. }
  616. void TextureRegionEditor::_zoom_in() {
  617. _zoom_on_position(draw_zoom * 1.5, edit_draw->get_size() / 2.0);
  618. }
  619. void TextureRegionEditor::_zoom_reset() {
  620. _zoom_on_position(1.0, edit_draw->get_size() / 2.0);
  621. }
  622. void TextureRegionEditor::_zoom_out() {
  623. _zoom_on_position(draw_zoom / 1.5, edit_draw->get_size() / 2.0);
  624. }
  625. void TextureRegionEditor::apply_rect(const Rect2 &p_rect) {
  626. if (node_sprite) {
  627. node_sprite->set_region_rect(p_rect);
  628. } else if (node_sprite_3d) {
  629. node_sprite_3d->set_region_rect(p_rect);
  630. } else if (node_ninepatch) {
  631. node_ninepatch->set_region_rect(p_rect);
  632. } else if (obj_styleBox.is_valid()) {
  633. obj_styleBox->set_region_rect(p_rect);
  634. } else if (atlas_tex.is_valid()) {
  635. atlas_tex->set_region(p_rect);
  636. }
  637. }
  638. void TextureRegionEditor::_update_rect() {
  639. if (node_sprite) {
  640. rect = node_sprite->get_region_rect();
  641. } else if (node_sprite_3d) {
  642. rect = node_sprite_3d->get_region_rect();
  643. } else if (node_ninepatch) {
  644. rect = node_ninepatch->get_region_rect();
  645. if (rect == Rect2()) {
  646. rect = Rect2(Vector2(), node_ninepatch->get_texture()->get_size());
  647. }
  648. } else if (obj_styleBox.is_valid()) {
  649. rect = obj_styleBox->get_region_rect();
  650. } else if (atlas_tex.is_valid()) {
  651. rect = atlas_tex->get_region();
  652. }
  653. }
  654. void TextureRegionEditor::_update_autoslice() {
  655. autoslice_is_dirty = false;
  656. autoslice_cache.clear();
  657. Ref<Texture> texture = nullptr;
  658. if (node_sprite) {
  659. texture = node_sprite->get_texture();
  660. } else if (node_sprite_3d) {
  661. texture = node_sprite_3d->get_texture();
  662. } else if (node_ninepatch) {
  663. texture = node_ninepatch->get_texture();
  664. } else if (obj_styleBox.is_valid()) {
  665. texture = obj_styleBox->get_texture();
  666. } else if (atlas_tex.is_valid()) {
  667. texture = atlas_tex->get_atlas();
  668. }
  669. if (texture.is_null()) {
  670. return;
  671. }
  672. for (int y = 0; y < texture->get_height(); y++) {
  673. for (int x = 0; x < texture->get_width(); x++) {
  674. if (texture->is_pixel_opaque(x, y)) {
  675. bool found = false;
  676. for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
  677. Rect2 grown = E->get().grow(1.5);
  678. if (grown.has_point(Point2(x, y))) {
  679. E->get().expand_to(Point2(x, y));
  680. E->get().expand_to(Point2(x + 1, y + 1));
  681. x = E->get().position.x + E->get().size.x - 1;
  682. bool merged = true;
  683. while (merged) {
  684. merged = false;
  685. bool queue_erase = false;
  686. for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) {
  687. if (queue_erase) {
  688. autoslice_cache.erase(F->prev());
  689. queue_erase = false;
  690. }
  691. if (F == E) {
  692. continue;
  693. }
  694. if (E->get().grow(1).intersects(F->get())) {
  695. E->get().expand_to(F->get().position);
  696. E->get().expand_to(F->get().position + F->get().size);
  697. if (F->prev()) {
  698. F = F->prev();
  699. autoslice_cache.erase(F->next());
  700. } else {
  701. queue_erase = true;
  702. // Can't delete the first rect in the list.
  703. }
  704. merged = true;
  705. }
  706. }
  707. }
  708. found = true;
  709. break;
  710. }
  711. }
  712. if (!found) {
  713. Rect2 new_rect(x, y, 1, 1);
  714. autoslice_cache.push_back(new_rect);
  715. }
  716. }
  717. }
  718. }
  719. cache_map[texture->get_rid()] = autoslice_cache;
  720. }
  721. void TextureRegionEditor::_notification(int p_what) {
  722. switch (p_what) {
  723. case NOTIFICATION_ENTER_TREE:
  724. case NOTIFICATION_THEME_CHANGED: {
  725. edit_draw->add_style_override("panel", get_stylebox("bg", "Tree"));
  726. } break;
  727. case NOTIFICATION_READY: {
  728. zoom_out->set_icon(get_icon("ZoomLess", "EditorIcons"));
  729. zoom_reset->set_icon(get_icon("ZoomReset", "EditorIcons"));
  730. zoom_in->set_icon(get_icon("ZoomMore", "EditorIcons"));
  731. vscroll->set_anchors_and_margins_preset(PRESET_RIGHT_WIDE);
  732. hscroll->set_anchors_and_margins_preset(PRESET_BOTTOM_WIDE);
  733. } break;
  734. case NOTIFICATION_VISIBILITY_CHANGED: {
  735. if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
  736. _update_autoslice();
  737. }
  738. } break;
  739. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  740. // This happens when the user leaves the Editor and returns,
  741. // they could have changed the textures, so the cache is cleared.
  742. cache_map.clear();
  743. _edit_region();
  744. } break;
  745. }
  746. }
  747. void TextureRegionEditor::_node_removed(Object *p_obj) {
  748. if (p_obj == node_sprite || p_obj == node_sprite_3d || p_obj == node_ninepatch || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) {
  749. node_sprite = nullptr;
  750. node_sprite_3d = nullptr;
  751. node_ninepatch = nullptr;
  752. obj_styleBox = Ref<StyleBox>(nullptr);
  753. atlas_tex = Ref<AtlasTexture>(nullptr);
  754. hide();
  755. }
  756. }
  757. void TextureRegionEditor::_bind_methods() {
  758. ClassDB::bind_method(D_METHOD("_edit_region"), &TextureRegionEditor::_edit_region);
  759. ClassDB::bind_method(D_METHOD("_region_draw"), &TextureRegionEditor::_region_draw);
  760. ClassDB::bind_method(D_METHOD("_region_input"), &TextureRegionEditor::_region_input);
  761. ClassDB::bind_method(D_METHOD("_scroll_changed"), &TextureRegionEditor::_scroll_changed);
  762. ClassDB::bind_method(D_METHOD("_node_removed"), &TextureRegionEditor::_node_removed);
  763. ClassDB::bind_method(D_METHOD("_set_snap_mode"), &TextureRegionEditor::_set_snap_mode);
  764. ClassDB::bind_method(D_METHOD("_set_snap_off_x"), &TextureRegionEditor::_set_snap_off_x);
  765. ClassDB::bind_method(D_METHOD("_set_snap_off_y"), &TextureRegionEditor::_set_snap_off_y);
  766. ClassDB::bind_method(D_METHOD("_set_snap_step_x"), &TextureRegionEditor::_set_snap_step_x);
  767. ClassDB::bind_method(D_METHOD("_set_snap_step_y"), &TextureRegionEditor::_set_snap_step_y);
  768. ClassDB::bind_method(D_METHOD("_set_snap_sep_x"), &TextureRegionEditor::_set_snap_sep_x);
  769. ClassDB::bind_method(D_METHOD("_set_snap_sep_y"), &TextureRegionEditor::_set_snap_sep_y);
  770. ClassDB::bind_method(D_METHOD("_zoom_on_position"), &TextureRegionEditor::_zoom_on_position);
  771. ClassDB::bind_method(D_METHOD("_zoom_in"), &TextureRegionEditor::_zoom_in);
  772. ClassDB::bind_method(D_METHOD("_zoom_reset"), &TextureRegionEditor::_zoom_reset);
  773. ClassDB::bind_method(D_METHOD("_zoom_out"), &TextureRegionEditor::_zoom_out);
  774. ClassDB::bind_method(D_METHOD("_update_rect"), &TextureRegionEditor::_update_rect);
  775. }
  776. bool TextureRegionEditor::is_stylebox() {
  777. return obj_styleBox.is_valid();
  778. }
  779. bool TextureRegionEditor::is_atlas_texture() {
  780. return atlas_tex.is_valid();
  781. }
  782. bool TextureRegionEditor::is_ninepatch() {
  783. return node_ninepatch != nullptr;
  784. }
  785. Sprite3D *TextureRegionEditor::get_sprite_3d() {
  786. return node_sprite_3d;
  787. }
  788. Sprite *TextureRegionEditor::get_sprite() {
  789. return node_sprite;
  790. }
  791. void TextureRegionEditor::edit(Object *p_obj) {
  792. if (node_sprite) {
  793. node_sprite->remove_change_receptor(this);
  794. }
  795. if (node_sprite_3d) {
  796. node_sprite_3d->remove_change_receptor(this);
  797. }
  798. if (node_ninepatch) {
  799. node_ninepatch->remove_change_receptor(this);
  800. }
  801. if (obj_styleBox.is_valid()) {
  802. obj_styleBox->remove_change_receptor(this);
  803. }
  804. if (atlas_tex.is_valid()) {
  805. atlas_tex->remove_change_receptor(this);
  806. }
  807. if (p_obj) {
  808. node_sprite = Object::cast_to<Sprite>(p_obj);
  809. node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
  810. node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
  811. if (Object::cast_to<StyleBoxTexture>(p_obj)) {
  812. obj_styleBox = Ref<StyleBoxTexture>(Object::cast_to<StyleBoxTexture>(p_obj));
  813. }
  814. if (Object::cast_to<AtlasTexture>(p_obj)) {
  815. atlas_tex = Ref<AtlasTexture>(Object::cast_to<AtlasTexture>(p_obj));
  816. }
  817. p_obj->add_change_receptor(this);
  818. _edit_region();
  819. } else {
  820. node_sprite = nullptr;
  821. node_sprite_3d = nullptr;
  822. node_ninepatch = nullptr;
  823. obj_styleBox = Ref<StyleBoxTexture>(nullptr);
  824. atlas_tex = Ref<AtlasTexture>(nullptr);
  825. }
  826. edit_draw->update();
  827. if ((node_sprite && !node_sprite->is_region()) || (node_sprite_3d && !node_sprite_3d->is_region())) {
  828. set_process(true);
  829. }
  830. if (!p_obj) {
  831. set_process(false);
  832. }
  833. }
  834. void TextureRegionEditor::_changed_callback(Object *p_changed, const char *p_prop) {
  835. if (!is_visible()) {
  836. return;
  837. }
  838. if (p_prop == StringName("atlas") || p_prop == StringName("texture") || p_prop == StringName("region")) {
  839. _edit_region();
  840. }
  841. if (Object::cast_to<NinePatchRect>(p_changed) && (p_prop == StringName("region_rect") || p_prop == StringName("patch_margin_left") || p_prop == StringName("patch_margin_right") || p_prop == StringName("patch_margin_top") || p_prop == StringName("patch_margin_bottom"))) {
  842. _edit_region();
  843. }
  844. if (Object::cast_to<StyleBoxTexture>(p_changed) && (p_prop == StringName("content_margin_left") || p_prop == StringName("content_margin_right") || p_prop == StringName("content_margin_top") || p_prop == StringName("content_margin_bottom"))) {
  845. _edit_region();
  846. }
  847. }
  848. void TextureRegionEditor::_edit_region() {
  849. Ref<Texture> texture = nullptr;
  850. if (node_sprite) {
  851. texture = node_sprite->get_texture();
  852. } else if (node_sprite_3d) {
  853. texture = node_sprite_3d->get_texture();
  854. } else if (node_ninepatch) {
  855. texture = node_ninepatch->get_texture();
  856. } else if (obj_styleBox.is_valid()) {
  857. texture = obj_styleBox->get_texture();
  858. } else if (atlas_tex.is_valid()) {
  859. texture = atlas_tex->get_atlas();
  860. }
  861. if (texture.is_null()) {
  862. _zoom_reset();
  863. hscroll->hide();
  864. vscroll->hide();
  865. edit_draw->update();
  866. return;
  867. }
  868. if (cache_map.has(texture->get_rid())) {
  869. autoslice_cache = cache_map[texture->get_rid()];
  870. autoslice_is_dirty = false;
  871. } else {
  872. if (is_visible() && snap_mode == SNAP_AUTOSLICE) {
  873. _update_autoslice();
  874. } else {
  875. autoslice_is_dirty = true;
  876. }
  877. }
  878. _update_rect();
  879. edit_draw->update();
  880. }
  881. Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
  882. if (snap_mode == SNAP_GRID) {
  883. p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x);
  884. p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y);
  885. }
  886. return p_target;
  887. }
  888. TextureRegionEditor::TextureRegionEditor(EditorNode *p_editor) {
  889. node_sprite = nullptr;
  890. node_sprite_3d = nullptr;
  891. node_ninepatch = nullptr;
  892. obj_styleBox = Ref<StyleBoxTexture>(nullptr);
  893. atlas_tex = Ref<AtlasTexture>(nullptr);
  894. editor = p_editor;
  895. undo_redo = editor->get_undo_redo();
  896. snap_step = Vector2(10, 10);
  897. snap_separation = Vector2(0, 0);
  898. snap_mode = SNAP_NONE;
  899. edited_margin = -1;
  900. drag_index = -1;
  901. drag = false;
  902. HBoxContainer *hb_tools = memnew(HBoxContainer);
  903. add_child(hb_tools);
  904. hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
  905. snap_mode_button = memnew(OptionButton);
  906. hb_tools->add_child(snap_mode_button);
  907. snap_mode_button->add_item(TTR("None"), 0);
  908. snap_mode_button->add_item(TTR("Pixel Snap"), 1);
  909. snap_mode_button->add_item(TTR("Grid Snap"), 2);
  910. snap_mode_button->add_item(TTR("Auto Slice"), 3);
  911. snap_mode_button->select(0);
  912. snap_mode_button->connect("item_selected", this, "_set_snap_mode");
  913. hb_grid = memnew(HBoxContainer);
  914. hb_tools->add_child(hb_grid);
  915. hb_grid->add_child(memnew(VSeparator));
  916. hb_grid->add_child(memnew(Label(TTR("Offset:"))));
  917. sb_off_x = memnew(SpinBox);
  918. sb_off_x->set_min(-256);
  919. sb_off_x->set_max(256);
  920. sb_off_x->set_step(1);
  921. sb_off_x->set_value(snap_offset.x);
  922. sb_off_x->set_suffix("px");
  923. sb_off_x->connect("value_changed", this, "_set_snap_off_x");
  924. hb_grid->add_child(sb_off_x);
  925. sb_off_y = memnew(SpinBox);
  926. sb_off_y->set_min(-256);
  927. sb_off_y->set_max(256);
  928. sb_off_y->set_step(1);
  929. sb_off_y->set_value(snap_offset.y);
  930. sb_off_y->set_suffix("px");
  931. sb_off_y->connect("value_changed", this, "_set_snap_off_y");
  932. hb_grid->add_child(sb_off_y);
  933. hb_grid->add_child(memnew(VSeparator));
  934. hb_grid->add_child(memnew(Label(TTR("Step:"))));
  935. sb_step_x = memnew(SpinBox);
  936. sb_step_x->set_min(-256);
  937. sb_step_x->set_max(256);
  938. sb_step_x->set_step(1);
  939. sb_step_x->set_value(snap_step.x);
  940. sb_step_x->set_suffix("px");
  941. sb_step_x->connect("value_changed", this, "_set_snap_step_x");
  942. hb_grid->add_child(sb_step_x);
  943. sb_step_y = memnew(SpinBox);
  944. sb_step_y->set_min(-256);
  945. sb_step_y->set_max(256);
  946. sb_step_y->set_step(1);
  947. sb_step_y->set_value(snap_step.y);
  948. sb_step_y->set_suffix("px");
  949. sb_step_y->connect("value_changed", this, "_set_snap_step_y");
  950. hb_grid->add_child(sb_step_y);
  951. hb_grid->add_child(memnew(VSeparator));
  952. hb_grid->add_child(memnew(Label(TTR("Separation:"))));
  953. sb_sep_x = memnew(SpinBox);
  954. sb_sep_x->set_min(0);
  955. sb_sep_x->set_max(256);
  956. sb_sep_x->set_step(1);
  957. sb_sep_x->set_value(snap_separation.x);
  958. sb_sep_x->set_suffix("px");
  959. sb_sep_x->connect("value_changed", this, "_set_snap_sep_x");
  960. hb_grid->add_child(sb_sep_x);
  961. sb_sep_y = memnew(SpinBox);
  962. sb_sep_y->set_min(0);
  963. sb_sep_y->set_max(256);
  964. sb_sep_y->set_step(1);
  965. sb_sep_y->set_value(snap_separation.y);
  966. sb_sep_y->set_suffix("px");
  967. sb_sep_y->connect("value_changed", this, "_set_snap_sep_y");
  968. hb_grid->add_child(sb_sep_y);
  969. hb_grid->hide();
  970. edit_draw = memnew(Panel);
  971. add_child(edit_draw);
  972. edit_draw->set_v_size_flags(SIZE_EXPAND_FILL);
  973. edit_draw->connect("draw", this, "_region_draw");
  974. edit_draw->connect("gui_input", this, "_region_input");
  975. draw_zoom = 1.0;
  976. edit_draw->set_clip_contents(true);
  977. HBoxContainer *zoom_hb = memnew(HBoxContainer);
  978. edit_draw->add_child(zoom_hb);
  979. zoom_hb->set_begin(Point2(5, 5));
  980. zoom_out = memnew(ToolButton);
  981. zoom_out->set_tooltip(TTR("Zoom Out"));
  982. zoom_out->connect("pressed", this, "_zoom_out");
  983. zoom_hb->add_child(zoom_out);
  984. zoom_reset = memnew(ToolButton);
  985. zoom_reset->set_tooltip(TTR("Zoom Reset"));
  986. zoom_reset->connect("pressed", this, "_zoom_reset");
  987. zoom_hb->add_child(zoom_reset);
  988. zoom_in = memnew(ToolButton);
  989. zoom_in->set_tooltip(TTR("Zoom In"));
  990. zoom_in->connect("pressed", this, "_zoom_in");
  991. zoom_hb->add_child(zoom_in);
  992. vscroll = memnew(VScrollBar);
  993. vscroll->set_step(0.001);
  994. edit_draw->add_child(vscroll);
  995. vscroll->connect("value_changed", this, "_scroll_changed");
  996. hscroll = memnew(HScrollBar);
  997. hscroll->set_step(0.001);
  998. edit_draw->add_child(hscroll);
  999. hscroll->connect("value_changed", this, "_scroll_changed");
  1000. updating_scroll = false;
  1001. autoslice_is_dirty = true;
  1002. }
  1003. void TextureRegionEditorPlugin::edit(Object *p_object) {
  1004. region_editor->edit(p_object);
  1005. }
  1006. bool TextureRegionEditorPlugin::handles(Object *p_object) const {
  1007. return p_object->is_class("Sprite") || p_object->is_class("Sprite3D") || p_object->is_class("NinePatchRect") || p_object->is_class("StyleBoxTexture") || p_object->is_class("AtlasTexture");
  1008. }
  1009. void TextureRegionEditorPlugin::_editor_visiblity_changed() {
  1010. manually_hidden = !region_editor->is_visible_in_tree();
  1011. }
  1012. void TextureRegionEditorPlugin::make_visible(bool p_visible) {
  1013. if (p_visible) {
  1014. texture_region_button->show();
  1015. bool is_node_configured = region_editor->is_stylebox() || region_editor->is_atlas_texture() || region_editor->is_ninepatch() || (region_editor->get_sprite() && region_editor->get_sprite()->is_region()) || (region_editor->get_sprite_3d() && region_editor->get_sprite_3d()->is_region());
  1016. if ((is_node_configured && !manually_hidden) || texture_region_button->is_pressed()) {
  1017. editor->make_bottom_panel_item_visible(region_editor);
  1018. }
  1019. } else {
  1020. if (region_editor->is_visible_in_tree()) {
  1021. editor->hide_bottom_panel();
  1022. manually_hidden = false;
  1023. }
  1024. texture_region_button->hide();
  1025. region_editor->edit(nullptr);
  1026. }
  1027. }
  1028. Dictionary TextureRegionEditorPlugin::get_state() const {
  1029. Dictionary state;
  1030. state["snap_offset"] = region_editor->snap_offset;
  1031. state["snap_step"] = region_editor->snap_step;
  1032. state["snap_separation"] = region_editor->snap_separation;
  1033. state["snap_mode"] = region_editor->snap_mode;
  1034. return state;
  1035. }
  1036. void TextureRegionEditorPlugin::set_state(const Dictionary &p_state) {
  1037. Dictionary state = p_state;
  1038. if (state.has("snap_step")) {
  1039. Vector2 s = state["snap_step"];
  1040. region_editor->sb_step_x->set_value(s.x);
  1041. region_editor->sb_step_y->set_value(s.y);
  1042. region_editor->snap_step = s;
  1043. }
  1044. if (state.has("snap_offset")) {
  1045. Vector2 ofs = state["snap_offset"];
  1046. region_editor->sb_off_x->set_value(ofs.x);
  1047. region_editor->sb_off_y->set_value(ofs.y);
  1048. region_editor->snap_offset = ofs;
  1049. }
  1050. if (state.has("snap_separation")) {
  1051. Vector2 sep = state["snap_separation"];
  1052. region_editor->sb_sep_x->set_value(sep.x);
  1053. region_editor->sb_sep_y->set_value(sep.y);
  1054. region_editor->snap_separation = sep;
  1055. }
  1056. if (state.has("snap_mode")) {
  1057. region_editor->_set_snap_mode(state["snap_mode"]);
  1058. region_editor->snap_mode_button->select(state["snap_mode"]);
  1059. }
  1060. }
  1061. void TextureRegionEditorPlugin::_bind_methods() {
  1062. ClassDB::bind_method(D_METHOD("_editor_visiblity_changed"), &TextureRegionEditorPlugin::_editor_visiblity_changed);
  1063. }
  1064. TextureRegionEditorPlugin::TextureRegionEditorPlugin(EditorNode *p_node) {
  1065. manually_hidden = false;
  1066. editor = p_node;
  1067. region_editor = memnew(TextureRegionEditor(p_node));
  1068. region_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  1069. region_editor->hide();
  1070. region_editor->connect("visibility_changed", this, "_editor_visiblity_changed");
  1071. texture_region_button = p_node->add_bottom_panel_item(TTR("TextureRegion"), region_editor);
  1072. texture_region_button->hide();
  1073. }