Browse Source

Convert hand method to args classes

dev
Andy Meneely 10 years ago
parent
commit
4d1661ac0b
  1. 9
      lib/squib/api/save.rb
  2. 37
      lib/squib/args/hand_special.rb
  3. 4
      lib/squib/args/sheet.rb
  4. 1
      lib/squib/args/showcase_special.rb
  5. 23
      lib/squib/graphics/hand.rb
  6. 4
      spec/data/samples/hand.rb.txt

9
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

37
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

4
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

1
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

23
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

4
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])

Loading…
Cancel
Save