Allow to edit VoxelColorPalette in the editor

This commit is contained in:
Marc Gilleron 2021-05-15 20:28:07 +01:00
parent 5f7c0cc668
commit 93cfab7495
2 changed files with 46 additions and 1 deletions

View File

@ -1,5 +1,14 @@
#include "voxel_color_palette.h"
VoxelColorPalette::VoxelColorPalette() {
// Default palette
_colors[0] = Color8(0, 0, 0, 0);
_colors[1] = Color8(255, 255, 255, 255);
for (unsigned int i = 2; i < _colors.size(); ++i) {
_colors[i] = Color8(0, 0, 0, 255);
}
}
void VoxelColorPalette::set_color(int index, Color color) {
ERR_FAIL_INDEX(index, static_cast<int>(_colors.size()));
_colors[index] = Color8(color);
@ -10,6 +19,29 @@ Color VoxelColorPalette::get_color(int index) const {
return _colors[index];
}
PoolColorArray VoxelColorPalette::get_colors() const {
PoolColorArray colors;
colors.resize(_colors.size());
{
PoolColorArray::Write w = colors.write();
for (unsigned int i = 0; i < _colors.size(); ++i) {
w[i] = _colors[i];
}
}
return colors;
}
void VoxelColorPalette::set_colors(PoolColorArray colors) {
// Color count is fixed, but we can't easily prevent Godot from allowing users to set a dynamic array
ERR_FAIL_COND(colors.size() != _colors.size());
{
PoolColorArray::Read r = colors.read();
for (unsigned int i = 0; i < _colors.size(); ++i) {
_colors[i] = Color8(r[i]);
}
}
}
void VoxelColorPalette::clear() {
for (size_t i = 0; i < _colors.size(); ++i) {
_colors[i] = Color8();
@ -40,10 +72,18 @@ void VoxelColorPalette::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color", "color"), &VoxelColorPalette::set_color);
ClassDB::bind_method(D_METHOD("get_color"), &VoxelColorPalette::get_color);
ClassDB::bind_method(D_METHOD("set_colors", "colors"), &VoxelColorPalette::set_colors);
ClassDB::bind_method(D_METHOD("get_colors"), &VoxelColorPalette::get_colors);
ClassDB::bind_method(D_METHOD("set_data", "d"), &VoxelColorPalette::_b_set_data);
ClassDB::bind_method(D_METHOD("get_data"), &VoxelColorPalette::_b_get_data);
ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "data"), "set_data", "get_data");
// This is just to allow editing colors in the editor
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "colors", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR),
"set_colors", "get_colors");
ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE),
"set_data", "get_data");
BIND_CONSTANT(MAX_COLORS);
}

View File

@ -11,9 +11,14 @@ class VoxelColorPalette : public Resource {
public:
static const unsigned int MAX_COLORS = 256;
VoxelColorPalette();
void set_color(int index, Color color);
Color get_color(int index) const;
PoolColorArray get_colors() const;
void set_colors(PoolColorArray colors);
void clear();
// Internal