2DBed.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Bed shape dialog
  2. package Slic3r::GUI::2DBed;
  3. use strict;
  4. use warnings;
  5. use List::Util qw(min max);
  6. use Slic3r::Geometry qw(X Y unscale deg2rad);
  7. use Slic3r::Geometry::Clipper qw(intersection_pl);
  8. use Wx qw(:misc :pen :brush :font :systemsettings wxTAB_TRAVERSAL wxSOLID);
  9. use Wx::Event qw(EVT_PAINT EVT_ERASE_BACKGROUND EVT_MOUSE_EVENTS EVT_SIZE);
  10. use base qw(Wx::Panel Class::Accessor);
  11. __PACKAGE__->mk_accessors(qw(bed_shape interactive pos _scale_factor _shift on_move _painted));
  12. sub new {
  13. my ($class, $parent, $bed_shape) = @_;
  14. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [250,-1], wxTAB_TRAVERSAL);
  15. $self->{user_drawn_background} = $^O ne 'darwin';
  16. $self->bed_shape($bed_shape // []);
  17. EVT_PAINT($self, \&_repaint);
  18. EVT_ERASE_BACKGROUND($self, sub {}) if $self->{user_drawn_background};
  19. EVT_MOUSE_EVENTS($self, \&_mouse_event);
  20. EVT_SIZE($self, sub { $self->Refresh; });
  21. return $self;
  22. }
  23. sub _repaint {
  24. my ($self, $event) = @_;
  25. my $dc = Wx::AutoBufferedPaintDC->new($self);
  26. my ($cw, $ch) = $self->GetSizeWH;
  27. return if $cw == 0; # when canvas is not rendered yet, size is 0,0
  28. if ($self->{user_drawn_background}) {
  29. # On all systems the AutoBufferedPaintDC() achieves double buffering.
  30. # On MacOS the background is erased, on Windows the background is not erased
  31. # and on Linux/GTK the background is erased to gray color.
  32. # Fill DC with the background on Windows & Linux/GTK.
  33. my $color = Wx::SystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT);
  34. $dc->SetPen(Wx::Pen->new($color, 1, wxSOLID));
  35. $dc->SetBrush(Wx::Brush->new($color, wxSOLID));
  36. my $rect = $self->GetUpdateRegion()->GetBox();
  37. $dc->DrawRectangle($rect->GetLeft(), $rect->GetTop(), $rect->GetWidth(), $rect->GetHeight());
  38. }
  39. # turn $cw and $ch from sizes to max coordinates
  40. $cw--;
  41. $ch--;
  42. my $cbb = Slic3r::Geometry::BoundingBoxf->new_from_points([
  43. Slic3r::Pointf->new(0, 0),
  44. Slic3r::Pointf->new($cw, $ch),
  45. ]);
  46. # leave space for origin point
  47. $cbb->set_x_min($cbb->x_min + 4);
  48. $cbb->set_x_max($cbb->x_max - 4);
  49. $cbb->set_y_max($cbb->y_max - 4);
  50. # leave space for origin label
  51. $cbb->set_y_max($cbb->y_max - 13);
  52. # read new size
  53. ($cw, $ch) = @{$cbb->size};
  54. my $ccenter = $cbb->center;
  55. # get bounding box of bed shape in G-code coordinates
  56. my $bed_shape = $self->bed_shape;
  57. my $bed_polygon = Slic3r::Polygon->new_scale(@$bed_shape);
  58. my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($bed_shape);
  59. $bb->merge_point(Slic3r::Pointf->new(0,0)); # origin needs to be in the visible area
  60. my ($bw, $bh) = @{$bb->size};
  61. my $bcenter = $bb->center;
  62. # calculate the scaling factor for fitting bed shape in canvas area
  63. my $sfactor = min($cw/$bw, $ch/$bh);
  64. my $shift = Slic3r::Pointf->new(
  65. $ccenter->x - $bcenter->x * $sfactor,
  66. $ccenter->y - $bcenter->y * $sfactor, #-
  67. );
  68. $self->_scale_factor($sfactor);
  69. $self->_shift(Slic3r::Pointf->new(
  70. $shift->x + $cbb->x_min,
  71. $shift->y - ($cbb->y_max-$self->GetSize->GetHeight), #++
  72. ));
  73. # draw bed fill
  74. {
  75. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  76. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxSOLID));
  77. $dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
  78. }
  79. # draw grid
  80. {
  81. my $step = 10; # 1cm grid
  82. my @polylines = ();
  83. for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
  84. push @polylines, Slic3r::Polyline->new_scale([$x, $bb->y_min], [$x, $bb->y_max]);
  85. }
  86. for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
  87. push @polylines, Slic3r::Polyline->new_scale([$bb->x_min, $y], [$bb->x_max, $y]);
  88. }
  89. @polylines = @{intersection_pl(\@polylines, [$bed_polygon])};
  90. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(230,230,230), 1, wxSOLID));
  91. $dc->DrawLine(map @{$self->to_pixels([map unscale($_), @$_])}, @$_[0,-1]) for @polylines;
  92. }
  93. # draw bed contour
  94. {
  95. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  96. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxTRANSPARENT));
  97. $dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
  98. }
  99. my $origin_px = $self->to_pixels(Slic3r::Pointf->new(0,0));
  100. # draw axes
  101. {
  102. my $axes_len = 50;
  103. my $arrow_len = 6;
  104. my $arrow_angle = deg2rad(45);
  105. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(255,0,0), 2, wxSOLID)); # red
  106. my $x_end = Slic3r::Pointf->new($origin_px->[X] + $axes_len, $origin_px->[Y]);
  107. $dc->DrawLine(@$origin_px, @$x_end);
  108. foreach my $angle (-$arrow_angle, +$arrow_angle) {
  109. my $end = $x_end->clone;
  110. $end->translate(-$arrow_len, 0);
  111. $end->rotate($angle, $x_end);
  112. $dc->DrawLine(@$x_end, @$end);
  113. }
  114. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,255,0), 2, wxSOLID)); # green
  115. my $y_end = Slic3r::Pointf->new($origin_px->[X], $origin_px->[Y] - $axes_len);
  116. $dc->DrawLine(@$origin_px, @$y_end);
  117. foreach my $angle (-$arrow_angle, +$arrow_angle) {
  118. my $end = $y_end->clone;
  119. $end->translate(0, +$arrow_len);
  120. $end->rotate($angle, $y_end);
  121. $dc->DrawLine(@$y_end, @$end);
  122. }
  123. }
  124. # draw origin
  125. {
  126. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  127. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(0,0,0), wxSOLID));
  128. $dc->DrawCircle(@$origin_px, 3);
  129. $dc->SetTextForeground(Wx::Colour->new(0,0,0));
  130. $dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
  131. $dc->DrawText("(0,0)", $origin_px->[X] + 1, $origin_px->[Y] + 2);
  132. }
  133. # draw current position
  134. if (defined $self->pos) {
  135. my $pos_px = $self->to_pixels($self->pos);
  136. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(200,0,0), 2, wxSOLID));
  137. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(200,0,0), wxTRANSPARENT));
  138. $dc->DrawCircle(@$pos_px, 5);
  139. $dc->DrawLine($pos_px->[X]-15, $pos_px->[Y], $pos_px->[X]+15, $pos_px->[Y]);
  140. $dc->DrawLine($pos_px->[X], $pos_px->[Y]-15, $pos_px->[X], $pos_px->[Y]+15);
  141. }
  142. $self->_painted(1);
  143. }
  144. sub _mouse_event {
  145. my ($self, $event) = @_;
  146. return if !$self->interactive;
  147. return if !$self->_painted;
  148. my $pos = $event->GetPosition;
  149. my $point = $self->to_units([ $pos->x, $pos->y ]); #]]
  150. if ($event->LeftDown || $event->Dragging) {
  151. $self->on_move->($point) if $self->on_move;
  152. $self->Refresh;
  153. }
  154. }
  155. # convert G-code coordinates into pixels
  156. sub to_pixels {
  157. my ($self, $point) = @_;
  158. my $p = Slic3r::Pointf->new(@$point);
  159. $p->scale($self->_scale_factor);
  160. $p->translate(@{$self->_shift});
  161. return [$p->x, $self->GetSize->GetHeight - $p->y]; #]]
  162. }
  163. # convert pixels into G-code coordinates
  164. sub to_units {
  165. my ($self, $point) = @_;
  166. my $p = Slic3r::Pointf->new(
  167. $point->[X],
  168. $self->GetSize->GetHeight - $point->[Y],
  169. );
  170. $p->translate(@{$self->_shift->negative});
  171. $p->scale(1/$self->_scale_factor);
  172. return $p;
  173. }
  174. sub set_pos {
  175. my ($self, $pos) = @_;
  176. $self->pos($pos);
  177. $self->Refresh;
  178. }
  179. 1;