gosu 0.7.9-x86-mswin32-60 → 0.7.9.1-x86-mswin32-60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
File without changes
@@ -19,7 +19,7 @@ rescue LoadError
19
19
  end
20
20
 
21
21
  require 'gosu'
22
- require 'rmagick'
22
+ require 'RMagick'
23
23
 
24
24
  NULL_PIXEL = Magick::Pixel.from_color('none')
25
25
 
@@ -0,0 +1,139 @@
1
+ # This example demonstrates the use of the TextInput functionality.
2
+ # One can tab through, or click into the text fields and change it's contents.
3
+
4
+ # At its most basic form, you only need to create a new TextInput instance and
5
+ # set the text_input attribute of your window to it. Until you set this
6
+ # attribute to nil again, the TextInput object will build a text that can be
7
+ # accessed via TextInput#text.
8
+
9
+ # The TextInput object also maintains the position of the caret as the index
10
+ # of the character that it's left to via the caret_pos attribute. Furthermore,
11
+ # if there is a selection, the selection_start attribute yields its beginning,
12
+ # using the same indexing scheme. If there is no selection, selection_start
13
+ # is equal to caret_pos.
14
+
15
+ # A TextInput object is purely abstract, though; drawing the input field is left
16
+ # to the user. In this case, we are subclassing TextInput to add this code.
17
+ # As with most of Gosu, how this is handled is completely left open; the scheme
18
+ # presented here is not mandatory! Gosu only aims to provide enough code for
19
+ # games (or intermediate UI toolkits) to be built upon it.
20
+
21
+ begin
22
+ # In case you use Gosu via RubyGems.
23
+ require 'rubygems'
24
+ rescue LoadError
25
+ # In case you don't.
26
+ end
27
+
28
+ require 'gosu'
29
+
30
+ class TextField < Gosu::TextInput
31
+ # Some constants that define our appearance.
32
+ INACTIVE_COLOR = 0xcc666666
33
+ ACTIVE_COLOR = 0xccff6666
34
+ SELECTION_COLOR = 0xcc0000ff
35
+ CARET_COLOR = 0xffffffff
36
+ PADDING = 5
37
+
38
+ attr_reader :x, :y
39
+
40
+ def initialize(window, font, x, y)
41
+ # TextInput's constructor doesn't expect any arguments.
42
+ super()
43
+
44
+ @window, @font, @x, @y = window, font, x, y
45
+
46
+ # Start with a self-explanatory text in each field.
47
+ self.text = "Click to change text"
48
+ end
49
+
50
+ def draw
51
+ # Depending on whether this is the currently selected input or not, change the
52
+ # background's color.
53
+ if @window.text_input == self then
54
+ background_color = ACTIVE_COLOR
55
+ else
56
+ background_color = INACTIVE_COLOR
57
+ end
58
+ @window.draw_quad(x - PADDING, y - PADDING, background_color,
59
+ x + width + PADDING, y - PADDING, background_color,
60
+ x - PADDING, y + height + PADDING, background_color,
61
+ x + width + PADDING, y + height + PADDING, background_color, 0)
62
+
63
+ # Calculate the position of the caret and the selection start.
64
+ pos_x = x + @font.text_width(self.text[0...self.caret_pos])
65
+ sel_x = x + @font.text_width(self.text[0...self.selection_start])
66
+
67
+ # Draw the selection background, if any; if not, sel_x and pos_x will be
68
+ # the same value, making this quad empty.
69
+ @window.draw_quad(sel_x, y, SELECTION_COLOR,
70
+ pos_x, y, SELECTION_COLOR,
71
+ sel_x, y + height, SELECTION_COLOR,
72
+ pos_x, y + height, SELECTION_COLOR, 0)
73
+
74
+ # Draw the caret; again, only if this is the currently selected field.
75
+ if @window.text_input == self then
76
+ @window.draw_line(pos_x, y, CARET_COLOR,
77
+ pos_x, y + height, CARET_COLOR, 0)
78
+ end
79
+
80
+ # Finally, draw the text itself!
81
+ @font.draw(self.text, x, y, 0)
82
+ end
83
+
84
+ # This text field grows with the text that's being entered.
85
+ # (Without clipping, one has to be a bit creative about this ;) )
86
+ def width
87
+ @font.text_width(self.text)
88
+ end
89
+
90
+ def height
91
+ @font.height
92
+ end
93
+
94
+ # Hit-test for selecting a text field with the mouse.
95
+ def under_point?(mouse_x, mouse_y)
96
+ mouse_x > x - PADDING and mouse_x < x + width + PADDING and
97
+ mouse_y > y - PADDING and mouse_y < y + height + PADDING
98
+ end
99
+ end
100
+
101
+ class TextInputWindow < Gosu::Window
102
+ def initialize
103
+ super(300, 200, false)
104
+ self.caption = "Text Input Example"
105
+
106
+ font = Gosu::Font.new(self, Gosu::default_font_name, 20)
107
+
108
+ # Set up an array of three text fields.
109
+ @text_fields = Array.new(3) { |index| TextField.new(self, font, 50, 30 + index * 50) }
110
+
111
+ @cursor = Gosu::Image.new(self, "media/Cursor.png", false)
112
+ end
113
+
114
+ def draw
115
+ @text_fields.each { |tf| tf.draw }
116
+ @cursor.draw(mouse_x, mouse_y, 0)
117
+ end
118
+
119
+ def button_down(id)
120
+ if id == Gosu::KbTab then
121
+ # Tab key will not be 'eaten' by text fields; use for switching through
122
+ # text fields.
123
+ index = @text_fields.index(self.text_input) || -1
124
+ self.text_input = @text_fields[(index + 1) % @text_fields.size]
125
+ elsif id == Gosu::KbEscape then
126
+ # Escape key will not be 'eaten' by text fields; use for deselecting.
127
+ if self.text_input then
128
+ self.text_input = nil
129
+ else
130
+ close
131
+ end
132
+ elsif id == Gosu::MsLeft then
133
+ # Mouse click: Select text field based on mouse position.
134
+ self.text_input = @text_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
135
+ end
136
+ end
137
+ end
138
+
139
+ TextInputWindow.new.show
File without changes
File without changes
Binary file
File without changes
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.7.9.1
5
5
  platform: x86-mswin32-60
6
6
  authors:
7
7
  - Julian Raschke
@@ -31,12 +31,14 @@ files:
31
31
  - examples/MoreChipmunkAndRMagick.rb
32
32
  - examples/OpenGLIntegration.rb
33
33
  - examples/RMagickIntegration.rb
34
+ - examples/TextInput.rb
34
35
  - examples/Tutorial.rb
35
36
  - examples/media/Beep.wav
36
37
  - examples/media/CptnRuby Gem.png
37
38
  - examples/media/CptnRuby Map.txt
38
39
  - examples/media/CptnRuby Tileset.png
39
40
  - examples/media/CptnRuby.png
41
+ - examples/media/Cursor.png
40
42
  - examples/media/Earth.png
41
43
  - examples/media/Explosion.wav
42
44
  - examples/media/LargeStar.png