Skip to content

Commit

Permalink
Show estimated print time in the ruler (#2305)
Browse files Browse the repository at this point in the history
* Calculate tick by mouse hover position

* Draw hover tick

* Show hover layer time

* Make sure hovered tick get updated immediately
  • Loading branch information
Noisyfox authored Oct 3, 2023
1 parent 328543c commit 85e24e6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
62 changes: 58 additions & 4 deletions src/slic3r/GUI/IMSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <imgui/imgui_internal.h>
#include <boost/algorithm/string/replace.hpp>

namespace Slic3r {

Expand Down Expand Up @@ -662,10 +663,8 @@ void IMSlider::draw_ticks(const ImRect& slideable_region) {
ImGui::RenderFrame(right_hover_box.Min, right_hover_box.Max, tick_hover_box_clr, false);

show_tooltip(*tick_it);
if (context.IO.MouseClicked[0]) {
m_tick_value = tick_it->tick;
m_tick_rect = ImVec4(tick_hover_box.Min.x, tick_hover_box.Min.y, tick_hover_box.Max.x, tick_hover_box.Max.y);
}
m_tick_value = tick_it->tick;
m_tick_rect = ImVec4(tick_hover_box.Min.x, tick_hover_box.Min.y, tick_hover_box.Max.x, tick_hover_box.Max.y);
}
++tick_it;
}
Expand Down Expand Up @@ -746,6 +745,51 @@ void IMSlider::show_tooltip(const TickCode& tick){
}
}

int IMSlider::get_tick_near_point(int v_min, int v_max, const ImVec2& pt, const ImRect& rect) {
ImS32 v_range = (v_min < v_max ? v_max - v_min : v_min - v_max);

const ImGuiAxis axis = is_horizontal() ? ImGuiAxis_X : ImGuiAxis_Y;
const float region_usable_sz = (rect.Max[axis] - rect.Min[axis]);
const float region_usable_pos_min = rect.Min[axis];

const float abs_pos = pt[axis];

float pos_ratio = (region_usable_sz > 0.0f) ? ImClamp((abs_pos - region_usable_pos_min) / region_usable_sz, 0.0f, 1.0f) : 0.0f;
if (axis == ImGuiAxis_Y)
pos_ratio = 1.0f - pos_ratio;

return v_min + (ImS32)(v_range * pos_ratio + 0.5f);
}

void IMSlider::draw_tick_on_mouse_position(const ImRect& slideable_region) {
int v_min = GetMinValue();
int v_max = GetMaxValue();
ImGuiContext& context = *GImGui;

int tick = get_tick_near_point(v_min, v_max, context.IO.MousePos, slideable_region);

// if (tick == v_min || tick == v_max) {
// return;
// }

//draw tick
ImVec2 tick_offset = ImVec2(22.0f, 14.0f) * m_scale;
float tick_width = 1.0f * m_scale;

const ImU32 tick_clr = IM_COL32(144, 144, 144, 255);

float tick_pos = get_pos_from_value(v_min, v_max, tick, slideable_region);
ImRect tick_left = ImRect(slideable_region.GetCenter().x - tick_offset.x, tick_pos - tick_width, slideable_region.GetCenter().x - tick_offset.y, tick_pos);
ImRect tick_right = ImRect(slideable_region.GetCenter().x + tick_offset.y, tick_pos - tick_width, slideable_region.GetCenter().x + tick_offset.x, tick_pos);
ImGui::RenderFrame(tick_left.Min, tick_left.Max, tick_clr, false);
ImGui::RenderFrame(tick_right.Min, tick_right.Max, tick_clr, false);

// draw layer time
std::string label = get_label(tick, ltEstimatedTime);
boost::ireplace_all(label, "\n", "");
show_tooltip(label);
}

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)
{
ImGuiWindow* window = ImGui::GetCurrentWindow();
Expand Down Expand Up @@ -914,6 +958,11 @@ bool IMSlider::vertical_slider(const char* str_id, int* higher_value, int* lower
pos_3 = pos_1 + triangle_offsets[2];
window->DrawList->AddTriangleFilled(pos_1, pos_2, pos_3, white_bg);
ImGui::RenderText(text_start + text_padding, lower_label.c_str());

// draw mouse position
if (hovered) {
draw_tick_on_mouse_position(h_selected ? higher_slideable_region : lower_slideable_region);
}
}
if (one_layer_flag)
{
Expand Down Expand Up @@ -953,6 +1002,11 @@ bool IMSlider::vertical_slider(const char* str_id, int* higher_value, int* lower
ImRect text_rect = ImRect(text_start, text_start + text_size);
ImGui::RenderFrame(text_rect.Min, text_rect.Max, white_bg, false, text_frame_rounding);
ImGui::RenderText(text_start + text_padding, higher_label.c_str());

// draw mouse position
if (hovered) {
draw_tick_on_mouse_position(one_slideable_region);
}
}

return value_changed;
Expand Down
2 changes: 2 additions & 0 deletions src/slic3r/GUI/IMSlider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class IMSlider
void draw_background_and_groove(const ImRect& bg_rect, const ImRect& groove);
void draw_colored_band(const ImRect& groove, const ImRect& slideable_region);
void draw_ticks(const ImRect& slideable_region);
void draw_tick_on_mouse_position(const ImRect& slideable_region);
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,
Expand All @@ -159,6 +160,7 @@ class IMSlider
double get_double_value(const SelectedSlider& selection);
int get_tick_from_value(double value, bool force_lower_bound = false);
float get_pos_from_value(int v_min, int v_max, int value, const ImRect& rect);
int get_tick_near_point(int v_min, int v_max, const ImVec2& pt, const ImRect& rect);

std::string get_color_for_tool_change_tick(std::set<TickCode>::const_iterator it) const;
// Get active extruders for tick.
Expand Down

0 comments on commit 85e24e6

Please sign in to comment.