Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options: add BOOLEAN & ENUM; rs2_set_option_value #12708

Merged
merged 17 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 10 additions & 49 deletions common/device-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,21 +2254,11 @@ namespace rs2

///////////////////////////////////////////
//TODO: make this a member function
std::vector<const char*> labels;
int selected;
std::vector< const char * > labels = opt_model.get_combo_labels( &selected );
std::vector< float > counters;
auto selected = 0, counter = 0;
for (auto i = opt_model.range.min; i <= opt_model.range.max; i += opt_model.range.step)
{
std::string product = dev.get_info(RS2_CAMERA_INFO_PRODUCT_LINE);

if (std::fabs(i - opt_model.value) < 0.001f)
{
selected = counter;
}
labels.push_back(opt_model.endpoint->get_option_value_description(opt_model.opt, i));
counters.push_back(i);
counter++;
}
///////////////////////////////////////////

ImGui_ScopePushStyleColor(ImGuiCol_TextSelectedBg, white);
Expand Down Expand Up @@ -2995,27 +2985,10 @@ namespace rs2
label = rsutils::string::from() << pb->get_name() << "##" << id;
if (ImGui::TreeNode(label.c_str()))
{
if (!viewer.is_option_skipped(RS2_OPTION_MIN_DISTANCE))
{
pb->get_option(RS2_OPTION_MIN_DISTANCE).update_all_fields(error_message, *viewer.not_model);
}
if (!viewer.is_option_skipped(RS2_OPTION_MAX_DISTANCE))
{
pb->get_option(RS2_OPTION_MAX_DISTANCE).update_all_fields(error_message, *viewer.not_model);
}
if (!viewer.is_option_skipped(RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED))
{
pb->get_option(RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED).update_all_fields(error_message, *viewer.not_model);
}

for (auto i = 0; i < RS2_OPTION_COUNT; i++)
{
auto opt = static_cast<rs2_option>(i);
if (viewer.is_option_skipped(opt)) continue;
pb->get_option(opt).draw_option(
dev.is<playback>() || update_read_only_options,
false, error_message, *viewer.not_model);
}
pb->draw_options( viewer,
dev.is< playback >() || update_read_only_options,
false,
error_message );

ImGui::TreePop();
}
Expand Down Expand Up @@ -3215,22 +3188,10 @@ namespace rs2
label = rsutils::string::from() << pb->get_name() << "##" << id;
if (ImGui::TreeNode(label.c_str()))
{
for (auto&& opt : pb->get_option_list())
{
if (viewer.is_option_skipped(opt)) continue;
pb->get_option(opt).draw_option(
dev.is<playback>() || update_read_only_options,
false, error_message, *viewer.not_model);

if (opt == RS2_OPTION_MIN_DISTANCE)
{
pb->get_option(RS2_OPTION_MAX_DISTANCE).update_all_fields(error_message, *viewer.not_model);
}
else if (opt == RS2_OPTION_MAX_DISTANCE)
{
pb->get_option(RS2_OPTION_MIN_DISTANCE).update_all_fields(error_message, *viewer.not_model);
}
}
pb->draw_options( viewer,
dev.is< playback >() || update_read_only_options,
false,
error_message );

ImGui::TreePop();
}
Expand Down
113 changes: 56 additions & 57 deletions common/option-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace rs2
{
option_model create_option_model(rs2_option opt,
option_model create_option_model( option_value const & opt,
const std::string& opt_base_label,
subdevice_model* model,
std::shared_ptr<options> options,
Expand All @@ -19,32 +19,17 @@ namespace rs2
{
option_model option = {};

std::stringstream ss;

ss << opt_base_label << "/" << options->get_option_name(opt);
option.id = ss.str();
option.opt = opt;
std::string const option_name = options->get_option_name( opt->id );
option.id = rsutils::string::from() << opt_base_label << '/' << option_name;
option.opt = opt->id;
option.endpoint = options;
option.label = options->get_option_name(opt) + std::string("##") + ss.str();
option.label = rsutils::string::from() << option_name << "##" << option.id;
option.invalidate_flag = options_invalidated;
option.dev = model;
option.range = { 0, 1, 0, 0 };
option.value = 0;

option.supported = options->supports(opt);
option.range = options->get_option_range( opt );
option.read_only = options->is_option_read_only( opt );
if (option.supported)
{
try
{
option.value = options->get_option(opt);
}
catch (const error& e)
{
error_message = error_to_string(e);
}
}
option.value = opt;
option.supported = opt->is_valid; // i.e., supported-and-enabled!
option.range = options->get_option_range( opt->id );
option.read_only = options->is_option_read_only( opt->id );
return option;
}
}
Expand Down Expand Up @@ -176,9 +161,10 @@ void option_model::update_all_fields( std::string & error_message, notifications
{
try
{
if( ( supported = endpoint->supports( opt ) ) )
value = endpoint->get_option_value( opt );
supported = value->is_valid;
if( supported )
{
value = endpoint->get_option( opt );
range = endpoint->get_option_range( opt );
read_only = endpoint->is_option_read_only( opt );
}
Expand Down Expand Up @@ -219,6 +205,33 @@ bool option_model::is_enum() const
return true;
}

std::vector< const char * > option_model::get_combo_labels( int * p_selected ) const
{
int selected = 0, counter = 0;
std::vector< const char * > labels;
for( auto i = range.min; i <= range.max; i += range.step, counter++ )
{
auto label = endpoint->get_option_value_description( opt, i );

switch( value->type )
{
case RS2_OPTION_TYPE_FLOAT:
if( std::fabs( i - value->as_float ) < 0.001f )
selected = counter;
break;
case RS2_OPTION_TYPE_STRING:
if( 0 == strcmp( label, value->as_string ) )
selected = counter;
break;
}

labels.push_back( label );
}
if( p_selected )
*p_selected = selected;
return labels;
}

bool option_model::draw_combobox( notifications_model & model,
std::string & error_message,
const char * description,
Expand All @@ -244,33 +257,18 @@ bool option_model::draw_combobox( notifications_model & model,

ImGui::PushItemWidth( new_line ? -1.f : 100.f );

std::vector< const char * > labels;
auto selected = 0, counter = 0;

for( auto i = range.min; i <= range.max; i += range.step, counter++ )
{
if( std::fabs( i - value ) < 0.001f )
selected = counter;

labels.push_back( endpoint->get_option_value_description( opt, i ) );
}
int selected;
std::vector< const char * > labels = get_combo_labels( &selected );
ImGui::PushStyleColor( ImGuiCol_TextSelectedBg, { 1, 1, 1, 1 } );

try
{
int tmp_selected = selected;
float tmp_value = value;

if( ImGui::Combo( id.c_str(),
&tmp_selected,
labels.data(),
static_cast< int >( labels.size() ) ) )
if( ImGui::Combo( id.c_str(), &selected, labels.data(), static_cast< int >( labels.size() ) ) )
{
tmp_value = range.min + range.step * tmp_selected;
float tmp_value = range.min + range.step * selected;
model.add_log( rsutils::string::from()
<< "Setting " << opt << " to " << tmp_value << " (" << labels[tmp_selected] << ")" );
<< "Setting " << opt << " to " << tmp_value << " (" << labels[selected] << ")" );
set_option( opt, tmp_value, error_message );
selected = tmp_selected;
if( invalidate_flag )
*invalidate_flag = true;
item_clicked = true;
Expand All @@ -285,6 +283,7 @@ bool option_model::draw_combobox( notifications_model & model,
ImGui::PopItemWidth();
return item_clicked;
}

bool option_model::draw_slider( notifications_model & model,
std::string & error_message,
const char * description,
Expand Down Expand Up @@ -322,9 +321,9 @@ bool option_model::draw_slider( notifications_model & model,
if( ImGui::Button( edit_id.c_str(), { 20, 20 } ) )
{
if( is_all_integers() )
edit_value = rsutils::string::from() << (int)value;
edit_value = rsutils::string::from() << (int)value->as_float;
else
edit_value = rsutils::string::from() << value;
edit_value = rsutils::string::from() << value->as_float;
edit_mode = true;
}
if( ImGui::IsItemHovered() )
Expand Down Expand Up @@ -359,18 +358,18 @@ bool option_model::draw_slider( notifications_model & model,
if( read_only )
{
ImVec2 vec{ 0, 20 };
std::string text
= ( value == (int)value ) ? std::to_string( (int)value ) : std::to_string( value );
std::string text = ( value->as_float == (int)value->as_float ) ? std::to_string( (int)value->as_float )
: std::to_string( value->as_float );
if( range.min != range.max )
{
ImGui::ProgressBar( ( value / ( range.max - range.min ) ), vec, text.c_str() );
ImGui::ProgressBar( ( value->as_float / ( range.max - range.min ) ), vec, text.c_str() );
}
else // constant value options
{
auto c = ImGui::ColorConvertU32ToFloat4( ImGui::GetColorU32( ImGuiCol_FrameBg ) );
ImGui::PushStyleColor( ImGuiCol_FrameBgActive, c );
ImGui::PushStyleColor( ImGuiCol_FrameBgHovered, c );
float dummy = std::floor( value );
float dummy = std::floor( value->as_float );
if( ImGui::DragFloat( id.c_str(), &dummy, 1, 0, 0, text.c_str() ) )
{
// Changing the depth units not on advanced mode is not allowed,
Expand Down Expand Up @@ -439,7 +438,7 @@ bool option_model::draw_slider( notifications_model & model,
{
if (invalidate_flag)
*invalidate_flag = true;
model.add_log(rsutils::string::from() << "Setting " << opt << " to " << value);
model.add_log(rsutils::string::from() << "Setting " << opt << " to " << value->as_float);
}
}
edit_mode = false;
Expand All @@ -455,7 +454,7 @@ bool option_model::draw_slider( notifications_model & model,
else if( is_all_integers() )
{
// runs when changing a value with slider and not the textbox
auto int_value = static_cast< int >( value );
auto int_value = static_cast< int >( value->as_float );

if( ImGui::SliderIntWithSteps( id.c_str(),
&int_value,
Expand All @@ -479,7 +478,7 @@ bool option_model::draw_slider( notifications_model & model,
}
else
{
float tmp_value = value;
float tmp_value = value->as_float;
float temp_value_displayed = tmp_value;
float min_range_displayed = range.min;
float max_range_displayed = range.max;
Expand Down Expand Up @@ -555,7 +554,7 @@ bool option_model::draw_checkbox( notifications_model & model,
{
bool checkbox_was_clicked = false;

auto bool_value = value > 0.0f;
auto bool_value = value->as_float > 0.0f;
if( ImGui::Checkbox( label.c_str(), &bool_value ) )
{
checkbox_was_clicked = true;
Expand Down Expand Up @@ -669,7 +668,7 @@ bool option_model::set_option(rs2_option opt,
// anything...
try
{
value = endpoint->get_option(opt);
value = endpoint->get_option_value(opt);
}
catch (...)
{
Expand Down
12 changes: 7 additions & 5 deletions common/option-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace rs2
bool draw_option( bool update_read_only_options, bool is_streaming,
std::string& error_message, notifications_model& model );

std::vector< const char * > get_combo_labels( int * p_selected = nullptr ) const;

rs2_option opt;
option_range range;
std::shared_ptr<options> endpoint;
Expand All @@ -32,13 +34,13 @@ namespace rs2
bool* invalidate_flag = nullptr;
bool supported = false;
bool read_only = false;
float value = 0.0f;
std::string label = "";
std::string id = "";
rs2::option_value value;
std::string label;
std::string id;
subdevice_model* dev;
std::function<bool( option_model&, std::string&, notifications_model& )> custom_draw_method = nullptr;
bool edit_mode = false;
std::string edit_value = "";
std::string edit_value;
private:
bool is_all_integers() const;
bool is_enum() const;
Expand All @@ -59,7 +61,7 @@ namespace rs2
std::string adjust_description( const std::string& str_in, const std::string& to_be_replaced, const std::string& to_replace );
};

option_model create_option_model(rs2_option opt,
option_model create_option_model(option_value const & opt,
const std::string& opt_base_label,
subdevice_model* model,
std::shared_ptr<options> options,
Expand Down
Loading
Loading