2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* tile_set_editor_plugin.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef TILE_SET_EDITOR_PLUGIN_H
|
|
|
|
#define TILE_SET_EDITOR_PLUGIN_H
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "editor/editor_node.h"
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "scene/2d/sprite_2d.h"
|
2019-03-02 22:50:19 +01:00
|
|
|
#include "scene/resources/concave_polygon_shape_2d.h"
|
2017-12-28 06:57:37 +01:00
|
|
|
#include "scene/resources/convex_polygon_shape_2d.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/resources/tile_set.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
#define WORKSPACE_MARGIN Vector2(10, 10)
|
|
|
|
class TilesetEditorContext;
|
2018-02-17 12:08:44 +01:00
|
|
|
|
2018-11-24 05:38:26 +01:00
|
|
|
class TileSetEditor : public HSplitContainer {
|
2017-10-22 03:42:23 +02:00
|
|
|
friend class TileSetEditorPlugin;
|
2018-07-29 23:54:12 +02:00
|
|
|
friend class TilesetEditorContext;
|
2018-02-17 12:08:44 +01:00
|
|
|
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(TileSetEditor, HSplitContainer);
|
2018-07-29 23:54:12 +02:00
|
|
|
|
2020-06-19 20:49:04 +02:00
|
|
|
enum TextureButtons {
|
2018-07-29 23:54:12 +02:00
|
|
|
TOOL_TILESET_ADD_TEXTURE,
|
|
|
|
TOOL_TILESET_REMOVE_TEXTURE,
|
|
|
|
TOOL_TILESET_CREATE_SCENE,
|
|
|
|
TOOL_TILESET_MERGE_SCENE,
|
|
|
|
TOOL_TILESET_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum WorkspaceMode {
|
|
|
|
WORKSPACE_EDIT,
|
|
|
|
WORKSPACE_CREATE_SINGLE,
|
|
|
|
WORKSPACE_CREATE_AUTOTILE,
|
|
|
|
WORKSPACE_CREATE_ATLAS,
|
|
|
|
WORKSPACE_MODE_MAX
|
|
|
|
};
|
2017-10-22 03:42:23 +02:00
|
|
|
|
|
|
|
enum EditMode {
|
2018-07-29 23:54:12 +02:00
|
|
|
EDITMODE_REGION,
|
2017-10-22 03:42:23 +02:00
|
|
|
EDITMODE_COLLISION,
|
|
|
|
EDITMODE_OCCLUSION,
|
|
|
|
EDITMODE_NAVIGATION,
|
2018-02-17 12:08:44 +01:00
|
|
|
EDITMODE_BITMASK,
|
2017-10-22 03:42:23 +02:00
|
|
|
EDITMODE_PRIORITY,
|
2018-02-17 12:08:44 +01:00
|
|
|
EDITMODE_ICON,
|
2018-07-02 15:39:39 +02:00
|
|
|
EDITMODE_Z_INDEX,
|
2017-10-22 03:42:23 +02:00
|
|
|
EDITMODE_MAX
|
|
|
|
};
|
|
|
|
|
2018-02-17 12:08:44 +01:00
|
|
|
enum TileSetTools {
|
2019-03-01 00:48:34 +01:00
|
|
|
SELECT_PREVIOUS,
|
|
|
|
SELECT_NEXT,
|
2017-10-22 03:42:23 +02:00
|
|
|
TOOL_SELECT,
|
|
|
|
BITMASK_COPY,
|
|
|
|
BITMASK_PASTE,
|
|
|
|
BITMASK_CLEAR,
|
|
|
|
SHAPE_NEW_POLYGON,
|
2019-02-20 12:27:53 +01:00
|
|
|
SHAPE_NEW_RECTANGLE,
|
2019-03-02 22:50:19 +01:00
|
|
|
SHAPE_TOGGLE_TYPE,
|
2017-10-22 03:42:23 +02:00
|
|
|
SHAPE_DELETE,
|
|
|
|
SHAPE_KEEP_INSIDE_TILE,
|
2018-07-29 23:54:12 +02:00
|
|
|
TOOL_GRID_SNAP,
|
2017-10-22 03:42:23 +02:00
|
|
|
ZOOM_OUT,
|
|
|
|
ZOOM_1,
|
|
|
|
ZOOM_IN,
|
2018-07-29 23:54:12 +02:00
|
|
|
VISIBLE_INFO,
|
2017-10-22 03:42:23 +02:00
|
|
|
TOOL_MAX
|
|
|
|
};
|
|
|
|
|
2019-03-01 02:45:16 +01:00
|
|
|
struct SubtileData {
|
|
|
|
Array collisions;
|
|
|
|
Ref<OccluderPolygon2D> occlusion_shape;
|
|
|
|
Ref<NavigationPolygon> navigation_shape;
|
|
|
|
};
|
|
|
|
|
2018-02-17 12:08:44 +01:00
|
|
|
Ref<TileSet> tileset;
|
2018-07-29 23:54:12 +02:00
|
|
|
TilesetEditorContext *helper;
|
|
|
|
EditorNode *editor;
|
2018-12-19 17:20:49 +01:00
|
|
|
UndoRedo *undo_redo;
|
2018-07-29 23:54:12 +02:00
|
|
|
|
|
|
|
ConfirmationDialog *cd;
|
|
|
|
AcceptDialog *err_dialog;
|
|
|
|
EditorFileDialog *texture_dialog;
|
|
|
|
|
|
|
|
ItemList *texture_list;
|
|
|
|
int option;
|
2020-06-19 20:49:04 +02:00
|
|
|
Button *tileset_toolbar_buttons[TOOL_TILESET_MAX];
|
2018-07-29 23:54:12 +02:00
|
|
|
MenuButton *tileset_toolbar_tools;
|
2020-03-17 07:33:00 +01:00
|
|
|
Map<RID, Ref<Texture2D>> texture_map;
|
2018-07-29 23:54:12 +02:00
|
|
|
|
|
|
|
bool creating_shape;
|
|
|
|
int dragging_point;
|
2018-12-24 17:35:12 +01:00
|
|
|
bool tile_names_visible;
|
2018-07-29 23:54:12 +02:00
|
|
|
Vector2 region_from;
|
|
|
|
Rect2 edited_region;
|
|
|
|
bool draw_edited_region;
|
|
|
|
Vector2 edited_shape_coord;
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector2Array current_shape;
|
2019-03-01 02:45:16 +01:00
|
|
|
Map<Vector2i, SubtileData> current_tile_data;
|
2019-03-31 19:28:42 +02:00
|
|
|
Map<Vector2, uint32_t> bitmask_map_copy;
|
2018-07-29 23:54:12 +02:00
|
|
|
|
|
|
|
Vector2 snap_step;
|
|
|
|
Vector2 snap_offset;
|
|
|
|
Vector2 snap_separation;
|
2018-02-17 12:08:44 +01:00
|
|
|
|
2019-03-02 22:50:19 +01:00
|
|
|
Ref<Shape2D> edited_collision_shape;
|
2017-10-22 03:42:23 +02:00
|
|
|
Ref<OccluderPolygon2D> edited_occlusion_shape;
|
|
|
|
Ref<NavigationPolygon> edited_navigation_shape;
|
|
|
|
|
|
|
|
int current_item_index;
|
2020-03-26 22:49:16 +01:00
|
|
|
Sprite2D *preview;
|
2017-12-30 06:34:33 +01:00
|
|
|
ScrollContainer *scroll;
|
2019-09-04 07:20:57 +02:00
|
|
|
Label *empty_message;
|
2017-10-22 03:42:23 +02:00
|
|
|
Control *workspace_container;
|
2018-03-02 12:36:18 +01:00
|
|
|
bool draw_handles;
|
|
|
|
Control *workspace_overlay;
|
2017-10-22 03:42:23 +02:00
|
|
|
Control *workspace;
|
2018-07-29 23:54:12 +02:00
|
|
|
Button *tool_workspacemode[WORKSPACE_MODE_MAX];
|
2017-10-22 03:42:23 +02:00
|
|
|
Button *tool_editmode[EDITMODE_MAX];
|
2018-08-13 01:24:20 +02:00
|
|
|
HSeparator *separator_editmode;
|
2017-10-22 03:42:23 +02:00
|
|
|
HBoxContainer *toolbar;
|
2020-06-19 20:49:04 +02:00
|
|
|
Button *tools[TOOL_MAX];
|
2019-03-02 22:50:19 +01:00
|
|
|
VSeparator *separator_shape_toggle;
|
2018-11-24 05:38:26 +01:00
|
|
|
VSeparator *separator_bitmask;
|
2018-08-13 01:24:20 +02:00
|
|
|
VSeparator *separator_delete;
|
|
|
|
VSeparator *separator_grid;
|
2017-10-22 03:42:23 +02:00
|
|
|
SpinBox *spin_priority;
|
2018-07-02 15:39:39 +02:00
|
|
|
SpinBox *spin_z_index;
|
2018-07-29 23:54:12 +02:00
|
|
|
WorkspaceMode workspace_mode;
|
2017-10-22 03:42:23 +02:00
|
|
|
EditMode edit_mode;
|
2018-07-29 23:54:12 +02:00
|
|
|
int current_tile;
|
2017-10-22 03:42:23 +02:00
|
|
|
|
2018-10-21 15:56:05 +02:00
|
|
|
float max_scale;
|
|
|
|
float min_scale;
|
|
|
|
float scale_ratio;
|
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
void update_texture_list();
|
|
|
|
void update_texture_list_icon();
|
2017-12-28 06:57:37 +01:00
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
void add_texture(Ref<Texture2D> p_texture);
|
|
|
|
void remove_texture(Ref<Texture2D> p_texture);
|
2018-12-19 17:20:49 +01:00
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> get_current_texture();
|
2018-02-17 12:08:44 +01:00
|
|
|
|
|
|
|
static void _import_node(Node *p_node, Ref<TileSet> p_library);
|
|
|
|
static void _import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge);
|
2018-12-19 17:20:49 +01:00
|
|
|
void _undo_redo_import_scene(Node *p_scene, bool p_merge);
|
2017-10-22 03:42:23 +02:00
|
|
|
|
2019-08-26 11:15:19 +02:00
|
|
|
bool _is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const;
|
|
|
|
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
|
|
|
|
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
|
|
|
|
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
|
2020-02-17 22:06:54 +01:00
|
|
|
void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);
|
2019-08-26 11:15:19 +02:00
|
|
|
|
2017-10-22 03:42:23 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
2018-02-17 12:08:44 +01:00
|
|
|
public:
|
|
|
|
void edit(const Ref<TileSet> &p_tileset);
|
|
|
|
static Error update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge = true);
|
|
|
|
|
|
|
|
TileSetEditor(EditorNode *p_editor);
|
2018-02-27 03:54:09 +01:00
|
|
|
~TileSetEditor();
|
2018-02-17 12:08:44 +01:00
|
|
|
|
2017-10-22 03:42:23 +02:00
|
|
|
private:
|
2018-07-29 23:54:12 +02:00
|
|
|
void _on_tileset_toolbar_button_pressed(int p_index);
|
|
|
|
void _on_tileset_toolbar_confirm();
|
|
|
|
void _on_texture_list_selected(int p_index);
|
2020-02-17 22:06:54 +01:00
|
|
|
void _on_textures_added(const PackedStringArray &p_paths);
|
2017-10-22 03:42:23 +02:00
|
|
|
void _on_edit_mode_changed(int p_edit_mode);
|
2018-07-29 23:54:12 +02:00
|
|
|
void _on_workspace_mode_changed(int p_workspace_mode);
|
2018-03-02 12:36:18 +01:00
|
|
|
void _on_workspace_overlay_draw();
|
2017-10-22 03:42:23 +02:00
|
|
|
void _on_workspace_draw();
|
2018-07-29 23:54:12 +02:00
|
|
|
void _on_workspace_process();
|
2020-05-30 12:29:53 +02:00
|
|
|
void _on_scroll_container_input(const Ref<InputEvent> &p_event);
|
2017-10-22 03:42:23 +02:00
|
|
|
void _on_workspace_input(const Ref<InputEvent> &p_ie);
|
|
|
|
void _on_tool_clicked(int p_tool);
|
|
|
|
void _on_priority_changed(float val);
|
2018-07-02 15:39:39 +02:00
|
|
|
void _on_z_index_changed(float val);
|
2017-12-28 06:57:37 +01:00
|
|
|
void _on_grid_snap_toggled(bool p_val);
|
2019-03-02 22:50:19 +01:00
|
|
|
Vector<Vector2> _get_collision_shape_points(const Ref<Shape2D> &p_shape);
|
|
|
|
Vector<Vector2> _get_edited_shape_points();
|
2019-07-10 11:54:12 +02:00
|
|
|
void _set_edited_shape_points(const Vector<Vector2> &points);
|
2019-03-01 02:45:16 +01:00
|
|
|
void _update_tile_data();
|
2019-03-02 22:50:19 +01:00
|
|
|
void _update_toggle_shape_button();
|
2019-03-01 00:48:34 +01:00
|
|
|
void _select_next_tile();
|
|
|
|
void _select_previous_tile();
|
|
|
|
Array _get_tiles_in_current_texture(bool sorted = false);
|
|
|
|
bool _sort_tiles(Variant p_a, Variant p_b);
|
|
|
|
void _select_next_subtile();
|
|
|
|
void _select_previous_subtile();
|
2019-03-01 02:45:16 +01:00
|
|
|
void _select_next_shape();
|
|
|
|
void _select_previous_shape();
|
2019-03-02 22:50:19 +01:00
|
|
|
void _set_edited_collision_shape(const Ref<Shape2D> &p_shape);
|
2018-07-29 23:54:12 +02:00
|
|
|
void _set_snap_step(Vector2 p_val);
|
|
|
|
void _set_snap_off(Vector2 p_val);
|
|
|
|
void _set_snap_sep(Vector2 p_val);
|
|
|
|
|
2018-12-19 17:20:49 +01:00
|
|
|
void _validate_current_tile_id();
|
|
|
|
void _select_edited_shape_coord();
|
|
|
|
void _undo_tile_removal(int p_id);
|
|
|
|
|
2018-10-21 15:56:05 +02:00
|
|
|
void _zoom_in();
|
|
|
|
void _zoom_out();
|
2018-11-24 05:38:26 +01:00
|
|
|
void _zoom_reset();
|
2018-10-21 15:56:05 +02:00
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
void draw_highlight_current_tile();
|
|
|
|
void draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted = Vector<Vector2>());
|
|
|
|
void draw_tile_subdivision(int p_id, Color p_color) const;
|
|
|
|
void draw_edited_region_subdivision() const;
|
2017-12-28 06:57:37 +01:00
|
|
|
void draw_grid_snap();
|
2017-10-22 03:42:23 +02:00
|
|
|
void draw_polygon_shapes();
|
|
|
|
void close_shape(const Vector2 &shape_anchor);
|
2018-01-05 11:53:30 +01:00
|
|
|
void select_coord(const Vector2 &coord);
|
2017-10-22 03:42:23 +02:00
|
|
|
Vector2 snap_point(const Vector2 &point);
|
2018-02-17 12:08:44 +01:00
|
|
|
void update_workspace_tile_mode();
|
2019-02-28 02:09:11 +01:00
|
|
|
void update_workspace_minsize();
|
2018-07-29 23:54:12 +02:00
|
|
|
void update_edited_region(const Vector2 &end_point);
|
2019-12-01 21:35:23 +01:00
|
|
|
int get_grabbed_point(const Vector2 &p_mouse_pos, real_t grab_threshold);
|
|
|
|
bool is_within_grabbing_distance_of_first_point(const Vector2 &p_pos, real_t p_grab_threshold);
|
2017-10-22 03:42:23 +02:00
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
int get_current_tile() const;
|
|
|
|
void set_current_tile(int p_id);
|
2017-10-22 03:42:23 +02:00
|
|
|
};
|
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
class TilesetEditorContext : public Object {
|
2018-02-17 12:08:44 +01:00
|
|
|
friend class TileSetEditor;
|
2018-07-29 23:54:12 +02:00
|
|
|
GDCLASS(TilesetEditorContext, Object);
|
2017-10-22 03:42:23 +02:00
|
|
|
|
2018-02-17 12:08:44 +01:00
|
|
|
Ref<TileSet> tileset;
|
|
|
|
TileSetEditor *tileset_editor;
|
2018-07-29 23:54:12 +02:00
|
|
|
bool snap_options_visible;
|
2017-10-22 03:42:23 +02:00
|
|
|
|
|
|
|
public:
|
2019-07-22 12:03:57 +02:00
|
|
|
bool _hide_script_from_inspector() { return true; }
|
2017-10-22 03:42:23 +02:00
|
|
|
void set_tileset(const Ref<TileSet> &p_tileset);
|
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
private:
|
|
|
|
void set_snap_options_visible(bool p_visible);
|
|
|
|
|
2017-10-22 03:42:23 +02:00
|
|
|
protected:
|
|
|
|
bool _set(const StringName &p_name, const Variant &p_value);
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const;
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
2018-12-07 05:18:02 +01:00
|
|
|
static void _bind_methods();
|
2017-10-22 03:42:23 +02:00
|
|
|
|
2018-07-29 23:54:12 +02:00
|
|
|
public:
|
|
|
|
TilesetEditorContext(TileSetEditor *p_tileset_editor);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TileSetEditorPlugin : public EditorPlugin {
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(TileSetEditorPlugin, EditorPlugin);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
TileSetEditor *tileset_editor;
|
2018-07-29 23:54:12 +02:00
|
|
|
Button *tileset_editor_button;
|
2014-02-10 02:10:30 +01:00
|
|
|
EditorNode *editor;
|
|
|
|
|
|
|
|
public:
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual String get_name() const override { return "TileSet"; }
|
|
|
|
bool has_main_screen() const override { return false; }
|
|
|
|
virtual void edit(Object *p_node) override;
|
|
|
|
virtual bool handles(Object *p_node) const override;
|
|
|
|
virtual void make_visible(bool p_visible) override;
|
|
|
|
void set_state(const Dictionary &p_state) override;
|
|
|
|
Dictionary get_state() const override;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
TileSetEditorPlugin(EditorNode *p_node);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // TILE_SET_EDITOR_PLUGIN_H
|