Skip to content

Commit ce2a306

Browse files
authored
Rework palette previews (#1656)
1 parent 1cef420 commit ce2a306

30 files changed

+751
-576
lines changed

src/autoload/HandlerGUI.gd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ func add_popup(new_popup: Control, add_shadow := true) -> Control:
196196
if add_shadow:
197197
var shadow_container := PanelContainer.new()
198198
var sb := StyleBoxFlat.new()
199-
sb.bg_color = Color(0, 0, 0, 0.1)
200-
sb.shadow_color = Color(0, 0, 0, 0.1)
199+
sb.bg_color = ThemeUtils.shadow_color
200+
sb.shadow_color = ThemeUtils.shadow_color
201201
sb.shadow_size = 8
202202
if new_popup is PanelContainer:
203203
var stylebox_wrapped := new_popup.get_theme_stylebox("panel")
@@ -269,17 +269,21 @@ func popup_under_rect(popup: Control, rect: Rect2, vp: Viewport) -> void:
269269
# Should usually be the global rect of a control.
270270
func popup_under_rect_center(popup: Control, rect: Rect2, vp: Viewport) -> void:
271271
var top_popup := add_popup(popup)
272+
top_popup.resized.connect(_move_popup_under_rect_center.bind(top_popup, rect, vp))
273+
_move_popup_under_rect_center(top_popup, rect, vp)
274+
275+
func _move_popup_under_rect_center(popup: Control, rect: Rect2, vp: Viewport) -> void:
272276
var screen_transform := vp.get_screen_transform()
273277
var screen_h := vp.get_visible_rect().size.y
274-
var popup_pos := Vector2(rect.position.x - top_popup.size.x / 2.0 + rect.size.x / 2, 0)
278+
var popup_pos := Vector2(rect.position.x - popup.size.x / 2.0 + rect.size.x / 2, 0)
275279
# Popup below if there's enough space or we're in the bottom half of the screen.
276-
if rect.position.y + rect.size.y + top_popup.size.y < screen_h or rect.position.y + rect.size.y / 2 <= screen_h / 2.0:
280+
if rect.position.y + rect.size.y + popup.size.y < screen_h or rect.position.y + rect.size.y / 2 <= screen_h / 2.0:
277281
popup_pos.y = rect.position.y + rect.size.y
278282
else:
279-
popup_pos.y = rect.position.y - top_popup.size.y
283+
popup_pos.y = rect.position.y - popup.size.y
280284
# Align horizontally and other things.
281285
popup_pos += screen_transform.get_origin() / screen_transform.get_scale()
282-
top_popup.position = popup_clamp_pos(top_popup, popup_pos, vp)
286+
popup.position = popup_clamp_pos(popup, popup_pos, vp)
283287

284288
# Should usually be the global position of the mouse.
285289
func popup_under_pos(popup: Control, pos: Vector2, vp: Viewport) -> void:

src/config_classes/SaveData.gd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func get_setting_default(setting: String) -> Variant:
7676
ThemePreset.LIGHT: return Color("b22")
7777
"basic_color_warning":
7878
match theme_preset:
79-
ThemePreset.DARK, ThemePreset.BLACK, ThemePreset.GRAY: return Color("ee6")
80-
ThemePreset.LIGHT: return Color("991")
79+
ThemePreset.DARK, ThemePreset.BLACK, ThemePreset.GRAY: return Color("fd5")
80+
ThemePreset.LIGHT: return Color("b92")
8181
"handle_size": return 1.0 if OS.get_name() != "Android" else 2.0
8282
"handle_inner_color": return Color("fff")
8383
"handle_color": return Color("111")
@@ -378,7 +378,7 @@ const CURRENT_VERSION = 1
378378
emit_changed()
379379
Configs.basic_colors_changed.emit()
380380

381-
@export var basic_color_warning := Color("ee6"):
381+
@export var basic_color_warning := Color("fd5"):
382382
set(new_value):
383383
if basic_color_warning != new_value:
384384
basic_color_warning = new_value

src/data_classes/ColorParser.gd

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,22 @@ static func are_colors_same(col1: String, col2: String) -> bool:
152152
return _get_url_id(col1) == _get_url_id(col2)
153153

154154
# Represent both colors as 6-digit hex codes to serve as basis for comparison.
155-
for i in 2:
156-
var col := col1 if i == 0 else col2
157-
# Start of conversion logic.
158-
if is_valid_rgb(col):
159-
col = text_to_color(col).to_html(false)
160-
elif is_valid_hex(col) and col.length() == 4:
161-
col = col[1] + col[1] + col[2] + col[2] + col[3] + col[3]
162-
elif is_valid_named(col, false):
163-
col = AttributeColor.get_named_colors()[col]
164-
col = col.trim_prefix("#")
165-
# End of conversion logic.
166-
if i == 0:
167-
col1 = col
168-
elif i == 1:
169-
col2 = col
155+
if is_valid_rgb(col1):
156+
col1 = text_to_color(col1).to_html(false)
157+
elif is_valid_hex(col1) and col1.length() == 4:
158+
col1 = col1[1] + col1[1] + col1[2] + col1[2] + col1[3] + col1[3]
159+
elif is_valid_named(col1, false):
160+
col1 = AttributeColor.get_named_colors()[col1]
161+
col1 = col1.trim_prefix("#")
162+
163+
if is_valid_rgb(col2):
164+
col2 = text_to_color(col2).to_html(false)
165+
elif is_valid_hex(col2) and col2.length() == 4:
166+
col2 = col2[1] + col2[1] + col2[2] + col2[2] + col2[3] + col2[3]
167+
elif is_valid_named(col2, false):
168+
col2 = AttributeColor.get_named_colors()[col2]
169+
col2 = col2.trim_prefix("#")
170+
170171
return col1 == col2
171172

172173

src/shaders/color_area.gdshader

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ shader_type canvas_item;
66
// 1 = HS+L Circle
77
// 2 = SV+H Square
88
// 3 = SL+H Square
9+
// 4 = HL+S Square
10+
// 100 = Normal Circle
911
uniform int interpolation = 0;
1012
uniform float third_value = 1.0;
1113

@@ -24,5 +26,15 @@ void fragment() {
2426
COLOR = vec4(hsv_to_rgb(vec3(third_value, clamp(UV.x, 0.0, 1.0), clamp(1.0 - UV.y, 0.0, 1.0))), 1.0);
2527
} else if (interpolation == 3) {
2628
COLOR = vec4(hsl_to_rgb(vec3(third_value, clamp(UV.x, 0.0, 1.0), clamp(1.0 - UV.y, 0.0, 1.0))), 1.0);
29+
} else if (interpolation == 4) {
30+
COLOR = vec4(hsl_to_rgb(vec3(clamp(UV.x, 0.0, 1.0), 1.0, clamp(1.0 - UV.y, 0.0, 1.0))), 1.0);
31+
} else if (interpolation == 100) {
32+
vec2 p = UV - vec2(0.5);
33+
vec2 xy = p * 2.0;
34+
xy.y = -xy.y;
35+
float dist = length(p);
36+
float edge = fwidth(dist);
37+
vec3 final_normal = normalize(mix(vec3(0.0, 0.0, 1.0), vec3(xy, sqrt(1.0 - dot(xy, xy))), third_value));
38+
COLOR = vec4(final_normal * 0.5 + 0.5, 1.0 - smoothstep(0.5 - edge * 0.5, 0.5 + edge * 0.5, dist));
2739
}
2840
}

src/shaders/slider_visuals.gdshader

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ uniform bool inverted = false;
1010
// RGB --> 1 = Red, 2 = Green, 3 = Blue
1111
// HSV --> 4 = Hue, 5 = HSV saturation, 6 = Value
1212
// HSL --> 4 = Hue, 7 = HSL saturation, 8 = Lightness
13+
// 100 = Normal intensity
1314
uniform int interpolation = 0;
1415

1516
void fragment() {
@@ -42,6 +43,16 @@ void fragment() {
4243
hsl.z = offset;
4344
}
4445
COLOR = vec4(hsl_to_rgb(hsl), 1.0);
46+
} else if (interpolation == 100) {
47+
vec2 dir = normalize(base_color.rgb * 2.0 - 1.0).xy;
48+
float len = length(dir);
49+
if (len > 0.00001) {
50+
dir /= len;
51+
} else {
52+
dir = vec2(0.0);
53+
}
54+
vec2 xy = dir * offset;
55+
COLOR = vec4(vec3(xy, sqrt(max(0.0, 1.0 - dot(xy, xy)))) * 0.5 + 0.5, 1.0);
4556
} else {
4657
COLOR = base_color;
4758
}

src/ui_parts/debug_content.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var advanced_mode := false
88

99
func _ready() -> void:
1010
var shortcuts := ShortcutsRegistration.new()
11-
shortcuts.add_shortcut("debug", toggle_debug)
12-
shortcuts.add_shortcut("advanced_debug", toggle_debug.bind(true))
11+
shortcuts.add_shortcut("debug", toggle_debug, ShortcutsRegistration.Behavior.PASS_THROUGH_POPUPS)
12+
shortcuts.add_shortcut("advanced_debug", toggle_debug.bind(true), ShortcutsRegistration.Behavior.PASS_THROUGH_POPUPS)
1313
HandlerGUI.register_shortcuts(self, shortcuts)
1414

1515
set_debug_visibility(false)

src/ui_parts/previews.gd

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
extends VTitledPanel
22

3-
const ColorSwatch = preload("res://src/ui_widgets/color_swatch.gd")
43
const ColorEdit = preload("res://src/ui_widgets/color_edit.gd")
54
const NumberEdit = preload("res://src/ui_widgets/number_edit.gd")
65

@@ -21,10 +20,6 @@ const MAX_ICON_PREVIEW_SIZE = 128
2120
@onready var scaled_preview_panel: PanelContainer = %ScaledPreviewPanel
2221
@onready var size_label: Label = %SizeLabel
2322
@onready var split_container: SplitContainer = %SplitContainer
24-
@onready var transparent_color_swatch: ColorSwatch = %TransparentColorSwatch
25-
@onready var black_color_swatch: ColorSwatch = %BlackColorSwatch
26-
@onready var white_color_swatch: ColorSwatch = %WhiteColorSwatch
27-
@onready var color_edit: ColorEdit = %ColorEdit
2823
@onready var preview_top_panel: PanelContainer = $SplitContainer/PreviewTopPanel
2924
@onready var more_button: Button = $ActionContainer/MoreButton
3025
@onready var size_label_margins: MarginContainer = %SizeLabelMargins
@@ -80,16 +75,6 @@ func _ready() -> void:
8075
icon_preview_tiles.mouse_exited.connect(_on_tiles_mouse_exited)
8176
more_button.pressed.connect(_on_more_button_pressed)
8277

83-
transparent_color_swatch.color = "none"
84-
transparent_color_swatch.pressed.connect(func() -> void: color_edit.value = "#fff0")
85-
black_color_swatch.color = "#000"
86-
black_color_swatch.pressed.connect(func() -> void: color_edit.value = "#000")
87-
white_color_swatch.color = "#fff"
88-
white_color_swatch.pressed.connect(func() -> void: color_edit.value = "#fff")
89-
90-
color_edit.value_changed.connect(_update_preview_background)
91-
color_edit.value = Configs.savedata.previews_background.to_html()
92-
9378
Configs.theme_changed.connect(sync_theming)
9479
sync_theming()
9580

@@ -110,8 +95,7 @@ func _ready() -> void:
11095
)
11196
icon_preview_tiles.resized.connect(sync_tile_positions)
11297
sync_tiles()
113-
HandlerGUI.register_focus_sequence(self, [add_new_preview_button,
114-
transparent_color_swatch, black_color_swatch, white_color_swatch, color_edit, more_button])
98+
HandlerGUI.register_focus_sequence(self, [add_new_preview_button, more_button])
11599

116100

117101
func sync_theming() -> void:
@@ -284,7 +268,7 @@ func _edit_tile_size(tile: IconPreviewTileData) -> void:
284268
edit_field.position = icon_preview_tiles.position + tile.position + tile.label_rect.position - Vector2(3, 4)
285269
edit_field.size = tile.label_rect.size
286270
edit_field.add_theme_font_override("font", ThemeUtils.main_font)
287-
edit_field.focus_exited.connect(edit_field.queue_free)
271+
edit_field.editing_toggled.connect(_on_edit_field_editing_toggled)
288272
edit_field.value_changed.connect(_on_edit_field_value_changed)
289273
edit_field.grab_focus()
290274
edit_field.select_all()
@@ -297,6 +281,10 @@ func _on_edit_field_value_changed(new_value: float) -> void:
297281
if edited_tile_index == selected_tile_index:
298282
_select_tile(edited_tile_index)
299283

284+
func _on_edit_field_editing_toggled(toggled_on: bool) -> void:
285+
if not toggled_on:
286+
edit_field.queue_free()
287+
300288
func _delete_tile(tile: IconPreviewTileData) -> void:
301289
var sizes := Configs.savedata.preview_sizes.duplicate()
302290
if tile.index >= 0 and tile.index <= Configs.savedata.preview_sizes.size() - 1:
@@ -334,15 +322,16 @@ func _add_new_tile() -> void:
334322
Configs.savedata.preview_sizes = old_icon_sizes
335323
sync_tiles()
336324

337-
func _update_preview_background(new_value: String) -> void:
338-
Configs.savedata.previews_background = ColorParser.text_to_color(new_value, Color.BLACK, true)
339-
sync_tiles()
340-
if Configs.savedata.previews_background == Color.TRANSPARENT:
341-
scaled_preview_panel.remove_theme_stylebox_override("panel")
342-
else:
343-
var colored_sb := StyleBoxFlat.new()
344-
colored_sb.bg_color = Configs.savedata.previews_background
345-
scaled_preview_panel.add_theme_stylebox_override("panel", colored_sb)
325+
# TODO reinstate
326+
#func _update_preview_background(new_value: String) -> void:
327+
#Configs.savedata.previews_background = ColorParser.text_to_color(new_value, Color.BLACK, true)
328+
#sync_tiles()
329+
#if Configs.savedata.previews_background == Color.TRANSPARENT:
330+
#scaled_preview_panel.remove_theme_stylebox_override("panel")
331+
#else:
332+
#var colored_sb := StyleBoxFlat.new()
333+
#colored_sb.bg_color = Configs.savedata.previews_background
334+
#scaled_preview_panel.add_theme_stylebox_override("panel", colored_sb)
346335

347336

348337
func _on_more_button_pressed() -> void:

src/ui_parts/previews.tscn

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
[ext_resource type="Script" uid="uid://dpn15qyr7xsds" path="res://src/ui_parts/previews.gd" id="1_uogc6"]
44
[ext_resource type="Texture2D" uid="uid://eif2ioi0mw17" path="res://assets/icons/Plus.svg" id="2_pm7dr"]
5-
[ext_resource type="PackedScene" uid="uid://bujllg1bqlub6" path="res://src/ui_widgets/color_swatch.tscn" id="3_auodr"]
6-
[ext_resource type="PackedScene" uid="uid://5f8uxavn1or1" path="res://src/ui_widgets/color_edit.tscn" id="4_mkhk3"]
75
[ext_resource type="Texture2D" uid="uid://ccbta5q43jobk" path="res://assets/icons/More.svg" id="5_ue2r5"]
86
[ext_resource type="Script" uid="uid://bniafq6ma1iln" path="res://src/ui_widgets/ProceduralControl.gd" id="6_6v74c"]
97

@@ -22,6 +20,7 @@ metadata/_custom_type_script = "uid://ojo0537b6hcy"
2220

2321
[node name="ActionContainer" type="HBoxContainer" parent="." unique_id=1812445449]
2422
layout_mode = 2
23+
size_flags_horizontal = 3
2524
theme_override_constants/separation = 8
2625

2726
[node name="AddNewPreviewButton" type="Button" parent="ActionContainer" unique_id=1393167346]
@@ -33,33 +32,9 @@ mouse_default_cursor_shape = 2
3332
theme_type_variation = &"IconButton"
3433
icon = ExtResource("2_pm7dr")
3534

36-
[node name="ColorsContainer" type="HBoxContainer" parent="ActionContainer" unique_id=1645315072]
37-
layout_mode = 2
38-
size_flags_horizontal = 6
39-
40-
[node name="TransparentColorSwatch" parent="ActionContainer/ColorsContainer" unique_id=940556769 instance=ExtResource("3_auodr")]
41-
unique_name_in_owner = true
42-
layout_mode = 2
43-
size_flags_vertical = 4
44-
45-
[node name="BlackColorSwatch" parent="ActionContainer/ColorsContainer" unique_id=181031709 instance=ExtResource("3_auodr")]
46-
unique_name_in_owner = true
47-
layout_mode = 2
48-
size_flags_vertical = 4
49-
50-
[node name="WhiteColorSwatch" parent="ActionContainer/ColorsContainer" unique_id=780192261 instance=ExtResource("3_auodr")]
51-
unique_name_in_owner = true
52-
layout_mode = 2
53-
size_flags_vertical = 4
54-
55-
[node name="ColorEdit" parent="ActionContainer/ColorsContainer" unique_id=382124865 instance=ExtResource("4_mkhk3")]
56-
unique_name_in_owner = true
57-
layout_mode = 2
58-
size_flags_vertical = 4
59-
enable_alpha = true
60-
6135
[node name="MoreButton" type="Button" parent="ActionContainer" unique_id=1528240669]
6236
layout_mode = 2
37+
size_flags_horizontal = 10
6338
mouse_default_cursor_shape = 2
6439
theme_type_variation = &"IconButton"
6540
icon = ExtResource("5_ue2r5")

src/ui_parts/tab_bar.gd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ func _gui_input(event: InputEvent) -> void:
194194
if hovered_idx == Configs.savedata.get_active_tab_index():
195195
scroll_to_active()
196196
else:
197-
# Give time for deferred callbacks that might change the active SVG.
198-
# For example, the code editor might get unfocused by clicking on a tab,
199-
# changing the SVG, so this should be deferred.
197+
# Give time for deferred callbacks that might change the active SVG. For example, the code editor
198+
# might get unfocused by clicking on a tab, changing the SVG, so this should be deferred.
200199
Configs.savedata.set_active_tab_index.call_deferred(hovered_idx)
201200
if event.button_index == MOUSE_BUTTON_LEFT:
202201
var scroll_backwards_area_rect := get_scroll_backwards_area_rect()
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
extends PanelContainer
22

3-
signal color_deletion_requested
4-
53
@onready var color_label: Label = %LabelContainer/ColorLabel
64
@onready var color_name_edit: BetterLineEdit = %ConfigureContainer/TopContainer/ColorNameEdit
75
@onready var color_edit: LineEditButton = %ConfigureContainer/BottomContainer/ColorEdit
@@ -10,17 +8,19 @@ signal color_deletion_requested
108
@onready var label_container: HBoxContainer = %LabelContainer
119

1210
var palette: Palette
13-
var idx: int
11+
var index: int
1412

1513
func _ready() -> void:
1614
Configs.language_changed.connect(sync_localization)
17-
set_label_text(palette.get_color_name(idx))
18-
color_edit.value = palette.get_color(idx)
15+
color_edit.value = palette.get_color(index)
1916
sync_localization()
2017
color_name_edit.text_submitted.connect(_on_name_edit_text_submitted)
21-
color_name_edit.text_change_canceled.connect(hide_name_edit)
18+
color_name_edit.editing_toggled.connect(_on_editing_toggled)
19+
color_edit.value_changed.connect(change_color)
2220
edit_button.pressed.connect(_on_edit_button_pressed)
23-
delete_button.pressed.connect(_on_delete_button_pressed)
21+
delete_button.pressed.connect(queue_free)
22+
palette.changed_deferred.connect(sync)
23+
sync()
2424
HandlerGUI.register_focus_sequence(self, [edit_button, color_name_edit, color_edit, delete_button], true)
2525

2626

@@ -29,32 +29,30 @@ func sync_localization() -> void:
2929
$ConfigureContainer/BottomContainer/DeleteButton.tooltip_text = Translator.translate("Delete color")
3030

3131
func _on_edit_button_pressed() -> void:
32-
color_name_edit.text = palette.get_color_name(idx)
32+
color_name_edit.text = palette.get_color_name(index)
3333
color_name_edit.show()
3434
color_name_edit.grab_focus()
3535
color_name_edit.caret_column = color_name_edit.text.length()
3636
label_container.hide()
3737

38-
3938
func _on_name_edit_text_submitted(new_text: String) -> void:
4039
var new_name := new_text.strip_edges()
41-
set_label_text(new_name)
42-
hide_name_edit()
40+
palette.modify_color_name(index, new_name)
4341

42+
func change_color(new_color: String) -> void:
43+
palette.modify_color(index, new_color)
4444

45-
func hide_name_edit() -> void:
46-
color_name_edit.hide()
47-
label_container.show()
45+
func _on_editing_toggled(toggled_on: bool) -> void:
46+
if not toggled_on:
47+
color_name_edit.hide()
48+
label_container.show()
4849

49-
func set_label_text(new_text: String) -> void:
50-
if new_text.is_empty():
50+
func sync() -> void:
51+
var color_name := palette.get_color_name(index)
52+
if color_name.is_empty():
5153
color_label.text = Translator.translate("Unnamed")
5254
color_label.add_theme_color_override("font_color", ThemeUtils.subtle_text_color)
5355
else:
54-
color_label.text = new_text
56+
color_label.text = color_name
5557
color_label.remove_theme_color_override("font_color")
56-
57-
58-
func _on_delete_button_pressed() -> void:
59-
color_deletion_requested.emit()
60-
queue_free()
58+
reset_size()

0 commit comments

Comments
 (0)