| 1 | /**************************************************************************/ |
| 2 | /* tiles_editor_plugin.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 TILES_EDITOR_PLUGIN_H |
| 32 | #define TILES_EDITOR_PLUGIN_H |
| 33 | |
| 34 | #include "editor/editor_plugin.h" |
| 35 | #include "scene/gui/box_container.h" |
| 36 | |
| 37 | #include "tile_atlas_view.h" |
| 38 | #include "tile_map_editor.h" |
| 39 | #include "tile_set_editor.h" |
| 40 | |
| 41 | class TilesEditorUtils : public Object { |
| 42 | GDCLASS(TilesEditorUtils, Object); |
| 43 | |
| 44 | static TilesEditorUtils *singleton; |
| 45 | |
| 46 | public: |
| 47 | enum SourceSortOption { |
| 48 | SOURCE_SORT_ID = 0, |
| 49 | SOURCE_SORT_ID_REVERSE, |
| 50 | SOURCE_SORT_NAME, |
| 51 | SOURCE_SORT_NAME_REVERSE, |
| 52 | SOURCE_SORT_MAX |
| 53 | }; |
| 54 | |
| 55 | private: |
| 56 | // For synchronization. |
| 57 | int atlas_sources_lists_current = 0; |
| 58 | float atlas_view_zoom = 1.0; |
| 59 | Vector2 atlas_view_scroll; |
| 60 | |
| 61 | // Source sorting. |
| 62 | int source_sort = SOURCE_SORT_ID; |
| 63 | |
| 64 | struct SourceNameComparator { |
| 65 | static Ref<TileSet> tile_set; |
| 66 | bool operator()(const int &p_a, const int &p_b) const; |
| 67 | }; |
| 68 | |
| 69 | // Patterns preview generation. |
| 70 | struct QueueItem { |
| 71 | Ref<TileSet> tile_set; |
| 72 | Ref<TileMapPattern> pattern; |
| 73 | Callable callback; |
| 74 | }; |
| 75 | List<QueueItem> pattern_preview_queue; |
| 76 | Mutex pattern_preview_mutex; |
| 77 | Semaphore pattern_preview_sem; |
| 78 | Thread pattern_preview_thread; |
| 79 | SafeFlag pattern_thread_exit; |
| 80 | SafeFlag pattern_thread_exited; |
| 81 | Semaphore pattern_preview_done; |
| 82 | void _preview_frame_started(); |
| 83 | void _pattern_preview_done(); |
| 84 | static void _thread_func(void *ud); |
| 85 | void _thread(); |
| 86 | |
| 87 | public: |
| 88 | _FORCE_INLINE_ static TilesEditorUtils *get_singleton() { return singleton; } |
| 89 | |
| 90 | // Pattern preview API. |
| 91 | void queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback); |
| 92 | |
| 93 | // To synchronize the atlas sources lists. |
| 94 | void set_sources_lists_current(int p_current); |
| 95 | void synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button); |
| 96 | |
| 97 | void set_atlas_view_transform(float p_zoom, Vector2 p_scroll); |
| 98 | void synchronize_atlas_view(Object *p_current); |
| 99 | |
| 100 | // Sorting. |
| 101 | void set_sorting_option(int p_option); |
| 102 | List<int> get_sorted_sources(const Ref<TileSet> p_tile_set) const; |
| 103 | |
| 104 | // Misc. |
| 105 | void display_tile_set_editor_panel(); |
| 106 | |
| 107 | static void draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color = Color(1.0, 1.0, 1.0)); |
| 108 | |
| 109 | TilesEditorUtils(); |
| 110 | ~TilesEditorUtils(); |
| 111 | }; |
| 112 | |
| 113 | class TileMapEditorPlugin : public EditorPlugin { |
| 114 | GDCLASS(TileMapEditorPlugin, EditorPlugin); |
| 115 | |
| 116 | TileMapEditor *editor = nullptr; |
| 117 | Button *button = nullptr; |
| 118 | ObjectID tile_map_id; |
| 119 | |
| 120 | bool tile_map_changed_needs_update = false; |
| 121 | ObjectID edited_tileset; // The TileSet associated with the TileMap. |
| 122 | |
| 123 | void _tile_map_changed(); |
| 124 | void _update_tile_map(); |
| 125 | |
| 126 | protected: |
| 127 | void _notification(int p_notification); |
| 128 | |
| 129 | public: |
| 130 | virtual void edit(Object *p_object) override; |
| 131 | virtual bool handles(Object *p_object) const override; |
| 132 | virtual void make_visible(bool p_visible) override; |
| 133 | |
| 134 | virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override; |
| 135 | virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override; |
| 136 | |
| 137 | void hide_editor(); |
| 138 | bool is_editor_visible() const; |
| 139 | |
| 140 | TileMapEditorPlugin(); |
| 141 | ~TileMapEditorPlugin(); |
| 142 | }; |
| 143 | |
| 144 | class TileSetEditorPlugin : public EditorPlugin { |
| 145 | GDCLASS(TileSetEditorPlugin, EditorPlugin); |
| 146 | |
| 147 | TileSetEditor *editor = nullptr; |
| 148 | Button *button = nullptr; |
| 149 | |
| 150 | ObjectID edited_tileset; |
| 151 | |
| 152 | public: |
| 153 | virtual void edit(Object *p_object) override; |
| 154 | virtual bool handles(Object *p_object) const override; |
| 155 | virtual void make_visible(bool p_visible) override; |
| 156 | |
| 157 | ObjectID get_edited_tileset() const; |
| 158 | |
| 159 | TileSetEditorPlugin(); |
| 160 | ~TileSetEditorPlugin(); |
| 161 | }; |
| 162 | |
| 163 | #endif // TILES_EDITOR_PLUGIN_H |
| 164 | |