lib/rays/painter.rb in rays-0.1.12 vs lib/rays/painter.rb in rays-0.1.13

- old
+ new

@@ -1,139 +1,102 @@ # -*- coding: utf-8 -*- +require 'xot/universal_accessor' require 'xot/block_util' require 'rays/ext' -require 'rays/color' module Rays class Painter - def background (*args, &block) - set_or_get :background=, :get_background, args, block - end + def push (*types, **attributes, &block) + each_type types do |type| + case type + when :state then push_state + when :matrix then push_matrix + else raise ArgumentError, "invalid push/pop type '#{type}'." + end + end - def background= (*args) - send_set :set_background, :get_background, :no_background, args, :color - end + raise ArgumentError, 'missing block with pushing attributes.' if + !attributes.empty? && !block - def fill (*args, &block) - set_or_get :fill=, :get_fill, args, block - end + if block + attributes.each do |key, value| + attributes[key] = __send__ key + __send__ key, *value + end - def fill= (*args) - send_set :set_fill, :get_fill, :no_fill, args, :color - end + Xot::BlockUtil.instance_eval_or_block_call self, &block - def stroke (*args, &block) - set_or_get :stroke=, :get_stroke, args, block - end + attributes.each do |key, value| + __send__ key, *value + end - def stroke= (*args) - send_set :set_stroke, :get_stroke, :no_stroke, args, :color + pop *types + end end - def clip (*args, &block) - set_or_get :clip=, :get_clip, args, block + def pop (*types) + each_type types, reverse: true do |type| + case type + when :state then pop_state + when :matrix then pop_matrix + else raise ArgumentError, "invalid push/pop type '#{type}'." + end + end end - def clip= (*args) - send_set :set_clip, :get_clip, :no_clip, args + def paint (*args, &block) + begin_paint + Xot::BlockUtil.instance_eval_or_block_call self, *args, &block + self + ensure + end_paint end - def font (*args, &block) - set_or_get :font=, :get_font, args, block + def line (*args, loop: false) + if args.first.kind_of?(Polyline) + draw_polyline args.first + else + draw_line args, loop + end end - def font= (*args) - send_set :set_font, :get_font, nil, args + def rect (*args, round: nil, lt: nil, rt: nil, lb: nil, rb: nil) + draw_rect args, round, lt, rt, lb, rb end - def color (fill, stroke = nil, &block) - org = [self.fill, self.stroke] - self.fill, self.stroke = fill, stroke - if block - Xot::BlockUtil.instance_eval_or_block_call self, &block - self.fill, self.stroke = org - end - org + def ellipse (*args, center: nil, radius: nil, hole: nil, from: nil, to: nil) + draw_ellipse args, center, radius, hole, from, to end - def attach (shader, uniforms = {}) - shader = - case shader - when Shader then shader - when String then Shader.new shader - else raise ArgumentError - end - attach_shader shader - uniforms.each {|name, args| set_uniform name, *args} - shader + def color= (fill, stroke = nil) + self.fill fill + self.stroke stroke end - alias detach detach_shader - - def push (&block) - push_matrix - push_attr - push_shader - if block - Xot::BlockUtil.instance_eval_or_block_call self, &block - pop - end + def color () + return fill, stroke end - def pop () - pop_shader - pop_attr - pop_matrix + def shader= (shader, **uniforms) + shader.uniform **uniforms if shader && !uniforms.empty? + set_shader shader end - def begin (*args, &block) - begin_paint - Xot::BlockUtil.instance_eval_or_block_call self, *args, &block - end_paint - end + universal_accessor :background, :fill, :stroke, :color, + :stroke_width, :nsegment, :shader, :clip, :font private - NONES = [:no, :none, nil] - - def send_set (setter, getter, no, args, mode = nil) - args = args[0] if args[0].kind_of? Array - raise ArgumentError if args.empty? - - arg0 = args[0] - if args.size == 1 && NONES.include?(arg0) - no ? send(no) : send(setter, nil) - else - case mode - when :color then args = Color.color *args - end - send setter, *args - end - send getter - end - - def set_or_get (setter, getter, args, block) - unless args.empty? - set_or_push setter, getter, args, block - else - send getter - end - end - - def set_or_push (setter, getter, args, block) - org = send getter - send setter, *args - if block - Xot::BlockUtil.instance_eval_or_block_call self, &block - send setter, org - end - org + def each_type (types, reverse: false, &block) + types = [:state, :matrix] if types.empty? || types.include?(:all) + types = types.reverse if reverse + types.each &block end end# Painter