Class: Shoes::Swt::Common::Painter

Inherits:
Object
  • Object
show all
Includes:
Resource, Swt::Events::PaintListener
Defined in:
shoes-swt/lib/shoes/swt/common/painter.rb

Constant Summary collapse

LINECAP =
{
  curve:   ::Swt::SWT::CAP_ROUND,
  rect:    ::Swt::SWT::CAP_FLAT,
  project: ::Swt::SWT::CAP_SQUARE
}.freeze

Constants included from Resource

Resource::OPAQUE

Instance Method Summary collapse

Methods included from Resource

#clip_context_to, #dispose_previous_contexts, #reset_graphics_context, #set_defaults_on_context, #track_graphics_context

Constructor Details

#initialize(obj) ⇒ Painter

Returns a new instance of Painter.



16
17
18
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 16

def initialize(obj)
  @obj = obj
end

Instance Method Details

#after_paintedObject



122
123
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 122

def after_painted
end

#before_paintedObject



119
120
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 119

def before_painted
end

#draw(_graphics_context) ⇒ Object

Implement in subclass



78
79
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 78

def draw(_graphics_context)
end

#draw_setup(graphics_context) ⇒ Object

Override in subclass and return something falsy if not using draw



73
74
75
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 73

def draw_setup(graphics_context)
  @obj.apply_stroke(graphics_context)
end

#drawing_bottomObject



114
115
116
117
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 114

def drawing_bottom
  dsl = @obj.dsl
  dsl.element_bottom - dsl.parent.scroll_top
end

#drawing_topObject



109
110
111
112
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 109

def drawing_top
  dsl = @obj.dsl
  dsl.element_top - dsl.parent.scroll_top
end

#fill(_graphics_context) ⇒ Object

Implement in subclass



69
70
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 69

def fill(_graphics_context)
end

#fill_and_draw(graphics_context) ⇒ Object



58
59
60
61
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 58

def fill_and_draw(graphics_context)
  fill graphics_context if fill_setup(graphics_context)
  draw graphics_context if draw_setup(graphics_context)
end

#fill_setup(graphics_context) ⇒ Object

Override in subclass and return something falsy if not using fill



64
65
66
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 64

def fill_setup(graphics_context)
  @obj.apply_fill(graphics_context)
end

#paint_control(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 20

def paint_control(event)
  before_painted
  graphics_context = event.gc
  reset_graphics_context graphics_context
  if @obj.dsl.visible? && @obj.dsl.positioned?
    paint_object graphics_context
  end
rescue => e
  raise e if ENV['SHOES_ENV'] == 'test'

  # Really important to rescue here. Failures that escape this method
  # cause odd-ball hangs with no backtraces. See #559 for an example.
  #
  puts "SWALLOWED PAINT EXCEPTION ON #{@obj} - go take care of it: " + e.to_s
  puts 'Unfortunately we have to swallow it because it causes odd failures :('
ensure
  after_painted
end

#paint_object(graphics_context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 39

def paint_object(graphics_context)
  cap = LINECAP[@obj.dsl.style[:cap]]
  graphics_context.set_line_cap(cap) if cap
  graphics_context.set_transform(@obj.transform)

  obj = @obj.dsl
  clip_context_to(graphics_context, obj, obj.parent.fixed_height?) do
    if obj.needs_rotate?
      set_rotate graphics_context, obj.rotate,
                 obj.element_left + obj.element_width / 2.0,
                 drawing_top + obj.element_height / 2.0 do
        fill_and_draw(graphics_context)
      end
    else
      fill_and_draw(graphics_context)
    end
  end
end

#reset_rotate(transform, graphics_context, angle, left, top) ⇒ Object



102
103
104
105
106
107
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 102

def reset_rotate(transform, graphics_context, angle, left, top)
  transform.translate left, top
  transform.rotate angle
  transform.translate(-left, -top)
  graphics_context.set_transform transform
end

#set_rotate(graphics_context, angle, left, top) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'shoes-swt/lib/shoes/swt/common/painter.rb', line 81

def set_rotate(graphics_context, angle, left, top)
  angle = angle.to_i
  left = left.to_i
  top = top.to_i
  return unless block_given?

  begin
    transform = ::Swt::Transform.new Shoes.display

    # Why the negative angle? Older shoes rotated from the middle
    # right running counter clockwise (as seen on this web page:
    # https://www.mathsisfun.com/geometry/degrees.html). To get the
    # same effect, we have to negate the value passed in.
    reset_rotate transform, graphics_context, -angle, left, top
    yield
    reset_rotate transform, graphics_context, angle, left, top
  ensure
    transform.dispose unless transform.nil? || transform.disposed?
  end
end