summaryrefslogtreecommitdiffhomepage
path: root/test/testcard.rb
blob: a7dc3085b520e87d68db51d3a1c0fec778a5d9c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require 'ruby2d'

if RUBY_ENGINE == 'opal'
  media = "../test/media"
  font = "sans-serif"
else
  media = "media"
  font = "#{media}/bitstream_vera/vera.ttf"
end

set diagnostics: true

set width: 700, height: 500, title: "Ruby 2D — Test Card"

# Read window attributes
puts "=== Window Attributes ===
Title: #{get :title}
Background: #{get :background}
Width:  #{get :width}
Height: #{get :height}
Window: #{get :window}\n\n"

# Primary colors
Rectangle.new(0, 0, 50, 100,   [1, 0, 0, 1])
Rectangle.new(50, 0, 50, 100,  [0, 1, 0, 1])
Rectangle.new(100, 0, 50, 100, [0, 0, 1, 1])

# Color strings
Square.new(   150, 0, 50,      'teal')
Square.new(   200, 0, 50,      'gray')
Square.new(   250, 0, 50,      'silver')
Square.new(   300, 0, 50,      'white')
Rectangle.new(350, 0, 50, 50,  'navy')
Rectangle.new(400, 0, 50, 50,  'blue')
Rectangle.new(450, 0, 50, 50,  'aqua')
Rectangle.new(500, 0, 50, 50,  'teal')
Rectangle.new(550, 0, 50, 50,  'olive')

Rectangle.new(150, 50, 50, 50, 'green')
Rectangle.new(200, 50, 50, 50, 'lime')
Rectangle.new(250, 50, 50, 50, 'yellow')
Rectangle.new(300, 50, 50, 50, 'orange')
Rectangle.new(350, 50, 50, 50, 'red')
Rectangle.new(400, 50, 50, 50, 'maroon')
Rectangle.new(450, 50, 50, 50, 'fuchsia')
Rectangle.new(500, 50, 50, 50, 'purple')
Rectangle.new(550, 50, 50, 50, 'brown')

# Mix of named colors and numbers
Rectangle.new(600, 0, 50, 50,
[
  'red',
  'green',
  'blue',
  'yellow'
])
Rectangle.new(650, 0, 50, 50,
[
  [1.0, 0, 0, 1],
  'green',
  [0.0, 0, 1, 1.0],
  'yellow'
])
Rectangle.new(600, 50, 50, 50, 'random')
Rectangle.new(650, 50, 50, 50, 'random')

# White to black gradient
Rectangle.new(0, 100, 700, 25,
[
  [1.0, 1.0, 1.0, 1.0],
  [0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0],
  [1.0, 1.0, 1.0, 1.0]
])

# Color gradient
Rectangle.new(0, 125, 700, 50,
[
  [1.0, 0.0, 0.0, 1.0],
  [0.0, 1.0, 0.0, 1.0],
  [0.0, 0.0, 1.0, 1.0],
  [1.0, 1.0, 0.0, 1.0]
])

# Transparancy
Rectangle.new(0, 165, 700, 35,
[
  [1.0, 1.0, 1.0, 0.0],
  [1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 0.0]
])

# Triangles
Triangle.new(25, 200, 50, 250, 0, 250,     [1.0,   0,   0, 1.0])
Triangle.new(75, 200, 100, 250, 50, 250,   [  0, 1.0,   0, 1.0])
Triangle.new(125, 200, 150, 250, 100, 250, [  0,   0, 1.0, 1.0])
Triangle.new(175, 200, 200, 250, 150, 250,
[
  [1.0, 0, 0, 1.0],
  [0, 1.0, 0, 1.0],
  [0, 0, 1.0, 1.0]
])
Rectangle.new(200, 200, 50, 50, [0.5, 0.5, 0.5, 1.0])  # add background for transparancy
Triangle.new(225, 200, 250, 250, 200, 250,
[
  [1.0, 1.0, 1.0, 1.0],
  [0.0, 0.0, 0.0, 1.0],
  [1.0, 1.0, 1.0, 0.0]
])

# Quadrilaterals
Quad.new(
  300, 200,
  350, 200,
  300, 250,
  250, 250,
  [
    [1.0, 0.0, 0.0, 1.0],
    [0.0, 1.0, 0.0, 1.0],
    [0.0, 0.0, 1.0, 1.0],
    [1.0, 1.0, 0.0, 1.0]
  ]
)

Quad.new(
  250, 200,
  300, 200,
  350, 250,
  300, 250,
  [
    [1.0, 1.0, 1.0, 0.0],
    [1.0, 1.0, 1.0, 0.0],
    [1.0, 1.0, 1.0, 1.0],
    [1.0, 1.0, 1.0, 0.0]
  ]
)

# Images
Image.new(590, 180, "#{media}/image.png")
Image.new(590, 290, "#{media}/image.jpg")
Image.new(590, 400, "#{media}/image.bmp")
img_r = Image.new(350, 200, "#{media}/colors.png")
img_r.width, img_r.height = 50, 50
img_r.color = [1.0, 0.3, 0.3, 1.0]
img_g = Image.new(400, 200, "#{media}/colors.png")
img_g.width, img_g.height = 50, 50
img_g.color = [0.3, 1.0, 0.3, 1.0]
img_b = Image.new(450, 200, "#{media}/colors.png")
img_b.width, img_b.height = 50, 50
img_b.color = [0.3, 0.3, 1.0, 1.0]

# Text
txt_r = Text.new( 44, 202, "R", 20, font)
txt_r.color = [1.0, 0.0, 0.0, 1.0]
txt_g = Text.new( 92, 202, "G", 20, font)
txt_g.color = [0.0, 1.0, 0.0, 1.0]
txt_b = Text.new(144, 202, "B", 20, font)
txt_b.color = [0.0, 0.0, 1.0, 1.0]

# Frames per second
fps = Text.new(10, 470, "", 20, font)

# Sprites
s1 = Sprite.new(500, 200, "#{media}/sprite_sheet.png")
s1.add(forwards: [
  [  0, 0, 50, 50, 30],
  [ 50, 0, 50, 50, 40],
  [100, 0, 50, 50, 50],
  [150, 0, 50, 50, 60]
])

# Pointer for mouse
pointer = Square.new(0, 0, 10, [1, 1, 1, 1])
pointer_outline = Square.new(0, 0, 18, [0, 1, 0, 0])
flash = 0

# Updating opacity
opacity_square = Square.new(500, 255, 50, ["red", "green", "blue", "yellow"])
time_start     = Time.now

# Text size
created_text = Text.new(10, 270, "Created text", 20, font)
created_text_background = Rectangle.new(
  created_text.x - 10,
  created_text.y - 10,
  created_text.width + 20,
  created_text.height + 20,
  "red"
)
created_text.remove
created_text.add

updated_text = Text.new(20 + created_text_background.x2, 270, "Updated text", 20, font)
updated_text_background = Rectangle.new(
  updated_text.x - 10,
  updated_text.y - 10,
  updated_text.width + 20,
  updated_text.height + 20,
  "blue"
)
updated_text.remove
updated_text.add
UPDATED_TEXT_OPTIONS = "of various size".split(" ")

on :key_down do |event|
  close if event.key == 'escape'
end

on :mouse_down do
  pointer_outline.x = (get :mouse_x) - 9
  pointer_outline.y = (get :mouse_y) - 11
  flash = 2
end

update do
  pointer.x = (get :mouse_x) - 5
  pointer.y = (get :mouse_y) - 7
  
  if flash > 0
    pointer_outline.color = [0, 1, 0, 1]
    flash -= 1
  else
    pointer_outline.color = [0, 1, 0, 0]
  end
  
  s1.animate(:forwards)
  
  if (get :frames) % 20 == 0
    fps.text = "FPS: #{(get :fps).round(3)}"
  end

  elapsed_time = Time.now - time_start
  opacity = Math.sin(3 * elapsed_time.to_f).abs
  opacity_square.color.opacity = opacity

  if (get :frames) % 60 == 0
    updated_text.text = "Updated text " + UPDATED_TEXT_OPTIONS[Time.now.to_i % UPDATED_TEXT_OPTIONS.length]
    updated_text_background.width = updated_text.width + 20
  end
end

show