From c176f22ea1538f19c2fea806eb63a97de16aea72 Mon Sep 17 00:00:00 2001 From: Eric Watson Date: Fri, 1 Jun 2012 10:03:52 -0500 Subject: [PATCH] Change SwtShoes to Swt::Shoes. Close #6 Note, this commit doesn't change the directory structure of specs. --- bin/swt-shoooes | 2 +- bin/swt-shoooes.bat | 2 +- bin/swt-shoooesd | 4 +- bin/swt-shoooesd.bat | 2 +- lib/{swt_shoes.rb => shoes/swt.rb} | 28 ++-- lib/shoes/swt/animation.rb | 31 +++++ lib/shoes/swt/app.rb | 52 ++++++++ lib/shoes/swt/button.rb | 48 +++++++ lib/shoes/swt/color.rb | 15 +++ .../swt}/element_methods.rb | 10 +- lib/shoes/swt/flow.rb | 47 +++++++ lib/shoes/swt/line.rb | 32 +++++ lib/shoes/swt/oval.rb | 34 +++++ lib/shoes/swt/shape.rb | 96 ++++++++++++++ lib/shoes/swt/sound.rb | 122 ++++++++++++++++++ lib/swt_shoes/animation.rb | 29 ----- lib/swt_shoes/app.rb | 50 ------- lib/swt_shoes/button.rb | 46 ------- lib/swt_shoes/color.rb | 13 -- lib/swt_shoes/flow.rb | 45 ------- lib/swt_shoes/line.rb | 30 ----- lib/swt_shoes/oval.rb | 32 ----- lib/swt_shoes/shape.rb | 94 -------------- lib/swt_shoes/sound.rb | 120 ----------------- spec/swt_shoes/animation_spec.rb | 8 +- spec/swt_shoes/app_spec.rb | 12 +- spec/swt_shoes/button_spec.rb | 4 +- spec/swt_shoes/color_spec.rb | 2 +- spec/swt_shoes/element_methods_spec.rb | 4 +- spec/swt_shoes/flow_spec.rb | 10 +- spec/swt_shoes/line_spec.rb | 2 +- spec/swt_shoes/oval_spec.rb | 2 +- spec/swt_shoes/shape_spec.rb | 6 +- .../shared_examples/common_methods_spec.rb | 2 +- spec/swt_shoes/spec_helper.rb | 4 +- testing/swt_test.rb | 2 +- 36 files changed, 530 insertions(+), 512 deletions(-) rename lib/{swt_shoes.rb => shoes/swt.rb} (53%) create mode 100644 lib/shoes/swt/animation.rb create mode 100644 lib/shoes/swt/app.rb create mode 100644 lib/shoes/swt/button.rb create mode 100644 lib/shoes/swt/color.rb rename lib/{swt_shoes => shoes/swt}/element_methods.rb (91%) create mode 100644 lib/shoes/swt/flow.rb create mode 100644 lib/shoes/swt/line.rb create mode 100644 lib/shoes/swt/oval.rb create mode 100644 lib/shoes/swt/shape.rb create mode 100644 lib/shoes/swt/sound.rb delete mode 100644 lib/swt_shoes/animation.rb delete mode 100644 lib/swt_shoes/app.rb delete mode 100644 lib/swt_shoes/button.rb delete mode 100644 lib/swt_shoes/color.rb delete mode 100644 lib/swt_shoes/flow.rb delete mode 100644 lib/swt_shoes/line.rb delete mode 100644 lib/swt_shoes/oval.rb delete mode 100644 lib/swt_shoes/shape.rb delete mode 100644 lib/swt_shoes/sound.rb diff --git a/bin/swt-shoooes b/bin/swt-shoooes index d2602436e..e5ba4a288 100755 --- a/bin/swt-shoooes +++ b/bin/swt-shoooes @@ -7,4 +7,4 @@ case "${MACHTYPE:-}" in esac jruby --1.9 ${swt_opt:-} -rrubygems -Ilib -rshoes -rshoes/configuration -e \ - "Shoes.configuration.framework = 'swt_shoes'; require '$1';" + "Shoes.configuration.framework = 'shoes/swt'; require '$1';" diff --git a/bin/swt-shoooes.bat b/bin/swt-shoooes.bat index 1989e60e4..d69918d36 100644 --- a/bin/swt-shoooes.bat +++ b/bin/swt-shoooes.bat @@ -1 +1 @@ -call jruby --1.9 -e "$:<< 'lib'; require 'shoes'; require 'shoes/configuration'; Shoes.configuration.framework = 'swt_shoes'; require '%1' " +call jruby --1.9 -e "$:<< 'lib'; require 'shoes'; require 'shoes/configuration'; Shoes.configuration.framework = 'shoes/swt'; require '%1' " diff --git a/bin/swt-shoooesd b/bin/swt-shoooesd index a29ab60b3..07664197e 100755 --- a/bin/swt-shoooesd +++ b/bin/swt-shoooesd @@ -8,10 +8,10 @@ ENV['log_level'] = 'debug' require 'shoes' require 'shoes/configuration' -Shoes.configuration.framework = 'swt_shoes' +Shoes.configuration.framework = 'shoes/swt' require ARGV[0] puts "Exiting ./shooesd. Have a good day!" -exit 0 \ No newline at end of file +exit 0 diff --git a/bin/swt-shoooesd.bat b/bin/swt-shoooesd.bat index 8abcb8a2d..765522d4f 100644 --- a/bin/swt-shoooesd.bat +++ b/bin/swt-shoooesd.bat @@ -1 +1 @@ -call jruby --1.9 --debug -e "$:<< 'lib'; require 'shoes'; require 'shoes/configuration'; Shoes.configuration.framework = 'swt_shoes'; require '%1' " +call jruby --1.9 --debug -e "$:<< 'lib'; require 'shoes'; require 'shoes/configuration'; Shoes.configuration.framework = 'shoes/swt'; require '%1' " diff --git a/lib/swt_shoes.rb b/lib/shoes/swt.rb similarity index 53% rename from lib/swt_shoes.rb rename to lib/shoes/swt.rb index dbae02d6e..f3e62cb5d 100644 --- a/lib/swt_shoes.rb +++ b/lib/shoes/swt.rb @@ -10,19 +10,19 @@ def window(*a, &b) Shoes.app(*a, &b) end -require 'swt_shoes/element_methods' -require 'swt_shoes/animation' -require 'swt_shoes/app' -#require 'swt_shoes/window' -require 'swt_shoes/flow' -require 'swt_shoes/button' -require 'swt_shoes/line' -require 'swt_shoes/oval' -require 'swt_shoes/shape' -require 'swt_shoes/color' -require 'swt_shoes/sound' - -module SwtShoes +require 'shoes/swt/element_methods' +require 'shoes/swt/animation' +require 'shoes/swt/app' +#require 'shoes/swt/window' +require 'shoes/swt/flow' +require 'shoes/swt/button' +require 'shoes/swt/line' +require 'shoes/swt/oval' +require 'shoes/swt/shape' +require 'shoes/swt/color' +require 'shoes/swt/sound' + +module Shoes::Swt module Shoes def self.app(opts={}, &blk) Shoes::App.new(opts, &blk) @@ -34,7 +34,7 @@ def self.logger end def self.display - Swt::Widgets::Display.getCurrent + ::Swt::Widgets::Display.getCurrent end end end diff --git a/lib/shoes/swt/animation.rb b/lib/shoes/swt/animation.rb new file mode 100644 index 000000000..96f46104f --- /dev/null +++ b/lib/shoes/swt/animation.rb @@ -0,0 +1,31 @@ +require 'shoes/timer_base' + +module Shoes + module Swt + module Animation + def gui_init + # Wrap the animation block so we can count frames. + # Note that the task re-calls itself on each run. + task = Proc.new do + @blk.call(@current_frame) + @current_frame += 1 + @app.gui_container.redraw + ::Swt.display.timer_exec (2000 / @framerate), task + end + ::Swt.display.timer_exec (2000 / @framerate), task + end + + #def stop + #end + + #def start + #end + end + end +end + +module Shoes + class Animation + include Shoes::Swt::Animation + end +end diff --git a/lib/shoes/swt/app.rb b/lib/shoes/swt/app.rb new file mode 100644 index 000000000..3b74b4280 --- /dev/null +++ b/lib/shoes/swt/app.rb @@ -0,0 +1,52 @@ +require 'swt' + +#require 'shoes/framework_adapters/swt_shoes/window' + +module Shoes + module Swt + + # Shoes::App.new creates a new Shoes application window! + # The default window is a [flow] + # + module App + + def gui_init + self.gui_container = container = ::Swt::Widgets::Shell.new(::Swt.display, ::Swt::SWT::CLOSE) + layout = ::Swt::Layout::RowLayout.new + container.setLayout(layout) + + opts = self.opts + + container.setSize(self.width, self.height) + container.setText(self.title) + + container.addListener(::Swt::SWT::Close, main_window_on_close) + end + + + def gui_open + self.gui_container.open + + ::Swt.event_loop { ::Swt.display.isDisposed } + + Shoes.logger.debug "::Swt.display disposed... exiting Shoes::App.new" + end + + private + def main_window_on_close + lambda { + Shoes.logger.debug "main_window on_close block begin... disposing ::Swt.display" + ::Swt.display.dispose + Shoes.logger.debug "::Swt.display disposed" + } + end + end + end +end + +module Shoes + class App + include Shoes::Swt::App + end +end + diff --git a/lib/shoes/swt/button.rb b/lib/shoes/swt/button.rb new file mode 100644 index 000000000..ff3e46a34 --- /dev/null +++ b/lib/shoes/swt/button.rb @@ -0,0 +1,48 @@ +module Shoes + module Swt + module Button + + def gui_button_init + # Create a button on the specified _shell_ + #def initialize(container, text = 'Button', opts = {}, &blk) + self.gui_element = button = ::Swt::Widgets::Button.new(self.gui_container, ::Swt::SWT::PUSH) + button.setText(self.text) + #@native_widget.setBounds(10, 10, 150, 30) + + button.addSelectionListener(self.click_event_lambda) if click_event_lambda + button.pack + end + + def move(left, top) + super(left, top) + unless gui_element.disposed? + # If this element is part of a layout, we need to pop it into its own + # composite layer before moving it, so the rest of of the elements in + # the layout can reflow. + if gui_container.get_layout + old_gui_container = self.gui_container + self.gui_container = ::Swt::Widgets::Composite.new(@app.gui_container, ::Swt::SWT::NO_BACKGROUND) + self.gui_element.dispose + self.gui_container.set_layout nil + self.gui_element = ::Swt::Widgets::Button.new(gui_container, ::Swt::SWT::PUSH).tap do |button| + button.set_text(self.text) + button.add_selection_listener(self.click_event_lambda) if click_event_lambda + button.pack + end + self.gui_container.set_bounds(0, 0, @app.gui_container.size.x, @app.gui_container.size.y) + self.gui_container.move_above(old_gui_container) + old_gui_container.layout + end + self.gui_element.set_location left, top + self.gui_element.redraw + end + end + end + end +end + +module Shoes + class Button + include Shoes::Swt::Button + end +end diff --git a/lib/shoes/swt/color.rb b/lib/shoes/swt/color.rb new file mode 100644 index 000000000..80a6827ef --- /dev/null +++ b/lib/shoes/swt/color.rb @@ -0,0 +1,15 @@ +module Shoes + module Swt + module Color + def to_native + ::Swt::Graphics::Color.new(Shoes.display, @red, @green, @blue) + end + end + end +end + +module Shoes + class Color + include Shoes::Swt::Color + end +end diff --git a/lib/swt_shoes/element_methods.rb b/lib/shoes/swt/element_methods.rb similarity index 91% rename from lib/swt_shoes/element_methods.rb rename to lib/shoes/swt/element_methods.rb index 34ca0498e..f88a2abc2 100644 --- a/lib/swt_shoes/element_methods.rb +++ b/lib/shoes/swt/element_methods.rb @@ -3,7 +3,8 @@ require 'white_shoes/element_methods' -module SwtShoes +module Shoes + module Swt module ElementMethods #def stack(opts={}, &blk) @@ -12,7 +13,7 @@ module ElementMethods #end #def flow(opts = {}, &blk) - # swt_flow = SwtShoes::Shoes::Flow.new(container, opts, &blk) + # swt_flow = Shoes::Swt::Shoes::Flow.new(container, opts, &blk) #end # @@ -64,7 +65,7 @@ def oval(*opts) paint_callback: lambda do |event, shape| #return if hidden? gc = event.gc - gc.set_antialias Swt::SWT::ON + gc.set_antialias ::Swt::SWT::ON gc.set_line_width shape.style[:strokewidth] gc.setForeground(shape.style[:stroke].to_native) gc.draw_oval(shape.left, shape.top, shape.width, shape.height) @@ -80,10 +81,11 @@ def shape(*opts) super(*opts, args) end end + end end module Shoes class App - include SwtShoes::ElementMethods + include Shoes::Swt::ElementMethods end end diff --git a/lib/shoes/swt/flow.rb b/lib/shoes/swt/flow.rb new file mode 100644 index 000000000..c43b87863 --- /dev/null +++ b/lib/shoes/swt/flow.rb @@ -0,0 +1,47 @@ +module Shoes + module Swt + # flow takes these options + # :margin - integer - add this many pixels to all 4 sides of the layout + + module Flow + + def gui_flow_init + self.gui_container = container = ::Swt::Widgets::Composite.new(self.parent_gui_container, ::Swt::SWT::NO_BACKGROUND) + + # RowLayout is horizontal by default, wrapping by default + layout = ::Swt::Layout::RowLayout.new + + # set the margins + set_margin(layout) + + if self.width && self.height + container.setSize(self.width, self.height) + end + + container.setLayout(layout) + end + + def gui_flow_add_to_parent + #self.parent_gui_container.add(self.gui_container) + end + + # Add this many pixels to margins on layout + def set_margin(layout) + if margin_pixels = self.margin + layout.marginTop = margin_pixels + layout.marginRight = margin_pixels + layout.marginBottom = margin_pixels + layout.marginLeft = margin_pixels + end + end + end + end +end + + +module Shoes + class Flow + include Shoes::Swt::Flow + end +end + diff --git a/lib/shoes/swt/line.rb b/lib/shoes/swt/line.rb new file mode 100644 index 000000000..388a0be8b --- /dev/null +++ b/lib/shoes/swt/line.rb @@ -0,0 +1,32 @@ +module Shoes + module Swt + module Line + attr_reader :gui_container, :gui_element + attr_reader :gui_paint_callback + + def gui_init + # @gui_opts must be provided if this shape is responsible for + # drawing itself. If this shape is part of another shape, then + # @gui_opts should be nil + if @gui_opts + @gui_container = @gui_opts[:container] + default_paint_callback = lambda do |event| + gc = event.gc + gc.set_antialias ::Swt::SWT::ON + gc.set_foreground self.stroke.to_native + gc.set_line_width self.style[:strokewidth] + gc.draw_line(@left, @top, right, bottom) + end + @gui_paint_callback = @gui_opts[:paint_callback] || default_paint_callback + @gui_container.add_paint_listener(@gui_paint_callback) + end + end + end + end +end + +module Shoes + class Line + include Shoes::Swt::Line + end +end diff --git a/lib/shoes/swt/oval.rb b/lib/shoes/swt/oval.rb new file mode 100644 index 000000000..19aef1711 --- /dev/null +++ b/lib/shoes/swt/oval.rb @@ -0,0 +1,34 @@ +module Shoes + module Swt + module Oval + attr_reader :gui_container, :gui_element + attr_reader :gui_paint_callback + + # FIXME: This (mostly) duplicates Shoes::Swt::Line#gui_init + def gui_init + # @gui_opts must be provided if this shape is responsible for + # drawing itself. If this shape is part of another shape, then + # @gui_opts should be nil + if @gui_opts + @gui_container = @gui_opts[:container] + @gui_paint_callback = lambda do |event| + gc = event.gc + gc.set_antialias ::Swt::SWT::ON + gc.set_background self.fill.to_native + gc.fill_oval(@left, @top, @width, @height) + gc.set_foreground self.stroke.to_native + gc.set_line_width self.style[:strokewidth] + gc.draw_oval(@left, @top, @width, @height) + end + @gui_container.add_paint_listener(@gui_paint_callback) + end + end + end + end +end + +module Shoes + class Oval + include Shoes::Swt::Oval + end +end diff --git a/lib/shoes/swt/shape.rb b/lib/shoes/swt/shape.rb new file mode 100644 index 000000000..0d6bcd919 --- /dev/null +++ b/lib/shoes/swt/shape.rb @@ -0,0 +1,96 @@ +module Shoes + module Swt + # Shape methods + # + # Including classes must provide instance variables: + # + # @x - the current x-value + # @y - the current y-value + # @opts - options + # + module Shape + attr_reader :gui_container, :gui_element + attr_reader :gui_paint_callback + + # The initialization hook for Shoes::Swt. + # + # Swt calls to Shoes::Shape#new should include in the style hash + # a :gui key that contains Swt-specific settings. + # + # Example: + # + # args[:top] = 100 + # args[:width] = 80 + # args[:gui] = { + # container: self.gui_container, + # paint_callback: lambda do |event, shape| + # gc = event.gc + # gc.set_antialias ::Swt::SWT::ON + # gc.set_line_width 1 + # gc.draw_oval(shape.left, shape.top, shape.width, shape.height) + # end + # } + # + # This hook method is only interested in the `:gui` values, which will + # be passed in the @gui_opts variable. + # + # Note in particular the `:paint_callback`. It has 2 parameters. The + # first is the Swt event that will trigger the callback. The second is + # this shape. + def gui_init + # @gui_opts must be provided if this shape is responsible for + # drawing itself. If this shape is part of another shape, then + # @gui_opts should be nil + if @gui_opts + @gui_container = @gui_opts[:container] + @gui_element = @gui_opts[:element] || ::Swt::Path.new(::Swt.display) + #@transform.translate(-130, -100) + @gui_paint_callback = lambda do |event| + gc = event.gc + @transform = ::Swt::Transform.new(::Swt.display) unless @transform + gc.setTransform(@transform) + gc.set_background self.fill.to_native + gc.fill_path(@gui_element) + gc.set_antialias ::Swt::SWT::ON + gc.set_foreground self.stroke.to_native + gc.set_line_width self.style[:strokewidth] + gc.draw_path(@gui_element) + @transform.dispose + end + @gui_container.add_paint_listener(@gui_paint_callback) + end + end + + def line_to(x, y) + @components << ::Shoes::Line.new(@x, @y, x, y, @style) + @gui_element.line_to(x, y) + end + + def move_to(x, y) + @x, @y = x, y + @gui_element.move_to(x, y) + end + + def move(left, top) + super left, top + @transform = ::Swt::Transform.new(::Swt.display) + @transform.translate(self.left, self.top) + end + end + end +end + +module Shoes + class Shape + # This is a hack to allow methods in this class to override methods + # defined in Shoes::Shape. It would be better fixed by a different + # architecture. The self.extend doesn't work if the module has already + # been included by the class. + #include Shoes::Swt::Shape + alias :old_initialize :initialize + def initialize(opts = {}, blk = nil) + self.extend Shoes::Swt::Shape + old_initialize opts, blk + end + end +end diff --git a/lib/shoes/swt/sound.rb b/lib/shoes/swt/sound.rb new file mode 100644 index 000000000..9eb9c8d2d --- /dev/null +++ b/lib/shoes/swt/sound.rb @@ -0,0 +1,122 @@ +# JavaZOOM Sound-API Projects +# - Shared lib +require 'support/tritonus_share.jar' +# - MP3 lib +require 'support/mp3spi1.9.5.jar' +require 'support/jl1.0.1.jar' +# - Ogg/Vorbis lib +require 'support/jogg-0.0.7.jar' +require 'support/jorbis-0.0.15.jar' +require 'support/vorbisspi1.0.3.jar' + +module Shoes + module Swt + module Sound + JFile = java.io.File + import java.io.BufferedInputStream + import javax.sound.sampled + import java.io.IOException + + BufferSize = 4096 + + attr_accessor :mixer_channel, :audio_input_stream, :audio_format + + def gui_sound_init + # noop for java.sound.SourceDataLine implementation + end + + def gui_sound_play + Thread.new do + begin + sound_file = JFile.new(self.filepath) + + audio_input_stream = AudioSystem.getAudioInputStream(sound_file) + audio_format = audio_input_stream.getFormat + + decoded_audio_format, decoded_audio_input_stream = decode_input_stream(audio_format, audio_input_stream) + + # Play now. + rawplay(decoded_audio_format, decoded_audio_input_stream) + audio_input_stream.close + + rescue UnsupportedAudioFileException => uafex + puts uafex.inspect, uafex.backtrace + rescue IOException => ioex + puts ioex.inspect, ioex.backtrace + #rescue JIOException => jioex + # jioex.stacktrace + rescue LineUnavailableException => luex + puts luex.inspect, luex.backtrace + rescue Exception => e + puts e.inspect, e.backtrace + end + end + end + + def decode_input_stream(audio_format, audio_input_stream) + case audio_format.encoding + when Java::JavazoomSpiVorbisSampledFile::VorbisEncoding, Java::JavazoomSpiMpegSampledFile::MpegEncoding + decoded_format = AudioFormat.new(AudioFormat::Encoding::PCM_SIGNED, + audio_format.getSampleRate(), + 16, + audio_format.getChannels(), + audio_format.getChannels() * 2, + audio_format.getSampleRate(), + false) + decoded_audio_input_stream = AudioSystem.getAudioInputStream(decoded_format, audio_input_stream) + + return decoded_format, decoded_audio_input_stream + + else + return audio_format, audio_input_stream + end + end + + def rawplay(decoded_audio_format, decoded_audio_input_stream) + + #throws IOException, LineUnavailableException + + sampled_data = Java::byte[BufferSize].new + + line = getLine(decoded_audio_format) + if line != nil + + # Start + line.start() + bytes_read = 0, bytes_written = 0 + while bytes_read != -1 + + bytes_read = decoded_audio_input_stream.read(sampled_data, 0, sampled_data.length) + if bytes_read != -1 + + bytes_written = line.write(sampled_data, 0, bytes_read) + end + end + # Stop + line.drain() + line.stop() + line.close() + decoded_audio_input_stream.close() + end + end + + def getLine(audioFormat) + + #throws LineUnavailableException + + res = nil + info = DataLine::Info.new(SourceDataLine.java_class, audioFormat) + res = AudioSystem.getLine(info) + res.open(audioFormat) + res + end + end + end +end + + +module Shoes + class Sound + include Shoes::Swt::Sound + end +end diff --git a/lib/swt_shoes/animation.rb b/lib/swt_shoes/animation.rb deleted file mode 100644 index dffecdf12..000000000 --- a/lib/swt_shoes/animation.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'shoes/timer_base' - -module SwtShoes - module Animation - def gui_init - # Wrap the animation block so we can count frames. - # Note that the task re-calls itself on each run. - task = Proc.new do - @blk.call(@current_frame) - @current_frame += 1 - @app.gui_container.redraw - Swt.display.timer_exec (2000 / @framerate), task - end - Swt.display.timer_exec (2000 / @framerate), task - end - - #def stop - #end - - #def start - #end - end -end - -module Shoes - class Animation - include SwtShoes::Animation - end -end diff --git a/lib/swt_shoes/app.rb b/lib/swt_shoes/app.rb deleted file mode 100644 index a843e6074..000000000 --- a/lib/swt_shoes/app.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'swt' - -#require 'shoes/framework_adapters/swt_shoes/window' - -module SwtShoes - - # Shoes::App.new creates a new Shoes application window! - # The default window is a [flow] - # - module App - - def gui_init - self.gui_container = container = Swt::Widgets::Shell.new(Swt.display, Swt::SWT::CLOSE) - layout = Swt::Layout::RowLayout.new - container.setLayout(layout) - - opts = self.opts - - container.setSize(self.width, self.height) - container.setText(self.title) - - container.addListener(Swt::SWT::Close, main_window_on_close) - end - - - def gui_open - self.gui_container.open - - Swt.event_loop { Swt.display.isDisposed } - - Shoes.logger.debug "Swt.display disposed... exiting Shoes::App.new" - end - - private - def main_window_on_close - lambda { - Shoes.logger.debug "main_window on_close block begin... disposing Swt.display" - Swt.display.dispose - Shoes.logger.debug "Swt.display disposed" - } - end - end -end - -module Shoes - class App - include SwtShoes::App - end -end - diff --git a/lib/swt_shoes/button.rb b/lib/swt_shoes/button.rb deleted file mode 100644 index 193599e66..000000000 --- a/lib/swt_shoes/button.rb +++ /dev/null @@ -1,46 +0,0 @@ -module SwtShoes - module Button - - def gui_button_init - # Create a button on the specified _shell_ - #def initialize(container, text = 'Button', opts = {}, &blk) - self.gui_element = button = Swt::Widgets::Button.new(self.gui_container, Swt::SWT::PUSH) - button.setText(self.text) - #@native_widget.setBounds(10, 10, 150, 30) - - button.addSelectionListener(self.click_event_lambda) if click_event_lambda - button.pack - end - - def move(left, top) - super(left, top) - unless gui_element.disposed? - # If this element is part of a layout, we need to pop it into its own - # composite layer before moving it, so the rest of of the elements in - # the layout can reflow. - if gui_container.get_layout - old_gui_container = self.gui_container - self.gui_container = Swt::Widgets::Composite.new(@app.gui_container, Swt::SWT::NO_BACKGROUND) - self.gui_element.dispose - self.gui_container.set_layout nil - self.gui_element = Swt::Widgets::Button.new(gui_container, Swt::SWT::PUSH).tap do |button| - button.set_text(self.text) - button.add_selection_listener(self.click_event_lambda) if click_event_lambda - button.pack - end - self.gui_container.set_bounds(0, 0, @app.gui_container.size.x, @app.gui_container.size.y) - self.gui_container.move_above(old_gui_container) - old_gui_container.layout - end - self.gui_element.set_location left, top - self.gui_element.redraw - end - end - end -end - -module Shoes - class Button - include SwtShoes::Button - end -end diff --git a/lib/swt_shoes/color.rb b/lib/swt_shoes/color.rb deleted file mode 100644 index 33f4b1a4e..000000000 --- a/lib/swt_shoes/color.rb +++ /dev/null @@ -1,13 +0,0 @@ -module SwtShoes - module Color - def to_native - Swt::Graphics::Color.new(Shoes.display, @red, @green, @blue) - end - end -end - -module Shoes - class Color - include SwtShoes::Color - end -end diff --git a/lib/swt_shoes/flow.rb b/lib/swt_shoes/flow.rb deleted file mode 100644 index 21b8679c4..000000000 --- a/lib/swt_shoes/flow.rb +++ /dev/null @@ -1,45 +0,0 @@ -module SwtShoes -# flow takes these options -# :margin - integer - add this many pixels to all 4 sides of the layout - - module Flow - - def gui_flow_init - self.gui_container = container = Swt::Widgets::Composite.new(self.parent_gui_container, Swt::SWT::NO_BACKGROUND) - - # RowLayout is horizontal by default, wrapping by default - layout = Swt::Layout::RowLayout.new - - # set the margins - set_margin(layout) - - if self.width && self.height - container.setSize(self.width, self.height) - end - - container.setLayout(layout) - end - - def gui_flow_add_to_parent - #self.parent_gui_container.add(self.gui_container) - end - - # Add this many pixels to margins on layout - def set_margin(layout) - if margin_pixels = self.margin - layout.marginTop = margin_pixels - layout.marginRight = margin_pixels - layout.marginBottom = margin_pixels - layout.marginLeft = margin_pixels - end - end - end -end - - -module Shoes - class Flow - include SwtShoes::Flow - end -end - diff --git a/lib/swt_shoes/line.rb b/lib/swt_shoes/line.rb deleted file mode 100644 index 4ac554820..000000000 --- a/lib/swt_shoes/line.rb +++ /dev/null @@ -1,30 +0,0 @@ -module SwtShoes - module Line - attr_reader :gui_container, :gui_element - attr_reader :gui_paint_callback - - def gui_init - # @gui_opts must be provided if this shape is responsible for - # drawing itself. If this shape is part of another shape, then - # @gui_opts should be nil - if @gui_opts - @gui_container = @gui_opts[:container] - default_paint_callback = lambda do |event| - gc = event.gc - gc.set_antialias Swt::SWT::ON - gc.set_foreground self.stroke.to_native - gc.set_line_width self.style[:strokewidth] - gc.draw_line(@left, @top, right, bottom) - end - @gui_paint_callback = @gui_opts[:paint_callback] || default_paint_callback - @gui_container.add_paint_listener(@gui_paint_callback) - end - end - end -end - -module Shoes - class Line - include SwtShoes::Line - end -end diff --git a/lib/swt_shoes/oval.rb b/lib/swt_shoes/oval.rb deleted file mode 100644 index 6ab26fa3a..000000000 --- a/lib/swt_shoes/oval.rb +++ /dev/null @@ -1,32 +0,0 @@ -module SwtShoes - module Oval - attr_reader :gui_container, :gui_element - attr_reader :gui_paint_callback - - # FIXME: This (mostly) duplicates SwtShoes::Line#gui_init - def gui_init - # @gui_opts must be provided if this shape is responsible for - # drawing itself. If this shape is part of another shape, then - # @gui_opts should be nil - if @gui_opts - @gui_container = @gui_opts[:container] - @gui_paint_callback = lambda do |event| - gc = event.gc - gc.set_antialias Swt::SWT::ON - gc.set_background self.fill.to_native - gc.fill_oval(@left, @top, @width, @height) - gc.set_foreground self.stroke.to_native - gc.set_line_width self.style[:strokewidth] - gc.draw_oval(@left, @top, @width, @height) - end - @gui_container.add_paint_listener(@gui_paint_callback) - end - end - end -end - -module Shoes - class Oval - include SwtShoes::Oval - end -end diff --git a/lib/swt_shoes/shape.rb b/lib/swt_shoes/shape.rb deleted file mode 100644 index 8b5506f59..000000000 --- a/lib/swt_shoes/shape.rb +++ /dev/null @@ -1,94 +0,0 @@ -module SwtShoes - # Shape methods - # - # Including classes must provide instance variables: - # - # @x - the current x-value - # @y - the current y-value - # @opts - options - # - module Shape - attr_reader :gui_container, :gui_element - attr_reader :gui_paint_callback - - # The initialization hook for SwtShoes. - # - # Swt calls to Shoes::Shape#new should include in the style hash - # a :gui key that contains Swt-specific settings. - # - # Example: - # - # args[:top] = 100 - # args[:width] = 80 - # args[:gui] = { - # container: self.gui_container, - # paint_callback: lambda do |event, shape| - # gc = event.gc - # gc.set_antialias Swt::SWT::ON - # gc.set_line_width 1 - # gc.draw_oval(shape.left, shape.top, shape.width, shape.height) - # end - # } - # - # This hook method is only interested in the `:gui` values, which will - # be passed in the @gui_opts variable. - # - # Note in particular the `:paint_callback`. It has 2 parameters. The - # first is the Swt event that will trigger the callback. The second is - # this shape. - def gui_init - # @gui_opts must be provided if this shape is responsible for - # drawing itself. If this shape is part of another shape, then - # @gui_opts should be nil - if @gui_opts - @gui_container = @gui_opts[:container] - @gui_element = @gui_opts[:element] || Swt::Path.new(Swt.display) - #@transform.translate(-130, -100) - @gui_paint_callback = lambda do |event| - gc = event.gc - @transform = Swt::Transform.new(Swt.display) unless @transform - gc.setTransform(@transform) - gc.set_background self.fill.to_native - gc.fill_path(@gui_element) - gc.set_antialias Swt::SWT::ON - gc.set_foreground self.stroke.to_native - gc.set_line_width self.style[:strokewidth] - gc.draw_path(@gui_element) - @transform.dispose - end - @gui_container.add_paint_listener(@gui_paint_callback) - end - end - - def line_to(x, y) - @components << ::Shoes::Line.new(@x, @y, x, y, @style) - @gui_element.line_to(x, y) - end - - def move_to(x, y) - @x, @y = x, y - @gui_element.move_to(x, y) - end - - def move(left, top) - super left, top - @transform = Swt::Transform.new(Swt.display) - @transform.translate(self.left, self.top) - end - end -end - -module Shoes - class Shape - # This is a hack to allow methods in this class to override methods - # defined in Shoes::Shape. It would be better fixed by a different - # architecture. The self.extend doesn't work if the module has already - # been included by the class. - #include SwtShoes::Shape - alias :old_initialize :initialize - def initialize(opts = {}, blk = nil) - self.extend SwtShoes::Shape - old_initialize opts, blk - end - end -end diff --git a/lib/swt_shoes/sound.rb b/lib/swt_shoes/sound.rb deleted file mode 100644 index fc1f453dd..000000000 --- a/lib/swt_shoes/sound.rb +++ /dev/null @@ -1,120 +0,0 @@ -# JavaZOOM Sound-API Projects -# - Shared lib -require 'support/tritonus_share.jar' -# - MP3 lib -require 'support/mp3spi1.9.5.jar' -require 'support/jl1.0.1.jar' -# - Ogg/Vorbis lib -require 'support/jogg-0.0.7.jar' -require 'support/jorbis-0.0.15.jar' -require 'support/vorbisspi1.0.3.jar' - -module SwtShoes - module Sound - JFile = java.io.File - import java.io.BufferedInputStream - import javax.sound.sampled - import java.io.IOException - - BufferSize = 4096 - - attr_accessor :mixer_channel, :audio_input_stream, :audio_format - - def gui_sound_init - # noop for java.sound.SourceDataLine implementation - end - - def gui_sound_play - Thread.new do - begin - sound_file = JFile.new(self.filepath) - - audio_input_stream = AudioSystem.getAudioInputStream(sound_file) - audio_format = audio_input_stream.getFormat - - decoded_audio_format, decoded_audio_input_stream = decode_input_stream(audio_format, audio_input_stream) - - # Play now. - rawplay(decoded_audio_format, decoded_audio_input_stream) - audio_input_stream.close - - rescue UnsupportedAudioFileException => uafex - puts uafex.inspect, uafex.backtrace - rescue IOException => ioex - puts ioex.inspect, ioex.backtrace - #rescue JIOException => jioex - # jioex.stacktrace - rescue LineUnavailableException => luex - puts luex.inspect, luex.backtrace - rescue Exception => e - puts e.inspect, e.backtrace - end - end - end - - def decode_input_stream(audio_format, audio_input_stream) - case audio_format.encoding - when Java::JavazoomSpiVorbisSampledFile::VorbisEncoding, Java::JavazoomSpiMpegSampledFile::MpegEncoding - decoded_format = AudioFormat.new(AudioFormat::Encoding::PCM_SIGNED, - audio_format.getSampleRate(), - 16, - audio_format.getChannels(), - audio_format.getChannels() * 2, - audio_format.getSampleRate(), - false) - decoded_audio_input_stream = AudioSystem.getAudioInputStream(decoded_format, audio_input_stream) - - return decoded_format, decoded_audio_input_stream - - else - return audio_format, audio_input_stream - end - end - - def rawplay(decoded_audio_format, decoded_audio_input_stream) - - #throws IOException, LineUnavailableException - - sampled_data = Java::byte[BufferSize].new - - line = getLine(decoded_audio_format) - if line != nil - - # Start - line.start() - bytes_read = 0, bytes_written = 0 - while bytes_read != -1 - - bytes_read = decoded_audio_input_stream.read(sampled_data, 0, sampled_data.length) - if bytes_read != -1 - - bytes_written = line.write(sampled_data, 0, bytes_read) - end - end - # Stop - line.drain() - line.stop() - line.close() - decoded_audio_input_stream.close() - end - end - - def getLine(audioFormat) - - #throws LineUnavailableException - - res = nil - info = DataLine::Info.new(SourceDataLine.java_class, audioFormat) - res = AudioSystem.getLine(info) - res.open(audioFormat) - res - end - end -end - - -module Shoes - class Sound - include SwtShoes::Sound - end -end diff --git a/spec/swt_shoes/animation_spec.rb b/spec/swt_shoes/animation_spec.rb index 1a4d6ce43..dd761d83a 100644 --- a/spec/swt_shoes/animation_spec.rb +++ b/spec/swt_shoes/animation_spec.rb @@ -1,8 +1,8 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Animation do +describe Shoes::Swt::Animation do class AnimationShoeLaces - include SwtShoes::Animation + include Shoes::Swt::Animation # This is painfully duplicated from Shoes::Animation def initialize(*opts, &blk) @style = opts.last.class == Hash ? opts.pop : {} @@ -15,12 +15,12 @@ def initialize(*opts, &blk) end let(:block) { Proc.new {} } - let(:display) { Swt.display } + let(:display) { ::Swt.display } let(:gui_container) { double(:gui_container) } subject { AnimationShoeLaces.new &block } it "injects into Shoes::Animation" do - Shoes::Animation.ancestors.should include(SwtShoes::Animation) + Shoes::Animation.ancestors.should include(Shoes::Swt::Animation) end it "triggers an Swt timer" do diff --git a/spec/swt_shoes/app_spec.rb b/spec/swt_shoes/app_spec.rb index 5e8b5c966..e0610af13 100644 --- a/spec/swt_shoes/app_spec.rb +++ b/spec/swt_shoes/app_spec.rb @@ -1,13 +1,13 @@ require "swt_shoes/spec_helper" -describe SwtShoes::App do +describe Shoes::Swt::App do let(:mock_shell) { mock(:swt_shell, :setSize => true, :setText => true, :addListener => true, :setLayout => true) } before :each do - Swt::Widgets::Shell.stub(:new) { mock_shell } - Swt.stub(:event_loop) + ::Swt::Widgets::Shell.stub(:new) { mock_shell } + ::Swt.stub(:event_loop) end describe Shoes::App do @@ -22,9 +22,9 @@ context "Shoes::App ancestors" do subject { Shoes::App.ancestors } - it { should include(SwtShoes::ElementMethods) } - it "uses SwtShoes::ElementMethods before Shoes::ElementMethods" do - framework_index = subject.index(SwtShoes::ElementMethods) + it { should include(Shoes::Swt::ElementMethods) } + it "uses Shoes::Swt::ElementMethods before Shoes::ElementMethods" do + framework_index = subject.index(Shoes::Swt::ElementMethods) shoes_index = subject.index(Shoes::ElementMethods) framework_index.should be < shoes_index end diff --git a/spec/swt_shoes/button_spec.rb b/spec/swt_shoes/button_spec.rb index 73f1bf5bf..6f6395f38 100644 --- a/spec/swt_shoes/button_spec.rb +++ b/spec/swt_shoes/button_spec.rb @@ -2,7 +2,7 @@ #require 'support/shared_examples_for_common_elements_spec' -describe SwtShoes::Button do +describe Shoes::Swt::Button do #it_should_behave_like "A Common Element" @@ -10,7 +10,7 @@ class ButtonShoeLaces attr_accessor :gui_container, :gui_element, :text, :height, :width, :margin, :click_event_lambda attr_accessor :app - # because SwtShoes::Button#move calls super :( + # because Shoes::Swt::Button#move calls super :( def move(left, top) # no-op end diff --git a/spec/swt_shoes/color_spec.rb b/spec/swt_shoes/color_spec.rb index f43203204..a853064a9 100644 --- a/spec/swt_shoes/color_spec.rb +++ b/spec/swt_shoes/color_spec.rb @@ -1,6 +1,6 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Color do +describe Shoes::Swt::Color do subject { Shoes::COLORS[:salmon].to_native } its(:class) { should eq(Java::OrgEclipseSwtGraphics::Color) } diff --git a/spec/swt_shoes/element_methods_spec.rb b/spec/swt_shoes/element_methods_spec.rb index 5b017b5d3..0c44db863 100644 --- a/spec/swt_shoes/element_methods_spec.rb +++ b/spec/swt_shoes/element_methods_spec.rb @@ -6,14 +6,12 @@ class ElementMethodsShoeLaces attr_accessor :gui_container include Shoes::ElementMethods - include SwtShoes::ElementMethods + include Shoes::Swt::ElementMethods def initialize @style = {} end end - Shoes.configuration.framework = 'swt_shoes' - let(:gui_container) { double('gui_container') } let(:app) { ElementMethodsShoeLaces.new.tap { |a| diff --git a/spec/swt_shoes/flow_spec.rb b/spec/swt_shoes/flow_spec.rb index a937d81e8..112e0b8d9 100644 --- a/spec/swt_shoes/flow_spec.rb +++ b/spec/swt_shoes/flow_spec.rb @@ -1,13 +1,13 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Flow do +describe Shoes::Swt::Flow do class FlowShoeLaces - include SwtShoes::Flow + include Shoes::Swt::Flow attr_accessor :parent_gui_container, :gui_container, :opts, :width, :height, :margin end - let(:parent_gui_container) { Swt.display } + let(:parent_gui_container) { ::Swt.display } let(:mock_slot) { mock(:slot) } let(:shoelace) { shoelace = FlowShoeLaces.new @@ -18,7 +18,7 @@ class FlowShoeLaces describe "gui_flow_init" do before do - Swt::Widgets::Composite.should_receive(:new).with(parent_gui_container, anything).and_return mock_slot + ::Swt::Widgets::Composite.should_receive(:new).with(parent_gui_container, anything).and_return mock_slot end it "should create a composite and set accessor" do mock_slot.stub(:setLayout) @@ -43,7 +43,7 @@ class FlowShoeLaces mock_slot.stub(:setLayout) shoelace.margin = 131 mock_layout = mock(:layout) - Swt::Layout::RowLayout.should_receive(:new).and_return mock_layout + ::Swt::Layout::RowLayout.should_receive(:new).and_return mock_layout mock_layout.should_receive(:marginTop=).with 131 mock_layout.should_receive(:marginRight=).with 131 mock_layout.should_receive(:marginBottom=).with 131 diff --git a/spec/swt_shoes/line_spec.rb b/spec/swt_shoes/line_spec.rb index 69418503f..cce1728b9 100644 --- a/spec/swt_shoes/line_spec.rb +++ b/spec/swt_shoes/line_spec.rb @@ -1,6 +1,6 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Line do +describe Shoes::Swt::Line do let(:gui_container) { double("gui container") } let(:opts) { {:gui => {:container => gui_container}} } diff --git a/spec/swt_shoes/oval_spec.rb b/spec/swt_shoes/oval_spec.rb index 2ae3d661a..0aeba5b71 100644 --- a/spec/swt_shoes/oval_spec.rb +++ b/spec/swt_shoes/oval_spec.rb @@ -1,6 +1,6 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Oval do +describe Shoes::Swt::Oval do let(:gui_container) { double("gui container") } let(:opts) { {:gui => {:container => gui_container}} } diff --git a/spec/swt_shoes/shape_spec.rb b/spec/swt_shoes/shape_spec.rb index 65a69c714..dfc0affce 100644 --- a/spec/swt_shoes/shape_spec.rb +++ b/spec/swt_shoes/shape_spec.rb @@ -1,9 +1,9 @@ require 'swt_shoes/spec_helper' -describe SwtShoes::Shape do +describe Shoes::Swt::Shape do class ShapeShoeLaces - include SwtShoes::Shape + include Shoes::Swt::Shape attr_accessor :gui_container attr_reader :gui_element attr_reader :blk @@ -27,7 +27,7 @@ def initialize(opts = {}, blk = nil ) it "uses Swt and not White Shoes" do ancestors = subject.class.ancestors.map(&:name) - ancestors.should include('SwtShoes::Shape') + ancestors.should include('Shoes::Swt::Shape') ancestors.should_not include('WhiteShoes::Shape') subject end diff --git a/spec/swt_shoes/shared_examples/common_methods_spec.rb b/spec/swt_shoes/shared_examples/common_methods_spec.rb index a50447657..51d9a8a8e 100644 --- a/spec/swt_shoes/shared_examples/common_methods_spec.rb +++ b/spec/swt_shoes/shared_examples/common_methods_spec.rb @@ -1,7 +1,7 @@ # Requires gui_element shared_examples_for "movable object with disposable gui element" do before :each do - Swt::Widgets::Composite.stub(:new) { double("composite").as_null_object } + ::Swt::Widgets::Composite.stub(:new) { double("composite").as_null_object } end it "disposes its gui element" do diff --git a/spec/swt_shoes/spec_helper.rb b/spec/swt_shoes/spec_helper.rb index a32a472ff..20e3072fb 100644 --- a/spec/swt_shoes/spec_helper.rb +++ b/spec/swt_shoes/spec_helper.rb @@ -1,6 +1,6 @@ require "spec_helper" -require "swt_shoes" -Shoes.configuration.framework = 'swt_shoes' +require "shoes/swt" +Shoes.configuration.framework = 'shoes/swt' shared_examples = File.join(File.expand_path(File.dirname(__FILE__)), 'shared_examples', '**/*.rb') Dir[shared_examples].each { |f| require f } diff --git a/testing/swt_test.rb b/testing/swt_test.rb index ca61bbb62..45e98e898 100644 --- a/testing/swt_test.rb +++ b/testing/swt_test.rb @@ -4,7 +4,7 @@ class Object include_package 'org.eclipse.swt' include_package 'org.eclipse.swt.widgets' end - display = Swt.display + display = ::Swt.display shell = Shell.new display shell.setSize 300, 300 shell.open