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