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)


144
145
146
147
148
149
150
151
# File 'lib/drakkon/lib/images/index.rb', line 144

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



67
68
69
# File 'lib/drakkon/lib/images/index.rb', line 67

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/drakkon/lib/images/index.rb', line 116

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



175
176
177
178
179
180
181
182
183
# File 'lib/drakkon/lib/images/index.rb', line 175

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/drakkon/lib/images/index.rb', line 153

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/drakkon/lib/images/index.rb', line 185

def self.process_animation(list = nil)
  # Append all images to `all`
  list.each do |file|
    # Also store individual image
    process 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

rubocop:disable Layout/HeredocIndentation



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/drakkon/lib/images/index.rb', line 72

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

    		def self.catalog
    			#{catalog.inspect}
    		end

	def self.catalog_dig(long)
		catalog.dig(*long.split('/'))
	end

	# Return Path from Catalog
    		def self.find_sprite(path)
		catalog.dig(*path.split('/')).values.map(&:path)
    		end

	# Return Path from Catalog
    		def self.find_anim(path)
		catalog.dig(*path.split('/')).keys.map do |key|
			"\#{path}/\#{key}"
		end
    		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
63
64
65
# File 'lib/drakkon/lib/images/index.rb', line 46

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

  # Make Directory `app/drakkon` if it doesn't exist
  FileUtils.mkdir_p('app/drakkon') unless File.directory?('app/drakkon')

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

  if Settings.config[:image_digest] == digest && File.exist?("#{context}/app/drakkon/image_index.rb")
    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