-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new sample code, ViewModelDemo, ported from RubyCocoa
git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@252 23306eb0-4c56-4727-a40e-e92c0eb68959
- Loading branch information
Showing
12 changed files
with
755 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
26 changes: 26 additions & 0 deletions
26
sample-macruby/ViewModelDemo/English.lproj/MainMenu.nib/classes.nib
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
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.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.