"Bezier Curve Laboratory checkpoint 3. File in this complete source, then evaluate LBZApp open."! Object subclass: #LBZModel instanceVariableNames: 'points parameter samples' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Bezier'! Morph subclass: #LBZApp instanceVariableNames: 'model window buttons sliders handles selected running lastTick clockWorld' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Bezier'! SystemWindow subclass: #LBZWindow instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Bezier'! !LBZModel methodsFor: 'tutorial'! initialize super initialize. points := {60@280. 540@80}. parameter := 1/2. samples := 48 ! ! !LBZModel methodsFor: 'tutorial'! points ^ points collect: [:point | point copy] ! ! !LBZModel methodsFor: 'tutorial'! validPoint: point ^ point isPoint and: [point x isNumber and: [point y isNumber and: [ (point x between: 0 and: 600) and: [point y between: 0 and: 360]]]] ! ! !LBZModel methodsFor: 'tutorial'! setPoints: values (values isArray and: [(values size between: 2 and: 4) and: [values allSatisfy: [:point | self validPoint: point]]]) ifFalse: [^ false]. points := values collect: [:point | point copy]. ^ true ! ! !LBZModel methodsFor: 'tutorial'! parameter ^ parameter ! ! !LBZModel methodsFor: 'tutorial'! parameter: number (number isNumber and: [number between: 0 and: 1]) ifFalse: [^ false]. parameter := number. ^ true ! ! !LBZModel methodsFor: 'tutorial'! interpolate: start to: finish at: fraction ^ start * (1 - fraction) + (finish * fraction) ! ! !LBZModel methodsFor: 'tutorial'! pointAt: fraction ^ (self levelsAt: fraction) last first ! ! !LBZModel methodsFor: 'tutorial'! levelsAt: fraction | levels row | levels := OrderedCollection new. row := self points. levels add: row. [row size > 1] whileTrue: [ row := (1 to: row size - 1) collect: [:index | self interpolate: (row at: index) to: (row at: index + 1) at: fraction]. levels add: row]. ^ levels asArray ! ! !LBZModel methodsFor: 'tutorial'! quadraticAt: fraction | first second | first := self interpolate: points first to: points second at: fraction. second := self interpolate: points second to: points third at: fraction. ^ self interpolate: first to: second at: fraction ! ! !LBZApp methodsFor: 'tutorial'! initialize super initialize. self extent: 980@700; color: (Color fromString: '#F6F4EF'). model := LBZModel new. buttons := Dictionary new. sliders := Dictionary new. handles := OrderedCollection new. selected := 1. running := false. lastTick := nil. self stage = 2 ifTrue: [model setPoints: {60@280. 300@30. 540@280}]. self stage >= 3 ifTrue: [model setPoints: {60@280. 140@30. 460@30. 540@280}]. self buildControls ! ! !LBZApp methodsFor: 'tutorial'! model ^ model ! ! !LBZApp methodsFor: 'tutorial'! window ^ window ! ! !LBZApp methodsFor: 'tutorial'! running ^ running ! ! !LBZApp methodsFor: 'tutorial'! selected ^ selected ! ! !LBZApp methodsFor: 'tutorial'! plotOrigin ^ self position + (35@140) ! ! !LBZApp methodsFor: 'tutorial'! plotExtent ^ 600@360 ! ! !LBZApp class methodsFor: 'tutorial'! open ^ self new open ! ! !LBZApp methodsFor: 'tutorial'! open window ifNotNil: [window comeToFront. ^ self]. window := LBZWindow new model: self; setLabel: 'Bezier Curve Laboratory - LearnSqueak'. window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window fixedExtent: 1004@735. window openInWorld. self refresh. ^ self ! ! !LBZApp methodsFor: 'tutorial'! okToClose ^ true ! ! !LBZApp methodsFor: 'tutorial'! release self pause ! ! !LBZApp methodsFor: 'tutorial'! windowIsClosing self pause. window := nil ! ! !LBZApp methodsFor: 'tutorial'! pause running := false. lastTick := nil. self cancelDrags. clockWorld ifNotNil: [clockWorld stopStepping: self]. clockWorld := nil. self stopStepping. self changed ! ! !LBZApp methodsFor: 'tutorial'! wantsSteps ^ false ! ! !LBZWindow methodsFor: 'tutorial'! fixedExtent: point fixedExtent := point. super extent: point ! ! !LBZWindow methodsFor: 'tutorial'! extent: point super extent: (fixedExtent ifNil: [point]) ! ! !LBZWindow methodsFor: 'tutorial'! passivate model ifNotNil: [model pause]. super passivate ! ! !LBZWindow methodsFor: 'tutorial'! lookUnfocused model ifNotNil: [model pause]. super lookUnfocused ! ! !LBZWindow methodsFor: 'tutorial'! collapse model ifNotNil: [model pause]. super collapse ! ! !LBZWindow methodsFor: 'tutorial'! delete model ifNotNil: [model pause]. super delete ! ! !LBZApp methodsFor: 'tutorial'! buttonNamed: label ^ buttons at: label ! ! !LBZApp methodsFor: 'tutorial'! sliderAt: key ^ sliders at: key ! ! !LBZApp methodsFor: 'tutorial'! button: label at: point action: selector args: arguments | button | button := SimpleButtonMorph new label: label; target: self; actionSelector: selector; arguments: arguments; color: Color white; yourself. button position: self position + point. self addMorph: button. buttons at: label put: button. ^ button ! ! !LBZApp methodsFor: 'tutorial'! slider: key at: point max: maximum action: selector | slider | slider := SimpleSliderMorph new extent: 250@18; minVal: 0; maxVal: maximum; target: self; actionSelector: selector; arguments: #(); yourself. slider color: (Color fromString: '#D8DEDF'); sliderColor: (Color fromString: '#366B83'). slider position: self position + point. self addMorph: slider. sliders at: key put: slider. ^ slider ! ! !LBZApp methodsFor: 'tutorial'! buildControls self stage >= 4 ifTrue: [ self slider: #t at: 688@185 max: 100 action: #percent:. #(0 25 50 75 100) withIndexDo: [:value :index | self button: value printString, '%' at: (688 + ((index - 1) * 51))@218 action: #percent: args: {value}]]. self stage >= 5 ifTrue: [ model points withIndexDo: [:point :index | | handle | handle := LBZHandle new app: self index: index. handles add: handle. self addMorph: handle. self button: 'P', (index - 1) printString at: (688 + ((index - 1) * 62))@295 action: #select: args: {index}]. self slider: #x at: 688@359 max: 600 action: #xValue:. self slider: #y at: 688@410 max: 360 action: #yValue:]. self stage >= 6 ifTrue: [#(12 48 120) withIndexDo: [:count :index | self button: count printString, ' segments' at: (35 + ((index - 1) * 117))@570 action: #sampleCount: args: {count}]]. self stage >= 7 ifTrue: [ self button: 'Play' at: 688@476 action: #play args: #(). self button: 'Pause' at: 758@476 action: #pause args: #()]. self stage >= 8 ifTrue: [ self button: 'Arch' at: 688@543 action: #preset: args: {#arch}. self button: 'Loop' at: 756@543 action: #preset: args: {#loop}. self button: 'Line' at: 824@543 action: #preset: args: {#line}. self button: 'Reset' at: 688@584 action: #reset args: #()] ! ! !LBZApp methodsFor: 'tutorial'! refresh self stage >= 4 ifTrue: [(sliders at: #t) adjustToValue: model parameter * 100]. self stage >= 5 ifTrue: [ handles withIndexDo: [:handle :index | handle center: (self plotOrigin + (model points at: index)) rounded. handle color: (index = selected ifTrue: [Color fromString: '#D26845'] ifFalse: [Color fromString: '#314F63'])]. (sliders at: #x) adjustToValue: (model points at: selected) x. (sliders at: #y) adjustToValue: (model points at: selected) y]. self changed ! ! !LBZApp methodsFor: 'tutorial'! cancelDrags handles ifNotNil: [handles do: [:handle | handle cancelDrag]] ! ! !LBZApp methodsFor: 'tutorial'! label: text at: point size: size on: canvas canvas drawString: text at: self position + point font: (TextStyle default fontOfSize: size) color: (Color fromString: '#213F53') ! ! !LBZApp methodsFor: 'tutorial'! polyline: values color: color width: width on: canvas 1 to: values size - 1 do: [:index | canvas line: (self plotOrigin + (values at: index)) rounded to: (self plotOrigin + (values at: index + 1)) rounded width: width color: color] ! ! !LBZApp methodsFor: 'tutorial'! dot: point radius: radius color: color on: canvas canvas fillOval: (((self plotOrigin + point) rounded - (radius@radius)) extent: (radius * 2)@(radius * 2)) color: color ! ! !LBZApp methodsFor: 'tutorial'! drawOn: canvas | levels palette result | super drawOn: canvas. self label: 'Bezier curve laboratory' at: 35@20 size: 25 on: canvas. self label: 'Checkpoint ', self stage printString, ' / 8 - ', self stageTitle at: 35@62 size: 14 on: canvas. self label: 'MODEL COORDINATES / x right, y down' at: 35@110 size: 12 on: canvas. canvas fillRectangle: (self plotOrigin extent: self plotExtent) color: Color white. 0 to: 600 by: 60 do: [:x | canvas line: self plotOrigin + (x@0) to: self plotOrigin + (x@360) width: 1 color: (Color fromString: '#E8EDEF')]. 0 to: 360 by: 60 do: [:y | canvas line: self plotOrigin + (0@y) to: self plotOrigin + (600@y) width: 1 color: (Color fromString: '#E8EDEF')]. canvas frameRectangle: (self plotOrigin extent: self plotExtent) width: 1 color: (Color fromString: '#B3C0C5'). self stage >= 6 ifTrue: [self polyline: model sampledPoints color: (Color fromString: '#286A99') width: 3 on: canvas]. levels := model levelsAt: model parameter. palette := {Color fromString: '#9AA8AE'. Color fromString: '#D69352'. Color fromString: '#43A88F'}. 1 to: levels size - 1 do: [:level | self polyline: (levels at: level) color: (palette at: level) width: 2 on: canvas. (levels at: level) do: [:point | self dot: point radius: 4 color: (palette at: level) on: canvas]]. model points withIndexDo: [:point :index | self label: 'P', (index - 1) printString at: (35@140) + point + (9@6) size: 12 on: canvas]. result := model pointAt: model parameter. self dot: result radius: 7 color: (Color fromString: '#BF3E76') on: canvas. self label: 'Q(t) = ', (result x printShowingDecimalPlaces: 2), ' @ ', (result y printShowingDecimalPlaces: 2) at: 35@518 size: 16 on: canvas. self label: 't = ', (model parameter printShowingDecimalPlaces: 3) at: 688@145 size: 22 on: canvas. self stage < 4 ifTrue: [ self label: 'Change t in the Workspace.' at: 688@198 size: 13 on: canvas. self label: 'Dark pink: one evaluated point' at: 688@235 size: 13 on: canvas. self label: 'Gray: the control polygon' at: 688@270 size: 13 on: canvas. self label: 'Orange / green: interpolation' at: 688@305 size: 13 on: canvas]. self stage >= 5 ifTrue: [ self label: 'EDIT CONTROL / drag a handle' at: 688@268 size: 12 on: canvas. self label: 'P', (selected - 1) printString, ' x = ', (model points at: selected) x rounded printString at: 688@338 size: 12 on: canvas. self label: 'P', (selected - 1) printString, ' y = ', (model points at: selected) y rounded printString at: 688@389 size: 12 on: canvas]. self stage >= 6 ifTrue: [ self label: 'BLUE POLYLINE / ', model samples printString, ' segments; ', (model samples + 1) printString, ' evaluated points' at: 35@550 size: 12 on: canvas]. self stage < 6 ifTrue: [self label: 'Construction at one t. No sampled curve is drawn yet.' at: 35@568 size: 14 on: canvas]. self stage >= 7 ifTrue: [ self label: (running ifTrue: ['Playing / 4 second parameter cycle'] ifFalse: ['Paused / Play starts the native clock']) at: 688@448 size: 11 on: canvas]. self label: 'GEOMETRY: repeated interpolation. DRAWING: straight lines between samples.' at: 35@635 size: 13 on: canvas. self label: 'The model owns Points. Morphic owns pixels, controls and scheduling.' at: 35@664 size: 12 on: canvas ! ! !LBZApp methodsFor: 'tutorial'! stage ^ 3 ! ! !LBZApp methodsFor: 'tutorial'! stageTitle ^ 'Generalize to cubic de Casteljau' ! !