tile_data_editors.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**************************************************************************/
  2. /* tile_data_editors.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_DATA_EDITORS_H
  31. #define TILE_DATA_EDITORS_H
  32. #include "tile_atlas_view.h"
  33. #include "editor/editor_properties.h"
  34. #include "scene/2d/tile_map.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/panel_container.h"
  37. class Label;
  38. class MenuButton;
  39. class SpinBox;
  40. class EditorUndoRedoManager;
  41. class TileDataEditor : public VBoxContainer {
  42. GDCLASS(TileDataEditor, VBoxContainer);
  43. private:
  44. bool _tile_set_changed_update_needed = false;
  45. void _tile_set_changed_plan_update();
  46. void _tile_set_changed_deferred_update();
  47. protected:
  48. Ref<TileSet> tile_set;
  49. TileData *_get_tile_data(TileMapCell p_cell);
  50. virtual void _tile_set_changed() {}
  51. static void _bind_methods();
  52. public:
  53. void set_tile_set(Ref<TileSet> p_tile_set);
  54. // Input to handle painting.
  55. virtual Control *get_toolbar() { return nullptr; }
  56. virtual void forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {}
  57. virtual void forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {}
  58. virtual void forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) {}
  59. virtual void forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) {}
  60. // Used to draw the tile data property value over a tile.
  61. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) {}
  62. };
  63. class DummyObject : public Object {
  64. GDCLASS(DummyObject, Object)
  65. private:
  66. HashMap<String, Variant> properties;
  67. protected:
  68. bool _set(const StringName &p_name, const Variant &p_value);
  69. bool _get(const StringName &p_name, Variant &r_ret) const;
  70. public:
  71. bool has_dummy_property(const StringName &p_name);
  72. void add_dummy_property(const StringName &p_name);
  73. void remove_dummy_property(const StringName &p_name);
  74. void clear_dummy_properties();
  75. };
  76. class GenericTilePolygonEditor : public VBoxContainer {
  77. GDCLASS(GenericTilePolygonEditor, VBoxContainer);
  78. private:
  79. Ref<TileSet> tile_set;
  80. LocalVector<Vector<Point2>> polygons;
  81. bool multiple_polygon_mode = false;
  82. bool use_undo_redo = true;
  83. // UI
  84. int hovered_polygon_index = -1;
  85. int hovered_point_index = -1;
  86. int hovered_segment_index = -1;
  87. Vector2 hovered_segment_point;
  88. enum DragType {
  89. DRAG_TYPE_NONE,
  90. DRAG_TYPE_DRAG_POINT,
  91. DRAG_TYPE_CREATE_POINT,
  92. DRAG_TYPE_PAN,
  93. };
  94. DragType drag_type = DRAG_TYPE_NONE;
  95. int drag_polygon_index = 0;
  96. int drag_point_index = 0;
  97. Vector2 drag_last_pos;
  98. PackedVector2Array drag_old_polygon;
  99. HBoxContainer *toolbar = nullptr;
  100. Ref<ButtonGroup> tools_button_group;
  101. Button *button_expand = nullptr;
  102. Button *button_create = nullptr;
  103. Button *button_edit = nullptr;
  104. Button *button_delete = nullptr;
  105. MenuButton *button_advanced_menu = nullptr;
  106. enum Snap {
  107. SNAP_NONE,
  108. SNAP_HALF_PIXEL,
  109. SNAP_GRID,
  110. };
  111. int current_snap_option = SNAP_HALF_PIXEL;
  112. MenuButton *button_pixel_snap = nullptr;
  113. SpinBox *snap_subdivision = nullptr;
  114. Vector<Point2> in_creation_polygon;
  115. Panel *panel = nullptr;
  116. Control *base_control = nullptr;
  117. EditorZoomWidget *editor_zoom_widget = nullptr;
  118. Button *button_center_view = nullptr;
  119. Vector2 panning;
  120. bool initializing = true;
  121. Ref<TileSetAtlasSource> background_atlas_source;
  122. Vector2i background_atlas_coords;
  123. int background_alternative_id;
  124. Color polygon_color = Color(1.0, 0.0, 0.0);
  125. enum AdvancedMenuOption {
  126. RESET_TO_DEFAULT_TILE,
  127. CLEAR_TILE,
  128. ROTATE_RIGHT,
  129. ROTATE_LEFT,
  130. FLIP_HORIZONTALLY,
  131. FLIP_VERTICALLY,
  132. };
  133. void _base_control_draw();
  134. void _zoom_changed();
  135. void _advanced_menu_item_pressed(int p_item_pressed);
  136. void _center_view();
  137. void _base_control_gui_input(Ref<InputEvent> p_event);
  138. void _set_snap_option(int p_index);
  139. void _store_snap_options();
  140. void _toggle_expand(bool p_expand);
  141. void _snap_to_tile_shape(Point2 &r_point, float &r_current_snapped_dist, float p_snap_dist);
  142. void _snap_point(Point2 &r_point);
  143. void _grab_polygon_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_point_index);
  144. void _grab_polygon_segment_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_segment_index, Vector2 &r_point);
  145. protected:
  146. void _notification(int p_what);
  147. static void _bind_methods();
  148. public:
  149. void set_use_undo_redo(bool p_use_undo_redo);
  150. void set_tile_set(Ref<TileSet> p_tile_set);
  151. void set_background_tile(const TileSetAtlasSource *p_atlas_source, const Vector2 &p_atlas_coords, int p_alternative_id);
  152. int get_polygon_count();
  153. int add_polygon(const Vector<Point2> &p_polygon, int p_index = -1);
  154. void remove_polygon(int p_index);
  155. void clear_polygons();
  156. void set_polygon(int p_polygon_index, const Vector<Point2> &p_polygon);
  157. Vector<Point2> get_polygon(int p_polygon_index);
  158. void set_polygons_color(Color p_color);
  159. void set_multiple_polygon_mode(bool p_multiple_polygon_mode);
  160. GenericTilePolygonEditor();
  161. };
  162. class TileDataDefaultEditor : public TileDataEditor {
  163. GDCLASS(TileDataDefaultEditor, TileDataEditor);
  164. private:
  165. // Toolbar
  166. HBoxContainer *toolbar = memnew(HBoxContainer);
  167. Button *picker_button = nullptr;
  168. // UI
  169. Ref<Texture2D> tile_bool_checked;
  170. Ref<Texture2D> tile_bool_unchecked;
  171. Label *label = nullptr;
  172. EditorProperty *property_editor = nullptr;
  173. // Painting state.
  174. enum DragType {
  175. DRAG_TYPE_NONE = 0,
  176. DRAG_TYPE_PAINT,
  177. DRAG_TYPE_PAINT_RECT,
  178. };
  179. DragType drag_type = DRAG_TYPE_NONE;
  180. Vector2 drag_start_pos;
  181. Vector2 drag_last_pos;
  182. HashMap<TileMapCell, Variant, TileMapCell> drag_modified;
  183. Variant drag_painted_value;
  184. void _property_value_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field);
  185. protected:
  186. DummyObject *dummy_object = memnew(DummyObject);
  187. StringName type;
  188. String property;
  189. Variant::Type property_type;
  190. void _notification(int p_what);
  191. virtual Variant _get_painted_value();
  192. virtual void _set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile);
  193. virtual void _set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value);
  194. virtual Variant _get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile);
  195. virtual void _setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, const HashMap<TileMapCell, Variant, TileMapCell> &p_previous_values, const Variant &p_new_value);
  196. public:
  197. virtual Control *get_toolbar() override { return toolbar; }
  198. virtual void forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) override;
  199. virtual void forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) override;
  200. virtual void forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) override;
  201. virtual void forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) override;
  202. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  203. void setup_property_editor(Variant::Type p_type, const String &p_property, const String &p_label = "", const Variant &p_default_value = Variant());
  204. Variant::Type get_property_type();
  205. TileDataDefaultEditor();
  206. ~TileDataDefaultEditor();
  207. };
  208. class TileDataTextureOriginEditor : public TileDataDefaultEditor {
  209. GDCLASS(TileDataTextureOriginEditor, TileDataDefaultEditor);
  210. public:
  211. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  212. };
  213. class TileDataPositionEditor : public TileDataDefaultEditor {
  214. GDCLASS(TileDataPositionEditor, TileDataDefaultEditor);
  215. public:
  216. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  217. };
  218. class TileDataYSortEditor : public TileDataDefaultEditor {
  219. GDCLASS(TileDataYSortEditor, TileDataDefaultEditor);
  220. public:
  221. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  222. };
  223. class TileDataOcclusionShapeEditor : public TileDataDefaultEditor {
  224. GDCLASS(TileDataOcclusionShapeEditor, TileDataDefaultEditor);
  225. private:
  226. int occlusion_layer = -1;
  227. // UI
  228. GenericTilePolygonEditor *polygon_editor = nullptr;
  229. void _polygon_changed(const PackedVector2Array &p_polygon);
  230. virtual Variant _get_painted_value() override;
  231. virtual void _set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  232. virtual void _set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) override;
  233. virtual Variant _get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  234. virtual void _setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, const HashMap<TileMapCell, Variant, TileMapCell> &p_previous_values, const Variant &p_new_value) override;
  235. protected:
  236. virtual void _tile_set_changed() override;
  237. void _notification(int p_what);
  238. public:
  239. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  240. void set_occlusion_layer(int p_occlusion_layer) { occlusion_layer = p_occlusion_layer; }
  241. TileDataOcclusionShapeEditor();
  242. };
  243. class TileDataCollisionEditor : public TileDataDefaultEditor {
  244. GDCLASS(TileDataCollisionEditor, TileDataDefaultEditor);
  245. int physics_layer = -1;
  246. // UI
  247. GenericTilePolygonEditor *polygon_editor = nullptr;
  248. DummyObject *dummy_object = memnew(DummyObject);
  249. HashMap<StringName, EditorProperty *> property_editors;
  250. void _property_value_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field);
  251. void _property_selected(const StringName &p_path, int p_focusable);
  252. void _polygons_changed();
  253. virtual Variant _get_painted_value() override;
  254. virtual void _set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  255. virtual void _set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) override;
  256. virtual Variant _get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  257. virtual void _setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, const HashMap<TileMapCell, Variant, TileMapCell> &p_previous_values, const Variant &p_new_value) override;
  258. protected:
  259. virtual void _tile_set_changed() override;
  260. void _notification(int p_what);
  261. public:
  262. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  263. void set_physics_layer(int p_physics_layer) { physics_layer = p_physics_layer; }
  264. TileDataCollisionEditor();
  265. ~TileDataCollisionEditor();
  266. };
  267. class TileDataTerrainsEditor : public TileDataEditor {
  268. GDCLASS(TileDataTerrainsEditor, TileDataEditor);
  269. private:
  270. // Toolbar
  271. HBoxContainer *toolbar = memnew(HBoxContainer);
  272. Button *picker_button = nullptr;
  273. // Painting state.
  274. enum DragType {
  275. DRAG_TYPE_NONE = 0,
  276. DRAG_TYPE_PAINT_TERRAIN_SET,
  277. DRAG_TYPE_PAINT_TERRAIN_SET_RECT,
  278. DRAG_TYPE_PAINT_TERRAIN_BITS,
  279. DRAG_TYPE_PAINT_TERRAIN_BITS_RECT,
  280. };
  281. DragType drag_type = DRAG_TYPE_NONE;
  282. Vector2 drag_start_pos;
  283. Vector2 drag_last_pos;
  284. HashMap<TileMapCell, Variant, TileMapCell> drag_modified;
  285. Variant drag_painted_value;
  286. // UI
  287. Label *label = nullptr;
  288. DummyObject *dummy_object = memnew(DummyObject);
  289. EditorPropertyEnum *terrain_set_property_editor = nullptr;
  290. EditorPropertyEnum *terrain_property_editor = nullptr;
  291. void _property_value_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field);
  292. void _update_terrain_selector();
  293. protected:
  294. virtual void _tile_set_changed() override;
  295. void _notification(int p_what);
  296. public:
  297. virtual Control *get_toolbar() override { return toolbar; }
  298. virtual void forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) override;
  299. virtual void forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) override;
  300. virtual void forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) override;
  301. virtual void forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_atlas_source, const Ref<InputEvent> &p_event) override;
  302. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  303. TileDataTerrainsEditor();
  304. ~TileDataTerrainsEditor();
  305. };
  306. class TileDataNavigationEditor : public TileDataDefaultEditor {
  307. GDCLASS(TileDataNavigationEditor, TileDataDefaultEditor);
  308. private:
  309. int navigation_layer = -1;
  310. PackedVector2Array navigation_polygon;
  311. // UI
  312. GenericTilePolygonEditor *polygon_editor = nullptr;
  313. void _polygon_changed(const PackedVector2Array &p_polygon);
  314. virtual Variant _get_painted_value() override;
  315. virtual void _set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  316. virtual void _set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) override;
  317. virtual Variant _get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) override;
  318. virtual void _setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, const HashMap<TileMapCell, Variant, TileMapCell> &p_previous_values, const Variant &p_new_value) override;
  319. protected:
  320. virtual void _tile_set_changed() override;
  321. void _notification(int p_what);
  322. public:
  323. virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
  324. void set_navigation_layer(int p_navigation_layer) { navigation_layer = p_navigation_layer; }
  325. TileDataNavigationEditor();
  326. };
  327. #endif // TILE_DATA_EDITORS_H