Class: CyberarmEngine::BackgroundNineSlice

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/cyberarm_engine/background_nine_slice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window

Constructor Details

#initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE) ⇒ BackgroundNineSlice

Returns a new instance of BackgroundNineSlice.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 7

def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE)
  @image = get_image(image_path) if image_path

  @x = x
  @y = y
  @z = z

  @width = width
  @height = height

  @mode = mode

  @left = left
  @top = top
  @right = right
  @bottom = bottom

  @color = color

  nine_slice if @image
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def bottom
  @bottom
end

#colorObject

Returns the value of attribute color.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def color
  @color
end

#heightObject

Returns the value of attribute height.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def height
  @height
end

#imageObject

Returns the value of attribute image.



5
6
7
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 5

def image
  @image
end

#leftObject

Returns the value of attribute left.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def left
  @left
end

#modeObject

Returns the value of attribute mode.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def mode
  @mode
end

#rightObject

Returns the value of attribute right.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def right
  @right
end

#topObject

Returns the value of attribute top.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def top
  @top
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def width
  @width
end

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def y
  @y
end

#zObject

Returns the value of attribute z.



4
5
6
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 4

def z
  @z
end

Instance Method Details

#cheightObject



65
66
67
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 65

def cheight
  @cy - @height
end

#cwidthObject



61
62
63
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 61

def cwidth
  @cx - @width
end

#cxObject



53
54
55
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 53

def cx
  @x + @left
end

#cyObject



57
58
59
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 57

def cy
  @y + @top
end

#drawObject



79
80
81
82
83
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 79

def draw
  return unless @image && @segment_top_left

  @mode == :tiled ? draw_tiled : draw_stretched
end

#draw_stretchedObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 85

def draw_stretched
  @segment_top_left.draw(@x, @y, @z, 1, 1, @color)
  @segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color) # SCALE X
  @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)

  @segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color) # SCALE Y
  @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
  @segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
  @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
  @segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
  @segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
end

#draw_tiledObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 98

def draw_tiled
  @segment_top_left.draw(@x, @y, @z, 1, 1, @color)

  # p [width_scale, height_scale]

  Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
    width_scale.ceil.times do |i|
      @segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z, 1, 1, @color) # SCALE X
    end
  end

  @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)

  Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
    height_scale.ceil.times do |i|
      @segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z, 1, 1, @color) # SCALE Y
    end
  end

  @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)

  Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
    width_scale.ceil.times do |i|
      @segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z, 1, 1, @color) # SCALE X
    end
  end

  @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)

  Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
    height_scale.ceil.times do |i|
      @segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z, 1, 1, @color) # SCALE Y
    end
  end

  Gosu.clip_to(@x + @segment_top_left.width, @y + @segment_top.height, @width - (@segment_left.width + @segment_right.width), @height - (@segment_top.height + @segment_bottom.height)) do
    height_scale.ceil.times do |y|
      width_scale.ceil.times do |x|
        @segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z, 1, 1, @color) # SCALE X and SCALE Y
      end
    end
  end
end

#height_scaleObject



74
75
76
77
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 74

def height_scale
  scale = (@height - (@top + @bottom)).to_f / (@image.height - (@top + @bottom))
  scale.abs
end

#nine_sliceObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 35

def nine_slice
  # pp [@left, @top, @right, @bottom, @image.width]

  @segment_top_left = @image.subimage(0, 0, @left, @top)
  @segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)

  @segment_left = @image.subimage(0, @top, @left, @image.height - (@top + @bottom))
  @segment_right = @image.subimage(@image.width - @right, @top, @left, @image.height - (@top + @bottom))

  @segment_bottom_left = @image.subimage(0, @image.height - @bottom, @left, @bottom)
  @segment_bottom_right = @image.subimage(@image.width - @right, @image.height - @bottom, @right, @bottom)

  @segment_top = @image.subimage(@left, 0, @image.width - (@left + @right), @top)
  @segment_bottom = @image.subimage(@left, @image.height - @bottom, @image.width - (@left + @right), @bottom)

  @segment_middle = @image.subimage(@left, @top, @image.width - (@left + @right), @image.height - (@top + @bottom))
end

#width_scaleObject



69
70
71
72
# File 'lib/cyberarm_engine/background_nine_slice.rb', line 69

def width_scale
  scale = (@width.to_f - (@left + @right)) / (@image.width - (@left + @right))
  scale.abs
end