Path: blob/master/editor/scene/2d/tiles/tile_atlas_view.h
9906 views
/**************************************************************************/1/* tile_atlas_view.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#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"3940class ViewPanner;4142class TileAtlasView : public Control {43GDCLASS(TileAtlasView, Control);4445private:46Ref<TileSet> tile_set;47Ref<TileSetAtlasSource> tile_set_atlas_source;48int source_id = TileSet::INVALID_SOURCE;4950enum DragType {51DRAG_TYPE_NONE,52DRAG_TYPE_PAN,53};54DragType drag_type = DRAG_TYPE_NONE;55float previous_zoom = 1.0;56EditorZoomWidget *zoom_widget = nullptr;57Button *button_center_view = nullptr;58CenterContainer *center_container = nullptr;59Vector2 panning;60void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false, const Vector2 &p_mouse_pos = Vector2());61void _zoom_widget_changed();62void _center_view();63virtual void gui_input(const Ref<InputEvent> &p_event) override;6465Ref<ViewPanner> panner;66void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);67void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);6869HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache;70void _update_alternative_tiles_rect_cache();7172MarginContainer *margin_container = nullptr;73int margin_container_paddings[4] = { 0, 0, 0, 0 };74HBoxContainer *hbox = nullptr;75Label *missing_source_label = nullptr;7677// Background78Control *background_left = nullptr;79void _draw_background_left();80Control *background_right = nullptr;81void _draw_background_right();8283// Left side.84Control *base_tiles_root_control = nullptr;85void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);8687Control *base_tiles_drawing_root = nullptr;8889Control *base_tiles_draw = nullptr;90HashMap<Ref<Material>, RID> material_tiles_draw;91HashMap<Ref<Material>, RID> material_alternatives_draw;92void _draw_base_tiles();93RID _get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map);94void _clear_material_canvas_items();9596Control *base_tiles_texture_grid = nullptr;97void _draw_base_tiles_texture_grid();9899Control *base_tiles_shape_grid = nullptr;100void _draw_base_tiles_shape_grid();101102Size2i _compute_base_tiles_control_size();103104// Right side.105Control *alternative_tiles_root_control = nullptr;106void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);107108Control *alternative_tiles_drawing_root = nullptr;109110Control *alternatives_draw = nullptr;111void _draw_alternatives();112113Size2i _compute_alternative_tiles_control_size();114115struct ThemeCache {116Ref<Texture2D> center_view_icon;117Ref<Texture2D> checkerboard;118} theme_cache;119120protected:121virtual void _update_theme_item_cache() override;122123void _notification(int p_what);124static void _bind_methods();125126public:127// Global.128void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id);129130float get_zoom() const;131void set_transform(float p_zoom, Vector2i p_panning);132133void set_padding(Side p_side, int p_padding);134135// Left side.136void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); }137void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); }138139Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const;140141void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) {142if (scaled) {143base_tiles_drawing_root->add_child(p_control);144} else {145base_tiles_root_control->add_child(p_control);146}147p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);148p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);149}150151// Right side.152Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const;153Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile);154155void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) {156if (scaled) {157alternative_tiles_drawing_root->add_child(p_control);158} else {159alternative_tiles_root_control->add_child(p_control);160}161p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);162p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);163}164165// Redraw everything.166void queue_redraw();167168TileAtlasView();169~TileAtlasView();170};171172173