Skip to content

Commit

Permalink
Created a pack (contains scenes) resource. Made imported scenes autom…
Browse files Browse the repository at this point in the history
…atically get put into a pack(s). Added pack selector when picking scenes.
  • Loading branch information
bil-bas committed Oct 31, 2010
1 parent 3695c59 commit 439a26c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 12 deletions.
23 changes: 15 additions & 8 deletions lib/sidney/resource_browser.rb
Expand Up @@ -6,12 +6,17 @@ module Sidney
class ResourceBrowser < Composite
DEFAULT_BORDER_COLOR = Color.rgb(255, 255, 255)

attr_reader :value
def value; @group.value; end

def refresh
refresh_group
def type=(type)
@type = type
refresh
value
end

selected_resource = @value
def refresh
selected_resource = @group.value
recreate_group

# TODO: This search should be _very_ much better...
resources = @type.all
Expand All @@ -24,10 +29,11 @@ def refresh
end
@label.text = "#{@object_grid.size} found from #{resources.size}"

# See if the previously selected resource is still in the list and if so, select it again.
if resources.find {|r| r == selected_resource }
@group.value = selected_resource
else
publish :changed, @value
publish :changed, nil
end

nil
Expand All @@ -53,17 +59,18 @@ def initialize(parent, type, options = {})

@label = Label.new(inner_container)

@group = RadioButton::Group.new(inner_container) # Dummy group. Will be replaced.

@text_entry.text = options[:search] # This will create the group and new radios.
end

protected
def refresh_group
inner_container.remove @group if @group
def recreate_group
inner_container.remove @group

@group = RadioButton::Group.new(inner_container) do |group|
@object_grid = GridPacker.new(group, width: 135, num_columns: 3)
group.subscribe :changed do |sender, value|
@value = value
publish :changed, value
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/sidney/resources.rb
@@ -1,7 +1,8 @@
# encoding: utf-8

require_relative 'resources/pack'
require_relative 'resources/room'
require_relative 'resources/sprite'
require_relative 'resources/tile'
require_relative 'resources/room'
require_relative 'resources/state_object'
require_relative 'resources/scene'
require_relative 'resources/scene'
20 changes: 20 additions & 0 deletions lib/sidney/resources/pack.rb
@@ -0,0 +1,20 @@
# encoding: utf-8

require_relative 'visual_resource'

module Sidney
class Pack < Resource
has_and_belongs_to_many :scenes
has_one :sprite

def image
@image ||= Image.create(32, 32, color: :red)
@image.rect 10, 10, 22, 22
@image
end

def thumbnail
image
end
end
end
34 changes: 34 additions & 0 deletions lib/sidney/resources/scene.rb
Expand Up @@ -4,19 +4,23 @@
require_relative 'visual_resource'
require_relative 'room'
require_relative 'state_object_layer'
require_relative 'pack'

module Sidney
class Scene < VisualResource
belongs_to :room

has_many :state_object_layers
has_many :state_objects, through: :state_object_layers
has_and_belongs_to_many :packs

# Manage saving tint (Gosu::Color) as json.
before_save JsonSerializationWrapper.new(:tint)
after_save JsonSerializationWrapper.new(:tint)
after_find JsonSerializationWrapper.new(:tint)

after_create :link_to_packs

CURRENT_VERSION = 2
DEFAULT_OBJECT_ZERO_FROZEN = false
DEFAULT_TINT = Color.rgba(0, 0, 0, 0)
Expand Down Expand Up @@ -95,6 +99,36 @@ def self.default_attributes(attributes = {})
super(attributes)
end

# After creating, link the scene to a pack.
def link_to_packs
# If a scene has a prefix, put it into that pack. E.g. JPNmegacat will be put into the pack JPN.
if pack_name = name[/^[A-Z]{2,}/]
add_to_pack(pack_name)
else
add_to_pack('NOPACK') # Put in this if we couldn't find a prefix.
end

add_to_pack('IMPORTED') # All SiD resources are put in this pack.

nil
end

# Add to a pack, by name. If the pack does not exist, create it.
def add_to_pack(pack_name)
# TODO: Need to calculate a better ID. But from what?
pack_id = pack_name.downcase.tr(' ', 'x').ljust(12, "x")

if Pack.exists?(pack_id)
packs << Pack.find(pack_id)
else
pack = packs.build(name: pack_name, sprite_id: "dummysprite1")
pack.id = pack_id
pack.save!
end

nil
end

protected
def to_binary
layers = @state_object_layers ? @state_object_layers : state_object_layers.order(:z)
Expand Down
9 changes: 7 additions & 2 deletions lib/sidney/schema.rb
Expand Up @@ -127,12 +127,17 @@
t.primary_key :id

t.string :name, null: false

t.string :scene_id, null: false
t.string :sprite_id, limit: uid_length, null: false
end

add_index :packs, :id, unique: true

# Packs->Scenes join table.
create_table :packs_scenes, id: false do |t|
t.string :pack_id, limit: uid_length, null: false
t.string :scene_id, limit: uid_length, null: false
end

# Stories (contains scenes via frames).
create_table :stories, id: false do |t|
t.string :id, limit: uid_length, null: false
Expand Down
7 changes: 7 additions & 0 deletions lib/sidney/states/pick_scene.rb
Expand Up @@ -9,6 +9,13 @@ def initialize
super

HorizontalPacker.new(container) do |packer|
# Picking a pack limits which scenes will be found in the scene picker.
@pack_picker = ResourceBrowser.new(packer, Pack) do |picker|
picker.subscribe :changed do |sender, value|
@scene_picker.type = value.scenes
end
end

@scene_picker = ResourceBrowser.new(packer, Scene, search: 'jp', square_icons: false) do |picker|
picker.subscribe :changed do |sender, value|
if value
Expand Down
8 changes: 8 additions & 0 deletions spec/resources/pack_spec.rb
@@ -0,0 +1,8 @@
require_relative 'resource_helper'

require 'pack'
include Sidney

describe Pack do

end

0 comments on commit 439a26c

Please sign in to comment.