lib/rubysketch/processing.rb in rubysketch-0.3.0 vs lib/rubysketch/processing.rb in rubysketch-0.3.1
- old
+ new
@@ -141,10 +141,34 @@
end
end# TextBounds
+ # Touch object.
+ #
+ class Touch
+
+ # Horizontal position of touch
+ #
+ attr_reader :x
+
+ # Vertical position of touch
+ #
+ attr_reader :y
+
+ # @private
+ def initialize (x, y)
+ @x, @y = x, y
+ end
+
+ def id ()
+ raise NotImplementedError
+ end
+
+ end# Touch
+
+
# Drawing context
#
module GraphicsContext
# PI / 2
@@ -477,30 +501,36 @@
end
# Sets font.
#
# @param name [String] font name
- # @param size [Numeric] font size
+ # @param size [Numeric] font size (max 256)
#
# @return [Font] current font
#
def textFont (name = nil, size = nil)
- @painter__.font name, size if name || size
+ setFont__ name, size if name || size
Font.new @painter__.font
end
# Sets text size.
#
- # @param size [Numeric] font size
+ # @param size [Numeric] font size (max 256)
#
# @return [nil] nil
#
def textSize (size)
- @painter__.font @painter__.font.name, size
+ setFont__ @painter__.font.name, size
nil
end
+ # @private
+ def setFont__ (name, size)
+ size = 256 if size && size > 256
+ @painter__.font name, size
+ end
+
# Clears screen.
#
# @overload background(str)
# @overload background(str, alpha)
# @overload background(gray)
@@ -1260,74 +1290,68 @@
def setup__ (window)
super()
@loop__ = true
@redraw__ = false
@frameCount__ = 0
- @mouseX__ =
- @mouseY__ =
- @mousePrevX__ =
- @mousePrevY__ = 0
+ @mousePos__ =
+ @mousePrevPos__ = Rays::Point.new 0
@mousePressed__ = false
+ @touches__ = []
@window__ = window
@image__ = @window__.canvas
@painter__ = @window__.canvas_painter
@painter__.miter_limit = 10
- drawFrame = -> event {
+ @window__.before_draw = proc {beginDraw}
+ @window__.after_draw = proc {endDraw}
+
+ drawFrame = -> {
@image__ = @window__.canvas
@painter__ = @window__.canvas_painter
begin
push
- @drawBlock__.call event if @drawBlock__
+ @drawBlock__.call if @drawBlock__
ensure
pop
@frameCount__ += 1
end
}
- updateMouseState = -> x, y, pressed = nil {
- @mouseX__ = x
- @mouseY__ = y
- @mousePressed__ = pressed if pressed != nil
- }
-
- updateMousePrevPos = -> {
- @mousePrevX__ = @mouseX__
- @mousePrevY__ = @mouseY__
- }
-
- @window__.before_draw = proc {beginDraw}
- @window__.after_draw = proc {endDraw}
-
@window__.draw = proc do |e|
if @loop__ || @redraw__
@redraw__ = false
- drawFrame.call e
+ drawFrame.call
end
- updateMousePrevPos.call
+ @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|
- updateMouseState.call e.x, e.y, true
- @mousePressedBlock__.call e if @mousePressedBlock__
+ updatePointerStates.call e, true
+ (@touchStartedBlock__ || @mousePressedBlock__)&.call
end
@window__.pointer_up = proc do |e|
- updateMouseState.call e.x, e.y, false
- @mouseReleasedBlock__.call e if @mouseReleasedBlock__
+ updatePointerStates.call e, false
+ (@touchEndedBlock__ || @mouseReleasedBlock__)&.call
end
@window__.pointer_move = proc do |e|
- updateMouseState.call e.x, e.y
- @mouseMovedBlock__.call e if @mouseMovedBlock__
+ updatePointerStates.call e
+ (@touchMovedBlock__ || @mouseMovedBlock__)&.call
end
@window__.pointer_drag = proc do |e|
- updateMouseState.call e.x, e.y
- @mouseDraggedBlock__.call e if @mouseDraggedBlock__
+ updatePointerStates.call e
+ (@touchMovedBlock__ || @mouseDraggedBlock__)&.call
end
end
# Define setup block.
#
@@ -1366,10 +1390,25 @@
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
@@ -1414,34 +1453,42 @@
# Returns mouse x position
#
# @return [Numeric] horizontal position of mouse
#
def mouseX ()
- @mouseX__
+ @mousePos__.x
end
# Returns mouse y position
#
# @return [Numeric] vertical position of mouse
#
def mouseY ()
- @mouseY__
+ @mousePos__.y
end
# Returns mouse x position in previous frame
#
# @return [Numeric] horizontal position of mouse
#
def pmouseX ()
- @mousePrevX__
+ @mousePrevPos__.x
end
# Returns mouse y position in previous frame
#
# @return [Numeric] vertical position of mouse
#
def pmouseY ()
- @mousePrevY__
+ @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