2DBed.pm 7.9 KB

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