Class: R2dSvg

Inherits:
Object
  • Object
show all
Includes:
R2dEngine, Ruby2D
Defined in:
lib/r2dsvg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, title: 'R2dSVG', debug: false, server: false) ⇒ R2dSvg

Returns a new instance of R2dSvg.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/r2dsvg.rb', line 38

def initialize(s, title: 'R2dSVG', debug: false, server: false)

  @debug = debug
  
  @window = window = Window.new
  @loaded = false
  @model = Svgle
  
  read(s, title)    

  if server then
    drb = OneDrb::Server.new(host: '127.0.0.1', port: '57844', obj: self)
    Thread.new { drb.start }
  end
  
  if @loaded then
    
    sleep 0.05
    
    window.on(:mouse_move) do |event|
      mouse :mousemove, event
      mouse :mouseenter, event
    end
    
    window.on(:mouse_down) do |event|
      
      if event.button == :left then
        
        # click and mousedown do the same thing
        mouse :click, event 
        mouse :mousedown, event
      end
      
    end          
  
    window.on :key_down do |event|
      # A key was pressed
      keyboard :keydown, event
    end
    
  end
=begin     
  window.on :key_held do |event|
    # A key is being held down
    puts event.key
    keyboard :onkeyheld, event
  end
  
  window.on :key_up do |event|
    # A key was released
    puts event.key
    keyboard :onkeyup, event
  end    
=end
  window.show
  
  
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



36
37
38
# File 'lib/r2dsvg.rb', line 36

def doc
  @doc
end

Instance Method Details

#clearObject



97
98
99
# File 'lib/r2dsvg.rb', line 97

def clear()
  @window.clear
end

#read(unknown, title = @title) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/r2dsvg.rb', line 101

def read(unknown, title=@title)

  @loaded = false
  @window.clear
  doc = nil
  
  if unknown.is_a? String
    
    svg, _ = RXFHelper.read(unknown)    
    doc = @model.new(svg, callback: self, debug: @debug)
    instructions = Render.new(doc, debug: @debug).to_a      

  elsif unknown.is_a? Array
    
    puts 'array found' if @debug
    
    instructions = unknown
    
  end
 
  drawing = DrawingInstructions.new @window, debug: @debug

  if doc then    
    
    @width, @height = %i(width height).map{|x| doc.root.attributes[x].to_i }
    @window.set title: title, width: @width, height: @height        
  

    threads = []
    
    threads << Thread.new do
      doc.root.xpath('//script').each {|x| eval x.texts.join }      
      drawing.render instructions   
    end
    
    threads.join

    @loaded = true
    @doc = doc
    
  else
    
    drawing.render instructions   
    h = instructions[2]
    @width, @height = h[:width].to_i, h[:height].to_i
    @window.set title: 'Untitled', width: @width, height: @height    
  end
  
end

#refreshObject



151
152
153
# File 'lib/r2dsvg.rb', line 151

def refresh()
  puts 'do nothing' if @debug
end