minigl 1.3.7 → 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,143 +1,143 @@
1
1
  module AGL
2
- # This class provides methods for easily drawing one or multiple lines of
3
- # text, with control over the text alignment and coloring.
4
- class TextHelper
5
- # Creates a TextHelper.
6
- #
7
- # Parameters:
8
- # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
- # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
- # between each line.
11
- def initialize font, line_spacing = 0
12
- @font = font
13
- @line_spacing = line_spacing
14
- end
15
-
16
- # Draws a single line of text.
17
- #
18
- # Parameters:
19
- # [text] The text to be drawn. No line breaks are allowed.
20
- # [x] The horizontal reference for drawing the text. If +mode+ is +:left+,
21
- # all text will be drawn from this point to the right; if +mode+ is
22
- # +:right+, all text will be drawn from this point to the left; and if
23
- # +mode+ is +:center+, the text will be equally distributed to the
24
- # left and to the right of this point.
25
- # [y] The vertical reference for drawing the text. All text will be drawn
26
- # from this point down.
27
- # [mode] The alignment of the text. Valid values are +:left+, +:right+ and
28
- # +:center+.
29
- # [color] The color of the text, in hexadecimal RRGGBB format.
30
- # [alpha] The opacity of the text. Valid values vary from 0 (fully
31
- # transparent) to 255 (fully opaque).
32
- # [z_index] The z-order to draw the object. Objects with larger z-orders
33
- # will be drawn on top of the ones with smaller z-orders.
34
- def write_line text, x, y, mode = :left, color = 0, alpha = 0xff, z_index = 0
35
- color = (alpha << 24) | color
36
- rel =
37
- case mode
38
- when :left then 0
39
- when :center then 0.5
40
- when :right then 1
41
- else 0
42
- end
43
- @font.draw_rel text, x, y, z_index, rel, 0, 1, 1, color
44
- end
45
-
46
- # Draws text, breaking lines when needed and when explicitly caused by the
47
- # "\n" character.
48
- #
49
- # Parameters:
50
- # [text] The text to be drawn. Line breaks are allowed.
51
- # [x] The horizontal reference for drawing the text. Works like in
52
- # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
53
- # +:justified+ mode, works the same as for +:left+.
54
- # [y] The vertical reference for drawing the text. All text will be drawn
55
- # from this point down.
56
- # [width] The maximum width for the lines of text. Line is broken when
57
- # this width is exceeded.
58
- # [mode] The alignment of the text. Valid values are +:left+, +:right+,
59
- # +:center+ and +:justified+.
60
- # [color] The color of the text, in hexadecimal RRGGBB format.
61
- # [alpha] The opacity of the text. Valid values vary from 0 (fully
62
- # transparent) to 255 (fully opaque).
63
- # [z_index] The z-order to draw the object. Objects with larger z-orders
64
- # will be drawn on top of the ones with smaller z-orders.
65
- def write_breaking text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0
66
- color = (alpha << 24) | color
67
- text.split("\n").each do |p|
68
- if mode == :justified
69
- y = write_paragraph_justified p, x, y, width, color, z_index
70
- else
71
- rel =
72
- case mode
73
- when :left then 0
74
- when :center then 0.5
75
- when :right then 1
76
- else 0
77
- end
78
- y = write_paragraph p, x, y, width, rel, color, z_index
79
- end
80
- end
81
- end
82
-
83
- private
84
-
85
- def write_paragraph text, x, y, width, rel, color, z_index
86
- line = ""
87
- line_width = 0
88
- text.split(' ').each do |word|
89
- w = @font.text_width word
90
- if line_width + w > width
91
- @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color
92
- line = ""
93
- line_width = 0
94
- y += @font.height + @line_spacing
95
- end
96
- line += "#{word} "
97
- line_width += @font.text_width "#{word} "
98
- end
99
- @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color if not line.empty?
100
- y + @font.height + @line_spacing
101
- end
102
-
103
- def write_paragraph_justified text, x, y, width, color, z_index
104
- space_width = @font.text_width " "
105
- spaces = [[]]
106
- line_index = 0
107
- new_x = x
108
- words = text.split(' ')
109
- words.each do |word|
110
- w = @font.text_width word
111
- if new_x + w > x + width
112
- space = x + width - new_x + space_width
113
- index = 0
114
- while space > 0
115
- spaces[line_index][index] += 1
116
- space -= 1
117
- index += 1
118
- index = 0 if index == spaces[line_index].size - 1
119
- end
120
-
121
- spaces << []
122
- line_index += 1
123
-
124
- new_x = x
125
- end
126
- new_x += @font.text_width(word) + space_width
127
- spaces[line_index] << space_width
128
- end
129
-
130
- index = 0
131
- spaces.each do |line|
132
- new_x = x
133
- line.each do |s|
134
- @font.draw words[index], new_x, y, z_index, 1, 1, color
135
- new_x += @font.text_width(words[index]) + s
136
- index += 1
137
- end
138
- y += @font.height + @line_spacing
139
- end
140
- y
141
- end
142
- end
2
+ # This class provides methods for easily drawing one or multiple lines of
3
+ # text, with control over the text alignment and coloring.
4
+ class TextHelper
5
+ # Creates a TextHelper.
6
+ #
7
+ # Parameters:
8
+ # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
+ # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
+ # between each line.
11
+ def initialize font, line_spacing = 0
12
+ @font = font
13
+ @line_spacing = line_spacing
14
+ end
15
+
16
+ # Draws a single line of text.
17
+ #
18
+ # Parameters:
19
+ # [text] The text to be drawn. No line breaks are allowed.
20
+ # [x] The horizontal reference for drawing the text. If +mode+ is +:left+,
21
+ # all text will be drawn from this point to the right; if +mode+ is
22
+ # +:right+, all text will be drawn from this point to the left; and if
23
+ # +mode+ is +:center+, the text will be equally distributed to the
24
+ # left and to the right of this point.
25
+ # [y] The vertical reference for drawing the text. All text will be drawn
26
+ # from this point down.
27
+ # [mode] The alignment of the text. Valid values are +:left+, +:right+ and
28
+ # +:center+.
29
+ # [color] The color of the text, in hexadecimal RRGGBB format.
30
+ # [alpha] The opacity of the text. Valid values vary from 0 (fully
31
+ # transparent) to 255 (fully opaque).
32
+ # [z_index] The z-order to draw the object. Objects with larger z-orders
33
+ # will be drawn on top of the ones with smaller z-orders.
34
+ def write_line text, x, y, mode = :left, color = 0, alpha = 0xff, z_index = 0
35
+ color = (alpha << 24) | color
36
+ rel =
37
+ case mode
38
+ when :left then 0
39
+ when :center then 0.5
40
+ when :right then 1
41
+ else 0
42
+ end
43
+ @font.draw_rel text, x, y, z_index, rel, 0, 1, 1, color
44
+ end
45
+
46
+ # Draws text, breaking lines when needed and when explicitly caused by the
47
+ # "\n" character.
48
+ #
49
+ # Parameters:
50
+ # [text] The text to be drawn. Line breaks are allowed.
51
+ # [x] The horizontal reference for drawing the text. Works like in
52
+ # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
53
+ # +:justified+ mode, works the same as for +:left+.
54
+ # [y] The vertical reference for drawing the text. All text will be drawn
55
+ # from this point down.
56
+ # [width] The maximum width for the lines of text. Line is broken when
57
+ # this width is exceeded.
58
+ # [mode] The alignment of the text. Valid values are +:left+, +:right+,
59
+ # +:center+ and +:justified+.
60
+ # [color] The color of the text, in hexadecimal RRGGBB format.
61
+ # [alpha] The opacity of the text. Valid values vary from 0 (fully
62
+ # transparent) to 255 (fully opaque).
63
+ # [z_index] The z-order to draw the object. Objects with larger z-orders
64
+ # will be drawn on top of the ones with smaller z-orders.
65
+ def write_breaking text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0
66
+ color = (alpha << 24) | color
67
+ text.split("\n").each do |p|
68
+ if mode == :justified
69
+ y = write_paragraph_justified p, x, y, width, color, z_index
70
+ else
71
+ rel =
72
+ case mode
73
+ when :left then 0
74
+ when :center then 0.5
75
+ when :right then 1
76
+ else 0
77
+ end
78
+ y = write_paragraph p, x, y, width, rel, color, z_index
79
+ end
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def write_paragraph text, x, y, width, rel, color, z_index
86
+ line = ""
87
+ line_width = 0
88
+ text.split(' ').each do |word|
89
+ w = @font.text_width word
90
+ if line_width + w > width
91
+ @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color
92
+ line = ""
93
+ line_width = 0
94
+ y += @font.height + @line_spacing
95
+ end
96
+ line += "#{word} "
97
+ line_width += @font.text_width "#{word} "
98
+ end
99
+ @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color unless line.empty?
100
+ y + @font.height + @line_spacing
101
+ end
102
+
103
+ def write_paragraph_justified text, x, y, width, color, z_index
104
+ space_width = @font.text_width " "
105
+ spaces = [[]]
106
+ line_index = 0
107
+ new_x = x
108
+ words = text.split(' ')
109
+ words.each do |word|
110
+ w = @font.text_width word
111
+ if new_x + w > x + width
112
+ space = x + width - new_x + space_width
113
+ index = 0
114
+ while space > 0
115
+ spaces[line_index][index] += 1
116
+ space -= 1
117
+ index += 1
118
+ index = 0 if index == spaces[line_index].size - 1
119
+ end
120
+
121
+ spaces << []
122
+ line_index += 1
123
+
124
+ new_x = x
125
+ end
126
+ new_x += @font.text_width(word) + space_width
127
+ spaces[line_index] << space_width
128
+ end
129
+
130
+ index = 0
131
+ spaces.each do |line|
132
+ new_x = x
133
+ line.each do |s|
134
+ @font.draw words[index], new_x, y, z_index, 1, 1, color
135
+ new_x += @font.text_width(words[index]) + s
136
+ index += 1
137
+ end
138
+ y += @font.height + @line_spacing
139
+ end
140
+ y
141
+ end
142
+ end
143
143
  end
@@ -3,61 +3,61 @@ require_relative '../lib/minigl'
3
3
  include AGL
4
4
 
5
5
  class SpriteTest < Test::Unit::TestCase
6
- def setup
7
- @window = Gosu::Window.new 800, 600, false
8
- Game.initialize @window
6
+ def setup
7
+ @window = Gosu::Window.new 800, 600, false
8
+ Game.initialize @window
9
9
  Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
10
- end
10
+ end
11
11
 
12
- def test_sprite_position
13
- s = Sprite.new 10, 20, :image
14
- assert_equal 10, s.x
15
- assert_equal 20, s.y
16
- s = Sprite.new -100, 200, :image
17
- assert_equal -100, s.x
18
- assert_equal 200, s.y
19
- end
12
+ def test_sprite_position
13
+ s = Sprite.new 10, 20, :image
14
+ assert_equal 10, s.x
15
+ assert_equal 20, s.y
16
+ s = Sprite.new -100, 200, :image
17
+ assert_equal -100, s.x
18
+ assert_equal 200, s.y
19
+ end
20
20
 
21
- def test_sprite_animation
22
- s = Sprite.new 10, 20, :image, 3, 1
23
- indices = [0, 1, 2]
24
- interval = 1
25
- 3.times { s.animate indices, interval }
26
- assert_equal 0, s.img_index
27
- 5.times { s.animate indices, interval }
28
- assert_equal 2, s.img_index
29
- end
21
+ def test_sprite_animation
22
+ s = Sprite.new 10, 20, :image, 3, 1
23
+ indices = [0, 1, 2]
24
+ interval = 1
25
+ 3.times { s.animate indices, interval }
26
+ assert_equal 0, s.img_index
27
+ 5.times { s.animate indices, interval }
28
+ assert_equal 2, s.img_index
29
+ end
30
30
  end
31
31
 
32
32
  class GameObjectTest < Test::Unit::TestCase
33
- def setup
34
- @window = Gosu::Window.new 800, 600, false
35
- Game.initialize @window
33
+ def setup
34
+ @window = Gosu::Window.new 800, 600, false
35
+ Game.initialize @window
36
36
  Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
37
- end
37
+ end
38
38
 
39
- def test_game_object_attributes
40
- o = GameObject.new 10, 20, 3, 1, :image
41
- assert_equal 10, o.x
42
- assert_equal 20, o.y
43
- assert_equal 3, o.w
44
- assert_equal 1, o.h
45
- assert_equal 0, o.speed.x
46
- assert_equal 0, o.speed.y
47
- assert_equal 0, o.stored_forces.x
48
- assert_equal 0, o.stored_forces.y
49
- end
39
+ def test_game_object_attributes
40
+ o = GameObject.new 10, 20, 3, 1, :image
41
+ assert_equal 10, o.x
42
+ assert_equal 20, o.y
43
+ assert_equal 3, o.w
44
+ assert_equal 1, o.h
45
+ assert_equal 0, o.speed.x
46
+ assert_equal 0, o.speed.y
47
+ assert_equal 0, o.stored_forces.x
48
+ assert_equal 0, o.stored_forces.y
49
+ end
50
50
 
51
- def test_game_object_animation
52
- o = GameObject.new 10, 20, 3, 1, :image, nil, 3, 1
53
- indices = [0, 1, 2]
54
- interval = 10
55
- 5.times { o.animate indices, interval }
56
- assert_equal 0, o.img_index
57
- o.set_animation 0
58
- 5.times { o.animate indices, interval }
59
- assert_equal 0, o.img_index
60
- 5.times { o.animate indices, interval }
61
- assert_equal 1, o.img_index
62
- end
51
+ def test_game_object_animation
52
+ o = GameObject.new 10, 20, 3, 1, :image, nil, 3, 1
53
+ indices = [0, 1, 2]
54
+ interval = 10
55
+ 5.times { o.animate indices, interval }
56
+ assert_equal 0, o.img_index
57
+ o.set_animation 0
58
+ 5.times { o.animate indices, interval }
59
+ assert_equal 0, o.img_index
60
+ 5.times { o.animate indices, interval }
61
+ assert_equal 1, o.img_index
62
+ end
63
63
  end
@@ -2,39 +2,39 @@ require_relative '../lib/minigl'
2
2
  include AGL
3
3
 
4
4
  class MyGame < Gosu::Window
5
- def initialize
6
- super 800, 600, false
7
- Game.initialize self
5
+ def initialize
6
+ super 800, 600, false
7
+ Game.initialize self
8
8
 
9
- @tile1 = Res.img :tile2
10
- @tile2 = Res.img :tile2b
11
- @map = Map.new 25, 17, 200, 200, 800, 600, true
12
- @p = Vector.new -1, -1
13
- end
9
+ @tile1 = Res.img :tile2
10
+ @tile2 = Res.img :tile2b
11
+ @map = Map.new 25, 17, 200, 200, 800, 600, true
12
+ @p = Vector.new -1, -1
13
+ end
14
14
 
15
- def needs_cursor?
16
- true
17
- end
15
+ def needs_cursor?
16
+ true
17
+ end
18
18
 
19
- def update
20
- KB.update
21
- Mouse.update
22
- p = @map.get_map_pos Mouse.x, Mouse.y
23
- @p = p if @map.is_in_map p
19
+ def update
20
+ KB.update
21
+ Mouse.update
22
+ p = @map.get_map_pos Mouse.x, Mouse.y
23
+ @p = p if @map.is_in_map p
24
24
 
25
- @map.move_camera 0, -5 if KB.key_down? Gosu::KbUp
26
- @map.move_camera 5, 0 if KB.key_down? Gosu::KbRight
27
- @map.move_camera 0, 5 if KB.key_down? Gosu::KbDown
28
- @map.move_camera -5, 0 if KB.key_down? Gosu::KbLeft
25
+ @map.move_camera 0, -4.5 if KB.key_down? Gosu::KbUp
26
+ @map.move_camera 4.5, 0 if KB.key_down? Gosu::KbRight
27
+ @map.move_camera 0, 4.5 if KB.key_down? Gosu::KbDown
28
+ @map.move_camera -4.5, 0 if KB.key_down? Gosu::KbLeft
29
29
  @map.set_camera 0, 0 if KB.key_down? Gosu::KbReturn
30
- end
30
+ end
31
31
 
32
- def draw
33
- @map.foreach do |i, j, x, y|
34
- if i == @p.x and j == @p.y; @tile2.draw x, y, 0
35
- else; @tile1.draw x, y, 0; end
36
- end
37
- end
32
+ def draw
33
+ @map.foreach do |i, j, x, y|
34
+ if i == @p.x and j == @p.y; @tile2.draw x, y, 0
35
+ else; @tile1.draw x, y, 0; end
36
+ end
37
+ end
38
38
  end
39
39
 
40
40
  MyGame.new.show