sgl 0.3.1 → 0.4.0
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.
- data/ChangeLog +234 -0
- data/Manifest.txt +94 -0
- data/Rakefile +16 -5
- data/examples/cocoa1-basic.rb +14 -0
- data/examples/cocoa10-transparent.rb +26 -0
- data/examples/cocoa11-application.rb +13 -0
- data/examples/cocoa2-draw.rb +23 -0
- data/examples/cocoa3-affine.rb +29 -0
- data/examples/cocoa4-font.rb +24 -0
- data/examples/cocoa5-image.rb +22 -0
- data/examples/cocoa6-sound.rb +24 -0
- data/examples/cocoa7-movie.rb +40 -0
- data/examples/cocoa8-movieoverlay.rb +42 -0
- data/examples/cocoa9-streaming.rb +40 -0
- data/examples/opengl1-basic.rb +14 -0
- data/examples/opengl11-application.rb +13 -0
- data/examples/opengl2-draw.rb +23 -0
- data/examples/opengl3-affine.rb +29 -0
- data/examples/sample1.rb +11 -0
- data/examples/sample10.rb +16 -0
- data/examples/sample11.rb +22 -0
- data/examples/sample11a.rb +33 -0
- data/examples/sample12.rb +30 -0
- data/examples/sample12a.rb +35 -0
- data/examples/sample13.rb +116 -0
- data/examples/sample15.rb +24 -0
- data/examples/sample16.rb +31 -0
- data/examples/sample16a.rb +35 -0
- data/examples/sample6.rb +18 -0
- data/examples/sample7.rb +16 -0
- data/examples/sample9.rb +20 -0
- data/examples/sgl-sound1.rb +6 -0
- data/examples/sgl-sound2.rb +19 -0
- data/examples/sine.ogg +0 -0
- data/examples/testgl.rb +169 -0
- data/lib/sgl/bass-api.rb +202 -0
- data/lib/sgl/bass-sample.rb +106 -0
- data/lib/sgl/bass.rb +46 -0
- data/lib/sgl/cocoa-app.rb +43 -0
- data/lib/sgl/cocoa-color.rb +65 -0
- data/lib/sgl/cocoa-draw.rb +72 -0
- data/lib/sgl/cocoa-event.rb +229 -0
- data/lib/sgl/cocoa-media.rb +144 -0
- data/lib/sgl/cocoa-notuse.rb +493 -0
- data/lib/sgl/cocoa-window.rb +203 -0
- data/lib/sgl/cocoa.rb +8 -0
- data/lib/sgl/notuse-objs.rb +358 -0
- data/lib/sgl/opengl-app.rb +38 -0
- data/lib/sgl/opengl-color.rb +44 -0
- data/lib/sgl/opengl-draw.rb +260 -0
- data/lib/sgl/opengl-event.rb +325 -0
- data/lib/sgl/opengl-modules.rb +22 -0
- data/lib/sgl/opengl-window.rb +224 -0
- data/lib/sgl/opengl.rb +7 -0
- data/lib/sgl/sgl-basic.rb +61 -0
- data/lib/sgl/sgl-button.rb +135 -0
- data/lib/sgl/sgl-client.rb +21 -0
- data/lib/sgl/sgl-color.rb +82 -0
- data/lib/sgl/sgl-connect.rb +9 -0
- data/lib/sgl/sgl-objlib.rb +0 -0
- data/lib/sgl/sgl-objs.rb +0 -0
- data/lib/sgl/sgl-server.rb +58 -0
- data/lib/sgl/sgl-sound.rb +24 -0
- data/lib/sgl/sgl-spring.rb +216 -0
- data/lib/sgl/sgl-timer.rb +72 -0
- data/lib/sgl/sgl.rb +5 -0
- data/lib/sgl/version.rb +2 -2
- data/scripts/makemanifest.rb +20 -0
- data/test/test_bass.rb +27 -0
- data/test/test_cocoa_app.rb +5 -5
- data/test/test_helper.rb +9 -1
- data/test/test_module_ruby16.rb +22 -21
- data/test/test_opengl_app.rb +4 -5
- data/test/test_opengl_basic.rb +3 -4
- data/test/test_opengl_fullscreen.rb +4 -6
- data/test/test_opengl_novice.rb +3 -6
- data/test/test_sgl.rb +4 -27
- data/test/test_sound.rb +15 -0
- data/website/challenge1.html +91 -0
- data/website/challenge1.txt +30 -0
- data/website/challenge2.html +144 -0
- data/website/challenge2.txt +73 -0
- data/website/challenge3.html +63 -0
- data/website/challenge3.txt +15 -0
- data/website/cocoa.html +88 -0
- data/website/cocoa.txt +35 -0
- data/website/description.html +138 -0
- data/website/description.txt +90 -0
- data/website/exercise1.html +264 -0
- data/website/exercise1.txt +192 -0
- data/website/exercise2.html +683 -0
- data/website/exercise2.txt +597 -0
- data/website/exhibition.html +84 -0
- data/website/exhibition.txt +24 -0
- data/website/howto.html +134 -0
- data/website/howto.txt +56 -0
- data/website/index.html +17 -9
- data/website/index.txt +13 -4
- data/website/sound.html +141 -0
- data/website/sound.txt +87 -0
- data/website/stylesheets/grade.png +0 -0
- data/website/stylesheets/screen.css +21 -20
- metadata +109 -3
@@ -0,0 +1,493 @@
|
|
1
|
+
# Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
|
2
|
+
# License: Ruby License
|
3
|
+
# These classes are not using.
|
4
|
+
|
5
|
+
=begin
|
6
|
+
module SGL
|
7
|
+
class NuVideo
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
Thread.abort_on_exception = true
|
12
|
+
@win = Window.alloc.init
|
13
|
+
end
|
14
|
+
attr_reader :win
|
15
|
+
end
|
16
|
+
|
17
|
+
class NuWindow < OSX::NSObject
|
18
|
+
def init
|
19
|
+
OSX.ruby_thread_switcher_start(0.001, 0.01)
|
20
|
+
@app = OSX::NSApplication.sharedApplication
|
21
|
+
@app.setMainMenu(OSX::NSMenu.alloc.init)
|
22
|
+
@rgb = ColorTranslatorRGB.new(100, 100, 100, 100) # color translator
|
23
|
+
@hsv = ColorTranslatorHSV.new(100, 100, 100, 100)
|
24
|
+
@mouseX = @mouseY = @mouseDown = 0
|
25
|
+
@keynum = 0
|
26
|
+
$windowBorder = 1
|
27
|
+
$windowShadow = 1
|
28
|
+
$windowBackground = 1
|
29
|
+
$windowDelay = 1.0/60
|
30
|
+
$windowMovie = 0
|
31
|
+
@window_initialized = false
|
32
|
+
@setup_done = false
|
33
|
+
@display_drawing = false
|
34
|
+
|
35
|
+
@bgview = @oview = nil
|
36
|
+
|
37
|
+
return self # important for RubyCocoa
|
38
|
+
end
|
39
|
+
attr_reader :mouseX, :mouseY, :mouseDown, :keynum
|
40
|
+
|
41
|
+
# generate window
|
42
|
+
def window(w, h)
|
43
|
+
return if @window_initialized
|
44
|
+
s = OSX::NSScreen.mainScreen.frame.size
|
45
|
+
x = (s.width - w)/2.0
|
46
|
+
y = (s.height - h)/2.0
|
47
|
+
@win_frame = [x.to_f, y.to_f, w.to_f, h.to_f]
|
48
|
+
window_open
|
49
|
+
end
|
50
|
+
|
51
|
+
def window_open
|
52
|
+
style = OSX::NSClosableWindowMask
|
53
|
+
style |= ($windowBorder && $windowBorder == 0) ?
|
54
|
+
OSX::NSBorderlessWindowMask : OSX::NSTitledWindowMask
|
55
|
+
@win = SglWindow.alloc.
|
56
|
+
initWithContentRect(@win_frame,
|
57
|
+
:styleMask, style,
|
58
|
+
:backing, OSX::NSBackingStoreBuffered,
|
59
|
+
:defer, true)
|
60
|
+
@win.setTitle("sgl")
|
61
|
+
@bgview = BackView.alloc.init
|
62
|
+
@bgview.myinit(self)
|
63
|
+
@win.setContentView(@bgview)
|
64
|
+
@bgcolor = @curcolor = nil
|
65
|
+
background(100) # white
|
66
|
+
color(0) # black
|
67
|
+
@win.setDelegate(self) # for handling windowShouldClose
|
68
|
+
@win.setOpaque(false) # can be transparent
|
69
|
+
@win.setHasShadow(!($windowShadow && $windowShadow == 0))
|
70
|
+
@win.setReleasedWhenClosed(false)
|
71
|
+
@win.makeKeyAndOrderFront(self)
|
72
|
+
@win.orderFrontRegardless # show the window
|
73
|
+
window_movie if $windowMovie == 1
|
74
|
+
window_overlay if defined?(displayOverlay)
|
75
|
+
@window_initialized = true
|
76
|
+
@win
|
77
|
+
end
|
78
|
+
|
79
|
+
def window_movie
|
80
|
+
o = @bgview.frame.origin
|
81
|
+
so = @win.convertBaseToScreen([o.x, o.y])
|
82
|
+
s = @bgview.frame.size
|
83
|
+
@movwin = SglWindow.alloc.
|
84
|
+
initWithContentRect([so.x, so.y, s.width, s.height],
|
85
|
+
:styleMask, OSX::NSBorderlessWindowMask,
|
86
|
+
:backing, OSX::NSBackingStoreBuffered,
|
87
|
+
:defer, true)
|
88
|
+
@movwin.setOpaque(false) # can be transparent
|
89
|
+
@movwin.setHasShadow(false)
|
90
|
+
@movwin.setIgnoresMouseEvents(true)
|
91
|
+
@movwin.setAlphaValue(1.0)
|
92
|
+
b = @bgview.bounds
|
93
|
+
bo, bs = b.origin, b.size
|
94
|
+
@movview = MovieView.alloc.initWithFrame([bo.x, bo.y, bs.width, bs.height])
|
95
|
+
@movview.myinit(self)
|
96
|
+
@movwin.contentView.addSubview(@movview)
|
97
|
+
@movwin.orderFront(self)
|
98
|
+
@win.addChildWindow(@movwin, :ordered, OSX::NSWindowAbove)
|
99
|
+
@movview.setNeedsDisplay(true)
|
100
|
+
@movewin
|
101
|
+
end
|
102
|
+
|
103
|
+
def window_overlay
|
104
|
+
o = @bgview.frame.origin
|
105
|
+
so = @win.convertBaseToScreen([o.x, o.y])
|
106
|
+
s = @bgview.frame.size
|
107
|
+
@owin = SglWindow.alloc.
|
108
|
+
initWithContentRect([so.x, so.y, s.width, s.height],
|
109
|
+
:styleMask, OSX::NSBorderlessWindowMask,
|
110
|
+
:backing, OSX::NSBackingStoreBuffered,
|
111
|
+
:defer, true)
|
112
|
+
@owin.setOpaque(false) # can be transparent
|
113
|
+
@owin.setHasShadow(false)
|
114
|
+
@owin.setIgnoresMouseEvents(true)
|
115
|
+
@owin.setAlphaValue(1.0)
|
116
|
+
b = @bgview.bounds
|
117
|
+
bo, bs = b.origin, b.size
|
118
|
+
@oview = OverView.alloc.initWithFrame([bo.x, bo.y, bs.width, bs.height])
|
119
|
+
@oview.myinit(self)
|
120
|
+
@owin.contentView.addSubview(@oview)
|
121
|
+
@owin.orderFront(self)
|
122
|
+
if $windowMovie == 1
|
123
|
+
@movwin.addChildWindow(@owin, :ordered, OSX::NSWindowAbove)
|
124
|
+
else
|
125
|
+
@win.addChildWindow(@owin, :ordered, OSX::NSWindowAbove)
|
126
|
+
end
|
127
|
+
@oview.setNeedsDisplay(true)
|
128
|
+
# timer = OSX::NSTimer.
|
129
|
+
# scheduledTimerWithTimeInterval(1.0/60,
|
130
|
+
# :target, self,
|
131
|
+
# :selector, "timerloop",
|
132
|
+
# :userInfo, nil,
|
133
|
+
# :repeats, true).retain
|
134
|
+
# OSX::NSRunLoop.currentRunLoop.
|
135
|
+
# addTimer(timer, :forMode, OSX::NSEventTrackingRunLoopMode)
|
136
|
+
@owin
|
137
|
+
end
|
138
|
+
attr_reader :bgcolor, :curcolor
|
139
|
+
|
140
|
+
def setup_post
|
141
|
+
@setup_done = true
|
142
|
+
end
|
143
|
+
|
144
|
+
def windowShouldClose(sender)
|
145
|
+
OSX.NSApp.stop(nil)
|
146
|
+
false
|
147
|
+
end
|
148
|
+
|
149
|
+
def timerloop
|
150
|
+
p "timerloop"
|
151
|
+
end
|
152
|
+
|
153
|
+
# main loop
|
154
|
+
def mainloop
|
155
|
+
@thread = Thread.start {
|
156
|
+
loop {
|
157
|
+
setNeedsDisplay
|
158
|
+
delay
|
159
|
+
}
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
def setNeedsDisplay
|
164
|
+
@bgview.setNeedsDisplay(true)
|
165
|
+
@oview.setNeedsDisplay(true) if @oview
|
166
|
+
end
|
167
|
+
|
168
|
+
def delay
|
169
|
+
sleep $windowDelay
|
170
|
+
end
|
171
|
+
|
172
|
+
def run
|
173
|
+
@app.run
|
174
|
+
end
|
175
|
+
|
176
|
+
# handle events
|
177
|
+
def winmouseDown
|
178
|
+
@mouseDown = 1
|
179
|
+
onMouseDown(@mouseX, @mouseY)
|
180
|
+
end
|
181
|
+
|
182
|
+
def winmouseUp
|
183
|
+
@mouseDown = 0
|
184
|
+
onMouseUp(@mouseX, @mouseY)
|
185
|
+
end
|
186
|
+
|
187
|
+
def winkeyDown(e)
|
188
|
+
calc_keynum(e)
|
189
|
+
p @keynum
|
190
|
+
onKeyDown(@keynum)
|
191
|
+
end
|
192
|
+
|
193
|
+
def winkeyUp(e)
|
194
|
+
calc_keynum(e)
|
195
|
+
@keynum = 0
|
196
|
+
p @keynum
|
197
|
+
onKeyUp(@keynum)
|
198
|
+
end
|
199
|
+
|
200
|
+
def calc_keynum(e)
|
201
|
+
input = event.characters
|
202
|
+
@keynum = input.to_s[0]
|
203
|
+
end
|
204
|
+
|
205
|
+
# display
|
206
|
+
def display_bg(rect)
|
207
|
+
return if $windowBackground != 1
|
208
|
+
color_set(*@bgcolor)
|
209
|
+
OSX::NSRectFill(rect)
|
210
|
+
# if @lastbgcolor && @lastbgcolor != @bgcolor
|
211
|
+
# end
|
212
|
+
# @lastbgcolor = @bgcolor.dup
|
213
|
+
end
|
214
|
+
|
215
|
+
def display_pre
|
216
|
+
pos = @win.mouseLocationOutsideOfEventStream
|
217
|
+
@mouseX, @mouseY = pos.x, pos.y
|
218
|
+
color_set(*@curcolor) # set back current color
|
219
|
+
end
|
220
|
+
|
221
|
+
def display_all(rect)
|
222
|
+
return if !@setup_done
|
223
|
+
# return if @display_drawing
|
224
|
+
return if defined?(display_drawing) && display_drawing
|
225
|
+
@display_drawing = true
|
226
|
+
display_bg(rect)
|
227
|
+
display_pre
|
228
|
+
display
|
229
|
+
display_post
|
230
|
+
@display_drawing = false
|
231
|
+
end
|
232
|
+
|
233
|
+
def display_mov(rect)
|
234
|
+
color_set(0, 0, 0, 0) # full transparent
|
235
|
+
OSX::NSRectFill(rect)
|
236
|
+
color_set(*@curcolor) # set back current color
|
237
|
+
end
|
238
|
+
|
239
|
+
def display_overlay_all(rect)
|
240
|
+
return if !@setup_done
|
241
|
+
return if @display_overlay_drawing
|
242
|
+
@display_overlay_drawing = true
|
243
|
+
color_set(0, 0, 0, 0) # display_bg
|
244
|
+
OSX::NSRectFill(rect)
|
245
|
+
color_set(*@curcolor) # set back current color
|
246
|
+
displayOverlay
|
247
|
+
@display_overlay_drawing = false
|
248
|
+
end
|
249
|
+
|
250
|
+
def display_post
|
251
|
+
end
|
252
|
+
|
253
|
+
# sgl commands
|
254
|
+
def background(x, y=nil, z=nil, a=nil)
|
255
|
+
@bgcolor = @rgb.norm(x,y,z,a)
|
256
|
+
end
|
257
|
+
|
258
|
+
def backgroundHSV(x, y=nil, z=nil, a=nil)
|
259
|
+
@bgcolor = @hsv.norm(x,y,z,a)
|
260
|
+
end
|
261
|
+
|
262
|
+
def color(x, y=nil, z=nil, a=nil)
|
263
|
+
@curcolor = @rgb.norm(x,y,z,a)
|
264
|
+
color_set(*@curcolor)
|
265
|
+
end
|
266
|
+
|
267
|
+
def colorHSV(x,y=nil,z=nil,a=nil)
|
268
|
+
@curcolor = @hsv.norm(x,y,z,a)
|
269
|
+
color_set(*@curcolor)
|
270
|
+
end
|
271
|
+
|
272
|
+
def color_set(*a)
|
273
|
+
color_make(*a).set if @window_initialized
|
274
|
+
end
|
275
|
+
|
276
|
+
def color_make(r, g, b, a)
|
277
|
+
OSX::NSColor.colorWithDeviceRed(r,:green, g, :blue, b, :alpha, a)
|
278
|
+
end
|
279
|
+
|
280
|
+
def color_cur
|
281
|
+
color_make(*@curcolor)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
class SglWindow < OSX::NSWindow
|
286
|
+
# ns_overrides :keyDown_, :keyUp_
|
287
|
+
# def keyDown(event)
|
288
|
+
# p "ho"
|
289
|
+
# end
|
290
|
+
end
|
291
|
+
|
292
|
+
class BackView < OSX::NSView
|
293
|
+
ns_overrides :drawRect_,
|
294
|
+
:mouseDown_, :mouseDragged_, :mouseUp_, :keyDown_, :keyUp_
|
295
|
+
def myinit(win) @win = win; end
|
296
|
+
def drawRect(rect) @win.display_all(rect); end
|
297
|
+
def mouseDown(event) @win.winmouseDown; end
|
298
|
+
def mouseDragged(event) end
|
299
|
+
def mouseUp(event) @win.winmouseUp; end
|
300
|
+
def keyDown(event) @win.winkeyDown(event);
|
301
|
+
p "ho"
|
302
|
+
end
|
303
|
+
def keyUp(event) @win.winkeyUp(event); end
|
304
|
+
end
|
305
|
+
|
306
|
+
class OverView < OSX::NSView
|
307
|
+
ns_overrides :drawRect_
|
308
|
+
def myinit(win) @win = win; end
|
309
|
+
def drawRect(rect) @win.display_overlay_all(rect); end
|
310
|
+
end
|
311
|
+
|
312
|
+
class MovieView < OSX::NSView
|
313
|
+
ns_overrides :drawRect_
|
314
|
+
def myinit(win) @win = win; end
|
315
|
+
def drawRect(rect) @win.display_mov(rect); end
|
316
|
+
end
|
317
|
+
|
318
|
+
class NuWindow < OSX::NSObject
|
319
|
+
def movie(u)
|
320
|
+
# if %r$^http://$ =~ u || %r$^rtsp://$ =~ u
|
321
|
+
if /\Ahttp:\/\// =~ u || /\Artsp:\/\// =~ u
|
322
|
+
url = OSX::NSURL.URLWithString_(u)
|
323
|
+
else
|
324
|
+
url = OSX::NSURL.fileURLWithPath_(u)
|
325
|
+
end
|
326
|
+
mov = OSX::NSMovie.alloc.initWithURL(url, :byReference, true)
|
327
|
+
# place it outer of the screen
|
328
|
+
obj = SglMovieView.alloc.initWithFrame([-100.0, -100.0, 10.0, 10.0])
|
329
|
+
obj.myinit(self)
|
330
|
+
obj.setMovie(mov)
|
331
|
+
obj.showController(false, :adjustingSize, false)
|
332
|
+
if $windowMovie == 1
|
333
|
+
@movview.addSubview(obj)
|
334
|
+
else
|
335
|
+
@bgview.addSubview(obj)
|
336
|
+
end
|
337
|
+
# @oview.addSubview(obj)
|
338
|
+
obj
|
339
|
+
end
|
340
|
+
|
341
|
+
def image(file)
|
342
|
+
img = SglImage.alloc.initWithContentsOfFile(file)
|
343
|
+
img.myinit(self)
|
344
|
+
img
|
345
|
+
end
|
346
|
+
|
347
|
+
def font(*a)
|
348
|
+
SglFont.new(self, *a)
|
349
|
+
end
|
350
|
+
|
351
|
+
def sound(file)
|
352
|
+
url = OSX::NSURL.fileURLWithPath_(file)
|
353
|
+
snd = SglSound.alloc.initWithContentsOfURL(url, :byReference, true)
|
354
|
+
snd
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
class SglMovieView < OSX::NSMovieView
|
359
|
+
def myinit(win)
|
360
|
+
@win = win
|
361
|
+
end
|
362
|
+
|
363
|
+
def rect(a,b,c,d)
|
364
|
+
frame(*to_xywh(a, b, c, d))
|
365
|
+
end
|
366
|
+
|
367
|
+
def frame(a,b,c,d)
|
368
|
+
setFrame([a, b, c, d])
|
369
|
+
# @win.switch_to_over
|
370
|
+
end
|
371
|
+
|
372
|
+
def play() start_; end
|
373
|
+
def stop() stop_; end
|
374
|
+
def goBegin() gotoBeginning_; end
|
375
|
+
def goEnd() gotoEnd_; end
|
376
|
+
def forward() stepForward_; end
|
377
|
+
def back() stepBack_; end
|
378
|
+
def loop=(a) setLoopMode(a); end
|
379
|
+
def rate=(r) setRate(r/100.0); end
|
380
|
+
def volume=(v) setVolume(v/100.0); end
|
381
|
+
end
|
382
|
+
|
383
|
+
class SglImage < OSX::NSImage
|
384
|
+
def myinit(win)
|
385
|
+
@win = win
|
386
|
+
end
|
387
|
+
|
388
|
+
def rect(a,b,c,d) frame(*to_xywh(a, b, c, d)); end
|
389
|
+
def frame(x,y,w,h)
|
390
|
+
drawInRect([x,y,w,h],
|
391
|
+
:fromRect, [0,0,size.width,size.height],
|
392
|
+
:operation, OSX::NSCompositeSourceOver,
|
393
|
+
:fraction, @win.curcolor[3])
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
class SglFont
|
398
|
+
def initialize(w, n="Helvetica", s=0.0)
|
399
|
+
@win, @name, @size = w, n, s.abs
|
400
|
+
end
|
401
|
+
attr_accessor :name
|
402
|
+
attr_reader :size
|
403
|
+
|
404
|
+
def size=(s)
|
405
|
+
@size = s.abs
|
406
|
+
end
|
407
|
+
|
408
|
+
def to_sjis(str)
|
409
|
+
NKF.nkf("-m0 -s", str)
|
410
|
+
end
|
411
|
+
|
412
|
+
def text(x, y, str)
|
413
|
+
return unless str.is_a? String
|
414
|
+
str = str.to_sjis
|
415
|
+
str = to_sjis(str)
|
416
|
+
str = OSX::NSMutableAttributedString.alloc.initWithString(str)
|
417
|
+
str.addAttribute(OSX::NSFontAttributeName(),
|
418
|
+
:value, OSX::NSFont.fontWithName(@name, :size, @size),
|
419
|
+
:range, [0,str.length])
|
420
|
+
str.addAttribute(OSX::NSForegroundColorAttributeName(),
|
421
|
+
:value, @win.color_cur,
|
422
|
+
:range, [0,str.length])
|
423
|
+
str.drawAtPoint([x, y])
|
424
|
+
end
|
425
|
+
|
426
|
+
def show_fixed() show(OSX::NSFixedPitchFontMask); end
|
427
|
+
def show_all() show(); end
|
428
|
+
|
429
|
+
private
|
430
|
+
def show(mask=0)
|
431
|
+
fmgr = OSX::NSFontManager.sharedFontManager
|
432
|
+
fonts = fmgr.availableFontNamesWithTraits(mask).to_a.map{|i| i.to_s }.sort
|
433
|
+
puts fonts
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
class SglSound < OSX::NSSound
|
438
|
+
# def play() play_; end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
=end
|
442
|
+
|
443
|
+
=begin
|
444
|
+
# sgl commands
|
445
|
+
def point(a,b)
|
446
|
+
line(a,b,a,b)
|
447
|
+
end
|
448
|
+
|
449
|
+
def lineWidth(w)
|
450
|
+
OSX::NSBezierPath.setDefaultLineWidth(w)
|
451
|
+
end
|
452
|
+
|
453
|
+
def line(a,b,c,d)
|
454
|
+
OSX::NSBezierPath.strokeLineFromPoint(OSX::NSPoint.new(a, b),
|
455
|
+
:toPoint, OSX::NSPoint.new(c, d))
|
456
|
+
end
|
457
|
+
|
458
|
+
def rect(a,b,c,d)
|
459
|
+
rect = OSX::NSRect.new(*to_xywh(a, b, c, d))
|
460
|
+
OSX::NSBezierPath.bezierPathWithRect(rect).fill
|
461
|
+
end
|
462
|
+
|
463
|
+
def circle(x, y, r)
|
464
|
+
rect = OSX::NSRect.new(x - r, y - r, 2*r, 2*r)
|
465
|
+
OSX::NSBezierPath.bezierPathWithOvalInRect(rect).fill
|
466
|
+
end
|
467
|
+
|
468
|
+
def to_xywh(a,b,c,d)
|
469
|
+
[[a, c].min, [b, d].min, (a - c).abs, (b - d).abs]
|
470
|
+
end
|
471
|
+
|
472
|
+
def rotateZ(deg)
|
473
|
+
af = OSX::NSAffineTransform.transform
|
474
|
+
af.rotateByDegrees(deg)
|
475
|
+
af.concat
|
476
|
+
end
|
477
|
+
|
478
|
+
def translate(x, y)
|
479
|
+
af = OSX::NSAffineTransform.transform
|
480
|
+
af.translateXBy(x, :yBy, y)
|
481
|
+
af.concat
|
482
|
+
end
|
483
|
+
|
484
|
+
def scale(x, y=nil)
|
485
|
+
af = OSX::NSAffineTransform.transform
|
486
|
+
y ? af.scaleXBy(x, :yBy, y) : af.scaleBy(x)
|
487
|
+
af.concat
|
488
|
+
end
|
489
|
+
|
490
|
+
def reset()
|
491
|
+
OSX::NSAffineTransform.transform.set
|
492
|
+
end
|
493
|
+
=end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
# Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
|
2
|
+
# License: Ruby License
|
3
|
+
|
4
|
+
module SGL
|
5
|
+
# window functions
|
6
|
+
def window(*a) $__a__.window(*a); end
|
7
|
+
def close_window() $__a__.close_window; end
|
8
|
+
def width() $__a__.width; end
|
9
|
+
def height() $__a__.height; end
|
10
|
+
|
11
|
+
class Application
|
12
|
+
def initialize_window
|
13
|
+
@options = default_options
|
14
|
+
@win = @bgview = nil
|
15
|
+
@width = @height = nil
|
16
|
+
@oview = @movview = nil
|
17
|
+
@thread = nil
|
18
|
+
end
|
19
|
+
private :initialize_window
|
20
|
+
|
21
|
+
def default_options
|
22
|
+
{
|
23
|
+
:shadow=>true,
|
24
|
+
:border=>true,
|
25
|
+
:movie=>false,
|
26
|
+
:overlay=>false,
|
27
|
+
}
|
28
|
+
end
|
29
|
+
private :default_options
|
30
|
+
|
31
|
+
# create window
|
32
|
+
def window(*a)
|
33
|
+
return if @win
|
34
|
+
|
35
|
+
@options.update(a.pop) if a.last.is_a? Hash
|
36
|
+
|
37
|
+
if defined?($windowShadow)
|
38
|
+
@options[:shadow] = $windowShadow == 1
|
39
|
+
end
|
40
|
+
|
41
|
+
if defined?($windowBorder)
|
42
|
+
@options[:border] = $windowBorder == 1
|
43
|
+
end
|
44
|
+
|
45
|
+
if @block[:display_overlay]
|
46
|
+
@options[:overlay] = true
|
47
|
+
end
|
48
|
+
|
49
|
+
# get window size
|
50
|
+
case a.length
|
51
|
+
when 2
|
52
|
+
w, h = a
|
53
|
+
when 4
|
54
|
+
raise "not implemented" # x1, y1, x2, y3 = a
|
55
|
+
else
|
56
|
+
raise "please specify width and height"
|
57
|
+
end
|
58
|
+
@width, @height = w, h
|
59
|
+
|
60
|
+
cocoa_create_window(w, h)
|
61
|
+
end
|
62
|
+
|
63
|
+
def close_window
|
64
|
+
@win.close if @win
|
65
|
+
@win = @bgview = nil
|
66
|
+
end
|
67
|
+
|
68
|
+
attr_reader :width, :height
|
69
|
+
|
70
|
+
def cocoa_create_window(w, h)
|
71
|
+
@receiver = CocoaReceiver.alloc.init
|
72
|
+
@receiver.setApp(self)
|
73
|
+
|
74
|
+
# create a window
|
75
|
+
s = OSX::NSScreen.mainScreen.frame.size
|
76
|
+
x = (s.width - w) / 2.0
|
77
|
+
y = (s.height - h) / 2.0
|
78
|
+
win_frame = [x.to_f, y.to_f, w.to_f, h.to_f]
|
79
|
+
style = OSX::NSClosableWindowMask
|
80
|
+
style |= @options[:border] ? OSX::NSTitledWindowMask :
|
81
|
+
OSX::NSBorderlessWindowMask
|
82
|
+
win = NSWindow.alloc.
|
83
|
+
initWithContentRect(win_frame,
|
84
|
+
:styleMask, style,
|
85
|
+
:backing, OSX::NSBackingStoreBuffered,
|
86
|
+
:defer, true)
|
87
|
+
win.setTitle("sgl")
|
88
|
+
|
89
|
+
# create a view
|
90
|
+
view = SglNSView.alloc.init
|
91
|
+
view.setApp(self)
|
92
|
+
win.setContentView(view)
|
93
|
+
background(100) # white
|
94
|
+
color(0) # black
|
95
|
+
|
96
|
+
# for handling windowShouldClose
|
97
|
+
win.setDelegate(@receiver)
|
98
|
+
win.setOpaque(false) # can be transparent
|
99
|
+
win.setHasShadow(@options[:shadow])
|
100
|
+
win.setReleasedWhenClosed(false)
|
101
|
+
win.makeKeyAndOrderFront(@receiver)
|
102
|
+
win.orderFrontRegardless # show the window now
|
103
|
+
@win = win
|
104
|
+
@bgview = view
|
105
|
+
window_movie(@options) if @options[:movie]
|
106
|
+
#window_overlay(@options) if defined?(displayOverlay)
|
107
|
+
window_overlay(@options) if @options[:overlay]
|
108
|
+
@win
|
109
|
+
end
|
110
|
+
|
111
|
+
# sub view
|
112
|
+
def window_movie(options)
|
113
|
+
@movwin, @movview = make_view(@win, @bgview, NSViewForMovie, @options)
|
114
|
+
@win.addChildWindow(@movwin, :ordered, OSX::NSWindowAbove)
|
115
|
+
@movview.setNeedsDisplay(true)
|
116
|
+
@movwin
|
117
|
+
end
|
118
|
+
|
119
|
+
def window_overlay(options)
|
120
|
+
@owin, @oview = make_view(@win, @bgview, NSViewForOverlay, options)
|
121
|
+
win = options[:movie] ? @movwin : @win
|
122
|
+
win.addChildWindow(@owin, :ordered, OSX::NSWindowAbove)
|
123
|
+
@oview.setNeedsDisplay(true)
|
124
|
+
@owin
|
125
|
+
end
|
126
|
+
|
127
|
+
def make_view(parent_win, parent_view, viewclass, options)
|
128
|
+
o = parent_view.frame.origin
|
129
|
+
so = parent_win.convertBaseToScreen([o.x, o.y])
|
130
|
+
s = parent_view.frame.size
|
131
|
+
win_frame = [so.x, so.y, s.width, s.height]
|
132
|
+
win = NSWindow.alloc.
|
133
|
+
initWithContentRect(win_frame,
|
134
|
+
:styleMask, OSX::NSBorderlessWindowMask,
|
135
|
+
:backing, OSX::NSBackingStoreBuffered,
|
136
|
+
:defer, true)
|
137
|
+
win.setOpaque(false) # can be transparent
|
138
|
+
win.setHasShadow(false)
|
139
|
+
win.setIgnoresMouseEvents(true)
|
140
|
+
win.setAlphaValue(1.0) # transparent
|
141
|
+
b = parent_view.bounds
|
142
|
+
bo, bs = b.origin, b.size
|
143
|
+
frame = [bo.x, bo.y, bs.width, bs.height]
|
144
|
+
view = viewclass.alloc.initWithFrame(frame)
|
145
|
+
view.setApp(self)
|
146
|
+
win.contentView.addSubview(view)
|
147
|
+
win.orderFront(@receiver)
|
148
|
+
return win, view
|
149
|
+
end
|
150
|
+
private :window_movie, :window_overlay, :make_view
|
151
|
+
|
152
|
+
# Cocoa callback methods
|
153
|
+
def window_should_close
|
154
|
+
stop
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
class CocoaReceiver < OSX::NSObject
|
159
|
+
def init
|
160
|
+
@app = nil
|
161
|
+
return self
|
162
|
+
end
|
163
|
+
|
164
|
+
def setApp(app)
|
165
|
+
@app = app
|
166
|
+
end
|
167
|
+
|
168
|
+
# Cocoa callback methods
|
169
|
+
def windowShouldClose(sender)
|
170
|
+
@app.window_should_close
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
class NSWindow < OSX::NSWindow
|
175
|
+
end
|
176
|
+
|
177
|
+
# Do not use NSView for class name.
|
178
|
+
# It causes Illeagal Instruction Error.
|
179
|
+
# I don't know why.
|
180
|
+
class SglNSView < OSX::NSView
|
181
|
+
ns_overrides :drawRect_,
|
182
|
+
:mouseDown_, :mouseDragged_, :mouseUp_, :keyDown_, :keyUp_
|
183
|
+
def setApp(app) @app = app; end
|
184
|
+
def drawRect(rect) @app.display_all(rect); end
|
185
|
+
def mouseDown(event) @app.do_mousedown; end
|
186
|
+
def mouseDragged(event) end # ignore
|
187
|
+
def mouseUp(event) @app.do_mouseup; end
|
188
|
+
def keyDown(event) @app.do_keydown(event); end
|
189
|
+
def keyUp(event) @app.do_keyup(event); end
|
190
|
+
end
|
191
|
+
|
192
|
+
class NSViewForOverlay < OSX::NSView
|
193
|
+
ns_overrides :drawRect_
|
194
|
+
def setApp(app) @app = app; end
|
195
|
+
def drawRect(rect) @app.display_overlay_all(rect); end
|
196
|
+
end
|
197
|
+
|
198
|
+
class NSViewForMovie < OSX::NSView
|
199
|
+
ns_overrides :drawRect_
|
200
|
+
def setApp(app) @app = app; end
|
201
|
+
def drawRect(rect) @app.display_mov(rect); end
|
202
|
+
end
|
203
|
+
end
|