Midstream on #8 : Got saving to JSON working, moved the tileset saving functionality to Tailor\Tileset
This commit is contained in:
@@ -113,6 +113,10 @@ module Tailor
|
|||||||
@rectlist.length
|
@rectlist.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_image
|
||||||
|
@pristineImage
|
||||||
|
end
|
||||||
|
|
||||||
def get_tiles
|
def get_tiles
|
||||||
ret = []
|
ret = []
|
||||||
@rectlist.each do |rect|
|
@rectlist.each do |rect|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
require 'wx'
|
||||||
require 'tailor'
|
require 'tailor'
|
||||||
require 'Tailor/GUI/TilesetProperties'
|
require 'Tailor/GUI/TilesetProperties'
|
||||||
require 'Tailor/GUI/TilesetDisplay'
|
require 'Tailor/GUI/TilesetDisplay'
|
||||||
require 'Tailor/GUI/GridDisplay'
|
require 'Tailor/GUI/GridDisplay'
|
||||||
|
require 'Tailor/Tileset'
|
||||||
module Tailor
|
module Tailor
|
||||||
module GUI
|
module GUI
|
||||||
class TilesetEditor < Wx::Frame
|
class TilesetEditor < Wx::Frame
|
||||||
@@ -11,6 +12,7 @@ module Tailor
|
|||||||
@tilesetFilename = ""
|
@tilesetFilename = ""
|
||||||
@tilesetImage = nil
|
@tilesetImage = nil
|
||||||
@tilesetNames = []
|
@tilesetNames = []
|
||||||
|
@tileset = nil
|
||||||
|
|
||||||
@panel = Wx::Panel.new(self)
|
@panel = Wx::Panel.new(self)
|
||||||
@sizer = Wx::BoxSizer.new(Wx::VERTICAL)
|
@sizer = Wx::BoxSizer.new(Wx::VERTICAL)
|
||||||
@@ -89,6 +91,7 @@ module Tailor
|
|||||||
end
|
end
|
||||||
|
|
||||||
def on_ImportClicked(event)
|
def on_ImportClicked(event)
|
||||||
|
@tileset = Tailor::Tileset.new
|
||||||
wildcards = "*.png;*.bmp;*.tiff;*.gif"
|
wildcards = "*.png;*.bmp;*.tiff;*.gif"
|
||||||
fd = Wx::FileDialog.new(self, "Select tileset to import",
|
fd = Wx::FileDialog.new(self, "Select tileset to import",
|
||||||
:wildcard => wildcards,
|
:wildcard => wildcards,
|
||||||
@@ -139,8 +142,41 @@ module Tailor
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_SaveClicked(event)
|
def on_SaveClicked(event)
|
||||||
|
wildcards = "*.json"
|
||||||
|
fd = Wx::FileDialog.new(self, "Select tileset to import",
|
||||||
|
:wildcard => wildcards)
|
||||||
|
if fd.show_modal == Wx::ID_OK
|
||||||
|
filename = fd.get_path
|
||||||
|
@tileset.tileset_name = @tilesetNameCtrl.get_value
|
||||||
|
@tileset.license = @tilesetLicenseCtrl.get_value
|
||||||
|
@tileset.notes = @tilesetNotesCtrl.get_value
|
||||||
|
@tileset.tile_x = @tilesetProperties.TileX
|
||||||
|
@tileset.tile_y = @tilesetProperties.TileY
|
||||||
|
@tileset.space_x = @tilesetProperties.SpaceX
|
||||||
|
@tileset.space_y = @tilesetProperties.SpaceY
|
||||||
|
@tileset.pad_x = @tilesetProperties.PadX
|
||||||
|
@tileset.pad_y = @tilesetProperties.PadY
|
||||||
|
@tileset.image = @tilesetSlicer.get_image
|
||||||
|
|
||||||
|
progdialog = Wx::ProgressDialog.new("Saving...",
|
||||||
|
"Saving...",
|
||||||
|
@tilesetSlicer.get_size - 1,
|
||||||
|
self,
|
||||||
|
style = Wx::PD_SMOOTH | Wx::PD_AUTO_HIDE)
|
||||||
|
tiles = @tilesetSlicer.get_tiles
|
||||||
|
(0..(tiles.size-1)).each do |i|
|
||||||
|
@tileset.add_tile(@tilesetNames[i], tiles[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
callback = Proc.new do |msg, tile, tileName, tileIndex|
|
||||||
|
progdialog.update(tileIndex)
|
||||||
|
end
|
||||||
|
|
||||||
|
File.open(filename, "w") do |file|
|
||||||
|
@tileset.write(file, callback)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_tilepropsChanged(event)
|
def on_tilepropsChanged(event)
|
||||||
|
|||||||
100
lib/tailor/Tileset.rb
Normal file
100
lib/tailor/Tileset.rb
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
require 'wx'
|
||||||
|
require 'json'
|
||||||
|
require 'base64'
|
||||||
|
require 'stringio'
|
||||||
|
|
||||||
|
module Tailor
|
||||||
|
class Tileset
|
||||||
|
attr_accessor :image
|
||||||
|
attr_accessor :license
|
||||||
|
attr_accessor :notes
|
||||||
|
attr_accessor :tileset_name
|
||||||
|
attr_accessor :tile_x
|
||||||
|
attr_accessor :tile_y
|
||||||
|
attr_accessor :space_x
|
||||||
|
attr_accessor :space_y
|
||||||
|
attr_accessor :pad_x
|
||||||
|
attr_accessor :pad_y
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
self.image = nil
|
||||||
|
self.license = ''
|
||||||
|
self.notes = ''
|
||||||
|
self.tileset_name = ''
|
||||||
|
self.tile_x = 0
|
||||||
|
self.tile_y = 0
|
||||||
|
self.space_x = 0
|
||||||
|
self.space_y = 0
|
||||||
|
self.pad_x = 0
|
||||||
|
self.pad_y = 0
|
||||||
|
@tiles = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_tile(name, image)
|
||||||
|
@tiles << {"name" => name, "image" => image}
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_tile(elem)
|
||||||
|
if elem.instance_of?(String)
|
||||||
|
@tiles.each do
|
||||||
|
if tile['name'] == elem
|
||||||
|
return tile['image']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elsif elem.instance_of?(Integer)
|
||||||
|
return @tiles[elem]['image']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def write(io_obj, callback = nil)
|
||||||
|
obj = self.to_json(callback)
|
||||||
|
if not callback.nil?
|
||||||
|
callback.call("Saving JSON...", nil, '', 0)
|
||||||
|
end
|
||||||
|
io_obj.write(JSON.pretty_generate(obj))
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json(callback = nil)
|
||||||
|
obj = {
|
||||||
|
"name" => self.tileset_name,
|
||||||
|
"license" => self.license,
|
||||||
|
"notes" => self.notes,
|
||||||
|
"dimensions" => {
|
||||||
|
"tile_x" => self.tile_x,
|
||||||
|
"tile_y" => self.tile_y,
|
||||||
|
"space_x" => self.space_x,
|
||||||
|
"space_y" => self.space_y,
|
||||||
|
"pad_x" => self.pad_x,
|
||||||
|
"pad_y" => self.pad_y
|
||||||
|
},
|
||||||
|
"image" => "",
|
||||||
|
"tiles" => []
|
||||||
|
}
|
||||||
|
|
||||||
|
StringIO.open do |iostream|
|
||||||
|
Wx::Image.from_bitmap(self.image).write(iostream, Wx::BITMAP_TYPE_PNG)
|
||||||
|
iostream.rewind
|
||||||
|
obj['image'] = Base64.encode64(iostream.read)
|
||||||
|
end
|
||||||
|
|
||||||
|
idx = 0
|
||||||
|
@tiles.each do |tile|
|
||||||
|
StringIO.open do |iostream|
|
||||||
|
if not callback.nil?
|
||||||
|
callback.call("Converting to base64", tile['image'], tile['name'], idx)
|
||||||
|
end
|
||||||
|
Wx::Image.from_bitmap(tile['image']).write(iostream, Wx::BITMAP_TYPE_PNG)
|
||||||
|
iostream.rewind
|
||||||
|
data = {
|
||||||
|
"name" => tile['name'],
|
||||||
|
"image" => Base64.encode64(iostream.read)
|
||||||
|
}
|
||||||
|
obj['tiles'] << data
|
||||||
|
end
|
||||||
|
idx += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
obj
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user