tile_set_editor_plugin.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/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. TOOL_SELECT,
  70. BITMASK_COPY,
  71. BITMASK_PASTE,
  72. BITMASK_CLEAR,
  73. SHAPE_NEW_POLYGON,
  74. SHAPE_DELETE,
  75. SHAPE_KEEP_INSIDE_TILE,
  76. TOOL_GRID_SNAP,
  77. ZOOM_OUT,
  78. ZOOM_1,
  79. ZOOM_IN,
  80. VISIBLE_INFO,
  81. TOOL_MAX
  82. };
  83. Ref<TileSet> tileset;
  84. TilesetEditorContext *helper;
  85. EditorNode *editor;
  86. UndoRedo *undo_redo;
  87. ConfirmationDialog *cd;
  88. AcceptDialog *err_dialog;
  89. EditorFileDialog *texture_dialog;
  90. ItemList *texture_list;
  91. int option;
  92. ToolButton *tileset_toolbar_buttons[TOOL_TILESET_MAX];
  93. MenuButton *tileset_toolbar_tools;
  94. Map<RID, Ref<Texture> > texture_map;
  95. bool creating_shape;
  96. int dragging_point;
  97. bool tile_names_visible;
  98. Vector2 region_from;
  99. Rect2 edited_region;
  100. bool draw_edited_region;
  101. Vector2 edited_shape_coord;
  102. PoolVector2Array current_shape;
  103. Map<Vector2, uint16_t> bitmask_map_copy;
  104. Vector2 snap_step;
  105. Vector2 snap_offset;
  106. Vector2 snap_separation;
  107. Ref<ConvexPolygonShape2D> edited_collision_shape;
  108. Ref<OccluderPolygon2D> edited_occlusion_shape;
  109. Ref<NavigationPolygon> edited_navigation_shape;
  110. int current_item_index;
  111. Sprite *preview;
  112. ScrollContainer *scroll;
  113. Control *workspace_container;
  114. bool draw_handles;
  115. Control *workspace_overlay;
  116. Control *workspace;
  117. Button *tool_workspacemode[WORKSPACE_MODE_MAX];
  118. Button *tool_editmode[EDITMODE_MAX];
  119. HSeparator *separator_editmode;
  120. HBoxContainer *toolbar;
  121. ToolButton *tools[TOOL_MAX];
  122. VSeparator *separator_bitmask;
  123. VSeparator *separator_delete;
  124. VSeparator *separator_grid;
  125. SpinBox *spin_priority;
  126. SpinBox *spin_z_index;
  127. WorkspaceMode workspace_mode;
  128. EditMode edit_mode;
  129. int current_tile;
  130. float max_scale;
  131. float min_scale;
  132. float scale_ratio;
  133. void update_texture_list();
  134. void update_texture_list_icon();
  135. void add_texture(Ref<Texture> p_texture);
  136. void remove_texture(Ref<Texture> p_texture);
  137. Ref<Texture> get_current_texture();
  138. static void _import_node(Node *p_node, Ref<TileSet> p_library);
  139. static void _import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge);
  140. void _undo_redo_import_scene(Node *p_scene, bool p_merge);
  141. protected:
  142. static void _bind_methods();
  143. void _notification(int p_what);
  144. public:
  145. void edit(const Ref<TileSet> &p_tileset);
  146. static Error update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge = true);
  147. TileSetEditor(EditorNode *p_editor);
  148. ~TileSetEditor();
  149. private:
  150. void _on_tileset_toolbar_button_pressed(int p_index);
  151. void _on_tileset_toolbar_confirm();
  152. void _on_texture_list_selected(int p_index);
  153. void _on_textures_added(const PoolStringArray &p_paths);
  154. void _on_edit_mode_changed(int p_edit_mode);
  155. void _on_workspace_mode_changed(int p_workspace_mode);
  156. void _on_workspace_overlay_draw();
  157. void _on_workspace_draw();
  158. void _on_workspace_process();
  159. void _on_workspace_input(const Ref<InputEvent> &p_ie);
  160. void _on_tool_clicked(int p_tool);
  161. void _on_priority_changed(float val);
  162. void _on_z_index_changed(float val);
  163. void _on_grid_snap_toggled(bool p_val);
  164. void _set_snap_step(Vector2 p_val);
  165. void _set_snap_off(Vector2 p_val);
  166. void _set_snap_sep(Vector2 p_val);
  167. void _validate_current_tile_id();
  168. void _select_edited_shape_coord();
  169. void _undo_tile_removal(int p_id);
  170. void _zoom_in();
  171. void _zoom_out();
  172. void _zoom_reset();
  173. void draw_highlight_current_tile();
  174. void draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted = Vector<Vector2>());
  175. void draw_tile_subdivision(int p_id, Color p_color) const;
  176. void draw_edited_region_subdivision() const;
  177. void draw_grid_snap();
  178. void draw_polygon_shapes();
  179. void close_shape(const Vector2 &shape_anchor);
  180. void select_coord(const Vector2 &coord);
  181. Vector2 snap_point(const Vector2 &point);
  182. void update_workspace_tile_mode();
  183. void update_edited_region(const Vector2 &end_point);
  184. int get_current_tile() const;
  185. void set_current_tile(int p_id);
  186. };
  187. class TilesetEditorContext : public Object {
  188. friend class TileSetEditor;
  189. GDCLASS(TilesetEditorContext, Object);
  190. Ref<TileSet> tileset;
  191. TileSetEditor *tileset_editor;
  192. bool snap_options_visible;
  193. public:
  194. bool _hide_script_from_inspector() { return true; }
  195. void set_tileset(const Ref<TileSet> &p_tileset);
  196. private:
  197. void set_snap_options_visible(bool p_visible);
  198. protected:
  199. bool _set(const StringName &p_name, const Variant &p_value);
  200. bool _get(const StringName &p_name, Variant &r_ret) const;
  201. void _get_property_list(List<PropertyInfo> *p_list) const;
  202. static void _bind_methods();
  203. public:
  204. TilesetEditorContext(TileSetEditor *p_tileset_editor);
  205. };
  206. class TileSetEditorPlugin : public EditorPlugin {
  207. GDCLASS(TileSetEditorPlugin, EditorPlugin);
  208. TileSetEditor *tileset_editor;
  209. Button *tileset_editor_button;
  210. EditorNode *editor;
  211. public:
  212. virtual String get_name() const { return "TileSet"; }
  213. bool has_main_screen() const { return false; }
  214. virtual void edit(Object *p_node);
  215. virtual bool handles(Object *p_node) const;
  216. virtual void make_visible(bool p_visible);
  217. void set_state(const Dictionary &p_state);
  218. Dictionary get_state() const;
  219. TileSetEditorPlugin(EditorNode *p_node);
  220. };
  221. #endif // TILE_SET_EDITOR_PLUGIN_H