gradient_texture.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef GRADIENT_TEXTURE_H
  31. #define GRADIENT_TEXTURE_H
  32. #include "scene/resources/texture.h"
  33. class GradientTexture1D : public Texture2D {
  34. GDCLASS(GradientTexture1D, Texture2D);
  35. private:
  36. Ref<Gradient> gradient;
  37. mutable bool update_pending = false;
  38. mutable RID texture;
  39. int width = 256;
  40. bool use_hdr = false;
  41. void _queue_update();
  42. void _update() const;
  43. protected:
  44. static void _bind_methods();
  45. public:
  46. void set_gradient(Ref<Gradient> p_gradient);
  47. Ref<Gradient> get_gradient() const;
  48. void set_width(int p_width);
  49. int get_width() const override;
  50. void set_use_hdr(bool p_enabled);
  51. bool is_using_hdr() const;
  52. virtual RID get_rid() const override;
  53. virtual int get_height() const override { return 1; }
  54. virtual bool has_alpha() const override { return true; }
  55. virtual Ref<Image> get_image() const override;
  56. void update_now() const;
  57. GradientTexture1D();
  58. virtual ~GradientTexture1D();
  59. };
  60. class GradientTexture2D : public Texture2D {
  61. GDCLASS(GradientTexture2D, Texture2D);
  62. public:
  63. enum Fill {
  64. FILL_LINEAR,
  65. FILL_RADIAL,
  66. FILL_SQUARE,
  67. };
  68. enum Repeat {
  69. REPEAT_NONE,
  70. REPEAT,
  71. REPEAT_MIRROR,
  72. };
  73. private:
  74. Ref<Gradient> gradient;
  75. mutable RID texture;
  76. int width = 64;
  77. int height = 64;
  78. bool use_hdr = false;
  79. Vector2 fill_from;
  80. Vector2 fill_to = Vector2(1, 0);
  81. Fill fill = FILL_LINEAR;
  82. Repeat repeat = REPEAT_NONE;
  83. float _get_gradient_offset_at(int x, int y) const;
  84. mutable bool update_pending = false;
  85. void _queue_update();
  86. void _update() const;
  87. protected:
  88. static void _bind_methods();
  89. public:
  90. void set_gradient(Ref<Gradient> p_gradient);
  91. Ref<Gradient> get_gradient() const;
  92. void set_width(int p_width);
  93. virtual int get_width() const override;
  94. void set_height(int p_height);
  95. virtual int get_height() const override;
  96. void set_use_hdr(bool p_enabled);
  97. bool is_using_hdr() const;
  98. void set_fill(Fill p_fill);
  99. Fill get_fill() const;
  100. void set_fill_from(Vector2 p_fill_from);
  101. Vector2 get_fill_from() const;
  102. void set_fill_to(Vector2 p_fill_to);
  103. Vector2 get_fill_to() const;
  104. void set_repeat(Repeat p_repeat);
  105. Repeat get_repeat() const;
  106. virtual RID get_rid() const override;
  107. virtual bool has_alpha() const override { return true; }
  108. virtual Ref<Image> get_image() const override;
  109. void update_now() const;
  110. GradientTexture2D();
  111. virtual ~GradientTexture2D();
  112. };
  113. VARIANT_ENUM_CAST(GradientTexture2D::Fill);
  114. VARIANT_ENUM_CAST(GradientTexture2D::Repeat);
  115. #endif // GRADIENT_TEXTURE_H