texture_region_editor_plugin.cpp 41 KB

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