tile_data_editors.h 17 KB

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