summaryrefslogtreecommitdiffhomepage
path: root/test/testcard.rb
blob: 21b627ba4f1ca42bef5889e449f8e9118e5528ee (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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(x: 0,   width: 50, color: [1, 0, 0, 1])
Rectangle.new(x: 50,  width: 50, color: [0, 1, 0, 1])
Rectangle.new(x: 100, width: 50, color: [0, 0, 1, 1])

# Color strings
Square.new(x: 150, size: 50, color: 'teal')
Square.new(x: 200, size: 50, color: 'gray')
Square.new(x: 250, size: 50, color: 'silver')
Square.new(x: 300, size: 50, color: 'white')
Rectangle.new(x: 350, width: 50, height: 50, color: 'navy')
Rectangle.new(x: 400, width: 50, height: 50, color: 'blue')
Rectangle.new(x: 450, width: 50, height: 50, color: 'aqua')
Rectangle.new(x: 500, width: 50, height: 50, color: 'teal')
Rectangle.new(x: 550, width: 50, height: 50, color: 'olive')
Rectangle.new(x: 150, y: 50, width: 50, height: 50, color: 'green')
Rectangle.new(x: 200, y: 50, width: 50, height: 50, color: 'lime')
Rectangle.new(x: 250, y: 50, width: 50, height: 50, color: 'yellow')
Rectangle.new(x: 300, y: 50, width: 50, height: 50, color: 'orange')
Rectangle.new(x: 350, y: 50, width: 50, height: 50, color: 'red')
Rectangle.new(x: 400, y: 50, width: 50, height: 50, color: 'maroon')
Rectangle.new(x: 450, y: 50, width: 50, height: 50, color: 'fuchsia')
Rectangle.new(x: 500, y: 50, width: 50, height: 50, color: 'purple')
Rectangle.new(x: 550, y: 50, width: 50, height: 50, color: 'brown')

# Mix of named colors and numbers
Rectangle.new(
  x: 600,
  y: 0,
  width: 50,
  height: 50,
  color: [
    [1.0, 0, 0, 1],
    'green',
    [0.0, 0, 1, 1.0],
    'yellow'
  ]
)

# Check opacity
opacity_square = Square.new(
  x: 650,
  y: 0,
  size: 50,
  color: ['red', 'green', 'blue', 'yellow']
)

# Fill remaining area with random colors
Rectangle.new(x: 600, y: 50, width: 50, height: 50, color: 'random')
Square.new(x: 650, y: 50, size: 50, color: 'random')

# White to black gradient
Rectangle.new(
  x: 0,
  y: 100,
  width: 700,
  height: 25,
  color: [
    [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(
  x: 0,
  y: 125,
  width: 700,
  height: 50,
  color: [
    [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(
  x: 0,
  y: 165,
  width: 700,
  height: 35,
  color: [
    [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(x1: 25,  y1: 200, x2: 50,  y2: 250, x3: 0,   y3: 250, color: [1.0,   0,   0, 1.0])
Triangle.new(x1: 75,  y1: 200, x2: 100, y2: 250, x3: 50,  y3: 250, color: [  0, 1.0,   0, 1.0])
Triangle.new(x1: 125, y1: 200, x2: 150, y2: 250, x3: 100, y3: 250, color: [  0,   0, 1.0, 1.0])
Triangle.new(x1: 175, y1: 200, x2: 200, y2: 250, x3: 150, y3: 250,
  color: [
    [1.0, 0, 0, 1.0],
    [0, 1.0, 0, 1.0],
    [0, 0, 1.0, 1.0]
  ]
)
Rectangle.new(
  x: 200,
  y: 200,
  width: 50,
  height: 50,
  color: [0.5, 0.5, 0.5, 1.0]
)  # add background for transparancy
Triangle.new(
  x1: 225,
  y1: 200,
  x2: 250,
  y2: 250,
  x3: 200,
  y3: 250,
  color: [
    [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(
  x1: 300, y1: 200,
  x2: 350, y2: 200,
  x3: 300, y3: 250,
  x4: 250, y4: 250,
  color: [
    [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(
  x1: 250, y1: 200,
  x2: 300, y2: 200,
  x3: 350, y3: 250,
  x4: 300, y4: 250,
  color: [
    [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]
  ]
)

# Lines
Line.new(
  x1: 354, y1: 204,
  x2: 397, y2: 247,
  width: 11,
  color: [
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]
  ]
);

Line.new(
  x1: 395, y1: 205,
  x2: 355, y2: 245,
  width: 15,
  color: [
    [1, 0, 0, 0.5],
    [0, 1, 0, 0.5],
    [0, 0, 1, 0.5],
    [1, 0, 1, 0.5]
  ]
);

# Circles

Circle.new(x: 525, y: 225, radius: 25, color: [1.0, 0.2, 0.2, 1.0])
Circle.new(x: 575, y: 225, radius: 25, sectors:  8, color: [0.2, 1.0, 0.2, 1.0])
Circle.new(x: 575, y: 225, radius: 17, sectors: 16, color: [0, 0, 0, 0.6])

rotate = false

# Images
img_png = Image.new("#{media}/image.png", x: 600, y: 180)
img_jpg = Image.new("#{media}/image.jpg", x: 600, y: 290)
img_bmp = Image.new("#{media}/image.bmp", x: 600, y: 400)
img_r = Image.new("#{media}/colors.png",  x: 400, y: 200, width: 50, height: 25)
img_r.color = [1.0, 0.3, 0.3, 1.0]
img_g = Image.new("#{media}/colors.png", x: 400, y: 225)
img_g.width, img_g.height = 25, 25
img_g.color = [0.3, 1.0, 0.3, 1.0]
img_b = Image.new("#{media}/colors.png", x: 425, y: 225)
img_b.width, img_b.height = 25, 25
img_b.color = [0.3, 0.3, 1.0, 1.0]

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

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

# Sprites
spr = Sprite.new(
  "#{media}/sprite_sheet.png",
  x: 450, y: 200,
  clip_width: 50,
  time: 500,
  loop: true
)
spr.play

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

time_start = Time.now

# Default font for text
Text.new("Default font", x: 150, y: 470, size: 20)

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

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

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

  if event.key == 'r'
    rotate = rotate ? false : true;
  end
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 rotate
    img_png.x = get :mouse_x
    img_png.y = get :mouse_y
    angle = (get :frames).to_f
    img_png.rotate = angle
    img_jpg.rotate = angle
    img_bmp.rotate = angle
    img_r.rotate = angle
    img_g.rotate = angle
    img_b.rotate = angle
    spr.rotate = angle
    txt_r.rotate = angle
    txt_g.rotate = angle
    txt_b.rotate = angle
    fps.rotate = angle
  end

  if flash > 0
    pointer_outline.color = [0, 1, 0, 1]
    flash -= 1
  else
    pointer_outline.color = [0, 1, 0, 0]
  end

  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