Class: Sabrina::Plugins::Spritesheet

Inherits:
Sabrina::Plugin show all
Includes:
ChildrenManager
Defined in:
lib/sabrina/plugins/spritesheet.rb

Overview

This Sabrina::Plugin provides an abstraction for manipulating all Sprites and Palettes associated with a Monster.

See Also:

Constant Summary collapse

ENHANCES =
Monster
PLUGIN_NAME =
'Spritesheet'
SHORT_NAME =
'spritesheet'
FEATURES =
Set.new [:reread, :write, :save, :load]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChildrenManager

included

Methods inherited from Sabrina::Plugin

#feature?, feature_all, feature_this, features, inherited, plugin_name, #reread, short_name, target, to_s, #to_s, #write

Constructor Details

#initialize(monster) ⇒ Spritesheet

Generates a new Spritesheet object.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sabrina/plugins/spritesheet.rb', line 51

def initialize(monster)
  @monster = monster
  @rom = monster.rom
  @index = monster.index

  @palettes = [
    Palette.from_table(@monster.rom, :palette_table, @monster.index),
    Palette.from_table(@monster.rom, :shinypal_table, @monster.index)
  ]

  @sprites = [
    Sprite.from_table(@monster.rom, :front_table, @monster.index),
    Sprite.from_table(@monster.rom, :back_table, @monster.index)
  ]
end

Instance Attribute Details

#indexInteger

The real index of the monster.

Returns:

  • (Integer)


36
# File 'lib/sabrina/plugins/spritesheet.rb', line 36

attr_children :rom, :index

#palettesArray (readonly)

The palettes used by the sprites.

Returns:

  • (Array)


41
42
43
# File 'lib/sabrina/plugins/spritesheet.rb', line 41

def palettes
  @palettes
end

#romRom

The current working ROM file.

Returns:



36
# File 'lib/sabrina/plugins/spritesheet.rb', line 36

attr_children :rom, :index

#spritesArray (readonly)

The sprites.

Returns:

  • (Array)


46
47
48
# File 'lib/sabrina/plugins/spritesheet.rb', line 46

def sprites
  @sprites
end

Instance Method Details

#childrenArray

Returns:

  • (Array)

See Also:



69
70
71
# File 'lib/sabrina/plugins/spritesheet.rb', line 69

def children
  @sprites + @palettes
end

#justifyObject

Justify sprites to the target ROM count as per ROM Config, assuming 64x64 frame size.



75
76
77
# File 'lib/sabrina/plugins/spritesheet.rb', line 75

def justify
  @sprites.map(&:justify)
end

#load(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array

Load data from a file.

Returns:

  • (Array)

    Any return data from child methods.



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
113
114
115
116
# File 'lib/sabrina/plugins/spritesheet.rb', line 82

def load(file = @monster.filename, dir = @monster.work_dir, *_args)
  path = get_path(file, dir)
  a = split_canvas(ChunkyPNG::Canvas.from_file(path))
  h = { rom: @rom, index: @index }

  normal_rgb = a[0].to_rgb_stream + a[2].to_rgb_stream
  shiny_rgb = a[1].to_rgb_stream + a[3].to_rgb_stream

  @palettes = Palette.create_synced_palettes(
    normal_rgb,
    shiny_rgb,
    h.merge(table: :palette_table),
    h.merge(table: :shinypal_table)
  )

  frames = @rom.special_frames.fetch(@index, @rom.frames)
  front = crop_canvas(a[0], frames.first * 64)
  back = crop_canvas(a[2], frames.last * 64)

  @sprites = [
    Sprite.from_canvas(
      front,
      @palettes.first,
      h.merge(table: :front_table)
    ),
    Sprite.from_canvas(
      back,
      @palettes.first,
      h.merge(table: :back_table)
    )
  ]

  justify
  children
end

#save(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array

Save data to a file.

Returns:

  • (Array)

    Any return data from child methods.



121
122
123
124
125
126
127
128
129
130
# File 'lib/sabrina/plugins/spritesheet.rb', line 121

def save(file = @monster.filename, dir = @monster.work_dir, *_args)
  a = []
  @sprites.product(@palettes).each do |x|
    a << x.first.to_canvas(x.last)
  end

  path = get_path(file, dir, mkdir: true)

  combine_canvas(a).save(path)
end