tiles_editor_plugin.h 5.8 KB

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