Class: Squib::Deck
- Inherits:
-
Object
- Object
- Squib::Deck
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/squib/deck.rb,
lib/squib/api/data.rb,
lib/squib/api/save.rb,
lib/squib/api/text.rb,
lib/squib/api/image.rb,
lib/squib/api/units.rb,
lib/squib/api/shapes.rb,
lib/squib/api/settings.rb,
lib/squib/graphics/hand.rb,
lib/squib/api/background.rb,
lib/squib/graphics/save_doc.rb,
lib/squib/graphics/showcase.rb
Overview
The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
Instance Method Summary collapse
-
#background(opts = {}) ⇒ nil
Fills the background with the given color Options support Arrays, see Arrays and Singleon Expansion.
-
#circle(opts = {}) ⇒ nil
Draw a circle centered at the given coordinates.
-
#cm(n) ⇒ Decimal
Given cm, returns the number of pixels according to the deck's DPI.
-
#csv(opts = {}) ⇒ Object
Convenience call on deck goes to the module function.
-
#curve(opts = {}) ⇒ nil
Draw a curve using the given coordinates.
-
#ellipse(opts = {}) ⇒ nil
Draw an ellipse.
-
#hand(opts = {}) ⇒ nil
Renders a range of cards fanned out as if in a hand.
-
#hint(text: :off) ⇒ nil
Toggle hints globally.
-
#inches(n) ⇒ Decimal
Given inches, returns the number of pixels according to the deck's DPI.
-
#initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) ⇒ Deck
constructor
Squib's constructor that sets the immutable properties.
-
#line(opts = {}) ⇒ nil
Draw a line using the given coordinates.
-
#png(opts = {}) ⇒ nil
Renders a png file at the given location.
-
#polygon(opts = {}) ⇒ nil
Draw a regular polygon at the given x,y.
-
#rect(opts = {}) ⇒ nil
Draw a rounded rectangle.
-
#save(opts = {}) ⇒ Object
Saves the given range of cards to either PNG or PDF.
-
#save_pdf(opts = {}) ⇒ nil
Lays out the cards in range and renders a PDF.
-
#save_png(opts = {}) ⇒ nil
Saves the given range of cards to a PNG.
-
#save_sheet(opts = {}) ⇒ nil
Lays out the cards in range and renders a stitched PNG sheet.
-
#set(opts = {}) ⇒ nil
Sets various defaults for this deck.
-
#showcase(opts = {}) ⇒ nil
Renders a range of cards in a showcase as if they are sitting in 3D on a reflective surface See showcase for full example.
-
#star(opts = {}) ⇒ nil
Draw a star at the given x,y.
-
#svg(opts = {}) ⇒ nil
Renders an entire svg file at the given location.
-
#text(opts = {}) {|embed| ... } ⇒ Array
Renders a string at a given location, width, alignment, font, etc.
-
#triangle(opts = {}) ⇒ nil
Draw a triangle using the given coordinates.
-
#xlsx(opts = {}) ⇒ Object
Convenience call on deck goes to the module function.
Constructor Details
#initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) ⇒ Deck
Squib's constructor that sets the immutable properties.
This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
The documented methods in Deck are the ones intended for use by most users.
If your game requires multiple different sizes or orientations, I recommend using multiple Squib::Decks in your deck.rb. You can modify the internals of Squib::Deck (e.g. @cards), but that's not recommended.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/squib/deck.rb', line 61 def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) @dpi = dpi @font = SYSTEM_DEFAULTS[:default_font] @cards = [] @conf = Conf.load(config) @progress_bar = Progress.new(@conf.) # FIXME this is evil. Using something different with @ and non-@ show_info(config, layout) @width = Args::UnitConversion.parse width, dpi @height = Args::UnitConversion.parse height, dpi cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) } @layout = LayoutParser.load_layout(layout) if block_given? instance_eval(&block) # here we go. wheeeee! end end |
Instance Method Details
#background(opts = {}) ⇒ nil
Fills the background with the given color Options support Arrays, see Arrays and Singleon Expansion
13 14 15 16 17 |
# File 'lib/squib/api/background.rb', line 13 def background(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].background(draw.color[i]) } end |
#circle(opts = {}) ⇒ nil
Draw a circle centered at the given coordinates
Options support Arrays, see Arrays and Singleon Expansion
60 61 62 63 64 65 |
# File 'lib/squib/api/shapes.rb', line 60 def circle(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].circle(coords[i], draw[i]) } end |
#cm(n) ⇒ Decimal
Given cm, returns the number of pixels according to the deck's DPI.
26 27 28 |
# File 'lib/squib/api/units.rb', line 26 def cm(n) @dpi * Squib::INCHES_IN_CM * n.to_f end |
#csv(opts = {}) ⇒ Object
Convenience call on deck goes to the module function
95 96 97 |
# File 'lib/squib/api/data.rb', line 95 def csv(opts = {}) Squib.csv(opts) end |
#curve(opts = {}) ⇒ nil
Draw a curve using the given coordinates
168 169 170 171 172 173 |
# File 'lib/squib/api/shapes.rb', line 168 def curve(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].curve(coords[i], draw[i]) } end |
#ellipse(opts = {}) ⇒ nil
Draw an ellipse
Options support Arrays, see Arrays and Singleon Expansion
87 88 89 90 91 92 |
# File 'lib/squib/api/shapes.rb', line 87 def ellipse(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) box = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].ellipse(box[i], draw[i]) } end |
#hand(opts = {}) ⇒ nil
Renders a range of cards fanned out as if in a hand. Saves as PNG. See hand for full example
141 142 143 144 145 146 |
# File 'lib/squib/api/save.rb', line 141 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) render_hand(range, sheet, hand) end |
#hint(text: :off) ⇒ nil
Toggle hints globally.
Text hints are rectangles around where the text will be laid out. They are intended to be temporary. Setting a hint to nil or to :off will disable hints. @see samples/text.rb
15 16 17 |
# File 'lib/squib/api/settings.rb', line 15 def hint(text: :off) conf.text_hint = text end |
#inches(n) ⇒ Decimal
Given inches, returns the number of pixels according to the deck's DPI.
14 15 16 |
# File 'lib/squib/api/units.rb', line 14 def inches(n) @dpi * n.to_f end |
#line(opts = {}) ⇒ nil
Draw a line using the given coordinates
141 142 143 144 145 146 |
# File 'lib/squib/api/shapes.rb', line 141 def line(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].line(coords[i], draw[i]) } end |
#png(opts = {}) ⇒ nil
Renders a png file at the given location.
See samples/image.rb and samples/tgc-overlay.rb as examples.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/squib/api/image.rb', line 30 def png(opts = {}) Dir.chdir(img_dir) do range = Args::CardRange.new(opts[:range], deck_size: size) paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout) box = Args::ScaleBox.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) @progress_bar.start('Loading PNG(s)', range.size) do || range.each do |i| @cards[i].png(ifile[i].file, box[i], paint[i], trans[i]) .increment end end end end |
#polygon(opts = {}) ⇒ nil
Draw a regular polygon at the given x,y
221 222 223 224 225 226 227 |
# File 'lib/squib/api/shapes.rb', line 221 def polygon(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].polygon(coords[i], trans[i], draw[i]) } end |
#rect(opts = {}) ⇒ nil
Draw a rounded rectangle
Options support Arrays, see Arrays and Singleon Expansion
34 35 36 37 38 39 |
# File 'lib/squib/api/shapes.rb', line 34 def rect(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) box = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].rect(box[i], draw[i]) } end |
#save(opts = {}) ⇒ Object
Saves the given range of cards to either PNG or PDF
20 21 22 23 24 |
# File 'lib/squib/api/save.rb', line 20 def save(opts = {}) save_png(opts) if Array(opts[:format]).include? :png save_pdf(opts) if Array(opts[:format]).include? :pdf self end |
#save_pdf(opts = {}) ⇒ nil
Lays out the cards in range and renders a PDF
40 41 42 43 44 |
# File 'lib/squib/api/save.rb', line 40 def save_pdf(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) sheet = Args::Sheet.new(custom_colors, {file: 'output.pdf'}).load!(opts, expand_by: size, layout: layout, dpi: dpi) render_pdf(range, sheet) end |
#save_png(opts = {}) ⇒ nil
Saves the given range of cards to a PNG
Options support Arrays, see Arrays and Singleon Expansion
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/squib/api/save.rb', line 60 def save_png(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) @progress_bar.start("Saving PNGs to #{batch.summary}", size) do || range.each do |i| @cards[i].save_png(batch[i]) .increment end end end |
#save_sheet(opts = {}) ⇒ nil
Lays out the cards in range and renders a stitched PNG sheet
87 88 89 90 91 92 |
# File 'lib/squib/api/save.rb', line 87 def save_sheet(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) sheet = Args::Sheet.new(custom_colors, {margin: 0}, size).load!(opts, expand_by: size, layout: layout, dpi: dpi) render_sheet(range, batch, sheet) end |
#set(opts = {}) ⇒ nil
Sets various defaults for this deck. Defaults can be overriden by the commands themselves when that command supports it.
29 30 31 32 |
# File 'lib/squib/api/settings.rb', line 29 def set(opts = {}) raise 'DEPRECATED: As of v0.7 img_dir is no longer supported in "set". Use config.yml instead.' if opts.key? :img_dir @font = (opts[:font] == :default) ? Squib::SYSTEM_DEFAULTS[:default_font] : opts[:font] end |
#showcase(opts = {}) ⇒ nil
Renders a range of cards in a showcase as if they are sitting in 3D on a reflective surface See showcase for full example
115 116 117 118 119 120 |
# File 'lib/squib/api/save.rb', line 115 def showcase(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) showcase = Args::ShowcaseSpecial.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) sheet = Args::Sheet.new(custom_colors, {file: 'showcase.png'}).load!(opts, expand_by: size, layout: layout, dpi: dpi) render_showcase(range, sheet, showcase) end |
#star(opts = {}) ⇒ nil
Draw a star at the given x,y
194 195 196 197 198 199 200 |
# File 'lib/squib/api/shapes.rb', line 194 def star(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].star(coords[i], trans[i], draw[i]) } end |
#svg(opts = {}) ⇒ nil
Renders an entire svg file at the given location. Uses the SVG-specified units and DPI to determine the pixel width and height. If neither data nor file are specified for a given card, this method does nothing.
See samples/load-images.rb and samples/tgc-overlay.rb as examples.
Note: if alpha transparency is desired, set that in the SVG.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/squib/api/image.rb', line 70 def svg(opts = {}) Dir.chdir(img_dir) do range = Args::CardRange.new(opts[:range], deck_size: size) paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout) box = Args::ScaleBox.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) svg_args = Args::SvgSpecial.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) @progress_bar.start('Loading SVG(s)', range.size) do || range.each do |i| if svg_args.render?(i) @cards[i].svg(ifile[i].file, svg_args[i], box[i], paint[i], trans[i]) end .increment end end end end |
#text(opts = {}) {|embed| ... } ⇒ Array
Renders a string at a given location, width, alignment, font, etc.
Unix-like newlines are interpreted even on Windows. See the samples/text.rb for a lengthy example.
Options support Arrays, see Arrays and Singleon Expansion
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/squib/api/text.rb', line 51 def text(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) para = Args::Paragraph.new(font).load!(opts, expand_by: size, layout: layout) box = Args::Box.new(self, {width: :auto, height: :auto}).load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) draw = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi) = TextEmbed.new(size, custom_colors, layout, dpi, img_dir) yield() if block_given? #store the opts for later use extents = Array.new(@cards.size) range.each { |i| extents[i] = @cards[i].text(, para[i], box[i], trans[i], draw[i]) } return extents end |
#triangle(opts = {}) ⇒ nil
Draw a triangle using the given coordinates
Options support Arrays, see Arrays and Singleon Expansion
116 117 118 119 120 121 |
# File 'lib/squib/api/shapes.rb', line 116 def triangle(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi) coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) range.each { |i| @cards[i].triangle(coords[i], draw[i]) } end |