ENH: add a tooltip of adding custom gcode

Change-Id: I8908dd7cdba6138a90fb9a7de92d9eb346495690
This commit is contained in:
liz.li 2023-04-10 15:09:13 +08:00 committed by Lane.Wei
parent adb6583458
commit 92266c1edf
4 changed files with 33 additions and 17 deletions

View file

@ -708,35 +708,39 @@ void IMSlider::draw_ticks(const ImRect& slideable_region) {
} }
} }
void IMSlider::show_tooltip(const TickCode& tick){ void IMSlider::show_tooltip(const std::string tooltip) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 6 * m_scale, 3 * m_scale }); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 6 * m_scale, 3 * m_scale });
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, { 3 * m_scale }); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, { 3 * m_scale });
ImGui::PushStyleColor(ImGuiCol_PopupBg, ImGuiWrapper::COL_WINDOW_BACKGROUND); ImGui::PushStyleColor(ImGuiCol_PopupBg, ImGuiWrapper::COL_WINDOW_BACKGROUND);
ImGui::PushStyleColor(ImGuiCol_Border, { 0,0,0,0 }); ImGui::PushStyleColor(ImGuiCol_Border, { 0,0,0,0 });
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.00f, 1.00f, 1.00f, 1.00f)); ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.00f, 1.00f, 1.00f, 1.00f));
ImGui::BeginTooltip(); ImGui::BeginTooltip();
ImGui::TextUnformatted(tooltip.c_str());
ImGui::EndTooltip();
ImGui::PopStyleColor(3);
ImGui::PopStyleVar(2);
}
void IMSlider::show_tooltip(const TickCode& tick){
switch (tick.type) switch (tick.type)
{ {
case CustomGCode::ColorChange: case CustomGCode::ColorChange:
break; break;
case CustomGCode::PausePrint: case CustomGCode::PausePrint:
ImGui::TextUnformatted((_u8L("Pause:") + " \"" + gcode(PausePrint) + "\"").c_str()); show_tooltip(_u8L("Pause:") + " \"" + gcode(PausePrint) + "\"");
break; break;
case CustomGCode::ToolChange: case CustomGCode::ToolChange:
ImGui::TextUnformatted(_u8L("Change Filament").c_str()); show_tooltip(_u8L("Change Filament"));
break; break;
case CustomGCode::Template: case CustomGCode::Template:
ImGui::TextUnformatted((_u8L("Custom Template:") + " \"" + gcode(Template) + "\"").c_str()); show_tooltip(_u8L("Custom Template:") + " \"" + gcode(Template) + "\"");
break; break;
case CustomGCode::Custom: case CustomGCode::Custom:
ImGui::TextUnformatted((_u8L("Custom G-code:") + " \"" + tick.extra + "\"").c_str()); show_tooltip(_u8L("Custom G-code:") + " \"" + tick.extra + "\"");
break; break;
default: default:
break; break;
} }
ImGui::EndTooltip();
ImGui::PopStyleColor(3);
ImGui::PopStyleVar(2);
} }
bool IMSlider::vertical_slider(const char* str_id, int* higher_value, int* lower_value, std::string& higher_label, std::string& lower_label,int v_min, int v_max, const ImVec2& size, SelectedSlider& selection, bool one_layer_flag, float scale) bool IMSlider::vertical_slider(const char* str_id, int* higher_value, int* lower_value, std::string& higher_label, std::string& lower_label,int v_min, int v_max, const ImVec2& size, SelectedSlider& selection, bool one_layer_flag, float scale)
@ -1224,18 +1228,26 @@ void IMSlider::render_add_menu()
ImGui::OpenPopup("slider_add_menu_popup"); ImGui::OpenPopup("slider_add_menu_popup");
if (ImGui::BeginPopup("slider_add_menu_popup")) { if (ImGui::BeginPopup("slider_add_menu_popup")) {
bool menu_item_enable = m_draw_mode != dmSequentialFffPrint; bool menu_item_enable = m_draw_mode != dmSequentialFffPrint;
bool hovered = false;
{ {
if (menu_item_with_icon(_u8L("Add Pause").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable)) { if (menu_item_with_icon(_u8L("Add Pause").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable, &hovered)) {
add_code_as_tick(PausePrint); add_code_as_tick(PausePrint);
} }
if (menu_item_with_icon(_u8L("Add Custom G-code").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable)) { if (hovered) { show_tooltip(_u8L("Insert a pause command at the beginning of this layer.")); }
if (menu_item_with_icon(_u8L("Add Custom G-code").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable, &hovered)) {
m_show_custom_gcode_window = true; m_show_custom_gcode_window = true;
} }
if (hovered) { show_tooltip(_u8L("Insert custom G-code at the beginning of this layer.")); }
if (!gcode(Template).empty()) { if (!gcode(Template).empty()) {
if (menu_item_with_icon(_u8L("Add Custom Template").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable)) { if (menu_item_with_icon(_u8L("Add Custom Template").c_str(), "", ImVec2(0, 0), 0, false, menu_item_enable, &hovered)) {
add_code_as_tick(Template); add_code_as_tick(Template);
} }
} }
if (hovered) { show_tooltip(_u8L("Insert template custom G-code at the beginning of this layer.")); }
if (menu_item_with_icon(_u8L("Jump to Layer").c_str(), "")) { if (menu_item_with_icon(_u8L("Jump to Layer").c_str(), "")) {
m_show_go_to_layer_dialog = true; m_show_go_to_layer_dialog = true;
} }
@ -1250,11 +1262,13 @@ void IMSlider::render_add_menu()
for (int i = 0; i < extruder_num; i++) { for (int i = 0; i < extruder_num; i++) {
std::array<float, 4> rgba = decode_color_to_float_array(m_extruder_colors[i]); std::array<float, 4> rgba = decode_color_to_float_array(m_extruder_colors[i]);
ImU32 icon_clr = IM_COL32(rgba[0] * 255.0f, rgba[1] * 255.0f, rgba[2] * 255.0f, rgba[3] * 255.0f); ImU32 icon_clr = IM_COL32(rgba[0] * 255.0f, rgba[1] * 255.0f, rgba[2] * 255.0f, rgba[3] * 255.0f);
if (menu_item_with_icon((_u8L("Filament ") + std::to_string(i + 1)).c_str(), "", ImVec2(14, 14) * m_scale, icon_clr)) add_code_as_tick(ToolChange, i + 1); if (menu_item_with_icon((_u8L("Filament ") + std::to_string(i + 1)).c_str(), "", ImVec2(14, 14) * m_scale, icon_clr, false, true, &hovered)) add_code_as_tick(ToolChange, i + 1);
if (hovered) { show_tooltip(_u8L("Change filament at the beginning of this layer.")); }
} }
end_menu(); end_menu();
} }
} }
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }

View file

@ -147,6 +147,7 @@ protected:
void draw_colored_band(const ImRect& groove, const ImRect& slideable_region); void draw_colored_band(const ImRect& groove, const ImRect& slideable_region);
void draw_ticks(const ImRect& slideable_region); void draw_ticks(const ImRect& slideable_region);
void show_tooltip(const TickCode& tick); //menu void show_tooltip(const TickCode& tick); //menu
void show_tooltip(const std::string tooltip); //menu
bool vertical_slider(const char* str_id, int* higher_value, int* lower_value, bool vertical_slider(const char* str_id, int* higher_value, int* lower_value,
std::string& higher_label, std::string& lower_label, std::string& higher_label, std::string& lower_label,
int v_min, int v_max, const ImVec2& size, int v_min, int v_max, const ImVec2& size,

View file

@ -1119,7 +1119,7 @@ bool ImGuiWrapper::undo_redo_list(const ImVec2& size, const bool is_undo, bool (
// To do that we push a ColorMarkerHovered symbol at the very beginning of the label // To do that we push a ColorMarkerHovered symbol at the very beginning of the label
// This symbol will be used to a color selection for the highlighted letters. // This symbol will be used to a color selection for the highlighted letters.
// see imgui_draw.cpp, void ImFont::RenderText() // see imgui_draw.cpp, void ImFont::RenderText()
static bool selectable(const char* label, bool selected, ImGuiSelectableFlags flags = 0, const ImVec2& size_arg = ImVec2(0, 0)) static bool selectable(const char* label, bool selected, ImGuiSelectableFlags flags = 0, const ImVec2& size_arg = ImVec2(0, 0), bool* out_hovered = nullptr)
{ {
ImGuiWindow* window = ImGui::GetCurrentWindow(); ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems) if (window->SkipItems)
@ -1265,10 +1265,11 @@ static bool selectable(const char* label, bool selected, ImGuiSelectableFlags fl
if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor(); if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor();
if (hovered || selected) ImGui::PopStyleColor(); if (hovered || selected) ImGui::PopStyleColor();
if (out_hovered) *out_hovered = hovered;
// Automatically close popups // Automatically close popups
if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.CurrentItemFlags & ImGuiItemFlags_SelectableDontClosePopup)) if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.CurrentItemFlags & ImGuiItemFlags_SelectableDontClosePopup))
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags);
return pressed; return pressed;
} }
@ -1437,7 +1438,7 @@ void end_menu()
ImGui::EndMenu(); ImGui::EndMenu();
} }
bool menu_item_with_icon(const char *label, const char *shortcut, ImVec2 icon_size /* = ImVec2(0, 0)*/, ImU32 icon_color /* = 0*/, bool selected /* = false*/, bool enabled /* = true*/) bool menu_item_with_icon(const char *label, const char *shortcut, ImVec2 icon_size /* = ImVec2(0, 0)*/, ImU32 icon_color /* = 0*/, bool selected /* = false*/, bool enabled /* = true*/, bool* hovered/* = nullptr*/)
{ {
ImGuiWindow *window = ImGui::GetCurrentWindow(); ImGuiWindow *window = ImGui::GetCurrentWindow();
if (window->SkipItems) return false; if (window->SkipItems) return false;
@ -1470,7 +1471,7 @@ bool menu_item_with_icon(const char *label, const char *shortcut, ImVec2 icon_si
float shortcut_w = shortcut ? ImGui::CalcTextSize(shortcut, NULL).x : 0.0f; float shortcut_w = shortcut ? ImGui::CalcTextSize(shortcut, NULL).x : 0.0f;
float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_w, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_w, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame
float extra_w = std::max(0.0f, ImGui::GetContentRegionAvail().x - min_w); float extra_w = std::max(0.0f, ImGui::GetContentRegionAvail().x - min_w);
pressed = selectable(label, false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); pressed = selectable(label, false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f), hovered);
if (icon_size.x != 0 && icon_size.y != 0) { if (icon_size.x != 0 && icon_size.y != 0) {
float selectable_pos_y = pos.y + -0.5f * style.ItemSpacing.y; float selectable_pos_y = pos.y + -0.5f * style.ItemSpacing.y;

View file

@ -39,7 +39,7 @@ bool button_with_pos(ImTextureID user_texture_id,
const ImVec2 &margin = ImVec2(0, 0)); const ImVec2 &margin = ImVec2(0, 0));
bool begin_menu(const char *label, bool enabled = true); bool begin_menu(const char *label, bool enabled = true);
void end_menu(); void end_menu();
bool menu_item_with_icon(const char *label, const char *shortcut, ImVec2 icon_size = ImVec2(0, 0), ImU32 icon_color = 0, bool selected = false, bool enabled = true); bool menu_item_with_icon(const char *label, const char *shortcut, ImVec2 icon_size = ImVec2(0, 0), ImU32 icon_color = 0, bool selected = false, bool enabled = true, bool* hovered = nullptr);
class ImGuiWrapper class ImGuiWrapper