tile_set_editor_plugin.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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-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. #ifndef TILE_SET_EDITOR_PLUGIN_H
  31. #define TILE_SET_EDITOR_PLUGIN_H
  32. #include "editor/editor_node.h"
  33. #include "scene/2d/sprite.h"
  34. #include "scene/resources/concave_polygon_shape_2d.h"
  35. #include "scene/resources/convex_polygon_shape_2d.h"
  36. #include "scene/resources/tile_set.h"
  37. #define WORKSPACE_MARGIN Vector2(10, 10)
  38. class TilesetEditorContext;
  39. class TileSetEditor : public HSplitContainer {
  40. friend class TileSetEditorPlugin;
  41. friend class TilesetEditorContext;
  42. GDCLASS(TileSetEditor, HSplitContainer);
  43. enum TextureToolButtons {
  44. TOOL_TILESET_ADD_TEXTURE,
  45. TOOL_TILESET_REMOVE_TEXTURE,
  46. TOOL_TILESET_CREATE_SCENE,
  47. TOOL_TILESET_MERGE_SCENE,
  48. TOOL_TILESET_MAX
  49. };
  50. enum WorkspaceMode {
  51. WORKSPACE_EDIT,
  52. WORKSPACE_CREATE_SINGLE,
  53. WORKSPACE_CREATE_AUTOTILE,
  54. WORKSPACE_CREATE_ATLAS,
  55. WORKSPACE_MODE_MAX
  56. };
  57. enum EditMode {
  58. EDITMODE_REGION,
  59. EDITMODE_COLLISION,
  60. EDITMODE_OCCLUSION,
  61. EDITMODE_NAVIGATION,
  62. EDITMODE_BITMASK,
  63. EDITMODE_PRIORITY,
  64. EDITMODE_ICON,
  65. EDITMODE_Z_INDEX,
  66. EDITMODE_MAX
  67. };
  68. enum TileSetTools {
  69. SELECT_PREVIOUS,
  70. SELECT_NEXT,
  71. TOOL_SELECT,
  72. BITMASK_COPY,
  73. BITMASK_PASTE,
  74. BITMASK_CLEAR,
  75. SHAPE_NEW_POLYGON,
  76. SHAPE_NEW_RECTANGLE,
  77. SHAPE_TOGGLE_TYPE,
  78. SHAPE_DELETE,
  79. SHAPE_KEEP_INSIDE_TILE,
  80. TOOL_GRID_SNAP,
  81. ZOOM_OUT,
  82. ZOOM_1,
  83. ZOOM_IN,
  84. VISIBLE_INFO,
  85. TOOL_MAX
  86. };
  87. struct SubtileData {
  88. Array collisions;
  89. Ref<OccluderPolygon2D> occlusion_shape;
  90. Ref<NavigationPolygon> navigation_shape;
  91. };
  92. Ref<TileSet> tileset;
  93. TilesetEditorContext *helper;
  94. EditorNode *editor;
  95. UndoRedo *undo_redo;
  96. ConfirmationDialog *cd;
  97. AcceptDialog *err_dialog;
  98. EditorFileDialog *texture_dialog;
  99. ItemList *texture_list;
  100. int option;
  101. ToolButton *tileset_toolbar_buttons[TOOL_TILESET_MAX];
  102. MenuButton *tileset_toolbar_tools;
  103. Map<String, Ref<Texture> > texture_map;
  104. bool creating_shape;
  105. int dragging_point;
  106. bool tile_names_visible;
  107. Vector2 region_from;
  108. Rect2 edited_region;
  109. bool draw_edited_region;
  110. Vector2 edited_shape_coord;
  111. PoolVector2Array current_shape;
  112. Map<Vector2i, SubtileData> current_tile_data;
  113. Map<Vector2, uint32_t> bitmask_map_copy;
  114. Vector2 snap_step;
  115. Vector2 snap_offset;
  116. Vector2 snap_separation;
  117. Ref<Shape2D> edited_collision_shape;
  118. Ref<OccluderPolygon2D> edited_occlusion_shape;
  119. Ref<NavigationPolygon> edited_navigation_shape;
  120. int current_item_index;
  121. Sprite *preview;
  122. ScrollContainer *scroll;
  123. Label *empty_message;
  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. bool _is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const;
  154. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  155. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  156. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  157. void _file_load_request(const PoolVector<String> &p_path, int p_at_pos = -1);
  158. protected:
  159. static void _bind_methods();
  160. void _notification(int p_what);
  161. public:
  162. void edit(const Ref<TileSet> &p_tileset);
  163. static Error update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge = true);
  164. TileSetEditor(EditorNode *p_editor);
  165. ~TileSetEditor();
  166. private:
  167. void _on_tileset_toolbar_button_pressed(int p_index);
  168. void _on_tileset_toolbar_confirm();
  169. void _on_texture_list_selected(int p_index);
  170. void _on_textures_added(const PoolStringArray &p_paths);
  171. void _on_edit_mode_changed(int p_edit_mode);
  172. void _on_workspace_mode_changed(int p_workspace_mode);
  173. void _on_workspace_overlay_draw();
  174. void _on_workspace_draw();
  175. void _on_workspace_process();
  176. void _on_scroll_container_input(const Ref<InputEvent> &p_event);
  177. void _on_workspace_input(const Ref<InputEvent> &p_ie);
  178. void _on_tool_clicked(int p_tool);
  179. void _on_priority_changed(float val);
  180. void _on_z_index_changed(float val);
  181. void _on_grid_snap_toggled(bool p_val);
  182. Vector<Vector2> _get_collision_shape_points(const Ref<Shape2D> &p_shape);
  183. Vector<Vector2> _get_edited_shape_points();
  184. void _set_edited_shape_points(const Vector<Vector2> &points);
  185. void _update_tile_data();
  186. void _update_toggle_shape_button();
  187. void _select_next_tile();
  188. void _select_previous_tile();
  189. Array _get_tiles_in_current_texture(bool sorted = false);
  190. bool _sort_tiles(Variant p_a, Variant p_b);
  191. void _select_next_subtile();
  192. void _select_previous_subtile();
  193. void _select_next_shape();
  194. void _select_previous_shape();
  195. void _set_edited_collision_shape(const Ref<Shape2D> &p_shape);
  196. void _set_snap_step(Vector2 p_val);
  197. void _set_snap_off(Vector2 p_val);
  198. void _set_snap_sep(Vector2 p_val);
  199. void _validate_current_tile_id();
  200. void _select_edited_shape_coord();
  201. void _undo_tile_removal(int p_id);
  202. void _zoom_in();
  203. void _zoom_out();
  204. void _zoom_reset();
  205. void draw_highlight_current_tile();
  206. void draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted = Vector<Vector2>());
  207. void draw_tile_subdivision(int p_id, Color p_color) const;
  208. void draw_edited_region_subdivision() const;
  209. void draw_grid_snap();
  210. void draw_polygon_shapes();
  211. void close_shape(const Vector2 &shape_anchor);
  212. void select_coord(const Vector2 &coord);
  213. Vector2 snap_point(const Vector2 &point);
  214. void update_workspace_tile_mode();
  215. void update_workspace_minsize();
  216. void update_edited_region(const Vector2 &end_point);
  217. int get_grabbed_point(const Vector2 &p_mouse_pos, real_t grab_threshold);
  218. bool is_within_grabbing_distance_of_first_point(const Vector2 &p_pos, real_t p_grab_threshold);
  219. int get_current_tile() const;
  220. void set_current_tile(int p_id);
  221. };
  222. class TilesetEditorContext : public Object {
  223. friend class TileSetEditor;
  224. GDCLASS(TilesetEditorContext, Object);
  225. Ref<TileSet> tileset;
  226. TileSetEditor *tileset_editor;
  227. bool snap_options_visible;
  228. public:
  229. bool _hide_script_from_inspector() { return true; }
  230. void set_tileset(const Ref<TileSet> &p_tileset);
  231. private:
  232. void set_snap_options_visible(bool p_visible);
  233. protected:
  234. bool _set(const StringName &p_name, const Variant &p_value);
  235. bool _get(const StringName &p_name, Variant &r_ret) const;
  236. void _get_property_list(List<PropertyInfo> *p_list) const;
  237. static void _bind_methods();
  238. public:
  239. TilesetEditorContext(TileSetEditor *p_tileset_editor);
  240. };
  241. class TileSetEditorPlugin : public EditorPlugin {
  242. GDCLASS(TileSetEditorPlugin, EditorPlugin);
  243. TileSetEditor *tileset_editor;
  244. Button *tileset_editor_button;
  245. EditorNode *editor;
  246. public:
  247. virtual String get_name() const { return "TileSet"; }
  248. bool has_main_screen() const { return false; }
  249. virtual void edit(Object *p_node);
  250. virtual bool handles(Object *p_node) const;
  251. virtual void make_visible(bool p_visible);
  252. void set_state(const Dictionary &p_state);
  253. Dictionary get_state() const;
  254. TileSetEditorPlugin(EditorNode *p_node);
  255. };
  256. #endif // TILE_SET_EDITOR_PLUGIN_H