Object subclass: #LSTFiles instanceVariableNames: 'warning' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-SpriteTile'! SystemWindow subclass: #LSTWindow instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-SpriteTile'! !LSTWindow methodsFor: 'tutorial'! fixedExtent: aPoint fixedExtent := aPoint. super extent: aPoint ! ! !LSTWindow methodsFor: 'tutorial'! extent: aPoint super extent: (fixedExtent ifNil: [aPoint]) ! ! !LSTFiles methodsFor: 'tutorial'! replace: fileName writing: writeBlock | directory local temporary backup movedOriginal installed | warning := nil. directory := FileDirectory forFileName: fileName. local := FileDirectory localNameFor: fileName. temporary := local, '.new-', UUID new asString. backup := local, '.backup-', UUID new asString. movedOriginal := false. installed := false. [[writeBlock value: (directory fullNameFor: temporary). (directory fileExists: local) ifTrue: [ self rename: local to: backup in: directory. movedOriginal := true]. [self rename: temporary to: local in: directory. installed := true] on: Error do: [:failure | movedOriginal ifTrue: [self rename: backup to: local in: directory. movedOriginal := false]. failure pass]. movedOriginal ifTrue: [ [self cleanupBackup: backup in: directory] on: Error do: [:failure | warning := 'Saved; old backup retained: ', backup]] ] ensure: [(directory fileExists: temporary) ifTrue: [directory deleteFileNamed: temporary]] ] on: Error do: [:failure | ^ false]. ^ installed ! ! !LSTFiles methodsFor: 'tutorial'! rename: oldName to: newName in: directory directory rename: oldName toBe: newName ! ! !LSTFiles methodsFor: 'tutorial'! cleanupBackup: localName in: directory directory deleteFileNamed: localName ! ! !LSTFiles methodsFor: 'tutorial'! warning ^ warning ! ! !LSTFiles methodsFor: 'tutorial'! read: fileName | stream | (fileName isString and: [fileName withBlanksTrimmed isEmpty not]) ifFalse: [^ nil]. ^ [stream := FileStream readOnlyFileNamed: fileName. [stream size > 50000 ifTrue: [nil] ifFalse: [stream contentsOfEntireFile]] ensure: [stream close]] on: Error do: [:error | nil] ! ! !LSTFiles methodsFor: 'tutorial'! numbers: tokens ^ tokens collect: [:token | (token isEmpty or: [token size > 5 or: [(token allSatisfy: [:char | char >= $0 and: [char <= $9]]) not]]) ifTrue: [self error: 'Expected bounded decimal integers']. token asNumber] ! ! Object subclass: #LSSprite instanceVariableNames: 'frames current delay saved fileStore' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-SpriteTile'! Morph subclass: #LSSpriteGrid instanceVariableNames: 'app cellSize drawing' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-SpriteTile'! Morph subclass: #LSSpriteApp instanceVariableNames: 'document grid ink info status window drawing playing previewIndex wallMode' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-SpriteTile'! !LSSprite methodsFor: 'tutorial'! initialize super initialize. frames := OrderedCollection with: (Array new: 144 withAll: 0). current := 1. delay := 200. saved := nil. fileStore := LSTFiles new ! ! !LSSprite methodsFor: 'tutorial'! frames ^ frames ! ! !LSSprite methodsFor: 'tutorial'! current ^ current ! ! !LSSprite methodsFor: 'tutorial'! delay ^ delay ! ! !LSSprite methodsFor: 'tutorial'! frameCount ^ frames size ! ! !LSSprite methodsFor: 'tutorial'! pixels ^ frames at: current ! ! !LSSprite methodsFor: 'tutorial'! validCell: cell ^ cell isPoint and: [cell x isInteger and: [cell y isInteger and: [cell x between: 1 and: 12] and: [cell y between: 1 and: 12]]] ! ! !LSSprite methodsFor: 'tutorial'! indexOf: cell ^ ((cell y - 1) * 12) + cell x ! ! !LSSprite methodsFor: 'tutorial'! pixelAt: cell (self validCell: cell) ifFalse: [^ nil]. ^ self pixels at: (self indexOf: cell) ! ! !LSSprite methodsFor: 'tutorial'! paint: cell with: index ((self validCell: cell) and: [index isInteger and: [index between: 0 and: 5]]) ifFalse: [^ false]. self pixels at: (self indexOf: cell) put: index. ^ true ! ! !LSSprite methodsFor: 'tutorial'! clearFrame self pixels atAllPut: 0 ! ! !LSSprite methodsFor: 'tutorial'! selectFrame: index (index isInteger and: [index between: 1 and: frames size]) ifFalse: [^ false]. current := index. ^ true ! ! !LSSprite methodsFor: 'tutorial'! addFrame frames size >= 8 ifTrue: [^ false]. frames add: (Array new: 144 withAll: 0). current := frames size. ^ true ! ! !LSSprite methodsFor: 'tutorial'! duplicateFrame frames size >= 8 ifTrue: [^ false]. frames add: self pixels copy. current := frames size. ^ true ! ! !LSSprite methodsFor: 'tutorial'! deleteFrame frames size = 1 ifTrue: [^ false]. frames removeAt: current. current := current min: frames size. ^ true ! ! !LSSprite methodsFor: 'tutorial'! delay: milliseconds (milliseconds isInteger and: [milliseconds between: 50 and: 1000]) ifFalse: [^ false]. delay := milliseconds. ^ true ! ! !LSSprite methodsFor: 'tutorial'! asText ^ String streamContents: [:stream | stream nextPutAll: 'LSSPRITE1 '; print: delay; space; print: frames size; space; print: current. frames do: [:frame | frame do: [:pixel | stream space; print: pixel]]] ! ! !LSSprite methodsFor: 'tutorial'! loadText: text | tokens nums count newFrames newDelay newCurrent | text isString ifFalse: [^ false]. text size > 12000 ifTrue: [^ false]. [tokens := text findTokens: Character separators. (tokens size < 4 or: [tokens first ~= 'LSSPRITE1']) ifTrue: [self error: 'Header']. nums := fileStore numbers: tokens allButFirst. newDelay := nums first. count := nums second. newCurrent := nums third. ((newDelay between: 50 and: 1000) and: [count between: 1 and: 8] and: [newCurrent between: 1 and: count] and: [nums size = (3 + (144 * count))]) ifFalse: [self error: 'Shape']. (nums allButFirst: 3) do: [:value | (value between: 0 and: 5) ifFalse: [self error: 'Palette']]. newFrames := OrderedCollection new. 1 to: count do: [:index | newFrames add: (nums copyFrom: 4 + ((index - 1) * 144) to: 3 + (index * 144)) asArray] ] on: Error do: [:error | ^ false]. frames := newFrames. delay := newDelay. current := newCurrent. saved := self asText. ^ true ! ! !LSSprite methodsFor: 'tutorial'! isDirty ^ saved ~= self asText ! ! !LSSprite methodsFor: 'tutorial'! markSaved saved := self asText ! ! !LSSprite methodsFor: 'tutorial'! fileStore: aStore fileStore := aStore ! ! !LSSprite methodsFor: 'tutorial'! saveWarning ^ fileStore warning ! ! !LSSprite methodsFor: 'tutorial'! saveTo: fileName overwrite: permitted | success | (fileName isString and: [fileName withBlanksTrimmed isEmpty not and: [fileName asLowercase endsWith: '.sprite']]) ifFalse: [^ false]. ((FileDirectory default fileExists: fileName) and: [permitted not]) ifTrue: [^ false]. success := fileStore replace: fileName writing: [:temporary | | stream | stream := FileStream forceNewFileNamed: temporary. [stream nextPutAll: self asText] ensure: [stream close]]. success ifTrue: [self markSaved]. ^ success ! ! !LSSprite methodsFor: 'tutorial'! loadFrom: fileName | text | text := fileStore read: fileName. text ifNil: [^ false]. ^ self loadText: text ! ! !LSSpriteApp methodsFor: 'tutorial'! initialize super initialize. self extent: 750@570; color: (Color fromString: '#E9EFF6'). document := LSSprite new. ink := 1. drawing := false. playing := false. previewIndex := 1. wallMode := false. grid := LSSpriteGrid new app: self; position: self position + (28@170). self addMorph: grid. self addLabel: 'Sprite Studio' at: 25@14 size: 24. self addLabel: ('Checkpoint ', self stage printString, ' / 9 - ', self stageTitle) at: 26@48 size: 14. info := self addLabel: '' at: 28@510 size: 14. status := self addLabel: 'Ready. Each cell is a model value.' at: 28@539 size: 12. self buildToolbar. self stage >= 7 ifTrue: [document markSaved]. self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! addLabel: text at: point size: size | label | label := StringMorph contents: text font: (TextStyle default fontOfSize: size). label position: self position + point; color: (Color fromString: '#18334B'). self addMorph: label. ^ label ! ! !LSSpriteApp methodsFor: 'tutorial'! button: text at: point action: selector arguments: args | button | button := SimpleButtonMorph new label: text; target: self; actionSelector: selector; arguments: args; color: Color white; yourself. button position: self position + point. self addMorph: button. ^ button ! ! !LSSpriteApp methodsFor: 'tutorial'! document ^ document ! ! !LSSpriteApp methodsFor: 'tutorial'! grid ^ grid ! ! !LSSpriteApp methodsFor: 'tutorial'! window ^ window ! ! !LSSpriteApp methodsFor: 'tutorial'! playing ^ playing ! ! !LSSpriteApp methodsFor: 'tutorial'! previewIndex ^ previewIndex ! ! !LSSpriteApp methodsFor: 'tutorial'! ink ^ ink ! ! !LSSpriteApp methodsFor: 'tutorial'! status: text status contents: text ! ! !LSSpriteApp class methodsFor: 'tutorial'! open ^ self new open ! ! !LSSpriteApp methodsFor: 'tutorial'! open window ifNotNil: [window comeToFront. ^ self]. window := LSTWindow new model: self; setLabel: 'Sprite Studio'. window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window fixedExtent: 760@603. window openInWorld. ^ self ! ! !LSSpriteApp methodsFor: 'tutorial'! okToClose ^ true ! ! !LSSpriteApp methodsFor: 'tutorial'! windowIsClosing window := nil ! ! !LSSpriteApp methodsFor: 'tutorial'! release ^ self ! ! !LSSpriteGrid methodsFor: 'tutorial'! handlesMouseDown: event ^ true ! ! !LSSpriteGrid methodsFor: 'tutorial'! handlesMouseMove: event ^ drawing ! ! !LSSpriteGrid methodsFor: 'tutorial'! mouseDown: event drawing := true. self paintAt: event position ! ! !LSSpriteGrid methodsFor: 'tutorial'! mouseMove: event drawing ifTrue: [self paintAt: event position] ! ! !LSSpriteGrid methodsFor: 'tutorial'! mouseUp: event drawing := false ! ! !LSSpriteGrid methodsFor: 'tutorial'! endDrawing drawing := false. World hands do: [:hand | hand mouseFocus == self ifTrue: [hand newMouseFocus: nil]] ! ! !LSSpriteGrid methodsFor: 'tutorial'! paintAt: point | local cell | local := point - self position. ((0@0 extent: self extent) containsPoint: local) ifFalse: [^ false]. cell := ((local x // cellSize) + 1) @ ((local y // cellSize) + 1). ^ app paintCell: cell ! ! !LSSpriteApp methodsFor: 'tutorial'! uiManager ^ UIManager default ! ! !LSSpriteApp methodsFor: 'tutorial'! requestPath ^ self uiManager request: 'Sprite Studio filename (.sprite)' initialAnswer: 'my-work.sprite' onCancelReturn: nil ! ! !LSSpriteApp methodsFor: 'tutorial'! confirmOverwrite: path ^ UIManager default confirm: ('Replace existing file ', path, '?') ! ! !LSSpriteApp methodsFor: 'tutorial'! confirmDiscard ^ UIManager default confirm: 'Discard unsaved edits?' ! ! !LSSpriteApp methodsFor: 'tutorial'! save | path permission result | path := self requestPath. (path isNil or: [path withBlanksTrimmed isEmpty]) ifTrue: [self status: 'Save cancelled.' . ^ false]. permission := (FileDirectory default fileExists: path) ifTrue: [self confirmOverwrite: path] ifFalse: [true]. permission ifFalse: [self status: 'Save cancelled. Original file preserved.'. ^ false]. result := document saveTo: path overwrite: permission. self status: (result ifTrue: [document saveWarning ifNil: ['Saved.']] ifFalse: ['Save failed. Check extension and writable folder.']). ^ result ! ! !LSSpriteApp methodsFor: 'tutorial'! load | path result | path := self requestPath. (path isNil or: [path withBlanksTrimmed isEmpty]) ifTrue: [self status: 'Load cancelled.'. ^ false]. (document isDirty and: [self confirmDiscard not]) ifTrue: [self status: 'Load cancelled. Edits preserved.'. ^ false]. result := document loadFrom: path. result ifTrue: [self stopActivity. self refresh]. self status: (result ifTrue: ['Loaded and validated.'] ifFalse: ['Load rejected. Existing work is unchanged.']). ^ result ! ! !LSSpriteApp methodsFor: 'tutorial'! okToClose ^ document isDirty not or: [self confirmDiscard] ! ! !LSSpriteApp methodsFor: 'tutorial'! windowIsClosing self stopActivity. grid endDrawing. window := nil ! ! !LSSpriteGrid methodsFor: 'tutorial'! initialize super initialize. cellSize := 24. drawing := false. self extent: 288@288; color: (Color fromString: '#B8C7D8') ! ! !LSSpriteGrid methodsFor: 'tutorial'! app: anApp app := anApp ! ! !LSSpriteGrid methodsFor: 'tutorial'! drawOn: canvas | origin value shade | super drawOn: canvas. 1 to: 12 do: [:y | 1 to: 12 do: [:x | origin := self position + (((x - 1) * cellSize) @ ((y - 1) * cellSize)). value := app document pixelAt: x@y. shade := value = 0 ifTrue: [(x + y) even ifTrue: [Color white] ifFalse: [Color fromString: '#DCE3EC']] ifFalse: [app palette at: value + 1]. canvas fillRectangle: (origin extent: (cellSize - 1)@(cellSize - 1)) color: shade]] ! ! !LSSpriteApp methodsFor: 'tutorial'! palette ^ #( '#FFFFFF' '#23334D' '#3B82F6' '#F97352' '#FACC45' '#41B883') collect: [:hex | Color fromString: hex] ! ! !LSSpriteApp methodsFor: 'tutorial'! refresh grid changed. info contents: ('Frame ', document current printString, ' of ', document frameCount printString, ' | 12 x 12 pixels | ink ', ink printString). self changed ! ! !LSSpriteApp methodsFor: 'tutorial'! paintCell: cell | result | self stage >= 5 ifTrue: [self stopActivity]. result := document paint: cell with: ink. self refresh. ^ result ! ! !LSSpriteApp methodsFor: 'tutorial'! chooseInk: index (index isInteger and: [index between: 0 and: 5]) ifFalse: [^ false]. ink := index. self status: (index = 0 ifTrue: ['Eraser selected. Checkerboard means empty.'] ifFalse: ['Palette color ', index printString, ' selected.']). self refresh. ^ true ! ! !LSSpriteApp methodsFor: 'tutorial'! clear self stage >= 5 ifTrue: [self stopActivity]. document clearFrame. self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! changeFrame: delta self stage >= 5 ifTrue: [self stopActivity]. document selectFrame: (((document current - 1 + delta + document frameCount) rem: document frameCount) + 1). self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! add self stage >= 5 ifTrue: [self stopActivity]. self status: ((document addFrame) ifTrue: ['Frames updated.'] ifFalse: ['Keep between 1 and 8 frames.']). self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! duplicate self stage >= 5 ifTrue: [self stopActivity]. self status: ((document duplicateFrame) ifTrue: ['Frames updated.'] ifFalse: ['Keep between 1 and 8 frames.']). self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! remove self stage >= 5 ifTrue: [self stopActivity]. self status: ((document deleteFrame) ifTrue: ['Frames updated.'] ifFalse: ['Keep between 1 and 8 frames.']). self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! drawOn: canvas | origin value shade frame | super drawOn: canvas. frame := document frames at: (playing ifTrue: [previewIndex] ifFalse: [document current]). 1 to: 12 do: [:y | 1 to: 12 do: [:x | origin := self position + (430@200) + (((x - 1) * 14) @ ((y - 1) * 14)). value := frame at: ((y - 1) * 12) + x. shade := value = 0 ifTrue: [(x + y) even ifTrue: [Color white] ifFalse: [Color fromString: '#DCE3EC']] ifFalse: [self palette at: value + 1]. canvas fillRectangle: (origin extent: 14@14) color: shade]] ! ! !LSSpriteApp methodsFor: 'tutorial'! togglePreview playing ifTrue: [self stopActivity] ifFalse: [playing := true. previewIndex := document current. self startStepping]. self status: (playing ifTrue: ['Preview running. Painting or changing frames stops it.'] ifFalse: ['Preview stopped.']). self refresh ! ! !LSSpriteApp methodsFor: 'tutorial'! stepTime ^ document delay ! ! !LSSpriteApp methodsFor: 'tutorial'! step playing ifFalse: [^ self]. previewIndex := (previewIndex rem: document frameCount) + 1. self changed ! ! !LSSpriteApp methodsFor: 'tutorial'! stopActivity playing := false. self stopStepping. previewIndex := document current. self changed ! ! !LSSpriteApp methodsFor: 'tutorial'! windowIsClosing self stopActivity. grid endDrawing. window := nil ! ! !LSSpriteApp methodsFor: 'tutorial'! setDelay: milliseconds | result wasPlaying | wasPlaying := playing. result := document delay: milliseconds. result ifTrue: [wasPlaying ifTrue: [self stopStepping; startStepping]]. self status: (result ifTrue: ['Frame delay: ', document delay printString, ' ms'] ifFalse: ['Use an integer from 50 to 1000 ms.']). ^ result ! ! !LSSpriteApp methodsFor: 'tutorial'! requestTiming | answer number | answer := UIManager default request: 'Milliseconds per frame, 50 through 1000' initialAnswer: document delay printString onCancelReturn: nil. ^ self applyTimingText: answer ! ! !LSSpriteApp methodsFor: 'tutorial'! applyTimingText: answer | number | (answer isNil or: [answer withBlanksTrimmed isEmpty]) ifTrue: [self status: 'Timing cancelled.'. ^ false]. [number := (LSTFiles new numbers: {answer withBlanksTrimmed}) first] on: Error do: [:error | self status: 'Timing needs decimal digits.'. ^ false]. ^ self setDelay: number ! ! !LSSpriteApp methodsFor: 'tutorial'! stage ^ 9 ! ! !LSSpriteApp methodsFor: 'tutorial'! stageTitle ^ 'Close safely and finish the editor' ! ! !LSSpriteApp methodsFor: 'tutorial'! buildToolbar self button: 'Erase' at: 28@82 action: #chooseInk: arguments: #(0). self button: 'Ink' at: 95@82 action: #chooseInk: arguments: #(1). self button: 'Blue' at: 162@82 action: #chooseInk: arguments: #(2). self button: 'Coral' at: 229@82 action: #chooseInk: arguments: #(3). self button: 'Gold' at: 296@82 action: #chooseInk: arguments: #(4). self button: 'Mint' at: 363@82 action: #chooseInk: arguments: #(5). self button: 'Clear' at: 450@82 action: #clear arguments: #(). self button: 'Prev' at: 28@112 action: #changeFrame: arguments: #(-1). self button: 'Next' at: 90@112 action: #changeFrame: arguments: #(1). self button: 'New' at: 152@112 action: #add arguments: #(). self button: 'Copy' at: 211@112 action: #duplicate arguments: #(). self button: 'Delete' at: 275@112 action: #remove arguments: #(). self button: 'Play / Stop' at: 365@112 action: #togglePreview arguments: #(). self addLabel: 'Animation preview' at: 429@176 size: 14. self button: 'Timing' at: 478@112 action: #requestTiming arguments: #(). self button: 'Save' at: 570@82 action: #save arguments: #(). self button: 'Load' at: 636@82 action: #load arguments: #(). ! ! !LSSprite methodsFor: 'tutorial'! paintedPixelCount ^ self pixels count: [:value | value ~= 0] ! !