hero-mark-2/scripts/palette_texture.gd

50 lines
1 KiB
GDScript

tool
class_name PaletteTexture
extends ImageTexture
export (Array, Color) var colors := [] setget _set_colors
export var convert_from: Texture = null setget _convert_from
var _data := PoolByteArray()
func _init() -> void:
colors.resize(16)
colors.fill(Color.white)
_data.resize(16 * 4)
_data.fill(255)
_update_image()
func _set_colors(new_array: Array) -> void:
if new_array.size() == 16:
colors = new_array
_update_image()
func _update_image() -> void:
for i in colors.size():
var c: Color = colors[i]
var j: int = i * 4
_data[j] = c.r8
_data[j + 1] = c.g8
_data[j + 2] = c.b8
_data[j + 3] = c.a8
var image := Image.new()
image.create_from_data(4, 4, false, Image.FORMAT_RGBA8, _data)
create_from_image(image, 0)
func _convert_from(texture: Texture) -> void:
if texture != null and texture.get_size() == Vector2(4.0, 4.0):
var image := texture.get_data()
image.lock()
for x in 4:
for y in 4:
colors[x + (y * 4)] = image.get_pixel(x, y)
print(colors[x + (y * 4)])
image.unlock()
_update_image()