summaryrefslogtreecommitdiff
path: root/scene/2d/light_2d.cpp
blob: 2935f6b9f709adc993d3166d33e5e06e01959d2e (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*************************************************************************/
/*  light_2d.cpp                                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 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 "light_2d.h"
#include "servers/visual_server.h"

void Light2D::edit_set_pivot(const Point2& p_pivot) {

	set_texture_offset(p_pivot);

}

Point2 Light2D::edit_get_pivot() const {

	return get_texture_offset();
}
bool Light2D::edit_has_pivot() const {

	return true;
}

Rect2 Light2D::get_item_rect() const {

	if (texture.is_null())
		return Rect2(0,0,1,1);

	Size2i s;

	s = texture->get_size()*_scale;
	Point2i ofs=texture_offset;
	ofs-=s/2;

	if (s==Size2(0,0))
		s=Size2(1,1);

	return Rect2(ofs,s);
}


void Light2D::_update_light_visibility() {

	if (!is_inside_tree())
		return;

	bool editor_ok=true;

#ifdef TOOLS_ENABLED
	if (editor_only) {
		if (!get_tree()->is_editor_hint()) {
			editor_ok=false;
		} else {
			editor_ok = (get_tree()->get_edited_scene_root() && (this==get_tree()->get_edited_scene_root() || get_owner()==get_tree()->get_edited_scene_root()));
		}
	}
#else
	if (editor_only) {
		editor_ok=false;
	}
#endif

	VS::get_singleton()->canvas_light_set_enabled(canvas_light,enabled && is_visible_in_tree() && editor_ok);
}

void Light2D::set_enabled( bool p_enabled) {


	enabled=p_enabled;
	_update_light_visibility();
}

bool Light2D::is_enabled() const {

	return enabled;
}

void Light2D::set_editor_only(bool p_editor_only) {

	editor_only=p_editor_only;
	_update_light_visibility();
}

bool Light2D::is_editor_only() const{

	return editor_only;
}

void Light2D::set_texture( const Ref<Texture>& p_texture) {

	texture=p_texture;
	if (texture.is_valid())
		VS::get_singleton()->canvas_light_set_texture(canvas_light,texture->get_rid());
	else
		VS::get_singleton()->canvas_light_set_texture(canvas_light,RID());

	update_configuration_warning();
}

Ref<Texture> Light2D::get_texture() const {

	return texture;
}

void Light2D::set_texture_offset( const Vector2& p_offset) {

	texture_offset=p_offset;
	VS::get_singleton()->canvas_light_set_texture_offset(canvas_light,texture_offset);
	item_rect_changed();

}

Vector2 Light2D::get_texture_offset() const {

	return texture_offset;
}

void Light2D::set_color( const Color& p_color) {

	color=p_color;
	VS::get_singleton()->canvas_light_set_color(canvas_light,color);

}
Color Light2D::get_color() const {

	return color;
}

void Light2D::set_height( float p_height) {

	height=p_height;
	VS::get_singleton()->canvas_light_set_height(canvas_light,height);

}


float Light2D::get_height() const {

	return height;
}

void Light2D::set_energy( float p_energy) {

	energy=p_energy;
	VS::get_singleton()->canvas_light_set_energy(canvas_light,energy);

}


float Light2D::get_energy() const {

	return energy;
}



void Light2D::set_texture_scale( float p_scale) {

	_scale=p_scale;
	VS::get_singleton()->canvas_light_set_scale(canvas_light,_scale);
	item_rect_changed();

}


float Light2D::get_texture_scale() const {

	return _scale;
}

void Light2D::set_z_range_min( int p_min_z) {

	z_min=p_min_z;
	VS::get_singleton()->canvas_light_set_z_range(canvas_light,z_min,z_max);

}
int Light2D::get_z_range_min() const {

	return z_min;
}

void Light2D::set_z_range_max( int p_max_z) {

	z_max=p_max_z;
	VS::get_singleton()->canvas_light_set_z_range(canvas_light,z_min,z_max);

}
int Light2D::get_z_range_max() const {

	return z_max;
}

void Light2D::set_layer_range_min( int p_min_layer) {

	layer_min=p_min_layer;
	VS::get_singleton()->canvas_light_set_layer_range(canvas_light,layer_min,layer_max);

}
int Light2D::get_layer_range_min() const {

	return layer_min;
}

void Light2D::set_layer_range_max( int p_max_layer) {

	layer_max=p_max_layer;
	VS::get_singleton()->canvas_light_set_layer_range(canvas_light,layer_min,layer_max);

}
int Light2D::get_layer_range_max() const {

	return layer_max;
}

void Light2D::set_item_cull_mask( int p_mask) {

	item_mask=p_mask;
	VS::get_singleton()->canvas_light_set_item_cull_mask(canvas_light,item_mask);

}

int Light2D::get_item_cull_mask() const {

	return item_mask;
}

void Light2D::set_item_shadow_cull_mask( int p_mask) {

	item_shadow_mask=p_mask;
	VS::get_singleton()->canvas_light_set_item_shadow_cull_mask(canvas_light,item_shadow_mask);

}

int Light2D::get_item_shadow_cull_mask() const {

	return item_shadow_mask;
}

void Light2D::set_mode( Mode p_mode ) {

	mode=p_mode;
	VS::get_singleton()->canvas_light_set_mode(canvas_light,VS::CanvasLightMode(p_mode));
}

Light2D::Mode Light2D::get_mode() const {

	return mode;
}

void Light2D::set_shadow_enabled( bool p_enabled) {

	shadow=p_enabled;
	VS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light,shadow);

}
bool Light2D::is_shadow_enabled() const {

	return shadow;
}

void Light2D::set_shadow_buffer_size( int p_size ) {

	shadow_buffer_size=p_size;
	VS::get_singleton()->canvas_light_set_shadow_buffer_size(canvas_light,shadow_buffer_size);
}

int Light2D::get_shadow_buffer_size() const {

	return shadow_buffer_size;
}

void Light2D::set_shadow_gradient_length( float p_multiplier) {

	shadow_gradient_length=p_multiplier;
	VS::get_singleton()->canvas_light_set_shadow_gradient_length(canvas_light,p_multiplier);
}

float Light2D::get_shadow_gradient_length() const{

	return shadow_gradient_length;
}


void Light2D::set_shadow_filter( ShadowFilter p_filter) {
	shadow_filter=p_filter;
	VS::get_singleton()->canvas_light_set_shadow_filter(canvas_light,VS::CanvasLightShadowFilter(p_filter ));
}

Light2D::ShadowFilter Light2D::get_shadow_filter() const {

	return shadow_filter;
}



void Light2D::set_shadow_color( const Color& p_shadow_color) {
	shadow_color=p_shadow_color;
	VS::get_singleton()->canvas_light_set_shadow_color(canvas_light,shadow_color);
}

Color Light2D::get_shadow_color() const {
	return shadow_color;
}



void Light2D::_notification(int p_what) {

	if (p_what==NOTIFICATION_ENTER_TREE) {

		VS::get_singleton()->canvas_light_attach_to_canvas( canvas_light, get_canvas() );
		_update_light_visibility();
	}

	if (p_what==NOTIFICATION_TRANSFORM_CHANGED) {

		VS::get_singleton()->canvas_light_set_transform( canvas_light, get_global_transform());
	}
	if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {

		_update_light_visibility();
	}

	if (p_what==NOTIFICATION_EXIT_TREE) {

		VS::get_singleton()->canvas_light_attach_to_canvas( canvas_light, RID() );
		_update_light_visibility();
	}

}

String Light2D::get_configuration_warning() const {

	if (!texture.is_valid()) {
		return TTR("A texture with the shape of the light must be supplied to the 'texture' property.");
	}

	return String();
}


void Light2D::_bind_methods() {


	ClassDB::bind_method(_MD("set_enabled","enabled"),&Light2D::set_enabled);
	ClassDB::bind_method(_MD("is_enabled"),&Light2D::is_enabled);

	ClassDB::bind_method(_MD("set_editor_only","editor_only"), &Light2D::set_editor_only );
	ClassDB::bind_method(_MD("is_editor_only"), &Light2D::is_editor_only );

	ClassDB::bind_method(_MD("set_texture","texture"),&Light2D::set_texture);
	ClassDB::bind_method(_MD("get_texture"),&Light2D::get_texture);

	ClassDB::bind_method(_MD("set_texture_offset","texture_offset"),&Light2D::set_texture_offset);
	ClassDB::bind_method(_MD("get_texture_offset"),&Light2D::get_texture_offset);

	ClassDB::bind_method(_MD("set_color","color"),&Light2D::set_color);
	ClassDB::bind_method(_MD("get_color"),&Light2D::get_color);

	ClassDB::bind_method(_MD("set_height","height"),&Light2D::set_height);
	ClassDB::bind_method(_MD("get_height"),&Light2D::get_height);

	ClassDB::bind_method(_MD("set_energy","energy"),&Light2D::set_energy);
	ClassDB::bind_method(_MD("get_energy"),&Light2D::get_energy);

	ClassDB::bind_method(_MD("set_texture_scale","texture_scale"),&Light2D::set_texture_scale);
	ClassDB::bind_method(_MD("get_texture_scale"),&Light2D::get_texture_scale);


	ClassDB::bind_method(_MD("set_z_range_min","z"),&Light2D::set_z_range_min);
	ClassDB::bind_method(_MD("get_z_range_min"),&Light2D::get_z_range_min);

	ClassDB::bind_method(_MD("set_z_range_max","z"),&Light2D::set_z_range_max);
	ClassDB::bind_method(_MD("get_z_range_max"),&Light2D::get_z_range_max);

	ClassDB::bind_method(_MD("set_layer_range_min","layer"),&Light2D::set_layer_range_min);
	ClassDB::bind_method(_MD("get_layer_range_min"),&Light2D::get_layer_range_min);

	ClassDB::bind_method(_MD("set_layer_range_max","layer"),&Light2D::set_layer_range_max);
	ClassDB::bind_method(_MD("get_layer_range_max"),&Light2D::get_layer_range_max);


	ClassDB::bind_method(_MD("set_item_cull_mask","item_cull_mask"),&Light2D::set_item_cull_mask);
	ClassDB::bind_method(_MD("get_item_cull_mask"),&Light2D::get_item_cull_mask);

	ClassDB::bind_method(_MD("set_item_shadow_cull_mask","item_shadow_cull_mask"),&Light2D::set_item_shadow_cull_mask);
	ClassDB::bind_method(_MD("get_item_shadow_cull_mask"),&Light2D::get_item_shadow_cull_mask);

	ClassDB::bind_method(_MD("set_mode","mode"),&Light2D::set_mode);
	ClassDB::bind_method(_MD("get_mode"),&Light2D::get_mode);

	ClassDB::bind_method(_MD("set_shadow_enabled","enabled"),&Light2D::set_shadow_enabled);
	ClassDB::bind_method(_MD("is_shadow_enabled"),&Light2D::is_shadow_enabled);

	ClassDB::bind_method(_MD("set_shadow_buffer_size","size"),&Light2D::set_shadow_buffer_size);
	ClassDB::bind_method(_MD("get_shadow_buffer_size"),&Light2D::get_shadow_buffer_size);

	ClassDB::bind_method(_MD("set_shadow_gradient_length","multiplier"),&Light2D::set_shadow_gradient_length);
	ClassDB::bind_method(_MD("get_shadow_gradient_length"),&Light2D::get_shadow_gradient_length);

	ClassDB::bind_method(_MD("set_shadow_filter","filter"),&Light2D::set_shadow_filter);
	ClassDB::bind_method(_MD("get_shadow_filter"),&Light2D::get_shadow_filter);

	ClassDB::bind_method(_MD("set_shadow_color","shadow_color"),&Light2D::set_shadow_color);
	ClassDB::bind_method(_MD("get_shadow_color"),&Light2D::get_shadow_color);


	ADD_PROPERTY( PropertyInfo(Variant::BOOL,"enabled"),_SCS("set_enabled"),_SCS("is_enabled"));
	ADD_PROPERTY( PropertyInfo(Variant::BOOL, "editor_only"),_SCS("set_editor_only"),_SCS("is_editor_only"));
	ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture"));
	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"offset"),_SCS("set_texture_offset"),_SCS("get_texture_offset"));
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"scale",PROPERTY_HINT_RANGE,"0.01,50,0.01"),_SCS("set_texture_scale"),_SCS("get_texture_scale"));
	ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color"));
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"energy",PROPERTY_HINT_RANGE,"0.01,100,0.01"),_SCS("set_energy"),_SCS("get_energy"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Add,Sub,Mix,Mask"),_SCS("set_mode"),_SCS("get_mode"));
	ADD_GROUP("Range","range_");
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"range_height",PROPERTY_HINT_RANGE,"-100,100,0.1"),_SCS("set_height"),_SCS("get_height"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"range_z_min",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_min"),_SCS("get_z_range_min"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"range_z_max",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_max"),_SCS("get_z_range_max"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"range_layer_min",PROPERTY_HINT_RANGE,"-512,512,1"),_SCS("set_layer_range_min"),_SCS("get_layer_range_min"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"range_layer_max",PROPERTY_HINT_RANGE,"-512,512,1"),_SCS("set_layer_range_max"),_SCS("get_layer_range_max"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"range_item_cull_mask",PROPERTY_HINT_LAYERS_2D_RENDER),_SCS("set_item_cull_mask"),_SCS("get_item_cull_mask"));

	ADD_GROUP("Shadow","shadow_");
	ADD_PROPERTY( PropertyInfo(Variant::BOOL,"shadow_enabled"),_SCS("set_shadow_enabled"),_SCS("is_shadow_enabled"));
	ADD_PROPERTY( PropertyInfo(Variant::COLOR,"shadow_color"),_SCS("set_shadow_color"),_SCS("get_shadow_color"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"shadow_buffer_size",PROPERTY_HINT_RANGE,"32,16384,1"),_SCS("set_shadow_buffer_size"),_SCS("get_shadow_buffer_size"));
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"shadow_gradient_length",PROPERTY_HINT_RANGE,"1,4096,0.1"),_SCS("set_shadow_gradient_length"),_SCS("get_shadow_gradient_length"));
	ADD_PROPERTY( PropertyInfo(Variant::REAL,"shadow_filter",PROPERTY_HINT_ENUM,"None,PCF3,PCF5,PCF9,PCF13"),_SCS("set_shadow_filter"),_SCS("get_shadow_filter"));
	ADD_PROPERTY( PropertyInfo(Variant::INT,"shadow_item_cull_mask",PROPERTY_HINT_LAYERS_2D_RENDER),_SCS("set_item_shadow_cull_mask"),_SCS("get_item_shadow_cull_mask"));

	BIND_CONSTANT( MODE_ADD );
	BIND_CONSTANT( MODE_SUB );
	BIND_CONSTANT( MODE_MIX );
	BIND_CONSTANT( MODE_MASK );


}

Light2D::Light2D() {

	canvas_light=VisualServer::get_singleton()->canvas_light_create();
	enabled=true;
	editor_only=false;
	shadow=false;
	color=Color(1,1,1);
	height=0;
	_scale=1.0;
	z_min=-1024;
	z_max=1024;
	layer_min=0;
	layer_max=0;
	item_mask=1;
	item_shadow_mask=1;
	mode=MODE_ADD;
	shadow_buffer_size=2048;
	shadow_gradient_length=0;
	energy=1.0;
	shadow_color=Color(0,0,0,0);
	shadow_filter=SHADOW_FILTER_NONE;

	set_notify_transform(true);
}

Light2D::~Light2D() {

	VisualServer::get_singleton()->free(canvas_light);
}