Skip to content

Commit d3402f7

Browse files
WhalesStateSplizard
authored andcommitted
Add ColorPicker cursor background and reuse the cursor for wheel.
Add a cursor's background to fill the picker cursor. Unhardcode the wheel radius. Reuse the picker cursor image for the HSV wheel.
1 parent 05ef2f9 commit d3402f7

9 files changed

+59
-72
lines changed

doc/classes/ColorPicker.xml

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@
183183
<theme_item name="picker_cursor" data_type="icon" type="Texture2D">
184184
The image displayed over the color box/circle (depending on the [member picker_shape]), marking the currently selected color.
185185
</theme_item>
186+
<theme_item name="picker_cursor_bg" data_type="icon" type="Texture2D">
187+
The fill image displayed behind the picker cursor.
188+
</theme_item>
186189
<theme_item name="sample_bg" data_type="icon" type="Texture2D">
187190
Background panel for the color preview box (visible when the color is translucent).
188191
</theme_item>

editor/icons/PickerCursor.svg

+1-1
Loading

editor/icons/PickerCursorBg.svg

+1
Loading

editor/themes/editor_theme_manager.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@ void EditorThemeManager::_populate_standard_styles(const Ref<EditorTheme> &p_the
17671767
p_theme->set_icon("overbright_indicator", "ColorPicker", p_theme->get_icon(SNAME("OverbrightIndicator"), EditorStringName(EditorIcons)));
17681768
p_theme->set_icon("bar_arrow", "ColorPicker", p_theme->get_icon(SNAME("ColorPickerBarArrow"), EditorStringName(EditorIcons)));
17691769
p_theme->set_icon("picker_cursor", "ColorPicker", p_theme->get_icon(SNAME("PickerCursor"), EditorStringName(EditorIcons)));
1770+
p_theme->set_icon("picker_cursor_bg", "ColorPicker", p_theme->get_icon(SNAME("PickerCursorBg"), EditorStringName(EditorIcons)));
17701771

17711772
// ColorPickerButton.
17721773
p_theme->set_icon("bg", "ColorPickerButton", p_theme->get_icon(SNAME("GuiMiniCheckerboard"), EditorStringName(EditorIcons)));

scene/gui/color_picker.cpp

+48-70
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,21 @@ void ColorPicker::init_shaders() {
193193
194194
shader_type canvas_item;
195195
196+
uniform float wheel_radius = 0.42;
197+
196198
void fragment() {
197199
float x = UV.x - 0.5;
198200
float y = UV.y - 0.5;
199201
float a = atan(y, x);
200202
x += 0.001;
201203
y += 0.001;
202-
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
204+
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
203205
x -= 0.002;
204-
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
206+
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
205207
y -= 0.002;
206-
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
208+
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
207209
x += 0.002;
208-
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
210+
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
209211
210212
COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
211213
}
@@ -1315,70 +1317,46 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
13151317

13161318
PickerShapeType actual_shape = _get_actual_shape();
13171319
if (p_which == 0) {
1318-
Vector<Point2> points;
1319-
Vector<Color> colors;
1320-
Vector<Color> colors2;
13211320
Color col = color;
13221321
Vector2 center = c->get_size() / 2.0;
13231322

1324-
switch (actual_shape) {
1325-
case SHAPE_HSV_WHEEL: {
1326-
points.resize(4);
1327-
colors.resize(4);
1328-
colors2.resize(4);
1329-
real_t ring_radius_x = Math_SQRT12 * c->get_size().width * 0.42;
1330-
real_t ring_radius_y = Math_SQRT12 * c->get_size().height * 0.42;
1323+
if (actual_shape == SHAPE_HSV_RECTANGLE || actual_shape == SHAPE_HSV_WHEEL) {
1324+
Vector<Point2> points;
1325+
Vector<Color> colors;
1326+
Vector<Color> colors2;
1327+
points.resize(4);
1328+
colors.resize(4);
1329+
colors2.resize(4);
1330+
if (actual_shape == SHAPE_HSV_RECTANGLE) {
1331+
points.set(0, Vector2());
1332+
points.set(1, Vector2(c->get_size().x, 0));
1333+
points.set(2, c->get_size());
1334+
points.set(3, Vector2(0, c->get_size().y));
1335+
} else {
1336+
real_t ring_radius_x = Math_SQRT12 * c->get_size().width * WHEEL_RADIUS;
1337+
real_t ring_radius_y = Math_SQRT12 * c->get_size().height * WHEEL_RADIUS;
13311338

13321339
points.set(0, center - Vector2(ring_radius_x, ring_radius_y));
13331340
points.set(1, center + Vector2(ring_radius_x, -ring_radius_y));
13341341
points.set(2, center + Vector2(ring_radius_x, ring_radius_y));
13351342
points.set(3, center + Vector2(-ring_radius_x, ring_radius_y));
1336-
colors.set(0, Color(1, 1, 1, 1));
1337-
colors.set(1, Color(1, 1, 1, 1));
1338-
colors.set(2, Color(0, 0, 0, 1));
1339-
colors.set(3, Color(0, 0, 0, 1));
1340-
c->draw_polygon(points, colors);
1341-
1342-
col.set_hsv(h, 1, 1);
1343-
col.a = 0;
1344-
colors2.set(0, col);
1345-
col.a = 1;
1346-
colors2.set(1, col);
1347-
col.set_hsv(h, 1, 0);
1348-
colors2.set(2, col);
1349-
col.a = 0;
1350-
colors2.set(3, col);
1351-
c->draw_polygon(points, colors2);
1352-
break;
1353-
}
1354-
case SHAPE_HSV_RECTANGLE: {
1355-
points.resize(4);
1356-
colors.resize(4);
1357-
colors2.resize(4);
1358-
points.set(0, Vector2());
1359-
points.set(1, Vector2(c->get_size().x, 0));
1360-
points.set(2, c->get_size());
1361-
points.set(3, Vector2(0, c->get_size().y));
1362-
colors.set(0, Color(1, 1, 1, 1));
1363-
colors.set(1, Color(1, 1, 1, 1));
1364-
colors.set(2, Color(0, 0, 0, 1));
1365-
colors.set(3, Color(0, 0, 0, 1));
1366-
c->draw_polygon(points, colors);
1367-
col = color;
1368-
col.set_hsv(h, 1, 1);
1369-
col.a = 0;
1370-
colors2.set(0, col);
1371-
col.a = 1;
1372-
colors2.set(1, col);
1373-
col.set_hsv(h, 1, 0);
1374-
colors2.set(2, col);
1375-
col.a = 0;
1376-
colors2.set(3, col);
1377-
c->draw_polygon(points, colors2);
1378-
break;
1379-
}
1380-
default: {
13811343
}
1344+
colors.set(0, Color(1, 1, 1, 1));
1345+
colors.set(1, Color(1, 1, 1, 1));
1346+
colors.set(2, Color(0, 0, 0, 1));
1347+
colors.set(3, Color(0, 0, 0, 1));
1348+
c->draw_polygon(points, colors);
1349+
1350+
col.set_hsv(h, 1, 1);
1351+
col.a = 0;
1352+
colors2.set(0, col);
1353+
col.a = 1;
1354+
colors2.set(1, col);
1355+
col.set_hsv(h, 1, 0);
1356+
colors2.set(2, col);
1357+
col.a = 0;
1358+
colors2.set(3, col);
1359+
c->draw_polygon(points, colors2);
13821360
}
13831361

13841362
int x;
@@ -1393,25 +1371,23 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
13931371
x = center.x + hue_offset.x - (theme_cache.picker_cursor->get_width() / 2);
13941372
y = center.y + hue_offset.y - (theme_cache.picker_cursor->get_height() / 2);
13951373
} else {
1396-
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
1397-
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
1374+
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * WHEEL_RADIUS : 0;
1375+
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * WHEEL_RADIUS : 0;
13981376

13991377
Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
14001378
x = CLAMP(real_size.x * s, 0, real_size.x) + corner_x - (theme_cache.picker_cursor->get_width() / 2);
14011379
y = CLAMP(real_size.y - real_size.y * v, 0, real_size.y) + corner_y - (theme_cache.picker_cursor->get_height() / 2);
14021380
}
1381+
Color _col = color;
1382+
_col.a = 1.0;
1383+
c->draw_texture(theme_cache.picker_cursor_bg, Point2(x, y), _col);
14031384
c->draw_texture(theme_cache.picker_cursor, Point2(x, y));
14041385

1405-
col.set_hsv(h, 1, 1);
14061386
if (actual_shape == SHAPE_HSV_WHEEL) {
1407-
points.resize(4);
1408-
double h1 = h - (0.5 / 360);
1409-
double h2 = h + (0.5 / 360);
1410-
points.set(0, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU)), center.y + (center.y * Math::sin(h1 * Math_TAU))));
1411-
points.set(1, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h1 * Math_TAU) * 0.84)));
1412-
points.set(2, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU)), center.y + (center.y * Math::sin(h2 * Math_TAU))));
1413-
points.set(3, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h2 * Math_TAU) * 0.84)));
1414-
c->draw_multiline(points, col.inverted());
1387+
float _radius = WHEEL_RADIUS * 2.0;
1388+
_radius += (1.0 - _radius) * 0.5;
1389+
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);
1390+
c->draw_texture(theme_cache.picker_cursor, pos);
14151391
}
14161392

14171393
} else if (p_which == 1) {
@@ -2187,6 +2163,7 @@ void ColorPicker::_bind_methods() {
21872163
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_revert);
21882164
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, overbright_indicator);
21892165
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor);
2166+
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor_bg);
21902167
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_hue);
21912168

21922169
BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_normal, "tab_unselected", "TabContainer");
@@ -2337,6 +2314,7 @@ ColorPicker::ColorPicker() {
23372314

23382315
wheel_mat.instantiate();
23392316
wheel_mat->set_shader(wheel_shader);
2317+
wheel_mat->set_shader_parameter("wheel_radius", WHEEL_RADIUS);
23402318
circle_mat.instantiate();
23412319
circle_mat->set_shader(circle_shader);
23422320

scene/gui/color_picker.h

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class ColorPicker : public VBoxContainer {
127127

128128
int current_slider_count = SLIDER_COUNT;
129129
static const int MODE_BUTTON_COUNT = 3;
130+
const float WHEEL_RADIUS = 0.42;
130131

131132
bool slider_theme_modified = true;
132133

@@ -263,6 +264,7 @@ class ColorPicker : public VBoxContainer {
263264
Ref<Texture2D> sample_revert;
264265
Ref<Texture2D> overbright_indicator;
265266
Ref<Texture2D> picker_cursor;
267+
Ref<Texture2D> picker_cursor_bg;
266268
Ref<Texture2D> color_hue;
267269

268270
/* Mode buttons */

scene/theme/default_theme.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
10531053
theme->set_icon("overbright_indicator", "ColorPicker", icons["color_picker_overbright"]);
10541054
theme->set_icon("bar_arrow", "ColorPicker", icons["color_picker_bar_arrow"]);
10551055
theme->set_icon("picker_cursor", "ColorPicker", icons["color_picker_cursor"]);
1056+
theme->set_icon("picker_cursor_bg", "ColorPicker", icons["color_picker_cursor_bg"]);
10561057

10571058
{
10581059
const int precision = 7;
+1-1
Loading
Loading

0 commit comments

Comments
 (0)