Skip to content

Commit

Permalink
[add] improved multi selection of palette indices
Browse files Browse the repository at this point in the history
  • Loading branch information
begla committed Nov 20, 2023
1 parent ca05157 commit 01171f3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
- { key: readability-identifier-naming.ClassCase, value: lower_case }
- { key: readability-identifier-naming.ClassSuffix, value: _t }
- { key: readability-identifier-naming.ClassMethodCase, value: lower_case }
- { key: readability-identifier-naming.StructCase, value: lower_case }
- { key: readability-identifier-naming.StructSuffix, value: _t }
- { key: readability-identifier-naming.MemberCase, value: lower_case }
- { key: readability-identifier-naming.MethodCase, value: lower_case }
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.GlobalVariableCase, value: lower_case }
Expand Down
1 change: 1 addition & 0 deletions .vscode/ltex.dictionary.en-US.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ OIDN
Denoiser
voxelized
matadata
Wrensch
54 changes: 50 additions & 4 deletions iolite_plugins/voxel_editing_plugin/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,59 @@ inline static auto rand_float(float min, float max, uint64_t& seed = rng_seed)
//----------------------------------------------------------------------------//
struct palette_range_t
{
int32_t index_start{0}, index_end{0};
inline palette_range_t() { palette_indices.emplace_back(0u); }
inline palette_range_t(io_uint8_t palette_index)
{
palette_indices.emplace_back(palette_index);
}
inline palette_range_t(io_uint8_t palette_index_start,
io_uint8_t palette_index_end)
{
for (io_uint8_t idx = palette_index_start; idx <= palette_index_end; ++idx)
palette_indices.emplace_back(idx);
}

inline auto get_palette_index() const -> int32_t
inline auto add_or_remove_palette_index(io_uint8_t palette_index)
{
const int32_t range = index_end - index_start + 1;
return index_start + common::rand() % range;
// Remove if the index exists
for (auto it = palette_indices.begin(); it != palette_indices.end(); ++it)
{
if (*it == palette_index)
{
// Always keep at least one index
if (palette_indices.size() > 1u)
palette_indices.erase(it);

return;
}
}

// Insert otherwise
palette_indices.emplace_back(palette_index);
}

inline auto has_palette_index(uint8_t palette_index) const -> bool
{
for (auto idx : palette_indices)
{
if (idx == palette_index)
return true;
}

return false;
}

inline auto get_first_palette_index() const -> io_uint8_t
{
return palette_indices.front();
}

inline auto get_random_palette_index() const -> io_uint8_t
{
return palette_indices[common::rand() % palette_indices.size()];
}

std::vector<io_uint8_t> palette_indices;
};

//----------------------------------------------------------------------------//
Expand Down
8 changes: 4 additions & 4 deletions iolite_plugins/voxel_editing_plugin/editing.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void global_full(io_ref_t shape, const palette_range_t& range)
{
for (uint32_t x = 0u; x < dim.x; ++x)
{
const auto value = (uint8_t)range.get_palette_index() + 1u;
const auto value = range.get_random_palette_index() + 1u;
if (*write_ptr != value)
{
change.set(x, y, z, *write_ptr, dim);
Expand Down Expand Up @@ -83,7 +83,7 @@ static void global_fill(io_ref_t shape, const palette_range_t& range)
{
for (uint32_t x = 0u; x < dim.x; ++x)
{
const auto value = (uint8_t)range.get_palette_index() + 1u;
const auto value = range.get_random_palette_index() + 1u;
if (*write_ptr != 0u && *write_ptr != value)
{
change.set(x, y, z, *write_ptr, dim);
Expand Down Expand Up @@ -112,7 +112,7 @@ static void global_invert(io_ref_t shape, const palette_range_t& range)
{
for (uint32_t x = 0u; x < dim.x; ++x)
{
const auto value = (uint8_t)range.get_palette_index() + 1u;
const auto value = range.get_random_palette_index() + 1u;
if (*write_ptr != 0u)
{
change.set(x, y, z, *write_ptr, dim);
Expand All @@ -131,7 +131,7 @@ static void global_invert(io_ref_t shape, const palette_range_t& range)
}

//----------------------------------------------------------------------------//
static void global_erase(io_ref_t shape) { global_full(shape, {-1, -1}); };
static void global_erase(io_ref_t shape) { global_full(shape, {255u}); };

//----------------------------------------------------------------------------//
template <bool_op_t op>
Expand Down
11 changes: 6 additions & 5 deletions iolite_plugins/voxel_editing_plugin/editing_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static void handle_tool_voxel(io_ref_t shape, const tool_parameters_t& params)

const uint8_t value =
params.placement_mode != placement_mode_erase
? (params.palette_range.get_palette_index() + 1u)
? (params.palette_range.get_random_palette_index() + 1u)
: 0u;
voxels.set(offset.x + x, offset.y + y, offset.z + z, value, dim);
}
Expand Down Expand Up @@ -327,7 +327,7 @@ static void handle_tool_voxel(io_ref_t shape, const tool_parameters_t& params)
continue;

uint8_t palette_index =
params.palette_range.get_palette_index() + 1u;
params.palette_range.get_random_palette_index() + 1u;
if (params.placement_mode == placement_mode_erase)
palette_index = 0u;

Expand Down Expand Up @@ -657,7 +657,7 @@ static void handle_tool_grass(io_ref_t shape, tool_parameters_t& params)
const float h = common::rand_float(0.0f, 1.0f);

if (params.face_palette_fill)
palette_index = params.palette_range.get_palette_index() + 1;
palette_index = params.palette_range.get_random_palette_index() + 1;

if (r > params.tool_grass_density)
continue;
Expand Down Expand Up @@ -826,7 +826,8 @@ static void handle_tool_eyedropper(io_ref_t shape, tool_parameters_t& params)
uint8_t x, y, z, palette_index;
sparse_volume_t::unpack(voxels.entries.front().data, x, y, z,
&palette_index);
params.palette_range = {palette_index - 1, palette_index - 1};
params.palette_range = {io_uint8_t(palette_index - 1u),
io_uint8_t(palette_index - 1u)};
}
}

Expand Down Expand Up @@ -916,7 +917,7 @@ static void handle_tool_box(io_ref_t shape, tool_parameters_t& params)
{
const uint8_t palette_index =
(!should_erase && !is_selection)
? params.palette_range.get_palette_index() + 1u
? params.palette_range.get_random_palette_index() + 1u
: 0u;
voxels.set(x, y, z, palette_index, dim);
}
Expand Down
16 changes: 6 additions & 10 deletions iolite_plugins/voxel_editing_plugin/editing_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void show_palette_index_picker(io_ref_t palette, palette_range_t& range,
}

const auto current_color =
io_resource_palette->get_color(palette, range.index_start);
io_resource_palette->get_color(palette, range.get_first_palette_index());

bool should_scroll = false;
if (ImGui::ColorButton("###color", to_imgui(current_color), 0, button_size))
Expand All @@ -67,7 +67,7 @@ void show_palette_index_picker(io_ref_t palette, palette_range_t& range,
if (i % 4u != 0u)
ImGui::SameLine();

const bool is_current = i >= range.index_start && i <= range.index_end;
const bool is_current = range.has_palette_index(i);
if (is_current)
{
// Highlight picked color
Expand All @@ -86,13 +86,9 @@ void show_palette_index_picker(io_ref_t palette, palette_range_t& range,
if (ImGui::ColorButton("###button", to_imgui(color)))
{
if (!ImGui::GetIO().KeyShift)
range.index_start = range.index_end = i;
range = {(uint8_t)i};
else
range.index_end = i;

// Ensure start <= end
if (range.index_start > range.index_end)
std::swap(range.index_start, range.index_end);
range.add_or_remove_palette_index(i);
}

if (is_current)
Expand Down Expand Up @@ -205,8 +201,8 @@ void show_editing_toolbar()
show_palette_index_picker(palette, current_tool_params.palette_range,
tb_button_size);
show_tooltip(
"The color or color range for the editing operations. Press "
"[SHIFT] while picking a color to select a range of colors.");
"The color or color selection for the editing operations. Press "
"[SHIFT] to add or remove colors to/from the selection.");
ImGui::EndDisabled();
}

Expand Down
2 changes: 1 addition & 1 deletion iolite_plugins/voxel_editing_plugin/sparse_volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ struct sparse_volume_t

const uint32_t index =
coord.x + coord.y * dim.x + coord.z * dim.x * dim.y;
const auto palette_index = range.get_palette_index() + 1u;
const auto palette_index = range.get_random_palette_index() + 1u;

if (data[index] != palette_index || force_identical_voxels)
change.set(coord, palette_index, dim);
Expand Down

0 comments on commit 01171f3

Please sign in to comment.