cyberarm_engine 0.19.0 → 0.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -8
  3. data/.rubocop.yml +7 -7
  4. data/.travis.yml +5 -5
  5. data/Gemfile +6 -6
  6. data/LICENSE.txt +21 -21
  7. data/README.md +74 -74
  8. data/Rakefile +10 -10
  9. data/bin/console +14 -14
  10. data/bin/setup +8 -8
  11. data/cyberarm_engine.gemspec +39 -39
  12. data/lib/cyberarm_engine/animator.rb +219 -219
  13. data/lib/cyberarm_engine/background.rb +179 -179
  14. data/lib/cyberarm_engine/background_nine_slice.rb +142 -142
  15. data/lib/cyberarm_engine/bounding_box.rb +150 -150
  16. data/lib/cyberarm_engine/builtin/intro_state.rb +130 -130
  17. data/lib/cyberarm_engine/cache/download_manager.rb +121 -121
  18. data/lib/cyberarm_engine/cache.rb +4 -4
  19. data/lib/cyberarm_engine/common.rb +113 -113
  20. data/lib/cyberarm_engine/config_file.rb +46 -46
  21. data/lib/cyberarm_engine/console/command.rb +157 -157
  22. data/lib/cyberarm_engine/console/commands/help_command.rb +43 -43
  23. data/lib/cyberarm_engine/console/subcommand.rb +99 -99
  24. data/lib/cyberarm_engine/console.rb +248 -248
  25. data/lib/cyberarm_engine/game_object.rb +248 -248
  26. data/lib/cyberarm_engine/game_state.rb +97 -97
  27. data/lib/cyberarm_engine/model/material.rb +21 -21
  28. data/lib/cyberarm_engine/model/model_object.rb +131 -131
  29. data/lib/cyberarm_engine/model/parser.rb +74 -74
  30. data/lib/cyberarm_engine/model/parsers/collada_parser.rb +138 -138
  31. data/lib/cyberarm_engine/model/parsers/wavefront_parser.rb +154 -154
  32. data/lib/cyberarm_engine/model.rb +212 -212
  33. data/lib/cyberarm_engine/model_cache.rb +31 -31
  34. data/lib/cyberarm_engine/opengl/light.rb +50 -50
  35. data/lib/cyberarm_engine/opengl/orthographic_camera.rb +46 -46
  36. data/lib/cyberarm_engine/opengl/perspective_camera.rb +38 -38
  37. data/lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb +249 -249
  38. data/lib/cyberarm_engine/opengl/renderer/g_buffer.rb +164 -164
  39. data/lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb +298 -298
  40. data/lib/cyberarm_engine/opengl/renderer/renderer.rb +22 -22
  41. data/lib/cyberarm_engine/opengl/shader.rb +406 -406
  42. data/lib/cyberarm_engine/opengl/texture.rb +69 -69
  43. data/lib/cyberarm_engine/opengl.rb +28 -28
  44. data/lib/cyberarm_engine/ray.rb +56 -56
  45. data/lib/cyberarm_engine/stats.rb +21 -21
  46. data/lib/cyberarm_engine/text.rb +197 -197
  47. data/lib/cyberarm_engine/timer.rb +23 -23
  48. data/lib/cyberarm_engine/transform.rb +296 -296
  49. data/lib/cyberarm_engine/ui/border_canvas.rb +102 -102
  50. data/lib/cyberarm_engine/ui/dsl.rb +139 -139
  51. data/lib/cyberarm_engine/ui/element.rb +488 -488
  52. data/lib/cyberarm_engine/ui/elements/button.rb +97 -97
  53. data/lib/cyberarm_engine/ui/elements/check_box.rb +54 -54
  54. data/lib/cyberarm_engine/ui/elements/container.rb +256 -256
  55. data/lib/cyberarm_engine/ui/elements/edit_box.rb +179 -179
  56. data/lib/cyberarm_engine/ui/elements/edit_line.rb +263 -263
  57. data/lib/cyberarm_engine/ui/elements/flow.rb +15 -15
  58. data/lib/cyberarm_engine/ui/elements/image.rb +72 -72
  59. data/lib/cyberarm_engine/ui/elements/list_box.rb +88 -82
  60. data/lib/cyberarm_engine/ui/elements/progress.rb +51 -51
  61. data/lib/cyberarm_engine/ui/elements/radio.rb +6 -6
  62. data/lib/cyberarm_engine/ui/elements/slider.rb +104 -104
  63. data/lib/cyberarm_engine/ui/elements/stack.rb +11 -11
  64. data/lib/cyberarm_engine/ui/elements/text_block.rb +162 -162
  65. data/lib/cyberarm_engine/ui/elements/toggle_button.rb +65 -65
  66. data/lib/cyberarm_engine/ui/event.rb +54 -54
  67. data/lib/cyberarm_engine/ui/gui_state.rb +256 -256
  68. data/lib/cyberarm_engine/ui/style.rb +49 -49
  69. data/lib/cyberarm_engine/ui/theme.rb +207 -207
  70. data/lib/cyberarm_engine/vector.rb +293 -293
  71. data/lib/cyberarm_engine/version.rb +4 -4
  72. data/lib/cyberarm_engine/window.rb +120 -120
  73. data/lib/cyberarm_engine.rb +71 -71
  74. metadata +3 -3
@@ -1,179 +1,179 @@
1
- module CyberarmEngine
2
- class Background
3
- attr_accessor :x, :y, :z, :width, :height, :angle, :debug
4
- attr_reader :background
5
-
6
- def initialize(x: 0, y: 0, z: 0, width: 0, height: 0, background: Gosu::Color::BLACK, angle: 0, debug: false)
7
- @x = x
8
- @y = y
9
- @z = z
10
- @width = width
11
- @height = height
12
- @debug = debug
13
-
14
- @paint = Paint.new(background)
15
- @angle = angle
16
-
17
- @top_left = Vector.new(@x, @y)
18
- @top_right = Vector.new(@x + @width, @y)
19
- @bottom_left = Vector.new(@x, @y + @height)
20
- @bottom_right = Vector.new(@x + @width, @y + @height)
21
- end
22
-
23
- def draw
24
- Gosu.clip_to(@x, @y, @width, @height) do
25
- Gosu.draw_quad(
26
- @top_left.x, @top_left.y, @paint.top_left,
27
- @top_right.x, @top_right.y, @paint.top_right,
28
- @bottom_right.x, @bottom_right.y, @paint.bottom_right,
29
- @bottom_left.x, @bottom_left.y, @paint.bottom_left,
30
- @z
31
- )
32
- end
33
-
34
- debug_outline if @debug
35
- end
36
-
37
- def update
38
- origin_x = (@x + (@width / 2))
39
- origin_y = (@y + (@height / 2))
40
-
41
- points = [
42
- @top_left = Vector.new(@x, @y),
43
- @top_right = Vector.new(@x + @width, @y),
44
- @bottom_left = Vector.new(@x, @y + @height),
45
- @bottom_right = Vector.new(@x + @width, @y + @height)
46
- ]
47
-
48
- [@top_left, @top_right, @bottom_left, @bottom_right].each do |vector|
49
- temp_x = vector.x - origin_x
50
- temp_y = vector.y - origin_y
51
-
52
- # 90 is up here, while gosu uses 0 for up.
53
- radians = (@angle + 90).gosu_to_radians
54
- vector.x = (@x + (@width / 2)) + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
55
- vector.y = (@y + (@height / 2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
56
- end
57
-
58
- # [
59
- # [:top, @top_left, @top_right],
60
- # [:right, @top_right, @bottom_right],
61
- # [:bottom, @bottom_right, @bottom_left],
62
- # [:left, @bottom_left, @top_left]
63
- # ].each do |edge|
64
- # points.each do |point|
65
- # puts "#{edge.first} -> #{shortest_distance(point, edge[1], edge[2])}"
66
- # end
67
- # end
68
- end
69
-
70
- def shortest_distance(point, la, lb)
71
- a = la.x - lb.x
72
- b = la.y - lb.y
73
- c = Gosu.distance(la.x, la.y, lb.x, lb.y)
74
- p a, b, c
75
- d = (a * point.x + b * point.y + c).abs / Math.sqrt(a * a + b * b)
76
- puts "Distance: #{d}"
77
- exit!
78
- d
79
- end
80
-
81
- def debug_outline
82
- # Top
83
- Gosu.draw_line(
84
- @x, @y, Gosu::Color::RED,
85
- @x + @width, @y, Gosu::Color::RED,
86
- @z
87
- )
88
-
89
- # Right
90
- Gosu.draw_line(
91
- @x + @width, @y, Gosu::Color::RED,
92
- @x + @width, @y + @height, Gosu::Color::RED,
93
- @z
94
- )
95
-
96
- # Bottom
97
- Gosu.draw_line(
98
- @x + @width, @y + @height, Gosu::Color::RED,
99
- @x, @y + @height, Gosu::Color::RED,
100
- @z
101
- )
102
-
103
- # Left
104
- Gosu.draw_line(
105
- @x, @y + @height, Gosu::Color::RED,
106
- @x, @y, Gosu::Color::RED,
107
- @z
108
- )
109
- end
110
-
111
- def background=(_background)
112
- @paint.set(_background)
113
- update
114
- end
115
-
116
- def angle=(n)
117
- @angle = n
118
- update
119
- end
120
- end
121
-
122
- class Paint
123
- attr_accessor :top_left, :top_right, :bottom_left, :bottom_right
124
-
125
- def initialize(background)
126
- set(background)
127
- end
128
-
129
- def set(background)
130
- @background = background
131
-
132
- if background.is_a?(Numeric)
133
- @top_left = background
134
- @top_right = background
135
- @bottom_left = background
136
- @bottom_right = background
137
- elsif background.is_a?(Gosu::Color)
138
- @top_left = background
139
- @top_right = background
140
- @bottom_left = background
141
- @bottom_right = background
142
- elsif background.is_a?(Array)
143
- if background.size == 1
144
- set(background.first)
145
- elsif background.size == 2
146
- @top_left = background.first
147
- @top_right = background.last
148
- @bottom_left = background.first
149
- @bottom_right = background.last
150
- elsif background.size == 4
151
- @top_left = background[0]
152
- @top_right = background[1]
153
- @bottom_left = background[2]
154
- @bottom_right = background[3]
155
- else
156
- raise ArgumentError, "background array was empty or had wrong number of elements (expected 2 or 4 elements)"
157
- end
158
- elsif background.is_a?(Hash)
159
- @top_left = background[:top_left]
160
- @top_right = background[:top_right]
161
- @bottom_left = background[:bottom_left]
162
- @bottom_right = background[:bottom_right]
163
- elsif background.is_a?(Range)
164
- set([background.begin, background.begin, background.end, background.end])
165
- else
166
- raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed"
167
- end
168
- end
169
- end
170
- end
171
-
172
- # Add <=> method to support Range based gradients
173
- module Gosu
174
- class Color
175
- def <=>(_other)
176
- self
177
- end
178
- end
179
- end
1
+ module CyberarmEngine
2
+ class Background
3
+ attr_accessor :x, :y, :z, :width, :height, :angle, :debug
4
+ attr_reader :background
5
+
6
+ def initialize(x: 0, y: 0, z: 0, width: 0, height: 0, background: Gosu::Color::BLACK, angle: 0, debug: false)
7
+ @x = x
8
+ @y = y
9
+ @z = z
10
+ @width = width
11
+ @height = height
12
+ @debug = debug
13
+
14
+ @paint = Paint.new(background)
15
+ @angle = angle
16
+
17
+ @top_left = Vector.new(@x, @y)
18
+ @top_right = Vector.new(@x + @width, @y)
19
+ @bottom_left = Vector.new(@x, @y + @height)
20
+ @bottom_right = Vector.new(@x + @width, @y + @height)
21
+ end
22
+
23
+ def draw
24
+ Gosu.clip_to(@x, @y, @width, @height) do
25
+ Gosu.draw_quad(
26
+ @top_left.x, @top_left.y, @paint.top_left,
27
+ @top_right.x, @top_right.y, @paint.top_right,
28
+ @bottom_right.x, @bottom_right.y, @paint.bottom_right,
29
+ @bottom_left.x, @bottom_left.y, @paint.bottom_left,
30
+ @z
31
+ )
32
+ end
33
+
34
+ debug_outline if @debug
35
+ end
36
+
37
+ def update
38
+ origin_x = (@x + (@width / 2))
39
+ origin_y = (@y + (@height / 2))
40
+
41
+ points = [
42
+ @top_left = Vector.new(@x, @y),
43
+ @top_right = Vector.new(@x + @width, @y),
44
+ @bottom_left = Vector.new(@x, @y + @height),
45
+ @bottom_right = Vector.new(@x + @width, @y + @height)
46
+ ]
47
+
48
+ [@top_left, @top_right, @bottom_left, @bottom_right].each do |vector|
49
+ temp_x = vector.x - origin_x
50
+ temp_y = vector.y - origin_y
51
+
52
+ # 90 is up here, while gosu uses 0 for up.
53
+ radians = (@angle + 90).gosu_to_radians
54
+ vector.x = (@x + (@width / 2)) + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
55
+ vector.y = (@y + (@height / 2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
56
+ end
57
+
58
+ # [
59
+ # [:top, @top_left, @top_right],
60
+ # [:right, @top_right, @bottom_right],
61
+ # [:bottom, @bottom_right, @bottom_left],
62
+ # [:left, @bottom_left, @top_left]
63
+ # ].each do |edge|
64
+ # points.each do |point|
65
+ # puts "#{edge.first} -> #{shortest_distance(point, edge[1], edge[2])}"
66
+ # end
67
+ # end
68
+ end
69
+
70
+ def shortest_distance(point, la, lb)
71
+ a = la.x - lb.x
72
+ b = la.y - lb.y
73
+ c = Gosu.distance(la.x, la.y, lb.x, lb.y)
74
+ p a, b, c
75
+ d = (a * point.x + b * point.y + c).abs / Math.sqrt(a * a + b * b)
76
+ puts "Distance: #{d}"
77
+ exit!
78
+ d
79
+ end
80
+
81
+ def debug_outline
82
+ # Top
83
+ Gosu.draw_line(
84
+ @x, @y, Gosu::Color::RED,
85
+ @x + @width, @y, Gosu::Color::RED,
86
+ @z
87
+ )
88
+
89
+ # Right
90
+ Gosu.draw_line(
91
+ @x + @width, @y, Gosu::Color::RED,
92
+ @x + @width, @y + @height, Gosu::Color::RED,
93
+ @z
94
+ )
95
+
96
+ # Bottom
97
+ Gosu.draw_line(
98
+ @x + @width, @y + @height, Gosu::Color::RED,
99
+ @x, @y + @height, Gosu::Color::RED,
100
+ @z
101
+ )
102
+
103
+ # Left
104
+ Gosu.draw_line(
105
+ @x, @y + @height, Gosu::Color::RED,
106
+ @x, @y, Gosu::Color::RED,
107
+ @z
108
+ )
109
+ end
110
+
111
+ def background=(_background)
112
+ @paint.set(_background)
113
+ update
114
+ end
115
+
116
+ def angle=(n)
117
+ @angle = n
118
+ update
119
+ end
120
+ end
121
+
122
+ class Paint
123
+ attr_accessor :top_left, :top_right, :bottom_left, :bottom_right
124
+
125
+ def initialize(background)
126
+ set(background)
127
+ end
128
+
129
+ def set(background)
130
+ @background = background
131
+
132
+ if background.is_a?(Numeric)
133
+ @top_left = background
134
+ @top_right = background
135
+ @bottom_left = background
136
+ @bottom_right = background
137
+ elsif background.is_a?(Gosu::Color)
138
+ @top_left = background
139
+ @top_right = background
140
+ @bottom_left = background
141
+ @bottom_right = background
142
+ elsif background.is_a?(Array)
143
+ if background.size == 1
144
+ set(background.first)
145
+ elsif background.size == 2
146
+ @top_left = background.first
147
+ @top_right = background.last
148
+ @bottom_left = background.first
149
+ @bottom_right = background.last
150
+ elsif background.size == 4
151
+ @top_left = background[0]
152
+ @top_right = background[1]
153
+ @bottom_left = background[2]
154
+ @bottom_right = background[3]
155
+ else
156
+ raise ArgumentError, "background array was empty or had wrong number of elements (expected 2 or 4 elements)"
157
+ end
158
+ elsif background.is_a?(Hash)
159
+ @top_left = background[:top_left]
160
+ @top_right = background[:top_right]
161
+ @bottom_left = background[:bottom_left]
162
+ @bottom_right = background[:bottom_right]
163
+ elsif background.is_a?(Range)
164
+ set([background.begin, background.begin, background.end, background.end])
165
+ else
166
+ raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed"
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ # Add <=> method to support Range based gradients
173
+ module Gosu
174
+ class Color
175
+ def <=>(_other)
176
+ self
177
+ end
178
+ end
179
+ end
@@ -1,142 +1,142 @@
1
- module CyberarmEngine
2
- class BackgroundNineSlice
3
- include CyberarmEngine::Common
4
- attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color
5
- attr_reader :image
6
-
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)
8
- @image = get_image(image_path) if image_path
9
-
10
- @x = x
11
- @y = y
12
- @z = z
13
-
14
- @width = width
15
- @height = height
16
-
17
- @mode = mode
18
-
19
- @left = left
20
- @top = top
21
- @right = right
22
- @bottom = bottom
23
-
24
- @color = color
25
-
26
- nine_slice if @image
27
- end
28
-
29
- def image=(image_path)
30
- old_image = @image
31
- @image = image_path ? get_image(image_path) : image_path
32
- nine_slice if @image && old_image != @image
33
- end
34
-
35
- def nine_slice
36
- # pp [@left, @top, @right, @bottom, @image.width]
37
-
38
- @segment_top_left = @image.subimage(0, 0, @left, @top)
39
- @segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)
40
-
41
- @segment_left = @image.subimage(0, @top, @left, @image.height - (@top + @bottom))
42
- @segment_right = @image.subimage(@image.width - @right, @top, @left, @image.height - (@top + @bottom))
43
-
44
- @segment_bottom_left = @image.subimage(0, @image.height - @bottom, @left, @bottom)
45
- @segment_bottom_right = @image.subimage(@image.width - @right, @image.height - @bottom, @right, @bottom)
46
-
47
- @segment_top = @image.subimage(@left, 0, @image.width - (@left + @right), @top)
48
- @segment_bottom = @image.subimage(@left, @image.height - @bottom, @image.width - (@left + @right), @bottom)
49
-
50
- @segment_middle = @image.subimage(@left, @top, @image.width - (@left + @right), @image.height - (@top + @bottom))
51
- end
52
-
53
- def cx
54
- @x + @left
55
- end
56
-
57
- def cy
58
- @y + @top
59
- end
60
-
61
- def cwidth
62
- @cx - @width
63
- end
64
-
65
- def cheight
66
- @cy - @height
67
- end
68
-
69
- def width_scale
70
- scale = (@width.to_f - (@left + @right)) / (@image.width - (@left + @right))
71
- scale.abs
72
- end
73
-
74
- def height_scale
75
- scale = (@height - (@top + @bottom)).to_f / (@image.height - (@top + @bottom))
76
- scale.abs
77
- end
78
-
79
- def draw
80
- return unless @image && @segment_top_left
81
-
82
- @mode == :tiled ? draw_tiled : draw_stretched
83
- end
84
-
85
- def draw_stretched
86
- @segment_top_left.draw(@x, @y, @z, 1, 1, @color)
87
- @segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color) # SCALE X
88
- @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
89
-
90
- @segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color) # SCALE Y
91
- @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
92
- @segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
93
- @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
94
- @segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
95
- @segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
96
- end
97
-
98
- def draw_tiled
99
- @segment_top_left.draw(@x, @y, @z, 1, 1, @color)
100
-
101
- # p [width_scale, height_scale]
102
-
103
- Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
104
- width_scale.ceil.times do |i|
105
- @segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z, 1, 1, @color) # SCALE X
106
- end
107
- end
108
-
109
- @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
110
-
111
- Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
112
- height_scale.ceil.times do |i|
113
- @segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z, 1, 1, @color) # SCALE Y
114
- end
115
- end
116
-
117
- @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
118
-
119
- Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
120
- width_scale.ceil.times do |i|
121
- @segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z, 1, 1, @color) # SCALE X
122
- end
123
- end
124
-
125
- @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
126
-
127
- Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
128
- height_scale.ceil.times do |i|
129
- @segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z, 1, 1, @color) # SCALE Y
130
- end
131
- end
132
-
133
- 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
134
- height_scale.ceil.times do |y|
135
- width_scale.ceil.times do |x|
136
- @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
137
- end
138
- end
139
- end
140
- end
141
- end
142
- end
1
+ module CyberarmEngine
2
+ class BackgroundNineSlice
3
+ include CyberarmEngine::Common
4
+ attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color
5
+ attr_reader :image
6
+
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)
8
+ @image = get_image(image_path) if image_path
9
+
10
+ @x = x
11
+ @y = y
12
+ @z = z
13
+
14
+ @width = width
15
+ @height = height
16
+
17
+ @mode = mode
18
+
19
+ @left = left
20
+ @top = top
21
+ @right = right
22
+ @bottom = bottom
23
+
24
+ @color = color
25
+
26
+ nine_slice if @image
27
+ end
28
+
29
+ def image=(image_path)
30
+ old_image = @image
31
+ @image = image_path ? get_image(image_path) : image_path
32
+ nine_slice if @image && old_image != @image
33
+ end
34
+
35
+ def nine_slice
36
+ # pp [@left, @top, @right, @bottom, @image.width]
37
+
38
+ @segment_top_left = @image.subimage(0, 0, @left, @top)
39
+ @segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)
40
+
41
+ @segment_left = @image.subimage(0, @top, @left, @image.height - (@top + @bottom))
42
+ @segment_right = @image.subimage(@image.width - @right, @top, @left, @image.height - (@top + @bottom))
43
+
44
+ @segment_bottom_left = @image.subimage(0, @image.height - @bottom, @left, @bottom)
45
+ @segment_bottom_right = @image.subimage(@image.width - @right, @image.height - @bottom, @right, @bottom)
46
+
47
+ @segment_top = @image.subimage(@left, 0, @image.width - (@left + @right), @top)
48
+ @segment_bottom = @image.subimage(@left, @image.height - @bottom, @image.width - (@left + @right), @bottom)
49
+
50
+ @segment_middle = @image.subimage(@left, @top, @image.width - (@left + @right), @image.height - (@top + @bottom))
51
+ end
52
+
53
+ def cx
54
+ @x + @left
55
+ end
56
+
57
+ def cy
58
+ @y + @top
59
+ end
60
+
61
+ def cwidth
62
+ @cx - @width
63
+ end
64
+
65
+ def cheight
66
+ @cy - @height
67
+ end
68
+
69
+ def width_scale
70
+ scale = (@width.to_f - (@left + @right)) / (@image.width - (@left + @right))
71
+ scale.abs
72
+ end
73
+
74
+ def height_scale
75
+ scale = (@height - (@top + @bottom)).to_f / (@image.height - (@top + @bottom))
76
+ scale.abs
77
+ end
78
+
79
+ def draw
80
+ return unless @image && @segment_top_left
81
+
82
+ @mode == :tiled ? draw_tiled : draw_stretched
83
+ end
84
+
85
+ def draw_stretched
86
+ @segment_top_left.draw(@x, @y, @z, 1, 1, @color)
87
+ @segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color) # SCALE X
88
+ @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
89
+
90
+ @segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color) # SCALE Y
91
+ @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
92
+ @segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
93
+ @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
94
+ @segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
95
+ @segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
96
+ end
97
+
98
+ def draw_tiled
99
+ @segment_top_left.draw(@x, @y, @z, 1, 1, @color)
100
+
101
+ # p [width_scale, height_scale]
102
+
103
+ Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
104
+ width_scale.ceil.times do |i|
105
+ @segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z, 1, 1, @color) # SCALE X
106
+ end
107
+ end
108
+
109
+ @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
110
+
111
+ Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
112
+ height_scale.ceil.times do |i|
113
+ @segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z, 1, 1, @color) # SCALE Y
114
+ end
115
+ end
116
+
117
+ @segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
118
+
119
+ Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
120
+ width_scale.ceil.times do |i|
121
+ @segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z, 1, 1, @color) # SCALE X
122
+ end
123
+ end
124
+
125
+ @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
126
+
127
+ Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
128
+ height_scale.ceil.times do |i|
129
+ @segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z, 1, 1, @color) # SCALE Y
130
+ end
131
+ end
132
+
133
+ 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
134
+ height_scale.ceil.times do |y|
135
+ width_scale.ceil.times do |x|
136
+ @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
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end