lib/wx/shapes/shapes/rect_shape.rb in wxruby3-shapes-0.9.0.pre.beta.3 vs lib/wx/shapes/shapes/rect_shape.rb in wxruby3-shapes-0.9.5

- old
+ new

@@ -5,79 +5,93 @@ class RectShape < Shape # default values module DEFAULT + class << self + # Default value of RectShape fill data member. + def fill; fill ||= Wx::WHITE_BRUSH.dup; end + # Default value of RectShape @border data member. + def border; @border ||= Wx::BLACK_PEN.dup; end + end # Default value of RectShape @rect_size data member. SIZE = Wx::RealPoint.new(100, 50) - # Default value of RectShape @fill data member. - FILL = Wx::Brush.new(Wx::WHITE) if Wx::App.is_main_loop_running - Wx.add_delayed_constant(self, :FILL) { Wx::Brush.new(Wx::WHITE) } - # Default value of RectShape @border data member. - BORDER = Wx::Pen.new(Wx::BLACK) if Wx::App.is_main_loop_running - Wx.add_delayed_constant(self, :BORDER) { Wx::Pen.new(Wx::BLACK) } end - property :rect_size, :fill, :border + property rect_size: :serialize_rect_size + property({ fill: :serialize_rect_fill, + border: :serialize_rect_border }, + optional: true) - # @overload initialize() - # Default constructor. - # @overload initialize(pos, size, diagram) - # User constructor. - # @param [Wx::RealPoint] pos Initial position - # @param [Wx::RealPoint] size Initial size - # @param [Wx::SF::Diagram] diagram parent diagram - def initialize(*args) - size = nil - if args.empty? - super - else - pos, size, diagram = args - super(pos, diagram) - end - @rect_size = size ? size.to_real_point : DEFAULT::SIZE.dup - @fill = DEFAULT::FILL - @border = DEFAULT::BORDER - @prev_size = @prev_position = Wx::RealPoint + # Constructor. + # @param [Wx::RealPoint,Wx::Point] pos Initial position + # @param [Wx::RealPoint,Wx::Size,Wx::Point,Array(Float,Float)] size Initial size + # @param [Wx::SF::Diagram] diagram parent diagram + def initialize(pos = Shape::DEFAULT::POSITION, size = DEFAULT::SIZE, diagram: nil) + super(pos, diagram: diagram) + set_rect_size(size.to_real_point) + @fill = nil + @border = nil end # Set rectangle's fill style. - # @param [Wx::Brush] brush Reference to a brush object - def set_fill(brush) - @fill = brush + # @overload set_fill(brush) + # @param [Wx::Brush] brush + # @overload set_fill(color, style=Wx::BrushStyle::BRUSHSTYLE_SOLID) + # @param [Wx::Colour,Symbol,String] color brush color + # @param [Wx::BrushStyle] style + # @overload set_fill(stipple_bitmap) + # @param [Wx::Bitmap] stipple_bitmap + def set_fill(*args) + @fill = if args.size == 1 && Wx::Brush === args.first + args.first + else + Wx::Brush.new(*args) + end end alias :fill= :set_fill # Get current fill style. # @return [Wx::Brush] Current brush def get_fill - @fill + @fill || (@diagram&.shape_canvas ? @diagram.shape_canvas.fill_brush : DEFAULT.fill) end alias :fill :get_fill # Set rectangle's border style. - # @param [Wx::Pen] pen Reference to a pen object - def set_border(pen) - @border = pen + # @overload set_border(pen) + # @param [Wx::Pen] pen + # @overload set_border(color, width=1, style=Wx::PenStyle::PENSTYLE_SOLID) + # @param [Wx::Colour,String,Symbol] color + # @param [Integer] width + # @param [Wx::PenStyle] style + def set_border(*args) + @border = if args.size == 1 && Wx::Pen === args.first + args.first + else + Wx::Pen.new(*args) + end end alias :border= :set_border # Get current border style. # @return [Wx::Pen] Current pen def get_border - @border + @border || (@diagram&.shape_canvas ? @diagram.shape_canvas.border_pen : DEFAULT.border) end alias :border :get_border # Set the rectangle size. # @overload set_rect_size(x, y) # @param [Float] x Horizontal size # @param [Float] y Vertical size # @overload set_rect_size(size) - # @param [Wx::RealPoint] size New size - def set_rect_size(arg1, arg2 = nil) - @rect_size = arg2 ? Wx::RealPoint.new(arg1.to_f, arg2.to_f) : arg1.to_real_point + # @param [Wx::RealPoint,Array(Float, Float)] size New size + def set_rect_size(*args) + x, y = args.size == 1 ? args.first.to_real_point : args + # set new size while preventing 'invisible' shapes + @rect_size = Wx::RealPoint.new([1.0, x].max, [1.0, y].max) end alias :rect_size= :set_rect_size # Get the rectangle size. # @return [Wx::RealPoint] Current size @@ -161,11 +175,11 @@ # Resize the shape to bound all child shapes. The function can be overridden if necessary. def fit_to_children # HINT: overload it for custom actions... - # get bounding box of the shape and children set be inside it + # get bounding box of the shape and children set to be inside it ch_bb = get_bounding_box shp_bb = ch_bb.dup @child_shapes.each do |child| if child.has_style?(STYLE::ALWAYS_INSIDE) @@ -181,11 +195,11 @@ # resize parent shape shp_bb.union!(ch_bb) move_to(shp_bb.get_position.x, shp_bb.get_position.y) @rect_size = Wx::RealPoint.new(shp_bb.get_size.x.to_f, shp_bb.get_size.y.to_f) if has_style?(STYLE::EMIT_EVENTS) - evt = ShapeEvent.new(EVT_SF_SHAPE_SIZE_CHANGED, id) + evt = ShapeEvent.new(EVT_SF_SHAPE_SIZE_CHANGED, self.object_id) evt.set_shape(self) get_parent_canvas.get_event_handler.process_event(evt) end # move its "1st level" children if necessary @@ -198,15 +212,15 @@ end end end # Scale the shape size by in both directions. The function can be overridden if necessary - # (new implementation should call default one ore scale shape's children manualy if neccesary). + # (new implementation should call default one ore scale shape's children manually if necessary). # @param [Float] x Horizontal scale factor # @param [Float] y Vertical scale factor # @param [Boolean] children true if the shape's children should be scaled as well, otherwise the shape will be updated after scaling via update() function. - def scale(x, y, children = WITHCHILDREN) + def scale(x, y, children: WITHCHILDREN) # HINT: overload it for custom actions... if x > 0 && y > 0 scale_rectangle(x, y) # call default function implementation (needed for scaling of shape's children) @@ -216,12 +230,11 @@ protected # Handle action at handle drag beginning def do_begin_handle - @prev_position = @relative_position.dup - @prev_size = @rect_size.dup + # noop end # Scale the rectangle size for this shape. # @param [Float] x Horizontal scale factor # @param [Float] y Vertical scale factor @@ -268,12 +281,12 @@ # Draw the shape in the normal way. The function can be overridden if necessary. # @param [Wx::DC] dc Reference to device context where the shape will be drawn to def draw_normal(dc) # HINT: overload it for custom actions... - dc.with_pen(@border) do - dc.with_brush(@fill) do + dc.with_pen(border) do + dc.with_brush(fill) do dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size) end end end @@ -281,12 +294,12 @@ # The function can be overridden if necessary. # @param [Wx::DC] dc Reference to device context where the shape will be drawn to def draw_hover(dc) # HINT: overload it for custom actions... - dc.with_pen(Wx::Pen.new(@hover_color, 1)) do - dc.with_brush(@fill) do + dc.with_pen(Wx::Pen.new(hover_colour, 1)) do + dc.with_brush(fill) do dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size) end end end @@ -295,23 +308,23 @@ # The function can be overridden if necessary. # @param [Wx::DC] dc Reference to device context where the shape will be drawn to def draw_highlighted(dc) # HINT: overload it for custom actions... - dc.with_pen(Wx::Pen.new(@hover_color, 2)) do - dc.with_brush(@fill) do + dc.with_pen(Wx::Pen.new(hover_colour, 2)) do + dc.with_brush(fill) do dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size) end end end # Draw shadow under the shape. The function can be overridden if necessary. # @param [Wx::DC] dc Reference to device context where the shadow will be drawn to def draw_shadow(dc) # HINT: overload it for custom actions... - if @fill.style != Wx::BrushStyle::BRUSHSTYLE_TRANSPARENT + if fill.style != Wx::BrushStyle::BRUSHSTYLE_TRANSPARENT dc.with_pen(Wx::TRANSPARENT_PEN) do dc.with_brush(get_parent_canvas.get_shadow_fill) do dc.draw_rectangle((get_absolute_position + get_parent_canvas.get_shadow_offset).to_point, @rect_size.to_size) end end @@ -323,20 +336,25 @@ # @param [Shape::Handle] handle Reference to dragged shape handle def on_right_handle(handle) # HINT: overload it for custom actions... @rect_size.x += handle.get_delta.x + @rect_size.x = 1.0 if @rect_size.x < 1.0 end # Event handler called during dragging of the left shape handle. # The function can be overridden if necessary. # @param [Shape::Handle] handle Reference to dragged shape handle def on_left_handle(handle) # HINT: overload it for custom actions... dx = handle.get_delta.x.to_f - + + if (@rect_size.x - dx) < 1.0 + dx = @rect_size.x - 1.0 + end + # update position of children unless has_style?(STYLE::LOCK_CHILDREN) @child_shapes.each do |child| child.move_by(-dx, 0) if child.get_h_align == HALIGN::NONE end @@ -351,11 +369,15 @@ # @param [Shape::Handle] handle Reference to dragged shape handle def on_top_handle(handle) # HINT: overload it for custom actions... dy = handle.get_delta.y.to_f - + + if (@rect_size.y - dy) < 1.0 + dy = @rect_size.y - 1.0 + end + # update position of children unless has_style?(STYLE::LOCK_CHILDREN) @child_shapes.each do |child| child.move_by(0, -dy) if child.get_v_align == VALIGN::NONE end @@ -370,9 +392,29 @@ # @param [Shape::Handle] handle Reference to dragged shape handle def on_bottom_handle(handle) # HINT: overload it for custom actions... @rect_size.y += handle.get_delta.y + @rect_size.y = 1.0 if @rect_size.y < 1.0 end + + def serialize_rect_size(*val) + @rect_size = val.first unless val.empty? + @rect_size + end + private :serialize_rect_size + + def serialize_rect_fill(*val) + @fill = val.first unless val.empty? + @fill + end + private :serialize_rect_fill + + def serialize_rect_border(*val) + @border = val.first unless val.empty? + @border + end + private :serialize_rect_border + end end