Skip to content

Commit

Permalink
new sample code, ViewModelDemo, ported from RubyCocoa
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@252 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
lrz committed Jun 6, 2008
1 parent b11a8a0 commit ebcb807
Show file tree
Hide file tree
Showing 12 changed files with 755 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sample-macruby/About MacRuby Examples.rtf
@@ -1,8 +1,8 @@
{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf270
{\rtf1\ansi\ansicpg1252\cocoartf949
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fnil\fcharset0 LucidaGrande;\f2\froman\fcharset0 Times-Roman;
}
{\colortbl;\red255\green255\blue255;}
\vieww18720\viewh20420\viewkind0
\vieww18720\viewh16000\viewkind0
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\li1120\fi-400\ri140\ql\qnatural

\f0\fs30 \cf0 \
Expand Down Expand Up @@ -41,13 +41,17 @@ AnimatingViews
\b PathDemo
\b0 Path operations (circles, rectangles, B\'e9zier paths)\
\

\b ViewModelDemo
\b0 NSView, NSTimer\
\
You will also find some standalone scripts in the
\b Scripts
\b0 directory. These \
files are meant to be run from the command line.\
\pard\tx960\tx4320\tx5760\tx6720\tx7680\tx8640\tx9600\li4300\fi-3360\ri-1180\ql\qnatural
\cf0 \
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\li960\fi-20\ri-1180\ql\qnatural
\cf0 The source code of all MacRuby examples is covered by the Ruby license, which can be found online at: {\field{\*\fldinst{HYPERLINK "http://www.ruby-lang.org/en/LICENSE.txt"}}{\fldrslt http://www.ruby-lang.org/en/LICENSE.txt}}\
\cf0 The source code of all MacRuby examples, unless specified, is covered by the Ruby license, which can be found online at: {\field{\*\fldinst{HYPERLINK "http://www.ruby-lang.org/en/LICENSE.txt"}}{\fldrslt http://www.ruby-lang.org/en/LICENSE.txt}}\
\
}
109 changes: 109 additions & 0 deletions sample-macruby/ViewModelDemo/Controller.rb
@@ -0,0 +1,109 @@
class Controller

ib_outlet :mySuperview, :numberText, :speedText, :numberSlider, :speedSlider

def init
super

@objs = [] # This will hold the ViewModel objects.

# A global flag to keep track of whether one of the subviews is moving.
# There is no particular reason why I used a global instead of a method call
# to mySuperview to retrieve an objMoving instance variable. A global
# just seems perfectly acceptable for this kind of thing.
$objMoving = false

self
end

def awakeFromNib
for n in 0..(@numberSlider.intValue-1)
addNewViewModel
end
end

def adjustNumberOfViewModels
desired = @numberSlider.intValue
current = @objs.length

if current < desired
for n in 1..(desired-current)
addNewViewModel
end
end

if current > desired
until @objs.length == desired
@objs.last.removeFromSuperview
@objs.pop
end
end
end

def addNewViewModel
myRect = getRandomViewRect
# Create a new ViewModel object, which is a subclass of NSView.
myObj = ViewModel.alloc.initWithFrame myRect
# Just for testing, the object knows its number.
myObj.setNum @objs.size
# Add it to the superview
@mySuperview.addSubview myObj
# Add it to the array
@objs.push myObj
end

ib_action :numberSliderAction do |sender|
number = @numberSlider.intValue
@numberText.stringValue = "Number: " + number.to_s
adjustNumberOfViewModels
end

ib_action :moveButtonAction do |sender|
# Assign new destinations to the objects
@objs.each do |obj|
destPt = getRandomViewPoint
obj.setMoveDestination destPt
speed = @speedSlider.floatValue
obj.setSpeed speed
obj.startMovement
end
end

ib_action :speedSliderAction do |sender|
# No need to store the speed in an instance variable, since the speed is read
# right from the control in moveButtonAction. Just update the speed text.
speed = @speedSlider.floatValue
@objs.each do |obj|
if obj.speed > 0 # object is moving
obj.setSpeed speed
end
end
speed = speed.to_i
@speedText.stringValue = "Speed: " + speed.to_s
end

def getRandomViewRect
rx = rand(@mySuperview.bounds.size.width - SQUARE_SIZE)
ry = rand(@mySuperview.bounds.size.height - SQUARE_SIZE)
NSRect.new(NSPoint.new(rx, ry), NSSize.new(SQUARE_SIZE, SQUARE_SIZE))
end

def getRandomViewPoint
rx = rand(@mySuperview.bounds.size.width - SQUARE_SIZE)
ry = rand(@mySuperview.bounds.size.height - SQUARE_SIZE)
NSPoint.new(rx, ry)
end

def handleEvents
app = NSApplication.sharedApplication
event = app.nextEventMatchingMask NSAnyEventMask,
untilDate:NSDate.dateWithTimeIntervalSinceNow(0.01),
inMode:OSX.NSDefaultRunLoopMode,
dequeue:true
if event
# Could put special event handling here.
app.sendEvent(event)
end
end

end
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions sample-macruby/ViewModelDemo/English.lproj/MainMenu.nib/info.nib

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
28 changes: 28 additions & 0 deletions sample-macruby/ViewModelDemo/Info.plist
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.ViewModelDemo</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
77 changes: 77 additions & 0 deletions sample-macruby/ViewModelDemo/Superview.rb
@@ -0,0 +1,77 @@

class MySuperview < NSView

ib_outlet :controller, :shadowSwitch, :moveToTopSwitch

attr_reader :shadowSwitch, :moveToTopSwitch

def initWithFrame(frame)
super
return self
end

def awakeFromNib
@timer = nil
end

def drawRect(rect)
# Draw the background only since the ViewModel objects (subviews) will
# draw themselves automatically.
# An appropriate rect passed in to this method will keep objects from
# unnecessarily drawing themselves.
NSColor.whiteColor.set
NSBezierPath.fillRect(rect)
end

def shadowSwitchAction(sender)
setNeedsDisplay true
end

def mouseDown(event)
puts "MySuperview clicked."
end

def moveSubviewToTop(mySubview)
# Moves the given subview to the top, for drawing order purposes
addSubview_positioned_relativeTo_(mySubview, NSWindowBelow, subviews.lastObject)
end

def moveSubviewToIndex(mySubview, i)
# An index of 0 will move it behind all others
addSubview_positioned_relativeTo_(mySubview, NSWindowBelow, subviews.objectAtIndex(i))
end

def startTimer
if @timer == nil
@timer = NSTimer.scheduledTimerWithTimeInterval 1.0/60.0,
target:self,
selector:'timerAction',
userInfo:nil,
repeats:true
puts "startTimer"
end
end

def timerAction
numMoving = 0
subviews.each do |sv|
# The move function returns 0 if object is not moving, 1 otherwise
numMoving += sv.moveToDestination
end

if numMoving == 0
endTimer
$objMoving = false
return
end

setNeedsDisplay true
end

def endTimer
puts "endTimer"
@timer.invalidate
@timer = nil
end

end

0 comments on commit ebcb807

Please sign in to comment.