1 2require 'osx/cocoa' 3 4include OSX 5 6class MySuperview < NSView 7 8 ib_outlets :controller, :shadowSwitch, :moveToTopSwitch 9 10 attr_reader :shadowSwitch, :moveToTopSwitch 11 12 def initWithFrame (frame) 13 super_initWithFrame(frame) 14 return self 15 end 16 17 def awakeFromNib 18 @timer = nil 19 end 20 21 def drawRect(rect) 22 # Draw the background only since the ViewModel objects (subviews) will 23 # draw themselves automatically. 24 # An appropriate rect passed in to this method will keep objects from 25 # unnecessarily drawing themselves. 26 NSColor.whiteColor.set 27 NSBezierPath.fillRect(rect) 28 end 29 30 def shadowSwitchAction 31 setNeedsDisplay true 32 end 33 34 def mouseDown(event) 35 puts "MySuperview clicked." 36 end 37 38 def moveSubviewToTop(mySubview) 39 # Moves the given subview to the top, for drawing order purposes 40 addSubview_positioned_relativeTo_(mySubview, NSWindowBelow, subviews.lastObject) 41 end 42 43 def moveSubviewToIndex(mySubview, i) 44 # An index of 0 will move it behind all others 45 addSubview_positioned_relativeTo_(mySubview, NSWindowBelow, subviews.objectAtIndex(i)) 46 end 47 48 def startTimer 49 if @timer == nil 50 @timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_( 51 1.0/60.0, self, :timerAction, nil, true) 52 puts "startTimer" 53 end 54 end 55 56 def timerAction 57 numMoving = 0 58 subviews.each do |sv| 59 # The move function returns 0 if object is not moving, 1 otherwise 60 numMoving += sv.moveToDestination 61 end 62 63 if numMoving == 0 64 endTimer 65 $objMoving = false 66 return 67 end 68 69 setNeedsDisplay true 70 #puts "objs moving = #{numMoving}" 71 end 72 73 def endTimer 74 puts "endTimer" 75 @timer.invalidate 76 @timer = nil 77 end 78end 79