Module: Drakkon::Images::SplitSpriteSheet

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

Overview

General Image Index Helper

Class Method Summary collapse

Class Method Details

.dimensions(file) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/drakkon/lib/images/split.rb', line 77

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

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

.imagesObject



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

def self.images
  Dir["#{Dir.pwd}/*.png"].map do |x|
    File.basename(x, '.png')
  end.sort
end

.process(file, width, height) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/drakkon/lib/images/split.rb', line 62

def self.process(file, width, height)
  LogBot.info('Image Split', file)
  convert = MiniMagick::Tool::Convert.new
  convert << file
  convert.crop("#{width}x#{height}")
  convert << file
  convert.call
end

.promptObject



86
87
88
# File 'lib/drakkon/lib/images/split.rb', line 86

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
54
55
56
57
58
59
60
# File 'lib/drakkon/lib/images/split.rb', line 5

def self.run!(args = [])
  image = if args.empty?
            prompt.select('What image to split?', images, filter: true)
          else
            args.first
          end

  image = "#{Dir.pwd}/#{image}.png"

  unless File.exist?(image)
    LogBot.fatal('Split', "Unable to find: '#{image}'")
    exit 1
  end

  sizing = dimensions(image)

  LogBot.info('Split', "Image Size: #{sizing[:w]}, #{sizing[:h]}")

  # Columns
  columns = if args.empty?
              prompt.ask('Columns: ', default: 5, convert: :int)
            else
              args.shift
            end

  # Rows
  rows = if args.empty?
           prompt.ask('Rows: ', default: 5, convert: :int)
         else
           args.shift
         end

  width = sizing[:w] / columns
  height = sizing[:h] / rows

  puts <<~HELP
              Usage [file]
              - Modify one file into a directory of split images


    Split:
        Rows: #{rows}, Columns: #{columns}
        Size: #{width} x #{height}
        Current Directory: #{Dir.pwd.pastel(:yellow)}

    Image to Modify: #{image}
  HELP

  puts

  exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Split!? #{'(Destructive)'.pastel(:red)}"

  process(image, width, height)
rescue TTY::Reader::InputInterrupt
  exit 0
end