ruby2d 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,289 +0,0 @@
1
- # Web extension for Opal
2
-
3
- # Ruby 2D window
4
- $R2D_WINDOW = nil
5
-
6
- # Simple 2D window
7
- `
8
- var win;
9
-
10
- // ruby2d.js
11
-
12
- function on_key(e) {
13
-
14
- switch (e.type) {
15
- case S2D.KEY_DOWN:
16
- #{type = :down};
17
- break;
18
- case S2D.KEY_HELD:
19
- #{type = :held};
20
- break;
21
- case S2D.KEY_UP:
22
- #{type = :up};
23
- break;
24
- }
25
-
26
- #{$R2D_WINDOW.key_callback(type, `e.key`)};
27
- }
28
-
29
-
30
- function on_mouse(e) {
31
-
32
- #{direction = nil}
33
- #{button = nil}
34
-
35
- switch (e.type) {
36
- case S2D.MOUSE_DOWN:
37
- #{type = :down};
38
- break;
39
- case S2D.MOUSE_UP:
40
- #{type = :up};
41
- break;
42
- case S2D.MOUSE_SCROLL:
43
- #{type = :scroll};
44
- #{direction} = e.direction == S2D.MOUSE_SCROLL_NORMAL ? #{:normal} : #{:inverted};
45
- break;
46
- case S2D.MOUSE_MOVE:
47
- #{type = :move};
48
- break;
49
- }
50
-
51
- if (e.type == S2D.MOUSE_DOWN || e.type == S2D.MOUSE_UP) {
52
- switch (e.button) {
53
- case S2D.MOUSE_LEFT:
54
- #{button = :left};
55
- break;
56
- case S2D.MOUSE_MIDDLE:
57
- #{button = :middle};
58
- break;
59
- case S2D.MOUSE_RIGHT:
60
- #{button = :right};
61
- break;
62
- case S2D.MOUSE_X1:
63
- #{button = :x1};
64
- break;
65
- case S2D.MOUSE_X2:
66
- #{button = :x2};
67
- break;
68
- }
69
- }
70
-
71
- #{$R2D_WINDOW.mouse_callback(
72
- type, button, direction,
73
- `e.x`, `e.y`, `e.delta_x`, `e.delta_y`
74
- )};
75
- }
76
-
77
-
78
- function update() {
79
- #{$R2D_WINDOW.mouse_x = `win.mouse.x`};
80
- #{$R2D_WINDOW.mouse_y = `win.mouse.y`};
81
- #{$R2D_WINDOW.frames = `win.frames`};
82
- #{$R2D_WINDOW.fps = `win.fps`};
83
- #{$R2D_WINDOW.update_callback};
84
- }
85
-
86
-
87
- function render() {
88
-
89
- // Set background color
90
- win.background.r = #{$R2D_WINDOW.get(:background).r};
91
- win.background.g = #{$R2D_WINDOW.get(:background).g};
92
- win.background.b = #{$R2D_WINDOW.get(:background).b};
93
- win.background.a = #{$R2D_WINDOW.get(:background).a};
94
-
95
- var objects = #{$R2D_WINDOW.objects};
96
-
97
- for (var i = 0; i < objects.length; i++) {
98
- var el = objects[i];
99
- el['$ext_render']();
100
- }
101
- }
102
- `
103
-
104
-
105
- module Ruby2D
106
- class Triangle
107
- def ext_render
108
- `S2D.DrawTriangle(
109
- #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a,
110
- #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a,
111
- #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a
112
- );`
113
- end
114
- end
115
-
116
- class Quad
117
- def ext_render
118
- `S2D.DrawQuad(
119
- #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a,
120
- #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a,
121
- #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a,
122
- #{self}.x4, #{self}.y4, #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a
123
- );`
124
- end
125
- end
126
-
127
- class Line
128
- def ext_render
129
- `S2D.DrawLine(
130
- #{self}.x1, #{self}.y1, #{self}.x2, #{self}.y2, #{self}.width,
131
- #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a,
132
- #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a,
133
- #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a,
134
- #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a
135
- );`
136
- end
137
- end
138
-
139
- class Image
140
- def ext_init(path)
141
- `
142
- #{self}.data = S2D.CreateImage(path, function() {
143
- if (#{@width} == Opal.nil) {
144
- #{@width} = #{self}.data.width;
145
- }
146
- if (#{@height} == Opal.nil) {
147
- #{@height} = #{self}.data.height;
148
- }
149
- });
150
- `
151
- end
152
-
153
- def ext_render
154
- `
155
- #{self}.data.x = #{self}.x;
156
- #{self}.data.y = #{self}.y;
157
-
158
- if (#{self}.width != Opal.nil) #{self}.data.width = #{self}.width;
159
- if (#{self}.height != Opal.nil) #{self}.data.height = #{self}.height;
160
-
161
- #{self}.data.color.r = #{self}.color.r;
162
- #{self}.data.color.g = #{self}.color.g;
163
- #{self}.data.color.b = #{self}.color.b;
164
- #{self}.data.color.a = #{self}.color.a;
165
-
166
- S2D.DrawImage(#{self}.data);
167
- `
168
- end
169
- end
170
-
171
- class Sprite
172
- def ext_init(path)
173
- `#{self}.data = S2D.CreateSprite(path);`
174
- end
175
-
176
- def ext_render
177
- `
178
- #{self}.data.x = #{self}.x;
179
- #{self}.data.y = #{self}.y;
180
-
181
- S2D.ClipSprite(
182
- #{self}.data,
183
- #{self}.clip_x,
184
- #{self}.clip_y,
185
- #{self}.clip_w,
186
- #{self}.clip_h
187
- );
188
-
189
- S2D.DrawSprite(#{self}.data);
190
- `
191
- end
192
- end
193
-
194
- class Text
195
- def ext_init
196
- `
197
- #{self}.data = S2D.CreateText(#{self}.font, #{self}.text, #{self}.size);
198
- #{@width} = #{self}.data.width;
199
- #{@height} = #{self}.data.height;
200
- `
201
- end
202
-
203
- def ext_set(msg)
204
- `
205
- S2D.SetText(#{self}.data, #{msg});
206
- #{@width} = #{self}.data.width;
207
- #{@height} = #{self}.data.height;
208
- `
209
- end
210
-
211
- def ext_render
212
- `
213
- #{self}.data.x = #{self}.x;
214
- #{self}.data.y = #{self}.y;
215
-
216
- #{self}.data.color.r = #{self}.color.r;
217
- #{self}.data.color.g = #{self}.color.g;
218
- #{self}.data.color.b = #{self}.color.b;
219
- #{self}.data.color.a = #{self}.color.a;
220
-
221
- S2D.DrawText(#{self}.data);
222
- `
223
- end
224
- end
225
-
226
- class Sound
227
- def ext_init(path)
228
- `#{self}.data = S2D.CreateSound(path);`
229
- end
230
-
231
- def ext_play
232
- `S2D.PlaySound(#{self}.data);`
233
- end
234
- end
235
-
236
- class Music
237
- def ext_init(path)
238
- `#{self}.data = S2D.CreateMusic(path);`
239
- end
240
-
241
- def ext_play
242
- `S2D.PlayMusic(#{self}.data, #{self}.loop);`
243
- end
244
-
245
- def ext_pause
246
- `S2D.PauseMusic();`
247
- end
248
-
249
- def ext_resume
250
- `S2D.ResumeMusic();`
251
- end
252
-
253
- def ext_stop
254
- `S2D.StopMusic();`
255
- end
256
-
257
- def ext_music_fadeout(ms)
258
- `S2D.FadeOutMusic(ms);`
259
- end
260
- end
261
-
262
- class Window
263
- def ext_show
264
- $R2D_WINDOW = self
265
-
266
- `
267
- var width = #{$R2D_WINDOW.get(:width)};
268
- var height = #{$R2D_WINDOW.get(:height)};
269
-
270
- var vp_w = #{$R2D_WINDOW.get(:viewport_width)};
271
- var viewport_width = vp_w != Opal.nil ? vp_w : width;
272
-
273
- var vp_h = #{$R2D_WINDOW.get(:viewport_height)};
274
- var viewport_height = vp_h != Opal.nil ? vp_h : height;
275
-
276
- win = S2D.CreateWindow(
277
- #{$R2D_WINDOW.get(:title)}, width, height, update, render, "ruby2d-app", {}
278
- );
279
-
280
- win.viewport.width = viewport_width;
281
- win.viewport.height = viewport_height;
282
- win.on_key = on_key;
283
- win.on_mouse = on_mouse;
284
-
285
- S2D.Show(win);
286
- `
287
- end
288
- end
289
- end