Class: SobyPlayer

Inherits:
Processing::App
  • Object
show all
Defined in:
lib/soby.rb

Constant Summary collapse

TRANSITION_DURATION =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screen_id) ⇒ SobyPlayer

Returns a new instance of SobyPlayer.



42
43
44
45
# File 'lib/soby.rb', line 42

def initialize(screen_id)
  @screen_id = screen_id
  super()
end

Instance Attribute Details

#camObject

attr_accessor :background_max_color, :background_min_color, :background_constrain



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

def cam
  @cam
end

#current_slide_noObject (readonly)

Returns the value of attribute current_slide_no.



31
32
33
# File 'lib/soby.rb', line 31

def current_slide_no
  @current_slide_no
end

#has_threadObject (readonly)

Returns the value of attribute has_thread.



33
34
35
# File 'lib/soby.rb', line 33

def has_thread
  @has_thread
end

#is_movingObject (readonly)

Returns the value of attribute is_moving.



31
32
33
# File 'lib/soby.rb', line 31

def is_moving
  @is_moving
end

#next_camObject

Returns the value of attribute next_cam.



30
31
32
# File 'lib/soby.rb', line 30

def next_cam
  @next_cam
end

#prev_camObject

Returns the value of attribute prev_cam.



30
31
32
# File 'lib/soby.rb', line 30

def prev_cam
  @prev_cam
end

#prezObject

Returns the value of attribute prez.



30
31
32
# File 'lib/soby.rb', line 30

def prez
  @prez
end

#slidesObject

Returns the value of attribute slides.



30
31
32
# File 'lib/soby.rb', line 30

def slides
  @slides
end

#threadObject

Returns the value of attribute thread.



34
35
36
# File 'lib/soby.rb', line 34

def thread
  @thread
end

Instance Method Details

#compute_view(view, slide_number) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/soby.rb', line 269

def compute_view(view, slide_number)
  #    view = createGraphics(@width, @height, P3D)
  #    @next_view = createGraphics(@width, @height)
  view.beginDraw

  cam = slide_view slide_number
  view.g.modelview.apply(cam)
  view.shapeMode(CORNER)
  view.shape(@prez.pshape, 0, 0)
  view.endDraw

  view
end

#custom_post_drawObject



103
104
105
# File 'lib/soby.rb', line 103

def custom_post_draw

end

#custom_pre_drawObject



99
100
101
# File 'lib/soby.rb', line 99

def custom_pre_draw
  background(255)
end

#custom_setupObject

To be overriden by the Presentation Code.



95
96
97
# File 'lib/soby.rb', line 95

def custom_setup

end

#display_slide_numberObject Also known as: default_display_slide_number



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/soby.rb', line 148

def display_slide_number
  # Slide number
  push_matrix
  translate(@width - 40, @height - 45)
  fill(30)
  strokeWeight(3)
  stroke(190)
  ellipseMode(CENTER)
  ellipse(18, 22, 35, 35)
  fill(255)
  noStroke
  textSize(20)
  text(@current_slide_no.to_s, 10, 30)
  pop_matrix
end

#drawObject



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

def draw
  rect 0, 0, 100, millis / 1000
  if not @custom_setup_done
    custom_setup
    @custom_setup_done = true
  end

  custom_pre_draw

  shapeMode(CORNER)
  imageMode(CORNER)

  if(running?)

    push_matrix

    update_cam
    self.g.modelview.apply(@cam)
    @prez.draw
    @prez.display_videos
    pop_matrix

    run_slide_code
    display_slide_number
  end

  custom_post_draw
end

#fileSelected(selection) ⇒ Object



223
224
# File 'lib/soby.rb', line 223

def fileSelected selection
end

#global_viewObject



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/soby.rb', line 437

def global_view
  # ortho(left, right, bottom, top)
  # frustum(left, right, bottom, top, near, far)

  my_scale = find_scale

  # centering
  dx = ((@width / my_scale) - @prez.width) * 0.5
  dy = ((@height / my_scale) - @prez.height) * 0.5

  cam = Cam.new
  cam.scale = my_scale
  # Not necessary : display from the center...
  cam.post_translation.set(dx, dy)
  cam.compute_mat
  cam
end

#goto_slide(next_slide) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/soby.rb', line 331

def goto_slide (next_slide)

  current_slide = @current_slide_no

  use_global_view =  next_slide == 0 || next_slide > @prez.slides.size

  cam = global_view if use_global_view
  cam = slide_view(next_slide)  if next_slide > 0

  # previous next is now old.
  @prev_cam = @next_cam
  @prev_cam.mat.set(@cam)
  @next_cam = cam

  @current_slide_no = next_slide

  if use_global_view
    @transition_duration = TRANSITION_DURATION.to_f
  else
    @transition_duration = @slides[@current_slide_no].transition_duration_ms
  end

  @transition_start_time = millis
  @is_moving = true
  @current_ratio = 0

  ## trigger the slide_change function (user defined)
  slide_change
end

#initObject

no Border



49
50
51
52
# File 'lib/soby.rb', line 49

def init
  super
  getSurface.getNative.setUndecorated true
end

#init_camerasObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/soby.rb', line 82

def init_cameras
  # current camera matrix
  @cam = PMatrix3D.new

  # Cameras for movement.
  @prev_cam = Cam.new
  @next_cam = Cam.new
  @is_moving = false
  @current_ratio = 0
end

#init_playerObject



75
76
77
78
79
80
# File 'lib/soby.rb', line 75

def init_player
  @prez = nil
  @current_slide_no = 0
  @is_running = false
  init_cameras
end

#key_pressedObject



171
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
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/soby.rb', line 171

def key_pressed

  if key == 'g'
    puts "Garbage"
    Java::JavaLang::System.gc
  end

  return if @prez == nil

  if keyCode == LEFT
    prev_slide
  end

  if keyCode == RIGHT
    next_slide
  end

  if key == 'l'
    # selectInput("Select a file to process:",
    #             "fileSelected",
    #             Java::JavaIo::File.new(SKETCH_ROOT))

    folder = Java::JavaIo::File.new(SKETCH_ROOT)
    fc = Java::javax::swing::JFileChooser.new("Soby Loader")
    fc.set_dialog_title "Select your presentation"
    fc.setFileFilter AppFilter.new
    fc.setCurrentDirectory folder
    success = fc.show_open_dialog(nil)
    if success == Java::javax::swing::JFileChooser::APPROVE_OPTION
      path = fc.get_selected_file.get_absolute_path
      puts "User selected " + path
      presentation = Soby::load_presentation path
      Soby::start_presentation presentation
    else
      puts "No file"
    end
  end

  if key == 'a'
    return if @has_thread
    puts "Thread starting"
    Soby::auto_update self
    @has_thread = true
  end

  if key == 's'
    Thread::kill @thread if @has_thread
  end
  #    puts "slide #{@current_slide_no} "
end

#mouse_draggedObject



228
229
230
231
232
233
234
235
236
# File 'lib/soby.rb', line 228

def mouse_dragged
  if not @is_moving
    tr = PMatrix3D.new
    tr.translate(mouse_x - pmouse_x, mouse_y - pmouse_y)

    @cam.preApply(tr)
    @next_cam.mat.preApply(tr)
  end
end

#mouseWheel(event) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/soby.rb', line 238

def mouseWheel(event)
  e = event.getAmount()
  if not @is_moving
    tr = PMatrix3D.new
    tr.translate(mouse_x, mouse_y)
    tr.scale(e < 0 ?  1.05 :  1 / 1.05)
    tr.translate(-mouse_x, -mouse_y)
    @cam.preApply(tr)
  end
end

#next_slideObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/soby.rb', line 284

def next_slide
  is_last_slide = @current_slide_no >= @prez.nb_slides
  is_slide_zero = current_slide_no == 0

  # Gloal view
  if is_slide_zero
    goto_slide(@current_slide_no + 1)
    return
  end

  if @slides[@current_slide_no] == nil
    p "ERROR invalid slide"
    p "Try to go to next slide"
    goto_slide(@current_slide_no + 1) unless is_last_slide
    return
  end

  # animation
  if @slides[@current_slide_no].has_next_animation?
    puts "Animation Next "
    anim = @slides[@current_slide_no].next_animation
    anim.pshape_elem.setVisible(true)
    return
  end

  goto_slide(@current_slide_no + 1) unless is_last_slide

end

#presentation_pathObject



164
165
166
# File 'lib/soby.rb', line 164

def presentation_path
  File.dirname(@prez.url)
end

#prev_slideObject



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/soby.rb', line 313

def prev_slide
  return if @current_slide_no <= 0

  if current_slide_no == 0
    goto_slide(@current_slide_no - 1)
    return
  end

  if @slides[@current_slide_no].has_previous_animation?
    puts "Animation Previous "
    anim = @slides[@current_slide_no].previous_animation
    anim.pshape_elem.setVisible(false)
  else
    goto_slide(@current_slide_no - 1)
  end

end

#ready?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/soby.rb', line 71

def ready?
  @ready
end

#run_slide_codeObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/soby.rb', line 136

def run_slide_code
  translate 0, 0, 1
  if not @is_moving and @current_slide_no != 0
    desc = @prez.slides[@current_slide_no].description
    if(desc != nil)
      #          puts "EVAL #{desc}"
      eval desc
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


38
# File 'lib/soby.rb', line 38

def running? () @is_running  end

#set_prez(prez) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/soby.rb', line 250

def set_prez (prez)
  current_slide = @current_slide_no

  #    PShape.loadedImages.clear
  @prez = prez
  @slides = prez.slides

  goto_slide current_slide

  @is_running = true
  @prez_middle = PVector.new(@prez.width / 2.0, @prez.height / 2.0)

  puts "Presentation size "
  puts @prez.width
  puts @prez.height

  @custom_setup_done = false
end

#settingsObject



54
55
56
57
# File 'lib/soby.rb', line 54

def settings
  # todo: fullscreen ?
  fullScreen P3D, @screen_id
end

#setupObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/soby.rb', line 59

def setup
  @ready = false

  @width = self.width
  @height = self.height
  init_player

  @custom_setup_done = true
  @ready = true
  @has_thread = false
end

#slide_changeObject



361
362
363
# File 'lib/soby.rb', line 361

def slide_change

end

#slide_view(slide_no) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/soby.rb', line 390

def slide_view (slide_no)

  if slide_no > @prez.slides.size
    puts "No more slides"
    return
  end

  return if @prez.slides[slide_no] == nil

  puts "slide view..." << slide_no.to_s

  # check slide number
  w = @prez.slides[slide_no].width
  h = @prez.slides[slide_no].height
  x = @prez.slides[slide_no].x
  y = @prez.slides[slide_no].y

  sc1 = @width.to_f / w.to_f
  sc2 = @height.to_f / h.to_f

  # scale
  sc = [sc1, sc2].min


  # translate
  dx = ((@width / sc) - w) * 0.5
  dy = ((@height / sc) - h) * 0.5

  cam = Cam.new

  m = PMatrix3D.new
  m.apply @prez.slides[slide_no].matrix
  m.invert

  # Scale
  tr = PMatrix3D.new
  tr.scale(sc)
  m.preApply(tr)
  m.translate(dx, dy)

  # center
  cam.mat = m
  cam

end

#update_camObject



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/soby.rb', line 366

def update_cam

  return unless @is_moving
  return if @slides.size == 0

  elapsed_time = millis - @transition_start_time
  @current_time_ratio = elapsed_time.to_f / @transition_duration.to_f

  @current_ratio = @slides[@current_slide_no].transition @current_time_ratio unless @current_slide_no == 0

  if (@current_ratio > 1 || @current_time_ratio > 1) && @is_moving
    @is_moving = false
    @cam = @prev_cam.lerp(@next_cam, 1)

    # save a copy
    @prev_cam.mat = @cam.get
    return
  end

  #    puts @current_ratio
  @cam = @prev_cam.lerp(@next_cam, @current_ratio)

end