lib/rubysketch/processing.rb in rubysketch-0.3.3 vs lib/rubysketch/processing.rb in rubysketch-0.3.4

- old
+ new

@@ -4,10 +4,17 @@ # Processing compatible API # module Processing + # @private + DEG2RAD__ = Math::PI / 180.0 + + # @private + RAD2DEG__ = 180.0 / Math::PI + + # Vector class. # class Vector include Comparable @@ -25,18 +32,18 @@ # @param y [Numeric] y of vector # @param z [Numeric] z of vector # @param v [Vector] vector object to copy # @param a [Array] array like [x, y, z] # - def initialize (x = 0, y = 0, z = 0, context: context) + def initialize (x = 0, y = 0, z = 0, context: nil) @point = case x when Rays::Point then x.dup when Vector then x.getInternal__.dup when Array then Rays::Point.new x[0] || 0, x[1] || 0, x[2] || 0 else Rays::Point.new x || 0, y || 0, z || 0 end - @context = context + @context = context || Context.context__ end # Initializer for dup or clone # def initialize_copy (o) @@ -462,11 +469,11 @@ # @param angle [Numeric] the angle of rotation # # @return [Vector] rotated this object # def rotate (angle) - angle = @context ? @context.toAngle__(angle) : angle * Utility::RAD2DEG__ + angle = @context ? @context.toAngle__(angle) : angle * RAD2DEG__ @point.rotate! angle self end # Returns the angle of rotation for this vector. @@ -913,11 +920,11 @@ # # @return [nil] nil # def angleMode (mode) @angleScale__ = case mode.upcase.to_sym - when RADIANS then Utility::RAD2DEG__ + when RADIANS then RAD2DEG__ when DEGREES then 1.0 else raise ArgumentError, "invalid angle mode: #{mode}" end nil end @@ -1615,18 +1622,253 @@ end end# Graphics - module Utility + # Processing context + # + module Context + include GraphicsContext, Math + + @@context__ = nil + + def self.context__ () + @@context__ + end + # @private - DEG2RAD__ = Math::PI / 180.0 + def setup__ (window) + @@context__ = self + @window__ = window + @image__ = @window__.canvas + super @window__.canvas_painter.paint {background 0.8} + + @loop__ = true + @redraw__ = false + @frameCount__ = 0 + @mousePos__ = + @mousePrevPos__ = Rays::Point.new 0 + @mousePressed__ = false + @touches__ = [] + + @window__.before_draw = proc {beginDraw} + @window__.after_draw = proc {endDraw} + + drawFrame = -> { + @image__ = @window__.canvas + @painter__ = @window__.canvas_painter + begin + push + @drawBlock__.call if @drawBlock__ + ensure + pop + @frameCount__ += 1 + end + } + + @window__.draw = proc do |e| + if @loop__ || @redraw__ + @redraw__ = false + drawFrame.call + end + @mousePrevPos__ = @mousePos__ + end + + updatePointerStates = -> event, pressed = nil { + @mousePos__ = event.pos + @mousePressed__ = pressed if pressed != nil + @touches__ = event.positions.map {|pos| Touch.new pos.x, pos.y} + } + + @window__.pointer_down = proc do |e| + updatePointerStates.call e, true + (@touchStartedBlock__ || @mousePressedBlock__)&.call + end + + @window__.pointer_up = proc do |e| + updatePointerStates.call e, false + (@touchEndedBlock__ || @mouseReleasedBlock__)&.call + end + + @window__.pointer_move = proc do |e| + updatePointerStates.call e + (@touchMovedBlock__ || @mouseMovedBlock__)&.call + end + + @window__.pointer_drag = proc do |e| + updatePointerStates.call e + (@touchMovedBlock__ || @mouseDraggedBlock__)&.call + end + end + + # Define setup block. + # + def setup (&block) + @window__.setup = block + nil + end + + # Define draw block. + # + def draw (&block) + @drawBlock__ = block if block + nil + end + + def key (&block) + @window__.key = block + nil + end + + def mousePressed (&block) + @mousePressedBlock__ = block if block + @mousePressed__ + end + + def mouseReleased (&block) + @mouseReleasedBlock__ = block if block + nil + end + + def mouseMoved (&block) + @mouseMovedBlock__ = block if block + nil + end + + def mouseDragged (&block) + @mouseDraggedBlock__ = block if block + nil + end + + def touchStarted (&block) + @touchStartedBlock__ = block if block + nil + end + + def touchEnded (&block) + @touchEndedBlock__ = block if block + nil + end + + def touchMoved (&block) + @touchMovedBlock__ = block if block + nil + end + # @private - RAD2DEG__ = 180.0 / Math::PI + private def size__ (width, height) + raise 'size() must be called on startup or setup block' if @started__ + @painter__.__send__ :end_paint + @window__.__send__ :reset_canvas, width, height + @painter__.__send__ :begin_paint + + @auto_resize__ = false + end + + def windowWidth () + @window__.width + end + + def windowHeight () + @window__.height + end + + # Returns number of frames since program started. + # + # @return [Integer] total number of frames + # + def frameCount () + @frameCount__ + end + + # Returns number of frames per second. + # + # @return [Float] frames per second + # + def frameRate () + @window__.event.fps + end + + # Returns pixel density + # + # @return [Numeric] pixel density + # + def displayDensity () + @painter__.pixel_density + end + + # Returns mouse x position + # + # @return [Numeric] horizontal position of mouse + # + def mouseX () + @mousePos__.x + end + + # Returns mouse y position + # + # @return [Numeric] vertical position of mouse + # + def mouseY () + @mousePos__.y + end + + # Returns mouse x position in previous frame + # + # @return [Numeric] horizontal position of mouse + # + def pmouseX () + @mousePrevPos__.x + end + + # Returns mouse y position in previous frame + # + # @return [Numeric] vertical position of mouse + # + def pmouseY () + @mousePrevPos__.y + end + + # Returns array of touches + # + # @return [Array] Touch objects + # + def touches () + @touches__ + end + + # Enables calling draw block on every frame. + # + # @return [nil] nil + # + def loop () + @loop__ = true + end + + # Disables calling draw block on every frame. + # + # @return [nil] nil + # + def noLoop () + @loop__ = false + end + + # Calls draw block to redraw frame. + # + # @return [nil] nil + # + def redraw () + @redraw__ = true + end + + # + # Utility functions + # + # Converts degree to radian. # # @param degree [Numeric] degree to convert # # @return [Numeric] radian @@ -1852,10 +2094,37 @@ # def noise (x, y = 0, z = 0) Rays.perlin(x, y, z) / 2.0 + 0.5 end + # Creates a new vector. + # + # @overload createVector() + # @overload createVector(x, y) + # @overload createVector(x, y, z) + # + # @param x [Numeric] x of new vector + # @param y [Numeric] y of new vector + # @param z [Numeric] z of new vector + # + # @return [Vector] new vector + # + def createVector (*args) + Vector.new *args + end + + # Creates a new off-screen graphics context object. + # + # @param width [Numeric] width of graphics image + # @param height [Numeric] height of graphics image + # + # @return [Graphics] graphics object + # + def createGraphics (width, height) + Graphics.new width, height + end + # Loads image. # # @param filename [String] file name to load image # @param extension [String] type of image to load (ex. 'png') # @@ -1884,247 +2153,9 @@ end end end end path.to_s - end - - def createGraphics (width, height) - Graphics.new width, height - end - - end# Utility - - - # Processing context - # - module Context - - include GraphicsContext, Utility, Math - - # @private - def setup__ (window) - @window__ = window - @image__ = @window__.canvas - super @window__.canvas_painter.paint {background 0.8} - - @loop__ = true - @redraw__ = false - @frameCount__ = 0 - @mousePos__ = - @mousePrevPos__ = Rays::Point.new 0 - @mousePressed__ = false - @touches__ = [] - - @window__.before_draw = proc {beginDraw} - @window__.after_draw = proc {endDraw} - - drawFrame = -> { - @image__ = @window__.canvas - @painter__ = @window__.canvas_painter - begin - push - @drawBlock__.call if @drawBlock__ - ensure - pop - @frameCount__ += 1 - end - } - - @window__.draw = proc do |e| - if @loop__ || @redraw__ - @redraw__ = false - drawFrame.call - end - @mousePrevPos__ = @mousePos__ - end - - updatePointerStates = -> event, pressed = nil { - @mousePos__ = event.pos - @mousePressed__ = pressed if pressed != nil - @touches__ = event.positions.map {|pos| Touch.new pos.x, pos.y} - } - - @window__.pointer_down = proc do |e| - updatePointerStates.call e, true - (@touchStartedBlock__ || @mousePressedBlock__)&.call - end - - @window__.pointer_up = proc do |e| - updatePointerStates.call e, false - (@touchEndedBlock__ || @mouseReleasedBlock__)&.call - end - - @window__.pointer_move = proc do |e| - updatePointerStates.call e - (@touchMovedBlock__ || @mouseMovedBlock__)&.call - end - - @window__.pointer_drag = proc do |e| - updatePointerStates.call e - (@touchMovedBlock__ || @mouseDraggedBlock__)&.call - end - end - - # Define setup block. - # - def setup (&block) - @window__.setup = block - nil - end - - # Define draw block. - # - def draw (&block) - @drawBlock__ = block if block - nil - end - - def key (&block) - @window__.key = block - nil - end - - def mousePressed (&block) - @mousePressedBlock__ = block if block - @mousePressed__ - end - - def mouseReleased (&block) - @mouseReleasedBlock__ = block if block - nil - end - - def mouseMoved (&block) - @mouseMovedBlock__ = block if block - nil - end - - def mouseDragged (&block) - @mouseDraggedBlock__ = block if block - nil - end - - def touchStarted (&block) - @touchStartedBlock__ = block if block - nil - end - - def touchEnded (&block) - @touchEndedBlock__ = block if block - nil - end - - def touchMoved (&block) - @touchMovedBlock__ = block if block - nil - end - - # @private - private def size__ (width, height) - raise 'size() must be called on startup or setup block' if @started__ - - @painter__.__send__ :end_paint - @window__.__send__ :reset_canvas, width, height - @painter__.__send__ :begin_paint - - @auto_resize__ = false - end - - def windowWidth () - @window__.width - end - - def windowHeight () - @window__.height - end - - # Returns number of frames since program started. - # - # @return [Integer] total number of frames - # - def frameCount () - @frameCount__ - end - - # Returns number of frames per second. - # - # @return [Float] frames per second - # - def frameRate () - @window__.event.fps - end - - # Returns pixel density - # - # @return [Numeric] pixel density - # - def displayDensity () - @painter__.pixel_density - end - - # Returns mouse x position - # - # @return [Numeric] horizontal position of mouse - # - def mouseX () - @mousePos__.x - end - - # Returns mouse y position - # - # @return [Numeric] vertical position of mouse - # - def mouseY () - @mousePos__.y - end - - # Returns mouse x position in previous frame - # - # @return [Numeric] horizontal position of mouse - # - def pmouseX () - @mousePrevPos__.x - end - - # Returns mouse y position in previous frame - # - # @return [Numeric] vertical position of mouse - # - def pmouseY () - @mousePrevPos__.y - end - - # Returns array of touches - # - # @return [Array] Touch objects - # - def touches () - @touches__ - end - - # Enables calling draw block on every frame. - # - # @return [nil] nil - # - def loop () - @loop__ = true - end - - # Disables calling draw block on every frame. - # - # @return [nil] nil - # - def noLoop () - @loop__ = false - end - - # Calls draw block to redraw frame. - # - # @return [nil] nil - # - def redraw () - @redraw__ = true end end# Context