tile_set_atlas_source_editor.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**************************************************************************/
  2. /* tile_set_atlas_source_editor.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_ATLAS_SOURCE_EDITOR_H
  31. #define TILE_SET_ATLAS_SOURCE_EDITOR_H
  32. #include "tile_atlas_view.h"
  33. #include "tile_data_editors.h"
  34. #include "scene/gui/split_container.h"
  35. #include "scene/resources/2d/tile_set.h"
  36. class Popup;
  37. class TileSet;
  38. class Tree;
  39. class VSeparator;
  40. class TileSetAtlasSourceEditor : public HSplitContainer {
  41. GDCLASS(TileSetAtlasSourceEditor, HSplitContainer);
  42. public:
  43. // A class to store which tiles are selected.
  44. struct TileSelection {
  45. Vector2i tile = TileSetSource::INVALID_ATLAS_COORDS;
  46. int alternative = TileSetSource::INVALID_TILE_ALTERNATIVE;
  47. bool operator<(const TileSelection &p_other) const {
  48. if (tile == p_other.tile) {
  49. return alternative < p_other.alternative;
  50. } else {
  51. return tile < p_other.tile;
  52. }
  53. }
  54. };
  55. // -- Proxy object for an atlas source, needed by the inspector --
  56. class TileSetAtlasSourceProxyObject : public Object {
  57. GDCLASS(TileSetAtlasSourceProxyObject, Object);
  58. private:
  59. Ref<TileSet> tile_set;
  60. Ref<TileSetAtlasSource> tile_set_atlas_source;
  61. int source_id = TileSet::INVALID_SOURCE;
  62. protected:
  63. bool _set(const StringName &p_name, const Variant &p_value);
  64. bool _get(const StringName &p_name, Variant &r_ret) const;
  65. void _get_property_list(List<PropertyInfo> *p_list) const;
  66. static void _bind_methods();
  67. public:
  68. void set_id(int p_id);
  69. int get_id() const;
  70. void edit(Ref<TileSet> p_tile_set, Ref<TileSetAtlasSource> p_tile_set_atlas_source, int p_source_id);
  71. Ref<TileSetAtlasSource> get_edited() { return tile_set_atlas_source; }
  72. };
  73. // -- Proxy object for a tile, needed by the inspector --
  74. class AtlasTileProxyObject : public Object {
  75. GDCLASS(AtlasTileProxyObject, Object);
  76. private:
  77. TileSetAtlasSourceEditor *tiles_set_atlas_source_editor = nullptr;
  78. Ref<TileSetAtlasSource> tile_set_atlas_source;
  79. RBSet<TileSelection> tiles;
  80. protected:
  81. bool _set(const StringName &p_name, const Variant &p_value);
  82. bool _get(const StringName &p_name, Variant &r_ret) const;
  83. void _get_property_list(List<PropertyInfo> *p_list) const;
  84. static void _bind_methods();
  85. public:
  86. Ref<TileSetAtlasSource> get_edited_tile_set_atlas_source() const { return tile_set_atlas_source; }
  87. RBSet<TileSelection> get_edited_tiles() const { return tiles; }
  88. // Update the proxyed object.
  89. void edit(Ref<TileSetAtlasSource> p_tile_set_atlas_source, const RBSet<TileSelection> &p_tiles = RBSet<TileSelection>());
  90. AtlasTileProxyObject(TileSetAtlasSourceEditor *p_tiles_set_atlas_source_editor) {
  91. tiles_set_atlas_source_editor = p_tiles_set_atlas_source_editor;
  92. }
  93. };
  94. class TileAtlasControl : public Control {
  95. TileSetAtlasSourceEditor *editor = nullptr;
  96. public:
  97. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  98. TileAtlasControl(TileSetAtlasSourceEditor *p_editor) { editor = p_editor; }
  99. };
  100. friend class TileAtlasControl;
  101. private:
  102. bool read_only = false;
  103. Ref<TileSet> tile_set;
  104. TileSetAtlasSource *tile_set_atlas_source = nullptr;
  105. int tile_set_atlas_source_id = TileSet::INVALID_SOURCE;
  106. Ref<Texture2D> atlas_source_texture;
  107. bool tile_set_changed_needs_update = false;
  108. // -- Properties painting --
  109. ScrollContainer *tile_data_editors_scroll = nullptr;
  110. VBoxContainer *tile_data_painting_editor_container = nullptr;
  111. Label *tile_data_editors_label = nullptr;
  112. Button *tile_data_editor_dropdown_button = nullptr;
  113. Popup *tile_data_editors_popup = nullptr;
  114. Tree *tile_data_editors_tree = nullptr;
  115. void _tile_data_editor_dropdown_button_draw();
  116. void _tile_data_editor_dropdown_button_pressed();
  117. // -- Tile data editors --
  118. String current_property;
  119. Control *current_tile_data_editor_toolbar = nullptr;
  120. HashMap<String, TileDataEditor *> tile_data_editors;
  121. TileDataEditor *current_tile_data_editor = nullptr;
  122. void _tile_data_editors_tree_selected();
  123. // -- Inspector --
  124. AtlasTileProxyObject *tile_proxy_object = nullptr;
  125. EditorInspector *tile_inspector = nullptr;
  126. Label *tile_inspector_no_tile_selected_label = nullptr;
  127. String selected_property;
  128. void _inspector_property_selected(const String &p_property);
  129. TileSetAtlasSourceProxyObject *atlas_source_proxy_object = nullptr;
  130. EditorInspector *atlas_source_inspector = nullptr;
  131. // -- Atlas view --
  132. TileAtlasView *tile_atlas_view = nullptr;
  133. VBoxContainer *tile_create_help = nullptr;
  134. // Dragging
  135. enum DragType {
  136. DRAG_TYPE_NONE = 0,
  137. DRAG_TYPE_CREATE_TILES,
  138. DRAG_TYPE_CREATE_TILES_USING_RECT,
  139. DRAG_TYPE_CREATE_BIG_TILE,
  140. DRAG_TYPE_REMOVE_TILES,
  141. DRAG_TYPE_REMOVE_TILES_USING_RECT,
  142. DRAG_TYPE_MOVE_TILE,
  143. DRAG_TYPE_RECT_SELECT,
  144. DRAG_TYPE_MAY_POPUP_MENU,
  145. // WARNING: Keep in this order.
  146. DRAG_TYPE_RESIZE_TOP_LEFT,
  147. DRAG_TYPE_RESIZE_TOP,
  148. DRAG_TYPE_RESIZE_TOP_RIGHT,
  149. DRAG_TYPE_RESIZE_RIGHT,
  150. DRAG_TYPE_RESIZE_BOTTOM_RIGHT,
  151. DRAG_TYPE_RESIZE_BOTTOM,
  152. DRAG_TYPE_RESIZE_BOTTOM_LEFT,
  153. DRAG_TYPE_RESIZE_LEFT,
  154. };
  155. DragType drag_type = DRAG_TYPE_NONE;
  156. Vector2i drag_start_mouse_pos;
  157. Vector2i drag_last_mouse_pos;
  158. Vector2i drag_current_tile;
  159. Rect2i drag_start_tile_shape;
  160. RBSet<Vector2i> drag_modified_tiles;
  161. void _end_dragging();
  162. HashMap<Vector2i, List<const PropertyInfo *>> _group_properties_per_tiles(const List<PropertyInfo> &r_list, const TileSetAtlasSource *p_atlas);
  163. // Popup functions.
  164. enum MenuOptions {
  165. TILE_CREATE,
  166. TILE_CREATE_ALTERNATIVE,
  167. TILE_DELETE,
  168. ADVANCED_AUTO_CREATE_TILES,
  169. ADVANCED_AUTO_REMOVE_TILES,
  170. ADVANCED_CLEANUP_TILES,
  171. };
  172. Vector2i menu_option_coords;
  173. int menu_option_alternative = TileSetSource::INVALID_TILE_ALTERNATIVE;
  174. void _menu_option(int p_option);
  175. // Tool buttons.
  176. Ref<ButtonGroup> tools_button_group;
  177. Button *tool_setup_atlas_source_button = nullptr;
  178. Button *tool_select_button = nullptr;
  179. Button *tool_paint_button = nullptr;
  180. Label *tool_tile_id_label = nullptr;
  181. // Tool settings.
  182. HBoxContainer *tool_settings = nullptr;
  183. HBoxContainer *tool_settings_tile_data_toolbar_container = nullptr;
  184. Button *tools_settings_erase_button = nullptr;
  185. MenuButton *tool_advanced_menu_button = nullptr;
  186. TextureRect *outside_tiles_warning = nullptr;
  187. // Selection.
  188. RBSet<TileSelection> selection;
  189. void _set_selection_from_array(const Array &p_selection);
  190. Array _get_selection_as_array();
  191. // A control on the tile atlas to draw and handle input events.
  192. Vector2i hovered_base_tile_coords = TileSetSource::INVALID_ATLAS_COORDS;
  193. PopupMenu *base_tile_popup_menu = nullptr;
  194. PopupMenu *empty_base_tile_popup_menu = nullptr;
  195. Ref<Texture2D> resize_handle;
  196. Ref<Texture2D> resize_handle_disabled;
  197. Control *tile_atlas_control = nullptr;
  198. Control *tile_atlas_control_unscaled = nullptr;
  199. void _tile_atlas_control_draw();
  200. void _tile_atlas_control_unscaled_draw();
  201. void _tile_atlas_control_mouse_exited();
  202. void _tile_atlas_control_gui_input(const Ref<InputEvent> &p_event);
  203. void _tile_atlas_view_transform_changed();
  204. // A control over the alternative tiles.
  205. Vector3i hovered_alternative_tile_coords = Vector3i(TileSetSource::INVALID_ATLAS_COORDS.x, TileSetSource::INVALID_ATLAS_COORDS.y, TileSetSource::INVALID_TILE_ALTERNATIVE);
  206. PopupMenu *alternative_tile_popup_menu = nullptr;
  207. Control *alternative_tiles_control = nullptr;
  208. Control *alternative_tiles_control_unscaled = nullptr;
  209. void _tile_alternatives_control_draw();
  210. void _tile_alternatives_control_unscaled_draw();
  211. void _tile_alternatives_control_mouse_exited();
  212. void _tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event);
  213. // -- Update functions --
  214. void _update_tile_id_label();
  215. void _update_source_inspector();
  216. void _update_fix_selected_and_hovered_tiles();
  217. void _update_atlas_source_inspector();
  218. void _update_tile_inspector();
  219. void _update_tile_data_editors();
  220. void _update_current_tile_data_editor();
  221. void _update_manage_tile_properties_button();
  222. void _update_atlas_view();
  223. void _update_toolbar();
  224. void _update_buttons();
  225. // -- Misc --
  226. void _auto_create_tiles();
  227. void _auto_remove_tiles();
  228. void _cancel_auto_create_tiles();
  229. AcceptDialog *confirm_auto_create_tiles = nullptr;
  230. Vector<Ref<TileSetAtlasSource>> atlases_to_auto_create_tiles;
  231. Vector2i _get_drag_offset_tile_coords(const Vector2i &p_offset) const;
  232. void _update_source_texture();
  233. void _check_outside_tiles();
  234. void _cleanup_outside_tiles();
  235. void _tile_set_changed();
  236. void _tile_proxy_object_changed(const String &p_what);
  237. void _atlas_source_proxy_object_changed(const String &p_what);
  238. void _undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value);
  239. protected:
  240. void _notification(int p_what);
  241. static void _bind_methods();
  242. // -- input events --
  243. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  244. public:
  245. void edit(Ref<TileSet> p_tile_set, TileSetAtlasSource *p_tile_set_source, int p_source_id);
  246. void init_new_atlases(const Vector<Ref<TileSetAtlasSource>> &p_atlases);
  247. TileSetAtlasSourceEditor();
  248. ~TileSetAtlasSourceEditor();
  249. };
  250. class EditorPropertyTilePolygon : public EditorProperty {
  251. GDCLASS(EditorPropertyTilePolygon, EditorProperty);
  252. StringName count_property;
  253. String element_pattern;
  254. String base_type;
  255. void _add_focusable_children(Node *p_node);
  256. GenericTilePolygonEditor *generic_tile_polygon_editor = nullptr;
  257. void _polygons_changed();
  258. public:
  259. virtual void update_property() override;
  260. void setup_single_mode(const StringName &p_property, const String &p_base_type);
  261. void setup_multiple_mode(const StringName &p_property, const StringName &p_count_property, const String &p_element_pattern, const String &p_base_type);
  262. EditorPropertyTilePolygon();
  263. };
  264. class EditorInspectorPluginTileData : public EditorInspectorPlugin {
  265. GDCLASS(EditorInspectorPluginTileData, EditorInspectorPlugin);
  266. void _occlusion_polygon_set_callback();
  267. void _polygons_changed(Object *p_generic_tile_polygon_editor, Object *p_object, const String &p_path);
  268. public:
  269. virtual bool can_handle(Object *p_object) override;
  270. virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide = false) override;
  271. };
  272. #endif // TILE_SET_ATLAS_SOURCE_EDITOR_H