diff --git a/lib/shoes/arc.rb b/lib/shoes/arc.rb index 92c8b0d2d..717d0f380 100644 --- a/lib/shoes/arc.rb +++ b/lib/shoes/arc.rb @@ -1,13 +1,11 @@ class Shoes class Arc include CommonMethods - include Common::Fill - include Common::Stroke include Common::Style include Common::Clickable include DimensionsDelegations - attr_reader :app, :parent, :dimensions + attr_reader :app, :dimensions, :parent, :gui style_with :angle1, :angle2, :art_styles, :center, :dimensions, :radius, :wedge STYLES = {wedge: false} diff --git a/lib/shoes/common/style.rb b/lib/shoes/common/style.rb index ed6cfb8ad..31d7645a4 100644 --- a/lib/shoes/common/style.rb +++ b/lib/shoes/common/style.rb @@ -30,7 +30,6 @@ def style(new_styles = nil) end def style_init(arg_styles, new_styles = {}) - default_element_styles = {} default_element_styles = self.class::STYLES if defined? self.class::STYLES @@ -63,7 +62,6 @@ def unpack_style_groups(styles) end def define_accessor_methods - needs_accessors = @supported_styles.reject do |style| self.method_defined?(style) end diff --git a/lib/shoes/dsl.rb b/lib/shoes/dsl.rb index 223776c07..e1671790c 100644 --- a/lib/shoes/dsl.rb +++ b/lib/shoes/dsl.rb @@ -184,7 +184,7 @@ def sound(soundfile, opts = {}, &blk) # @option opts [Boolean] wedge (false) # @option opts [Boolean] center (false) is (left, top) the center of the rectangle? def arc(left, top, width, height, angle1, angle2, opts = {}) - create Shoes::Arc, left, top, width, height, angle1, angle2, opts#style.merge(arc_style) + create Shoes::Arc, left, top, width, height, angle1, angle2, opts end # Draws a line from point A (x1,y1) to point B (x2,y2) @@ -221,7 +221,8 @@ def line(x1, y1, x2, y2, opts = {}) # @option styles [Integer] top (0) the y-coordinate of the top-left corner # @option styles [Boolean] center (false) is (left, top) the center of the oval def oval(*opts, &blk) - oval_style = style_normalizer.normalize pop_style(opts) + oval_style = pop_style(opts) + oval_style = style_normalizer.normalize(oval_style) case opts.length when 3 left, top, width = opts @@ -243,7 +244,7 @@ def oval(*opts, &blk) EOS raise ArgumentError, message end - create Shoes::Oval, left, top, width, height, style.merge(oval_style), &blk + create Shoes::Oval, left, top, width, height, oval_style, &blk end # Creates a rectangle diff --git a/lib/shoes/oval.rb b/lib/shoes/oval.rb index 1eb560918..5ed0d25d0 100644 --- a/lib/shoes/oval.rb +++ b/lib/shoes/oval.rb @@ -1,28 +1,23 @@ class Shoes class Oval include CommonMethods - include Common::Fill - include Common::Stroke include Common::Style include Common::Clickable include DimensionsDelegations - attr_reader :app, :dimensions, :angle, :gui, :parent + attr_reader :app, :dimensions, :parent, :gui + style_with :art_styles, :center, :radius - def initialize(app, parent, left, top, width, height, opts = {}, &blk) + def initialize(app, parent, left, top, width, height, styles = {}, &blk) @app = app - @dimensions = AbsoluteDimensions.new left, top, width, height, opts - @style = Shoes::Common::Fill::DEFAULTS.merge( - Shoes::Common::Stroke::DEFAULTS).merge(opts) - @style[:strokewidth] ||= 1 - @angle = opts[:angle] + @dimensions = AbsoluteDimensions.new left, top, width, height, styles @parent = parent - + + style_init(styles) @parent.add_child self - @gui = Shoes.backend_for(self, &blk) - clickable_options(opts) + clickable_options(styles) end end end diff --git a/spec/shoes/arc_spec.rb b/spec/shoes/arc_spec.rb index 90178d5f1..d0aca2a65 100644 --- a/spec/shoes/arc_spec.rb +++ b/spec/shoes/arc_spec.rb @@ -17,21 +17,9 @@ it_behaves_like "object with style" it_behaves_like "object with dimensions" it_behaves_like "left, top as center", :start_angle, :end_angle - it_behaves_like 'object with parent' - - #unpack styles. In the future we'd like to do something clever to avoid this duplication. - supported_styles = [] - %w[art_styles cap center dimensions radius].map(&:to_sym).each do |style| - if Shoes::Common::Style::STYLE_GROUPS[style] - Shoes::Common::Style::STYLE_GROUPS[style].each{|style| supported_styles << style} - else - supported_styles << style - end - end - - supported_styles.each do |style| - it_behaves_like "object that styles with #{style}" - end + it_behaves_like "object with parent" + + it_styles_with :art_styles, :center, :dimensions, :radius it "is a Shoes::Arc" do expect(arc.class).to be(Shoes::Arc) diff --git a/spec/shoes/oval_spec.rb b/spec/shoes/oval_spec.rb index 4dc877d85..e81dc5a11 100644 --- a/spec/shoes/oval_spec.rb +++ b/spec/shoes/oval_spec.rb @@ -10,11 +10,12 @@ describe "basic" do subject { Shoes::Oval.new(app, parent, left, top, width, height) } - it_behaves_like "object with stroke" - it_behaves_like "object with fill" + it_behaves_like "object with style" it_behaves_like "object with dimensions" it_behaves_like "movable object" it_behaves_like "left, top as center" - it_behaves_like 'object with parent' + it_behaves_like "object with parent" + + it_styles_with :art_styles, :center, :dimensions, :radius end end diff --git a/spec/shoes/shared_examples/dsl/oval.rb b/spec/shoes/shared_examples/dsl/oval.rb index 8b4e02ad2..2e1be55e4 100644 --- a/spec/shoes/shared_examples/dsl/oval.rb +++ b/spec/shoes/shared_examples/dsl/oval.rb @@ -9,6 +9,14 @@ expect(oval.width).to eq(width) expect(oval.height).to eq(height) end + + it "sets stroke" do + expect(oval.style[:stroke]).to eq(stroke) + end + + it "sets fill" do + expect(oval.style[:fill]).to eq(fill) + end end shared_examples_for "oval DSL method" do @@ -16,45 +24,65 @@ let(:top) { 30 } let(:width) { 100 } let(:height) { 200 } + let(:stroke) { Shoes::COLORS[:black] } + let(:fill) { Shoes::COLORS[:black] } - let(:oval) { dsl.oval(left, top, width, height) } + describe "creating an oval" do + describe "with explicit arguments" do + let(:oval) { dsl.oval(left, top, width, height) } + it_behaves_like "an oval/circle element" + end - context "eccentric, from explicit arguments" do - it_behaves_like "an oval/circle element" - end + describe "with stroke and fill styles" do + let(:stroke) { Shoes::COLORS.fetch :orchid } + let(:fill) { Shoes::COLORS.fetch :lemonchiffon } - it "raises an ArgumentError" do - expect { dsl.oval(10) }.to raise_error(ArgumentError) + describe "as colors" do + let(:oval) { dsl.oval(left, top, width, height, stroke: stroke, fill: fill) } + it_behaves_like "an oval/circle element" + end + + describe "as hex strings" do + let(:oval) { dsl.oval(left, top, width, height, stroke: stroke.hex, fill: fill.hex) } + it_behaves_like "an oval/circle element" + end + end + + describe "with too few arguments" do + it "raises an ArgumentError" do + expect { dsl.oval(10) }.to raise_error(ArgumentError) + end + end end - context "a circle" do + describe "creating a circle" do let(:width) { height } - describe "when constructed from explicit arguments" do + describe "with explicit arguments" do + let(:oval) { dsl.oval(left, top, width, height) } it_behaves_like "an oval/circle element" end - describe "when constructed with a top, left and diameter" do - let(:circle) { dsl.oval(left, top, width) } + describe "with top, left, diameter" do + let(:oval) { dsl.oval(left, top, width) } it_behaves_like "an oval/circle element" end - describe "when constructing from a style hash" do - describe "using left, top, height, width" do - let(:circle) { dsl.oval(left: left, top: top, width: width, height: height) } + describe "with a style hash" do + describe "containing left, top, height, width" do + let(:oval) { dsl.oval(left: left, top: top, width: width, height: height) } it_behaves_like "an oval/circle element" end - describe "using left, top, height, width, center: false" do - let(:circle) { dsl.oval(left: left, top: top, width: width, height: height, center: false) } + describe "containing left, top, height, width, center: false" do + let(:oval) { dsl.oval(left: left, top: top, width: width, height: height, center: false) } it_behaves_like "an oval/circle element" end - describe "using left, top, diameter" do - let(:circle) { dsl.oval(left: left, top: top, diameter: width) } + describe "containing left, top, diameter" do + let(:oval) { dsl.oval(left: left, top: top, diameter: width) } it_behaves_like "an oval/circle element" end end end - end diff --git a/spec/shoes/shared_examples/dsl/stroke.rb b/spec/shoes/shared_examples/dsl/stroke.rb index c0c9740ad..c23d77ed9 100644 --- a/spec/shoes/shared_examples/dsl/stroke.rb +++ b/spec/shoes/shared_examples/dsl/stroke.rb @@ -13,11 +13,11 @@ it "applies to subsequently created objects" do dsl.stroke color - expect(Shoes::Oval).to receive(:new) do |*args| + expect(Shoes::Rect).to receive(:new) do |*args| style = args.pop expect(style[:stroke]).to eq(color) end - dsl.oval(10, 10, 100, 100) + dsl.rect(10, 10, 100, 100) end context "with hex string" do diff --git a/spec/shoes/shared_examples/dsl/strokewidth.rb b/spec/shoes/shared_examples/dsl/strokewidth.rb index 14d36c320..8e10e95a7 100644 --- a/spec/shoes/shared_examples/dsl/strokewidth.rb +++ b/spec/shoes/shared_examples/dsl/strokewidth.rb @@ -10,10 +10,10 @@ it "applies to subsequently created objects" do dsl.strokewidth 6 - expect(Shoes::Oval).to receive(:new) do |*args| + expect(Shoes::Rect).to receive(:new) do |*args| style = args.pop expect(style[:strokewidth]).to eq(6) end - dsl.oval(10, 10, 100, 100) + dsl.rect(10, 10, 100, 100) end end diff --git a/spec/shoes/spec_helper.rb b/spec/shoes/spec_helper.rb index f8ec36959..44b35c70e 100644 --- a/spec/shoes/spec_helper.rb +++ b/spec/shoes/spec_helper.rb @@ -1 +1,24 @@ require 'spec_helper' + +def it_styles_with(*styles) + supported_styles = unpack_styles(styles) + spec_styles(supported_styles) +end + +def unpack_styles(styles) + supported_styles = [] + styles.each do |style| + if Shoes::Common::Style::STYLE_GROUPS[style] + Shoes::Common::Style::STYLE_GROUPS[style].each{|style| supported_styles << style} + else + supported_styles << style + end + end + supported_styles +end + +def spec_styles(supported_styles) + supported_styles.each do |style| + it_behaves_like "object that styles with #{style}" + end +end diff --git a/spec/swt_shoes/shared_examples/swt_app_context.rb b/spec/swt_shoes/shared_examples/swt_app_context.rb index 004f779b6..b5009cee5 100644 --- a/spec/swt_shoes/shared_examples/swt_app_context.rb +++ b/spec/swt_shoes/shared_examples/swt_app_context.rb @@ -8,7 +8,7 @@ add_paint_listener: true, remove_paint_listener: true, add_clickable_element: true, add_listener: true, flush: true, redraw: true) } - let(:shoes_app) { double('shoes app', gui: swt_app, rotate: 0) } + let(:shoes_app) { double('shoes app', gui: swt_app, rotate: 0, style: {}, element_styles: {}) } let(:parent) { double('parent', app: swt_app, add_child: true, real: true) } let(:parent_dsl) {double("parent dsl", add_child: true, contents: [], gui: parent, x_dimension: double.as_null_object, diff --git a/static/manual-en.txt b/static/manual-en.txt index b8f020a52..a784630d6 100644 --- a/static/manual-en.txt +++ b/static/manual-en.txt @@ -1692,10 +1692,10 @@ also set, shapes drawn will not be visible. Draws a line using the current line color (aka "stroke") starting at coordinates (left, top) and ending at coordinates (x2, y2). -=== oval(left, top, radius) » Shoes::Shape === +=== oval(left, top, diameter) » Shoes::Shape === Draws a circular form at pixel coordinates (left, top) with a width and height -of `radius` pixels. The line and fill colors are used to draw the shape. By +of `diameter` pixels. The line and fill colors are used to draw the shape. By default, the coordinates are for the oval's leftmost, top corner, but this can be changed by calling the [[Art.transform]] method or by using the `:center` style on the next method below. @@ -1718,7 +1718,7 @@ Draw circular form using a style hash. The following styles are supported: * `top`: the y-coordinate for the oval pen. * `left`: the x-coordinate for the oval pen. - * `radius`: the width and height of the circle. + * `diameter`: the width and height of the circle. * `width`: a specific pixel width for the oval. * `height`: a specific pixel height for the oval. * `center`: do the coordinates specific the oval's center? (true or false)