Class: Wx::SF::BitmapShape

Inherits:
RectShape show all
Defined in:
lib/wx/shapes/shapes/bitmap_shape.rb

Overview

Class encapsulating the bitmap shape. The shape can display and control files stored in formats supported by Wx::Bitmap class loaded from a file or created from XPM image.

Instance Method Summary collapse

Methods inherited from RectShape

#create_handles, #do_on_handle, #draw_shadow, #fit_to_children, #get_border, #get_border_point, #get_bounding_box, #get_fill, #get_rect_size, #on_begin_handle, #on_bottom_handle, #on_left_handle, #on_right_handle, #on_top_handle, #scale, #set_border, #set_fill, #set_rect_size

Methods inherited from Shape

#accept_child, #accept_connection, #accept_currently_dragged_shapes, #accept_src_neighbour, #accept_trg_neighbour, #activate, #active?, #add_child_shape, #add_connection_point, #add_handle, #add_style, #ancestor?, #clear_accepted_childs, #clear_accepted_connections, #clear_accepted_src_neighbours, #clear_accepted_trg_neighbours, component, component_shapes, #contains?, #contains_style, #create_handles, #descendant?, #do_alignment, #does_not_accept_children?, #draw, #draw_selected, #draw_shadow, #fit_to_children, #get_absolute_position, #get_accepted_children, #get_accepted_connections, #get_accepted_src_neighbours, #get_accepted_trg_neighbours, #get_assigned_connections, #get_border_point, #get_bounding_box, #get_center, #get_child_shapes, #get_children, #get_children_recursively, #get_complete_bounding_box, #get_connection_point, #get_connection_points, #get_custom_dock_point, #get_diagram, #get_grand_parent_shape, #get_h_align, #get_h_border, #get_handle, #get_handles, #get_hover_colour, #get_nearest_connection_point, #get_neighbours, #get_parent_absolute_position, #get_parent_canvas, #get_parent_shape, #get_relative_position, #get_shape_canvas, #get_style, #get_user_data, #get_v_align, #get_v_border, #has_children, #has_selected_parent?, #include_child_shape?, #inside?, #inspect, #intersects?, #is_child_accepted, #is_connection_accepted, #is_managed, #is_manager, #is_src_neighbour_accepted, #is_trg_neighbour_accepted, lines_intersection, #lines_intersection, #move_by, #move_to, #on_begin_drag, #on_begin_handle, #on_child_dropped, #on_dragging, #on_end_drag, #on_import, #on_key, #on_left_click, #on_left_double_click, #on_mouse_enter, #on_mouse_leave, #on_mouse_over, #on_right_click, #on_right_double_click, #refresh, #refresh_rect, #remove_connection_point, #remove_handle, #remove_style, #scale, #scale_children, #select, #selected?, #set_custom_dock_point, #set_diagram, #set_h_align, #set_h_border, #set_hover_colour, #set_parent_shape, #set_relative_position, #set_style, #set_user_data, #set_v_align, #set_v_border, #show, #show_handles, #to_s, #update, #visible?

Constructor Details

#initialize(pos = Shape::DEFAULT::POSITION, bmp_path = nil, diagram: nil) ⇒ BitmapShape

Constructor.

Parameters:

  • pos (Wx::RealPoint, Wx::Point) (defaults to: Shape::DEFAULT::POSITION)

    Initial position

  • bmp_path (String) (defaults to: nil)

    Bitmap path

  • diagram (Wx::SF::Diagram) (defaults to: nil)

    parent diagram



20
21
22
23
24
25
26
27
28
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 20

def initialize(pos = Shape::DEFAULT::POSITION, bmp_path = nil, diagram: nil)
  super(pos, diagram: diagram)
  @bitmap = Wx::NULL_BITMAP
  @art_path = @art_section = nil
  @bitmap_path = bmp_path
  create_from_file(bmp_path) if bmp_path
  @rescale_in_progress = false
  @can_scale = true
end

Instance Method Details

#_on_canvas(change, *_args) ⇒ Boolean? (protected)

Event handler called by ShapeCanvas to request,report canvas changes.

Parameters:

Returns:

  • (Boolean, nil)


134
135
136
137
138
139
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 134

def _on_canvas(change, *_args)
  if change == ShapeCanvas::CHANGE::RESCALED
    self.scale(1, 1)
  end
  super
end

#can_scale?Boolean Also known as: get_can_scale

Get information about the possibility of the shape scaling.

Returns:

  • (Boolean)

    true if the shape can be scaled, otherwise false



45
46
47
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 45

def can_scale?
  @can_scale
end

#create_from_file(file, type = nil, art_path: nil, art_section: nil) ⇒ Boolean

Returns true on success, otherwise false.

Parameters:

  • file (String, Symbol)

    File name (absolute or relative) or base name for art file (for Wx::ArtLocator)

  • type (Wx::BitmapType, nil) (defaults to: nil)

    Bitmap type (see the wxBitmap class reference)

  • art_path (String) (defaults to: nil)

    base path to look up the art file for Wx::ArtLocator

  • art_section (String, nil) (defaults to: nil)

    optional owner folder name for art files for Wx::ArtLocator

Returns:

  • (Boolean)

    true on success, otherwise false

See Also:

  • ArtLocator


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 57

def create_from_file(file, type = nil, art_path: nil, art_section: nil)
  # load bitmap from the file
  @bitmap_path = file
  @bitmap_type = type
  if File.file?(@bitmap_path.to_s)
    @bitmap = Wx::Bitmap.new
    success = @bitmap.load_file(@bitmap_path, type ? type : Wx::BITMAP_TYPE_ANY)
  else
    art_path ||= File.dirname(caller_locations(1).first.absolute_path)
    art_section ||= File.basename(caller_locations(1).first.absolute_path, '.*')
    path = Wx::ArtLocator.find_art(@bitmap_path, art_path: art_path, art_section: art_section, art_type: :bitmap, bmp_type: type)
    if path
      @bitmap = Wx::Bitmap.new
      success = @bitmap.load_file(path, type ? type : Wx::BITMAP_TYPE_ANY)
      if success
        p = Pathname.new(art_path)
        if Wx::PLATFORM == 'WXMSW'
          # take possibility of different drive into account
          @art_path = if p.relative? || art_path[0] != Dir.getwd[0]
                        art_path
                      else
                        p.relative_path_from(Dir.getwd).to_s
                      end
        else
          @art_path = p.relative? ? art_path : p.relative_path_from(Dir.getwd).to_s
        end
        @art_section = art_section
      end
    else
      @bitmap = nil
      success = false
    end
  end

  @original_bitmap = @bitmap

  if success
    @rect_size.x = @bitmap.width
    @rect_size.y = @bitmap.height

    if @can_scale
      add_style(Shape::STYLE::SIZE_CHANGE)
    else
      remove_style(Shape::STYLE::SIZE_CHANGE)
    end
  end

  success
end

#do_begin_handleObject (protected)

Handle action at handle drag beginning



142
143
144
145
146
147
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 142

def do_begin_handle
  if @can_scale
    @rescale_in_progress = true
    @prev_pos = get_absolute_position.dup
  end
end

#draw_highlighted(dc) ⇒ Object (protected)

dragged one if it will be dropped on it). The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



208
209
210
211
212
213
214
215
216
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 208

def draw_highlighted(dc)
  _draw_bitmap(dc, get_absolute_position.to_point)

  dc.with_brush(Wx::TRANSPARENT_BRUSH) do
    dc.with_pen(Wx::Pen.new(hover_colour, 2)) do
      dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
    end
  end
end

#draw_hover(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



195
196
197
198
199
200
201
202
203
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 195

def draw_hover(dc)
  _draw_bitmap(dc, get_absolute_position.to_point)

  dc.with_brush(Wx::TRANSPARENT_BRUSH) do
    dc.with_pen(Wx::Pen.new(hover_colour, 1)) do
      dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
    end
  end
end

#draw_normal(dc) ⇒ Object (protected)

Draw the shape in the normal way. The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 178

def draw_normal(dc)
  # HINT: overload it for custom actions...
  if @rescale_in_progress
    _draw_bitmap(dc, @prev_pos.to_point)

    dc.with_brush(Wx::TRANSPARENT_BRUSH) do
      dc.with_pen(Wx::Pen.new(Wx::Colour.new(100, 100, 100), 1, Wx::PenStyle::PENSTYLE_DOT)) do
        dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
      end
    end
  else
    _draw_bitmap(dc, get_absolute_position.to_point)
  end
end

#enable_scale(canscale) ⇒ Object Also known as: set_can_scale

Enable/disable scaling mode of the bitmap.

Parameters:

  • canscale (Boolean)

    Set true if the bitmap shape could be scaled



38
39
40
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 38

def enable_scale(canscale)
  @can_scale = canscale
end

#get_bitmap_pathString?

Get full name of a source BMP file (if set).

Returns:

  • (String, nil)

    String containing full file name



32
33
34
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 32

def get_bitmap_path
  @bitmap_path
end

#on_end_handle(handle) ⇒ Object

Event handler called when the user finished dragging of the shape handle. The function can be overridden if necessary.

Parameters:

  • handle (Shape::Handle)

    Reference to the dragged shape handle



119
120
121
122
123
124
125
126
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 119

def on_end_handle(handle)
  if @can_scale
    @rescale_in_progress = false
    rescale_image(@rect_size)
  end

  super
end

#on_handle(handle) ⇒ Object

Event handler called during dragging of the shape handle. The function can be overridden if necessary.

Parameters:

  • handle (Shape::Handle)

    Reference to the dragged shape handle



109
110
111
112
113
114
115
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 109

def on_handle(handle)
  if @can_scale
    super
  else
    remove_style(STYLE::SIZE_CHANGE)
  end
end

#rescale_image(size) ⇒ Object (protected)

Parameters:



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 164

def rescale_image(size)
  if get_parent_canvas && @original_bitmap && @original_bitmap.ok?
    size = size.to_real_point
    if ShapeCanvas.gc_enabled?
      Wx::Bitmap.rescale(@bitmap = Wx::Bitmap.new(@original_bitmap), size.to_size)
    else
      scale = get_parent_canvas.get_scale
      Wx::Bitmap.rescale(@bitmap = Wx::Bitmap.new(@original_bitmap), (size * scale).to_size)
    end
  end
end

#scale_rectangle(x, y) ⇒ Object (protected)

Scale the rectangle size for this shape.

Parameters:

  • x (Float)

    Horizontal scale factor

  • y (Float)

    Vertical scale factor



152
153
154
155
156
157
158
159
# File 'lib/wx/shapes/shapes/bitmap_shape.rb', line 152

def scale_rectangle(x, y)
  if @can_scale
    @rect_size.x *= x
    @rect_size.y *= y

    rescale_image(@rect_size) unless @rescale_in_progress
  end
end