A
download cocoa-notuse.rb
Language: Ruby
Copyright: (C) 2004-2005 Kouichirou Eto, All rights reserved.
LOC: 355
Project Info
etocom project(etocom)
Server: RubyForge (SVN)
Type: svn
...s\REL‑start_newgem\sgl\sgl\
   cocoa-app.rb
   cocoa-color.rb
   cocoa-draw.rb
   cocoa-event.rb
   cocoa-media.rb
   cocoa-notuse.rb
   cocoa-window.rb
   cocoa.rb
   make-release.rb
   opengl-app.rb
   opengl-color.rb
   opengl-draw.rb
   opengl-event.rb
   opengl-modules.rb
   opengl-window.rb
   opengl.rb
   qp.rb
   sgl-bass.rb
   sgl-button.rb
   sgl-spring.rb
   test-cocoa-app.rb
   test-opengl-app.rb

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# Copyright (C) 2004-2005 Kouichirou Eto, All rights reserved.
# These classes are not using.

module SGL
  class NuVideo
    include Singleton

    def initialize
      Thread.abort_on_exception = true
      @win = Window.alloc.init
    end
    attr_reader :win
  end

  class NuWindow < OSX::NSObject
    def init
      OSX.ruby_thread_switcher_start(0.001, 0.01)
      @app = OSX::NSApplication.sharedApplication
      @app.setMainMenu(OSX::NSMenu.alloc.init)
      @rgb = ColorTranslatorRGB.new(100, 100, 100, 100) # color translator
      @hsv = ColorTranslatorHSV.new(100, 100, 100, 100)
      @mouseX = @mouseY = @mouseDown = 0
      @keynum = 0
      $windowBorder = 1
      $windowShadow = 1
      $windowBackground = 1
      $windowDelay = 1.0/60
      $windowMovie = 0
      @window_initialized = false
      @setup_done = false
      @display_drawing = false

      @bgview = @oview = nil

      return self		# important for RubyCocoa
    end
    attr_reader :mouseX, :mouseY, :mouseDown, :keynum

    # generate window
    def window(w, h)
      return if @window_initialized
      s = OSX::NSScreen.mainScreen.frame.size
      x = (s.width  - w)/2.0
      y = (s.height - h)/2.0
      @win_frame = [x.to_f, y.to_f, w.to_f, h.to_f]
      window_open
    end

    def window_open
      style = OSX::NSClosableWindowMask
      style |= ($windowBorder && $windowBorder == 0) ?
      OSX::NSBorderlessWindowMask : OSX::NSTitledWindowMask
      @win = SglWindow.alloc.
	initWithContentRect(@win_frame,
			    :styleMask, style,
			    :backing, OSX::NSBackingStoreBuffered,
			    :defer, true)
      @win.setTitle("sgl")
      @bgview = BackView.alloc.init
      @bgview.myinit(self)
      @win.setContentView(@bgview)
      @bgcolor = @curcolor = nil
      background(100)	# white
      color(0)		# black
      @win.setDelegate(self)	# for handling windowShouldClose
      @win.setOpaque(false)	# can be transparent
      @win.setHasShadow(!($windowShadow && $windowShadow == 0))
      @win.setReleasedWhenClosed(false)
      @win.makeKeyAndOrderFront(self)
      @win.orderFrontRegardless	# show the window
      window_movie if $windowMovie == 1
      window_overlay if defined?(displayOverlay)
      @window_initialized = true
      @win
    end

    def window_movie
      o = @bgview.frame.origin
      so = @win.convertBaseToScreen([o.x, o.y])
      s = @bgview.frame.size
      @movwin = SglWindow.alloc.
	initWithContentRect([so.x, so.y, s.width, s.height],
			    :styleMask, OSX::NSBorderlessWindowMask,
			    :backing, OSX::NSBackingStoreBuffered,
			    :defer, true)
      @movwin.setOpaque(false)	# can be transparent
      @movwin.setHasShadow(false)
      @movwin.setIgnoresMouseEvents(true)
      @movwin.setAlphaValue(1.0)
      b = @bgview.bounds
      bo, bs = b.origin, b.size
      @movview = MovieView.alloc.initWithFrame([bo.x, bo.y, bs.width, bs.height])
      @movview.myinit(self)
      @movwin.contentView.addSubview(@movview)
      @movwin.orderFront(self)
      @win.addChildWindow(@movwin, :ordered, OSX::NSWindowAbove)
      @movview.setNeedsDisplay(true)
      @movewin
    end

    def window_overlay
      o = @bgview.frame.origin
      so = @win.convertBaseToScreen([o.x, o.y])
      s = @bgview.frame.size
      @owin = SglWindow.alloc.
	initWithContentRect([so.x, so.y, s.width, s.height],
			    :styleMask, OSX::NSBorderlessWindowMask,
			    :backing, OSX::NSBackingStoreBuffered,
			    :defer, true)
      @owin.setOpaque(false)	# can be transparent
      @owin.setHasShadow(false)
      @owin.setIgnoresMouseEvents(true)
      @owin.setAlphaValue(1.0)
      b = @bgview.bounds
      bo, bs = b.origin, b.size
      @oview = OverView.alloc.initWithFrame([bo.x, bo.y, bs.width, bs.height])
      @oview.myinit(self)
      @owin.contentView.addSubview(@oview)
      @owin.orderFront(self)
      if $windowMovie == 1
        @movwin.addChildWindow(@owin, :ordered, OSX::NSWindowAbove)
      else
        @win.addChildWindow(@owin, :ordered, OSX::NSWindowAbove)
      end
      @oview.setNeedsDisplay(true)
#      timer = OSX::NSTimer.
#	scheduledTimerWithTimeInterval(1.0/60,
#				       :target, self,
#				       :selector, "timerloop",
#				       :userInfo, nil,
#				       :repeats, true).retain
#      OSX::NSRunLoop.currentRunLoop.
#	addTimer(timer, :forMode, OSX::NSEventTrackingRunLoopMode)
      @owin
    end
    attr_reader :bgcolor, :curcolor

    def setup_post
      @setup_done = true
    end

    def windowShouldClose(sender)
      OSX.NSApp.stop(nil)
      false
    end

    def timerloop
      p "timerloop"
    end

    # main loop
    def mainloop
      @thread = Thread.start {
	loop {
	  setNeedsDisplay
	  delay
	}
      }
    end

    def setNeedsDisplay
      @bgview.setNeedsDisplay(true)
      @oview.setNeedsDisplay(true) if @oview
    end

    def delay
      sleep $windowDelay
    end

    def run
      @app.run
    end

    # handle events
    def winmouseDown
      @mouseDown = 1
      onMouseDown(@mouseX, @mouseY)
    end

    def winmouseUp
      @mouseDown = 0
      onMouseUp(@mouseX, @mouseY)
    end

    def winkeyDown(e)
      calc_keynum(e)
      p @keynum
      onKeyDown(@keynum)
    end

    def winkeyUp(e)
      calc_keynum(e)
      @keynum = 0
      p @keynum
      onKeyUp(@keynum)
    end

    def calc_keynum(e)
      input = event.characters
      @keynum = input.to_s[0]
    end

    # display
    def display_bg(rect)
      return if $windowBackground != 1
      color_set(*@bgcolor)
      OSX::NSRectFill(rect)
#      if @lastbgcolor && @lastbgcolor != @bgcolor
#      end
#      @lastbgcolor = @bgcolor.dup
    end

    def display_pre
      pos = @win.mouseLocationOutsideOfEventStream
      @mouseX, @mouseY = pos.x, pos.y
      color_set(*@curcolor)	# set back current color
    end

    def display_all(rect)
      return if !@setup_done
#     return if @display_drawing
      return if defined?(display_drawing) && display_drawing
      @display_drawing = true
      display_bg(rect)
      display_pre
      display
      display_post
      @display_drawing = false
    end

    def display_mov(rect)
      color_set(0, 0, 0, 0)	# full transparent
      OSX::NSRectFill(rect)
      color_set(*@curcolor)	# set back current color
    end

    def display_overlay_all(rect)
      return if !@setup_done
      return if @display_overlay_drawing
      @display_overlay_drawing = true
      color_set(0, 0, 0, 0)	# display_bg
      OSX::NSRectFill(rect)
      color_set(*@curcolor)	# set back current color
      displayOverlay
      @display_overlay_drawing = false
    end

    def display_post
    end

    # sgl commands
    def background(x, y=nil, z=nil, a=nil)
      @bgcolor = @rgb.norm(x,y,z,a)
    end

    def backgroundHSV(x, y=nil, z=nil, a=nil)
      @bgcolor = @hsv.norm(x,y,z,a)
    end

    def color(x, y=nil, z=nil, a=nil)
      @curcolor = @rgb.norm(x,y,z,a)
      color_set(*@curcolor)
    end

    def colorHSV(x,y=nil,z=nil,a=nil)
      @curcolor = @hsv.norm(x,y,z,a)
      color_set(*@curcolor)
    end

    def color_set(*a)
      color_make(*a).set if @window_initialized
    end

    def color_make(r, g, b, a)
      OSX::NSColor.colorWithDeviceRed(r,:green, g, :blue, b, :alpha, a)
    end

    def color_cur
      color_make(*@curcolor)
    end
  end

  class SglWindow < OSX::NSWindow
#    ns_overrides :keyDown_, :keyUp_
#    def keyDown(event)
#      p "ho"
#    end
  end

  class BackView < OSX::NSView
    ns_overrides :drawRect_,
      :mouseDown_, :mouseDragged_, :mouseUp_, :keyDown_, :keyUp_
    def myinit(win)	@win = win;	end
    def drawRect(rect)	@win.display_all(rect);	end
    def mouseDown(event)	@win.winmouseDown;	end
    def mouseDragged(event)	end
    def mouseUp(event)	@win.winmouseUp;	end
    def keyDown(event)	@win.winkeyDown(event);
      p "ho"
    end
    def keyUp(event)	@win.winkeyUp(event);	end
  end

  class OverView < OSX::NSView
    ns_overrides :drawRect_
    def myinit(win)	@win = win;	end
    def drawRect(rect)	@win.display_overlay_all(rect);	end
  end

  class MovieView < OSX::NSView
    ns_overrides :drawRect_
    def myinit(win)	@win = win;	end
    def drawRect(rect)	@win.display_mov(rect);	end
  end

  class NuWindow < OSX::NSObject
    def movie(u)
#     if %r$^http://$ =~ u || %r$^rtsp://$ =~ u
      if /\Ahttp:\/\// =~ u || /\Artsp:\/\// =~ u
	url = OSX::NSURL.URLWithString_(u)
      else
	url = OSX::NSURL.fileURLWithPath_(u)
      end
      mov = OSX::NSMovie.alloc.initWithURL(url, :byReference, true)
      # place it outer of the screen
      obj = SglMovieView.alloc.initWithFrame([-100.0, -100.0, 10.0, 10.0])
      obj.myinit(self)
      obj.setMovie(mov)
      obj.showController(false, :adjustingSize, false)
      if $windowMovie == 1
        @movview.addSubview(obj)
      else
        @bgview.addSubview(obj)
      end
#     @oview.addSubview(obj)
      obj
    end

    def image(file)
      img = SglImage.alloc.initWithContentsOfFile(file)
      img.myinit(self)
      img
    end

    def font(*a)
      SglFont.new(self, *a)
    end

    def sound(file)
      url = OSX::NSURL.fileURLWithPath_(file)
      snd = SglSound.alloc.initWithContentsOfURL(url, :byReference, true)
      snd
    end
  end

  class SglMovieView < OSX::NSMovieView
    def myinit(win)
      @win = win
    end

    def rect(a,b,c,d)
      frame(*to_xywh(a, b, c, d))
    end

    def frame(a,b,c,d)
      setFrame([a, b, c, d])
#      @win.switch_to_over
    end

    def play()	start_;		end
    def stop()	stop_;		end
    def goBegin()	gotoBeginning_;	end
    def goEnd()	gotoEnd_;	end
    def forward()	stepForward_;	end
    def back()	stepBack_;	end
    def loop=(a)	setLoopMode(a);	end
    def rate=(r)	setRate(r/100.0);	end
    def volume=(v)	setVolume(v/100.0);	end
  end

  class SglImage < OSX::NSImage
    def myinit(win)
      @win = win
    end

    def rect(a,b,c,d)	frame(*to_xywh(a, b, c, d));	end
    def frame(x,y,w,h)
      drawInRect([x,y,w,h],
		 :fromRect, [0,0,size.width,size.height],
		 :operation, OSX::NSCompositeSourceOver,
		 :fraction, @win.curcolor[3])
    end
  end

  class SglFont
    def initialize(w, n="Helvetica", s=0.0)
      @win, @name, @size = w, n, s.abs
    end
    attr_accessor :name
    attr_reader :size

    def size=(s)
      @size = s.abs
    end

    def to_sjis(str)
      NKF.nkf("-m0 -s", str)
    end

    def text(x, y, str)
      return unless str.is_a? String
      str = str.to_sjis
      str = to_sjis(str)
      str = OSX::NSMutableAttributedString.alloc.initWithString(str)
      str.addAttribute(OSX::NSFontAttributeName(),
		       :value, OSX::NSFont.fontWithName(@name, :size, @size),
		       :range, [0,str.length])
      str.addAttribute(OSX::NSForegroundColorAttributeName(),
		       :value, @win.color_cur,
		       :range, [0,str.length])
      str.drawAtPoint([x, y])
    end

    def show_fixed()	show(OSX::NSFixedPitchFontMask);	end
    def show_all()	show();	end

    private
    def show(mask=0)
      fmgr = OSX::NSFontManager.sharedFontManager
      fonts = fmgr.availableFontNamesWithTraits(mask).to_a.map{|i| i.to_s }.sort
      puts fonts
    end
  end

  class SglSound < OSX::NSSound
#    def play()	play_;	end
  end


=begin
  # sgl commands
  def point(a,b)
    line(a,b,a,b)
  end

  def lineWidth(w)
    OSX::NSBezierPath.setDefaultLineWidth(w)
  end

  def line(a,b,c,d)
    OSX::NSBezierPath.strokeLineFromPoint(OSX::NSPoint.new(a, b),
				:toPoint, OSX::NSPoint.new(c, d))
  end

  def rect(a,b,c,d)
    rect = OSX::NSRect.new(*to_xywh(a, b, c, d))
    OSX::NSBezierPath.bezierPathWithRect(rect).fill
  end

  def circle(x, y, r)
    rect = OSX::NSRect.new(x - r, y - r, 2*r, 2*r)
    OSX::NSBezierPath.bezierPathWithOvalInRect(rect).fill
  end

  def to_xywh(a,b,c,d)
    [[a, c].min,  [b, d].min, (a - c).abs, (b - d).abs]
  end

  def rotateZ(deg)
    af = OSX::NSAffineTransform.transform
    af.rotateByDegrees(deg)
    af.concat
  end

  def translate(x, y)
    af = OSX::NSAffineTransform.transform
    af.translateXBy(x, :yBy, y)
    af.concat
  end

  def scale(x, y=nil)
    af = OSX::NSAffineTransform.transform
    y ? af.scaleXBy(x, :yBy, y) : af.scaleBy(x)
    af.concat
  end

  def reset()
    OSX::NSAffineTransform.transform.set
  end
=end

end

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us