Refactored code

This commit is contained in:
YuSanka 2018-08-22 14:24:30 +02:00
parent a5119a41a5
commit d2282c4bf9
3 changed files with 97 additions and 76 deletions

View File

@ -1053,11 +1053,13 @@ void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFl
add_objects_list(parent, sizer);
// experiment with slider
PrusaDoubleSlider* slider_h = new PrusaDoubleSlider(parent, wxID_ANY, 50, 70, 0, 200, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL);
PrusaDoubleSlider* slider_h = new PrusaDoubleSlider(parent, wxID_ANY, 50, 70, 0, 200, wxDefaultPosition,
wxSize(60, wxDefaultSize.y), wxSL_HORIZONTAL);
sizer->AddSpacer(5);
sizer->Add(slider_h, 0, wxEXPAND | wxLEFT, 20);
sizer->AddSpacer(5);
PrusaDoubleSlider* slider_v = new PrusaDoubleSlider(parent, wxID_ANY, 5, 7, 0, 10, wxDefaultPosition, wxSize(wxDefaultSize.x ,150), wxSL_VERTICAL);
PrusaDoubleSlider* slider_v = new PrusaDoubleSlider(parent, wxID_ANY, 50, 70, 0, 200, wxDefaultPosition,
wxSize(wxDefaultSize.x ,250), wxSL_VERTICAL);
slider_v->SetKoefForLabels(0.15);
sizer->AddSpacer(5);
sizer->Add(slider_v, 0, wxLEFT, 20);

View File

@ -775,8 +775,7 @@ PrusaDoubleSlider::PrusaDoubleSlider( wxWindow *parent,
Slic3r::GUI::from_u8(Slic3r::var("up_half_circle.png")), wxBITMAP_TYPE_PNG);
m_thumb_lower = wxBitmap(style == wxSL_HORIZONTAL ? Slic3r::GUI::from_u8(Slic3r::var("left_half_circle.png")) :
Slic3r::GUI::from_u8(Slic3r::var("down_half_circle.png")), wxBITMAP_TYPE_PNG);
m_thumb_size = m_thumb_lower.GetSize();
m_selection = ssUndef;
// slider events
@ -806,7 +805,7 @@ PrusaDoubleSlider::PrusaDoubleSlider( wxWindow *parent,
wxSize PrusaDoubleSlider::DoGetBestSize() const
{
wxSize size = wxControl::DoGetBestSize();
const wxSize size = wxControl::DoGetBestSize();
if (size.x > 1 && size.y > 1)
return size;
const int new_size = is_horizontal() ? 80 : 120;
@ -904,85 +903,98 @@ void PrusaDoubleSlider::render()
draw_scroll_line(dc, lower_pos, higher_pos);
//lower slider:
wxPoint pos = is_horizontal() ? wxPoint(lower_pos, height*0.5) : wxPoint(0.5*width, lower_pos);
draw_lower_thumb(dc, pos);
draw_thumb(dc, lower_pos, ssLower);
//higher slider:
pos = is_horizontal() ? wxPoint(higher_pos, height*0.5) : wxPoint(0.5*width, higher_pos);
draw_higher_thumb(dc, pos);
draw_thumb(dc, higher_pos, ssHigher);
}
void PrusaDoubleSlider::draw_info_line(wxDC& dc, const wxPoint& pos, const wxSize& thumb_size, const SelectedSlider selection)
void PrusaDoubleSlider::draw_info_line(wxDC& dc, const wxPoint& pos, const SelectedSlider selection) const
{
if (m_selection == selection) {
dc.SetPen(DARK_ORANGE_PEN);
is_horizontal() ? dc.DrawLine(pos.x, pos.y - thumb_size.y, pos.x, pos.y + thumb_size.y):
dc.DrawLine(pos.x - thumb_size.x, pos.y-1, pos.x + thumb_size.x, pos.y-1);
is_horizontal() ? dc.DrawLine(pos.x, pos.y - m_thumb_size.y, pos.x, pos.y + m_thumb_size.y):
dc.DrawLine(pos.x - m_thumb_size.x, pos.y-1, pos.x + m_thumb_size.x, pos.y-1);
}
}
wxString PrusaDoubleSlider::get_label(const int value)
wxString PrusaDoubleSlider::get_label(const SelectedSlider& selection) const
{
const int value = selection == ssLower ? m_lower_value : m_higher_value;
return m_label_koef == 1.0 ? wxString::Format("%d", value) :
wxNumberFormatter::ToString(m_label_koef*value, 2, wxNumberFormatter::Style_None);
}
void PrusaDoubleSlider::draw_lower_thumb(wxDC& dc, const wxPoint& pos)
void PrusaDoubleSlider::draw_thumb_text(wxDC& dc, const wxPoint& pos, const SelectedSlider& selection) const
{
if (selection == ssUndef) return;
wxCoord text_width, text_height;
const wxString label = get_label(selection);
dc.GetTextExtent(label, &text_width, &text_height);
wxPoint text_pos;
if (selection ==ssLower)
text_pos = is_horizontal() ? wxPoint(pos.x + 1, pos.y + m_thumb_size.x) :
wxPoint(pos.x + m_thumb_size.x+1, pos.y - 0.5*text_height - 1);
else
text_pos = is_horizontal() ? wxPoint(pos.x - text_width - 1, pos.y - m_thumb_size.x - text_height) :
wxPoint(pos.x - text_width - 1 - m_thumb_size.x, pos.y - 0.5*text_height + 1);
dc.DrawText(label, text_pos);
}
void PrusaDoubleSlider::draw_thumb_item(wxDC& dc, const wxPoint& pos, const SelectedSlider& selection)
{
wxCoord x_draw, y_draw;
if (selection == ssLower) {
if (is_horizontal()) {
x_draw = pos.x - m_thumb_size.x;
y_draw = pos.y - int(0.5*m_thumb_size.y);
}
else {
x_draw = pos.x - int(0.5*m_thumb_size.x);
y_draw = pos.y;
}
}
else{
if (is_horizontal()) {
x_draw = pos.x;
y_draw = pos.y - int(0.5*m_thumb_size.y);
}
else {
x_draw = pos.x - int(0.5*m_thumb_size.x);
y_draw = pos.y - m_thumb_size.y;
}
}
dc.DrawBitmap(selection == ssLower ? m_thumb_lower : m_thumb_higher, x_draw, y_draw);
// Update thumb rect
update_thumb_rect(x_draw, y_draw, selection);
}
void PrusaDoubleSlider::draw_thumb(wxDC& dc, const wxCoord& pos_coord, const SelectedSlider& selection)
{
//calculate thumb position on slider line
int width, height;
GetSize(&width, &height);
const wxPoint pos = is_horizontal() ? wxPoint(pos_coord, height*0.5) : wxPoint(0.5*width, pos_coord);
// Draw thumb
wxCoord x_draw, y_draw;
const wxSize thumb_size = m_thumb_lower.GetSize();
if (is_horizontal()) {
x_draw = pos.x - thumb_size.x;
y_draw = pos.y - int(0.5*thumb_size.y);
}
else {
x_draw = pos.x - int(0.5*thumb_size.x);
y_draw = pos.y;
}
dc.DrawBitmap(m_thumb_lower, x_draw, y_draw);
draw_thumb_item(dc, pos, selection);
// Draw info_line
draw_info_line(dc, pos, thumb_size, ssLower);
draw_info_line(dc, pos, selection);
// Draw thumb text
wxCoord text_width, text_height;
wxString label = get_label(m_lower_value);
dc.GetTextExtent(label, &text_width, &text_height);
wxPoint text_pos = is_horizontal() ? wxPoint(pos.x + 1, pos.y + thumb_size.x) :
wxPoint(pos.x + thumb_size.x+1, pos.y - 0.5*text_height - 1);
dc.DrawText(label, text_pos);
// Update thumb rect
m_rect_lower_thumb = wxRect(x_draw, y_draw, thumb_size.x, thumb_size.y);
draw_thumb_text(dc, pos, selection);
}
void PrusaDoubleSlider::draw_higher_thumb(wxDC& dc, const wxPoint& pos)
void PrusaDoubleSlider::update_thumb_rect(const wxCoord& begin_x, const wxCoord& begin_y, const SelectedSlider& selection)
{
wxCoord x_draw, y_draw;
const wxSize thumb_size = m_thumb_higher.GetSize();
if (is_horizontal()) {
x_draw = pos.x;
y_draw = pos.y - int(0.5*thumb_size.y);
}
else {
x_draw = pos.x - int(0.5*thumb_size.x);
y_draw = pos.y - thumb_size.y;
}
dc.DrawBitmap(m_thumb_higher, x_draw, y_draw);
// Draw info_line
draw_info_line(dc, pos, thumb_size, ssHigher);
// Draw thumb text
wxCoord text_width, text_height;
wxString label = get_label(m_higher_value);
dc.GetTextExtent(label, &text_width, &text_height);
wxPoint text_pos = is_horizontal() ? wxPoint(pos.x - text_width-1, pos.y - thumb_size.x - text_height) :
wxPoint(pos.x - text_width - 1 - thumb_size.x, pos.y - 0.5*text_height + 1);
dc.DrawText(label, text_pos);
// Update thumb rect
m_rect_higher_thumb = wxRect(x_draw, y_draw, thumb_size.x, thumb_size.y);
const wxRect& rect = wxRect(begin_x, begin_y, m_thumb_size.x, m_thumb_size.y);
if (selection == ssLower)
m_rect_lower_thumb = rect;
else
m_rect_higher_thumb = rect;
}
int PrusaDoubleSlider::position_to_value(wxDC& dc, const wxCoord x, const wxCoord y)
@ -1030,6 +1042,8 @@ void PrusaDoubleSlider::OnLeftDown(wxMouseEvent& event)
wxClientDC dc(this);
wxPoint pos = event.GetLogicalPosition(dc);
detect_selected_slider(pos);
Refresh();
Update();
event.Skip();
}
@ -1078,7 +1092,6 @@ void PrusaDoubleSlider::OnMotion(wxMouseEvent& event)
void PrusaDoubleSlider::OnLeftUp(wxMouseEvent& event)
{
m_is_left_down = false;
m_selection = ssUndef;
Refresh();
Update();
event.Skip();
@ -1099,6 +1112,7 @@ void PrusaDoubleSlider::OnEnterWin(wxMouseEvent& event)
void PrusaDoubleSlider::OnLeaveWin(wxMouseEvent& event)
{
m_is_focused = false;
m_selection = ssUndef;
OnLeftUp(event);
}

View File

@ -523,11 +523,10 @@ public:
int GetHigherValue() {
return m_higher_value;
}
wxSize DoGetBestSize() const override;
void SetLowerValue(int lower_val);
void SetHigherValue(int higher_val);
void SetKoefForLabels(float koef){ m_label_koef = koef;}
wxSize DoGetBestSize() const override;
void SetKoefForLabels(const double koef){ m_label_koef = koef;}
void OnPaint(wxPaintEvent& ){ render();}
void OnLeftDown(wxMouseEvent& event);
@ -541,21 +540,26 @@ public:
protected:
void render();
void draw_info_line(wxDC& dc, const wxPoint& pos, const wxSize& thumb_size, SelectedSlider selection);
wxString get_label(const int value);
void draw_focus_rect();
void draw_scroll_line(wxDC& dc, const int lower_pos, const int higher_pos);
void draw_thumb(wxDC& dc, const wxCoord& pos_coord, const SelectedSlider& selection);
void draw_thumb_item(wxDC& dc, const wxPoint& pos, const SelectedSlider& selection);
void draw_info_line(wxDC& dc, const wxPoint& pos, SelectedSlider selection) const;
void draw_thumb_text(wxDC& dc, const wxPoint& pos, const SelectedSlider& selection) const;
void update_thumb_rect(const wxCoord& begin_x, const wxCoord& begin_y, const SelectedSlider& selection);
void detect_selected_slider(const wxPoint& pt, const bool is_mouse_wheel = false);
void correct_lower_value();
void correct_higher_value();
void draw_scroll_line(wxDC& dc, const int lower_pos, const int higher_pos);
double get_scroll_step();
void get_lower_and_higher_position(int& lower_pos, int& higher_pos);
void draw_focus_rect();
void draw_lower_thumb(wxDC& dc, const wxPoint& pos);
void draw_higher_thumb(wxDC& dc, const wxPoint& pos);
int position_to_value(wxDC& dc, const wxCoord x, const wxCoord y);
void detect_selected_slider(const wxPoint& pt, const bool is_mouse_wheel = false);
void move_current_thumb(const bool condition);
bool is_point_in_rect(const wxPoint& pt, const wxRect& rect);
bool is_horizontal() const { return m_style == wxSL_HORIZONTAL; }
void move_current_thumb(const bool condition);
double get_scroll_step();
wxString get_label(const SelectedSlider& selection) const;
void get_lower_and_higher_position(int& lower_pos, int& higher_pos);
int position_to_value(wxDC& dc, const wxCoord x, const wxCoord y);
private:
int m_min_value;
@ -570,6 +574,7 @@ private:
wxRect m_rect_lower_thumb;
wxRect m_rect_higher_thumb;
wxSize m_thumb_size;
long m_style;
float m_label_koef = 1.0;