tiles_editor_plugin.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef TILES_EDITOR_PLUGIN_H
  31. #define TILES_EDITOR_PLUGIN_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "tile_atlas_view.h"
  35. #include "tile_map_editor.h"
  36. #include "tile_set_editor.h"
  37. class TilesEditorPlugin : public EditorPlugin {
  38. GDCLASS(TilesEditorPlugin, EditorPlugin);
  39. static TilesEditorPlugin *singleton;
  40. public:
  41. enum SourceSortOption {
  42. SOURCE_SORT_ID = 0,
  43. SOURCE_SORT_ID_REVERSE,
  44. SOURCE_SORT_NAME,
  45. SOURCE_SORT_NAME_REVERSE,
  46. SOURCE_SORT_MAX
  47. };
  48. private:
  49. bool is_visible = false;
  50. bool tile_map_changed_needs_update = false;
  51. ObjectID tile_map_id;
  52. Ref<TileSet> tile_set;
  53. bool is_editing_tile_set = false;
  54. Button *tilemap_editor_button = nullptr;
  55. TileMapEditor *tilemap_editor = nullptr;
  56. Button *tileset_editor_button = nullptr;
  57. TileSetEditor *tileset_editor = nullptr;
  58. void _update_editors();
  59. // For synchronization.
  60. int atlas_sources_lists_current = 0;
  61. float atlas_view_zoom = 1.0;
  62. Vector2 atlas_view_scroll;
  63. void _tile_map_changed();
  64. // Source sorting.
  65. int source_sort = SOURCE_SORT_ID;
  66. struct SourceNameComparator {
  67. static Ref<TileSet> tile_set;
  68. bool operator()(const int &p_a, const int &p_b) const;
  69. };
  70. // Patterns preview generation.
  71. struct QueueItem {
  72. Ref<TileSet> tile_set;
  73. Ref<TileMapPattern> pattern;
  74. Callable callback;
  75. };
  76. List<QueueItem> pattern_preview_queue;
  77. Mutex pattern_preview_mutex;
  78. Semaphore pattern_preview_sem;
  79. Thread pattern_preview_thread;
  80. SafeFlag pattern_thread_exit;
  81. SafeFlag pattern_thread_exited;
  82. Semaphore pattern_preview_done;
  83. void _preview_frame_started();
  84. void _pattern_preview_done();
  85. static void _thread_func(void *ud);
  86. void _thread();
  87. protected:
  88. void _notification(int p_what);
  89. public:
  90. _FORCE_INLINE_ static TilesEditorPlugin *get_singleton() { return singleton; }
  91. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return tilemap_editor->forward_canvas_gui_input(p_event); }
  92. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { tilemap_editor->forward_canvas_draw_over_viewport(p_overlay); }
  93. bool is_tile_map_selected();
  94. // Pattern preview API.
  95. void queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback);
  96. // To synchronize the atlas sources lists.
  97. void set_sources_lists_current(int p_current);
  98. void synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button);
  99. void set_atlas_view_transform(float p_zoom, Vector2 p_scroll);
  100. void synchronize_atlas_view(Object *p_current);
  101. // Sorting.
  102. void set_sorting_option(int p_option);
  103. List<int> get_sorted_sources(const Ref<TileSet> p_tile_set) const;
  104. virtual void edit(Object *p_object) override;
  105. virtual bool handles(Object *p_object) const override;
  106. virtual void make_visible(bool p_visible) override;
  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. TilesEditorPlugin();
  109. ~TilesEditorPlugin();
  110. };
  111. #endif // TILES_EDITOR_PLUGIN_H