Class: Shoes::Swt::ArcPainter

Inherits:
Common::Painter
  • Object
show all
Defined in:
shoes-swt/lib/shoes/swt/arc_painter.rb

Constant Summary collapse

FULL_CIRCLE_DEGREES =
360

Instance Method Summary collapse

Instance Method Details

#draw(graphics_context) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
# File 'shoes-swt/lib/shoes/swt/arc_painter.rb', line 26

def draw(graphics_context)
  line_width = graphics_context.get_line_width

  return unless @obj.element_left && @obj.element_top && @obj.element_width &&
                @obj.element_height

  graphics_context.draw_arc(@obj.translate_left + @obj.element_left + line_width / 2,
                            @obj.translate_top + drawing_top + line_width / 2,
                            @obj.element_width - line_width,
                            @obj.element_height - line_width,
                            start_angle, sweep)
end

#fill(graphics_context) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'shoes-swt/lib/shoes/swt/arc_painter.rb', line 8

def fill(graphics_context)
  if @obj.wedge?
    graphics_context.fill_arc(@obj.translate_left + @obj.element_left,
                              @obj.translate_top + drawing_top,
                              @obj.element_width,
                              @obj.element_height,
                              start_angle, sweep)
  else
    path = ::Swt::Path.new(::Swt.display)
    path.add_arc(@obj.translate_left + @obj.element_left,
                 @obj.translate_top + drawing_top,
                 @obj.element_width,
                 @obj.element_height,
                 start_angle, sweep)
    graphics_context.fill_path(path)
  end
end

#start_angleObject

Shoes angles to run clockwise, with 0 at the 3 o'clock position. SWT instead runs counter-clockwise from 3 o'clock, hence we negate the start angle to translate from Shoes -> SWT


42
43
44
# File 'shoes-swt/lib/shoes/swt/arc_painter.rb', line 42

def start_angle
  -@obj.angle1
end

#sweepObject

Shoes represents via a start and end angle, with the fill running clockwise from start to end. If start is greater than end, this just means that we wrap around the 3 o'clock-0 radians point as we run clockwise.

SWT instead is start angle plus a sweep, and runs counter-clockwise. Hence we end up calculating a negative (clockwise) sweep. There's a small case difference depending on if we're crossing over the 0 point.

See the specs for lots of good examples, with pictures, of this logic.


57
58
59
60
61
62
63
# File 'shoes-swt/lib/shoes/swt/arc_painter.rb', line 57

def sweep
  if @obj.angle1 <= @obj.angle2
    @obj.angle1 - @obj.angle2
  else
    @obj.angle1 - @obj.angle2 - FULL_CIRCLE_DEGREES
  end
end