From 3c192092d6cfc642810d77fe68bec7789076c78e Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Sat, 24 May 2014 08:15:01 -0700 Subject: [PATCH] Midstream on #8 : Got saving to JSON working, moved the tileset saving functionality to Tailor\Tileset --- lib/tailor/GUI/GridDisplay.rb | 4 ++ lib/tailor/GUI/TilesetEditor.rb | 42 +++++++++++++- lib/tailor/Tileset.rb | 100 ++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 lib/tailor/Tileset.rb diff --git a/lib/tailor/GUI/GridDisplay.rb b/lib/tailor/GUI/GridDisplay.rb index 5daade5..f3839db 100644 --- a/lib/tailor/GUI/GridDisplay.rb +++ b/lib/tailor/GUI/GridDisplay.rb @@ -113,6 +113,10 @@ module Tailor @rectlist.length end + def get_image + @pristineImage + end + def get_tiles ret = [] @rectlist.each do |rect| diff --git a/lib/tailor/GUI/TilesetEditor.rb b/lib/tailor/GUI/TilesetEditor.rb index c3427f8..c3362cd 100644 --- a/lib/tailor/GUI/TilesetEditor.rb +++ b/lib/tailor/GUI/TilesetEditor.rb @@ -1,8 +1,9 @@ +require 'wx' require 'tailor' require 'Tailor/GUI/TilesetProperties' require 'Tailor/GUI/TilesetDisplay' require 'Tailor/GUI/GridDisplay' - +require 'Tailor/Tileset' module Tailor module GUI class TilesetEditor < Wx::Frame @@ -11,6 +12,7 @@ module Tailor @tilesetFilename = "" @tilesetImage = nil @tilesetNames = [] + @tileset = nil @panel = Wx::Panel.new(self) @sizer = Wx::BoxSizer.new(Wx::VERTICAL) @@ -89,6 +91,7 @@ module Tailor end def on_ImportClicked(event) + @tileset = Tailor::Tileset.new wildcards = "*.png;*.bmp;*.tiff;*.gif" fd = Wx::FileDialog.new(self, "Select tileset to import", :wildcard => wildcards, @@ -139,8 +142,41 @@ module Tailor 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 def on_tilepropsChanged(event) diff --git a/lib/tailor/Tileset.rb b/lib/tailor/Tileset.rb new file mode 100644 index 0000000..3e00da8 --- /dev/null +++ b/lib/tailor/Tileset.rb @@ -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