Class: Ruby2D::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/window.rb,
ext/ruby2d/ruby2d-opal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Window

Returns a new instance of Window.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby2d/window.rb', line 9

def initialize(args = {})
  @title      = args[:title]  || "Ruby 2D"
  @background = Color.new([0.0, 0.0, 0.0, 1.0])
  @width      = args[:width]  || 640
  @height     = args[:height] || 480
  @viewport_width, @viewport_height = nil, nil
  @resizable  = false
  @borderless = false
  @fullscreen = false
  @highdpi    = false
  @frames     = 0
  @fps_cap    = args[:fps] || 60
  @fps        = @fps_cap
  @vsync      = args[:vsync] || true
  @mouse_x = 0; @mouse_y = 0
  @objects    = []
  @keys_down, @keys, @keys_up, @mouse, @controller = {}, {}, {}, {}, {}
  @on_key_proc        = Proc.new {}
  @on_controller_proc = Proc.new {}
  @update_proc        = Proc.new {}
  @diagnostics        = false
end

Instance Attribute Details

#fpsObject

Returns the value of attribute fps.



7
8
9
# File 'lib/ruby2d/window.rb', line 7

def fps
  @fps
end

#framesObject

Returns the value of attribute frames.



7
8
9
# File 'lib/ruby2d/window.rb', line 7

def frames
  @frames
end

#mouse_xObject

Returns the value of attribute mouse_x.



7
8
9
# File 'lib/ruby2d/window.rb', line 7

def mouse_x
  @mouse_x
end

#mouse_yObject

Returns the value of attribute mouse_y.



7
8
9
# File 'lib/ruby2d/window.rb', line 7

def mouse_y
  @mouse_y
end

#objectsObject (readonly)

Returns the value of attribute objects.



6
7
8
# File 'lib/ruby2d/window.rb', line 6

def objects
  @objects
end

Instance Method Details

#add(o) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby2d/window.rb', line 70

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

#clearObject



94
95
96
# File 'lib/ruby2d/window.rb', line 94

def clear
  @objects.clear
end

#controller_callback(which, is_axis, axis, val, is_btn, btn, pressed) ⇒ Object



179
180
181
# File 'lib/ruby2d/window.rb', line 179

def controller_callback(which, is_axis, axis, val, is_btn, btn, pressed)
  @on_controller_proc.call(which, is_axis, axis, val, is_btn, btn, pressed)
end

#get(sym) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby2d/window.rb', line 32

def get(sym)
  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 :resizable;       @resizable
  when :borderless;      @borderless
  when :fullscreen;      @fullscreen
  when :highdpi;         @highdpi
  when :frames;          @frames
  when :fps;             @fps
  when :mouse_x;         @mouse_x
  when :mouse_y;         @mouse_y
  when :diagnostics;     @diagnostics
  end
end

#key_callback(key) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/ruby2d/window.rb', line 153

def key_callback(key)
  key = key.downcase
  @on_key_proc.call(key)
  if @keys.has_key? 'any'
    @keys['any'].call
  end
  if @keys.has_key? key
    @keys[key].call
  end
end

#key_down_callback(key) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/ruby2d/window.rb', line 143

def key_down_callback(key)
  key = key.downcase
  if @keys_down.has_key? 'any'
    @keys_down['any'].call
  end
  if @keys_down.has_key? key
    @keys_down[key].call
  end
end

#key_up_callback(key) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/ruby2d/window.rb', line 164

def key_up_callback(key)
  key = key.downcase
  if @keys_up.has_key? 'any'
    @keys_up['any'].call
  end
  if @keys_up.has_key? key
    @keys_up[key].call
  end
end

#mouse_callback(btn, x, y) ⇒ Object



132
133
134
135
136
# File 'lib/ruby2d/window.rb', line 132

def mouse_callback(btn, x, y)
  if @mouse.has_key? 'any'
    @mouse[btn].call(x, y)
  end
end

#on(args = {}, &proc) ⇒ Object

def on(mouse: nil, key: nil, key_up: nil, key_down: nil, controller: nil, &proc)



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
# File 'lib/ruby2d/window.rb', line 104

def on(args = {}, &proc)
  mouse      = args[:mouse]
  key        = args[:key]
  key_up     = args[:key_up]
  key_down   = args[:key_down]
  controller = args[:controller]
  
  unless mouse.nil?
    reg_mouse(mouse, &proc)
  end
  
  unless key_down.nil?
    reg_key_down(key_down, &proc)
  end
  
  unless key.nil?
    reg_key(key, &proc)
  end
  
  unless key_up.nil?
    reg_key_up(key_up, &proc)
  end
  
  unless controller.nil?
    reg_controller(controller, &proc)
  end
end

#on_controller(&proc) ⇒ Object



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

def on_controller(&proc)
  @on_controller_proc = proc
  true
end

#on_key(&proc) ⇒ Object



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

def on_key(&proc)
  @on_key_proc = proc
  true
end

#remove(o) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby2d/window.rb', line 81

def remove(o)
  if o == nil
    raise Error, "Cannot remove '#{o.class}' from window!"
  end
  
  if i = @objects.index(o)
    @objects.slice!(i)
    true
  else
    false
  end
end

#set(opts) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby2d/window.rb', line 53

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
  @width           = opts[:width]           || @width
  @height          = opts[:height]          || @height
  @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
  @diagnostics     = opts[:diagnostics]     || @diagnostics
end

#showObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/ruby2d/ruby2d-opal.rb', line 194

def show
  $R2D_WINDOW = self
  
  `
  var width  = #{$R2D_WINDOW.get(:width)};
  var height = #{$R2D_WINDOW.get(:height)};
  
  var vp_w = #{$R2D_WINDOW.get(:viewport_width)};
  var viewport_width = vp_w != Opal.nil ? vp_w : width;
  
  var vp_h = #{$R2D_WINDOW.get(:viewport_height)};
  var viewport_height = vp_h != Opal.nil ? vp_h : height;
  
  win = S2D.CreateWindow(
    #{$R2D_WINDOW.get(:title)}, width, height, update, render, "ruby2d-app", {}
  );
  
  win.viewport.width  = viewport_width;
  win.viewport.height = viewport_height;
  win.on_key          = on_key;
  win.on_mouse        = on_mouse;
  
  S2D.Show(win);`
end

#update(&proc) ⇒ Object



98
99
100
101
# File 'lib/ruby2d/window.rb', line 98

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

#update_callbackObject



183
184
185
# File 'lib/ruby2d/window.rb', line 183

def update_callback
  @update_proc.call
end