tile_set_editor_plugin.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*************************************************************************/
  2. /* tile_set_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef TILE_SET_EDITOR_PLUGIN_H
  31. #define TILE_SET_EDITOR_PLUGIN_H
  32. #include "editor/editor_name_dialog.h"
  33. #include "editor/editor_node.h"
  34. #include "scene/2d/sprite.h"
  35. #include "scene/resources/concave_polygon_shape_2d.h"
  36. #include "scene/resources/convex_polygon_shape_2d.h"
  37. #include "scene/resources/tile_set.h"
  38. #define WORKSPACE_MARGIN Vector2(10, 10)
  39. class TilesetEditorContext;
  40. class TileSetEditor : public HSplitContainer {
  41. friend class TileSetEditorPlugin;
  42. friend class TilesetEditorContext;
  43. GDCLASS(TileSetEditor, HSplitContainer)
  44. enum TextureToolButtons {
  45. TOOL_TILESET_ADD_TEXTURE,
  46. TOOL_TILESET_REMOVE_TEXTURE,
  47. TOOL_TILESET_CREATE_SCENE,
  48. TOOL_TILESET_MERGE_SCENE,
  49. TOOL_TILESET_MAX
  50. };
  51. enum WorkspaceMode {
  52. WORKSPACE_EDIT,
  53. WORKSPACE_CREATE_SINGLE,
  54. WORKSPACE_CREATE_AUTOTILE,
  55. WORKSPACE_CREATE_ATLAS,
  56. WORKSPACE_MODE_MAX
  57. };
  58. enum EditMode {
  59. EDITMODE_REGION,
  60. EDITMODE_COLLISION,
  61. EDITMODE_OCCLUSION,
  62. EDITMODE_NAVIGATION,
  63. EDITMODE_BITMASK,
  64. EDITMODE_PRIORITY,
  65. EDITMODE_ICON,
  66. EDITMODE_Z_INDEX,
  67. EDITMODE_MAX
  68. };
  69. enum TileSetTools {
  70. SELECT_PREVIOUS,
  71. SELECT_NEXT,
  72. TOOL_SELECT,
  73. BITMASK_COPY,
  74. BITMASK_PASTE,
  75. BITMASK_CLEAR,
  76. SHAPE_NEW_POLYGON,
  77. SHAPE_NEW_RECTANGLE,
  78. SHAPE_TOGGLE_TYPE,
  79. SHAPE_DELETE,
  80. SHAPE_KEEP_INSIDE_TILE,
  81. TOOL_GRID_SNAP,
  82. ZOOM_OUT,
  83. ZOOM_1,
  84. ZOOM_IN,
  85. VISIBLE_INFO,
  86. TOOL_MAX
  87. };
  88. struct SubtileData {
  89. Array collisions;
  90. Ref<OccluderPolygon2D> occlusion_shape;
  91. Ref<NavigationPolygon> navigation_shape;
  92. };
  93. Ref<TileSet> tileset;
  94. TilesetEditorContext *helper;
  95. EditorNode *editor;
  96. UndoRedo *undo_redo;
  97. ConfirmationDialog *cd;
  98. AcceptDialog *err_dialog;
  99. EditorFileDialog *texture_dialog;
  100. ItemList *texture_list;
  101. int option;
  102. ToolButton *tileset_toolbar_buttons[TOOL_TILESET_MAX];
  103. MenuButton *tileset_toolbar_tools;
  104. Map<RID, Ref<Texture> > texture_map;
  105. bool creating_shape;
  106. int dragging_point;
  107. bool tile_names_visible;
  108. Vector2 region_from;
  109. Rect2 edited_region;
  110. bool draw_edited_region;
  111. Vector2 edited_shape_coord;
  112. PoolVector2Array current_shape;
  113. Map<Vector2i, SubtileData> current_tile_data;
  114. Map<Vector2, uint16_t> bitmask_map_copy;
  115. Vector2 snap_step;
  116. Vector2 snap_offset;
  117. Vector2 snap_separation;
  118. Ref<Shape2D> edited_collision_shape;
  119. Ref<OccluderPolygon2D> edited_occlusion_shape;
  120. Ref<NavigationPolygon> edited_navigation_shape;
  121. int current_item_index;
  122. Sprite *preview;
  123. ScrollContainer *scroll;
  124. Control *workspace_container;
  125. bool draw_handles;
  126. Control *workspace_overlay;
  127. Control *workspace;
  128. Button *tool_workspacemode[WORKSPACE_MODE_MAX];
  129. Button *tool_editmode[EDITMODE_MAX];
  130. HSeparator *separator_editmode;
  131. HBoxContainer *toolbar;
  132. ToolButton *tools[TOOL_MAX];
  133. VSeparator *separator_shape_toggle;
  134. VSeparator *separator_bitmask;
  135. VSeparator *separator_delete;
  136. VSeparator *separator_grid;
  137. SpinBox *spin_priority;
  138. SpinBox *spin_z_index;
  139. WorkspaceMode workspace_mode;
  140. EditMode edit_mode;
  141. int current_tile;
  142. float max_scale;
  143. float min_scale;
  144. float scale_ratio;
  145. void update_texture_list();
  146. void update_texture_list_icon();
  147. void add_texture(Ref<Texture> p_texture);
  148. void remove_texture(Ref<Texture> p_texture);
  149. Ref<Texture> get_current_texture();
  150. static void _import_node(Node *p_node, Ref<TileSet> p_library);
  151. static void _import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge);
  152. void _undo_redo_import_scene(Node *p_scene, bool p_merge);
  153. protected:
  154. static void _bind_methods();
  155. void _notification(int p_what);
  156. public:
  157. void edit(const Ref<TileSet> &p_tileset);
  158. static Error update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge = true);
  159. TileSetEditor(EditorNode *p_editor);
  160. ~TileSetEditor();
  161. private:
  162. void _on_tileset_toolbar_button_pressed(int p_index);
  163. void _on_tileset_toolbar_confirm();
  164. void _on_texture_list_selected(int p_index);
  165. void _on_textures_added(const PoolStringArray &p_paths);
  166. void _on_edit_mode_changed(int p_edit_mode);
  167. void _on_workspace_mode_changed(int p_workspace_mode);
  168. void _on_workspace_overlay_draw();
  169. void _on_workspace_draw();
  170. void _on_workspace_process();
  171. void _on_workspace_input(const Ref<InputEvent> &p_ie);
  172. void _on_tool_clicked(int p_tool);
  173. void _on_priority_changed(float val);
  174. void _on_z_index_changed(float val);
  175. void _on_grid_snap_toggled(bool p_val);
  176. Vector<Vector2> _get_collision_shape_points(const Ref<Shape2D> &p_shape);
  177. Vector<Vector2> _get_edited_shape_points();
  178. void _set_edited_shape_points(const Vector<Vector2> points);
  179. void _update_tile_data();
  180. void _update_toggle_shape_button();
  181. void _select_next_tile();
  182. void _select_previous_tile();
  183. Array _get_tiles_in_current_texture(bool sorted = false);
  184. bool _sort_tiles(Variant p_a, Variant p_b);
  185. void _select_next_subtile();
  186. void _select_previous_subtile();
  187. void _select_next_shape();
  188. void _select_previous_shape();
  189. void _set_edited_collision_shape(const Ref<Shape2D> &p_shape);
  190. void _set_snap_step(Vector2 p_val);
  191. void _set_snap_off(Vector2 p_val);
  192. void _set_snap_sep(Vector2 p_val);
  193. void _validate_current_tile_id();
  194. void _select_edited_shape_coord();
  195. void _undo_tile_removal(int p_id);
  196. void _zoom_in();
  197. void _zoom_out();
  198. void _zoom_reset();
  199. void draw_highlight_current_tile();
  200. void draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted = Vector<Vector2>());
  201. void draw_tile_subdivision(int p_id, Color p_color) const;
  202. void draw_edited_region_subdivision() const;
  203. void draw_grid_snap();
  204. void draw_polygon_shapes();
  205. void close_shape(const Vector2 &shape_anchor);
  206. void select_coord(const Vector2 &coord);
  207. Vector2 snap_point(const Vector2 &point);
  208. void update_workspace_tile_mode();
  209. void update_workspace_minsize();
  210. void update_edited_region(const Vector2 &end_point);
  211. int get_current_tile() const;
  212. void set_current_tile(int p_id);
  213. };
  214. class TilesetEditorContext : public Object {
  215. friend class TileSetEditor;
  216. GDCLASS(TilesetEditorContext, Object);
  217. Ref<TileSet> tileset;
  218. TileSetEditor *tileset_editor;
  219. bool snap_options_visible;
  220. public:
  221. bool _hide_script_from_inspector() { return true; }
  222. void set_tileset(const Ref<TileSet> &p_tileset);
  223. private:
  224. void set_snap_options_visible(bool p_visible);
  225. protected:
  226. bool _set(const StringName &p_name, const Variant &p_value);
  227. bool _get(const StringName &p_name, Variant &r_ret) const;
  228. void _get_property_list(List<PropertyInfo> *p_list) const;
  229. static void _bind_methods();
  230. public:
  231. TilesetEditorContext(TileSetEditor *p_tileset_editor);
  232. };
  233. class TileSetEditorPlugin : public EditorPlugin {
  234. GDCLASS(TileSetEditorPlugin, EditorPlugin);
  235. TileSetEditor *tileset_editor;
  236. Button *tileset_editor_button;
  237. EditorNode *editor;
  238. public:
  239. virtual String get_name() const { return "TileSet"; }
  240. bool has_main_screen() const { return false; }
  241. virtual void edit(Object *p_node);
  242. virtual bool handles(Object *p_node) const;
  243. virtual void make_visible(bool p_visible);
  244. void set_state(const Dictionary &p_state);
  245. Dictionary get_state() const;
  246. TileSetEditorPlugin(EditorNode *p_node);
  247. };
  248. #endif // TILE_SET_EDITOR_PLUGIN_H