Module: Drakkon::Images::Index

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

Overview

General Image Index Helper

Class Method Summary collapse

Class Method Details

.allObject



25
26
27
28
29
# File 'lib/drakkon/lib/images/index.rb', line 25

def self.all
  @all ||= []

  @all
end

.animation_directory?(list) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
# File 'lib/drakkon/lib/images/index.rb', line 123

def self.animation_directory?(list)
  # Ignore Directories
  return false if list.any? { |x| File.directory? x }

  list.reject { |x| x.include? 'png' }.empty? && list.all? do |x|
    File.basename(x).split('.png', 2).first.numeric?
  end
end

.build_indexObject



64
65
66
# File 'lib/drakkon/lib/images/index.rb', line 64

def self.build_index
  check sprites_directory
end

.catalogObject



11
12
13
14
15
16
# File 'lib/drakkon/lib/images/index.rb', line 11

def self.catalog
  # Better way to do this?
  @catalog ||= Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }

  @catalog
end

.check(dir = nil) ⇒ Object

Recursively Go through



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/drakkon/lib/images/index.rb', line 95

def self.check(dir = nil)
  LogBot.info('Image Index', "Check: #{dir}")

  # Collect Images
  list = Dir["#{dir}/*"]

  # Ignore Empties
  return if list.empty?

  # Check if its an Animation Directory
  if animation_directory?(list)
    LogBot.info('Image Index', "Animation: #{dir}")
    process_animation(list)
    return
  end

  # Do other things
  Dir["#{dir}/*"].each do |file|
    if File.directory?(file)
      Index.check(file)
    elsif File.extname(file) == '.png'
      process(file)
    end
  end

  :done
end

.contextObject



31
32
33
34
35
# File 'lib/drakkon/lib/images/index.rb', line 31

def self.context
  @context ||= Dir.pwd

  @context
end

.digestObject



37
38
39
40
# File 'lib/drakkon/lib/images/index.rb', line 37

def self.digest
  list = Dir["#{sprites_directory}**/*.png"]
  Digest::MD5.hexdigest(list.map { |x| Digest::MD5.file(x).hexdigest }.join)
end

.dimensions(file) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/drakkon/lib/images/index.rb', line 154

def self.dimensions(file)
  img = MiniMagick::Image.open(file)
  # data = "{ path: \"#{root}/#{file.gsub('.png', '')}\", w: #{img.width}, h: #{img.height} }"

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

.indexObject



5
6
7
8
9
# File 'lib/drakkon/lib/images/index.rb', line 5

def self.index
  @index ||= {}

  @index
end

.process(file = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/drakkon/lib/images/index.rb', line 132

def self.process(file = nil)
  details = dimensions(file)
  details[:path] = file.gsub(sprites_directory, '')[1..].gsub('.png', '')

  # Catalog
  catalog_list = details[:path].split('/')
  catalog_location = []
  catalog_list.each do |idx|
    catalog_location.push idx
    catalog.dig(*catalog_location)
  end
  catalog.dig(*catalog_list).merge! details

  # Full Index Push
  index[details[:path]] = details

  # Just Sprites Push
  sprites[details[:path]] = details

  all.push details[:path]
end

.process_animation(list = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/drakkon/lib/images/index.rb', line 164

def self.process_animation(list = nil)
  # Append all images to `all`
  list.each do |file|
    all.push file.gsub(sprites_directory, '')[1..].gsub('.png', '')
  end

  # Nab one to get data
  file = list.first
  details = dimensions(file)

  # Frames and Path
  details[:frames] = list.count
  details[:root_path] = File.dirname(file).gsub(sprites_directory, '')[1..]

  # Store via Root Path
  index[details[:root_path]] = details
end

.resultObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/drakkon/lib/images/index.rb', line 68

def self.result
  <<~RB
    module Drakkon
      module Images
        def self.index
          #{index.inspect}
        end

        def self.catalog
          #{catalog.inspect}
        end

        def self.sprites
          #{sprites.inspect}
        end

        def self.reset_all
          #{all.inspect}.each do |sprite|
            $gtk.reset_sprite "sprites/\#{sprite}.png"
          end
        end
      end
    end
  RB
end

.run!(force: false, dir: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/drakkon/lib/images/index.rb', line 46

def self.run!(force: false, dir: nil)
  @context = dir || Dir.pwd

  # Create Directory if sprites directory is missing
  FileUtils.mkdir_p(sprites_directory)

  if Settings.config[:image_digest] == digest
    LogBot.info('Images Index', 'Nothing New')
    return unless force
  end

  build_index

  Settings.update(:image_digest, digest)

  File.write("#{context}/app/drakkon/image_index.rb", result)
end

.spritesObject

No Animations



19
20
21
22
23
# File 'lib/drakkon/lib/images/index.rb', line 19

def self.sprites
  @sprites ||= {}

  @sprites
end

.sprites_directoryObject



42
43
44
# File 'lib/drakkon/lib/images/index.rb', line 42

def self.sprites_directory
  "#{context}/sprites/"
end