Module: Drakkon::Images::SpriteSheetCombine

Defined in:
lib/drakkon/lib/images/spritesheet.rb

Overview

General Image Index Helper

Class Method Summary collapse

Class Method Details

.dimensions(file) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/drakkon/lib/images/spritesheet.rb', line 57

def self.dimensions(file)
  img = MiniMagick::Image.open(file)

  {
    w: img.width,
    h: img.height
  }
end

.imagesObject



66
67
68
# File 'lib/drakkon/lib/images/spritesheet.rb', line 66

def self.images
  @images ||= sorted_images
end

.process(img_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/drakkon/lib/images/spritesheet.rb', line 39

def self.process(img_name)
  LogBot.info('Image Spritesheet', img_name)

  img = dimensions(images.first)
  img_name += '.png'

  montage = MiniMagick::Tool::Montage.new
  images.each do |imgn|
    montage << imgn
  end

  montage.geometry "#{img.w}x#{img.h}+0+0"
  montage.background 'none'
  montage << "#{Dir.pwd}/#{img_name}"

  montage.call
end

.promptObject



78
79
80
# File 'lib/drakkon/lib/images/spritesheet.rb', line 78

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
# File 'lib/drakkon/lib/images/spritesheet.rb', line 5

def self.run!(args = [])
  img_name = if args.empty?
               prompt.ask('New File Name? (e.g. rawr): ')
             else
               args.shift
             end

  puts <<~HELP
            Usage [size] [filter = Lanczos2]
            - Run in the directory you wish to modify the images
            - Resizes the images via MiniMagick
            - Each image sizing values scaled by (value / 100)


    Image Target: #{img_name}
            Current Directory;
              #{Dir.pwd.pastel(:yellow)}

    Images:
  HELP

  images.each do |img|
    img_s = img.gsub("#{Dir.pwd}/", '').pastel(:yellow)
    puts "  - #{img_s}"
  end
  puts

  exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Combine?'"

  process(img_name)
rescue TTY::Reader::InputInterrupt
  exit 0
end

.sorted_imagesObject



70
71
72
73
74
75
76
# File 'lib/drakkon/lib/images/spritesheet.rb', line 70

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