texture_region_editor_plugin.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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(), base_tex->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
  80. edit_draw->draw_texture(base_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_deferredp(nullptr, 0);
  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("bg"), 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(EditorSettings::get_singleton()->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. if (p_obj) {
  818. node_sprite_2d = Object::cast_to<Sprite2D>(p_obj);
  819. node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
  820. node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
  821. bool is_resource = false;
  822. if (Object::cast_to<StyleBoxTexture>(p_obj)) {
  823. obj_styleBox = Ref<StyleBoxTexture>(Object::cast_to<StyleBoxTexture>(p_obj));
  824. is_resource = true;
  825. }
  826. if (Object::cast_to<AtlasTexture>(p_obj)) {
  827. atlas_tex = Ref<AtlasTexture>(Object::cast_to<AtlasTexture>(p_obj));
  828. is_resource = true;
  829. }
  830. if (is_resource) {
  831. p_obj->connect("changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
  832. } else {
  833. p_obj->connect("texture_changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
  834. }
  835. _edit_region();
  836. } else {
  837. node_sprite_2d = nullptr;
  838. node_sprite_3d = nullptr;
  839. node_ninepatch = nullptr;
  840. obj_styleBox = Ref<StyleBoxTexture>(nullptr);
  841. atlas_tex = Ref<AtlasTexture>(nullptr);
  842. }
  843. edit_draw->queue_redraw();
  844. popup_centered_ratio(0.5);
  845. request_center = true;
  846. }
  847. void TextureRegionEditor::_texture_changed() {
  848. if (!is_visible()) {
  849. return;
  850. }
  851. _edit_region();
  852. }
  853. void TextureRegionEditor::_edit_region() {
  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. } else if (node_sprite_3d) {
  860. texture = node_sprite_3d->get_texture();
  861. } else if (node_ninepatch) {
  862. texture = node_ninepatch->get_texture();
  863. } else if (obj_styleBox.is_valid()) {
  864. texture = obj_styleBox->get_texture();
  865. }
  866. if (texture.is_null()) {
  867. _zoom_reset();
  868. hscroll->hide();
  869. vscroll->hide();
  870. edit_draw->queue_redraw();
  871. return;
  872. }
  873. if (cache_map.has(texture->get_rid())) {
  874. autoslice_cache = cache_map[texture->get_rid()];
  875. autoslice_is_dirty = false;
  876. } else {
  877. if (is_visible() && snap_mode == SNAP_AUTOSLICE) {
  878. _update_autoslice();
  879. } else {
  880. autoslice_is_dirty = true;
  881. }
  882. }
  883. _update_rect();
  884. edit_draw->queue_redraw();
  885. }
  886. Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
  887. if (snap_mode == SNAP_GRID) {
  888. p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x);
  889. p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y);
  890. }
  891. return p_target;
  892. }
  893. TextureRegionEditor::TextureRegionEditor() {
  894. set_ok_button_text(TTR("Close"));
  895. VBoxContainer *vb = memnew(VBoxContainer);
  896. add_child(vb);
  897. node_sprite_2d = nullptr;
  898. node_sprite_3d = nullptr;
  899. node_ninepatch = nullptr;
  900. obj_styleBox = Ref<StyleBoxTexture>(nullptr);
  901. atlas_tex = Ref<AtlasTexture>(nullptr);
  902. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  903. snap_step = Vector2(10, 10);
  904. snap_separation = Vector2(0, 0);
  905. snap_mode = SNAP_NONE;
  906. edited_margin = -1;
  907. drag_index = -1;
  908. drag = false;
  909. HBoxContainer *hb_tools = memnew(HBoxContainer);
  910. vb->add_child(hb_tools);
  911. hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
  912. snap_mode_button = memnew(OptionButton);
  913. hb_tools->add_child(snap_mode_button);
  914. snap_mode_button->add_item(TTR("None"), 0);
  915. snap_mode_button->add_item(TTR("Pixel Snap"), 1);
  916. snap_mode_button->add_item(TTR("Grid Snap"), 2);
  917. snap_mode_button->add_item(TTR("Auto Slice"), 3);
  918. snap_mode_button->select(0);
  919. snap_mode_button->connect("item_selected", callable_mp(this, &TextureRegionEditor::_set_snap_mode));
  920. hb_grid = memnew(HBoxContainer);
  921. hb_tools->add_child(hb_grid);
  922. hb_grid->add_child(memnew(VSeparator));
  923. hb_grid->add_child(memnew(Label(TTR("Offset:"))));
  924. sb_off_x = memnew(SpinBox);
  925. sb_off_x->set_min(-256);
  926. sb_off_x->set_max(256);
  927. sb_off_x->set_step(1);
  928. sb_off_x->set_value(snap_offset.x);
  929. sb_off_x->set_suffix("px");
  930. sb_off_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
  931. hb_grid->add_child(sb_off_x);
  932. sb_off_y = memnew(SpinBox);
  933. sb_off_y->set_min(-256);
  934. sb_off_y->set_max(256);
  935. sb_off_y->set_step(1);
  936. sb_off_y->set_value(snap_offset.y);
  937. sb_off_y->set_suffix("px");
  938. sb_off_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
  939. hb_grid->add_child(sb_off_y);
  940. hb_grid->add_child(memnew(VSeparator));
  941. hb_grid->add_child(memnew(Label(TTR("Step:"))));
  942. sb_step_x = memnew(SpinBox);
  943. sb_step_x->set_min(-256);
  944. sb_step_x->set_max(256);
  945. sb_step_x->set_step(1);
  946. sb_step_x->set_value(snap_step.x);
  947. sb_step_x->set_suffix("px");
  948. sb_step_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
  949. hb_grid->add_child(sb_step_x);
  950. sb_step_y = memnew(SpinBox);
  951. sb_step_y->set_min(-256);
  952. sb_step_y->set_max(256);
  953. sb_step_y->set_step(1);
  954. sb_step_y->set_value(snap_step.y);
  955. sb_step_y->set_suffix("px");
  956. sb_step_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
  957. hb_grid->add_child(sb_step_y);
  958. hb_grid->add_child(memnew(VSeparator));
  959. hb_grid->add_child(memnew(Label(TTR("Separation:"))));
  960. sb_sep_x = memnew(SpinBox);
  961. sb_sep_x->set_min(0);
  962. sb_sep_x->set_max(256);
  963. sb_sep_x->set_step(1);
  964. sb_sep_x->set_value(snap_separation.x);
  965. sb_sep_x->set_suffix("px");
  966. sb_sep_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
  967. hb_grid->add_child(sb_sep_x);
  968. sb_sep_y = memnew(SpinBox);
  969. sb_sep_y->set_min(0);
  970. sb_sep_y->set_max(256);
  971. sb_sep_y->set_step(1);
  972. sb_sep_y->set_value(snap_separation.y);
  973. sb_sep_y->set_suffix("px");
  974. sb_sep_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
  975. hb_grid->add_child(sb_sep_y);
  976. hb_grid->hide();
  977. panner.instantiate();
  978. panner->set_callbacks(callable_mp(this, &TextureRegionEditor::_scroll_callback), callable_mp(this, &TextureRegionEditor::_pan_callback), callable_mp(this, &TextureRegionEditor::_zoom_callback));
  979. edit_draw = memnew(Panel);
  980. vb->add_child(edit_draw);
  981. edit_draw->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  982. edit_draw->connect("draw", callable_mp(this, &TextureRegionEditor::_region_draw));
  983. edit_draw->connect("gui_input", callable_mp(this, &TextureRegionEditor::_region_input));
  984. edit_draw->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  985. edit_draw->set_focus_mode(Control::FOCUS_CLICK);
  986. draw_zoom = 1.0;
  987. edit_draw->set_clip_contents(true);
  988. HBoxContainer *zoom_hb = memnew(HBoxContainer);
  989. edit_draw->add_child(zoom_hb);
  990. zoom_hb->set_begin(Point2(5, 5));
  991. zoom_out = memnew(Button);
  992. zoom_out->set_flat(true);
  993. zoom_out->set_tooltip_text(TTR("Zoom Out"));
  994. zoom_out->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_out));
  995. zoom_hb->add_child(zoom_out);
  996. zoom_reset = memnew(Button);
  997. zoom_reset->set_flat(true);
  998. zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  999. zoom_reset->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_reset));
  1000. zoom_hb->add_child(zoom_reset);
  1001. zoom_in = memnew(Button);
  1002. zoom_in->set_flat(true);
  1003. zoom_in->set_tooltip_text(TTR("Zoom In"));
  1004. zoom_in->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_in));
  1005. zoom_hb->add_child(zoom_in);
  1006. vscroll = memnew(VScrollBar);
  1007. vscroll->set_step(0.001);
  1008. edit_draw->add_child(vscroll);
  1009. vscroll->connect("value_changed", callable_mp(this, &TextureRegionEditor::_scroll_changed));
  1010. hscroll = memnew(HScrollBar);
  1011. hscroll->set_step(0.001);
  1012. edit_draw->add_child(hscroll);
  1013. hscroll->connect("value_changed", callable_mp(this, &TextureRegionEditor::_scroll_changed));
  1014. updating_scroll = false;
  1015. autoslice_is_dirty = true;
  1016. set_title(TTR("Region Editor"));
  1017. }
  1018. ////////////////////////
  1019. bool EditorInspectorPluginTextureRegion::can_handle(Object *p_object) {
  1020. 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);
  1021. }
  1022. void EditorInspectorPluginTextureRegion::_region_edit(Object *p_object) {
  1023. texture_region_editor->edit(p_object);
  1024. }
  1025. 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) {
  1026. if ((p_type == Variant::RECT2 || p_type == Variant::RECT2I)) {
  1027. 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")) {
  1028. Button *button = EditorInspector::create_inspector_action_button(TTR("Edit Region"));
  1029. button->set_icon(texture_region_editor->get_theme_icon(SNAME("RegionEdit"), SNAME("EditorIcons")));
  1030. button->connect("pressed", callable_mp(this, &EditorInspectorPluginTextureRegion::_region_edit).bind(p_object));
  1031. add_property_editor(p_path, button, true);
  1032. }
  1033. }
  1034. return false; //not exclusive
  1035. }
  1036. EditorInspectorPluginTextureRegion::EditorInspectorPluginTextureRegion() {
  1037. texture_region_editor = memnew(TextureRegionEditor);
  1038. EditorNode::get_singleton()->get_gui_base()->add_child(texture_region_editor);
  1039. }
  1040. TextureRegionEditorPlugin::TextureRegionEditorPlugin() {
  1041. Ref<EditorInspectorPluginTextureRegion> inspector_plugin;
  1042. inspector_plugin.instantiate();
  1043. add_inspector_plugin(inspector_plugin);
  1044. }