From 4d1661ac0b153329249b26e7dbdc034ff6f91ac9 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Sat, 15 Aug 2015 00:17:29 -0400 Subject: [PATCH] Convert hand method to args classes --- lib/squib/api/save.rb | 9 +++++--- lib/squib/args/hand_special.rb | 37 ++++++++++++++++++++++++++++++ lib/squib/args/sheet.rb | 4 ++++ lib/squib/args/showcase_special.rb | 1 + lib/squib/graphics/hand.rb | 23 +++++++++---------- spec/data/samples/hand.rb.txt | 4 ++-- 6 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 lib/squib/args/hand_special.rb diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb index 941f294..f3acd8f 100644 --- a/lib/squib/api/save.rb +++ b/lib/squib/api/save.rb @@ -1,4 +1,5 @@ require 'squib/args/card_range' +require 'squib/args/hand_special' require 'squib/args/save_batch' require 'squib/args/sheet' require 'squib/args/showcase_special' @@ -138,13 +139,15 @@ module Squib # @return [nil] Returns nothing. # @api public def hand(opts = {}) + range = Args::CardRange.new(opts[:range], deck_size: size) + hand = Args::HandSpecial.new(height).load!(opts, expand_by: size, layout: layout, dpi: dpi) + sheet = Args::Sheet.new(custom_colors, {file: 'hand.png', trim_radius: 0}).load!(opts, expand_by: size, layout: layout, dpi: dpi) + opts = {file: 'hand.png', fill_color: :white, radius: :auto, trim_radius: 0} .merge(opts) opts = needs(opts,[:range, :margin, :trim, :trim_radius, :creatable_dir, :file_to_save]) opts[:radius] = 0.3 * height if opts[:radius] == :auto - render_hand(opts[:range], opts[:radius], opts[:angle_range], - opts[:trim], opts[:trim_radius], opts[:margin], - opts[:fill_color], opts[:dir], opts[:file]) + render_hand(range, sheet, hand) end end diff --git a/lib/squib/args/hand_special.rb b/lib/squib/args/hand_special.rb new file mode 100644 index 0000000..672d7d2 --- /dev/null +++ b/lib/squib/args/hand_special.rb @@ -0,0 +1,37 @@ +require 'cairo' + +module Squib + # @api private + module Args + + class HandSpecial + include ArgLoader + + def initialize(card_height) + @card_height = card_height + end + + def self.parameters + { + angle_range: (Math::PI / -4.0)..(Math::PI / 4), + radius: :auto + } + end + + def self.expanding_parameters + [] # none of them + end + + def self.params_with_units + [ :radius ] + end + + def validate_radius(arg) + return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto' + arg + end + + end + + end +end diff --git a/lib/squib/args/sheet.rb b/lib/squib/args/sheet.rb index c7774f3..dc90efc 100644 --- a/lib/squib/args/sheet.rb +++ b/lib/squib/args/sheet.rb @@ -62,6 +62,10 @@ module Squib (@deck_size.to_i / columns.to_i).ceil end + def full_filename + "#{dir}/#{file}" + end + end end diff --git a/lib/squib/args/showcase_special.rb b/lib/squib/args/showcase_special.rb index 04c26da..c393758 100644 --- a/lib/squib/args/showcase_special.rb +++ b/lib/squib/args/showcase_special.rb @@ -1,5 +1,6 @@ require 'cairo' require 'squib/args/arg_loader' +require 'squib/args/dir_validator' module Squib # @api private diff --git a/lib/squib/graphics/hand.rb b/lib/squib/graphics/hand.rb index 7b3afdc..3fe9ca2 100644 --- a/lib/squib/graphics/hand.rb +++ b/lib/squib/graphics/hand.rb @@ -5,25 +5,24 @@ module Squib # Draw cards in a fan. # @api private - def render_hand(range, radius, angle_range, trim, trim_radius, margin, - fill_color, dir, file) + def render_hand(range, sheet, hand) cards = range.collect { |i| @cards[i] } center_x = width / 2.0 - center_y = radius + height + center_y = hand.radius + height out_size = 3.0 * center_y - angle_delta = (angle_range.last - angle_range.first) / cards.size + angle_delta = (hand.angle_range.last - hand.angle_range.first) / cards.size cxt = Cairo::Context.new(Cairo::RecordingSurface.new(0, 0, out_size, out_size)) cxt.translate(out_size / 2.0, out_size / 2.0) - cxt.rotate(angle_range.first) + cxt.rotate(hand.angle_range.first) cxt.translate(-width, -width) cards.each_with_index do |card, i| cxt.translate(center_x, center_y) cxt.rotate(angle_delta) cxt.translate(-center_x, -center_y) card.use_cairo do |card_cxt| - cxt.rounded_rectangle(trim, trim, - width - (2 * trim), height - (2 * trim), - trim_radius, trim_radius) + cxt.rounded_rectangle(sheet.trim, sheet.trim, + width - (2 * sheet.trim), height - (2 * sheet.trim), + sheet.trim_radius, sheet.trim_radius) cxt.clip cxt.set_source(card_cxt.target) cxt.paint @@ -31,13 +30,13 @@ module Squib end end x, y, w, h = cxt.target.ink_extents # I love Ruby assignment ;) - png_cxt = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(Cairo::ImageSurface.new(w + 2*margin, h + 2*margin))) - png_cxt.set_source_squibcolor(fill_color) + png_cxt = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(Cairo::ImageSurface.new(w + 2*sheet.margin, h + 2*sheet.margin))) + png_cxt.set_source_squibcolor(sheet.fill_color) png_cxt.paint - png_cxt.translate(-x + margin, -y + margin) + png_cxt.translate(-x + sheet.margin, -y + sheet.margin) png_cxt.set_source(cxt.target) png_cxt.paint - png_cxt.target.write_to_png("#{dir}/#{file}") + png_cxt.target.write_to_png sheet.full_filename end end end diff --git a/spec/data/samples/hand.rb.txt b/spec/data/samples/hand.rb.txt index 16ec461..aea593c 100644 --- a/spec/data/samples/hand.rb.txt +++ b/spec/data/samples/hand.rb.txt @@ -441,7 +441,7 @@ cairo: set_source([MockDouble]) cairo: paint([]) cairo: reset_clip([]) cairo: restore([]) -cairo: set_source_color([:white]) +cairo: set_source_color(["white"]) cairo: paint([]) cairo: translate([75, 75]) cairo: set_source([MockDouble]) @@ -530,7 +530,7 @@ cairo: set_source([MockDouble]) cairo: paint([]) cairo: reset_clip([]) cairo: restore([]) -cairo: set_source_color([:white]) +cairo: set_source_color(["white"]) cairo: paint([]) cairo: translate([75, 75]) cairo: set_source([MockDouble])