gradient_texture.h 4.5 KB

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