Skip to content

Commit

Permalink
Fix consistency of GradientTexture changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Aug 29, 2023
1 parent cfe9cd5 commit 9f9210e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
39 changes: 27 additions & 12 deletions scene/resources/gradient_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ void GradientTexture1D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);
ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);

ClassDB::bind_method(D_METHOD("_update"), &GradientTexture1D::_update);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
Expand All @@ -72,7 +70,7 @@ void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {
if (gradient.is_valid()) {
gradient->connect_changed(callable_mp(this, &GradientTexture1D::_update));
}
_update();
_queue_update();
emit_changed();
}

Expand All @@ -84,9 +82,8 @@ void GradientTexture1D::_queue_update() {
if (update_pending) {
return;
}

update_pending = true;
call_deferred(SNAME("_update"));
callable_mp(this, &GradientTexture1D::update_now).call_deferred();
}

void GradientTexture1D::_update() {
Expand Down Expand Up @@ -140,14 +137,13 @@ void GradientTexture1D::_update() {
texture = RS::get_singleton()->texture_2d_create(image);
}
}

emit_changed();
}

void GradientTexture1D::set_width(int p_width) {
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
width = p_width;
_queue_update();
emit_changed();
}

int GradientTexture1D::get_width() const {
Expand All @@ -161,19 +157,27 @@ void GradientTexture1D::set_use_hdr(bool p_enabled) {

use_hdr = p_enabled;
_queue_update();
emit_changed();
}

bool GradientTexture1D::is_using_hdr() const {
return use_hdr;
}

Ref<Image> GradientTexture1D::get_image() const {
const_cast<GradientTexture1D *>(this)->update_now();
if (!texture.is_valid()) {
return Ref<Image>();
}
return RenderingServer::get_singleton()->texture_2d_get(texture);
}

void GradientTexture1D::update_now() {
if (update_pending) {
_update();
}
}

//////////////////

GradientTexture2D::GradientTexture2D() {
Expand All @@ -198,7 +202,7 @@ void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {
if (gradient.is_valid()) {
gradient->connect_changed(callable_mp(this, &GradientTexture2D::_queue_update));
}
_update();
_queue_update();
emit_changed();
}

Expand All @@ -211,7 +215,7 @@ void GradientTexture2D::_queue_update() {
return;
}
update_pending = true;
call_deferred(SNAME("_update"));
callable_mp(this, &GradientTexture2D::update_now).call_deferred();
}

void GradientTexture2D::_update() {
Expand Down Expand Up @@ -265,7 +269,6 @@ void GradientTexture2D::_update() {
} else {
texture = RS::get_singleton()->texture_2d_create(image);
}
emit_changed();
}

float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
Expand Down Expand Up @@ -315,6 +318,7 @@ void GradientTexture2D::set_width(int p_width) {
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
width = p_width;
_queue_update();
emit_changed();
}

int GradientTexture2D::get_width() const {
Expand All @@ -325,6 +329,7 @@ void GradientTexture2D::set_height(int p_height) {
ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");
height = p_height;
_queue_update();
emit_changed();
}
int GradientTexture2D::get_height() const {
return height;
Expand All @@ -337,6 +342,7 @@ void GradientTexture2D::set_use_hdr(bool p_enabled) {

use_hdr = p_enabled;
_queue_update();
emit_changed();
}

bool GradientTexture2D::is_using_hdr() const {
Expand All @@ -346,6 +352,7 @@ bool GradientTexture2D::is_using_hdr() const {
void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {
fill_from = p_fill_from;
_queue_update();
emit_changed();
}

Vector2 GradientTexture2D::get_fill_from() const {
Expand All @@ -355,6 +362,7 @@ Vector2 GradientTexture2D::get_fill_from() const {
void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {
fill_to = p_fill_to;
_queue_update();
emit_changed();
}

Vector2 GradientTexture2D::get_fill_to() const {
Expand All @@ -364,6 +372,7 @@ Vector2 GradientTexture2D::get_fill_to() const {
void GradientTexture2D::set_fill(Fill p_fill) {
fill = p_fill;
_queue_update();
emit_changed();
}

GradientTexture2D::Fill GradientTexture2D::get_fill() const {
Expand All @@ -373,6 +382,7 @@ GradientTexture2D::Fill GradientTexture2D::get_fill() const {
void GradientTexture2D::set_repeat(Repeat p_repeat) {
repeat = p_repeat;
_queue_update();
emit_changed();
}

GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {
Expand All @@ -387,12 +397,19 @@ RID GradientTexture2D::get_rid() const {
}

Ref<Image> GradientTexture2D::get_image() const {
const_cast<GradientTexture2D *>(this)->update_now();
if (!texture.is_valid()) {
return Ref<Image>();
}
return RenderingServer::get_singleton()->texture_2d_get(texture);
}

void GradientTexture2D::update_now() {
if (update_pending) {
_update();
}
}

void GradientTexture2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);
ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);
Expand All @@ -413,8 +430,6 @@ void GradientTexture2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);
ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);

ClassDB::bind_method(D_METHOD("_update"), &GradientTexture2D::_update);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");
Expand Down
2 changes: 2 additions & 0 deletions scene/resources/gradient_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class GradientTexture1D : public Texture2D {
virtual bool has_alpha() const override { return true; }

virtual Ref<Image> get_image() const override;
void update_now();

GradientTexture1D();
virtual ~GradientTexture1D();
Expand Down Expand Up @@ -133,6 +134,7 @@ class GradientTexture2D : public Texture2D {
virtual RID get_rid() const override;
virtual bool has_alpha() const override { return true; }
virtual Ref<Image> get_image() const override;
void update_now();

GradientTexture2D();
virtual ~GradientTexture2D();
Expand Down

0 comments on commit 9f9210e

Please sign in to comment.