summaryrefslogtreecommitdiff
path: root/scene/main/canvas_layer.cpp
blob: bc37cde4a27db04c88bfc9e0f7b5bf8303cefb4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*************************************************************************/
/*  canvas_layer.cpp                                                     */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 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.                */
/*************************************************************************/
#include "canvas_layer.h"
#include "viewport.h"


void CanvasLayer::set_layer(int p_xform) {

	layer=p_xform;
	if (viewport.is_valid())
		VisualServer::get_singleton()->viewport_set_canvas_layer(viewport,canvas->get_canvas(),layer);

}

int CanvasLayer::get_layer() const{

	return layer;
}

void CanvasLayer::set_transform(const Matrix32& p_xform) {

	transform=p_xform;
	locrotscale_dirty=true;
	if (viewport.is_valid())
		VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,canvas->get_canvas(),transform);

}

Matrix32 CanvasLayer::get_transform() const {

	return transform;
}

void CanvasLayer::_update_xform() {

	transform.set_rotation_and_scale(rot,scale);
	transform.set_origin(ofs);
	if (viewport.is_valid())
		VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,canvas->get_canvas(),transform);

}

void CanvasLayer::_update_locrotscale() {

	ofs=transform.elements[2];
	rot=transform.get_rotation();
	scale=transform.get_scale();
	locrotscale_dirty=false;
}


void CanvasLayer::set_offset(const Vector2& p_offset) {

	if (locrotscale_dirty)
		_update_locrotscale();

	ofs=p_offset;
	_update_xform();

}

Vector2 CanvasLayer::get_offset() const {

	if (locrotscale_dirty)
		const_cast<CanvasLayer*>(this)->_update_locrotscale();

	return ofs;
}


void CanvasLayer::set_rotation(real_t p_rotation) {

	if (locrotscale_dirty)
		_update_locrotscale();


	rot=p_rotation;
	_update_xform();

}

real_t CanvasLayer::get_rotation() const {

	if (locrotscale_dirty)
		const_cast<CanvasLayer*>(this)->_update_locrotscale();

	return rot;
}


void CanvasLayer::set_scale(const Vector2& p_scale) {

	if (locrotscale_dirty)
		_update_locrotscale();

	scale=p_scale;
	_update_xform();


}

Vector2 CanvasLayer::get_scale() const {

	if (locrotscale_dirty)
		const_cast<CanvasLayer*>(this)->_update_locrotscale();

	return scale;
}



Ref<World2D> CanvasLayer::get_world_2d() const {

	return canvas;
}

void CanvasLayer::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_ENTER_TREE: {

			Node *n = this;
			vp=NULL;

			while(n) {

				if (n->cast_to<Viewport>()) {

					vp = n->cast_to<Viewport>();
					break;
				}
				n=n->get_parent();
			}


			ERR_FAIL_COND(!vp);
			viewport=vp->get_viewport();

			VisualServer::get_singleton()->viewport_attach_canvas(viewport,canvas->get_canvas());
			VisualServer::get_singleton()->viewport_set_canvas_layer(viewport,canvas->get_canvas(),layer);
			VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,canvas->get_canvas(),transform);


		} break;
		case NOTIFICATION_EXIT_TREE: {

			VisualServer::get_singleton()->viewport_remove_canvas(viewport,canvas->get_canvas());
			viewport=RID();

		} break;
	}
}

Size2 CanvasLayer::get_viewport_size() const {

	if (!is_inside_tree())
		return Size2(1,1);

	Rect2 r = vp->get_visible_rect();
	return r.size;
}


RID CanvasLayer::get_viewport() const {

	return viewport;
}

void CanvasLayer::_set_rotationd(real_t p_rotation) {

	set_rotation(Math::deg2rad(p_rotation));
}

real_t CanvasLayer::_get_rotationd() const {

	return Math::rad2deg(get_rotation());
}


void CanvasLayer::_bind_methods() {


	ObjectTypeDB::bind_method(_MD("set_layer","layer"),&CanvasLayer::set_layer);
	ObjectTypeDB::bind_method(_MD("get_layer"),&CanvasLayer::get_layer);

	ObjectTypeDB::bind_method(_MD("set_transform","transform"),&CanvasLayer::set_transform);
	ObjectTypeDB::bind_method(_MD("get_transform"),&CanvasLayer::get_transform);

	ObjectTypeDB::bind_method(_MD("set_offset","offset"),&CanvasLayer::set_offset);
	ObjectTypeDB::bind_method(_MD("get_offset"),&CanvasLayer::get_offset);

	ObjectTypeDB::bind_method(_MD("set_rotation","rotation"),&CanvasLayer::set_rotation);
	ObjectTypeDB::bind_method(_MD("get_rotation"),&CanvasLayer::get_rotation);

	ObjectTypeDB::bind_method(_MD("_set_rotationd","rotationd"),&CanvasLayer::_set_rotationd);
	ObjectTypeDB::bind_method(_MD("_get_rotationd"),&CanvasLayer::_get_rotationd);

	ObjectTypeDB::bind_method(_MD("set_scale","scale"),&CanvasLayer::set_scale);
	ObjectTypeDB::bind_method(_MD("get_scale"),&CanvasLayer::get_scale);

	ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"),&CanvasLayer::get_world_2d);
	ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasLayer::get_viewport);

	ADD_PROPERTY( PropertyInfo(Variant::INT,"layer",PROPERTY_HINT_RANGE,"-128,128,1"),_SCS("set_layer"),_SCS("get_layer") );
	//ADD_PROPERTY( PropertyInfo(Variant::MATRIX32,"transform",PROPERTY_HINT_RANGE),_SCS("set_transform"),_SCS("get_transform") );
	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"offset"),_SCS("set_offset"),_SCS("get_offset") );
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"rotation"),_SCS("_set_rotationd"),_SCS("_get_rotationd") );
	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"scale"),_SCS("set_scale"),_SCS("get_scale") );

}

CanvasLayer::CanvasLayer() {

	vp=NULL;
	scale=Vector2(1,1);
	rot=0;
	locrotscale_dirty=false;
	layer=1;
	canvas = Ref<World2D>( memnew(World2D) );
}