diff --git a/src/Bloc-Memory/MGCardElement.class.st b/src/Bloc-Memory/MGCardElement.class.st index 6cf63e3..f9841ea 100644 --- a/src/Bloc-Memory/MGCardElement.class.st +++ b/src/Bloc-Memory/MGCardElement.class.st @@ -105,7 +105,21 @@ MGCardElement >> drawCommonOn: aCanvas [ { #category : #drawing } MGCardElement >> drawFlippedSideOn: aCanvas [ - "nothing for now" + + | font origin textPainter metrics | + font := aCanvas font + named: 'Source Sans Pro'; + size: 50; + build. + textPainter := aCanvas text + font: font; + paint: Color white; + string: self card symbol asString. + metrics := textPainter measure. + origin := self extent - metrics textMetrics bounds extent / 2.0. + textPainter + baseline: origin; + draw ] { #category : #drawing } diff --git a/src/Bloc-Memory/MGCardEventListener.class.st b/src/Bloc-Memory/MGCardEventListener.class.st new file mode 100644 index 0000000..8a58bdc --- /dev/null +++ b/src/Bloc-Memory/MGCardEventListener.class.st @@ -0,0 +1,19 @@ +Class { + #name : #MGCardEventListener, + #superclass : #BlEventListener, + #instVars : [ + 'memoryGame' + ], + #category : #'Bloc-Memory-Elements' +} + +{ #category : #'mouse handlers' } +MGCardEventListener >> clickEvent: anEvent [ + + memoryGame chooseCard: anEvent currentTarget card +] + +{ #category : #accessing } +MGCardEventListener >> memoryGame: aGame [ + memoryGame := aGame +] diff --git a/src/Bloc-Memory/MGGameElement.class.st b/src/Bloc-Memory/MGGameElement.class.st index af51342..bcb2d7d 100644 --- a/src/Bloc-Memory/MGGameElement.class.st +++ b/src/Bloc-Memory/MGGameElement.class.st @@ -11,42 +11,66 @@ Class { #name : #MGGameElement, #superclass : #BlElement, #instVars : [ - 'memoryGame' + 'game' ], #category : #'Bloc-Memory-Elements' } { #category : #example } -MGGameElement class >> example [ - "self example" +MGGameElement class >> exampleInASpace [ - | game board | + | space game gameElement | game := MGGame withNumbers. - board := MGGameElement new. - board memoryGame: game. - ^ board + gameElement := self new. + gameElement memoryGame: game. + + space := BlSpace new. + space extent: 420@420. + space addChild: gameElement. + space show. + ] { #category : #initialization } MGGameElement >> initialize [ + super initialize. - self layout: BlGridLayout horizontal. + + self background: (BlBackground paint: Color gray darker). + self layout: (BlGridLayout horizontal cellSpacing: 20). + + self constraintsDo: [ :aLayoutConstraints | + aLayoutConstraints horizontal fitContent. + aLayoutConstraints vertical fitContent ] ] { #category : #accessing } MGGameElement >> memoryGame [ - ^ memoryGame + ^ game ] { #category : #accessing } -MGGameElement >> memoryGame: anObject [ - memoryGame := anObject. - memoryGame availableCards - do: [ :aCard | self addChild: (self newCardElement card: aCard) ] +MGGameElement >> memoryGame: aGame [ + + | aCardEventListener | + game := aGame. + aCardEventListener := self newCardEventListener memoryGame: aGame. + self layout columnCount: game gridSize. + game availableCards do: [ :aCard | + | cardElement | + cardElement := self newCardElement card: aCard. + cardElement addEventHandler: aCardEventListener. + self addChild: cardElement ] ] { #category : #accessing } MGGameElement >> newCardElement [ ^ MGCardElement new ] + +{ #category : #'instance creation' } +MGGameElement >> newCardEventListener [ + + ^ MGCardEventListener new +]