lib/rubysketch/processing.rb in rubysketch-0.3.19 vs lib/rubysketch/processing.rb in rubysketch-0.3.20
- old
+ new
@@ -880,10 +880,77 @@
# Mode for rectMode() and ellipseMode().
#
RADIUS = :radius
+ # Mode for strokeCap().
+ #
+ BUTT = :butt
+
+ # Mode for strokeJoin().
+ #
+ MITER = :miter
+
+ # Mode for strokeCap() and strokeJoin().
+ #
+ ROUND = :round
+
+ # Mode for strokeCap() and strokeJoin().
+ #
+ SQUARE = :square
+
+ # Mode for blendMode().
+ #
+ BLEND = :normal
+
+ # Mode for blendMode().
+ #
+ ADD = :add
+
+ # Mode for blendMode().
+ #
+ SUBTRACT = :subtract
+
+ # Mode for blendMode().
+ #
+ LIGHTEST = :lightest
+
+ # Mode for blendMode().
+ #
+ DARKEST = :darkest
+
+ # Mode for blendMode().
+ #
+ EXCLUSION = :exclusion
+
+ # Mode for blendMode().
+ #
+ MULTIPLY = :multiply
+
+ # Mode for blendMode().
+ #
+ SCREEN = :screen
+
+ # Mode for blendMode().
+ #
+ REPLACE = :replace
+
+ # Key code or Mode for textAlign().
+ LEFT = :left
+
+ # Key code or Mode for textAlign().
+ RIGHT = :right
+
+ # Mode for textAlign().
+ TOP = :top
+
+ # Mode for textAlign().
+ BOTTOM = :bottom
+
+ # Mode for textAlign().
+ BASELINE = :baseline
+
# Key codes.
ENTER = :enter
SPACE = :space
TAB = :tab
DELETE = :delete
@@ -929,41 +996,11 @@
F23 = :f23
F24 = :f24
UP = :up
DOWN = :down
- # Key code or Mode for textAlign().
- LEFT = :left
-
- # Key code or Mode for textAlign().
- RIGHT = :right
-
- # Mode for textAlign().
- TOP = :top
-
- # Mode for textAlign().
- BOTTOM = :bottom
-
- # Mode for textAlign().
- BASELINE = :baseline
-
- # Mode for strokeCap().
- #
- BUTT = :butt
-
- # Mode for strokeJoin().
- #
- MITER = :miter
-
- # Mode for strokeCap() and strokeJoin().
- #
- ROUND = :round
-
- # Mode for strokeCap() and strokeJoin().
- #
- SQUARE = :square
-
+ # @private
def init__(image, painter)
@drawing__ = false
@hsbColor__ = false
@colorMaxes__ = [1.0] * 4
@angleScale__ = 1.0
@@ -972,25 +1009,28 @@
@imageMode__ = nil
@textAlignH__ = nil
@textAlignV__ = nil
@matrixStack__ = []
@styleStack__ = []
+ @fontCache__ = {}
updateCanvas__ image, painter
colorMode RGB, 255
angleMode RADIANS
rectMode CORNER
ellipseMode CENTER
imageMode CORNER
+ blendMode BLEND
textAlign LEFT
fill 255
stroke 0
strokeWeight 1
end
+ # @private
def updateCanvas__(image, painter)
@image__, @painter__ = image, painter
end
# @private
@@ -1167,10 +1207,19 @@
def fill(*args)
@painter__.fill(*toRGBA__(*args))
nil
end
+ # Disables filling.
+ #
+ # @return [nil] nil
+ #
+ def noFill()
+ @painter__.fill nil
+ nil
+ end
+
# Sets stroke color.
#
# @overload stroke(rgb)
# @overload stroke(rgb, alpha)
# @overload stroke(gray)
@@ -1190,10 +1239,19 @@
def stroke(*args)
@painter__.stroke(*toRGBA__(*args))
nil
end
+ # Disables drawing stroke.
+ #
+ # @return [nil] nil
+ #
+ def noStroke()
+ @painter__.stroke nil
+ nil
+ end
+
# Sets stroke weight.
#
# @param weight [Numeric] width of stroke
#
# @return [nil] nil
@@ -1223,48 +1281,73 @@
def strokeJoin(join)
@painter__.stroke_join join
nil
end
- # Disables filling.
+ # Limits the drawable rectangle.
#
+ # The parameters a, b, c, and d are determined by rectMode().
+ #
+ # @param a [Numeric] horizontal position of the drawable area, by default
+ # @param b [Numeric] vertical position of the drawable area, by default
+ # @param c [Numeric] width of the drawable area, by default
+ # @param d [Numeric] height of the drawable area, by default
+ #
# @return [nil] nil
#
- def noFill()
- @painter__.fill nil
+ def clip(a, b, c, d)
+ x, y, w, h = toXYWH__ @imageMode__, a, b, c, d
+ @painter__.clip x, y, w, h
nil
end
- # Disables drawing stroke.
+ # Disables clipping.
#
# @return [nil] nil
#
- def noStroke()
- @painter__.stroke nil
+ def noClip()
+ @painter__.no_clip
nil
end
+ # Sets blend mode. Default is BLEND.
+ #
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE]
+ #
+ # @return [nil] nil
+ #
+ def blendMode(mode)
+ @painter__.blend_mode = mode
+ nil
+ end
+
# Sets font.
#
+ # @overload textFont(font)
+ # @overload textFont(name)
+ # @overload textFont(font, size)
+ # @overload textFont(name, size)
+ #
+ # @param font [Font] font
# @param name [String] font name
# @param size [Numeric] font size (max 256)
#
# @return [Font] current font
#
- def textFont(name = nil, size = nil)
- setFont__ name, size if name || size
+ def textFont(font = nil, size = nil)
+ setFont__ font, size if font || size
Font.new @painter__.font
end
# Sets text size.
#
# @param size [Numeric] font size (max 256)
#
# @return [nil] nil
#
def textSize(size)
- setFont__ @painter__.font.name, size
+ setFont__ nil, size
nil
end
def textWidth(str)
@painter__.font.width str
@@ -1282,13 +1365,19 @@
@textAlignH__ = horizontal
@textAlignV__ = vertical
end
# @private
- def setFont__(name, size)
- size = 256 if size && size > 256
- @painter__.font name, size
+ def setFont__(fontOrName, size)
+ name = case fontOrName
+ when Font then fontOrName.name
+ else fontOrName || @painter__.font.name
+ end
+ size ||= @painter__.font.size
+ size = 256 if size > 256
+ font = @fontCache__[[name, size]] ||= Rays::Font.new name, size
+ @painter__.font = font
end
# Clears screen.
#
# @overload background(str)
@@ -1350,18 +1439,20 @@
nil
end
# Draws a rectangle.
#
+ # The parameters a, b, c, and d are determined by rectMode().
+ #
# @overload rect(a, b, c, d)
# @overload rect(a, b, c, d, r)
# @overload rect(a, b, c, d, tl, tr, br, bl)
#
- # @param a [Numeric] horizontal position of the shape by default
- # @param b [Numeric] vertical position of the shape by default
- # @param c [Numeric] width of the shape by default
- # @param d [Numeric] height of the shape by default
+ # @param a [Numeric] horizontal position of the shape, by default
+ # @param b [Numeric] vertical position of the shape, by default
+ # @param c [Numeric] width of the shape, by default
+ # @param d [Numeric] height of the shape, by default
# @param r [Numeric] radius for all corners
# @param tl [Numeric] radius for top-left corner
# @param tr [Numeric] radius for top-right corner
# @param br [Numeric] radius for bottom-right corner
# @param bl [Numeric] radius for bottom-left corner
@@ -1380,15 +1471,17 @@
nil
end
# Draws an ellipse.
#
- # @param a [Numeric] horizontal position of the shape
- # @param b [Numeric] vertical position of the shape
- # @param c [Numeric] width of the shape
- # @param d [Numeric] height of the shape
+ # The parameters a, b, c, and d are determined by ellipseMode().
#
+ # @param a [Numeric] horizontal position of the shape, by default
+ # @param b [Numeric] vertical position of the shape, by default
+ # @param c [Numeric] width of the shape, by default
+ # @param d [Numeric] height of the shape, by default
+ #
# @return [nil] nil
#
def ellipse(a, b, c, d)
assertDrawing__
x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
@@ -1403,19 +1496,23 @@
# @param extent [Numeric] width and height of the shape
#
# @return [nil] nil
#
def circle(x, y, extent)
- ellipse x, y, extent, extent
+ assertDrawing__
+ @painter__.ellipse x, y, extent, extent
+ nil
end
# Draws an arc.
#
- # @param a [Numeric] horizontal position of the shape
- # @param b [Numeric] vertical position of the shape
- # @param c [Numeric] width of the shape
- # @param d [Numeric] height of the shape
+ # The parameters a, b, c, and d are determined by ellipseMode().
+ #
+ # @param a [Numeric] horizontal position of the shape, by default
+ # @param b [Numeric] vertical position of the shape, by default
+ # @param c [Numeric] width of the shape, by default
+ # @param d [Numeric] height of the shape, by default
# @param start [Numeric] angle to start the arc
# @param stop [Numeric] angle to stop the arc
#
# @return [nil] nil
#
@@ -1514,21 +1611,23 @@
nil
end
# Draws a text.
#
+ # The parameters a, b, c, and d are determined by rectMode().
+ #
# @overload text(str)
# @overload text(str, x, y)
# @overload text(str, a, b, c, d)
#
# @param str [String] text to draw
# @param x [Numeric] horizontal position of the text
# @param y [Numeric] vertical position of the text
- # @param a [Numeric] equivalent to parameters of the rect(), see rectMode()
- # @param b [Numeric] equivalent to parameters of the rect(), see rectMode()
- # @param c [Numeric] equivalent to parameters of the rect(), see rectMode()
- # @param d [Numeric] equivalent to parameters of the rect(), see rectMode()
+ # @param a [Numeric] horizontal position of the text, by default
+ # @param b [Numeric] vertical position of the text, by default
+ # @param c [Numeric] width of the text, by default
+ # @param d [Numeric] height of the text, by default
#
# @return [nil] nil
#
def text(str, x, y, x2 = nil, y2 = nil)
assertDrawing__
@@ -1551,18 +1650,20 @@
nil
end
# Draws an image.
#
+ # The parameters a, b, c, and d are determined by imageMode().
+ #
# @overload image(img, a, b)
# @overload image(img, a, b, c, d)
#
# @param img [Image] image to draw
- # @param a [Numeric] horizontal position of the image
- # @param b [Numeric] vertical position of the image
- # @param c [Numeric] width of the image
- # @param d [Numeric] height of the image
+ # @param a [Numeric] horizontal position of the image, by default
+ # @param b [Numeric] vertical position of the image, by default
+ # @param c [Numeric] width of the image, by default
+ # @param d [Numeric] height of the image, by default
#
# @return [nil] nil
#
def image(img, a, b, c = nil, d = nil)
assertDrawing__
@@ -1595,18 +1696,22 @@
@painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
end
# Applies translation matrix to current transformation matrix.
#
- # @param x [Numeric] horizontal transformation
- # @param y [Numeric] vertical transformation
+ # @overload translate(x, y)
+ # @overload translate(x, y, z)
#
+ # @param x [Numeric] left/right translation
+ # @param y [Numeric] up/down translation
+ # @param y [Numeric] forward/backward translation
+ #
# @return [nil] nil
#
- def translate(x, y)
+ def translate(x, y, z = 0)
assertDrawing__
- @painter__.translate x, y
+ @painter__.translate x, y, z
nil
end
# Applies scale matrix to current transformation matrix.
#
@@ -1682,10 +1787,12 @@
@painter__.fill,
@painter__.stroke,
@painter__.stroke_width,
@painter__.stroke_cap,
@painter__.stroke_join,
+ @painter__.clip,
+ @painter__.blend_mode,
@painter__.font,
@hsbColor__,
@colorMaxes__,
@angleScale__,
@rectMode__,
@@ -1709,10 +1816,12 @@
@painter__.fill,
@painter__.stroke,
@painter__.stroke_width,
@painter__.stroke_cap,
@painter__.stroke_join,
+ @painter__.clip,
+ @painter__.blend_mode,
@painter__.font,
@hsbColor__,
@colorMaxes__,
@angleScale__,
@rectMode__,
@@ -1826,11 +1935,11 @@
@frameCount__ = 0
@key__ = nil
@keyCode__ = nil
@keysPressed__ = Set.new
@pointerPos__ =
- @pointerPrevPos__ = [0, 0]
+ @pointerPrevPos__ = Rays::Point.new 0
@pointersPressed__ = []
@touches__ = []
@motionGravity__ = createVector 0, 0
@window__.before_draw = proc {beginDraw__}
@@ -1850,11 +1959,10 @@
@window__.draw = proc do |e|
if @loop__ || @redraw__
@redraw__ = false
drawFrame.call
end
- @pointerPrevPos__ = @pointerPos__
end
updateKeyStates = -> event, pressed {
@key__ = event.chars
@keyCode__ = event.key
@@ -1869,11 +1977,12 @@
mouse_right: RIGHT,
mouse_middle: CENTER
}
updatePointerStates = -> event, pressed = nil {
- @pointerPos__ = event.pos.to_a
+ @pointerPrevPos__ = @pointerPos__
+ @pointerPos__ = event.pos.dup
@touches__ = event.pointers.map {|p| Touch.new(p.id, *p.pos.to_a)}
if pressed != nil
array = @pointersPressed__
event.types
.tap {|types| types.delete :mouse}
@@ -1893,16 +2002,21 @@
@keyReleasedBlock__&.call
end
@window__.pointer_down = proc do |e|
updatePointerStates.call e, true
+ @pointerDownStartPos__ = @pointerPos__.dup
(@touchStartedBlock__ || @mousePressedBlock__)&.call
end
@window__.pointer_up = proc do |e|
updatePointerStates.call e, false
(@touchEndedBlock__ || @mouseReleasedBlock__)&.call
+ if startPos = @pointerDownStartPos__
+ @mouseClickedBlock__&.call if (@pointerPos__ - startPos).length < 3
+ @pointerDownStartPos__ = nil
+ end
end
@window__.pointer_move = proc do |e|
updatePointerStates.call e
(@touchMovedBlock__ || @mouseMovedBlock__)&.call
@@ -1919,17 +2033,21 @@
end
end
# Defines setup block.
#
+ # @return [nil] nil
+ #
def setup(&block)
@window__.setup = block
nil
end
# Defines draw block.
#
+ # @return [nil] nil
+ #
def draw(&block)
@drawBlock__ = block if block
nil
end
@@ -1942,17 +2060,21 @@
not @keysPressed__.empty?
end
# Defines keyReleased block.
#
+ # @return [nil] nil
+ #
def keyReleased(&block)
@keyReleasedBlock__ = block if block
nil
end
# Defines keyTyped block.
#
+ # @return [nil] nil
+ #
def keyTyped(&block)
@keyTypedBlock__ = block if block
nil
end
@@ -1965,52 +2087,75 @@
not @pointersPressed__.empty?
end
# Defines mouseReleased block.
#
+ # @return [nil] nil
+ #
def mouseReleased(&block)
@mouseReleasedBlock__ = block if block
nil
end
# Defines mouseMoved block.
#
+ # @return [nil] nil
+ #
def mouseMoved(&block)
@mouseMovedBlock__ = block if block
nil
end
# Defines mouseDragged block.
#
+ # @return [nil] nil
+ #
def mouseDragged(&block)
@mouseDraggedBlock__ = block if block
nil
end
+ # Defines mouseClicked block.
+ #
+ # @return [nil] nil
+ #
+ def mouseClicked(&block)
+ @mouseClickedBlock__ = block if block
+ nil
+ end
+
# Defines touchStarted block.
#
+ # @return [nil] nil
+ #
def touchStarted(&block)
@touchStartedBlock__ = block if block
nil
end
# Defines touchEnded block.
#
+ # @return [nil] nil
+ #
def touchEnded(&block)
@touchEndedBlock__ = block if block
nil
end
# Defines touchMoved block.
#
+ # @return [nil] nil
+ #
def touchMoved(&block)
@touchMovedBlock__ = block if block
nil
end
# Defines motion block.
#
+ # @return [nil] nil
+ #
def motion(&block)
@motionBlock__ = block if block
nil
end
@@ -2054,13 +2199,16 @@
# @private
def resizeCanvas__(name, width, height, pixelDensity)
raise '#{name}() must be called on startup or setup block' if @started__
@painter__.__send__ :end_paint
- @window__.__send__ :resize_canvas, width, height, pixelDensity
- updateCanvas__ @window__.canvas_image, @window__.canvas_painter
- @painter__.__send__ :begin_paint
+ begin
+ @window__.__send__ :resize_canvas, width, height, pixelDensity
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
+ ensure
+ @painter__.__send__ :begin_paint
+ end
@window__.auto_resize = false
end
# Returns pixel density of display.
@@ -2122,34 +2270,34 @@
# Returns mouse x position
#
# @return [Numeric] horizontal position of mouse
#
def mouseX()
- @pointerPos__[0]
+ @pointerPos__.x
end
# Returns mouse y position
#
# @return [Numeric] vertical position of mouse
#
def mouseY()
- @pointerPos__[1]
+ @pointerPos__.y
end
# Returns mouse x position in previous frame
#
# @return [Numeric] horizontal position of mouse
#
def pmouseX()
- @pointerPrevPos__[0]
+ @pointerPrevPos__.x
end
# Returns mouse y position in previous frame
#
# @return [Numeric] vertical position of mouse
#
def pmouseY()
- @pointerPrevPos__[1]
+ @pointerPrevPos__.y
end
# Returns which mouse button was pressed
#
# @return [Numeric] LEFT, RIGHT, CENTER or 0