From 468f13b5131d63b170296da23cfe8ead98272e52 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Sat, 12 Jul 2014 10:23:25 -0400 Subject: [PATCH] Got the example workingrake install && squib-eg --- .gitignore | 1 + bin/squib-eg | 5 ++-- lib/squib.rb | 46 +++++++++++++++++++++------------- lib/squib/card.rb | 2 +- lib/squib/deck.rb | 22 ++++++++++------ lib/squib/graphics/save.rb | 18 +++++++++++++ lib/squib/graphics/text.rb | 23 +++++++++++++++++ lib/squib/graphics/text_cmd.rb | 7 ------ 8 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 lib/squib/graphics/save.rb create mode 100644 lib/squib/graphics/text.rb delete mode 100644 lib/squib/graphics/text_cmd.rb diff --git a/.gitignore b/.gitignore index b27e929..c24a3a6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ tmp *.o *.a mkmf.log +_img \ No newline at end of file diff --git a/bin/squib-eg b/bin/squib-eg index c80d723..e5f7276 100644 --- a/bin/squib-eg +++ b/bin/squib-eg @@ -1,11 +1,12 @@ #!/usr/bin/env ruby - require 'squib' -deck width: 825, height: 1125, 3 +deck width: 825, height: 1125, cards: 3 data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} text str: data['name'], x: 10, y: 250 text str: data['level'], x: 10, y:10 + +save format: :png \ No newline at end of file diff --git a/lib/squib.rb b/lib/squib.rb index 3473af2..81ca8d3 100644 --- a/lib/squib.rb +++ b/lib/squib.rb @@ -1,46 +1,58 @@ -require 'squib/commands/text_cmd' +require 'squib/graphics/text' +require 'squib/graphics/save' +require 'squib/deck' +require 'squib/card' +require 'singleton' -class Squib - attr_accessor :the_deck - +module Squib + @@the_deck = nil + + def self.the_deck=(d) + @@the_deck = d + end + + def self.the_deck + @@the_deck + end end ################## ### PUBLIC API ### ################## -def deck(width:, height:, cards: 1) - Squib.the_deck = Deck.new(width, height, cards) +def deck(width: , height: , cards: 1) + Squib::the_deck = Squib::Deck.new(width, height, cards) end def font(type: , size: 12, **options) - Font.new() end def set_font(type: 'Arial', size: 12, **options) - Squib::queue_command Squib::Commands::SetFont.new(type,size,options) + Squib::queue_command Squib::Graphics::SetFont.new(type,size,options) end def text(range=:all, str: , font: :use_set, x: 0, y: 0, **options) - range = 0..cards-1 if range == :all - str = [str] * cards unless str.respond_to? :each - #TODO define a singleton or something for the deck we're working on. - str.each{ |s| Squib::Graphics::Text.new(card, s, font, x, y, options) } + deck = Squib::the_deck + range = 0..(deck.num_cards-1) if range == :all + str = [str] * deck.num_cards unless str.respond_to? :each + range.each do |i| + Squib::Graphics::Text.new(deck[i], str[i], font, x, y, options).execute end end def image(range=:all, file: , x: 0, y: 0) end +def rect(x: , y: width: , height: , x_radius: 0, y_radius: 0) + +end + def load_csv(file:, header: true) end def data(field) end -def render - vv = VerifyVisitor.new - CMDS.each do |cmd| - cmd.accept(vv) - end +def save(format: :png) + Squib::Graphics::Save.new(format).execute end \ No newline at end of file diff --git a/lib/squib/card.rb b/lib/squib/card.rb index 30c9f63..e4652c6 100644 --- a/lib/squib/card.rb +++ b/lib/squib/card.rb @@ -3,7 +3,7 @@ require 'cairo' module Squib class Card - attr_reader :width:, :height + attr_reader :width, :height attr_accessor :cairo_surface, :cairo_context def initialize(width, height) diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 765712d..9343c5d 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -2,15 +2,23 @@ require 'squib/card' module Squib class Deck - attr_reader :width, :height, :cards - attr_accessor :deck + include Enumerable + attr_reader :width, :height, :num_cards - def initialize(width: 825, height: 1125, cards: 1) - @width=width; @height=height; @cards=cards - @deck = Array.new(cards) - (1..cards).each{ @deck << Squib::Card.new(width, height) } + def initialize(width, height, cards) + @width=width; @height=height; @num_cards=cards + @cards = [] + num_cards.times{ @cards << Squib::Card.new(width, height) } end - end + def [](key) + @cards[key] + end + #For enumerables + def each(&block) + @cards.each { |card| block.call(card) } + end + + end end \ No newline at end of file diff --git a/lib/squib/graphics/save.rb b/lib/squib/graphics/save.rb new file mode 100644 index 0000000..e0a9667 --- /dev/null +++ b/lib/squib/graphics/save.rb @@ -0,0 +1,18 @@ +module Squib + module Graphics + + class Save + def initialize(format) + @format = format + end + + def execute + puts "Here!" + Squib.the_deck.each_with_index do |card, i| + card.cairo_context.target.write_to_png("_img/img_#{i}.png") + end + end + end + + end +end \ No newline at end of file diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb new file mode 100644 index 0000000..939cf1e --- /dev/null +++ b/lib/squib/graphics/text.rb @@ -0,0 +1,23 @@ +module Squib + module Graphics + + class Text + def initialize(card, str, font, x, y, options) + @card=card + @str=str + @font=font + @x=x; @y=y + end + + def execute + cc = @card.cairo_context + cc.set_source_rgb(0.0,0.0,0.0) + cc.select_font_face ("Helvetica"); + cc.set_font_size(36) + cc.move_to(@x,@y) + cc.show_text(@str.to_s) + end + end + + end +end \ No newline at end of file diff --git a/lib/squib/graphics/text_cmd.rb b/lib/squib/graphics/text_cmd.rb deleted file mode 100644 index 2b5e9ed..0000000 --- a/lib/squib/graphics/text_cmd.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Squib - - class TextCmd - def initialize(range, str, ) - end - -end \ No newline at end of file