lib/processing/window.rb in processing-0.5.32 vs lib/processing/window.rb in processing-0.5.33
- old
+ new
@@ -4,18 +4,10 @@
# @private
class Window < Reflex::Window
include Xot::Inspectable
- attr_accessor :setup, :update, :draw,
- :key_down, :key_up,
- :pointer_down, :pointer_up, :pointer_move,
- :move, :resize, :motion,
- :before_draw, :after_draw, :update_window, :update_canvas
-
- attr_accessor :auto_resize
-
def initialize(width = 500, height = 500, *args, **kwargs, &block)
Processing.instance_variable_set :@window, self
@events = []
@active = false
@@ -24,23 +16,20 @@
@canvas = Canvas.new self, width, height
@canvas_view = add CanvasView.new name: :canvas
@overlay_view = @canvas_view.add Reflex::View.new name: :overlay
super(*args, size: [width, height], **kwargs, &block)
+ self.center = screen.center
end
- def canvas_image()
- @canvas.image
- end
+ attr_accessor :setup, :update, :draw, :move, :resize, :motion,
+ :key_down, :key_up, :pointer_down, :pointer_up, :pointer_move, :wheel,
+ :before_draw, :after_draw, :update_window, :update_canvas
- def canvas_painter()
- @canvas.painter
- end
+ attr_accessor :auto_resize
- def window_painter()
- self.painter
- end
+ attr_reader :canvas
def event()
@events.last
end
@@ -62,11 +51,13 @@
def on_setup()
call_block @setup, nil
end
def on_change_pixel_density(pixel_density)
- resize_canvas width, height, window_pixel_density: pixel_density
+ resize_canvas(
+ @canvas.width, @canvas.height,
+ window_pixel_density: pixel_density)
end
def on_activate(e)
@active = true
end
@@ -78,11 +69,11 @@
def on_update(e)
draw_canvas {call_block @update_window, e} if @update_window
end
def on_draw(e)
- window_painter.pixel_density.tap do |pd|
+ painter.pixel_density.tap do |pd|
prev, @prev_pixel_density = @prev_pixel_density, pd
on_change_pixel_density pd if prev && pd != prev
end
update_canvas_view
end
@@ -125,46 +116,53 @@
when :move then @pointer_move
end
draw_canvas {call_block block, e} if block
end
+ def on_canvas_wheel(e)
+ draw_canvas {call_block @wheel, e} if @wheel
+ end
+
def on_canvas_resize(e)
resize_canvas e.width, e.height if @auto_resize
draw_canvas {call_block @resize, e} if @resize
end
- def resize_canvas(width, height, pixel_density = nil, window_pixel_density: nil)
- painting = canvas_painter.painting?
- canvas_painter.__send__ :end_paint if painting
+ def resize_canvas(
+ width, height,
+ pixel_density = nil,
+ window_pixel_density: nil,
+ antialiasing: nil)
+ painting = @canvas.painter.painting?
+ @canvas.painter.__send__ :end_paint if painting
+
@pixel_density = pixel_density if pixel_density
resized =
begin
- @canvas.resize width, height, @pixel_density || window_pixel_density
+ pd = @pixel_density || window_pixel_density
+ @canvas.resize width, height, pd, antialiasing
ensure
- canvas_painter.__send__ :begin_paint if painting
+ @canvas.painter.__send__ :begin_paint if painting
end
- if resized
- @update_canvas.call canvas_image, canvas_painter if @update_canvas
- size width, height
- end
+ @update_canvas&.call @canvas.image, @canvas.painter if resized
end
private
def update_canvas_view()
scrollx, scrolly, zoom = get_scroll_and_zoom
@canvas_view.scroll_to scrollx, scrolly
@canvas_view.zoom = zoom
- @overlay_view.size = canvas_image.size
+ @overlay_view.size = @canvas.image.size
end
def get_scroll_and_zoom()
- ww, wh = width.to_f, height.to_f
- cw, ch = canvas_image.width.to_f, canvas_image.height.to_f
+ ww, wh = width.to_f, height.to_f
+ cw, ch = @canvas.image.width.to_f, @canvas.image.height.to_f
return [0, 0, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
wratio, cratio = ww / wh, cw / ch
if wratio >= cratio
scaled_w = wh * cratio
@@ -182,25 +180,25 @@
ensure
end_draw unless drawing
end
def begin_draw()
- canvas_painter.__send__ :begin_paint
+ @canvas.painter.__send__ :begin_paint
@before_draw&.call
end
def end_draw()
@after_draw&.call
- canvas_painter.__send__ :end_paint
+ @canvas.painter.__send__ :end_paint
end
def drawing?()
- canvas_painter.painting?
+ @canvas.painter.painting?
end
def draw_screen(painter)
- window_painter.image canvas_image
+ painter.image @canvas.render
end
def call_block(block, event, *args)
@events.push event
block.call event, *args if block && !@error
@@ -214,69 +212,109 @@
end# Window
class Window::Canvas
- attr_reader :image, :painter
-
def initialize(window, width, height)
- @image = nil
- @painter = window.painter
+ @framebuffer = nil
+ @paintable = nil
+ @painter = window.painter
+ @painter.miter_limit = 10
+
resize width, height
- painter.miter_limit = 10
end
- def resize(width, height, pixel_density = nil)
+ attr_reader :painter
+
+ def resize(width, height, pixel_density = nil, antialiasing = nil)
return false if width <= 0 || height <= 0
+ cs = @framebuffer&.color_space || Rays::RGBA
+ pd = pixel_density || (@framebuffer || @painter).pixel_density
+ aa = antialiasing == nil ? antialiasing? : (antialiasing && pd < 2)
return false if
- width == @image&.width &&
- height == @image&.height &&
- pixel_density == @painter.pixel_density
+ width == @framebuffer&.width &&
+ height == @framebuffer&.height &&
+ pd == @framebuffer&.pixel_density &&
+ aa == antialiasing?
- old_image = @image
- old_painter = @painter
- cs = old_image&.color_space || Rays::RGBA
- pd = pixel_density || old_painter.pixel_density
+ old_paintable, old_painter = @paintable, @painter
- @image = Rays::Image.new width, height, cs, pd
- @painter = @image.painter
+ @framebuffer = Rays::Image.new width, height, cs, pd
+ @paintable = aa ? Rays::Image.new(width, height, cs, pd * 2) : @framebuffer
+ @painter = @paintable.painter
- @painter.paint {image old_image} if old_image
+ @painter.paint {image old_paintable} if old_paintable
copy_painter old_painter, @painter
GC.start
return true
end
+ def render()
+ @framebuffer.paint {|p| p.image @paintable} if antialiasing?
+ @framebuffer
+ end
+
+ def image()
+ @paintable
+ end
+
+ def width()
+ @framebuffer.width
+ end
+
+ def height()
+ @framebuffer.height
+ end
+
+ def pixel_density()
+ @framebuffer.pixel_density
+ end
+
+ def antialiasing?()
+ !!@framebuffer && !!@paintable && @framebuffer != @paintable
+ end
+
private
def copy_painter(from, to)
- to.fill = from.fill
- to.stroke = from.stroke
- to.stroke_width = from.stroke_width
- to.stroke_cap = from.stroke_cap
- to.stroke_join = from.stroke_join
- to.miter_limit = from.miter_limit
- to.font = from.font
+ to.fill = from.fill
+ to.stroke = from.stroke
+ to.stroke_width = from.stroke_width
+ to.stroke_cap = from.stroke_cap
+ to.stroke_join = from.stroke_join
+ to.miter_limit = from.miter_limit
+ to.clip = from.clip
+ to.blend_mode = from.blend_mode
+ to.font = from.font
+ to.texture = from.texture
+ to.texcoord_mode = from.texcoord_mode
+ to.texcoord_wrap = from.texcoord_wrap
+ to.shader = from.shader
end
end# Window::Canvas
class Window::CanvasView < Reflex::View
def on_update(e)
window.on_canvas_update e
+ Thread.pass
end
def on_draw(e)
window.on_canvas_draw e
end
def on_pointer(e)
window.on_canvas_pointer e
+ end
+
+ def on_wheel(e)
+ window.on_canvas_wheel e
end
def on_resize(e)
window.on_canvas_resize e
end