Skip to content

Commit

Permalink
Fix ColorPicker's RAW mode colors and values
Browse files Browse the repository at this point in the history
  • Loading branch information
WhalesState committed Jan 12, 2025
1 parent f120dc0 commit 4bac3b2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
1 change: 0 additions & 1 deletion scene/gui/color_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class ColorMode {

virtual void slider_draw(int p_which) = 0;
virtual bool apply_theme() const { return false; }
virtual ColorPicker::PickerShapeType get_shape_override() const { return ColorPicker::SHAPE_MAX; }

ColorMode(ColorPicker *p_color_picker);
virtual ~ColorMode() {};
Expand Down
39 changes: 16 additions & 23 deletions scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void ColorPicker::_update_controls() {
alpha_label->hide();
}

switch (_get_actual_shape()) {
switch (current_shape) {
case SHAPE_HSV_RECTANGLE:
wheel_edit->hide();
w_edit->show();
Expand Down Expand Up @@ -536,7 +536,7 @@ void ColorPicker::_copy_color_to_hsv() {
}

void ColorPicker::_copy_hsv_to_color() {
if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_OKHSL_CIRCLE) {
color.set_ok_hsl(ok_hsl_h, ok_hsl_s, ok_hsl_l, color.a);
} else {
color.set_hsv(h, s, v, color.a);
Expand Down Expand Up @@ -568,10 +568,6 @@ bool ColorPicker::_select_from_recent_preset_hbc(const Color &p_color) {
return false;
}

ColorPicker::PickerShapeType ColorPicker::_get_actual_shape() const {
return modes[current_mode]->get_shape_override() != SHAPE_MAX ? modes[current_mode]->get_shape_override() : current_shape;
}

void ColorPicker::_reset_sliders_theme() {
Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale);
Expand Down Expand Up @@ -1153,19 +1149,18 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
return;
}

PickerShapeType actual_shape = _get_actual_shape();
if (p_which == 0) {
Color col = color;
Vector2 center = c->get_size() / 2.0;

if (actual_shape == SHAPE_HSV_RECTANGLE || actual_shape == SHAPE_HSV_WHEEL) {
if (current_shape == SHAPE_HSV_RECTANGLE || current_shape == SHAPE_HSV_WHEEL) {
Vector<Point2> points;
Vector<Color> colors;
Vector<Color> colors2;
points.resize(4);
colors.resize(4);
colors2.resize(4);
if (actual_shape == SHAPE_HSV_RECTANGLE) {
if (current_shape == SHAPE_HSV_RECTANGLE) {
points.set(0, Vector2());
points.set(1, Vector2(c->get_size().x, 0));
points.set(2, c->get_size());
Expand Down Expand Up @@ -1199,9 +1194,9 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {

int x;
int y;
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE || current_shape == SHAPE_OKHSL_CIRCLE) {
Vector2 hue_offset;
if (actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_OKHSL_CIRCLE) {
hue_offset = center * Vector2(Math::cos(ok_hsl_h * Math_TAU), Math::sin(ok_hsl_h * Math_TAU)) * ok_hsl_s;
} else {
hue_offset = center * Vector2(Math::cos(h * Math_TAU), Math::sin(h * Math_TAU)) * s;
Expand All @@ -1221,23 +1216,23 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
c->draw_texture(theme_cache.picker_cursor_bg, Point2(x, y), _col);
c->draw_texture(theme_cache.picker_cursor, Point2(x, y));

if (actual_shape == SHAPE_HSV_WHEEL) {
if (current_shape == SHAPE_HSV_WHEEL) {
float _radius = WHEEL_RADIUS * 2.0;
_radius += (1.0 - _radius) * 0.5;
Point2 pos = center - (theme_cache.picker_cursor->get_size() * 0.5) + Point2(center.x * Math::cos(h * Math_TAU) * _radius, center.y * Math::sin(h * Math_TAU) * _radius);
c->draw_texture(theme_cache.picker_cursor, pos);
}

} else if (p_which == 1) {
if (actual_shape == SHAPE_HSV_RECTANGLE) {
if (current_shape == SHAPE_HSV_RECTANGLE) {
c->draw_set_transform(Point2(), -Math_PI / 2, Size2(c->get_size().x, -c->get_size().y));
c->draw_texture_rect(theme_cache.color_hue, Rect2(Point2(), Size2(1, 1)));
c->draw_set_transform(Point2(), 0, Size2(1, 1));
int y = c->get_size().y - c->get_size().y * (1.0 - h);
Color col;
col.set_hsv(h, 1, 1);
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
} else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
} else if (current_shape == SHAPE_OKHSL_CIRCLE) {
Vector<Point2> points;
Vector<Color> colors;
Color col;
Expand All @@ -1264,7 +1259,7 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
int y = c->get_size().y - c->get_size().y * CLAMP(ok_hsl_l, 0, 1);
col.set_ok_hsl(ok_hsl_h, 1, ok_hsl_l);
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
} else if (actual_shape == SHAPE_VHS_CIRCLE) {
} else if (current_shape == SHAPE_VHS_CIRCLE) {
Vector<Point2> points;
Vector<Color> colors;
Color col;
Expand All @@ -1286,9 +1281,9 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
}
} else if (p_which == 2) {
c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
if (actual_shape == SHAPE_VHS_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE) {
circle_mat->set_shader_parameter("v", v);
} else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
} else if (current_shape == SHAPE_OKHSL_CIRCLE) {
circle_mat->set_shader_parameter("ok_hsl_l", ok_hsl_l);
}
}
Expand All @@ -1302,12 +1297,11 @@ void ColorPicker::_slider_draw(int p_which) {

void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
Ref<InputEventMouseButton> bev = p_event;
PickerShapeType actual_shape = _get_actual_shape();

if (bev.is_valid()) {
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
Vector2 center = c->get_size() / 2.0;
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE || current_shape == SHAPE_OKHSL_CIRCLE) {
real_t dist = center.distance_to(bev->get_position());
if (dist <= center.x) {
real_t rad = center.angle_to_point(bev->get_position());
Expand Down Expand Up @@ -1377,7 +1371,7 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
}

Vector2 center = c->get_size() / 2.0;
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE || current_shape == SHAPE_OKHSL_CIRCLE) {
real_t dist = center.distance_to(mev->get_position());
real_t rad = center.angle_to_point(mev->get_position());
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
Expand Down Expand Up @@ -1413,13 +1407,12 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {

void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> bev = p_event;
PickerShapeType actual_shape = _get_actual_shape();

if (bev.is_valid()) {
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
changing_color = true;
float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE || current_shape == SHAPE_OKHSL_CIRCLE) {
v = 1.0 - (y / w_edit->get_size().height);
ok_hsl_l = v;
} else {
Expand Down Expand Up @@ -1448,7 +1441,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
return;
}
float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
if (current_shape == SHAPE_VHS_CIRCLE || current_shape == SHAPE_OKHSL_CIRCLE) {
v = 1.0 - (y / w_edit->get_size().height);
ok_hsl_l = v;
} else {
Expand Down
1 change: 0 additions & 1 deletion scene/gui/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ class ColorPicker : public VBoxContainer {
void _copy_color_to_hsv();
void _copy_hsv_to_color();

PickerShapeType _get_actual_shape() const;
void create_slider(GridContainer *gc, int idx);
void _reset_sliders_theme();
void _html_submitted(const String &p_html);
Expand Down

0 comments on commit 4bac3b2

Please sign in to comment.