Class: Ruby2D::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/window.rb

Defined Under Namespace

Classes: ControllerAxisEvent, ControllerButtonEvent, ControllerEvent, EventDescriptor, KeyEvent, MouseEvent

Constant Summary collapse

@@open_window =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Window

Returns a new instance of Window.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby2d/window.rb', line 17

def initialize(args = {})

  # Title of the window
  @title = args[:title] || "Ruby 2D"

  # Window background color
  @background = Color.new([0.0, 0.0, 0.0, 1.0])

  # Window icon
  @icon = nil

  # Window size and characteristics
  @width  = args[:width]  || 640
  @height = args[:height] || 480
  @resizable = false
  @borderless = false
  @fullscreen = false
  @highdpi = false

  # Size of the window's viewport (the drawable area)
  @viewport_width, @viewport_height = nil, nil

  # Size of the computer's display
  @display_width, @display_height = nil, nil

  # Total number of frames that have been rendered
  @frames = 0

  # Frames per second upper limit, and the actual FPS
  @fps_cap = args[:fps_cap] || 60
  @fps = @fps_cap

  # Vertical synchronization, set to prevent screen tearing (recommended)
  @vsync = args[:vsync] || true

  # Renderable objects currently in the window, like a linear scene graph
  @objects = []

  # Entities currently in the window
  @entities = []

  # Mouse X and Y position in the window
  @mouse_x, @mouse_y = 0, 0

  # Controller axis and button mappings file
  @controller_mappings = File.expand_path('~') + "/.ruby2d/controllers.txt"

  # Event stores for class pattern
  @keys_down = []
  @keys_held = []
  @keys_up   = []
  @mouse_buttons_down = []
  @mouse_buttons_up   = []
  @mouse_scroll_event     = false
  @mouse_scroll_direction = nil
  @mouse_scroll_delta_x   = 0
  @mouse_scroll_delta_y   = 0
  @mouse_move_event   = false
  @mouse_move_delta_x = 0
  @mouse_move_delta_y = 0
  @controller_id = nil
  @controller_axes_moved   = []
  @controller_axis_left_x  = 0
  @controller_axis_left_y  = 0
  @controller_axis_right_x = 0
  @controller_axis_right_y = 0
  @controller_buttons_down = []
  @controller_buttons_up   = []

  # Unique ID for the input event being registered
  @event_key = 0

  # Registered input events
  @events = {
    key: {},
    key_down: {},
    key_held: {},
    key_up: {},
    mouse: {},
    mouse_up: {},
    mouse_down: {},
    mouse_scroll: {},
    mouse_move: {},
    controller: {},
    controller_axis: {},
    controller_button_down: {},
    controller_button_up: {}
  }

  # The window update block
  @update_proc = Proc.new {}

  # The window render block
  @render_proc = Proc.new {}

  # Detect if window is being used through the DSL or as a class instance
  @using_dsl = !(method(:update).parameters.empty? || method(:render).parameters.empty?)

  # Whether diagnostic messages should be printed
  @diagnostics = false

  # Console mode, enabled at command line
  if RUBY_ENGINE == 'ruby'
    @console = defined?($ruby2d_console_mode) ? true : false
  else
    @console = false
  end
end

Class Method Details

.add(o) ⇒ Object



165
166
167
# File 'lib/ruby2d/window.rb', line 165

def add(o)
  DSL.window.add(o)
end

.backgroundObject



130
# File 'lib/ruby2d/window.rb', line 130

def background;      get(:background)      end

.borderlessObject



138
# File 'lib/ruby2d/window.rb', line 138

def borderless;      get(:borderless)      end

.clearObject



173
174
175
# File 'lib/ruby2d/window.rb', line 173

def clear
  DSL.window.clear
end

.closeObject



189
190
191
# File 'lib/ruby2d/window.rb', line 189

def close
  DSL.window.close
end

.currentObject



128
# File 'lib/ruby2d/window.rb', line 128

def current;         get(:window)          end

.diagnosticsObject



146
# File 'lib/ruby2d/window.rb', line 146

def diagnostics;     get(:diagnostics)     end

.display_heightObject



136
# File 'lib/ruby2d/window.rb', line 136

def display_height;  get(:display_height)  end

.display_widthObject



135
# File 'lib/ruby2d/window.rb', line 135

def display_width;   get(:display_width)   end

.fpsObject



142
# File 'lib/ruby2d/window.rb', line 142

def fps;             get(:fps)             end

.fps_capObject



143
# File 'lib/ruby2d/window.rb', line 143

def fps_cap;         get(:fps_cap)         end

.framesObject



141
# File 'lib/ruby2d/window.rb', line 141

def frames;          get(:frames)          end

.fullscreenObject



139
# File 'lib/ruby2d/window.rb', line 139

def fullscreen;      get(:fullscreen)      end

.get(sym, opts = nil) ⇒ Object



149
150
151
# File 'lib/ruby2d/window.rb', line 149

def get(sym, opts = nil)
  DSL.window.get(sym, opts)
end

.heightObject



132
# File 'lib/ruby2d/window.rb', line 132

def height;          get(:height)          end

.highdpiObject



140
# File 'lib/ruby2d/window.rb', line 140

def highdpi;         get(:highdpi)         end

.mouse_xObject



144
# File 'lib/ruby2d/window.rb', line 144

def mouse_x;         get(:mouse_x)         end

.mouse_yObject



145
# File 'lib/ruby2d/window.rb', line 145

def mouse_y;         get(:mouse_y)         end

.off(event_descriptor) ⇒ Object



161
162
163
# File 'lib/ruby2d/window.rb', line 161

def off(event_descriptor)
  DSL.window.off(event_descriptor)
end

.on(event, &proc) ⇒ Object



157
158
159
# File 'lib/ruby2d/window.rb', line 157

def on(event, &proc)
  DSL.window.on(event, &proc)
end

.remove(o) ⇒ Object



169
170
171
# File 'lib/ruby2d/window.rb', line 169

def remove(o)
  DSL.window.remove(o)
end

.render(&proc) ⇒ Object



181
182
183
# File 'lib/ruby2d/window.rb', line 181

def render(&proc)
  DSL.window.render(&proc)
end

.render_ready_checkObject



193
194
195
196
197
# File 'lib/ruby2d/window.rb', line 193

def render_ready_check
  unless @@open_window
    raise Error, "Attempting to draw before the window is ready. Please put calls to draw() inside of a render block."
  end
end

.resizableObject



137
# File 'lib/ruby2d/window.rb', line 137

def resizable;       get(:resizable)       end

.screenshot(opts = nil) ⇒ Object



147
# File 'lib/ruby2d/window.rb', line 147

def screenshot(opts = nil); get(:screenshot, opts) end

.set(opts) ⇒ Object



153
154
155
# File 'lib/ruby2d/window.rb', line 153

def set(opts)
  DSL.window.set(opts)
end

.showObject



185
186
187
# File 'lib/ruby2d/window.rb', line 185

def show
  DSL.window.show
end

.titleObject



129
# File 'lib/ruby2d/window.rb', line 129

def title;           get(:title)           end

.update(&proc) ⇒ Object



177
178
179
# File 'lib/ruby2d/window.rb', line 177

def update(&proc)
  DSL.window.update(&proc)
end

.viewport_heightObject



134
# File 'lib/ruby2d/window.rb', line 134

def viewport_height; get(:viewport_height) end

.viewport_widthObject



133
# File 'lib/ruby2d/window.rb', line 133

def viewport_width;  get(:viewport_width)  end

.widthObject



131
# File 'lib/ruby2d/window.rb', line 131

def width;           get(:width)           end

Instance Method Details

#add(o) ⇒ Object

Add an object to the window



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ruby2d/window.rb', line 257

def add(o)
  case o
  when nil
    raise Error, "Cannot add '#{o.class}' to window!"
  #when Ruby2D::Entity
    #@entities.push(o)
  when Array
    o.each { |x| add_object(x) }
  else
    add_object(o)
  end
end

#add_controller_mappingsObject

Add controller mappings from file



475
476
477
478
479
# File 'lib/ruby2d/window.rb', line 475

def add_controller_mappings
  if File.exist? @controller_mappings
    ext_add_controller_mappings(@controller_mappings)
  end
end

#clearObject

Clear all objects from the window



286
287
288
289
# File 'lib/ruby2d/window.rb', line 286

def clear
  @objects.clear
  @entities.clear
end

#closeObject

Close the window



647
648
649
# File 'lib/ruby2d/window.rb', line 647

def close
  ext_close
end

#controller_axis(axis) ⇒ Object

Controller axis event method for class pattern



482
483
484
# File 'lib/ruby2d/window.rb', line 482

def controller_axis(axis)
  @controller_axes_moved.include? axis
end

#controller_button_down(btn) ⇒ Object

Controller button down event method for class pattern



487
488
489
# File 'lib/ruby2d/window.rb', line 487

def controller_button_down(btn)
  @controller_buttons_down.include? btn
end

#controller_button_up(btn) ⇒ Object

Controller button up event method for class pattern



492
493
494
# File 'lib/ruby2d/window.rb', line 492

def controller_button_up(btn)
  @controller_buttons_up.include? btn
end

#controller_callback(which, type, axis, value, button) ⇒ Object

Controller callback method, called by the native and web extentions



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/ruby2d/window.rb', line 497

def controller_callback(which, type, axis, value, button)
  # All controller events
  @events[:controller].each do |id, e|
    e.call(ControllerEvent.new(which, type, axis, value, button))
  end

  case type
  # When controller axis motion, like analog sticks
  when :axis

    # For class pattern
    unless @using_dsl
      @controller_id = which

      unless @controller_axes_moved.include? axis
        @controller_axes_moved << axis
      end

      case axis
      when :left_x
        @controller_axis_left_x = value
      when :left_y
        @controller_axis_left_y = value
      when :right_x
        @controller_axis_right_x = value
      when :right_y
        @controller_axis_right_y = value
      end
    end

    # Call event handler
    @events[:controller_axis].each do |id, e|
      e.call(ControllerAxisEvent.new(which, axis, value))
    end
  # When controller button is pressed
  when :button_down
    # For class pattern
    unless @using_dsl
      @controller_id = which
      unless @controller_buttons_down.include? button
        @controller_buttons_down << button
      end
    end

    # Call event handler
    @events[:controller_button_down].each do |id, e|
      e.call(ControllerButtonEvent.new(which, button))
    end
  # When controller button is released
  when :button_up
    # For class pattern
    unless @using_dsl
      @controller_id = which
      unless @controller_buttons_up.include? button
        @controller_buttons_up << button
      end
    end

    # Call event handler
    @events[:controller_button_up].each do |id, e|
      e.call(ControllerButtonEvent.new(which, button))
    end
  end
end

#get(sym, opts = nil) ⇒ Object

Retrieve an attribute of the window



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruby2d/window.rb', line 203

def get(sym, opts = nil)
  case sym
  when :window;          self
  when :title;           @title
  when :background;      @background
  when :width;           @width
  when :height;          @height
  when :viewport_width;  @viewport_width
  when :viewport_height; @viewport_height
  when :display_width, :display_height
    ext_get_display_dimensions
    if sym == :display_width
      @display_width
    else
      @display_height
    end
  when :resizable;       @resizable
  when :borderless;      @borderless
  when :fullscreen;      @fullscreen
  when :highdpi;         @highdpi
  when :frames;          @frames
  when :fps;             @fps
  when :fps_cap;         @fps_cap
  when :mouse_x;         @mouse_x
  when :mouse_y;         @mouse_y
  when :diagnostics;     @diagnostics
  when :screenshot;      screenshot(opts)
  end
end

#key_callback(type, key) ⇒ Object

Key callback method, called by the native and web extentions



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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/ruby2d/window.rb', line 339

def key_callback(type, key)
  key = key.downcase

  # All key events
  @events[:key].each do |id, e|
    e.call(KeyEvent.new(type, key))
  end

  case type
  # When key is pressed, fired once
  when :down
    # For class pattern
    unless @using_dsl
      unless @keys_down.include? key
        @keys_down << key
      end
    end

    # Call event handler
    @events[:key_down].each do |id, e|
      e.call(KeyEvent.new(type, key))
    end
  # When key is being held down, fired every frame
  when :held
    # For class pattern
    unless @using_dsl
      unless @keys_held.include? key
        @keys_held << key
      end
    end

    # Call event handler
    @events[:key_held].each do |id, e|
      e.call(KeyEvent.new(type, key))
    end
  # When key released, fired once
  when :up
    # For class pattern
    unless @using_dsl
      unless @keys_up.include? key
        @keys_up << key
      end
    end

    # Call event handler
    @events[:key_up].each do |id, e|
      e.call(KeyEvent.new(type, key))
    end
  end
end

#key_down(key) ⇒ Object

Key down event method for class pattern



324
325
326
# File 'lib/ruby2d/window.rb', line 324

def key_down(key)
  @keys_down.include? key
end

#key_held(key) ⇒ Object

Key held event method for class pattern



329
330
331
# File 'lib/ruby2d/window.rb', line 329

def key_held(key)
  @keys_held.include? key
end

#key_up(key) ⇒ Object

Key up event method for class pattern



334
335
336
# File 'lib/ruby2d/window.rb', line 334

def key_up(key)
  @keys_up.include? key
end

#mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object

Mouse callback method, called by the native and web extentions



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/ruby2d/window.rb', line 411

def mouse_callback(type, button, direction, x, y, delta_x, delta_y)
  # All mouse events
  @events[:mouse].each do |id, e|
    e.call(MouseEvent.new(type, button, direction, x, y, delta_x, delta_y))
  end

  case type
  # When mouse button pressed
  when :down
    # For class pattern
    unless @using_dsl
      unless @mouse_buttons_down.include? button
        @mouse_buttons_down << button
      end
    end

    # Call event handler
    @events[:mouse_down].each do |id, e|
      e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
    end
  # When mouse button released
  when :up
    # For class pattern
    unless @using_dsl
      unless @mouse_buttons_up.include? button
        @mouse_buttons_up << button
      end
    end

    # Call event handler
    @events[:mouse_up].each do |id, e|
      e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
    end
  # When mouse motion / movement
  when :scroll
    # For class pattern
    unless @using_dsl
      @mouse_scroll_event     = true
      @mouse_scroll_direction = direction
      @mouse_scroll_delta_x   = delta_x
      @mouse_scroll_delta_y   = delta_y
    end

    # Call event handler
    @events[:mouse_scroll].each do |id, e|
      e.call(MouseEvent.new(type, nil, direction, nil, nil, delta_x, delta_y))
    end
  # When mouse scrolling, wheel or trackpad
  when :move
    # For class pattern
    unless @using_dsl
      @mouse_move_event   = true
      @mouse_move_delta_x = delta_x
      @mouse_move_delta_y = delta_y
    end

    # Call event handler
    @events[:mouse_move].each do |id, e|
      e.call(MouseEvent.new(type, nil, nil, x, y, delta_x, delta_y))
    end
  end
end

#mouse_down(btn) ⇒ Object

Mouse down event method for class pattern



391
392
393
# File 'lib/ruby2d/window.rb', line 391

def mouse_down(btn)
  @mouse_buttons_down.include? btn
end

#mouse_moveObject

Mouse move event method for class pattern



406
407
408
# File 'lib/ruby2d/window.rb', line 406

def mouse_move
  @mouse_move_event
end

#mouse_scrollObject

Mouse scroll event method for class pattern



401
402
403
# File 'lib/ruby2d/window.rb', line 401

def mouse_scroll
  @mouse_scroll_event
end

#mouse_up(btn) ⇒ Object

Mouse up event method for class pattern



396
397
398
# File 'lib/ruby2d/window.rb', line 396

def mouse_up(btn)
  @mouse_buttons_up.include? btn
end

#new_event_keyObject

Generate a new event key (ID)



304
305
306
# File 'lib/ruby2d/window.rb', line 304

def new_event_key
  @event_key = @event_key.next
end

#off(event_descriptor) ⇒ Object

Remove an event handler



319
320
321
# File 'lib/ruby2d/window.rb', line 319

def off(event_descriptor)
  @events[event_descriptor.type].delete(event_descriptor.id)
end

#on(event, &proc) ⇒ Object

Set an event handler



309
310
311
312
313
314
315
316
# File 'lib/ruby2d/window.rb', line 309

def on(event, &proc)
  unless @events.has_key? event
    raise Error, "`#{event}` is not a valid event type"
  end
  event_id = new_event_key
  @events[event][event_id] = proc
  EventDescriptor.new(event, event_id)
end

#remove(o) ⇒ Object

Remove an object from the window



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ruby2d/window.rb', line 271

def remove(o)
  if o == nil
    raise Error, "Cannot remove '#{o.class}' from window!"
  end

  collection = o.class.ancestors.include?(Ruby2D::Entity) ? @entities : @objects
  if i = collection.index(o)
    collection.delete_at(i)
    true
  else
    false
  end
end

#render(&proc) ⇒ Object

Set the render callback



298
299
300
301
# File 'lib/ruby2d/window.rb', line 298

def render(&proc)
  @render_proc = proc
  true
end

#render_callbackObject

Render callback method, called by the native and web extentions



609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/ruby2d/window.rb', line 609

def render_callback
  unless @using_dsl
    render
  end

  @render_proc.call

  # Run render method on all entities
  @entities.each do |e|
    e.render
  end
end

#screenshot(path) ⇒ Object

Take screenshot



633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/ruby2d/window.rb', line 633

def screenshot(path)
  if path
    ext_screenshot(path)
  else
    if RUBY_ENGINE == 'ruby'
      time = Time.now.utc.strftime '%Y-%m-%d--%H-%M-%S'
    else
      time = Time.now.utc.to_i
    end
    ext_screenshot("./screenshot-#{time}.png")
  end
end

#set(opts) ⇒ Object

Set a window attribute



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ruby2d/window.rb', line 234

def set(opts)
  # Store new window attributes, or ignore if nil
  @title           = opts[:title]           || @title
  if Color.is_valid? opts[:background]
    @background    = Color.new(opts[:background])
  end
  @icon            = opts[:icon]            || @icon
  @width           = opts[:width]           || @width
  @height          = opts[:height]          || @height
  @fps_cap         = opts[:fps_cap]         || @fps_cap
  @viewport_width  = opts[:viewport_width]  || @viewport_width
  @viewport_height = opts[:viewport_height] || @viewport_height
  @resizable       = opts[:resizable]       || @resizable
  @borderless      = opts[:borderless]      || @borderless
  @fullscreen      = opts[:fullscreen]      || @fullscreen
  @highdpi         = opts[:highdpi]         || @highdpi
  unless opts[:diagnostics].nil?
    @diagnostics = opts[:diagnostics]
    ext_diagnostics(@diagnostics)
  end
end

#showObject

Show the window



623
624
625
626
627
628
629
630
# File 'lib/ruby2d/window.rb', line 623

def show
  if @@open_window
    raise Error, "Window#show called multiple times, Ruby2D only supports a single open window"
  end

  @@open_window = true
  ext_show
end

#update(&proc) ⇒ Object

Set the update callback



292
293
294
295
# File 'lib/ruby2d/window.rb', line 292

def update(&proc)
  @update_proc = proc
  true
end

#update_callbackObject

Update callback method, called by the native and web extentions



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/ruby2d/window.rb', line 563

def update_callback
  unless @using_dsl
    update
  end

  @update_proc.call

  # Run update method on all entities
  @entities.each do |e|
    e.update
  end

  # Accept and eval commands if in console mode
  if @console
    if STDIN.ready?
      cmd = STDIN.gets
      begin
        res = eval(cmd, TOPLEVEL_BINDING)
        STDOUT.puts "=> #{res.inspect}"
        STDOUT.flush
      rescue SyntaxError => se
        STDOUT.puts se
        STDOUT.flush
      rescue Exception => e
        STDOUT.puts e
        STDOUT.flush
      end
    end
  end

  # Clear inputs if using class pattern
  unless @using_dsl
    @keys_down.clear
    @keys_held.clear
    @keys_up.clear
    @mouse_buttons_down.clear
    @mouse_buttons_up.clear
    @mouse_scroll_event = false
    @mouse_move_event = false
    @controller_axes_moved.clear
    @controller_buttons_down.clear
    @controller_buttons_up.clear
  end
end