Class: Presentation

Inherits:
Object
  • Object
show all
Includes:
Soby
Defined in:
lib/soby/presentation.rb

Defined Under Namespace

Classes: MyVideo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Soby

#create_mat4x4, #get_angle, #get_global_transform, #read_transform

Constructor Details

#initialize(app, url) ⇒ Presentation



16
17
18
19
20
21
22
23
24
25
# File 'lib/soby/presentation.rb', line 16

def initialize (app, url)
  @app = app
  xml = app.loadXML(url)
  @pshape = PShapeSVG.new(xml)
  @svg = Nokogiri::XML(open(url)).children[1];

  @graphics = @app.g
  build_internal

end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



12
13
14
# File 'lib/soby/presentation.rb', line 12

def debug
  @debug
end

#geometryObject

Returns the value of attribute geometry.



10
11
12
# File 'lib/soby/presentation.rb', line 10

def geometry
  @geometry
end

#graphicsObject

Returns the value of attribute graphics.



14
15
16
# File 'lib/soby/presentation.rb', line 14

def graphics
  @graphics
end

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/soby/presentation.rb', line 11

def height
  @height
end

#matrixObject

Returns the value of attribute matrix.



11
12
13
# File 'lib/soby/presentation.rb', line 11

def matrix
  @matrix
end

#nb_slidesObject (readonly)

Returns the value of attribute nb_slides.



12
13
14
# File 'lib/soby/presentation.rb', line 12

def nb_slides
  @nb_slides
end

#pshapeObject

Returns the value of attribute pshape.



10
11
12
# File 'lib/soby/presentation.rb', line 10

def pshape
  @pshape
end

#slidesObject

Returns the value of attribute slides.



10
11
12
# File 'lib/soby/presentation.rb', line 10

def slides
  @slides
end

#svgObject

Returns the value of attribute svg.



10
11
12
# File 'lib/soby/presentation.rb', line 10

def svg
  @svg
end

#transformObject

Returns the value of attribute transform.



10
11
12
# File 'lib/soby/presentation.rb', line 10

def transform
  @transform
end

#videosObject

Returns the value of attribute videos.



11
12
13
# File 'lib/soby/presentation.rb', line 11

def videos
  @videos
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/soby/presentation.rb', line 11

def width
  @width
end

Instance Method Details

#add_group(group, slide) ⇒ Object



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/soby/presentation.rb', line 112

def add_group(group, slide) 
  
  id = slide.title
  refid = slide.refid
  add_slide(slide) 

  # TODO: Find the bounding box of any  group..
  # TODO: Hide the whole group ?
  surf_max = 0
  biggest_rect = nil
  
  ## biggest_rect surface..
  group.css("rect").each do |rect|
    #          (group.css("rect") + group.css("image")).each do |rect|
    
    if biggest_rect == nil 
      biggest_rect = rect
    end
    
    surf_rect = rect.attributes["width"].value.to_i \
    * rect.attributes["height"].value.to_i

    if(surf_rect > surf_max) 
      biggest_rect = rect 
      surf_max = surf_rect
    end
  end  # rect.each 
  
  rect_id = biggest_rect.attributes["id"].value
  transform = get_global_transform biggest_rect
  @slides[id].set_geometry(biggest_rect, transform[0])     

  # The description (code to excute) might be in the group  and not in the rect. 
  ## TODO: check if it should really be the first ?
  desc = group.css("desc").first
  title = group.css("title").first

  if desc != nil 
    if title == nil || (title.text.match(/animation/) == nil and title.text.match(/video/) == nil)
      puts "Group Description read #{desc.text}" 
      
      @slides[id].description =  desc.text
    end
  end
  
  e = @pshape.getChild(rect_id)
  # hide the rect
  if e == nil 
    puts "Error: rect ID:  #{rect_id} not found "
  else
    @pshape.getChild(rect_id).setVisible(!@slides[id].hide)
  end 

  puts "Slide #{id} created from a group, and the rectangle: #{rect_id}"

end

#add_rect_or_image_frame(element, slide) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/soby/presentation.rb', line 89

def add_rect_or_image_frame(element, slide)
  id = slide.title
  refid = slide.refid

  add_slide(slide) 

  # set the geometry &  transformation
  transform = get_global_transform element
  @slides[id].set_geometry(element, transform[0])        
  pshape_element = @pshape.getChild(refid)

  # hide the element
  if pshape_element == nil 
    puts "Error: rect or Image  ID:  #{refid} not found. CHECK THE PROCESSING VERSION."
  else
    pshape_element.setVisible(!@slides[id].hide) if element.name == "rect"
  end

  puts "Slide #{id} created from a rect : #{refid}" if element.name == "rect"
  puts "Slide #{id} created from an image: #{refid}" if element.name == "image"
end

#add_slide(new_slide) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/soby/presentation.rb', line 80

def add_slide(new_slide)

  puts "Add slide, id " << new_slide.title << " numero " << new_slide.sequence
  @slides[new_slide.sequence] = new_slide
  @slides[new_slide.title] = new_slide
  @nb_slides = @nb_slides+1
end

#build_internalObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/soby/presentation.rb', line 31

def build_internal 

  # Create the frames.. 
  @slides = {}
  @nb_slides = 0
  @playing_videos = []

  puts "Svg null, look for the error... !"  if @svg == nil
  return if @svg == nil 

  load_frames
  load_videos
  load_animations
end

#display_videosObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/soby/presentation.rb', line 255

def display_videos
  if not @app.is_moving 

    slide_no = @app.current_slide_no

    # # Display the videos
    if slide_no > 0 and  @slides[slide_no].has_videos? 
      

      # draw the object
      @graphics.imageMode(Processing::App::CORNER)
      
      @slides[slide_no].videos.each do |my_video| 
        @graphics.push_matrix
        @graphics.modelview.apply(my_video.matrix)
        
        # when the video is loaded it is saved... so that the memory can
        # hopefully be freed
        @playing_videos << my_video if my_video.play 

#          my_video.video.read if my_video.video.available?           
#          my_video.video.read
        @graphics.image(my_video.video, 0, 0, my_video.width, my_video.height)

        
        @graphics.pop_matrix
      end # videos.each
      
    else  # has_videos

      ## no video, free some memory 
      @playing_videos.each do |my_video|
        puts "Saving memory. "
        my_video.video.stop
        my_video.video = nil 
        System.gc
      end
      @playing_videos =  []
    end 
  end
end

#drawObject



46
47
48
49
# File 'lib/soby/presentation.rb', line 46

def draw
  @graphics.shape pshape, 0, 0
#    display_videos
end

#load_animationsObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/soby/presentation.rb', line 213

def load_animations

  puts "Loading the animations..."

  @svg.css("*").each do |elem|

    valid = false
    elem.children.each do |child|
      valid = true if child.name == "title" && child.text.match(/animation/)
    end

    next unless valid

    id = elem.attributes["id"].value

    # Get the animation information 
    t = elem.css("desc").text.split("\n")

    # 1. which slide
    # 2. when 
    slide_id = t[0]
    anim_id = t[1].to_i

    animation = OpenStruct.new
    animation.pshape_elem = @pshape.getChild(id)

    puts "Animation found on slide #{slide_id} with element #{animation.pshape_elem}"

    if  @slides[slide_id] == nil 
      puts "Error -> The animation #{id}  is linked to the slide #{slide_id} which is not found !" 
    else 
      # add the animation
      @slides[slide_id].add_animation(anim_id, animation)
      
      # hide the group !
      animation.pshape_elem.setVisible(false) unless animation.pshape_elem == nil
      puts "Animation #{id} loaded with  #{slide_id}"
    end
  end

end

#load_framesObject



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
# File 'lib/soby/presentation.rb', line 52

def load_frames

  puts "Loading the frames..."
  @svg.children.each do |child| 
    
    next unless child.name =~ /frame/

    ## ignore if it references nothing. 
    attr = child.attributes
    next if attr["refid"] == nil

    slide = Slide.new(child)      
    
    # get the element associated with each slide...
    @svg.search("[id=" + slide.refid + "]").each do |elem|

      case elem.name 
      when "rect", "image"
        add_rect_or_image_frame(elem, slide)
      when "g" 
        add_group(elem, slide)
      else 
        puts "Slide type not supported ! "  + elem.name
      end # end case
    end # search
  end # frame
end

#load_videosObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/soby/presentation.rb', line 172

def load_videos
  # Load the videos...
  puts "Loading the videos..."

  @svg.css("rect").each do |rect|

    return if rect.attributes["id"] == nil
    id = rect.attributes["id"].value       #get the id 

    title = rect.css("title")
    next if title == nil 

    is_video = title.text.match(/video/) != nil 
    next unless is_video
    
    t = rect.css("desc").text.split("\n")
    slide_id = t[0]
    path = t[1]

    puts ("Loading the video : " + path)

    # Get the transformation
    tr = get_global_transform rect 

    video = MyVideo.new(path, @slides[slide_id], tr[0], tr[1], tr[2]) 

    if  @slides[slide_id] == nil 
      puts "Error -> The video #{id}  is linked to the slide #{slide_id} which is not found !" 
    else 

      @slides[slide_id].add_video(video)
      puts "Video #{id} loaded with  #{slide_id}"
    end

    # Hide the rect, and ... TODO replace it with a special object
    #      @pshape.getChild(id).setVisible(false)
  end
end

#to_sObject



338
339
340
# File 'lib/soby/presentation.rb', line 338

def to_s 
  "Slides #{@slides.size}" 
end