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-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Author: Mariano Suligoy                                               */
12 /*                                                                       */
13 /* Permission is hereby granted, free of charge, to any person obtaining */
14 /* a copy of this software and associated documentation files (the       */
15 /* "Software"), to deal in the Software without restriction, including   */
16 /* without limitation the rights to use, copy, modify, merge, publish,   */
17 /* distribute, sublicense, and/or sell copies of the Software, and to    */
18 /* permit persons to whom the Software is furnished to do so, subject to */
19 /* the following conditions:                                             */
20 /*                                                                       */
21 /* The above copyright notice and this permission notice shall be        */
22 /* included in all copies or substantial portions of the Software.       */
23 /*                                                                       */
24 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
25 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
26 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
27 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
28 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
29 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
30 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
31 /*************************************************************************/
32 
33 #ifndef TEXTURE_REGION_EDITOR_PLUGIN_H
34 #define TEXTURE_REGION_EDITOR_PLUGIN_H
35 
36 #include "canvas_item_editor_plugin.h"
37 #include "editor/editor_node.h"
38 #include "editor/editor_plugin.h"
39 #include "scene/2d/sprite.h"
40 #include "scene/gui/patch_9_frame.h"
41 #include "scene/resources/style_box.h"
42 #include "scene/resources/texture.h"
43 
44 class TextureRegionEditor : public Control {
45 
46 	OBJ_TYPE(TextureRegionEditor, Control);
47 
48 	enum SnapMode {
49 		SNAP_NONE,
50 		SNAP_PIXEL,
51 		SNAP_GRID,
52 		SNAP_AUTOSLICE
53 	};
54 
55 	friend class TextureRegionEditorPlugin;
56 	MenuButton *snap_mode_button;
57 	TextureFrame *icon_zoom;
58 	ToolButton *zoom_in;
59 	ToolButton *zoom_reset;
60 	ToolButton *zoom_out;
61 	HBoxContainer *hb_grid; //For showing/hiding the grid controls when changing the SnapMode
62 	SpinBox *sb_step_y;
63 	SpinBox *sb_step_x;
64 	SpinBox *sb_off_y;
65 	SpinBox *sb_off_x;
66 	SpinBox *sb_sep_y;
67 	SpinBox *sb_sep_x;
68 	Control *edit_draw;
69 
70 	VScrollBar *vscroll;
71 	HScrollBar *hscroll;
72 
73 	EditorNode *editor;
74 	UndoRedo *undo_redo;
75 
76 	Vector2 draw_ofs;
77 	float draw_zoom;
78 	bool updating_scroll;
79 
80 	int snap_mode;
81 	Vector2 snap_offset;
82 	Vector2 snap_step;
83 	Vector2 snap_separation;
84 
85 	Patch9Frame *node_patch9;
86 	Sprite *node_sprite;
87 	Ref<StyleBoxTexture> obj_styleBox;
88 	Ref<AtlasTexture> atlas_tex;
89 
90 	Rect2 rect;
91 	Rect2 rect_prev;
92 	float prev_margin;
93 	int edited_margin;
94 	List<Rect2> autoslice_cache;
95 
96 	bool drag;
97 	bool creating;
98 	Vector2 drag_from;
99 	int drag_index;
100 
101 	void _set_snap_mode(int p_mode);
102 	void _set_snap_off_x(float p_val);
103 	void _set_snap_off_y(float p_val);
104 	void _set_snap_step_x(float p_val);
105 	void _set_snap_step_y(float p_val);
106 	void _set_snap_sep_x(float p_val);
107 	void _set_snap_sep_y(float p_val);
108 	void _zoom_in();
109 	void _zoom_reset();
110 	void _zoom_out();
111 	void apply_rect(const Rect2 &rect);
112 
113 protected:
114 	void _notification(int p_what);
115 	void _node_removed(Object *p_obj);
116 	static void _bind_methods();
117 
118 	Vector2 snap_point(Vector2 p_target) const;
119 
120 	virtual void _changed_callback(Object *p_changed, const char *p_prop);
121 
122 public:
123 	void _edit_region();
124 	void _region_draw();
125 	void _region_input(const InputEvent &p_input);
126 	void _scroll_changed(float);
127 
128 	void edit(Object *p_obj);
129 	TextureRegionEditor(EditorNode *p_editor);
130 };
131 
132 class TextureRegionEditorPlugin : public EditorPlugin {
133 	OBJ_TYPE(TextureRegionEditorPlugin, EditorPlugin);
134 
135 	Button *region_button;
136 	TextureRegionEditor *region_editor;
137 	EditorNode *editor;
138 
139 public:
get_name()140 	virtual String get_name() const { return "TextureRegion"; }
has_main_screen()141 	bool has_main_screen() const { return false; }
142 	virtual void edit(Object *p_node);
143 	virtual bool handles(Object *p_node) const;
144 	virtual void make_visible(bool p_visible);
145 	void set_state(const Dictionary &p_state);
146 	Dictionary get_state() const;
147 
148 	TextureRegionEditorPlugin(EditorNode *p_node);
149 };
150 
151 #endif // TEXTURE_REGION_EDITOR_PLUGIN_H
152