1 /*************************************************************************/
2 /*  texture_region_editor_plugin.h                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 
31 #ifndef TEXTURE_REGION_EDITOR_PLUGIN_H
32 #define TEXTURE_REGION_EDITOR_PLUGIN_H
33 
34 #include "canvas_item_editor_plugin.h"
35 #include "editor/editor_node.h"
36 #include "editor/editor_plugin.h"
37 #include "scene/2d/sprite.h"
38 #include "scene/3d/sprite_3d.h"
39 #include "scene/gui/nine_patch_rect.h"
40 #include "scene/resources/style_box.h"
41 #include "scene/resources/texture.h"
42 
43 /**
44 	@author Mariano Suligoy
45 */
46 
47 class TextureRegionEditor : public VBoxContainer {
48 
49 	GDCLASS(TextureRegionEditor, VBoxContainer);
50 
51 	enum SnapMode {
52 		SNAP_NONE,
53 		SNAP_PIXEL,
54 		SNAP_GRID,
55 		SNAP_AUTOSLICE
56 	};
57 
58 	friend class TextureRegionEditorPlugin;
59 	OptionButton *snap_mode_button;
60 	ToolButton *zoom_in;
61 	ToolButton *zoom_reset;
62 	ToolButton *zoom_out;
63 	HBoxContainer *hb_grid; //For showing/hiding the grid controls when changing the SnapMode
64 	SpinBox *sb_step_y;
65 	SpinBox *sb_step_x;
66 	SpinBox *sb_off_y;
67 	SpinBox *sb_off_x;
68 	SpinBox *sb_sep_y;
69 	SpinBox *sb_sep_x;
70 	Panel *edit_draw;
71 
72 	VScrollBar *vscroll;
73 	HScrollBar *hscroll;
74 
75 	EditorNode *editor;
76 	UndoRedo *undo_redo;
77 
78 	Vector2 draw_ofs;
79 	float draw_zoom;
80 	bool updating_scroll;
81 
82 	int snap_mode;
83 	Vector2 snap_offset;
84 	Vector2 snap_step;
85 	Vector2 snap_separation;
86 
87 	Sprite *node_sprite;
88 	Sprite3D *node_sprite_3d;
89 	NinePatchRect *node_ninepatch;
90 	Ref<StyleBoxTexture> obj_styleBox;
91 	Ref<AtlasTexture> atlas_tex;
92 
93 	Rect2 rect;
94 	Rect2 rect_prev;
95 	float prev_margin;
96 	int edited_margin;
97 	Map<RID, List<Rect2> > cache_map;
98 	List<Rect2> autoslice_cache;
99 	bool autoslice_is_dirty;
100 
101 	bool drag;
102 	bool creating;
103 	Vector2 drag_from;
104 	int drag_index;
105 
106 	void _set_snap_mode(int p_mode);
107 	void _set_snap_off_x(float p_val);
108 	void _set_snap_off_y(float p_val);
109 	void _set_snap_step_x(float p_val);
110 	void _set_snap_step_y(float p_val);
111 	void _set_snap_sep_x(float p_val);
112 	void _set_snap_sep_y(float p_val);
113 	void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
114 	void _zoom_in();
115 	void _zoom_reset();
116 	void _zoom_out();
117 	void apply_rect(const Rect2 &p_rect);
118 	void _update_rect();
119 	void _update_autoslice();
120 
121 protected:
122 	void _notification(int p_what);
123 	void _node_removed(Object *p_obj);
124 	static void _bind_methods();
125 
126 	Vector2 snap_point(Vector2 p_target) const;
127 
128 	virtual void _changed_callback(Object *p_changed, const char *p_prop);
129 
130 public:
131 	void _edit_region();
132 	void _region_draw();
133 	void _region_input(const Ref<InputEvent> &p_input);
134 	void _scroll_changed(float);
135 	bool is_stylebox();
136 	bool is_atlas_texture();
137 	bool is_ninepatch();
138 	Sprite3D *get_sprite_3d();
139 	Sprite *get_sprite();
140 
141 	void edit(Object *p_obj);
142 	TextureRegionEditor(EditorNode *p_editor);
143 };
144 
145 class TextureRegionEditorPlugin : public EditorPlugin {
146 	GDCLASS(TextureRegionEditorPlugin, EditorPlugin);
147 
148 	bool manually_hidden;
149 	Button *texture_region_button;
150 	TextureRegionEditor *region_editor;
151 	EditorNode *editor;
152 
153 protected:
154 	static void _bind_methods();
155 
156 	void _editor_visiblity_changed();
157 
158 public:
get_name()159 	virtual String get_name() const { return "TextureRegion"; }
has_main_screen()160 	bool has_main_screen() const { return false; }
161 	virtual void edit(Object *p_object);
162 	virtual bool handles(Object *p_object) const;
163 	virtual void make_visible(bool p_visible);
164 	void set_state(const Dictionary &p_state);
165 	Dictionary get_state() const;
166 
167 	TextureRegionEditorPlugin(EditorNode *p_node);
168 };
169 
170 #endif // TEXTURE_REGION_EDITOR_PLUGIN_H
171