| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-02-12 17:18:13 +01:00
										 |  |  | /*  gradient.h                                                           */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* 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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 17:18:13 +01:00
										 |  |  | #ifndef GRADIENT_H
 | 
					
						
							|  |  |  | #define GRADIENT_H
 | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/resource.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 21:46:48 -03:00
										 |  |  | class Gradient : public Resource { | 
					
						
							|  |  |  | 	GDCLASS(Gradient, Resource); | 
					
						
							|  |  |  | 	OBJ_SAVE_TYPE(Gradient); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	struct Point { | 
					
						
							|  |  |  | 		float offset; | 
					
						
							|  |  |  | 		Color color; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		bool operator<(const Point &p_ponit) const { | 
					
						
							|  |  |  | 			return offset < p_ponit.offset; | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	Vector<Point> points; | 
					
						
							| 
									
										
										
										
											2015-05-26 22:17:54 +03:00
										 |  |  | 	bool is_sorted; | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	static void _bind_methods(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-05-28 21:46:48 -03:00
										 |  |  | 	Gradient(); | 
					
						
							|  |  |  | 	virtual ~Gradient(); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void add_point(float p_offset, const Color &p_color); | 
					
						
							| 
									
										
										
										
											2015-12-12 09:45:17 -03:00
										 |  |  | 	void remove_point(int p_index); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	void set_points(Vector<Point> &p_points); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Vector<Point> &get_points(); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_offset(int pos, const float offset); | 
					
						
							|  |  |  | 	float get_offset(int pos) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void set_color(int pos, const Color &color); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 	Color get_color(int pos) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	void set_offsets(const Vector<float> &p_offsets); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 	Vector<float> get_offsets() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	void set_colors(const Vector<Color> &p_colors); | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 	Vector<Color> get_colors() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 	_FORCE_INLINE_ Color get_color_at_offset(float p_offset) { | 
					
						
							|  |  |  | 		if (points.empty()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			return Color(0, 0, 0, 1); | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (!is_sorted) { | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 			points.sort(); | 
					
						
							|  |  |  | 			is_sorted = true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//binary search
 | 
					
						
							|  |  |  | 		int low = 0; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int high = points.size() - 1; | 
					
						
							| 
									
										
										
										
											2017-09-01 22:33:39 +02:00
										 |  |  | 		int middle = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-05 23:20:20 +02:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-09-01 22:33:39 +02:00
										 |  |  | 		if (low > high) | 
					
						
							|  |  |  | 			ERR_PRINT("low > high, this may be a bug"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		while (low <= high) { | 
					
						
							|  |  |  | 			middle = (low + high) / 2; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 			const Point &point = points[middle]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (point.offset > p_offset) { | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 				high = middle - 1; //search low end of array
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			} else if (point.offset < p_offset) { | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 				low = middle + 1; //search high end of array
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return point.color; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//return interpolated value
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (points[middle].offset > p_offset) { | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 			middle--; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int first = middle; | 
					
						
							|  |  |  | 		int second = middle + 1; | 
					
						
							|  |  |  | 		if (second >= points.size()) | 
					
						
							|  |  |  | 			return points[points.size() - 1].color; | 
					
						
							|  |  |  | 		if (first < 0) | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 			return points[0].color; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		const Point &pointFirst = points[first]; | 
					
						
							|  |  |  | 		const Point &pointSecond = points[second]; | 
					
						
							| 
									
										
										
										
											2020-03-16 05:07:33 -04:00
										 |  |  | 		return pointFirst.color.lerp(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset)); | 
					
						
							| 
									
										
										
										
											2015-05-28 00:11:54 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-26 22:17:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-24 21:18:52 +03:00
										 |  |  | 	int get_points_count() const; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 17:18:13 +01:00
										 |  |  | #endif // GRADIENT_H
 |