| 1 | /**************************************************************************/ | 
| 2 | /*  gradient_texture.h                                                    */ | 
| 3 | /**************************************************************************/ | 
| 4 | /*                         This file is part of:                          */ | 
| 5 | /*                             GODOT ENGINE                               */ | 
| 6 | /*                        https://godotengine.org                         */ | 
| 7 | /**************************************************************************/ | 
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
| 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 GRADIENT_TEXTURE_H | 
| 32 | #define GRADIENT_TEXTURE_H | 
| 33 |  | 
| 34 | #include "scene/resources/texture.h" | 
| 35 |  | 
| 36 | class GradientTexture1D : public Texture2D { | 
| 37 | 	GDCLASS(GradientTexture1D, Texture2D); | 
| 38 |  | 
| 39 | private: | 
| 40 | 	Ref<Gradient> gradient; | 
| 41 | 	bool update_pending = false; | 
| 42 | 	mutable RID texture; | 
| 43 | 	int width = 256; | 
| 44 | 	bool use_hdr = false; | 
| 45 |  | 
| 46 | 	void _queue_update(); | 
| 47 | 	void _update(); | 
| 48 |  | 
| 49 | protected: | 
| 50 | 	static void _bind_methods(); | 
| 51 |  | 
| 52 | public: | 
| 53 | 	void set_gradient(Ref<Gradient> p_gradient); | 
| 54 | 	Ref<Gradient> get_gradient() const; | 
| 55 |  | 
| 56 | 	void set_width(int p_width); | 
| 57 | 	int get_width() const override; | 
| 58 |  | 
| 59 | 	void set_use_hdr(bool p_enabled); | 
| 60 | 	bool is_using_hdr() const; | 
| 61 |  | 
| 62 | 	virtual RID get_rid() const override; | 
| 63 | 	virtual int get_height() const override { return 1; } | 
| 64 | 	virtual bool has_alpha() const override { return true; } | 
| 65 |  | 
| 66 | 	virtual Ref<Image> get_image() const override; | 
| 67 | 	void update_now(); | 
| 68 |  | 
| 69 | 	GradientTexture1D(); | 
| 70 | 	virtual ~GradientTexture1D(); | 
| 71 | }; | 
| 72 |  | 
| 73 | class GradientTexture2D : public Texture2D { | 
| 74 | 	GDCLASS(GradientTexture2D, Texture2D); | 
| 75 |  | 
| 76 | public: | 
| 77 | 	enum Fill { | 
| 78 | 		FILL_LINEAR, | 
| 79 | 		FILL_RADIAL, | 
| 80 | 		FILL_SQUARE, | 
| 81 | 	}; | 
| 82 | 	enum Repeat { | 
| 83 | 		REPEAT_NONE, | 
| 84 | 		REPEAT, | 
| 85 | 		REPEAT_MIRROR, | 
| 86 | 	}; | 
| 87 |  | 
| 88 | private: | 
| 89 | 	Ref<Gradient> gradient; | 
| 90 | 	mutable RID texture; | 
| 91 |  | 
| 92 | 	int width = 64; | 
| 93 | 	int height = 64; | 
| 94 |  | 
| 95 | 	bool use_hdr = false; | 
| 96 |  | 
| 97 | 	Vector2 fill_from; | 
| 98 | 	Vector2 fill_to = Vector2(1, 0); | 
| 99 |  | 
| 100 | 	Fill fill = FILL_LINEAR; | 
| 101 | 	Repeat repeat = REPEAT_NONE; | 
| 102 |  | 
| 103 | 	float _get_gradient_offset_at(int x, int y) const; | 
| 104 |  | 
| 105 | 	bool update_pending = false; | 
| 106 | 	void _queue_update(); | 
| 107 | 	void _update(); | 
| 108 |  | 
| 109 | protected: | 
| 110 | 	static void _bind_methods(); | 
| 111 |  | 
| 112 | public: | 
| 113 | 	void set_gradient(Ref<Gradient> p_gradient); | 
| 114 | 	Ref<Gradient> get_gradient() const; | 
| 115 |  | 
| 116 | 	void set_width(int p_width); | 
| 117 | 	virtual int get_width() const override; | 
| 118 | 	void set_height(int p_height); | 
| 119 | 	virtual int get_height() const override; | 
| 120 |  | 
| 121 | 	void set_use_hdr(bool p_enabled); | 
| 122 | 	bool is_using_hdr() const; | 
| 123 |  | 
| 124 | 	void set_fill(Fill p_fill); | 
| 125 | 	Fill get_fill() const; | 
| 126 | 	void set_fill_from(Vector2 p_fill_from); | 
| 127 | 	Vector2 get_fill_from() const; | 
| 128 | 	void set_fill_to(Vector2 p_fill_to); | 
| 129 | 	Vector2 get_fill_to() const; | 
| 130 |  | 
| 131 | 	void set_repeat(Repeat p_repeat); | 
| 132 | 	Repeat get_repeat() const; | 
| 133 |  | 
| 134 | 	virtual RID get_rid() const override; | 
| 135 | 	virtual bool has_alpha() const override { return true; } | 
| 136 | 	virtual Ref<Image> get_image() const override; | 
| 137 | 	void update_now(); | 
| 138 |  | 
| 139 | 	GradientTexture2D(); | 
| 140 | 	virtual ~GradientTexture2D(); | 
| 141 | }; | 
| 142 |  | 
| 143 | VARIANT_ENUM_CAST(GradientTexture2D::Fill); | 
| 144 | VARIANT_ENUM_CAST(GradientTexture2D::Repeat); | 
| 145 |  | 
| 146 | #endif // GRADIENT_TEXTURE_H | 
| 147 |  |