gosu-examples 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/gosu-examples +19 -19
- data/examples/chipmunk_and_rmagick.rb +9 -10
- data/examples/chipmunk_integration.rb +3 -4
- data/examples/cptn_ruby.rb +4 -5
- data/examples/opengl_integration.rb +3 -4
- data/examples/rmagick_integration.rb +18 -19
- data/examples/text_input.rb +158 -0
- data/examples/tutorial.rb +2 -3
- data/examples/welcome.rb +4 -5
- data/lib/gosu-examples/example.rb +21 -0
- data/lib/gosu-examples/sidebar.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cb2816210de706b627f2c8120ee44874cd16035676c20f6b18d14ac52b3c3b09
|
4
|
+
data.tar.gz: 58c289986480228d033a32ba8c3e934d6166ff6b458fa2897fdafa09f90ddc9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59bb842a0be571014dbdf4e4257840f383360de3c79a7fecccc981ca384e6b27938da9e3544a843268ae6d8c5125e099154f4f6afc690796fe534fb10a644969
|
7
|
+
data.tar.gz: 32c948f2eeed4ac34d5168f7dea387c3c8d8fcbbea758088ca68fcfa95e0aa37f5ff46c925eaa410c450651966907c8d744118507c01191afb80b7550b612b02
|
data/bin/gosu-examples
CHANGED
@@ -1,25 +1,27 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'gosu'
|
3
|
+
require "gosu"
|
5
4
|
|
6
5
|
Dir.chdir "#{File.dirname __FILE__}/../examples"
|
7
6
|
|
8
7
|
$LOAD_PATH << "#{File.dirname __FILE__}/../lib/gosu-examples"
|
9
8
|
|
10
|
-
require
|
11
|
-
require
|
9
|
+
require "example"
|
10
|
+
require "sidebar"
|
12
11
|
|
13
12
|
Example.load_examples "*.rb"
|
14
13
|
|
15
14
|
class ExampleBox < Gosu::Window
|
16
15
|
def initialize
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
welcome_class = Example.initial_example
|
17
|
+
welcome = welcome_class.new
|
18
|
+
|
19
|
+
super welcome.width + Sidebar::WIDTH, welcome.height, fullscreen: ARGV.include?("--fullscreen")
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
@sidebar = Sidebar.new do |example_class|
|
22
|
+
self.current_example = example_class.new unless @current_example.is_a? example_class
|
23
|
+
end
|
24
|
+
self.current_example = welcome
|
23
25
|
end
|
24
26
|
|
25
27
|
def update
|
@@ -43,11 +45,11 @@ class ExampleBox < Gosu::Window
|
|
43
45
|
case id
|
44
46
|
when Gosu::KB_ESCAPE
|
45
47
|
close
|
46
|
-
when Gosu.char_to_button_id(
|
48
|
+
when Gosu.char_to_button_id("E")
|
47
49
|
if filename = @current_example.class.source_file
|
48
50
|
open_file_or_folder filename
|
49
51
|
end
|
50
|
-
when Gosu.char_to_button_id(
|
52
|
+
when Gosu.char_to_button_id("O")
|
51
53
|
if filename = @current_example.class.source_file
|
52
54
|
open_file_or_folder File.dirname(filename)
|
53
55
|
end
|
@@ -73,14 +75,12 @@ class ExampleBox < Gosu::Window
|
|
73
75
|
|
74
76
|
private
|
75
77
|
|
76
|
-
def
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
self.height = @current_example.height
|
83
|
-
end
|
78
|
+
def current_example=(example)
|
79
|
+
self.text_input = nil
|
80
|
+
@current_example = example
|
81
|
+
@current_example.parent_window = self
|
82
|
+
self.width = @current_example.width + Sidebar::WIDTH
|
83
|
+
self.height = @current_example.height
|
84
84
|
end
|
85
85
|
|
86
86
|
def open_file_or_folder(filename)
|
@@ -6,10 +6,9 @@
|
|
6
6
|
# License: Same as for Gosu (MIT)
|
7
7
|
# Created on 21/10/2007, 00:05:19 by Robert Sheehan
|
8
8
|
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require 'rmagick'
|
9
|
+
require "gosu"
|
10
|
+
require "chipmunk"
|
11
|
+
require "rmagick"
|
13
12
|
|
14
13
|
# Layering of sprites
|
15
14
|
module ZOrder
|
@@ -42,7 +41,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
42
41
|
fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
|
43
42
|
background = Magick::Image.new(WIDTH, HEIGHT, fill)
|
44
43
|
setup_triangles(background)
|
45
|
-
@background_image = Gosu::Image.new(background, :
|
44
|
+
@background_image = Gosu::Image.new(background, tileable: true) # turn the image into a Gosu one
|
46
45
|
@boxes = create_boxes(NUM_POLYGONS)
|
47
46
|
end
|
48
47
|
|
@@ -51,8 +50,8 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
51
50
|
def setup_triangles(background)
|
52
51
|
gc = Magick::Draw.new
|
53
52
|
gc.stroke_width(2)
|
54
|
-
gc.stroke(
|
55
|
-
gc.fill(
|
53
|
+
gc.stroke("red")
|
54
|
+
gc.fill("blue")
|
56
55
|
# all the triangles are part of the same body
|
57
56
|
body = CP::Body.new(Float::MAX, Float::MAX)
|
58
57
|
base = 15
|
@@ -87,10 +86,10 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
87
86
|
|
88
87
|
# Produces the image of a polygon.
|
89
88
|
def polygon_image(vertices)
|
90
|
-
box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color =
|
89
|
+
box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = "transparent" }
|
91
90
|
gc = Magick::Draw.new
|
92
|
-
gc.stroke(
|
93
|
-
gc.fill(
|
91
|
+
gc.stroke("red")
|
92
|
+
gc.fill("plum")
|
94
93
|
draw_vertices = vertices.map { |v| [v.x + EDGE_SIZE, v.y + EDGE_SIZE] }.flatten
|
95
94
|
gc.polygon(*draw_vertices)
|
96
95
|
gc.draw(box_image)
|
@@ -8,9 +8,8 @@
|
|
8
8
|
## Comments: Based on the Gosu Ruby Tutorial, but incorporating the Chipmunk Physics Engine
|
9
9
|
## See https://github.com/jlnr/gosu/wiki/Ruby-Chipmunk-Integration for the accompanying text.
|
10
10
|
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require 'chipmunk'
|
11
|
+
require "gosu"
|
12
|
+
require "chipmunk"
|
14
13
|
|
15
14
|
WIDTH = 640
|
16
15
|
HEIGHT = 480
|
@@ -125,7 +124,7 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
125
124
|
|
126
125
|
self.caption = "Gosu & Chipmunk Integration Demo"
|
127
126
|
|
128
|
-
@background_image = Gosu::Image.new("media/space.png", :
|
127
|
+
@background_image = Gosu::Image.new("media/space.png", tileable: true)
|
129
128
|
|
130
129
|
# Put the beep here, as it is the environment now that determines collision
|
131
130
|
@beep = Gosu::Sample.new("media/beep.wav")
|
data/examples/cptn_ruby.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Encoding: UTF-8
|
2
2
|
|
3
|
-
#
|
3
|
+
# A simple jump-and-run/platformer game with a tile-based map.
|
4
4
|
|
5
5
|
# Shows how to
|
6
6
|
# * implement jumping/gravity
|
@@ -27,8 +27,7 @@
|
|
27
27
|
# ...Enemies, a more sophisticated object system, weapons, title and credits
|
28
28
|
# screens...
|
29
29
|
|
30
|
-
require
|
31
|
-
require 'gosu'
|
30
|
+
require "gosu"
|
32
31
|
|
33
32
|
WIDTH, HEIGHT = 640, 480
|
34
33
|
|
@@ -140,7 +139,7 @@ class Map
|
|
140
139
|
|
141
140
|
def initialize(filename)
|
142
141
|
# Load 60x60 tiles, 5px overlap in all four directions.
|
143
|
-
@tileset = Gosu::Image.load_tiles("media/tileset.png", 60, 60, :
|
142
|
+
@tileset = Gosu::Image.load_tiles("media/tileset.png", 60, 60, tileable: true)
|
144
143
|
|
145
144
|
gem_img = Gosu::Image.new("media/gem.png")
|
146
145
|
@gems = []
|
@@ -193,7 +192,7 @@ class CptnRuby < (Example rescue Gosu::Window)
|
|
193
192
|
|
194
193
|
self.caption = "Cptn. Ruby"
|
195
194
|
|
196
|
-
@sky = Gosu::Image.new("media/space.png", :
|
195
|
+
@sky = Gosu::Image.new("media/space.png", tileable: true)
|
197
196
|
@map = Map.new("media/cptn_ruby_map.txt")
|
198
197
|
@cptn = Player.new(@map, 400, 100)
|
199
198
|
# The scrolling position is stored as top left corner of the screen.
|
@@ -5,9 +5,8 @@
|
|
5
5
|
# the block given to Window#gl, and that Gosu Images can be
|
6
6
|
# used as textures using the gl_tex_info call.
|
7
7
|
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require 'gl'
|
8
|
+
require "gosu"
|
9
|
+
require "gl"
|
11
10
|
|
12
11
|
WIDTH, HEIGHT = 640, 480
|
13
12
|
|
@@ -25,7 +24,7 @@ class GLBackground
|
|
25
24
|
SCROLLS_PER_STEP = 50
|
26
25
|
|
27
26
|
def initialize
|
28
|
-
@image = Gosu::Image.new("media/earth.png", :
|
27
|
+
@image = Gosu::Image.new("media/earth.png", tileable: true)
|
29
28
|
@scrolls = 0
|
30
29
|
@height_map = Array.new(POINTS_Y) { Array.new(POINTS_X) { rand } }
|
31
30
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# Encoding: UTF-8
|
2
2
|
|
3
|
-
# A
|
3
|
+
# A simple Gorilla-style shooter for two players.
|
4
4
|
# Shows how Gosu and RMagick can be used together to generate a map, implement
|
5
5
|
# a dynamic landscape and generally look great.
|
6
6
|
# Also shows a very minimal, yet effective way of designing a game's object system.
|
@@ -13,13 +13,12 @@
|
|
13
13
|
# * The look of dead soldiers is, err, by accident. Soldier.png needs to be
|
14
14
|
# designed in a less obfuscated way :)
|
15
15
|
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require 'rmagick'
|
16
|
+
require "gosu"
|
17
|
+
require "rmagick"
|
19
18
|
|
20
19
|
WIDTH, HEIGHT = 640, 480
|
21
20
|
|
22
|
-
NULL_PIXEL = Magick::Pixel.from_color(
|
21
|
+
NULL_PIXEL = Magick::Pixel.from_color("none")
|
23
22
|
|
24
23
|
# The class for this game's map.
|
25
24
|
# Design:
|
@@ -35,13 +34,13 @@ class Map
|
|
35
34
|
# Loading SVG files isn't possible with Gosu, so say wow!
|
36
35
|
# (Seems to take a while though)
|
37
36
|
sky = Magick::Image.read("media/landscape.svg").first
|
38
|
-
@sky = Gosu::Image.new(sky, :
|
37
|
+
@sky = Gosu::Image.new(sky, tileable: true)
|
39
38
|
|
40
39
|
# Create the map an stores the RMagick image in @image
|
41
40
|
create_rmagick_map
|
42
41
|
|
43
42
|
# Copy the RMagick Image to a Gosu Image (still unchanged)
|
44
|
-
@gosu_image = Gosu::Image.new(@image, :
|
43
|
+
@gosu_image = Gosu::Image.new(@image, tileable: true)
|
45
44
|
end
|
46
45
|
|
47
46
|
def solid? x, y
|
@@ -68,9 +67,9 @@ class Map
|
|
68
67
|
# Create the crater image (basically a circle shape that is used to erase
|
69
68
|
# parts of the map) and the crater shadow image.
|
70
69
|
CRATER_IMAGE = begin
|
71
|
-
crater = Magick::Image.new(2 * RADIUS, 2 * RADIUS) { self.background_color =
|
70
|
+
crater = Magick::Image.new(2 * RADIUS, 2 * RADIUS) { self.background_color = "none" }
|
72
71
|
gc = Magick::Draw.new
|
73
|
-
gc.fill(
|
72
|
+
gc.fill("black").circle(RADIUS, RADIUS, RADIUS, 0)
|
74
73
|
gc.draw crater
|
75
74
|
crater
|
76
75
|
end
|
@@ -93,14 +92,14 @@ class Map
|
|
93
92
|
|
94
93
|
def create_rmagick_map
|
95
94
|
# This is the one large RMagick image that represents the map.
|
96
|
-
@image = Magick::Image.new(WIDTH, HEIGHT) { self.background_color =
|
95
|
+
@image = Magick::Image.new(WIDTH, HEIGHT) { self.background_color = "none" }
|
97
96
|
|
98
97
|
# Set up a Draw object that fills with an earth texture.
|
99
|
-
earth = Magick::Image.read(
|
98
|
+
earth = Magick::Image.read("media/earth.png").first.resize(1.5)
|
100
99
|
gc = Magick::Draw.new
|
101
|
-
gc.pattern(
|
102
|
-
gc.fill(
|
103
|
-
gc.stroke(
|
100
|
+
gc.pattern("earth", 0, 0, earth.columns, earth.rows) { gc.composite(0, 0, 0, 0, earth) }
|
101
|
+
gc.fill("earth")
|
102
|
+
gc.stroke("#603000").stroke_width(1.5)
|
104
103
|
# Draw a smooth bezier island onto the map!
|
105
104
|
polypoints = [0, HEIGHT]
|
106
105
|
0.upto(8) do |x|
|
@@ -112,13 +111,13 @@ class Map
|
|
112
111
|
|
113
112
|
# Create a bright-dark gradient fill, an image from it and change the map's
|
114
113
|
# brightness with it.
|
115
|
-
fill = Magick::GradientFill.new(0, HEIGHT * 0.4, WIDTH, HEIGHT * 0.4,
|
114
|
+
fill = Magick::GradientFill.new(0, HEIGHT * 0.4, WIDTH, HEIGHT * 0.4, "#fff", "#666")
|
116
115
|
gradient = Magick::Image.new(WIDTH, HEIGHT, fill)
|
117
116
|
gradient = @image.composite(gradient, 0, 0, Magick::InCompositeOp)
|
118
117
|
@image.composite!(gradient, 0, 0, Magick::MultiplyCompositeOp)
|
119
118
|
|
120
119
|
# Finally, place the star in the middle of the map, just onto the ground.
|
121
|
-
star = Magick::Image.read(
|
120
|
+
star = Magick::Image.read("media/large_star.png").first
|
122
121
|
star_y = 0
|
123
122
|
star_y += 20 until solid?(WIDTH / 2, star_y)
|
124
123
|
@image.composite!(star, (WIDTH - star.columns) / 2, star_y - star.rows * 0.85,
|
@@ -299,7 +298,7 @@ end
|
|
299
298
|
class Particle
|
300
299
|
def initialize(window, x, y)
|
301
300
|
# All Particle instances use the same image
|
302
|
-
@@image ||= Gosu::Image.new(
|
301
|
+
@@image ||= Gosu::Image.new("media/smoke.png")
|
303
302
|
|
304
303
|
@x, @y = x, y
|
305
304
|
@color = Gosu::Color.new(255, 255, 255, 255)
|
@@ -342,10 +341,10 @@ class RMagickIntegration < (Example rescue Gosu::Window)
|
|
342
341
|
@player_instructions << Gosu::Image.from_text(
|
343
342
|
"It is the #{ plr == 0 ? 'green' : 'red' } toy soldier's turn.\n" +
|
344
343
|
"(Arrow keys to walk and aim, Return to jump, Space to shoot)",
|
345
|
-
30, :
|
344
|
+
30, width: width, align: :center)
|
346
345
|
@player_won_messages << Gosu::Image.from_text(
|
347
346
|
"The #{ plr == 0 ? 'green' : 'red' } toy soldier has won!",
|
348
|
-
30, :
|
347
|
+
30, width: width, align: :center)
|
349
348
|
end
|
350
349
|
|
351
350
|
# Create everything!
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# This example demonstrates use of the TextInput class with three text field widgets.
|
2
|
+
# One can cycle through them with tab, or click into the text fields and change their contents.
|
3
|
+
|
4
|
+
# The way TextInput works is that you create an instance of it, and then assign it to the text_input
|
5
|
+
# attribute of your window.
|
6
|
+
# Until you set this attribute to nil again, the TextInput object will then build a string from user
|
7
|
+
# input that can be accessed via TextInput#text.
|
8
|
+
|
9
|
+
# The TextInput object also maintains the position of the caret, which is defined as the index of
|
10
|
+
# its right neighbour character, i.e. a carent_pos of 0 is always the left-most position, and a
|
11
|
+
# caret_pos of text.length is always the right-most position.
|
12
|
+
# There is a second attribute called selection_start that is equal to caret_pos when there is no
|
13
|
+
# selection, and otherwise defines the selected range. If you set caret_pos to a different value,
|
14
|
+
# you usually want to set selection_start as well.
|
15
|
+
|
16
|
+
# A TextInput object is purely abstract. Drawing the input field is left to the user.
|
17
|
+
# In this example, we are subclassing TextInput to add this code, but you can also work with
|
18
|
+
# composition instead of inheritance.
|
19
|
+
|
20
|
+
require "gosu"
|
21
|
+
|
22
|
+
class TextField < Gosu::TextInput
|
23
|
+
FONT = Gosu::Font.new(20)
|
24
|
+
WIDTH = 350
|
25
|
+
LENGTH_LIMIT = 20
|
26
|
+
PADDING = 5
|
27
|
+
|
28
|
+
INACTIVE_COLOR = 0xcc_666666
|
29
|
+
ACTIVE_COLOR = 0xcc_ff6666
|
30
|
+
SELECTION_COLOR = 0xcc_0000ff
|
31
|
+
CARET_COLOR = 0xff_ffffff
|
32
|
+
|
33
|
+
attr_reader :x, :y
|
34
|
+
|
35
|
+
def initialize(window, x, y)
|
36
|
+
# It's important to call the inherited constructor.
|
37
|
+
super()
|
38
|
+
|
39
|
+
@window, @x, @y = window, x, y
|
40
|
+
|
41
|
+
# Start with a self-explanatory text in each field.
|
42
|
+
self.text = "Click to edit"
|
43
|
+
end
|
44
|
+
|
45
|
+
# In this example, we use the filter method to prevent the user from entering a text that exceeds
|
46
|
+
# the length limit. However, you can also use this to blacklist certain characters, etc.
|
47
|
+
def filter new_text
|
48
|
+
allowed_length = [LENGTH_LIMIT - text.length, 0].max
|
49
|
+
new_text[0, allowed_length]
|
50
|
+
end
|
51
|
+
|
52
|
+
def draw(z)
|
53
|
+
# Change the background colour if this is the currently selected text field.
|
54
|
+
if @window.text_input == self
|
55
|
+
color = ACTIVE_COLOR
|
56
|
+
else
|
57
|
+
color = INACTIVE_COLOR
|
58
|
+
end
|
59
|
+
Gosu.draw_rect x - PADDING, y - PADDING, WIDTH + 2 * PADDING, height + 2 * PADDING, color, z
|
60
|
+
|
61
|
+
# Calculate the position of the caret and the selection start.
|
62
|
+
pos_x = x + FONT.text_width(self.text[0...self.caret_pos])
|
63
|
+
sel_x = x + FONT.text_width(self.text[0...self.selection_start])
|
64
|
+
sel_w = pos_x - sel_x
|
65
|
+
|
66
|
+
# Draw the selection background, if any. (If not, sel_x and pos_x will be
|
67
|
+
# the same value, making this a no-op call.)
|
68
|
+
Gosu.draw_rect sel_x, y, sel_w, height, SELECTION_COLOR, z
|
69
|
+
|
70
|
+
# Draw the caret if this is the currently selected field.
|
71
|
+
if @window.text_input == self
|
72
|
+
Gosu.draw_line pos_x, y, CARET_COLOR, pos_x, y + height, CARET_COLOR, z
|
73
|
+
end
|
74
|
+
|
75
|
+
# Finally, draw the text itself!
|
76
|
+
FONT.draw self.text, x, y, z
|
77
|
+
end
|
78
|
+
|
79
|
+
def height
|
80
|
+
FONT.height
|
81
|
+
end
|
82
|
+
|
83
|
+
# Hit-test for selecting a text field with the mouse.
|
84
|
+
def under_mouse?
|
85
|
+
@window.mouse_x > x - PADDING and @window.mouse_x < x + WIDTH + PADDING and
|
86
|
+
@window.mouse_y > y - PADDING and @window.mouse_y < y + height + PADDING
|
87
|
+
end
|
88
|
+
|
89
|
+
# Tries to move the caret to the position specifies by mouse_x
|
90
|
+
def move_caret_to_mouse
|
91
|
+
# Test character by character
|
92
|
+
1.upto(self.text.length) do |i|
|
93
|
+
if @window.mouse_x < x + FONT.text_width(text[0...i])
|
94
|
+
self.caret_pos = self.selection_start = i - 1;
|
95
|
+
return
|
96
|
+
end
|
97
|
+
end
|
98
|
+
# Default case: user must have clicked the right edge
|
99
|
+
self.caret_pos = self.selection_start = self.text.length
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class TextInputDemo < (Example rescue Gosu::Window)
|
104
|
+
def initialize
|
105
|
+
super 640, 480
|
106
|
+
self.caption = "Text Input Demo"
|
107
|
+
|
108
|
+
|
109
|
+
text =
|
110
|
+
"This demo explains (in the source code) how to use the Gosu::TextInput API by building a little TextField class around it.
|
111
|
+
|
112
|
+
Each text field can take up to 30 characters, and you can use Tab to switch between them.
|
113
|
+
|
114
|
+
As in every example, press <b>S</b> to look at the source code."
|
115
|
+
|
116
|
+
# Remove all leading spaces so the text is left-aligned
|
117
|
+
text.gsub! /^ +/, ""
|
118
|
+
|
119
|
+
@text = Gosu::Image.from_text text, 20, width: 540
|
120
|
+
|
121
|
+
# Set up an array of three text fields.
|
122
|
+
@text_fields = Array.new(3) { |index| TextField.new(self, 50, 300 + index * 50) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def needs_cursor?
|
126
|
+
true
|
127
|
+
end
|
128
|
+
|
129
|
+
def draw
|
130
|
+
@text.draw 50, 50, 0
|
131
|
+
@text_fields.each { |tf| tf.draw(0) }
|
132
|
+
end
|
133
|
+
|
134
|
+
def button_down(id)
|
135
|
+
if id == Gosu::KB_TAB
|
136
|
+
# Tab key will not be 'eaten' by text fields; use for switching through
|
137
|
+
# text fields.
|
138
|
+
index = @text_fields.index(self.text_input) || -1
|
139
|
+
self.text_input = @text_fields[(index + 1) % @text_fields.size]
|
140
|
+
elsif id == Gosu::KB_ESCAPE
|
141
|
+
# Escape key will not be 'eaten' by text fields; use for deselecting.
|
142
|
+
if self.text_input
|
143
|
+
self.text_input = nil
|
144
|
+
else
|
145
|
+
close
|
146
|
+
end
|
147
|
+
elsif id == Gosu::MS_LEFT
|
148
|
+
# Mouse click: Select text field based on mouse position.
|
149
|
+
self.text_input = @text_fields.find { |tf| tf.under_mouse? }
|
150
|
+
# Also move caret to clicked position
|
151
|
+
self.text_input.move_caret_to_mouse unless self.text_input.nil?
|
152
|
+
else
|
153
|
+
super
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
TextInputDemo.new.show if __FILE__ == $0
|
data/examples/tutorial.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# Encoding: UTF-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'gosu'
|
3
|
+
require "gosu"
|
5
4
|
|
6
5
|
module ZOrder
|
7
6
|
BACKGROUND, STARS, PLAYER, UI = *0..3
|
@@ -86,7 +85,7 @@ class Tutorial < (Example rescue Gosu::Window)
|
|
86
85
|
super 640, 480
|
87
86
|
self.caption = "Tutorial Game"
|
88
87
|
|
89
|
-
@background_image = Gosu::Image.new("media/space.png", :
|
88
|
+
@background_image = Gosu::Image.new("media/space.png", tileable: true)
|
90
89
|
|
91
90
|
@player = Player.new
|
92
91
|
@player.warp(320, 240)
|
data/examples/welcome.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# Encoding: UTF-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'gosu'
|
3
|
+
require "gosu"
|
5
4
|
|
6
5
|
WIDTH, HEIGHT = 640, 480
|
7
6
|
|
@@ -27,12 +26,12 @@ class Welcome < (Example rescue Gosu::Window)
|
|
27
26
|
• To quit this tool, press <b>Esc</b>.
|
28
27
|
• To toggle fullscreen mode, press <b>Alt+Enter</b> (Windows, Linux) or <b>cmd+F</b> (macOS).
|
29
28
|
|
30
|
-
Why not take a look at the code for this example right now? Simply press <b>
|
29
|
+
Why not take a look at the code for this example right now? Simply press <b>E</b>."
|
31
30
|
|
32
31
|
# Remove all leading spaces so the text is left-aligned
|
33
|
-
text.gsub! /^ +/,
|
32
|
+
text.gsub! /^ +/, ""
|
34
33
|
|
35
|
-
@text = Gosu::Image.from_text text, 20, :
|
34
|
+
@text = Gosu::Image.from_text text, 20, width: WIDTH - 2 * PADDING
|
36
35
|
|
37
36
|
@background = Gosu::Image.new "media/space.png"
|
38
37
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
class Example
|
2
2
|
attr_accessor :caption
|
3
3
|
attr_reader :width, :height
|
4
|
+
attr_writer :parent_window
|
4
5
|
|
5
6
|
def initialize(width, height, *options)
|
6
7
|
@width, @height = width, height
|
@@ -22,6 +23,22 @@ class Example
|
|
22
23
|
# no-op, examples cannot close the containing window.
|
23
24
|
end
|
24
25
|
|
26
|
+
def mouse_x
|
27
|
+
@parent_window && @parent_window.mouse_x
|
28
|
+
end
|
29
|
+
|
30
|
+
def mouse_y
|
31
|
+
@parent_window && @parent_window.mouse_y
|
32
|
+
end
|
33
|
+
|
34
|
+
def text_input
|
35
|
+
@parent_window && @parent_window.text_input
|
36
|
+
end
|
37
|
+
|
38
|
+
def text_input=(text_input)
|
39
|
+
@parent_window && @parent_window.text_input = text_input
|
40
|
+
end
|
41
|
+
|
25
42
|
def self.current_source_file
|
26
43
|
@current_source_file
|
27
44
|
end
|
@@ -43,6 +60,10 @@ class Example
|
|
43
60
|
@@examples[self]
|
44
61
|
end
|
45
62
|
|
63
|
+
def self.initial_example
|
64
|
+
@@examples.keys.find { |cls| cls.name.end_with? "::Welcome" }
|
65
|
+
end
|
66
|
+
|
46
67
|
def self.load_examples(pattern)
|
47
68
|
Dir.glob(pattern) do |file|
|
48
69
|
begin
|
@@ -2,7 +2,7 @@ class Sidebar
|
|
2
2
|
WIDTH = 300
|
3
3
|
HEIGHT = 600
|
4
4
|
FONT = Gosu::Font.new(20)
|
5
|
-
HEADER = Gosu::Image.new("media/header@2x.psd", :
|
5
|
+
HEADER = Gosu::Image.new("media/header@2x.psd", tileable: true)
|
6
6
|
|
7
7
|
class Button
|
8
8
|
HEIGHT = 25
|
@@ -19,7 +19,7 @@ class Sidebar
|
|
19
19
|
text_color = Gosu::Color::BLACK
|
20
20
|
|
21
21
|
if is_current
|
22
|
-
Gosu.draw_rect 0, @top, Sidebar::WIDTH, HEIGHT, 0xff_1565e5
|
22
|
+
Gosu.draw_rect 0, @top, Sidebar::WIDTH, HEIGHT, 0xff_1565e5
|
23
23
|
text_color = Gosu::Color::WHITE
|
24
24
|
end
|
25
25
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gosu-examples
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Raschke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gosu
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- examples/media/vera.ttf
|
82
82
|
- examples/opengl_integration.rb
|
83
83
|
- examples/rmagick_integration.rb
|
84
|
+
- examples/text_input.rb
|
84
85
|
- examples/tutorial.rb
|
85
86
|
- examples/welcome.rb
|
86
87
|
- lib/gosu-examples/example.rb
|
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
105
|
version: '0'
|
105
106
|
requirements: []
|
106
107
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.6
|
108
|
+
rubygems_version: 2.7.6
|
108
109
|
signing_key:
|
109
110
|
specification_version: 4
|
110
111
|
summary: Ruby examples for the Gosu library
|