-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwatchd.gd
96 lines (76 loc) · 2.47 KB
/
Swatchd.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
tool
extends Resource
class_name Swatchd
export(Array, Color) var colors = [] setget set_colors
export(Dictionary) var named_colors:Dictionary setget set_named_colors
export(Resource) var override_swatch:Resource setget set_override_swatch
export(Texture) var texture
func set_override_swatch(o:Resource):
if o == override_swatch:
return
if override_swatch != null:
override_swatch.disconnect("changed", self, "on_override_swatch_changed")
if o != null:
# Check if the nested resource has the same script
if o.get_script() == get_script():
override_swatch = o
override_swatch.connect("changed", self, "on_override_swatch_changed")
else:
override_swatch = null
on_override_swatch_changed()
func on_override_swatch_changed():
clear_texture_cache()
emit_signal("changed")
func set_colors(colors_) -> void:
colors = colors_
clear_texture_cache()
emit_signal("changed")
func set_named_colors(c) -> void:
named_colors = c
clear_texture_cache()
emit_signal("changed")
func has_named_color(color_name:String) -> bool:
return named_colors.has(color_name)
func get_named_color(color_name:String) -> Color:
if override_swatch != null and override_swatch.has_named_color(color_name):
return override_swatch.get_named_color(color_name)
return named_colors.get(color_name, Color.white)
func clear_texture_cache():
texture = null
func cached_texture():
if !texture:
texture = generate_texture()
return texture
func get_texture_index_for_name(name:String) -> int:
var sorted_keys:Array = named_colors.keys()
sorted_keys.sort()
var index_in_dictionary = sorted_keys.find(name)
if (index_in_dictionary < 0):
printerr("[Swatchd] get_texture_index_for_name couldn't find key: " + name)
return -1
return colors.size() + index_in_dictionary
func generate_texture() -> Texture:
# print ("[Swatchd] generate_texture")
# Create texture for swatch
var num_colors = colors.size() + named_colors.size()
var image:Image = Image.new()
image.create(num_colors, 1, false, Image.FORMAT_RGBA8)
image.lock()
var iterator := 0
# Add colors from color array
for c in colors:
image.set_pixel(iterator, 0, c)
iterator += 1
# Add colors from named_colors dictionary
var sorted_keys = named_colors.keys()
sorted_keys.sort()
for k in sorted_keys:
var named_color = get_named_color(k)
assert(named_color is Color)
image.set_pixel(iterator, 0, named_color)
iterator += 1
image.unlock()
var tex = ImageTexture.new()
tex.create_from_image(image)
tex.flags = 0
return tex