Class: MSPhysics::CommonContext
- Defined in:
- RubyExtension/MSPhysics/common_context.rb
Overview
CommonContext contains methods that both BodyContext and ControllerContext objects have in common.
Direct Known Subclasses
Variables collapse
-
#delete_global_var(name) ⇒ Boolean
Remove global variable from hash.
-
#delete_var(name) ⇒ Boolean
Remove variable from hash.
-
#get_global_var(name) ⇒ Object
Get global variable value.
-
#get_set_global_var(name, value) ⇒ Object
Get original global variable value and assign a new value.
-
#get_set_var(name, value) ⇒ Object
Get original variable value and assign a new value.
-
#get_var(name) ⇒ Object
Get variable value.
-
#set_global_var(name, value) ⇒ Object
Set global variable value.
-
#set_var(name, value) ⇒ Object
Set variable value.
Instances collapse
-
#simulation ⇒ Simulation
Get Simulation instance.
-
#world ⇒ World
Get simulation World instance.
Time collapse
-
#frame ⇒ Integer
Get simulation frame.
User Input collapse
-
#joybutton(button) ⇒ Integer
Get joy-button value.
-
#joypad ⇒ Integer
Get joy-pad value.
-
#joystick(axis) ⇒ Numeric
Get joystick value.
-
#key(vk) ⇒ Integer
Get state of a keyboard key.
-
#key_slider(name, key1, key2, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric
Create a new range slider or get slider value if slider with the specified name already exists.
-
#leftx ⇒ Numeric
Output from keys D and A or X-axis position on the left joystick.
-
#lefty ⇒ Numeric
Output from keys W and S or Y-axis position on the left joystick.
-
#leftz ⇒ Numeric
Output from keys E and Q or position of the joy controller’s left trigger.
-
#numx ⇒ Integer
Output from keys NUMPAD6 and NUMPAD4 or centered-X-axis position on the joy-pad.
-
#numy ⇒ Integer
Output from keys NUMPAD8 and NUMPAD5 or centered-Y-axis position on the joy-pad.
-
#rightx ⇒ Numeric
Output from LEFT and RIGHT arrow keys or X-axis position on the right joystick.
-
#righty ⇒ Numeric
Output from UP and DOWN arrow keys or Y-axis position on the right joystick.
-
#rightz ⇒ Numeric
Output from keys PageUp and PageDown or position of the joy controller’s right trigger.
-
#slider(name, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric
Create a new range slider or get slider value if slider with the specified name already exists.
-
#toggle_key(vk) ⇒ Integer
Get toggled state of a keyboard key.
Functions collapse
-
#accumulator(rate, delay = 0.0) ⇒ Integer
Increment the accumulator by one at a specific rate and offset.
-
#oscillator(frequency, delay = 0.0) ⇒ Numeric
Calculate the value of a sine curve at a particular world time.
-
#oscillator2(frequency, delay = 0.0) ⇒ Numeric
Compute the value of a shifted sine curve at a particular world time.
-
#oscillator2_slope(frequency, delay = 0.0) ⇒ Numeric
Calculate the slope of a shifted sine curve at a particular world time.
-
#oscillator_slope(frequency, delay = 0.0) ⇒ Numeric
Calculate the slope of a sine curve at a particular world time.
-
#repeater(rate, hold, delay = 0.0) ⇒ Integer
Compute a repeater value based on rate, hold, and delay.
-
#singular_repeater(rate, delay = 0.0, id = nil) ⇒ Integer
Compute a repeater value based on rate and delay that repeats only one time whenever it is triggered.
Instance Method Summary collapse
-
#initialize ⇒ CommonContext
constructor
A new instance of CommonContext.
Methods inherited from Entity
Constructor Details
#initialize ⇒ CommonContext
Returns a new instance of CommonContext.
15 16 17 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 15 def initialize @_singular_repeater_flags = {} end |
Instance Method Details
#accumulator(rate, delay = 0.0) ⇒ Integer
Increment the accumulator by one at a specific rate and offset.
460 461 462 463 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 460 def accumulator(rate, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay (rtime > 0 && rate > MSPhysics::EPSILON) ? (rtime / rate).to_i : 0 end |
#delete_global_var(name) ⇒ Boolean
Remove global variable from hash.
126 127 128 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 126 def delete_global_var(name) @@_global_variables.delete(name) != nil end |
#delete_var(name) ⇒ Boolean
Remove variable from hash.
69 70 71 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 69 def delete_var(name) @@_variables.delete(name) != nil end |
#frame ⇒ Integer
Get simulation frame.
154 155 156 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 154 def frame MSPhysics::Simulation.instance.frame end |
#get_global_var(name) ⇒ Object
These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.
Get global variable value.
94 95 96 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 94 def get_global_var(name) @@_global_variables[name] end |
#get_set_global_var(name, value) ⇒ Object
These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.
Get original global variable value and assign a new value.
117 118 119 120 121 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 117 def get_set_global_var(name, value) orig_value = @@_global_variables[name] @@_global_variables[name] = value orig_value end |
#get_set_var(name, value) ⇒ Object
These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.
Get original variable value and assign a new value.
81 82 83 84 85 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 81 def get_set_var(name, value) orig_value = @@_variables[name] @@_variables[name] = value orig_value end |
#get_var(name) ⇒ Object
These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.
Get variable value.
50 51 52 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 50 def get_var(name) @@_variables[name] end |
#joybutton(button) ⇒ Integer
Button name parameter is not case sensitive.
Get joy-button value.
294 295 296 297 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 294 def () v = MSPhysics::Simulation.instance.[.to_s.downcase] v ? v : 0 end |
#joypad ⇒ Integer
Get joy-pad value.
-
0
if hat is centered -
1
if hat is up -
2
if hat is right -
4
if hat is down -
8
if hat is left -
12
if hat is left-down -
9
if hat is left-up -
6
if hat is right-down -
3
if hat is right-up
315 316 317 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 315 def joypad MSPhysics::Simulation.instance.joypad_data end |
#joystick(axis) ⇒ Numeric
Axis name parameter is not case sensitive.
Get joystick value.
268 269 270 271 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 268 def joystick(axis) v = MSPhysics::Simulation.instance.joystick_data[axis.to_s.downcase] v ? v : 0.0 end |
#key(vk) ⇒ Integer
The vk
parameter is not case sensitive.
Get state of a keyboard key.
169 170 171 172 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 169 def key(vk) return 0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? AMS::Keyboard.key(vk) end |
#key_slider(name, key1, key2, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric
Create a new range slider or get slider value if slider with the specified name already exists.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 237 def (name, key1, key2, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) data = @@_key_sliders[name] unless data data = { :key1 => key1, :key2 => key2, :value => default_value.to_f, :min => min.to_f, :max => max.to_f, :step => step.to_f } @@_key_sliders[name] = data end data[:value] end |
#leftx ⇒ Numeric
Output from keys D and A or X-axis position on the left joystick.
355 356 357 358 359 360 361 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 355 def leftx return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('d') - AMS::Keyboard.key('a') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['leftx'] v ? v : 0.0 end |
#lefty ⇒ Numeric
Output from keys W and S or Y-axis position on the left joystick.
365 366 367 368 369 370 371 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 365 def lefty return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('w') - AMS::Keyboard.key('s') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['lefty'] v ? v : 0.0 end |
#leftz ⇒ Numeric
Output from keys E and Q or position of the joy controller’s left trigger.
375 376 377 378 379 380 381 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 375 def leftz return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('e') - AMS::Keyboard.key('q') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['leftz'] v ? v : 0.0 end |
#numx ⇒ Integer
Output from keys NUMPAD6 and NUMPAD4 or centered-X-axis position on the joy-pad.
386 387 388 389 390 391 392 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 386 def numx return 0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('numpad6') - AMS::Keyboard.key('numpad4') return v if v != 0 jpd = MSPhysics::Simulation.instance.joypad_data jpd == 2 ? 1 : (jpd == 8 ? -1 : 0) end |
#numy ⇒ Integer
Output from keys NUMPAD8 and NUMPAD5 or centered-Y-axis position on the joy-pad.
397 398 399 400 401 402 403 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 397 def numy return 0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('numpad8') - AMS::Keyboard.key('numpad5') return v if v != 0 jpd = MSPhysics::Simulation.instance.joypad_data jpd == 1 ? 1 : (jpd == 4 ? -1 : 0) end |
#oscillator(frequency, delay = 0.0) ⇒ Numeric
Calculate the value of a sine curve at a particular world time.
414 415 416 417 418 419 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 414 def oscillator(frequency, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay return 0.0 if rtime < 0 inc = frequency * 2 * Math::PI Math.sin(inc * rtime) end |
#oscillator2(frequency, delay = 0.0) ⇒ Numeric
Compute the value of a shifted sine curve at a particular world time.
437 438 439 440 441 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 437 def oscillator2(frequency, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay return 0.0 if rtime < 0 Math.sin(2 * Math::PI * (frequency * rtime - 0.25)) * 0.5 + 0.5 end |
#oscillator2_slope(frequency, delay = 0.0) ⇒ Numeric
Calculate the slope of a shifted sine curve at a particular world time.
448 449 450 451 452 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 448 def oscillator2_slope(frequency, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay return 0.0 if rtime < 0 frequency * Math::PI * Math.cos(2 * Math::PI * (frequency * rtime - 0.25)) end |
#oscillator_slope(frequency, delay = 0.0) ⇒ Numeric
Calculate the slope of a sine curve at a particular world time.
426 427 428 429 430 431 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 426 def oscillator_slope(frequency, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay return 0.0 if rtime < 0 inc = frequency * 2 * Math::PI inc * Math.cos(inc * rtime) end |
#repeater(rate, hold, delay = 0.0) ⇒ Integer
Compute a repeater value based on rate, hold, and delay.
473 474 475 476 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 473 def repeater(rate, hold, delay = 0.0) rtime = MSPhysics::Simulation.instance.world.time - delay (rtime > 0 && rate > MSPhysics::EPSILON && rtime % rate < hold) ? 1 : 0 end |
#rightx ⇒ Numeric
Output from LEFT and RIGHT arrow keys or X-axis position on the right joystick.
322 323 324 325 326 327 328 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 322 def rightx return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('right') - AMS::Keyboard.key('left') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['rightx'] v ? v : 0.0 end |
#righty ⇒ Numeric
Output from UP and DOWN arrow keys or Y-axis position on the right joystick.
333 334 335 336 337 338 339 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 333 def righty return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('up') - AMS::Keyboard.key('down') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['righty'] v ? v : 0.0 end |
#rightz ⇒ Numeric
Output from keys PageUp and PageDown or position of the joy controller’s right trigger.
344 345 346 347 348 349 350 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 344 def rightz return 0.0 if AMS::IS_PLATFORM_WINDOWS && !AMS::Sketchup.is_main_window_active? v = AMS::Keyboard.key('PageUp') - AMS::Keyboard.key('PageDown') return v.to_f if v != 0 v = MSPhysics::Simulation.instance.joystick_data['rightz'] v ? v : 0.0 end |
#set_global_var(name, value) ⇒ Object
These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.
Set global variable value.
105 106 107 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 105 def set_global_var(name, value) @@_global_variables[name] = value end |
#set_var(name, value) ⇒ Object
These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.
Set variable value.
62 63 64 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 62 def set_var(name, value) @@_variables[name] = value end |
#simulation ⇒ Simulation
Get Simulation instance.
135 136 137 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 135 def simulation MSPhysics::Simulation.instance end |
#singular_repeater(rate, delay = 0.0, id = nil) ⇒ Integer
Compute a repeater value based on rate and delay that repeats only one time whenever it is triggered.
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 490 def singular_repeater(rate, delay = 0.0, id = nil) rtime = MSPhysics::Simulation.instance.world.time - delay if rtime > 0 && rate > MSPhysics::EPSILON res = (rtime / rate).to_i opts = [rate, delay, id] if @_singular_repeater_flags[opts] == res return 0 else @_singular_repeater_flags[opts] = res return 1 end else return 0 end end |
#slider(name, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric
Create a new range slider or get slider value if slider with the specified name already exists.
215 216 217 218 219 220 221 222 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 215 def (name, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) unless MSPhysics::ControlPanel.(name) MSPhysics::ControlPanel.open MSPhysics::ControlPanel.show MSPhysics::ControlPanel.(name, default_value, min, max, step) end MSPhysics::ControlPanel.(name) end |
#toggle_key(vk) ⇒ Integer
The vk
parameter is not case sensitive.
Get toggled state of a keyboard key.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'RubyExtension/MSPhysics/common_context.rb', line 182 def toggle_key(vk) vkc = AMS::Keyboard.get_key_code(vk) vks = AMS::Keyboard.key_down?(vkc) vkt = @@_toggled[vkc] || 0 if vkt == 0 && vks @@_toggled[vkc] = 1 1 elsif (vkt == 1 && vks) || (vkt == 2 && !vks) 1 elsif vkt == 1 && !vks @@_toggled[vkc] = 2 1 elsif vkt == 2 && vks @@_toggled[vkc] = 3 0 elsif vkt == 3 && !vks @@_toggled[vkc] = 0 0 else 0 end end |