tile_set_editor_plugin.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*************************************************************************/
  2. /* tile_set_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "tile_set_editor_plugin.h"
  31. #include "editor/plugins/canvas_item_editor_plugin.h"
  32. #include "scene/2d/physics_body_2d.h"
  33. #include "scene/2d/sprite.h"
  34. void TileSetEditor::edit(const Ref<TileSet> &p_tileset) {
  35. tileset = p_tileset;
  36. }
  37. void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {
  38. for (int i = 0; i < p_node->get_child_count(); i++) {
  39. Node *child = p_node->get_child(i);
  40. if (!Object::cast_to<Sprite>(child)) {
  41. if (child->get_child_count() > 0) {
  42. _import_node(child, p_library);
  43. }
  44. continue;
  45. }
  46. Sprite *mi = Object::cast_to<Sprite>(child);
  47. Ref<Texture> texture = mi->get_texture();
  48. Ref<Texture> normal_map = mi->get_normal_map();
  49. Ref<ShaderMaterial> material = mi->get_material();
  50. if (texture.is_null())
  51. continue;
  52. int id = p_library->find_tile_by_name(mi->get_name());
  53. if (id < 0) {
  54. id = p_library->get_last_unused_tile_id();
  55. p_library->create_tile(id);
  56. p_library->tile_set_name(id, mi->get_name());
  57. }
  58. p_library->tile_set_texture(id, texture);
  59. p_library->tile_set_normal_map(id, normal_map);
  60. p_library->tile_set_material(id, material);
  61. p_library->tile_set_modulate(id, mi->get_modulate());
  62. Vector2 phys_offset;
  63. Size2 s;
  64. if (mi->is_region()) {
  65. s = mi->get_region_rect().size;
  66. p_library->tile_set_region(id, mi->get_region_rect());
  67. } else {
  68. const int frame = mi->get_frame();
  69. const int hframes = mi->get_hframes();
  70. s = texture->get_size() / Size2(hframes, mi->get_vframes());
  71. p_library->tile_set_region(id, Rect2(Vector2(frame % hframes, frame / hframes) * s, s));
  72. }
  73. if (mi->is_centered()) {
  74. phys_offset += -s / 2;
  75. }
  76. Vector<TileSet::ShapeData> collisions;
  77. Ref<NavigationPolygon> nav_poly;
  78. Ref<OccluderPolygon2D> occluder;
  79. bool found_collisions = false;
  80. for (int j = 0; j < mi->get_child_count(); j++) {
  81. Node *child2 = mi->get_child(j);
  82. if (Object::cast_to<NavigationPolygonInstance>(child2))
  83. nav_poly = Object::cast_to<NavigationPolygonInstance>(child2)->get_navigation_polygon();
  84. if (Object::cast_to<LightOccluder2D>(child2))
  85. occluder = Object::cast_to<LightOccluder2D>(child2)->get_occluder_polygon();
  86. if (!Object::cast_to<StaticBody2D>(child2))
  87. continue;
  88. found_collisions = true;
  89. StaticBody2D *sb = Object::cast_to<StaticBody2D>(child2);
  90. List<uint32_t> shapes;
  91. sb->get_shape_owners(&shapes);
  92. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  93. if (sb->is_shape_owner_disabled(E->get())) continue;
  94. Transform2D shape_transform = sb->shape_owner_get_transform(E->get());
  95. bool one_way = sb->is_shape_owner_one_way_collision_enabled(E->get());
  96. shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
  97. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  98. Ref<Shape2D> shape = sb->shape_owner_get_shape(E->get(), k);
  99. TileSet::ShapeData shape_data;
  100. shape_data.shape = shape;
  101. shape_data.shape_transform = shape_transform;
  102. shape_data.one_way_collision = one_way;
  103. collisions.push_back(shape_data);
  104. }
  105. }
  106. }
  107. if (found_collisions) {
  108. p_library->tile_set_shapes(id, collisions);
  109. }
  110. p_library->tile_set_texture_offset(id, mi->get_offset());
  111. p_library->tile_set_navigation_polygon(id, nav_poly);
  112. p_library->tile_set_light_occluder(id, occluder);
  113. p_library->tile_set_occluder_offset(id, -phys_offset);
  114. p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
  115. }
  116. }
  117. void TileSetEditor::_import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge) {
  118. if (!p_merge)
  119. p_library->clear();
  120. _import_node(p_scene, p_library);
  121. }
  122. void TileSetEditor::_menu_confirm() {
  123. switch (option) {
  124. case MENU_OPTION_MERGE_FROM_SCENE:
  125. case MENU_OPTION_CREATE_FROM_SCENE: {
  126. EditorNode *en = editor;
  127. Node *scene = en->get_edited_scene();
  128. if (!scene)
  129. break;
  130. _import_scene(scene, tileset, option == MENU_OPTION_MERGE_FROM_SCENE);
  131. } break;
  132. }
  133. }
  134. void TileSetEditor::_name_dialog_confirm(const String &name) {
  135. switch (option) {
  136. case MENU_OPTION_REMOVE_ITEM: {
  137. int id = tileset->find_tile_by_name(name);
  138. if (id < 0 && name.is_valid_integer())
  139. id = name.to_int();
  140. if (tileset->has_tile(id)) {
  141. tileset->remove_tile(id);
  142. } else {
  143. err_dialog->set_text(TTR("Could not find tile:") + " " + name);
  144. err_dialog->popup_centered(Size2(300, 60));
  145. }
  146. } break;
  147. }
  148. }
  149. void TileSetEditor::_menu_cbk(int p_option) {
  150. option = p_option;
  151. switch (p_option) {
  152. case MENU_OPTION_ADD_ITEM: {
  153. tileset->create_tile(tileset->get_last_unused_tile_id());
  154. } break;
  155. case MENU_OPTION_REMOVE_ITEM: {
  156. nd->set_title(TTR("Remove Item"));
  157. nd->set_text(TTR("Item name or ID:"));
  158. nd->popup_centered(Size2(300, 95));
  159. } break;
  160. case MENU_OPTION_CREATE_FROM_SCENE: {
  161. cd->set_text(TTR("Create from scene?"));
  162. cd->popup_centered(Size2(300, 60));
  163. } break;
  164. case MENU_OPTION_MERGE_FROM_SCENE: {
  165. cd->set_text(TTR("Merge from scene?"));
  166. cd->popup_centered(Size2(300, 60));
  167. } break;
  168. }
  169. }
  170. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge) {
  171. _import_scene(p_base_scene, ml, p_merge);
  172. return OK;
  173. }
  174. void TileSetEditor::_bind_methods() {
  175. ClassDB::bind_method("_menu_cbk", &TileSetEditor::_menu_cbk);
  176. ClassDB::bind_method("_menu_confirm", &TileSetEditor::_menu_confirm);
  177. ClassDB::bind_method("_name_dialog_confirm", &TileSetEditor::_name_dialog_confirm);
  178. }
  179. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  180. Panel *panel = memnew(Panel);
  181. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  182. add_child(panel);
  183. MenuButton *options = memnew(MenuButton);
  184. panel->add_child(options);
  185. options->set_position(Point2(1, 1));
  186. options->set_text(TTR("Tile Set"));
  187. options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  188. options->get_popup()->add_item(TTR("Remove Item"), MENU_OPTION_REMOVE_ITEM);
  189. options->get_popup()->add_separator();
  190. options->get_popup()->add_item(TTR("Create from Scene"), MENU_OPTION_CREATE_FROM_SCENE);
  191. options->get_popup()->add_item(TTR("Merge from Scene"), MENU_OPTION_MERGE_FROM_SCENE);
  192. options->get_popup()->connect("id_pressed", this, "_menu_cbk");
  193. editor = p_editor;
  194. cd = memnew(ConfirmationDialog);
  195. add_child(cd);
  196. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  197. nd = memnew(EditorNameDialog);
  198. add_child(nd);
  199. nd->set_hide_on_ok(true);
  200. nd->get_line_edit()->set_margin(MARGIN_TOP, 28);
  201. nd->connect("name_confirmed", this, "_name_dialog_confirm");
  202. err_dialog = memnew(AcceptDialog);
  203. add_child(err_dialog);
  204. err_dialog->set_title(TTR("Error"));
  205. }
  206. void TileSetEditorPlugin::edit(Object *p_node) {
  207. if (Object::cast_to<TileSet>(p_node)) {
  208. tileset_editor->edit(Object::cast_to<TileSet>(p_node));
  209. tileset_editor->show();
  210. autotile_editor->edit(p_node);
  211. } else
  212. tileset_editor->hide();
  213. }
  214. bool TileSetEditorPlugin::handles(Object *p_node) const {
  215. return p_node->is_class("TileSet");
  216. }
  217. void TileSetEditorPlugin::make_visible(bool p_visible) {
  218. if (p_visible) {
  219. tileset_editor->show();
  220. autotile_button->show();
  221. autotile_editor->side_panel->show();
  222. if (autotile_button->is_pressed()) {
  223. autotile_editor->show();
  224. }
  225. } else {
  226. tileset_editor->hide();
  227. autotile_editor->side_panel->hide();
  228. autotile_editor->hide();
  229. autotile_button->hide();
  230. }
  231. }
  232. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  233. tileset_editor = memnew(TileSetEditor(p_node));
  234. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, tileset_editor);
  235. tileset_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  236. tileset_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
  237. tileset_editor->set_end(Point2(0, 22));
  238. tileset_editor->hide();
  239. autotile_editor = memnew(AutotileEditor(p_node));
  240. add_control_to_container(CONTAINER_CANVAS_EDITOR_SIDE, autotile_editor->side_panel);
  241. autotile_editor->side_panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  242. autotile_editor->side_panel->set_custom_minimum_size(Size2(200, 0));
  243. autotile_editor->side_panel->hide();
  244. autotile_button = p_node->add_bottom_panel_item("Autotiles", autotile_editor);
  245. autotile_button->hide();
  246. }
  247. AutotileEditor::AutotileEditor(EditorNode *p_editor) {
  248. editor = p_editor;
  249. //Side Panel
  250. side_panel = memnew(Control);
  251. side_panel->set_name("Autotiles");
  252. VSplitContainer *split = memnew(VSplitContainer);
  253. side_panel->add_child(split);
  254. split->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  255. autotile_list = memnew(ItemList);
  256. autotile_list->set_v_size_flags(SIZE_EXPAND_FILL);
  257. autotile_list->set_h_size_flags(SIZE_EXPAND_FILL);
  258. autotile_list->set_custom_minimum_size(Size2(02, 200));
  259. autotile_list->connect("item_selected", this, "_on_autotile_selected");
  260. split->add_child(autotile_list);
  261. property_editor = memnew(PropertyEditor);
  262. property_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  263. property_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  264. split->add_child(property_editor);
  265. helper = memnew(AutotileEditorHelper(this));
  266. property_editor->call_deferred("edit", helper);
  267. // Editor
  268. dragging_point = -1;
  269. creating_shape = false;
  270. set_custom_minimum_size(Size2(0, 150));
  271. VBoxContainer *main_vb = memnew(VBoxContainer);
  272. add_child(main_vb);
  273. main_vb->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  274. HBoxContainer *tool_hb = memnew(HBoxContainer);
  275. Ref<ButtonGroup> g(memnew(ButtonGroup));
  276. String label[EDITMODE_MAX] = { "Icon", "Bitmask", "Collision", "Occlusion", "Navigation", "Priority" };
  277. for (int i = 0; i < (int)EDITMODE_MAX; i++) {
  278. tool_editmode[i] = memnew(Button);
  279. tool_editmode[i]->set_text(label[i]);
  280. tool_editmode[i]->set_toggle_mode(true);
  281. tool_editmode[i]->set_button_group(g);
  282. Vector<Variant> args;
  283. args.push_back(i);
  284. tool_editmode[i]->connect("pressed", this, "_on_edit_mode_changed", args);
  285. tool_hb->add_child(tool_editmode[i]);
  286. }
  287. tool_editmode[EDITMODE_ICON]->set_pressed(true);
  288. main_vb->add_child(tool_hb);
  289. main_vb->add_child(memnew(HSeparator));
  290. toolbar = memnew(HBoxContainer);
  291. for (int i = 0; i < (int)TOOLBAR_MAX; i++) {
  292. tool_containers[i] = memnew(HBoxContainer);
  293. toolbar->add_child(tool_containers[i]);
  294. tool_containers[i]->hide();
  295. }
  296. Ref<ButtonGroup> tg(memnew(ButtonGroup));
  297. tools[TOOL_SELECT] = memnew(ToolButton);
  298. tool_containers[TOOLBAR_DUMMY]->add_child(tools[TOOL_SELECT]);
  299. tools[TOOL_SELECT]->set_tooltip("Select sub-tile to use as icon, this will be also used on invalid autotile bindings.");
  300. tools[TOOL_SELECT]->set_toggle_mode(true);
  301. tools[TOOL_SELECT]->set_button_group(tg);
  302. tools[TOOL_SELECT]->set_pressed(true);
  303. tool_containers[TOOLBAR_DUMMY]->show();
  304. Vector<Variant> p;
  305. tools[BITMASK_COPY] = memnew(ToolButton);
  306. p.push_back((int)BITMASK_COPY);
  307. tools[BITMASK_COPY]->connect("pressed", this, "_on_tool_clicked", p);
  308. tool_containers[TOOLBAR_BITMASK]->add_child(tools[BITMASK_COPY]);
  309. tools[BITMASK_PASTE] = memnew(ToolButton);
  310. p = Vector<Variant>();
  311. p.push_back((int)BITMASK_PASTE);
  312. tools[BITMASK_PASTE]->connect("pressed", this, "_on_tool_clicked", p);
  313. tool_containers[TOOLBAR_BITMASK]->add_child(tools[BITMASK_PASTE]);
  314. tools[BITMASK_CLEAR] = memnew(ToolButton);
  315. p = Vector<Variant>();
  316. p.push_back((int)BITMASK_CLEAR);
  317. tools[BITMASK_CLEAR]->connect("pressed", this, "_on_tool_clicked", p);
  318. tool_containers[TOOLBAR_BITMASK]->add_child(tools[BITMASK_CLEAR]);
  319. tools[SHAPE_NEW_POLYGON] = memnew(ToolButton);
  320. tool_containers[TOOLBAR_SHAPE]->add_child(tools[SHAPE_NEW_POLYGON]);
  321. tools[SHAPE_NEW_POLYGON]->set_toggle_mode(true);
  322. tools[SHAPE_NEW_POLYGON]->set_button_group(tg);
  323. tool_containers[TOOLBAR_SHAPE]->add_child(memnew(VSeparator));
  324. tools[SHAPE_DELETE] = memnew(ToolButton);
  325. p = Vector<Variant>();
  326. p.push_back((int)SHAPE_DELETE);
  327. tools[SHAPE_DELETE]->connect("pressed", this, "_on_tool_clicked", p);
  328. tool_containers[TOOLBAR_SHAPE]->add_child(tools[SHAPE_DELETE]);
  329. //tools[SHAPE_CREATE_FROM_NOT_BITMASKED] = memnew(ToolButton);
  330. //tool_containers[TOOLBAR_SHAPE]->add_child(tools[SHAPE_CREATE_FROM_NOT_BITMASKED]);
  331. tool_containers[TOOLBAR_SHAPE]->add_change_receptor(memnew(VSeparator));
  332. tools[SHAPE_KEEP_INSIDE_TILE] = memnew(ToolButton);
  333. tools[SHAPE_KEEP_INSIDE_TILE]->set_toggle_mode(true);
  334. tools[SHAPE_KEEP_INSIDE_TILE]->set_pressed(true);
  335. tool_containers[TOOLBAR_SHAPE]->add_child(tools[SHAPE_KEEP_INSIDE_TILE]);
  336. tools[SHAPE_SNAP_TO_BITMASK_GRID] = memnew(ToolButton);
  337. tools[SHAPE_SNAP_TO_BITMASK_GRID]->set_toggle_mode(true);
  338. tools[SHAPE_SNAP_TO_BITMASK_GRID]->set_pressed(true);
  339. tool_containers[TOOLBAR_SHAPE]->add_child(tools[SHAPE_SNAP_TO_BITMASK_GRID]);
  340. spin_priority = memnew(SpinBox);
  341. spin_priority->set_min(1);
  342. spin_priority->set_max(255);
  343. spin_priority->set_step(1);
  344. spin_priority->set_custom_minimum_size(Size2(100, 0));
  345. spin_priority->connect("value_changed", this, "_on_priority_changed");
  346. spin_priority->hide();
  347. toolbar->add_child(spin_priority);
  348. Control *separator = memnew(Control);
  349. separator->set_h_size_flags(SIZE_EXPAND_FILL);
  350. toolbar->add_child(separator);
  351. tools[ZOOM_OUT] = memnew(ToolButton);
  352. p = Vector<Variant>();
  353. p.push_back((int)ZOOM_OUT);
  354. tools[ZOOM_OUT]->connect("pressed", this, "_on_tool_clicked", p);
  355. toolbar->add_child(tools[ZOOM_OUT]);
  356. tools[ZOOM_1] = memnew(ToolButton);
  357. p = Vector<Variant>();
  358. p.push_back((int)ZOOM_1);
  359. tools[ZOOM_1]->connect("pressed", this, "_on_tool_clicked", p);
  360. toolbar->add_child(tools[ZOOM_1]);
  361. tools[ZOOM_IN] = memnew(ToolButton);
  362. p = Vector<Variant>();
  363. p.push_back((int)ZOOM_IN);
  364. tools[ZOOM_IN]->connect("pressed", this, "_on_tool_clicked", p);
  365. toolbar->add_child(tools[ZOOM_IN]);
  366. main_vb->add_child(toolbar);
  367. ScrollContainer *scroll = memnew(ScrollContainer);
  368. main_vb->add_child(scroll);
  369. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  370. workspace_container = memnew(Control);
  371. scroll->add_child(workspace_container);
  372. workspace = memnew(Control);
  373. workspace->connect("draw", this, "_on_workspace_draw");
  374. workspace->connect("gui_input", this, "_on_workspace_input");
  375. workspace_container->add_child(workspace);
  376. preview = memnew(Sprite);
  377. workspace->add_child(preview);
  378. preview->set_centered(false);
  379. preview->set_draw_behind_parent(true);
  380. preview->set_region(true);
  381. }
  382. void AutotileEditor::_bind_methods() {
  383. ClassDB::bind_method("_on_autotile_selected", &AutotileEditor::_on_autotile_selected);
  384. ClassDB::bind_method("_on_edit_mode_changed", &AutotileEditor::_on_edit_mode_changed);
  385. ClassDB::bind_method("_on_workspace_draw", &AutotileEditor::_on_workspace_draw);
  386. ClassDB::bind_method("_on_workspace_input", &AutotileEditor::_on_workspace_input);
  387. ClassDB::bind_method("_on_tool_clicked", &AutotileEditor::_on_tool_clicked);
  388. ClassDB::bind_method("_on_priority_changed", &AutotileEditor::_on_priority_changed);
  389. }
  390. void AutotileEditor::_notification(int p_what) {
  391. if (p_what == NOTIFICATION_ENTER_TREE) {
  392. tools[TOOL_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  393. tools[BITMASK_COPY]->set_icon(get_icon("Duplicate", "EditorIcons"));
  394. tools[BITMASK_PASTE]->set_icon(get_icon("Override", "EditorIcons"));
  395. tools[BITMASK_CLEAR]->set_icon(get_icon("Clear", "EditorIcons"));
  396. tools[SHAPE_NEW_POLYGON]->set_icon(get_icon("CollisionPolygon2D", "EditorIcons"));
  397. tools[SHAPE_DELETE]->set_icon(get_icon("Remove", "EditorIcons"));
  398. tools[SHAPE_KEEP_INSIDE_TILE]->set_icon(get_icon("Snap", "EditorIcons"));
  399. tools[SHAPE_SNAP_TO_BITMASK_GRID]->set_icon(get_icon("SnapGrid", "EditorIcons"));
  400. tools[ZOOM_OUT]->set_icon(get_icon("ZoomLess", "EditorIcons"));
  401. tools[ZOOM_1]->set_icon(get_icon("ZoomReset", "EditorIcons"));
  402. tools[ZOOM_IN]->set_icon(get_icon("ZoomMore", "EditorIcons"));
  403. }
  404. }
  405. void AutotileEditor::_on_autotile_selected(int p_index) {
  406. if (get_current_tile() >= 0) {
  407. current_item_index = p_index;
  408. preview->set_texture(tile_set->tile_get_texture(get_current_tile()));
  409. preview->set_region_rect(tile_set->tile_get_region(get_current_tile()));
  410. workspace->set_custom_minimum_size(tile_set->tile_get_region(get_current_tile()).size);
  411. } else {
  412. current_item_index = -1;
  413. preview->set_texture(NULL);
  414. workspace->set_custom_minimum_size(Size2i());
  415. }
  416. helper->_change_notify("");
  417. workspace->update();
  418. }
  419. void AutotileEditor::_on_edit_mode_changed(int p_edit_mode) {
  420. edit_mode = (EditMode)p_edit_mode;
  421. switch (edit_mode) {
  422. case EDITMODE_BITMASK: {
  423. tool_containers[TOOLBAR_DUMMY]->show();
  424. tool_containers[TOOLBAR_BITMASK]->show();
  425. tool_containers[TOOLBAR_SHAPE]->hide();
  426. tools[TOOL_SELECT]->set_pressed(true);
  427. tools[TOOL_SELECT]->set_tooltip("LMB: set bit on.\nRMB: set bit off.");
  428. spin_priority->hide();
  429. } break;
  430. case EDITMODE_COLLISION:
  431. case EDITMODE_NAVIGATION:
  432. case EDITMODE_OCCLUSION: {
  433. tool_containers[TOOLBAR_DUMMY]->show();
  434. tool_containers[TOOLBAR_BITMASK]->hide();
  435. tool_containers[TOOLBAR_SHAPE]->show();
  436. tools[TOOL_SELECT]->set_tooltip("Select current edited sub-tile.");
  437. spin_priority->hide();
  438. } break;
  439. default: {
  440. tool_containers[TOOLBAR_DUMMY]->show();
  441. tool_containers[TOOLBAR_BITMASK]->hide();
  442. tool_containers[TOOLBAR_SHAPE]->hide();
  443. if (edit_mode == EDITMODE_ICON) {
  444. tools[TOOL_SELECT]->set_tooltip("Select sub-tile to use as icon, this will be also used on invalid autotile bindings.");
  445. spin_priority->hide();
  446. } else {
  447. tools[TOOL_SELECT]->set_tooltip("Select sub-tile to change it's priority.");
  448. spin_priority->show();
  449. }
  450. } break;
  451. }
  452. workspace->update();
  453. }
  454. void AutotileEditor::_on_workspace_draw() {
  455. if (get_current_tile() >= 0 && !tile_set.is_null()) {
  456. int spacing = tile_set->autotile_get_spacing(get_current_tile());
  457. Vector2 size = tile_set->autotile_get_size(get_current_tile());
  458. Rect2i region = tile_set->tile_get_region(get_current_tile());
  459. Color c(0.347214, 0.722656, 0.617063);
  460. switch (edit_mode) {
  461. case EDITMODE_ICON: {
  462. Vector2 coord = tile_set->autotile_get_icon_coordinate(get_current_tile());
  463. draw_highlight_tile(coord);
  464. } break;
  465. case EDITMODE_BITMASK: {
  466. c = Color(1, 0, 0, 0.5);
  467. for (float x = 0; x < region.size.x / (spacing + size.x); x++) {
  468. for (float y = 0; y < region.size.y / (spacing + size.y); y++) {
  469. Vector2 coord(x, y);
  470. Point2 anchor(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  471. uint16_t mask = tile_set->autotile_get_bitmask(get_current_tile(), coord);
  472. if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  473. if (mask & TileSet::BIND_TOPLEFT) {
  474. workspace->draw_rect(Rect2(anchor, size / 2), c);
  475. }
  476. if (mask & TileSet::BIND_TOPRIGHT) {
  477. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, 0), size / 2), c);
  478. }
  479. if (mask & TileSet::BIND_BOTTOMLEFT) {
  480. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 2), size / 2), c);
  481. }
  482. if (mask & TileSet::BIND_BOTTOMRIGHT) {
  483. workspace->draw_rect(Rect2(anchor + size / 2, size / 2), c);
  484. }
  485. } else if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_3X3) {
  486. if (mask & TileSet::BIND_TOPLEFT) {
  487. workspace->draw_rect(Rect2(anchor, size / 3), c);
  488. }
  489. if (mask & TileSet::BIND_TOP) {
  490. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, 0), size / 3), c);
  491. }
  492. if (mask & TileSet::BIND_TOPRIGHT) {
  493. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, 0), size / 3), c);
  494. }
  495. if (mask & TileSet::BIND_LEFT) {
  496. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 3), size / 3), c);
  497. }
  498. if (mask & TileSet::BIND_CENTER) {
  499. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, size.y / 3), size / 3), c);
  500. }
  501. if (mask & TileSet::BIND_RIGHT) {
  502. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, size.y / 3), size / 3), c);
  503. }
  504. if (mask & TileSet::BIND_BOTTOMLEFT) {
  505. workspace->draw_rect(Rect2(anchor + Vector2(0, (size.y / 3) * 2), size / 3), c);
  506. }
  507. if (mask & TileSet::BIND_BOTTOM) {
  508. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, (size.y / 3) * 2), size / 3), c);
  509. }
  510. if (mask & TileSet::BIND_BOTTOMRIGHT) {
  511. workspace->draw_rect(Rect2(anchor + (size / 3) * 2, size / 3), c);
  512. }
  513. }
  514. }
  515. }
  516. } break;
  517. case EDITMODE_COLLISION:
  518. case EDITMODE_OCCLUSION:
  519. case EDITMODE_NAVIGATION: {
  520. Vector2 coord = edited_shape_coord;
  521. draw_highlight_tile(coord);
  522. draw_polygon_shapes();
  523. } break;
  524. case EDITMODE_PRIORITY: {
  525. spin_priority->set_value(tile_set->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord));
  526. uint16_t mask = tile_set->autotile_get_bitmask(get_current_tile(), edited_shape_coord);
  527. Vector<Vector2> queue_others;
  528. int total = 0;
  529. for (Map<Vector2, uint16_t>::Element *E = tile_set->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  530. if (E->value() == mask) {
  531. total += tile_set->autotile_get_subtile_priority(get_current_tile(), E->key());
  532. if (E->key() != edited_shape_coord) {
  533. queue_others.push_back(E->key());
  534. }
  535. }
  536. }
  537. spin_priority->set_suffix(" / " + String::num(total, 0));
  538. draw_highlight_tile(edited_shape_coord, queue_others);
  539. } break;
  540. }
  541. float j = -size.x; //make sure to draw at 0
  542. while (j < region.size.x) {
  543. j += size.x;
  544. if (spacing <= 0) {
  545. workspace->draw_line(Point2(j, 0), Point2(j, region.size.y), c);
  546. } else {
  547. workspace->draw_rect(Rect2(Point2(j, 0), Size2(spacing, region.size.y)), c);
  548. }
  549. j += spacing;
  550. }
  551. j = -size.y; //make sure to draw at 0
  552. while (j < region.size.y) {
  553. j += size.y;
  554. if (spacing <= 0) {
  555. workspace->draw_line(Point2(0, j), Point2(region.size.x, j), c);
  556. } else {
  557. workspace->draw_rect(Rect2(Point2(0, j), Size2(region.size.x, spacing)), c);
  558. }
  559. j += spacing;
  560. }
  561. }
  562. }
  563. #define MIN_DISTANCE_SQUARED 10
  564. void AutotileEditor::_on_workspace_input(const Ref<InputEvent> &p_ie) {
  565. if (get_current_tile() >= 0 && !tile_set.is_null()) {
  566. Ref<InputEventMouseButton> mb = p_ie;
  567. Ref<InputEventMouseMotion> mm = p_ie;
  568. static bool dragging;
  569. static bool erasing;
  570. int spacing = tile_set->autotile_get_spacing(get_current_tile());
  571. Vector2 size = tile_set->autotile_get_size(get_current_tile());
  572. switch (edit_mode) {
  573. case EDITMODE_ICON: {
  574. if (mb.is_valid()) {
  575. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  576. Vector2 coord((int)(mb->get_position().x / (spacing + size.x)), (int)(mb->get_position().y / (spacing + size.y)));
  577. tile_set->autotile_set_icon_coordinate(get_current_tile(), coord);
  578. Rect2 region = tile_set->tile_get_region(get_current_tile());
  579. region.size = size;
  580. coord.x *= (spacing + size.x);
  581. coord.y *= (spacing + size.y);
  582. region.position += coord;
  583. autotile_list->set_item_icon_region(current_item_index, region);
  584. workspace->update();
  585. }
  586. }
  587. } break;
  588. case EDITMODE_BITMASK: {
  589. if (mb.is_valid()) {
  590. if (mb->is_pressed()) {
  591. if (dragging) {
  592. return;
  593. }
  594. if (mb->get_button_index() == BUTTON_RIGHT || mb->get_button_index() == BUTTON_LEFT) {
  595. dragging = true;
  596. erasing = (mb->get_button_index() == BUTTON_RIGHT);
  597. Vector2 coord((int)(mb->get_position().x / (spacing + size.x)), (int)(mb->get_position().y / (spacing + size.y)));
  598. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  599. pos = mb->get_position() - pos;
  600. uint16_t bit;
  601. if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  602. if (pos.x < size.x / 2) {
  603. if (pos.y < size.y / 2) {
  604. bit = TileSet::BIND_TOPLEFT;
  605. } else {
  606. bit = TileSet::BIND_BOTTOMLEFT;
  607. }
  608. } else {
  609. if (pos.y < size.y / 2) {
  610. bit = TileSet::BIND_TOPRIGHT;
  611. } else {
  612. bit = TileSet::BIND_BOTTOMRIGHT;
  613. }
  614. }
  615. } else if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_3X3) {
  616. if (pos.x < size.x / 3) {
  617. if (pos.y < size.y / 3) {
  618. bit = TileSet::BIND_TOPLEFT;
  619. } else if (pos.y > (size.y / 3) * 2) {
  620. bit = TileSet::BIND_BOTTOMLEFT;
  621. } else {
  622. bit = TileSet::BIND_LEFT;
  623. }
  624. } else if (pos.x > (size.x / 3) * 2) {
  625. if (pos.y < size.y / 3) {
  626. bit = TileSet::BIND_TOPRIGHT;
  627. } else if (pos.y > (size.y / 3) * 2) {
  628. bit = TileSet::BIND_BOTTOMRIGHT;
  629. } else {
  630. bit = TileSet::BIND_RIGHT;
  631. }
  632. } else {
  633. if (pos.y < size.y / 3) {
  634. bit = TileSet::BIND_TOP;
  635. } else if (pos.y > (size.y / 3) * 2) {
  636. bit = TileSet::BIND_BOTTOM;
  637. } else {
  638. bit = TileSet::BIND_CENTER;
  639. }
  640. }
  641. }
  642. uint16_t mask = tile_set->autotile_get_bitmask(get_current_tile(), coord);
  643. if (erasing) {
  644. mask &= ~bit;
  645. } else {
  646. mask |= bit;
  647. }
  648. tile_set->autotile_set_bitmask(get_current_tile(), coord, mask);
  649. workspace->update();
  650. }
  651. } else {
  652. if ((erasing && mb->get_button_index() == BUTTON_RIGHT) || (!erasing && mb->get_button_index() == BUTTON_LEFT)) {
  653. dragging = false;
  654. erasing = false;
  655. }
  656. }
  657. }
  658. if (mm.is_valid()) {
  659. if (dragging) {
  660. Vector2 coord((int)(mm->get_position().x / (spacing + size.x)), (int)(mm->get_position().y / (spacing + size.y)));
  661. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  662. pos = mm->get_position() - pos;
  663. uint16_t bit;
  664. if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  665. if (pos.x < size.x / 2) {
  666. if (pos.y < size.y / 2) {
  667. bit = TileSet::BIND_TOPLEFT;
  668. } else {
  669. bit = TileSet::BIND_BOTTOMLEFT;
  670. }
  671. } else {
  672. if (pos.y < size.y / 2) {
  673. bit = TileSet::BIND_TOPRIGHT;
  674. } else {
  675. bit = TileSet::BIND_BOTTOMRIGHT;
  676. }
  677. }
  678. } else if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_3X3) {
  679. if (pos.x < size.x / 3) {
  680. if (pos.y < size.y / 3) {
  681. bit = TileSet::BIND_TOPLEFT;
  682. } else if (pos.y > (size.y / 3) * 2) {
  683. bit = TileSet::BIND_BOTTOMLEFT;
  684. } else {
  685. bit = TileSet::BIND_LEFT;
  686. }
  687. } else if (pos.x > (size.x / 3) * 2) {
  688. if (pos.y < size.y / 3) {
  689. bit = TileSet::BIND_TOPRIGHT;
  690. } else if (pos.y > (size.y / 3) * 2) {
  691. bit = TileSet::BIND_BOTTOMRIGHT;
  692. } else {
  693. bit = TileSet::BIND_RIGHT;
  694. }
  695. } else {
  696. if (pos.y < size.y / 3) {
  697. bit = TileSet::BIND_TOP;
  698. } else if (pos.y > (size.y / 3) * 2) {
  699. bit = TileSet::BIND_BOTTOM;
  700. } else {
  701. bit = TileSet::BIND_CENTER;
  702. }
  703. }
  704. }
  705. uint16_t mask = tile_set->autotile_get_bitmask(get_current_tile(), coord);
  706. if (erasing) {
  707. mask &= ~bit;
  708. } else {
  709. mask |= bit;
  710. }
  711. tile_set->autotile_set_bitmask(get_current_tile(), coord, mask);
  712. workspace->update();
  713. }
  714. }
  715. } break;
  716. case EDITMODE_COLLISION:
  717. case EDITMODE_OCCLUSION:
  718. case EDITMODE_NAVIGATION:
  719. case EDITMODE_PRIORITY: {
  720. Vector2 shape_anchor = edited_shape_coord;
  721. shape_anchor.x *= (size.x + spacing);
  722. shape_anchor.y *= (size.y + spacing);
  723. if (tools[TOOL_SELECT]->is_pressed()) {
  724. if (mb.is_valid()) {
  725. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  726. if (edit_mode != EDITMODE_PRIORITY && current_shape.size() > 0) {
  727. for (int i = 0; i < current_shape.size(); i++) {
  728. if ((current_shape[i] - mb->get_position()).length_squared() <= MIN_DISTANCE_SQUARED) {
  729. dragging_point = i;
  730. workspace->update();
  731. return;
  732. }
  733. }
  734. }
  735. Vector2 coord((int)(mb->get_position().x / (spacing + size.x)), (int)(mb->get_position().y / (spacing + size.y)));
  736. if (edited_shape_coord != coord) {
  737. edited_shape_coord = coord;
  738. edited_occlusion_shape = tile_set->autotile_get_light_occluder(get_current_tile(), edited_shape_coord);
  739. edited_navigation_shape = tile_set->autotile_get_navigation_polygon(get_current_tile(), edited_shape_coord);
  740. shape_anchor = edited_shape_coord;
  741. shape_anchor.x *= (size.x + spacing);
  742. shape_anchor.y *= (size.y + spacing);
  743. if (edit_mode == EDITMODE_OCCLUSION) {
  744. current_shape.resize(0);
  745. if (edited_occlusion_shape.is_valid()) {
  746. for (int i = 0; i < edited_occlusion_shape->get_polygon().size(); i++) {
  747. current_shape.push_back(edited_occlusion_shape->get_polygon()[i] + shape_anchor);
  748. }
  749. }
  750. } else if (edit_mode == EDITMODE_NAVIGATION) {
  751. current_shape.resize(0);
  752. if (edited_navigation_shape.is_valid()) {
  753. if (edited_navigation_shape->get_polygon_count() > 0) {
  754. PoolVector<Vector2> vertices = edited_navigation_shape->get_vertices();
  755. for (int i = 0; i < edited_navigation_shape->get_polygon(0).size(); i++) {
  756. current_shape.push_back(vertices[edited_navigation_shape->get_polygon(0)[i]] + shape_anchor);
  757. }
  758. }
  759. }
  760. }
  761. } else {
  762. if (edit_mode == EDITMODE_COLLISION) {
  763. Vector<TileSet::ShapeData> sd = tile_set->tile_get_shapes(get_current_tile());
  764. for (int i = 0; i < sd.size(); i++) {
  765. if (sd[i].autotile_coord == coord) {
  766. Ref<ConcavePolygonShape2D> shape = sd[i].shape;
  767. if (shape.is_valid()) {
  768. //FIXME: i need a way to know if the point is countained on the polygon instead of the rect
  769. Rect2 bounding_rect;
  770. PoolVector2Array polygon;
  771. bounding_rect.position = shape->get_segments()[0];
  772. for (int j = 0; j < shape->get_segments().size(); j += 2) {
  773. polygon.push_back(shape->get_segments()[j] + shape_anchor);
  774. bounding_rect.expand_to(shape->get_segments()[j] + shape_anchor);
  775. }
  776. if (bounding_rect.has_point(mb->get_position())) {
  777. current_shape = polygon;
  778. edited_collision_shape = shape;
  779. }
  780. }
  781. }
  782. }
  783. }
  784. }
  785. workspace->update();
  786. } else if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  787. if (edit_mode == EDITMODE_COLLISION) {
  788. if (dragging_point >= 0) {
  789. dragging_point = -1;
  790. PoolVector<Vector2> segments;
  791. segments.resize(current_shape.size() * 2);
  792. PoolVector<Vector2>::Write w = segments.write();
  793. for (int i = 0; i < current_shape.size(); i++) {
  794. w[(i << 1) + 0] = current_shape[i] - shape_anchor;
  795. w[(i << 1) + 1] = current_shape[(i + 1) % current_shape.size()] - shape_anchor;
  796. }
  797. w = PoolVector<Vector2>::Write();
  798. edited_collision_shape->set_segments(segments);
  799. workspace->update();
  800. }
  801. } else if (edit_mode == EDITMODE_OCCLUSION) {
  802. if (dragging_point >= 0) {
  803. dragging_point = -1;
  804. PoolVector<Vector2> polygon;
  805. polygon.resize(current_shape.size());
  806. PoolVector<Vector2>::Write w = polygon.write();
  807. for (int i = 0; i < current_shape.size(); i++) {
  808. w[i] = current_shape[i] - shape_anchor;
  809. }
  810. w = PoolVector<Vector2>::Write();
  811. edited_occlusion_shape->set_polygon(polygon);
  812. workspace->update();
  813. }
  814. } else if (edit_mode == EDITMODE_NAVIGATION) {
  815. if (dragging_point >= 0) {
  816. dragging_point = -1;
  817. PoolVector<Vector2> polygon;
  818. Vector<int> indices;
  819. polygon.resize(current_shape.size());
  820. PoolVector<Vector2>::Write w = polygon.write();
  821. for (int i = 0; i < current_shape.size(); i++) {
  822. w[i] = current_shape[i] - shape_anchor;
  823. indices.push_back(i);
  824. }
  825. w = PoolVector<Vector2>::Write();
  826. edited_navigation_shape->set_vertices(polygon);
  827. edited_navigation_shape->add_polygon(indices);
  828. workspace->update();
  829. }
  830. }
  831. }
  832. } else if (mm.is_valid()) {
  833. if (dragging_point >= 0) {
  834. current_shape.set(dragging_point, snap_point(mm->get_position()));
  835. workspace->update();
  836. }
  837. }
  838. } else if (tools[SHAPE_NEW_POLYGON]->is_pressed()) {
  839. if (mb.is_valid()) {
  840. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  841. Vector2 pos = mb->get_position();
  842. pos = snap_point(pos);
  843. if (creating_shape) {
  844. if (current_shape.size() > 0) {
  845. if ((pos - current_shape[0]).length_squared() <= MIN_DISTANCE_SQUARED) {
  846. if (current_shape.size() > 2) {
  847. close_shape(shape_anchor);
  848. workspace->update();
  849. return;
  850. }
  851. }
  852. }
  853. current_shape.push_back(pos);
  854. workspace->update();
  855. } else {
  856. creating_shape = true;
  857. current_shape.resize(0);
  858. current_shape.push_back(snap_point(pos));
  859. }
  860. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  861. if (creating_shape) {
  862. close_shape(shape_anchor);
  863. }
  864. }
  865. } else if (mm.is_valid()) {
  866. if (creating_shape) {
  867. workspace->update();
  868. }
  869. }
  870. }
  871. } break;
  872. }
  873. }
  874. }
  875. void AutotileEditor::_on_tool_clicked(int p_tool) {
  876. if (p_tool == BITMASK_COPY) {
  877. bitmask_map_copy = tile_set->autotile_get_bitmask_map(get_current_tile());
  878. } else if (p_tool == BITMASK_PASTE) {
  879. tile_set->autotile_clear_bitmask_map(get_current_tile());
  880. for (Map<Vector2, uint16_t>::Element *E = bitmask_map_copy.front(); E; E = E->next()) {
  881. tile_set->autotile_set_bitmask(get_current_tile(), E->key(), E->value());
  882. }
  883. workspace->update();
  884. } else if (p_tool == BITMASK_CLEAR) {
  885. tile_set->autotile_clear_bitmask_map(get_current_tile());
  886. workspace->update();
  887. } else if (p_tool == SHAPE_DELETE) {
  888. if (creating_shape) {
  889. creating_shape = false;
  890. current_shape.resize(0);
  891. workspace->update();
  892. } else {
  893. switch (edit_mode) {
  894. case EDITMODE_COLLISION: {
  895. if (!edited_collision_shape.is_null()) {
  896. Vector<TileSet::ShapeData> sd = tile_set->tile_get_shapes(get_current_tile());
  897. int index;
  898. for (int i = 0; i < sd.size(); i++) {
  899. if (sd[i].shape == edited_collision_shape) {
  900. index = i;
  901. break;
  902. }
  903. }
  904. if (index >= 0) {
  905. sd.remove(index);
  906. tile_set->tile_set_shapes(get_current_tile(), sd);
  907. edited_collision_shape = Ref<ConcavePolygonShape2D>();
  908. current_shape.resize(0);
  909. workspace->update();
  910. }
  911. }
  912. } break;
  913. case EDITMODE_NAVIGATION: {
  914. if (!edited_navigation_shape.is_null()) {
  915. tile_set->autotile_set_navigation_polygon(get_current_tile(), Ref<NavigationPolygon>(), edited_shape_coord);
  916. edited_navigation_shape = Ref<NavigationPolygon>();
  917. current_shape.resize(0);
  918. workspace->update();
  919. }
  920. } break;
  921. case EDITMODE_OCCLUSION: {
  922. if (!edited_occlusion_shape.is_null()) {
  923. tile_set->autotile_set_light_occluder(get_current_tile(), Ref<OccluderPolygon2D>(), edited_shape_coord);
  924. edited_occlusion_shape = Ref<OccluderPolygon2D>();
  925. current_shape.resize(0);
  926. workspace->update();
  927. }
  928. } break;
  929. }
  930. }
  931. } else if (p_tool == ZOOM_OUT) {
  932. float scale = workspace->get_scale().x;
  933. if (scale > 0.1) {
  934. scale /= 2;
  935. workspace->set_scale(Vector2(scale, scale));
  936. workspace_container->set_custom_minimum_size(preview->get_region_rect().size * scale);
  937. }
  938. } else if (p_tool == ZOOM_1) {
  939. workspace->set_scale(Vector2(1, 1));
  940. workspace_container->set_custom_minimum_size(preview->get_region_rect().size);
  941. } else if (p_tool == ZOOM_IN) {
  942. float scale = workspace->get_scale().x;
  943. scale *= 2;
  944. workspace->set_scale(Vector2(scale, scale));
  945. workspace_container->set_custom_minimum_size(preview->get_region_rect().size * scale);
  946. }
  947. }
  948. void AutotileEditor::_on_priority_changed(float val) {
  949. tile_set->autotile_set_subtile_priority(get_current_tile(), edited_shape_coord, (int)val);
  950. workspace->update();
  951. }
  952. void AutotileEditor::draw_highlight_tile(Vector2 coord, const Vector<Vector2> &other_highlighted) {
  953. Vector2 size = tile_set->autotile_get_size(get_current_tile());
  954. int spacing = tile_set->autotile_get_spacing(get_current_tile());
  955. Rect2 region = tile_set->tile_get_region(get_current_tile());
  956. coord.x *= (size.x + spacing);
  957. coord.y *= (size.y + spacing);
  958. workspace->draw_rect(Rect2(0, 0, region.size.x, coord.y), Color(0.5, 0.5, 0.5, 0.5));
  959. workspace->draw_rect(Rect2(0, coord.y, coord.x, size.y), Color(0.5, 0.5, 0.5, 0.5));
  960. workspace->draw_rect(Rect2(coord.x + size.x, coord.y, region.size.x - coord.x - size.x, size.y), Color(0.5, 0.5, 0.5, 0.5));
  961. workspace->draw_rect(Rect2(0, coord.y + size.y, region.size.x, region.size.y - size.y - coord.y), Color(0.5, 0.5, 0.5, 0.5));
  962. coord += Vector2(1, 1);
  963. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2)), Color(1, 0, 0), false);
  964. for (int i = 0; i < other_highlighted.size(); i++) {
  965. coord = other_highlighted[i];
  966. coord.x *= (size.x + spacing);
  967. coord.y *= (size.y + spacing);
  968. coord += Vector2(1, 1);
  969. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2)), Color(1, 0, 0), false);
  970. }
  971. }
  972. void AutotileEditor::draw_polygon_shapes() {
  973. int t_id = get_current_tile();
  974. if (t_id < 0)
  975. return;
  976. switch (edit_mode) {
  977. case EDITMODE_COLLISION: {
  978. Vector<TileSet::ShapeData> sd = tile_set->tile_get_shapes(t_id);
  979. for (int i = 0; i < sd.size(); i++) {
  980. Vector2 coord = sd[i].autotile_coord;
  981. Vector2 anchor = tile_set->autotile_get_size(t_id);
  982. anchor.x += tile_set->autotile_get_spacing(t_id);
  983. anchor.y += tile_set->autotile_get_spacing(t_id);
  984. anchor.x *= coord.x;
  985. anchor.y *= coord.y;
  986. Ref<ConcavePolygonShape2D> shape = sd[i].shape;
  987. if (shape.is_valid()) {
  988. Color c_bg;
  989. Color c_border;
  990. if (coord == edited_shape_coord && sd[i].shape == edited_collision_shape) {
  991. c_bg = Color(0, 1, 1, 0.5);
  992. c_border = Color(0, 1, 1);
  993. } else {
  994. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  995. c_border = Color(0.9, 0.7, 0.07, 1);
  996. }
  997. Vector<Vector2> polygon;
  998. Vector<Color> colors;
  999. if (shape == edited_collision_shape) {
  1000. for (int j = 0; j < current_shape.size(); j++) {
  1001. polygon.push_back(current_shape[j]);
  1002. colors.push_back(c_bg);
  1003. }
  1004. } else {
  1005. for (int j = 0; j < shape->get_segments().size(); j += 2) {
  1006. polygon.push_back(shape->get_segments()[j] + anchor);
  1007. colors.push_back(c_bg);
  1008. }
  1009. }
  1010. workspace->draw_polygon(polygon, colors);
  1011. if (coord == edited_shape_coord) {
  1012. for (int j = 0; j < shape->get_segments().size(); j += 2) {
  1013. workspace->draw_line(shape->get_segments()[j] + anchor, shape->get_segments()[j + 1] + anchor, c_border, 1, true);
  1014. }
  1015. if (shape == edited_collision_shape) {
  1016. for (int j = 0; j < current_shape.size(); j++) {
  1017. workspace->draw_circle(current_shape[j], 5, Color(1, 0, 0));
  1018. }
  1019. }
  1020. }
  1021. }
  1022. }
  1023. } break;
  1024. case EDITMODE_OCCLUSION: {
  1025. Map<Vector2, Ref<OccluderPolygon2D> > map = tile_set->autotile_get_light_oclusion_map(t_id);
  1026. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = map.front(); E; E = E->next()) {
  1027. Vector2 coord = E->key();
  1028. Vector2 anchor = tile_set->autotile_get_size(t_id);
  1029. anchor.x += tile_set->autotile_get_spacing(t_id);
  1030. anchor.y += tile_set->autotile_get_spacing(t_id);
  1031. anchor.x *= coord.x;
  1032. anchor.y *= coord.y;
  1033. Ref<OccluderPolygon2D> shape = E->value();
  1034. if (shape.is_valid()) {
  1035. Color c_bg;
  1036. Color c_border;
  1037. if (coord == edited_shape_coord && shape == edited_occlusion_shape) {
  1038. c_bg = Color(0, 1, 1, 0.5);
  1039. c_border = Color(0, 1, 1);
  1040. } else {
  1041. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  1042. c_border = Color(0.9, 0.7, 0.07, 1);
  1043. }
  1044. Vector<Vector2> polygon;
  1045. Vector<Color> colors;
  1046. if (shape == edited_occlusion_shape) {
  1047. for (int j = 0; j < current_shape.size(); j++) {
  1048. polygon.push_back(current_shape[j]);
  1049. colors.push_back(c_bg);
  1050. }
  1051. } else {
  1052. for (int j = 0; j < shape->get_polygon().size(); j++) {
  1053. polygon.push_back(shape->get_polygon()[j] + anchor);
  1054. colors.push_back(c_bg);
  1055. }
  1056. }
  1057. workspace->draw_polygon(polygon, colors);
  1058. if (coord == edited_shape_coord) {
  1059. for (int j = 0; j < shape->get_polygon().size() - 1; j++) {
  1060. workspace->draw_line(shape->get_polygon()[j] + anchor, shape->get_polygon()[j + 1] + anchor, c_border, 1, true);
  1061. }
  1062. workspace->draw_line(shape->get_polygon()[shape->get_polygon().size() - 1] + anchor, shape->get_polygon()[0] + anchor, c_border, 1, true);
  1063. if (shape == edited_occlusion_shape) {
  1064. for (int j = 0; j < current_shape.size(); j++) {
  1065. workspace->draw_circle(current_shape[j], 5, Color(1, 0, 0));
  1066. }
  1067. }
  1068. }
  1069. }
  1070. }
  1071. } break;
  1072. case EDITMODE_NAVIGATION: {
  1073. Map<Vector2, Ref<NavigationPolygon> > map = tile_set->autotile_get_navigation_map(t_id);
  1074. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = map.front(); E; E = E->next()) {
  1075. Vector2 coord = E->key();
  1076. Vector2 anchor = tile_set->autotile_get_size(t_id);
  1077. anchor.x += tile_set->autotile_get_spacing(t_id);
  1078. anchor.y += tile_set->autotile_get_spacing(t_id);
  1079. anchor.x *= coord.x;
  1080. anchor.y *= coord.y;
  1081. Ref<NavigationPolygon> shape = E->value();
  1082. if (shape.is_valid()) {
  1083. Color c_bg;
  1084. Color c_border;
  1085. if (coord == edited_shape_coord && shape == edited_navigation_shape) {
  1086. c_bg = Color(0, 1, 1, 0.5);
  1087. c_border = Color(0, 1, 1);
  1088. } else {
  1089. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  1090. c_border = Color(0.9, 0.7, 0.07, 1);
  1091. }
  1092. Vector<Vector2> polygon;
  1093. Vector<Color> colors;
  1094. if (shape == edited_navigation_shape) {
  1095. for (int j = 0; j < current_shape.size(); j++) {
  1096. polygon.push_back(current_shape[j]);
  1097. colors.push_back(c_bg);
  1098. }
  1099. } else if (shape->get_polygon_count() > 0) {
  1100. PoolVector<Vector2> vertices = shape->get_vertices();
  1101. for (int j = 0; j < shape->get_polygon(0).size(); j++) {
  1102. polygon.push_back(vertices[shape->get_polygon(0)[j]] + anchor);
  1103. colors.push_back(c_bg);
  1104. }
  1105. }
  1106. workspace->draw_polygon(polygon, colors);
  1107. if (coord == edited_shape_coord) {
  1108. if (shape->get_polygon_count() > 0) {
  1109. PoolVector<Vector2> vertices = shape->get_vertices();
  1110. for (int j = 0; j < shape->get_polygon(0).size() - 1; j++) {
  1111. workspace->draw_line(vertices[shape->get_polygon(0)[j]] + anchor, vertices[shape->get_polygon(0)[j + 1]] + anchor, c_border, 1, true);
  1112. }
  1113. if (shape == edited_navigation_shape) {
  1114. for (int j = 0; j < current_shape.size(); j++) {
  1115. workspace->draw_circle(current_shape[j], 5, Color(1, 0, 0));
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. } break;
  1123. }
  1124. if (creating_shape) {
  1125. for (int j = 0; j < current_shape.size() - 1; j++) {
  1126. workspace->draw_line(current_shape[j], current_shape[j + 1], Color(0, 1, 1), 1, true);
  1127. }
  1128. workspace->draw_line(current_shape[current_shape.size() - 1], snap_point(workspace->get_local_mouse_position()), Color(0, 1, 1), 1, true);
  1129. }
  1130. }
  1131. void AutotileEditor::close_shape(const Vector2 &shape_anchor) {
  1132. creating_shape = false;
  1133. if (edit_mode == EDITMODE_COLLISION) {
  1134. Ref<ConcavePolygonShape2D> shape = memnew(ConcavePolygonShape2D);
  1135. PoolVector<Vector2> segments;
  1136. segments.resize(current_shape.size() * 2);
  1137. PoolVector<Vector2>::Write w = segments.write();
  1138. for (int i = 0; i < current_shape.size(); i++) {
  1139. w[(i << 1) + 0] = current_shape[i] - shape_anchor;
  1140. w[(i << 1) + 1] = current_shape[(i + 1) % current_shape.size()] - shape_anchor;
  1141. }
  1142. w = PoolVector<Vector2>::Write();
  1143. shape->set_segments(segments);
  1144. tile_set->tile_add_shape(get_current_tile(), shape, Transform2D(), false, edited_shape_coord);
  1145. edited_collision_shape = shape;
  1146. tools[TOOL_SELECT]->set_pressed(true);
  1147. workspace->update();
  1148. } else if (edit_mode == EDITMODE_OCCLUSION) {
  1149. Ref<OccluderPolygon2D> shape = memnew(OccluderPolygon2D);
  1150. PoolVector<Vector2> polygon;
  1151. polygon.resize(current_shape.size());
  1152. PoolVector<Vector2>::Write w = polygon.write();
  1153. for (int i = 0; i < current_shape.size(); i++) {
  1154. w[i] = current_shape[i] - shape_anchor;
  1155. }
  1156. w = PoolVector<Vector2>::Write();
  1157. shape->set_polygon(polygon);
  1158. tile_set->autotile_set_light_occluder(get_current_tile(), shape, edited_shape_coord);
  1159. edited_occlusion_shape = shape;
  1160. tools[TOOL_SELECT]->set_pressed(true);
  1161. workspace->update();
  1162. } else if (edit_mode == EDITMODE_NAVIGATION) {
  1163. Ref<NavigationPolygon> shape = memnew(NavigationPolygon);
  1164. PoolVector<Vector2> polygon;
  1165. Vector<int> indices;
  1166. polygon.resize(current_shape.size());
  1167. PoolVector<Vector2>::Write w = polygon.write();
  1168. for (int i = 0; i < current_shape.size(); i++) {
  1169. w[i] = current_shape[i] - shape_anchor;
  1170. indices.push_back(i);
  1171. }
  1172. w = PoolVector<Vector2>::Write();
  1173. shape->set_vertices(polygon);
  1174. shape->add_polygon(indices);
  1175. tile_set->autotile_set_navigation_polygon(get_current_tile(), shape, edited_shape_coord);
  1176. edited_navigation_shape = shape;
  1177. tools[TOOL_SELECT]->set_pressed(true);
  1178. workspace->update();
  1179. }
  1180. }
  1181. Vector2 AutotileEditor::snap_point(const Vector2 &point) {
  1182. Vector2 p = point;
  1183. Vector2 coord = edited_shape_coord;
  1184. Vector2 tile_size = tile_set->autotile_get_size(get_current_tile());
  1185. int spacing = tile_set->autotile_get_spacing(get_current_tile());
  1186. Vector2 anchor = coord;
  1187. anchor.x *= (tile_size.x + spacing);
  1188. anchor.y *= (tile_size.y + spacing);
  1189. Rect2 region(anchor, tile_size);
  1190. if (tools[SHAPE_KEEP_INSIDE_TILE]->is_pressed()) {
  1191. if (p.x < region.position.x)
  1192. p.x = region.position.x;
  1193. if (p.y < region.position.y)
  1194. p.y = region.position.y;
  1195. if (p.x > region.position.x + region.size.x)
  1196. p.x = region.position.x + region.size.x;
  1197. if (p.y > region.position.y + region.size.y)
  1198. p.y = region.position.y + region.size.y;
  1199. }
  1200. if (tools[SHAPE_SNAP_TO_BITMASK_GRID]->is_pressed()) {
  1201. Vector2 p2 = p;
  1202. if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  1203. p2.x = Math::stepify(p2.x, tile_size.x / 2);
  1204. p2.y = Math::stepify(p2.y, tile_size.y / 2);
  1205. if ((p2 - p).length_squared() <= MAX(tile_size.y / 4, MIN_DISTANCE_SQUARED)) {
  1206. p = p2;
  1207. }
  1208. } else if (tile_set->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_3X3) {
  1209. p2.x = Math::stepify(p2.x, tile_size.x / 3);
  1210. p2.y = Math::stepify(p2.y, tile_size.y / 3);
  1211. if ((p2 - p).length_squared() <= MAX(tile_size.y / 6, MIN_DISTANCE_SQUARED)) {
  1212. p = p2;
  1213. }
  1214. }
  1215. }
  1216. p.floor();
  1217. return p;
  1218. }
  1219. void AutotileEditor::edit(Object *p_node) {
  1220. tile_set = Ref<TileSet>(Object::cast_to<TileSet>(p_node));
  1221. helper->set_tileset(tile_set);
  1222. autotile_list->clear();
  1223. List<int> ids;
  1224. tile_set->get_tile_list(&ids);
  1225. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  1226. if (tile_set->tile_get_is_autotile(E->get())) {
  1227. autotile_list->add_item(tile_set->tile_get_name(E->get()));
  1228. autotile_list->set_item_metadata(autotile_list->get_item_count() - 1, E->get());
  1229. autotile_list->set_item_icon(autotile_list->get_item_count() - 1, tile_set->tile_get_texture(E->get()));
  1230. Rect2 region = tile_set->tile_get_region(E->get());
  1231. region.size = tile_set->autotile_get_size(E->get());
  1232. Vector2 pos = tile_set->autotile_get_icon_coordinate(E->get());
  1233. pos.x *= (tile_set->autotile_get_spacing(E->get()) + region.size.x);
  1234. pos.y *= (tile_set->autotile_get_spacing(E->get()) + region.size.y);
  1235. region.position += pos;
  1236. autotile_list->set_item_icon_region(autotile_list->get_item_count() - 1, region);
  1237. }
  1238. }
  1239. if (autotile_list->get_item_count() > 0) {
  1240. autotile_list->select(0);
  1241. _on_autotile_selected(0);
  1242. }
  1243. helper->_change_notify("");
  1244. }
  1245. int AutotileEditor::get_current_tile() {
  1246. if (autotile_list->get_selected_items().size() == 0)
  1247. return -1;
  1248. else
  1249. return autotile_list->get_item_metadata(autotile_list->get_selected_items()[0]);
  1250. }
  1251. void AutotileEditorHelper::set_tileset(const Ref<TileSet> &p_tileset) {
  1252. tile_set = p_tileset;
  1253. }
  1254. bool AutotileEditorHelper::_set(const StringName &p_name, const Variant &p_value) {
  1255. if (autotile_editor->get_current_tile() < 0 || tile_set.is_null())
  1256. return false;
  1257. String name = p_name.operator String();
  1258. bool v = false;
  1259. if (name == "bitmask_mode") {
  1260. tile_set->set(String::num(autotile_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", p_value, &v);
  1261. } else if (name.left(7) == "layout/") {
  1262. tile_set->set(String::num(autotile_editor->get_current_tile(), 0) + "/autotile" + name.right(6), p_value, &v);
  1263. }
  1264. if (v) {
  1265. tile_set->_change_notify("");
  1266. autotile_editor->workspace->update();
  1267. }
  1268. return v;
  1269. }
  1270. bool AutotileEditorHelper::_get(const StringName &p_name, Variant &r_ret) const {
  1271. if (autotile_editor->get_current_tile() < 0 || tile_set.is_null())
  1272. return false;
  1273. String name = p_name.operator String();
  1274. bool v = false;
  1275. if (name == "bitmask_mode") {
  1276. r_ret = tile_set->get(String::num(autotile_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", &v);
  1277. } else if (name.left(7) == "layout/") {
  1278. r_ret = tile_set->get(String::num(autotile_editor->get_current_tile(), 0) + "/autotile" + name.right(6), &v);
  1279. }
  1280. return v;
  1281. }
  1282. void AutotileEditorHelper::_get_property_list(List<PropertyInfo> *p_list) const {
  1283. if (autotile_editor->get_current_tile() < 0 || tile_set.is_null())
  1284. return;
  1285. p_list->push_back(PropertyInfo(Variant::INT, "bitmask_mode", PROPERTY_HINT_ENUM, "2x2,3x3"));
  1286. p_list->push_back(PropertyInfo(Variant::VECTOR2, "layout/tile_size"));
  1287. p_list->push_back(PropertyInfo(Variant::INT, "layout/spacing", PROPERTY_HINT_RANGE, "0,256,1"));
  1288. }
  1289. AutotileEditorHelper::AutotileEditorHelper(AutotileEditor *p_autotile_editor) {
  1290. autotile_editor = p_autotile_editor;
  1291. }