Class: Zif::UI::NinePanel Abstract

Inherits:
CompoundSprite show all
Defined in:
lib/zif/ui/nine_panel.rb

Overview

This class is abstract.

Subclass and override at least #resize_width and #resize_height to behave in a way which makes sense for your specific assets. Suggested to override #initialize and call super before setting @corners @edges and @fill to reasonable defaults for the assets you are using.

A 9-slice panel/window

It is able to have multiple sprites because it inherits from CompoundSprite.

See below for subclassing examples such as ExampleApp::GlassPanel

Constant Summary

Constants inherited from Sprite

Sprite::BLENDMODE

Instance Attribute Summary collapse

Attributes inherited from CompoundSprite

#labels

Attributes inherited from Sprite

#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index

Attributes included from Clickable

#on_mouse_changed, #on_mouse_down, #on_mouse_up

Attributes included from Actions::Animatable

#animation_sequences, #cur_animation

Attributes included from Actions::Actionable

#actions, #dirty

1. Public Interface collapse

2. Private-ish methods collapse

Methods inherited from CompoundSprite

#draw_override, #source_rect

Methods inherited from Sprite

#blend, #blend=, #center, #center_x, #center_y, #clicked?, #color, #color=, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor

Methods included from Clickable

#absorb_click?, #clicked?

Methods included from Actions::Animatable

#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating

Methods included from Actions::Actionable

#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action

Methods included from Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Methods included from Assignable

#assign

Constructor Details

#initialize(name = Zif.unique_name('nine_panel')) ⇒ NinePanel

Draw 4 corners, 4 edges (with optional transition), then fill the center

Parameters:

  • name (String) (defaults to: Zif.unique_name('nine_panel'))

    The name of the nine panel, used for debugging



27
28
29
30
31
# File 'lib/zif/ui/nine_panel.rb', line 27

def initialize(name=Zif.unique_name('nine_panel'))
  super(name)
  @corners = Array.new(4) # ul, ur, ll, lr
  @edges = Array.new(4) # upper, right, lower, left
end

Instance Attribute Details

#cornersArray<Zif::Sprite>

Returns The corner slices in this order: [upper_left, upper_right, lower_left, lower_right].

Returns:

  • (Array<Zif::Sprite>)

    The corner slices in this order: [upper_left, upper_right, lower_left, lower_right]



14
15
16
# File 'lib/zif/ui/nine_panel.rb', line 14

def corners
  @corners
end

#edgesArray<Zif::Sprite>

Returns The edge slices in this order: [upper, right, lower, left].

Returns:

  • (Array<Zif::Sprite>)

    The edge slices in this order: [upper, right, lower, left]



16
17
18
# File 'lib/zif/ui/nine_panel.rb', line 16

def edges
  @edges
end

#fillZif::Sprite

Returns The center sprite.

Returns:



18
19
20
# File 'lib/zif/ui/nine_panel.rb', line 18

def fill
  @fill
end

Instance Method Details

#heightInteger

Returns height.

Returns:

  • (Integer)

    height



75
76
77
# File 'lib/zif/ui/nine_panel.rb', line 75

def height
  @h
end

#height=(new_height) ⇒ Object

Parameters:

  • new_height (Integer)

    Sets height to this value via #resize_height

See Also:



70
71
72
# File 'lib/zif/ui/nine_panel.rb', line 70

def height=(new_height)
  resize_height(new_height)
end

#lower_left_cornerZif::Sprite

Returns The specified corner. Grabs the correct element from #corners.

Returns:



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#lower_left_corner=(new_corner) ⇒ Object

Parameters:

  • new_corner (Zif::Sprite)

    Sets the specified corner to this. Sets the correct element from #corners.



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#lower_left_edgeZif::Sprite

Returns The specified edge. Grabs the correct element from #edges.

Returns:



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#lower_left_edge=(new_edge) ⇒ Object

Parameters:

  • new_edge (Zif::Sprite)

    Sets the specified edge to this. Sets the correct element from #edges.



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#lower_right_cornerZif::Sprite

Returns The specified corner. Grabs the correct element from #corners.

Returns:



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#lower_right_corner=(new_corner) ⇒ Object

Parameters:

  • new_corner (Zif::Sprite)

    Sets the specified corner to this. Sets the correct element from #corners.



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#lower_right_edgeZif::Sprite

Returns The specified edge. Grabs the correct element from #edges.

Returns:



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#lower_right_edge=(new_edge) ⇒ Object

Parameters:

  • new_edge (Zif::Sprite)

    Sets the specified edge to this. Sets the correct element from #edges.



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#resize(width, height) ⇒ Object

A convenience for setting both width and height.

Parameters:



36
37
38
39
40
# File 'lib/zif/ui/nine_panel.rb', line 36

def resize(width, height)
  resize_width(width)
  resize_height(height)
  view_actual_size!
end

#resize_height(_height) ⇒ Object

In your subclass, please redefine this method so that it can be scaled properly

See Also:



50
51
52
# File 'lib/zif/ui/nine_panel.rb', line 50

def resize_height(_height)
  raise "#{self.class.name} is expected to define #resize_height, to properly set component sprite attributes"
end

#resize_width(_width) ⇒ Object

In your subclass, please redefine this method so that it can be scaled properly

See Also:



44
45
46
# File 'lib/zif/ui/nine_panel.rb', line 44

def resize_width(_width)
  raise "#{self.class.name} is expected to define #resize_width, to properly set component sprite attributes"
end

#spritesArray<Zif::Sprite>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is an override to allow CompoundSprite to work with the three separate arrays

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zif/ui/nine_panel.rb', line 132

def sprites
  unless @sprites_assigned
    @sprites.unshift(
      @fill,
      *@edges,
      *@corners
    )
    @sprites.flatten!
    @sprites_assigned = true
  end

  @sprites
end

#upper_left_cornerZif::Sprite

Returns The specified corner. Grabs the correct element from #corners.

Returns:



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#upper_left_corner=(new_corner) ⇒ Object

Parameters:

  • new_corner (Zif::Sprite)

    Sets the specified corner to this. Sets the correct element from #corners.



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#upper_left_edgeZif::Sprite

Returns The specified edge. Grabs the correct element from #edges.

Returns:



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#upper_left_edge=(new_edge) ⇒ Object

Parameters:

  • new_edge (Zif::Sprite)

    Sets the specified edge to this. Sets the correct element from #edges.



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#upper_right_cornerZif::Sprite

Returns The specified corner. Grabs the correct element from #corners.

Returns:



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#upper_right_corner=(new_corner) ⇒ Object

Parameters:

  • new_corner (Zif::Sprite)

    Sets the specified corner to this. Sets the correct element from #corners.



99
100
101
102
# File 'lib/zif/ui/nine_panel.rb', line 99

%i[upper_left upper_right lower_right lower_left].each_with_index do |name, idx|
  define_method("#{name}_corner") { @corners[idx] }
  define_method("#{name}_corner=") { |new_corner| @corners[idx] = new_corner }
end

#upper_right_edgeZif::Sprite

Returns The specified edge. Grabs the correct element from #edges.

Returns:



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#upper_right_edge=(new_edge) ⇒ Object

Parameters:

  • new_edge (Zif::Sprite)

    Sets the specified edge to this. Sets the correct element from #edges.



121
122
123
124
# File 'lib/zif/ui/nine_panel.rb', line 121

%i[upper right lower left].each_with_index do |name, idx|
  define_method("#{name}_edge") { @edges[idx] }
  define_method("#{name}_edge=") { |new_edge| @edges[idx] = new_edge }
end

#widthInteger

Returns width.

Returns:

  • (Integer)

    width



64
65
66
# File 'lib/zif/ui/nine_panel.rb', line 64

def width
  @w
end

#width=(new_width) ⇒ Object

This class has special getters and setters for width and height. The reason is that it’s using #resize_width and #resize_height internally to support rescaling of the panel. This allows you to change the width and height using a Actions::Action as this is already an Actions::Actionable via CompoundSprite inheritance.

Parameters:

  • new_width (Integer)

    Sets width to this value via #resize_width



59
60
61
# File 'lib/zif/ui/nine_panel.rb', line 59

def width=(new_width)
  resize_width(new_width)
end