Module: Drakkon::Images::SpriteSheetCombine
- Defined in:
- lib/drakkon/lib/images/spritesheet.rb
Overview
General Image Index Helper
Class Method Summary collapse
- .dimensions(_file) ⇒ Object
- .image_size(file) ⇒ Object
- .images ⇒ Object
- .process(img_name, grid) ⇒ Object
- .prompt ⇒ Object
- .run!(args = []) ⇒ Object
- .sorted_images ⇒ Object
Class Method Details
.dimensions(_file) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 77 def self.dimensions(_file) # img = MiniMagick::Image.open(file) # { # w: img.width, # h: img.height # } index = images.map do |img| image_size(img) end w = index.max_by(&:w).w h = index.max_by(&:h).h { w: w, h: h } end |
.image_size(file) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 95 def self.image_size(file) # LogBot.info('Image Biggest', file) img = MiniMagick::Image.open(file) { w: img.width, h: img.height } end |
.images ⇒ Object
105 106 107 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 105 def self.images @images ||= sorted_images end |
.process(img_name, grid) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 55 def self.process(img_name, grid) LogBot.info('Image Spritesheet', img_name) # Find Largest img = dimensions(images) img_name += '.png' LogBot.info('Image Spritesheet', "Dimensions: #{img[:w]}x#{img[:h]}") montage = MiniMagick::Tool::Montage.new images.each do |imgn| montage << imgn end montage.geometry "#{img.w}x#{img.h}+0+0" montage.tile grid unless grid.nil? montage.background 'none' montage << "#{Dir.pwd}/#{img_name}" montage.call end |
.prompt ⇒ Object
117 118 119 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 117 def self.prompt TTY::Prompt.new(active_color: :cyan, interrupt: :exit) end |
.run!(args = []) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 5 def self.run!(args = []) # All images exist? unless images.all? { |x| File.exist?(x) } || LogBot.fatal('SpriteSheetCombine', 'Invalid Images - Files must be numbered. e.g. 1.png, 2.png, 3.png') exit 1 end img_name = if args.empty? prompt.ask('New File Name? (e.g. rawr): ') else args.shift end grid = if args.empty? prompt.ask('Grid? (WxH: e.g. 1x1, blank will leave up to ImageMagick): ') else args.shift end # Safety img_name ||= 'rawr' puts <<~HELP Usage - Run in the directory you wish to modify the images - Images must be numbered so that the spritesheet can be created correctly Images: HELP images.each do |img| img_s = img.gsub("#{Dir.pwd}/", '').pastel(:yellow) puts " - #{img_s}" end puts <<~OPTIONS Image Target: #{img_name.inspect}, Grid: #{grid.inspect} Current Directory; #{Dir.pwd.pastel(:yellow)} OPTIONS puts exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Combine?" process(img_name, grid) rescue TTY::Reader::InputInterrupt exit 0 end |
.sorted_images ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/drakkon/lib/images/spritesheet.rb', line 109 def self.sorted_images numbers = Dir["#{Dir.pwd}/*.png"].map do |x| File.basename(x, '.png').to_i end numbers.sort.map { |x| "#{x}.png" } end |