tiles_editor_plugin.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/plugins/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "tile_atlas_view.h"
  35. #include "tile_map_layer_editor.h"
  36. #include "tile_set_editor.h"
  37. class TilesEditorUtils : public Object {
  38. GDCLASS(TilesEditorUtils, Object);
  39. static TilesEditorUtils *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. // For synchronization.
  50. int atlas_sources_lists_current = 0;
  51. float atlas_view_zoom = 1.0;
  52. Vector2 atlas_view_scroll;
  53. // Source sorting.
  54. int source_sort = SOURCE_SORT_ID;
  55. struct SourceNameComparator {
  56. static Ref<TileSet> tile_set;
  57. bool operator()(const int &p_a, const int &p_b) const;
  58. };
  59. // Patterns preview generation.
  60. struct QueueItem {
  61. Ref<TileSet> tile_set;
  62. Ref<TileMapPattern> pattern;
  63. Callable callback;
  64. };
  65. List<QueueItem> pattern_preview_queue;
  66. Mutex pattern_preview_mutex;
  67. Semaphore pattern_preview_sem;
  68. Thread pattern_preview_thread;
  69. SafeFlag pattern_thread_exit;
  70. SafeFlag pattern_thread_exited;
  71. Semaphore pattern_preview_done;
  72. void _preview_frame_started();
  73. void _pattern_preview_done();
  74. static void _thread_func(void *ud);
  75. void _thread();
  76. public:
  77. _FORCE_INLINE_ static TilesEditorUtils *get_singleton() { return singleton; }
  78. // Pattern preview API.
  79. void queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback);
  80. // To synchronize the atlas sources lists.
  81. void set_sources_lists_current(int p_current);
  82. void synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button);
  83. void set_atlas_view_transform(float p_zoom, Vector2 p_scroll);
  84. void synchronize_atlas_view(Object *p_current);
  85. // Sorting.
  86. void set_sorting_option(int p_option);
  87. List<int> get_sorted_sources(const Ref<TileSet> p_tile_set) const;
  88. // Misc.
  89. void display_tile_set_editor_panel();
  90. static void draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color = Color(1.0, 1.0, 1.0));
  91. TilesEditorUtils();
  92. ~TilesEditorUtils();
  93. };
  94. class TileMapEditorPlugin : public EditorPlugin {
  95. GDCLASS(TileMapEditorPlugin, EditorPlugin);
  96. TileMapLayerEditor *editor = nullptr;
  97. Button *button = nullptr;
  98. ObjectID tile_map_layer_id;
  99. ObjectID tile_map_group_id; // Allow keeping the layer selector up to date.
  100. bool tile_map_changed_needs_update = false;
  101. ObjectID tile_set_id; // The TileSet associated with the TileMap.
  102. void _tile_map_layer_changed();
  103. void _tile_map_layer_removed();
  104. void _update_tile_map();
  105. void _select_layer(const StringName &p_name);
  106. void _edit_tile_map_layer(TileMapLayer *p_tile_map_layer, bool p_show_layer_selector);
  107. void _edit_tile_map(TileMap *p_tile_map);
  108. protected:
  109. void _notification(int p_notification);
  110. public:
  111. virtual void edit(Object *p_object) override;
  112. virtual bool handles(Object *p_object) const override;
  113. virtual void make_visible(bool p_visible) override;
  114. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override;
  115. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override;
  116. void hide_editor();
  117. bool is_editor_visible() const;
  118. TileMapEditorPlugin();
  119. ~TileMapEditorPlugin();
  120. };
  121. class TileSetEditorPlugin : public EditorPlugin {
  122. GDCLASS(TileSetEditorPlugin, EditorPlugin);
  123. TileSetEditor *editor = nullptr;
  124. Button *button = nullptr;
  125. ObjectID edited_tileset;
  126. public:
  127. virtual void edit(Object *p_object) override;
  128. virtual bool handles(Object *p_object) const override;
  129. virtual void make_visible(bool p_visible) override;
  130. ObjectID get_edited_tileset() const;
  131. TileSetEditorPlugin();
  132. ~TileSetEditorPlugin();
  133. };
  134. #endif // TILES_EDITOR_PLUGIN_H