From 1b708359a61fef3bc95271f39b201506805c110a Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Wed, 7 May 2014 07:18:01 -0700 Subject: [PATCH] For #8 : Tileset editor window mostly working * Tileset editor window appears with notes, buttons, tilesetproperties, etc. * Introduces a skeleton and placeholder for TilesetDisplay. * The text controls on the right aren't autosizing correctly, and they're too small Midstream on #8 - sizing problems. midstrean on #8 - sizers mostly fixed, still some min sizes to set Resizing works as expected now and minimum sizes enforced, moved the buttons for #8 For #8 midstream, all resizing works as expected now --- lib/tailor/GUI.rb | 4 +- lib/tailor/GUI/GridDisplay.rb | 46 ++++++++++ lib/tailor/GUI/ImageDisplay.rb | 28 ++++++ lib/tailor/GUI/MainWindow.rb | 2 +- lib/tailor/GUI/TilesetDisplay.rb | 8 ++ lib/tailor/GUI/TilesetEditor.rb | 128 ++++++++++++++++++++++++++++ lib/tailor/GUI/TilesetProperties.rb | 52 +++++++++++ 7 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 lib/tailor/GUI/GridDisplay.rb create mode 100644 lib/tailor/GUI/ImageDisplay.rb create mode 100644 lib/tailor/GUI/TilesetDisplay.rb create mode 100644 lib/tailor/GUI/TilesetEditor.rb diff --git a/lib/tailor/GUI.rb b/lib/tailor/GUI.rb index 85ed590..a942c98 100644 --- a/lib/tailor/GUI.rb +++ b/lib/tailor/GUI.rb @@ -1,11 +1,13 @@ require 'wx' require 'tailor/GUI/MainWindow' +require 'tailor/GUI/TilesetEditor' module Tailor module GUI class Application < Wx::App def on_init - Tailor::GUI::MainWindow.new + #Tailor::GUI::MainWindow.new + Tailor::GUI::TilesetEditor.new(nil, -1, 'Tailor') end end end diff --git a/lib/tailor/GUI/GridDisplay.rb b/lib/tailor/GUI/GridDisplay.rb new file mode 100644 index 0000000..baf0cd2 --- /dev/null +++ b/lib/tailor/GUI/GridDisplay.rb @@ -0,0 +1,46 @@ +require 'wx' +require 'Tailor/GUI/ImageDisplay' + +module Tailor + module GUI + class GridDisplay < Tailor::GUI::ImageDisplay + + def initialize(*args) + super(*args) + @padX = 0 + @padY = 0 + @pitchX = 0 + @pitchY = 0 + @gridX = 0 + @gridY = 0 + end + + def set_grid(padX, padY, pitchX, pitchY, gridX, gridY) + @padX = padX + @padY = padY + @pitchX = pitchX + @pitchY = pitchY + @gridX = gridX + @gridY = gridY + end + + def on_draw(dc) + super.on_draw(dc) + dc.set_pen(Wx::BLACK_DASHED_PEN) + rows = ( (@image.height - @padY) / (@gridY + @pitchY) ) + columns = ( (@image.width - @padX) / (@gridX + @pitchX) ) + points = [] + curX = @padX + curY = @padY + for row in 0..rows + for column in 0..columns + dc.draw_rectangle(curX, curY, @gridX, @gridY) + curX += (@gridX + @pitchX) + end + curX = @padX + curY += (@gridY + @pitchY) + end + end + end + end +end diff --git a/lib/tailor/GUI/ImageDisplay.rb b/lib/tailor/GUI/ImageDisplay.rb new file mode 100644 index 0000000..362d503 --- /dev/null +++ b/lib/tailor/GUI/ImageDisplay.rb @@ -0,0 +1,28 @@ +require 'wx' + +module Tailor + module GUI + + # Ganked from http://rubyforscientificresearch.blogspot.com/2009/05/displaying-images-in-wxruby.html + + class ImageDisplay < Wx::ScrolledWindow + def initialize(*args) + super(*args) + @image = nil + end + + def set_image image + @image = image + set_scrollbars(1, 1, @image.width, @image.height) + refresh + end + + def on_draw dc + dc.set_background Wx::WHITE_BRUSH + dc.clear + return if @image == nil + dc.draw_bitmap(@image, 0, 0, true) + end + end + end +end diff --git a/lib/tailor/GUI/MainWindow.rb b/lib/tailor/GUI/MainWindow.rb index aeab2ad..92adf3e 100644 --- a/lib/tailor/GUI/MainWindow.rb +++ b/lib/tailor/GUI/MainWindow.rb @@ -39,7 +39,7 @@ module Tailor def on_file_new @mainPanel = Wx::Panel.new(self) - @mainPanelSizer = Wx::BoxSizer.new(Wx::VERTICAL) + @mainPanelSizer = Wx::BoxSizer.new(Wx::HORIZONTAL) @mainPanel.set_sizer(@mainPanelSizer) @tilesetProperties = Tailor::GUI::TilesetProperties.new(@mainPanel, Wx::ID_ANY) @mainPanelSizer.add(@tilesetProperties) diff --git a/lib/tailor/GUI/TilesetDisplay.rb b/lib/tailor/GUI/TilesetDisplay.rb new file mode 100644 index 0000000..02d8e89 --- /dev/null +++ b/lib/tailor/GUI/TilesetDisplay.rb @@ -0,0 +1,8 @@ +require 'wx' + +module Tailor + module GUI + class TilesetDisplay < Wx::Panel + end + end +end diff --git a/lib/tailor/GUI/TilesetEditor.rb b/lib/tailor/GUI/TilesetEditor.rb new file mode 100644 index 0000000..69b38b2 --- /dev/null +++ b/lib/tailor/GUI/TilesetEditor.rb @@ -0,0 +1,128 @@ +require 'tailor' +require 'Tailor/GUI/TilesetProperties' +require 'Tailor/GUI/TilesetDisplay' +require 'Tailor/GUI/GridDisplay' + +module Tailor + module GUI + class TilesetEditor < Wx::Frame + def initialize(*args) + super(*args) + @tilesetFilename = "" + @tilesetImage = nil + + @panel = Wx::Panel.new(self) + @sizer = Wx::BoxSizer.new(Wx::VERTICAL) + + rowsizer = Wx::BoxSizer.new(Wx::HORIZONTAL) + + tmpversizer = Wx::BoxSizer.new(Wx::VERTICAL) + @tilesetProperties = Tailor::GUI::TilesetProperties.new(@panel, Wx::ID_ANY) + evt_tileprops_changed(@tilesetProperties) { |event| on_tilepropsChanged(event) } + tmpversizer.add(@tilesetProperties, 0, flag = Wx::EXPAND|Wx::ALL) + @cancelBtn = Wx::Button.new(@panel, Wx::ID_ANY, "Cancel") + evt_button(@cancelBtn.get_id()) { |event| on_CancelClicked(event) } + @importBtn = Wx::Button.new(@panel, Wx::ID_ANY, "Import") + evt_button(@importBtn.get_id()) { |event| on_ImportClicked(event) } + + @exportBtn = Wx::Button.new(@panel, Wx::ID_ANY, "Export") + evt_button(@exportBtn.get_id()) { |event| on_ExportClicked(event) } + + @saveBtn = Wx::Button.new(@panel, Wx::ID_ANY, "Save") + evt_button(@saveBtn.get_id()) { |event| on_SaveClicked(event) } + + tmpversizer.add_spacer(20) + tmpversizer.add(@importBtn, 0, flag=Wx::EXPAND) + tmpversizer.add_spacer(20) + tmpversizer.add(@exportBtn, 0, flag=Wx::EXPAND) + tmpversizer.add_spacer(20) + tmpversizer.add(@saveBtn, 0, flag=Wx::EXPAND) + tmpversizer.add_spacer(20) + tmpversizer.add(@cancelBtn, 0, flag=Wx::EXPAND) + rowsizer.add(tmpversizer, 0, flag = Wx::EXPAND|Wx::ALL) + + @tilesetSlicer = Tailor::GUI::GridDisplay.new(@panel, Wx::ID_ANY) + @tilesetSlicer.set_min_size(Wx::Size.new(320, 240)) + rowsizer.add(@tilesetSlicer, 1, flag = Wx::EXPAND|Wx::ALL) + + tmpversizer = Wx::BoxSizer.new(Wx::VERTICAL) + @tilesetNameCtrl = Wx::TextCtrl.new(@panel, + Wx::ID_ANY, + "Tileset Name") + tmpversizer.add(@tilesetNameCtrl, 0, flag = Wx::EXPAND|Wx::ALIGN_TOP) + @tilesetNotesCtrl = Wx::TextCtrl.new(@panel, + Wx::ID_ANY, + "Notes about this tileset", + Wx::DEFAULT_POSITION, + Wx::DEFAULT_SIZE, + Wx::TE_MULTILINE|Wx::TE_WORDWRAP) + @tilesetNotesCtrl.set_min_size(Wx::Size.new(200,120)) + tmpversizer.add(@tilesetNotesCtrl, 1, flag = Wx::EXPAND|Wx::ALL) + @tilesetLicenseCtrl = Wx::TextCtrl.new(@panel, + Wx::ID_ANY, + "Legal License of this tileset", + Wx::DEFAULT_POSITION, + Wx::DEFAULT_SIZE, + Wx::TE_MULTILINE|Wx::TE_WORDWRAP) + @tilesetLicenseCtrl.set_min_size(Wx::Size.new(200,120)) + tmpversizer.add(@tilesetLicenseCtrl, 1, flag = Wx::EXPAND|Wx::ALL) + + rowsizer.add(tmpversizer, 0, flag=Wx::EXPAND) + + @sizer.add(rowsizer, 1, flag = Wx::EXPAND|Wx::ALL) + + @panel.set_sizer(@sizer) + @sizer.set_size_hints(self) + show() + end + + def on_CancelClicked(event) + close + end + + def on_ImportClicked(event) + wildcards = "*.png;*.bmp;*.tiff;*.gif" + fd = Wx::FileDialog.new(self, "Select tileset to import", + :wildcard => wildcards, + :style => Wx::FD_FILE_MUST_EXIST | Wx::FD_PREVIEW ) + if fd.show_modal == Wx::ID_OK + @tilesetFilename = fd.get_path + refresh_image + end + end + + def on_ExportClicked(event) + end + + def on_SaveClicked(event) + end + + def on_tilepropsChanged(event) + @tilesetSlicer.set_grid(event.padX, + event.padY, + event.pitchX, + event.pitchY, + event.gridX, + event.gridY + ) + end + + def refresh_image + begin + @tilesetImage = Wx::Bitmap.new(@tilesetFilename) + if @tilesetImage.is_ok + @tilesetSlicer.set_image @tilesetImage + # The + 20 here is a hack to make scroll bars go away where we don't want them + x = ( @tilesetImage.get_width > 600 ? 600 : @tilesetImage.get_width + 20) + y = ( @tilesetImage.get_height > 400 ? 400 : @tilesetImage.get_height + 20) + @tilesetSlicer.set_min_size(Wx::Size.new(x, y)) + @sizer.set_size_hints(self) + end + rescue Exception => e + puts e + end + end + + end + end +end diff --git a/lib/tailor/GUI/TilesetProperties.rb b/lib/tailor/GUI/TilesetProperties.rb index f2dfd38..546839b 100644 --- a/lib/tailor/GUI/TilesetProperties.rb +++ b/lib/tailor/GUI/TilesetProperties.rb @@ -2,6 +2,38 @@ require 'wx' module Tailor module GUI + + class TilesetPropertiesChangedEvent < Wx::CommandEvent + attr_accessor :padX + attr_accessor :padY + attr_accessor :pitchX + attr_accessor :pitchY + attr_accessor :gridX + attr_accessor :gridY + + def initialize(*args) + super(*args) + @padX = args[2]['padX'] + @padY = args[2]['padY'] + @pitchX = args[2]['pitchX'] + @pitchY = args[2]['pitchY'] + @gridX = args[2]['gridX'] + @gridY = args[2]['gridY'] + end + + def clone + TilesetPropertiesChangedEvent.new(self.id, + self.eventType, + { 'padX' => @padX, + 'padY' => @padY, + 'pitchX' => @pitchX, + 'pitchY' => @pitchY, + 'gridX' => @gridX, + 'gridY' => @gridY } + ) + end + end + class TilesetProperties < Wx::Panel def create_method( name, &block ) self.class.send( :define_method, name, &block ) @@ -28,6 +60,7 @@ module Tailor elemCtrl = Wx::TextCtrl.new(self, Wx::ID_ANY, value.to_s) + evt_text(elemCtrl) { |event| on_textChanged(event) } tmpsizer.add(elemCtrl, flag = Wx::EXPAND|Wx::ALIGN_RIGHT) rubyname = elem.gsub(/ /, '') @@ -41,6 +74,25 @@ module Tailor end @sizer.set_size_hints(self) end + + def on_textChanged(event) + add_pending_event(TilesetPropertiesChangedEvent.new(Wx::ID_ANY, + EVT_TILEPROPS_CHANGED, + { 'PadX' => @TileX, + 'PadY' => @TileY, + 'PitchX' => @SpaceX, + 'PitchY' => @SpaceY, + 'GridX' => @TileX, + 'GridY' => @TileY } + ) + ) + end end + + EVT_TILEPROPS_CHANGED = Wx::Event.new_event_type + Wx::EvtHandler.register_class(TilesetPropertiesChangedEvent, + EVT_TILEPROPS_CHANGED, + "evt_tileprops_changed", + 1) end end