Class: MagicaVoxel::Chunk
- Inherits:
-
Object
- Object
- MagicaVoxel::Chunk
- Includes:
- Enumerable
- Defined in:
- lib/magica_voxel/chunk.rb
Overview
The MagicaVoxel Chunk
Constant Summary collapse
- TYPES =
Chunk Type Mapping
{ 'MAIN' => 'Main', 'SIZE' => 'Size', 'XYZI' => 'Model', 'RGBA' => 'Pattle', 'nTRN' => 'Transform', 'nGRP' => 'Group', 'nSHP' => 'Shape', 'MATL' => 'Material', 'LAYR' => 'Layer', 'rOBJ' => 'Object', 'rCAM' => 'Camera', 'NOTE' => 'Note', 'IMAP' => 'PattleMap' }.freeze
Class Method Summary collapse
-
.header(io) ⇒ Array<String|Number>
Read Chunk Header.
-
.read(io) ⇒ Enumerator<Chunk>
Read Chunk.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
:nodoc:.
-
#initialize(content, children) ⇒ Chunk
constructor
A new instance of Chunk.
Constructor Details
#initialize(content, children) ⇒ Chunk
Returns a new instance of Chunk.
69 70 71 72 73 74 75 76 |
# File 'lib/magica_voxel/chunk.rb', line 69 def initialize(content, children) @children = Chunk.read(StringIO.new(children)) Reader.open(content) do |reader| layout.each do |key, type| instance_variable_set(:"@#{key}", reader.send(type)) end end end |
Class Method Details
.header(io) ⇒ Array<String|Number>
Read Chunk Header
35 36 37 38 39 40 41 |
# File 'lib/magica_voxel/chunk.rb', line 35 def header(io) [ io.read(4), io.read(4).unpack1('L'), io.read(4).unpack1('L') ] end |
.read(io) ⇒ Enumerator<Chunk>
Read Chunk
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/magica_voxel/chunk.rb', line 16 def read(io) return enum_for(:read, io) unless defined?(yield) until io.eof? type, n_content, n_children = header(io) klass_name = Chunk::TYPES[type] || 'Chunk' yield MagicaVoxel .const_get(klass_name) .new(io.read(n_content), io.read(n_children)) end end |
Instance Method Details
#each(&block) ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/magica_voxel/chunk.rb', line 81 def each(&block) @children.each(&block) end |