tile_atlas_view.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**************************************************************************/
  2. /* tile_atlas_view.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 TILE_ATLAS_VIEW_H
  31. #define TILE_ATLAS_VIEW_H
  32. #include "editor/gui/editor_zoom_widget.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/center_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/margin_container.h"
  38. #include "scene/resources/2d/tile_set.h"
  39. class ViewPanner;
  40. class TileAtlasView : public Control {
  41. GDCLASS(TileAtlasView, Control);
  42. private:
  43. Ref<TileSet> tile_set;
  44. Ref<TileSetAtlasSource> tile_set_atlas_source;
  45. int source_id = TileSet::INVALID_SOURCE;
  46. enum DragType {
  47. DRAG_TYPE_NONE,
  48. DRAG_TYPE_PAN,
  49. };
  50. DragType drag_type = DRAG_TYPE_NONE;
  51. float previous_zoom = 1.0;
  52. EditorZoomWidget *zoom_widget = nullptr;
  53. Button *button_center_view = nullptr;
  54. CenterContainer *center_container = nullptr;
  55. Vector2 panning;
  56. void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false);
  57. void _zoom_widget_changed();
  58. void _center_view();
  59. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  60. Ref<ViewPanner> panner;
  61. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  62. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  63. HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache;
  64. void _update_alternative_tiles_rect_cache();
  65. MarginContainer *margin_container = nullptr;
  66. int margin_container_paddings[4] = { 0, 0, 0, 0 };
  67. HBoxContainer *hbox = nullptr;
  68. Label *missing_source_label = nullptr;
  69. // Background
  70. Control *background_left = nullptr;
  71. void _draw_background_left();
  72. Control *background_right = nullptr;
  73. void _draw_background_right();
  74. // Left side.
  75. Control *base_tiles_root_control = nullptr;
  76. void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
  77. Control *base_tiles_drawing_root = nullptr;
  78. Control *base_tiles_draw = nullptr;
  79. HashMap<Ref<Material>, RID> material_tiles_draw;
  80. HashMap<Ref<Material>, RID> material_alternatives_draw;
  81. void _draw_base_tiles();
  82. RID _get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map);
  83. void _clear_material_canvas_items();
  84. Control *base_tiles_texture_grid = nullptr;
  85. void _draw_base_tiles_texture_grid();
  86. Control *base_tiles_shape_grid = nullptr;
  87. void _draw_base_tiles_shape_grid();
  88. Size2i _compute_base_tiles_control_size();
  89. // Right side.
  90. Control *alternative_tiles_root_control = nullptr;
  91. void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
  92. Control *alternative_tiles_drawing_root = nullptr;
  93. Control *alternatives_draw = nullptr;
  94. void _draw_alternatives();
  95. Size2i _compute_alternative_tiles_control_size();
  96. struct ThemeCache {
  97. Ref<Texture2D> center_view_icon;
  98. Ref<Texture2D> checkerboard;
  99. } theme_cache;
  100. protected:
  101. virtual void _update_theme_item_cache() override;
  102. void _notification(int p_what);
  103. static void _bind_methods();
  104. public:
  105. // Global.
  106. void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id);
  107. float get_zoom() const;
  108. void set_transform(float p_zoom, Vector2i p_panning);
  109. void set_padding(Side p_side, int p_padding);
  110. // Left side.
  111. void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); }
  112. void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); }
  113. Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const;
  114. void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) {
  115. if (scaled) {
  116. base_tiles_drawing_root->add_child(p_control);
  117. } else {
  118. base_tiles_root_control->add_child(p_control);
  119. }
  120. p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  121. p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  122. }
  123. // Right side.
  124. Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const;
  125. Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile);
  126. void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) {
  127. if (scaled) {
  128. alternative_tiles_drawing_root->add_child(p_control);
  129. } else {
  130. alternative_tiles_root_control->add_child(p_control);
  131. }
  132. p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  133. p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  134. }
  135. // Redraw everything.
  136. void queue_redraw();
  137. TileAtlasView();
  138. ~TileAtlasView();
  139. };
  140. #endif // TILE_ATLAS_VIEW_H