Class: Processing::App

Inherits:
PApplet
  • Object
show all
Includes:
Math, HelperMethods
Defined in:
lib/ruby-processing/app.rb

Overview

This is the main Ruby-Processing class, and is what you’ll inherit from when you create a sketch. This class can call all of the methods available in Processing, and has two mandatory methods, ‘setup’ and ‘draw’, both of which you should define in your sketch. ‘setup’ will be called one time when the sketch is first loaded, and ‘draw’ will be called constantly, for every frame.

Constant Summary collapse

@@full_screen =
false
@@library_loader =
LibraryLoader.new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#buffer, #color, #find_method, #frame_count, #frame_rate, #grid, #java_self, #key, #key_code, #key_pressed?, #lerp_color, #load_strings, #loop, #mouse_button, #mouse_pressed?, #mouse_x, #mouse_y, #pmouse_x, #pmouse_y, #proxy_java_fields, #save_strings, #set_sketch_path, #sketch_path

Constructor Details

#initialize(options = {}) ⇒ App

When you make a new sketch, you pass in (optionally), a width, height, x, y, title, and whether or not you want to run in full-screen.

This is a little different than Processing where height and width are declared inside the setup method instead.



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
168
169
170
# File 'lib/ruby-processing/app.rb', line 122

def initialize(options={})
  super()
  $app = self
  proxy_java_fields
  set_sketch_path unless Processing.online?
  mix_proxy_into_inner_classes
  @started = false
 
  unless Processing.online?
    java.lang.Thread.default_uncaught_exception_handler = proc do |thread, exception|
      puts(exception.class.to_s)
      puts(exception.message)
      puts(exception.backtrace.collect { |trace| "\t" + trace })
      close
    end
  end

  # for the list of all available args, see 
  # http://processing.googlecode.com/svn/trunk/processing/build/javadoc/core/processing/core/PApplet.html#runSketch%28java.lang.String[],%20processing.core.PApplet%29
  args = []

  @width, @height = options[:width], options[:height]

  if @@full_screen || options[:full_screen] 
    @@full_screen = true
    args << "--present"
  end

  @render_mode  ||= JAVA2D

  if Processing.online?
    JRUBY_APPLET.background_color = nil
    JRUBY_APPLET.double_buffered = false
    JRUBY_APPLET.add self
    JRUBY_APPLET.validate
    # Add the callbacks to peacefully expire.
    JRUBY_APPLET.on_stop { self.stop }
    JRUBY_APPLET.on_destroy { self.destroy }
    init
  else
    x = options[:x] || 0
    y = options[:y] || 0
    args << "--location=#{x},#{y}"

    title = options[:title] || File.basename(SKETCH_PATH).sub(/(\.rb|\.pde)$/, '').titleize
    args << title
    PApplet.run_sketch(args, self)
  end
end

Class Method Details

.full_screenObject



77
# File 'lib/ruby-processing/app.rb', line 77

def self.full_screen;   @@full_screen = true; end

.has_slider(*args) ⇒ Object

:nodoc:



88
89
90
# File 'lib/ruby-processing/app.rb', line 88

def self.has_slider(*args) #:nodoc:
  raise "has_slider has been replaced with a nicer control_panel library. Check it out."
end

.inherited(subclass) ⇒ Object

Keep track of what inherits from the Processing::App, because we’re going to want to instantiate one.



83
84
85
86
# File 'lib/ruby-processing/app.rb', line 83

def self.inherited(subclass)
  super(subclass)
  @sketch_class = subclass
end

.library_loaded?(library_name) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/ruby-processing/app.rb', line 99

def library_loaded?(library_name)
  @@library_loader.library_loaded?(library_name)
end

.load_java_library(*args) ⇒ Object



107
108
109
# File 'lib/ruby-processing/app.rb', line 107

def load_java_library(*args)
  @@library_loader.load_java_library(*args)
end

.load_libraries(*args) ⇒ Object Also known as: load_library



94
95
96
# File 'lib/ruby-processing/app.rb', line 94

def load_libraries(*args)
  @@library_loader.load_library(*args)
end

.load_ruby_library(*args) ⇒ Object



103
104
105
# File 'lib/ruby-processing/app.rb', line 103

def load_ruby_library(*args)
  @@library_loader.load_ruby_library(*args)
end

.method_added(method_name) ⇒ Object

When certain special methods get added to the sketch, we need to let Processing call them by their expected Java names.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby-processing/app.rb', line 42

def self.method_added(method_name) #:nodoc:
  # Watch the definition of these methods, to make sure
  # that Processing is able to call them during events.
  methods_to_alias = {
    :mouse_pressed  => :mousePressed,
    :mouse_dragged  => :mouseDragged,
    :mouse_clicked  => :mouseClicked,
    :mouse_moved    => :mouseMoved,
    :mouse_released => :mouseReleased,
    :key_pressed    => :keyPressed,
    :key_released   => :keyReleased,
    :key_typed      => :keyTyped
  }
  if methods_to_alias.keys.include?(method_name)
    alias_method methods_to_alias[method_name], method_name
  end
end

.sketch_classObject

Handy getters and setters on the class go here:



75
# File 'lib/ruby-processing/app.rb', line 75

def self.sketch_class;  @sketch_class;        end

Instance Method Details

#closeObject

Cleanly close and shutter a running sketch.



225
226
227
228
229
230
231
232
233
234
# File 'lib/ruby-processing/app.rb', line 225

def close
  #$app = nil
  if Processing.online?
    JRUBY_APPLET.remove(self)
  else
    control_panel.remove if respond_to?(:control_panel)
    self.dispose
    self.frame.dispose
  end
end

#full_screen?Boolean

Returns:

  • (Boolean)


78
# File 'lib/ruby-processing/app.rb', line 78

def full_screen?;       @@full_screen;        end

#handleDrawObject



210
211
212
213
214
215
216
# File 'lib/ruby-processing/app.rb', line 210

def handleDraw()
  super
rescue Exception => exception
  puts(exception.message)
  puts(exception.backtrace.collect { |trace| "\t" + trace })
  close
end

#hint(*args) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/ruby-processing/app.rb', line 176

def hint(*args)
  begin
    super(*args)
  rescue Exception => e
    raise e.cause
  end
end

#inspectObject

Provide a loggable string to represent this sketch.



191
192
193
# File 'lib/ruby-processing/app.rb', line 191

def inspect
  "#<Processing::App:#{self.class}:#{@title}>"
end

#library_loaded?(library_name) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/ruby-processing/app.rb', line 112

def library_loaded?(library_name)
  self.class.library_loaded?(library_name)
end

#render_mode(mode_const) ⇒ Object

Specify what rendering Processing should use, without needing to pass size.



219
220
221
222
# File 'lib/ruby-processing/app.rb', line 219

def render_mode(mode_const)
  @render_mode = mode_const
  size(@width, @height, @render_mode)
end

#size(*args) ⇒ Object

We override size to support setting full_screen and to keep our internal @width and @height in line.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ruby-processing/app.rb', line 198

def size(*args)
  args[0], args[1] = screenWidth, screenHeight if @@full_screen && !args.empty?
  w, h, mode       = *args
  @width           = w     || @width
  @height          = h     || @height
  @render_mode     = mode  || @render_mode
  @started = true
  super(*args)
rescue Exception => e
  raise e.cause
end

#startObject

Make sure we set the size if we set it before we start the animation thread.



185
186
187
188
# File 'lib/ruby-processing/app.rb', line 185

def start
  self.size(@width, @height) if @width && @height
  super()
end

#started?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/ruby-processing/app.rb', line 172

def started?
  @started
end