FIX: support filament display in group result

1. Add "Sup." prefix for support filament
2. Align the filament group elems

jira:STUDIO-10263

Signed-off-by: xun.zhang <xun.zhang@bambulab.com>
Change-Id: Idd6d11d14cd378142dff03596eea5efb47dde79f
This commit is contained in:
xun.zhang 2025-02-10 21:05:35 +08:00 committed by lane.wei
parent e7454fa47d
commit 038df3180a
4 changed files with 96 additions and 31 deletions

View File

@ -1042,11 +1042,12 @@ void GCodeViewer::load(const GCodeProcessorResult& gcode_result, const Print& pr
std::vector<int> filament_maps = print.get_filament_maps();
std::vector<std::string> color_opt = print.config().option<ConfigOptionStrings>("filament_colour")->values;
std::vector<std::string> type_opt = print.config().option<ConfigOptionStrings>("filament_type")->values;
std::vector<unsigned char> support_filament_opt = print.config().option<ConfigOptionBools>("filament_is_support")->values;
for (auto extruder_id : m_extruder_ids) {
if (filament_maps[extruder_id] == 1) {
m_left_extruder_filament.push_back({type_opt[extruder_id], color_opt[extruder_id], extruder_id});
m_left_extruder_filament.push_back({type_opt[extruder_id], color_opt[extruder_id], extruder_id, (bool)(support_filament_opt[extruder_id])});
} else {
m_right_extruder_filament.push_back({type_opt[extruder_id], color_opt[extruder_id], extruder_id});
m_right_extruder_filament.push_back({type_opt[extruder_id], color_opt[extruder_id], extruder_id, (bool)(support_filament_opt[extruder_id])});
}
}
//BBS: add mutex for protection of gcode result
@ -4705,11 +4706,38 @@ void GCodeViewer::render_legend_color_arr_recommen(float window_padding)
bool less_to_single_ext = delta_weight_to_single_ext > EPSILON || delta_change_to_single_ext > 0;
bool more_to_best = delta_weight_to_best > EPSILON || delta_change_to_best > 0;
auto get_filament_display_type = [](const ExtruderFilament& filament) {
if (filament.is_support_filament && (filament.type == "PLA" || filament.type == "PA" || filament.type == "ABS"))
return "Sup." + filament.type;
return filament.type;
};
// BBS AMS containers
float line_height = ImGui::GetFrameHeight();
int AMS_filament_max_num = std::max(m_left_extruder_filament.size(), m_right_extruder_filament.size());
float three_words_width = imgui.calc_text_size("ABC").x;
float ams_item_height = std::ceil(AMS_filament_max_num / 4.0f) * (three_words_width * 1.6f + line_height) + line_height * 2;
float ams_item_height = 0;
float filament_group_item_align_width = 0;
{
float three_words_width = imgui.calc_text_size("ABC").x;
const int line_capacity = 4;
for (const auto& extruder_filaments : {m_left_extruder_filament,m_right_extruder_filament })
{
float container_height = 0.f;
for (size_t idx = 0; idx < extruder_filaments.size(); idx += line_capacity) {
float text_line_height = 0;
for (int j = idx; j < extruder_filaments.size() && j < idx + line_capacity; ++j) {
auto text_info = imgui.calculate_filament_group_text_size(get_filament_display_type(extruder_filaments[j]));
auto text_size = std::get<0>(text_info);
filament_group_item_align_width = max(filament_group_item_align_width, text_size.x);
text_line_height = max(text_line_height, text_size.y);
}
container_height += (three_words_width * 1.5f + text_line_height );
}
container_height += 2 * line_height;
ams_item_height = std::max(ams_item_height, container_height);
}
}
int tips_count = 8;
if (more_to_best)
@ -4754,7 +4782,7 @@ void GCodeViewer::render_legend_color_arr_recommen(float window_padding)
ImGui::Dummy({window_padding, window_padding});
int index = 1;
for (const auto &extruder_filament : m_left_extruder_filament) {
imgui.filament_group(extruder_filament.type, extruder_filament.hex_color.c_str(), extruder_filament.filament_id);
imgui.filament_group(get_filament_display_type(extruder_filament), extruder_filament.hex_color.c_str(), extruder_filament.filament_id, filament_group_item_align_width);
if (index % 4 != 0) { ImGui::SameLine(0, spacing); }
index++;
}
@ -4769,7 +4797,7 @@ void GCodeViewer::render_legend_color_arr_recommen(float window_padding)
ImGui::Dummy({window_padding, window_padding});
int index = 1;
for (const auto &extruder_filament : m_right_extruder_filament) {
imgui.filament_group(extruder_filament.type, extruder_filament.hex_color.c_str(), extruder_filament.filament_id);
imgui.filament_group(get_filament_display_type(extruder_filament), extruder_filament.hex_color.c_str(), extruder_filament.filament_id, filament_group_item_align_width);
if (index % 4 != 0) { ImGui::SameLine(0, spacing); }
index++;
}

View File

@ -715,6 +715,7 @@ public:
std::string type;
std::string hex_color;
unsigned char filament_id;
bool is_support_filament;
};
enum class EViewType : unsigned char

View File

@ -2899,17 +2899,34 @@ void ImGuiWrapper::clipboard_set(void* /* user_data */, const char* text)
}
}
void ImGuiWrapper::filament_group(const std::string &filament_type, const char *hex_color, unsigned char filament_id)
std::tuple<ImVec2, bool> ImGuiWrapper::calculate_filament_group_text_size(const std::string& filament_type)
{
ImVec2 text_size = ImGui::CalcTextSize(filament_type.c_str());
float four_word_width = ImGui::CalcTextSize("ABCD").x;
float wrap_width = four_word_width;
float line_height = ImGui::GetTextLineHeight();
bool is_multiline = text_size.x > wrap_width;
int line_count = std::ceil(text_size.x / wrap_width);
float text_height = line_count * line_height;
float final_width = is_multiline ? wrap_width : text_size.x;
float final_height = line_count * line_height;
return { { final_width,final_height },is_multiline };
}
void ImGuiWrapper::filament_group(const std::string& filament_type, const char* hex_color, unsigned char filament_id, float align_width)
{
//ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
std::string id = std::to_string(static_cast<unsigned int> (filament_id + 1));
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
static ImTextureID transparent;
ImVec2 text_size = ImGui::CalcTextSize(filament_type.c_str());
// BBS image sizing based on text width (DPI scaling)
float four_word_width = ImGui::CalcTextSize("ABCD").x;
float img_width = ImGui::CalcTextSize("ABC").x;
ImVec2 img_size = {img_width, img_width * 1.5f};
float img_width = ImGui::CalcTextSize("ABC").x;
ImVec2 img_size = { img_width, img_width * 1.5f };
ImVec2 id_text_size = this->calc_text_size(id);
unsigned char rgba[4];
rgba[3] = 0xff;
@ -2921,24 +2938,40 @@ void ImGuiWrapper::filament_group(const std::string &filament_type, const char *
BitmapCache::load_from_svg_file_change_color(Slic3r::resources_dir() + svg_path, img_size.x, img_size.y, transparent, hex_color);
ImGui::BeginGroup();
{
ImVec2 cursor_pos = ImGui::GetCursorScreenPos();
draw_list->AddImage(transparent, cursor_pos, {cursor_pos.x + img_size.x, cursor_pos.y + img_size.y}, {0, 0}, {1, 1}, ImGui::GetColorU32(ImVec4(1.f, 1.f, 1.f, 1.f)));
// image border test
// draw_list->AddRect(cursor_pos, {cursor_pos.x + img_size.x, cursor_pos.y + img_size.y}, IM_COL32(0, 0, 0, 255));
ImVec2 current_cursor = ImGui::GetCursorPos();
ImGui::SetCursorPos({current_cursor.x + (img_size.x - id_text_size.x) * 0.5f + 2, current_cursor.y + (img_size.y - id_text_size.y) * 0.5f - 2});
float gray = 0.299 * rgba[0] + 0.587 * rgba[1] + 0.114 * rgba[2];
ImVec4 text_color = gray < 80 ? ImVec4(1.0f, 1.0f, 1.0f, 1.0f) : ImVec4(0, 0, 0, 1.0f);
this->text_colored(text_color, id.c_str());
float text_width_max = four_word_width;
if (filament_type.size() < 4) text_width_max = text_size.x;
current_cursor = ImGui::GetCursorPos();
ImGui::SetCursorPos({current_cursor.x + (img_size.x - text_width_max) * 0.5f + 2, current_cursor.y + 4});
ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + four_word_width);
this->text(filament_type);
ImGui::PopTextWrapPos();
ImGui::EndGroup();
ImVec2 cursor_pos = ImGui::GetCursorScreenPos();
draw_list->AddImage(transparent, cursor_pos, { cursor_pos.x + img_size.x, cursor_pos.y + img_size.y }, { 0, 0 }, { 1, 1 }, ImGui::GetColorU32(ImVec4(1.f, 1.f, 1.f, 1.f)));
// image border test
// draw_list->AddRect(cursor_pos, {cursor_pos.x + img_size.x, cursor_pos.y + img_size.y}, IM_COL32(0, 0, 0, 255));
ImVec2 current_cursor = ImGui::GetCursorPos();
ImGui::SetCursorPos({ current_cursor.x + (img_size.x - id_text_size.x) * 0.5f + 2, current_cursor.y + (img_size.y - id_text_size.y) * 0.5f - 2 });
float gray = 0.299 * rgba[0] + 0.587 * rgba[1] + 0.114 * rgba[2];
ImVec4 text_color = gray < 80 ? ImVec4(1.0f, 1.0f, 1.0f, 1.0f) : ImVec4(0, 0, 0, 1.0f);
this->text_colored(text_color, id.c_str());
auto wrapped_text_info = calculate_filament_group_text_size(filament_type);
ImVec2 wrapped_text_size = std::get<0>(wrapped_text_info);
bool is_multiline = std::get<1>(wrapped_text_info);
float text_y_offset = 4.f;
float text_x_offset = is_multiline ? (img_size.x - wrapped_text_size.x) * 0.5f + 2.f : (img_size.x - wrapped_text_size.x) * 0.5f + 2.f;
auto cursor_x_before_text = ImGui::GetCursorPosX();
current_cursor = ImGui::GetCursorPos();
ImGui::SetCursorPos({
current_cursor.x + text_x_offset,
current_cursor.y + text_y_offset
});
if (is_multiline) {
ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + wrapped_text_size.x);
}
this->text(filament_type);
if (is_multiline) {
ImGui::PopTextWrapPos();
}
ImGui::Dummy(ImVec2(align_width, 0));
ImGui::EndGroup();
}
//ImGui::PopStyleVar(1);
}

View File

@ -153,7 +153,10 @@ public:
void text_wrapped(const wxString &label, float wrap_width);
void tooltip(const char *label, float wrap_width);
void tooltip(const wxString &label, float wrap_width);
void filament_group(const std::string &filament_type, const char *hex_color, unsigned char filament_id);
void filament_group(const std::string &filament_type, const char *hex_color, unsigned char filament_id, float align_width);
// text size and is_multi_line
std::tuple<ImVec2,bool> calculate_filament_group_text_size(const std::string& filament_type);
void sub_title(const std::string &label);