'From Cuis 1.0 of 2 October 2009 [latest update: #316] on 12 November 2009 at 10:41:40 am'!

!LightWidget methodsFor: 'copying' stamp: 'jmv 11/11/2009 23:50'!
duplicate
	"Make and return a duplicate of the receiver"

	| newMorph |
	newMorph _ self copy.
	newMorph arrangeToStartStepping.
	newMorph privateOwner: nil. "no longer in world"

	^ newMorph! !

!LightWidget methodsFor: 'initialization' stamp: 'jmv 11/11/2009 23:49'!
openInWorld: aWorld
	aWorld addMorph: self.
	aWorld startSteppingSubmorphsOf: self! !

!LightWidget methodsFor: 'stepping and presenter' stamp: 'jmv 11/11/2009 23:51'!
arrangeToStartStepping
	"Arrange to start getting sent the 'step' message, but don't do that initial #step call that startStepping does"

	self arrangeToStartSteppingIn: self world! !

!LightWidget methodsFor: 'stepping and presenter' stamp: 'jmv 11/11/2009 23:51'!
arrangeToStartSteppingIn: aWorld
	"Start getting sent the 'step' message in aWorld.  Like startSteppingIn:, but without the initial one to get started'"
	aWorld ifNotNil: [
		aWorld startStepping: self.
		self changed]! !


!WorldState methodsFor: 'stepping' stamp: 'jmv 11/12/2009 10:41'!
adjustWakeupTimes: now
	"Fix the wakeup times in my step list. This is necessary when this world has been restarted after a pause, say because some other view had control, after a snapshot, or because the millisecond clock has wrapped around. (The latter is a rare occurence with a 32-bit clock!!)"
	"jmv: I say it is only needed on clock rollover. See senders of #adjustWakeupTimes:
	and the conditions under it is called"

	| deltaTime |
	deltaTime _ now - lastStepTime.
	stepList do: [ :entry | entry scheduledTime: entry scheduledTime + deltaTime ].
	lastStepTime _ now! !

!WorldState methodsFor: 'stepping' stamp: 'jmv 11/12/2009 10:40'!
adjustWakeupTimesIfNecessary
	"Fix the wakeup times in my step list if necessary. This is needed after a snapshot, after a long pause (say because some other view had control or because the user was selecting from an MVC-style menu) or when the millisecond clock wraps around (a very rare occurence with a 32-bit clock!!)."
	"jmv: I say it is only needed on clock rollover. See commented code below."

	| now |
	now _ Time millisecondClockValue.
	
	"((now < lastStepTime) or: [(now - lastStepTime) > 5000])"
	now < lastStepTime		"clock slipped"
		 ifTrue: [ self adjustWakeupTimes: now ]! !

!WorldState methodsFor: 'stepping' stamp: 'jmv 11/12/2009 10:40'!
runLocalStepMethodsIn: aWorld 
	"Run morph 'step' methods (LOCAL TO THIS WORLD) whose time has come. Purge any morphs that are no longer in this world."

	| now morphToStep stepTime priorWorld |
	now _ Time millisecondClockValue.
	priorWorld := ActiveWorld.
	ActiveWorld := aWorld.
	self triggerAlarmsBefore: now.
	stepList isEmpty 
		ifTrue: [
			ActiveWorld := priorWorld.
			^self].
	"jmv: I say it is only needed on clock rollover. See commented code below."
	"(now < lastStepTime or: [now - lastStepTime > 5000]) "
	now < lastStepTime		"clock slipped"
		ifTrue: [ self adjustWakeupTimes: now ].
	[ stepList isEmpty not and: [ stepList first scheduledTime < now ]] 
		whileTrue: [
			lastStepMessage _ stepList removeFirst.
			morphToStep _ lastStepMessage receiver.
			(morphToStep shouldGetStepsFrom: aWorld) 
				ifTrue: [
					lastStepMessage value: now.
					lastStepMessage ifNotNil: [
							stepTime _ lastStepMessage stepTime ifNil: [ morphToStep stepTime ].
							lastStepMessage scheduledTime: now + (stepTime max: 1).
							stepList add: lastStepMessage]].
			lastStepMessage _ nil].
	lastStepTime _ now.
	ActiveWorld _ priorWorld! !

!WorldState methodsFor: 'update cycle' stamp: 'jmv 11/11/2009 23:54'!
doOneCycleNowFor: aWorld
	"Immediately do one cycle of the interaction loop.
	This should not be called directly, but only via doOneCycleFor:"

	DisplayScreen checkForNewScreenSize.

	"process user input events"
	LastCycleTime _ Time millisecondClockValue.
	self handsDo: [ :h |
		ActiveHand _ h.
		h processEvents.
		ActiveHand _ nil
	].

	"the default is the primary hand"
	ActiveHand _ self hands first.

	aWorld runStepMethods.		"there are currently some variations here"
	self displayWorldSafely: aWorld
! !

