From afd809d1e91c7b6b581c4f45104655d243cb9bc4 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Sun, 25 May 2014 21:59:59 -0700 Subject: [PATCH] Close #25 and Close #10 Fixed scroll bars not appearing. Also implemented load/save of Collections; tied the Library to a collection (libraries are per-project); Most of the File menu works now. Library manager works. Ready to start working on the drag and drop library pallette and tabbed page display at this point. --- lib/tailor/Collection.rb | 63 ++++++++++++++++++++++++++++++++ lib/tailor/GUI/LibraryManager.rb | 30 +++++++++++++++ lib/tailor/Library.rb | 34 +++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 lib/tailor/Collection.rb create mode 100644 lib/tailor/GUI/LibraryManager.rb create mode 100644 lib/tailor/Library.rb diff --git a/lib/tailor/Collection.rb b/lib/tailor/Collection.rb new file mode 100644 index 0000000..03ca49c --- /dev/null +++ b/lib/tailor/Collection.rb @@ -0,0 +1,63 @@ +require 'singleton' + +module Tailor + module Collection + include Singleton + + attr_accessor :library + + def initialize + @metadata = { + "author": "", + "name": "", + "license": "" + } + self.library = Tailor::Library.new + @pages = {} + end + + def to_json + js = { + "metadata" => metadata, + "library" => {}, + "pages" => {} + } + + @library.each do |tileset| + js['library'][tileset['name']] = tileset.get_filename + end + @pages.each do |page| + js['pages'][page.page_name] = page.to_json + end + end + + def save_json(filename) + dname = File.dirname(filename) + Dir.mkdir(dname) unless Dir.exist?(dname) + + File.open(filename, "w") do |file| + file.write(JSON.pretty_generate(to_json)) + end + end + + def from_json(js) + js['library'].each_pair do |name, filename| + @library.load_tileset(filename) + end + js['pages'].each_pair do |name, data| + @pages[name] = Tailor::Page.new + @pages[name].from_json(data) + end + end + + def load_json(filename) + dname = File.dirname(filename) + Dir.mkdir(dname) unless Dir.exist?(dname) + File.open(filename, "r") do |file| + js = JSON.parse(file.read) + end + from_json(js) + end + + end +end diff --git a/lib/tailor/GUI/LibraryManager.rb b/lib/tailor/GUI/LibraryManager.rb new file mode 100644 index 0000000..9458266 --- /dev/null +++ b/lib/tailor/GUI/LibraryManager.rb @@ -0,0 +1,30 @@ +require 'wx' +require 'Tailor/Library' + +module Tailor + module GUI + class LibraryManager + def initialize(*args) + super(*args) + @library = Tailor::Library.instance + @panel = Wx::Panel.new(self) + @sizer = Wx::BoxSizer.new(Wx::VERTICAL) + + horizSizer = Wx::BoxSizer.new(Wx::HORIZONTAL) + vertSizer = Wx::BoxSizer.new(Wx::VERTICAL) + @tilesetList = Wx::ListBox.new(@panel, + Wx::ID_ANY, + Wx::DEFAULT_POSITION, + Wx::DEFAULT_SIZE, + @library.tileset_names, + Wx::LB_SORT | Wx::LB_SINGLE) + vertSizer.add(@tilesetList, 0, flags = Wx::EXPAND|Wx::ALL) + horizSizer.add(vertSizer, 0, flags = Wx::EXPAND|Wx::ALL) + @sizer.add(horizSizer, 1, flag=Wx::EXPAND|Wx::ALL) + @panel.set_sizer(@sizer) + @sizer.set_size_hints(self) + show() + end + end + end +end diff --git a/lib/tailor/Library.rb b/lib/tailor/Library.rb new file mode 100644 index 0000000..3ccf1db --- /dev/null +++ b/lib/tailor/Library.rb @@ -0,0 +1,34 @@ +require 'Tailor/Tileset' +require 'singleton' + +module Tailor + class Library + include Singleton + + attr_accessor :tilesets + + def initialize + self.tilesets = [] + end + + def tileset_names + x = [] + idx = 0 + self.tilesets.each do |tileset| + x << [idx, tileset.tileset_name] + idx += 1 + end + end + + def unload_tileset(tileset) + self.tilesets.delete(tileset) + end + + def add_tileset(path) + ts = Tailor::Tileset.new + ts.from_json(JSON.parse(File.read(path))) + self.tilesets << ts + end + + end +end