Module: Glimmer::LibUI::Transformable

Included in:
ControlProxy::AreaProxy, ControlProxy::ImageProxy, ControlProxy::PathProxy, ControlProxy::TextProxy
Defined in:
lib/glimmer/libui/control_proxy/transformable.rb

Overview

This is meant to be prepended (not included) because it changes behavior of instance draw method Adds transform property to controls/shapes Automatically applies transform property at the beginning of draw operation and undoes it at the end Stacks up with Parent module (must include Parent beforehand) Expects transformable to implement redraw method

Instance Method Summary collapse

Instance Method Details

#apply_transform(area_draw_params) ⇒ Object

Apply transform matrix to coordinate system



59
60
61
# File 'lib/glimmer/libui/control_proxy/transformable.rb', line 59

def apply_transform(area_draw_params)
  ::LibUI.draw_transform(area_draw_params[:context], @transform.libui) unless @transform.nil?
end

#draw(area_draw_params) ⇒ Object



72
73
74
75
76
# File 'lib/glimmer/libui/control_proxy/transformable.rb', line 72

def draw(area_draw_params)
  apply_transform(area_draw_params)
  super(area_draw_params)
  undo_transform(area_draw_params)
end

#post_initialize_child(child, add_child: true) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/glimmer/libui/control_proxy/transformable.rb', line 32

def post_initialize_child(child, add_child: true)
  if child.is_a?(ControlProxy::MatrixProxy)
    super(child, add_child: false)
    self.transform = child if child.keyword == 'transform'
  else
    super(child, add_child: add_child)
  end
end

#transform(matrix = nil, &transform_body_block) ⇒ Object Also known as: transform=, set_transform

Returns transform or sets it. Expects transformable to implement redraw method (delegating work to area).



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/glimmer/libui/control_proxy/transformable.rb', line 42

def transform(matrix = nil, &transform_body_block)
  if matrix.nil?
    if transform_body_block
      # TODO Consider using alternate version of Engine call instead: Glimmer::DSL::Engine.interpret('transform', &transform_body_block) (or delete this comment if not needed)
      Glimmer::DSL::Engine.interpret_expression(Glimmer::DSL::Libui::ControlExpression.new, 'transform', &transform_body_block)
    else
      @transform
    end
  else
    @transform = matrix
    redraw
  end
end

#undo_transform(area_draw_params) ⇒ Object

Inverse of apply_transform (applies inverse transformation to undo initial transformation)



64
65
66
67
68
69
70
# File 'lib/glimmer/libui/control_proxy/transformable.rb', line 64

def undo_transform(area_draw_params)
  unless @transform.nil?
    inverse_transform = @transform.clone
    inverse_transform.invert
    ::LibUI.draw_transform(area_draw_params[:context], inverse_transform.libui)
  end
end