home *** CD-ROM | disk | FTP | other *** search
- Controller subclass: #LampController
- instanceVariableNames: ''
- classVariableNames: ''
- poolDictionaries: ''
- category: 'Examples-Lamps'!
-
-
- !LampController methodsFor: 'processing'!
-
- processKeyboard
-
- | int |
- int := sensor keyboard digitValue.
- (int between: 1 and: model size)
- ifTrue: [(model at: int) state: 1].!
-
- processRedButton
- "This method is called when the red button is pressed."
-
- | mpt image box |
-
- "Wait for the mouse button to be released."
- sensor waitNoButton.
-
- "Get the point where the red mouse button was last pressed down."
- mpt := sensor lastDownPoint.
- "Assuming all lamp images are the same, get the first lamp image to use for
- computation"
- image := (model at: 1) getLampOffImage.
- "Now iterate through each lamp in the model or until we find one that has been
- clicked on."
- 1 to: (model size) do: [ :lampNumber | | lamp |
- lamp := (model at: lampNumber).
- "Compute the bounding box of this lamp's image in the view's coordinates."
- box := Rectangle origin: (lamp position) extent: (image extent).
- "Check if the pointer was on the image when the button was pressed."
- (box containsPoint: mpt) ifTrue: [ "If so, then turn that lamp on."
- ^lamp state: 1].
- ].!
-
- processYellowButton
-
- (Dialog confirm: 'Quit ?')
- ifTrue: [view window controller closeAndUnschedule].! !
-
- !LampController methodsFor: 'control defaults'!
-
- controlActivity
- "Do this when the mouse is in the window."
-
- (sensor keyboardPressed)
- ifTrue: [ self processKeyboard]
- ifFalse: [
- sensor yellowButtonPressed
- ifTrue: [ self processYellowButton].
- sensor redButtonPressed
- ifTrue: [ self processRedButton].
- ].! !
-
- Object subclass: #TrafficLight
- instanceVariableNames: 'lamplist lampView '
- classVariableNames: ''
- poolDictionaries: ''
- category: 'Examples-Lamps'!
-
-
- !TrafficLight methodsFor: 'initialization'!
-
- initialize
- "Creates the three lights and turns the first on"
-
- lamplist := LampList make:3.
- lampView := LampView openOn: lamplist.
- ^self.! !
-
- !TrafficLight methodsFor: 'destruction'!
-
- removeDependents
- "Removes the dependents of each lamp in the TrafficLight"
-
- lamplist do: [:lamp | lamp release ].! !
-
- !TrafficLight methodsFor: 'accessing'!
-
- changeLight
- "advances to the next light in the list"
- | index |
- Transcript show: 'Changing the Lights'; cr.
- index := (self lightIsOn) id.
- (lamplist at: (((index) rem: 3)) + 1) state: 1.
- lampView update: lamplist.!
-
- lamplist
- "returns the lamplist"
- ^lamplist.!
-
- lightIsOn
- "returns the index of the light that is on."
-
- ^(lamplist detect:
- [ :lamp | lamp state = 1]).!
-
- showStates
- lamplist do: [ :lamp| lamp showState].! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- TrafficLight class
- instanceVariableNames: ''!
-
-
- !TrafficLight class methodsFor: 'instance creation'!
-
- new
- "Creates a new instance and initializes the lights."
-
- ^(super new) initialize.! !
-
- Model variableSubclass: #LampList
- instanceVariableNames: 'numin theList '
- classVariableNames: ''
- poolDictionaries: ''
- category: 'Examples-Lamps'!
-
-
- !LampList methodsFor: 'initialization'!
-
- initialize
- "Sets the count to zero."
-
- theList := OrderedCollection new.
- numin := 0.! !
-
- !LampList methodsFor: 'updating'!
-
- update: signal
- "Waits for all lamps to report in, then redraws the view."
-
- numin := numin + 1.
- (numin = self size) ifTrue: [
- numin := 0.
- self changed.]! !
-
- !LampList methodsFor: 'accessing'!
-
- at: anInteger
- "returns a Lamp for theList."
- ^(theList at: anInteger).!
-
- detect: aBlock
- "Passes a detect message to theList"
- ^(theList detect: aBlock).!
-
- do: aBlock
- "Tells theList to do aBlock."
- ^(theList do: aBlock).!
-
- numin
- "Returns the count of how many updates from lamps have been received."
-
- ^numin!
-
- numin: anInteger
- "Sets the numin count to anInteger."
-
- numin := anInteger!
-
- size
- "Returns the size of theList."
-
- ^(theList size).! !
-
- !LampList methodsFor: 'adding'!
-
- add: aLamp
- "Adds aLamp to theList."
- theList add: aLamp.
- ^theList.!
-
- grow
- "Override the normal system grow such that instance variables are
- preserved."
-
- | savednumin |
-
- savednumin := self numin.
- super grow.
- self numin: savednumin.!
-
- growToAtLeast: anInteger
- "Override the normal system growToAtLeast such that instance variables are
- preserved."
-
- | savednumin |
-
- savednumin := self numin.
- super growToAtLeast: anInteger.
- self numin: savednumin.! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- LampList class
- instanceVariableNames: ''!
-
-
- !LampList class methodsFor: 'builder'!
-
- make: anInteger
- "Makes a lamp list with anInteger number of lamps input by the user."
-
- | lamplist lamp |
- lamplist := LampList new: anInteger.
- anInteger
- timesRepeat:
- [ lamp := Lamp new.
- lamp position: 25 @ (lamplist size * 30).
- lamp id: (lamplist size + 1).
- lamp state: 0.
- lamplist add: lamp.
- lamp addDependent: lamplist].
- 1 to: lamplist size do: [ :l |
- 1 to: lamplist size do: [ :dep |
- l = dep ifFalse: [
- (lamplist at: l) addDependent: (lamplist at: dep)]]].
- ^lamplist! !
-
- !LampList class methodsFor: 'instance creation'!
-
- new
- "Creates a new instance and initializes numin."
-
- ^super new initialize!
-
- new: size
- "Creates a new instance and initializes numin."
-
- ^(super new: size) initialize! !
-
- Model subclass: #Lamp
- instanceVariableNames: 'state position id '
- classVariableNames: 'LampOffImage LampOnImage '
- poolDictionaries: ''
- category: 'Examples-Lamps'!
-
-
- !Lamp methodsFor: 'accessing'!
-
- getLampOffImage
- "Returns the image of the lamp in off state."
-
- ^LampOffImage!
-
- getLampOnImage
- "Returns the image of the lamp in on state."
-
- ^LampOnImage!
-
- id
- "Gets the id of the lamp"
-
- ^id.!
-
- id: anInteger
- "Sets the id of the lamp"
-
- id := anInteger.!
-
- position
- "Returns the position of the lamp."
-
- ^position.!
-
- position: aPoint
- "Sets the position of the lamp to aPoint."
-
- position := aPoint.!
-
- showState
- Transcript show: 'Lamp '; show: (id printString); show: ' state: '; show: (state printString); cr.!
-
- state
- "Returns the state of the lamp: 0=off, 1=on."
-
- ^state.!
-
- state: anInteger
- "Sets the state of the lamp: 0=off, 1=on."
-
- anInteger isZero ifFalse: [ state := 1.
- self changed: #on]
- ifTrue: [ state := 0 ].! !
-
- !Lamp methodsFor: 'updating'!
-
- update: signal
- "If some other lamp has turned on, turn myself off."
-
- (signal = #on)
- ifTrue: [state := 0.
- self changed].! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- Lamp class
- instanceVariableNames: ''!
-
-
- !Lamp class methodsFor: 'instance creation'!
-
- new
- "Gets a new instance."
-
- ^super new! !
-
- !Lamp class methodsFor: 'initialize-release'!
-
- initialize
- "Initialize class with an image."
-
- | bitPattern |
-
- bitPattern := #[
- 2r00001111 2r11110000
- 2r00110000 2r00001100
- 2r01000000 2r00000010
- 2r10000000 2r00000001
- 2r10000000 2r00000001
- 2r10000000 2r00000001
- 2r10000000 2r00000001
- 2r10000000 2r00000001
- 2r10000000 2r00000001
- 2r01000000 2r00000010
- 2r00100000 2r00000100
- 2r00010000 2r00001000
- 2r00001000 2r00010000
- 2r00000100 2r00100000
- 2r00000100 2r00100000
- 2r00000010 2r01000000
- 2r00000010 2r01000000
- 2r00000010 2r01000000
- 2r00000010 2r01000000
- 2r00000010 2r01000000 ].
-
- LampOnImage := Image
- extent: 16@20
- depth: 1
- palette: MappedPalette blackWhite
- bits: bitPattern
- pad: 8.
-
- LampOffImage := Image
- extent: 16@20
- depth: 1
- palette: MappedPalette whiteBlack
- bits: bitPattern
- pad: 8.! !
-
- AutoScrollingView subclass: #LampView
- instanceVariableNames: 'window '
- classVariableNames: 'OpenFlag '
- poolDictionaries: ''
- category: 'Examples-Lamps'!
-
-
- !LampView methodsFor: 'displaying'!
-
- displayObject
- "Display the lamps in the window."
-
- | image lamp |
- 1 to: model size do:
- [ :index | lamp := model at: index.
- lamp state = 0 ifTrue: [ image := lamp getLampOffImage ]
- ifFalse: [ image := lamp getLampOnImage ].
-
- self graphicsContext displayImage: image at: lamp position.].!
-
- displayOn: ingored
- "Display the lamps in a window."
-
- self displayObject! !
-
- !LampView methodsFor: 'updating'!
-
- update: aModel
- "The receiver's model has changed. Redisplay the receiver."
-
- "(aModel = #done) ifTrue: [ self controller closeAndUnschedule ]
- ifFalse: [OpenFlag ifTrue: ["self clearInside.
- self displayObject"]]"! !
-
- !LampView methodsFor: 'controller access'!
-
- defaultControllerClass
- "Answer the class of the default controller for the receiver."
-
- ^LampController.! !
-
- !LampView methodsFor: 'accessing'!
-
- window
- "Returns the window that the view is displayed in."
-
- ^window!
-
- window: aWindow
- "Sets the window that the view is displayed in."
-
- window := aWindow! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- LampView class
- instanceVariableNames: ''!
-
-
- !LampView class methodsFor: 'subview creation'!
-
- getLampViewOn: aModel
- "Get a view for the lamps."
-
- | view |
- view := self new.
- view model: aModel.
- ^view! !
-
- !LampView class methodsFor: 'instance creation'!
-
- openOn: aLampList
- "Creates a new Lamp View on aLampList."
-
- | view window |
- OpenFlag := false.
- view := self new.
- view model: aLampList.
-
- window := ScheduledWindow new.
- window label: 'Lamp Viewer'.
- window minimumSize: 50@100.
- window insideColor: (ColorValue red: 1.0 green: 0.0 blue: 0.0 ).
- window component: view.
- view window: window.
- window open.
- ^view.! !
- Lamp initialize!
-
-
-