From 569464a575df8797e8fcf1c4412e6c4e360e1f7f Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 18 Nov 2014 08:38:48 -0500 Subject: [PATCH] Support for built-in layouts, with examples --- lib/squib/deck.rb | 13 ++++++++--- lib/squib/layouts/hand.yml | 37 ++++++++++++++++++++++++++++++ lib/squib/layouts/playing-card.yml | 17 ++++++++++++++ samples/layouts.rb | 29 +++++++++++++++++++---- 4 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 lib/squib/layouts/hand.yml create mode 100644 lib/squib/layouts/playing-card.yml diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index d5bb1e0..0ed3289 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -112,9 +112,16 @@ module Squib @layout = {} Squib::logger.info { " using layout(s): #{files}" } Array(files).each do |file| - yml = @layout.merge(YAML.load_file(file)) - yml.each do |key, value| - @layout[key] = recurse_extends(yml, key, {}) + thefile = file + thefile = "#{File.dirname(__FILE__)}/layouts/#{file}" unless File.exists?(file) + if File.exists? thefile + yml = @layout.merge(YAML.load_file(thefile)) + yml.each do |key, value| + @layout[key] = recurse_extends(yml, key, {}) + end + else + puts "the file: #{thefile}" + Squib::logger.error { "Layout file not found: #{file}" } end end end diff --git a/lib/squib/layouts/hand.yml b/lib/squib/layouts/hand.yml new file mode 100644 index 0000000..0d02dc1 --- /dev/null +++ b/lib/squib/layouts/hand.yml @@ -0,0 +1,37 @@ +title: + x: 275 + y: 75 + width: 475 + height: 125 +art: + x: 275 + y: 225 + width: 475 + height: 475 +description: + x: 275 + y: 725 + width: 475 + height: 200 +snark: + x: 275 + y: 950 + width: 475 + height: 100 +bonus1: + x: 75 + y: 75 + width: 175 + height: 175 +bonus2: + extends: bonus1 + y: += 198 +bonus3: + extends: bonus2 + y: += 198 +bonus4: + extends: bonus3 + y: += 198 +bonus5: + extends: bonus4 + y: += 198 diff --git a/lib/squib/layouts/playing-card.yml b/lib/squib/layouts/playing-card.yml new file mode 100644 index 0000000..599a60f --- /dev/null +++ b/lib/squib/layouts/playing-card.yml @@ -0,0 +1,17 @@ +bonus_ul: + x: 75 + y: 75 + width: 200 + height: 200 +bonus_lr: + extends: bonus_ul + x: 750 + y: 1050 + angle: 3.14159 +art: + x: 150 + y: 275 + width: 525 + height: 575 + valign: middle + align: center diff --git a/samples/layouts.rb b/samples/layouts.rb index 98f0c21..ee5d018 100644 --- a/samples/layouts.rb +++ b/samples/layouts.rb @@ -14,9 +14,9 @@ Squib::Deck.new(layout: 'custom-layout.yml') do text str: 'The Title', layout: :title # Layouts also support YAML merge keys toreuse settings - svg file: 'spanner.svg', layout: :icon_left + svg file: 'spanner.svg', layout: :icon_left png file: 'shiny-purse.png', layout: :icon_middle - svg file: 'spanner.svg', layout: :icon_right + svg file: 'spanner.svg', layout: :icon_right # Squib has its own, richer merge key: "extends" rect fill_color: :black, layout: :bonus @@ -33,9 +33,28 @@ Squib::Deck.new(layout: 'custom-layout.yml') do save_png prefix: 'layout_' end -Squib::Deck.new(layout:['custom-layout.yml', 'custom-layout2.yml']) do - text str: 'The Title', layout: :title # from custom-layout.yml - text str: 'The Subtitle', layout: :subtitle # redefined in custom-layout2.yml +Squib::Deck.new(layout: ['custom-layout.yml', 'custom-layout2.yml']) do + text str: 'The Title', layout: :title # from custom-layout.yml + text str: 'The Subtitle', layout: :subtitle # redefined in custom-layout2.yml text str: 'The Description', layout: :description # from custom-layout2.yml save_png prefix: 'layout2_' end + +# Built-in layouts are easy to use and extend +Squib::Deck.new(layout: 'playing-card.yml') do + text str: "A\u2660", layout: :bonus_ul, font: 'Sans bold 100', hint: :red + text str: "A\u2660", layout: :bonus_lr, font: 'Sans bold 100', hint: :red + text str: "artwork here", layout: :art, hint: :red + save_png prefix: 'layout_builtin_playing_card_' +end + +# Built-in layouts are easy to use and extend +Squib::Deck.new(layout: 'hand.yml') do + %w(title bonus1 bonus2 bonus3 bonus4 bonus5 + description snark art).each do |icon| + text str: icon.capitalize, layout: icon, + hint: :red, valign: 'middle', align: 'center' + end + png file: 'pokercard.png', alpha: 0.5 + save_png prefix: 'layout_builtin_hand_' +end