2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* tile_atlas_view.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-05-07 13:41:39 +00:00
|
|
|
|
|
|
|
#ifndef TILE_ATLAS_VIEW_H
|
|
|
|
#define TILE_ATLAS_VIEW_H
|
|
|
|
|
|
|
|
#include "editor/editor_zoom_widget.h"
|
|
|
|
#include "scene/gui/box_container.h"
|
|
|
|
#include "scene/gui/button.h"
|
2021-06-09 18:01:08 +00:00
|
|
|
#include "scene/gui/center_container.h"
|
2021-05-07 13:41:39 +00:00
|
|
|
#include "scene/gui/label.h"
|
|
|
|
#include "scene/gui/margin_container.h"
|
|
|
|
#include "scene/resources/tile_set.h"
|
|
|
|
|
2021-09-28 16:00:16 +00:00
|
|
|
class ViewPanner;
|
|
|
|
|
2021-05-07 13:41:39 +00:00
|
|
|
class TileAtlasView : public Control {
|
|
|
|
GDCLASS(TileAtlasView, Control);
|
|
|
|
|
|
|
|
private:
|
2022-04-04 13:06:57 +00:00
|
|
|
TileSet *tile_set = nullptr;
|
|
|
|
TileSetAtlasSource *tile_set_atlas_source = nullptr;
|
2021-07-06 12:43:03 +00:00
|
|
|
int source_id = TileSet::INVALID_SOURCE;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
2021-06-09 18:01:08 +00:00
|
|
|
enum DragType {
|
|
|
|
DRAG_TYPE_NONE,
|
|
|
|
DRAG_TYPE_PAN,
|
|
|
|
};
|
|
|
|
DragType drag_type = DRAG_TYPE_NONE;
|
2021-05-07 13:41:39 +00:00
|
|
|
float previous_zoom = 1.0;
|
2022-04-04 13:06:57 +00:00
|
|
|
EditorZoomWidget *zoom_widget = nullptr;
|
|
|
|
Button *button_center_view = nullptr;
|
|
|
|
CenterContainer *center_container = nullptr;
|
2021-06-09 18:01:08 +00:00
|
|
|
Vector2 panning;
|
|
|
|
void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false);
|
2021-05-07 13:41:39 +00:00
|
|
|
void _zoom_widget_changed();
|
2021-06-09 18:01:08 +00:00
|
|
|
void _center_view();
|
2021-08-22 15:37:22 +00:00
|
|
|
virtual void gui_input(const Ref<InputEvent> &p_event) override;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
2021-09-28 16:00:16 +00:00
|
|
|
Ref<ViewPanner> panner;
|
2023-01-19 16:50:51 +00:00
|
|
|
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
|
|
|
|
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
|
2021-09-28 16:00:16 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _update_alternative_tiles_rect_cache();
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
MarginContainer *margin_container = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
int margin_container_paddings[4] = { 0, 0, 0, 0 };
|
2022-04-04 13:06:57 +00:00
|
|
|
HBoxContainer *hbox = nullptr;
|
|
|
|
Label *missing_source_label = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
|
|
|
// Background
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *background_left = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_background_left();
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *background_right = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_background_right();
|
|
|
|
|
|
|
|
// Left side.
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *base_tiles_root_control = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *base_tiles_drawing_root = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *base_tiles_draw = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_base_tiles();
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *base_tiles_texture_grid = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_base_tiles_texture_grid();
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *base_tiles_shape_grid = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_base_tiles_shape_grid();
|
|
|
|
|
|
|
|
Size2i _compute_base_tiles_control_size();
|
|
|
|
|
|
|
|
// Right side.
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *alternative_tiles_root_control = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *alternative_tiles_drawing_root = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Control *alternatives_draw = nullptr;
|
2021-05-07 13:41:39 +00:00
|
|
|
void _draw_alternatives();
|
|
|
|
|
|
|
|
Size2i _compute_alternative_tiles_control_size();
|
|
|
|
|
2023-04-03 16:01:11 +00:00
|
|
|
struct ThemeCache {
|
|
|
|
Ref<Texture2D> center_view_icon;
|
|
|
|
Ref<Texture2D> checkerboard;
|
|
|
|
} theme_cache;
|
|
|
|
|
2021-05-07 13:41:39 +00:00
|
|
|
protected:
|
2023-04-03 16:01:11 +00:00
|
|
|
virtual void _update_theme_item_cache() override;
|
|
|
|
|
2021-06-09 18:01:08 +00:00
|
|
|
void _notification(int p_what);
|
2021-05-07 13:41:39 +00:00
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Global.
|
|
|
|
void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id);
|
|
|
|
|
|
|
|
float get_zoom() const;
|
2021-06-09 18:01:08 +00:00
|
|
|
void set_transform(float p_zoom, Vector2i p_panning);
|
2021-05-07 13:41:39 +00:00
|
|
|
|
|
|
|
void set_padding(Side p_side, int p_padding);
|
|
|
|
|
|
|
|
// Left side.
|
|
|
|
void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); };
|
|
|
|
void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); };
|
|
|
|
|
2023-01-17 09:26:10 +00:00
|
|
|
Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const;
|
2021-05-07 13:41:39 +00:00
|
|
|
|
|
|
|
void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) {
|
|
|
|
if (scaled) {
|
|
|
|
base_tiles_drawing_root->add_child(p_control);
|
|
|
|
} else {
|
|
|
|
base_tiles_root_control->add_child(p_control);
|
|
|
|
}
|
2022-08-19 01:20:55 +00:00
|
|
|
p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
|
2021-05-07 13:41:39 +00:00
|
|
|
p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Right side.
|
|
|
|
Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const;
|
|
|
|
Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile);
|
|
|
|
|
|
|
|
void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) {
|
|
|
|
if (scaled) {
|
|
|
|
alternative_tiles_drawing_root->add_child(p_control);
|
|
|
|
} else {
|
|
|
|
alternative_tiles_root_control->add_child(p_control);
|
|
|
|
}
|
2022-08-19 01:20:55 +00:00
|
|
|
p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
|
2021-05-07 13:41:39 +00:00
|
|
|
p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
|
|
|
|
};
|
|
|
|
|
2022-08-13 21:21:24 +00:00
|
|
|
// Redraw everything.
|
|
|
|
void queue_redraw();
|
2021-05-07 13:41:39 +00:00
|
|
|
|
|
|
|
TileAtlasView();
|
|
|
|
};
|
|
|
|
|
2022-07-23 21:41:51 +00:00
|
|
|
#endif // TILE_ATLAS_VIEW_H
|