Skip to content

Commit

Permalink
Added grid rescaling api & minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo Zilkenat committed Jun 8, 2024
1 parent 6b2a379 commit 9805238
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 34 deletions.
12 changes: 6 additions & 6 deletions source/GM-TE/GMTEEditor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,26 @@ GMTEEditor class >> fromTileHeight: aHeight widht: aWidth [

{
#category : #'as yet unclassified',
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 22:22'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:33'
}
GMTEEditor class >> getTileMapFromFilePath: aFilePath [

|aFileStream tileMap|
"TODO: Check if .morph is a tileMap"
aFileStream := (MultiByteBinaryOrTextStream with: ((FileStream readOnlyFileNamed: aFilePath) binary contentsOfEntireFile)) binary setToPlaceholder.
aFileStream := (MultiByteBinaryOrTextStream with: ((FileStream readOnlyFileNamed: aFilePath) binary contentsOfEntireFile)) binary reset.
tileMap := aFileStream fileInObjectAndCode.
(tileMap isKindOf: GMTEEditableTileMap) ifFalse: [^nil].
^GMTETileMap newFrom: (tileMap copy).
]

{
#category : #'as yet unclassified',
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 22:22'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:33'
}
GMTEEditor class >> getTileMapFromProjectName: aName withPath: aPath [

|aFileStream tileMap|
aFileStream := (MultiByteBinaryOrTextStream with: ((GitAssetLoader for: aName) loadAsset: aPath)) binary setToPlaceholder.
aFileStream := (MultiByteBinaryOrTextStream with: ((GitAssetLoader for: aName) loadAsset: aPath)) binary reset.
tileMap := aFileStream fileInObjectAndCode.
(tileMap isKindOf: GMTEEditableTileMap) ifFalse: [^nil].
^GMTETileMap newFrom: (tileMap copy).
Expand Down Expand Up @@ -546,7 +546,7 @@ GMTEEditor >> import [

{
#category : #'button functions',
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 22:22'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:33'
}
GMTEEditor >> importFromMorph [
"import tilemap into editor from a .morph file"
Expand All @@ -556,7 +556,7 @@ GMTEEditor >> importFromMorph [

stream ifNil: [^self].

aFileStream := (MultiByteBinaryOrTextStream with: ((FileStream readOnlyFileNamed: stream fullName) binary contentsOfEntireFile)) binary setToPlaceholder.
aFileStream := (MultiByteBinaryOrTextStream with: ((FileStream readOnlyFileNamed: stream fullName) binary contentsOfEntireFile)) binary reset.
tileMap := aFileStream fileInObjectAndCode.
(tileMap isKindOf: GMTEEditableTileMap) ifFalse: [^self].

Expand Down
11 changes: 11 additions & 0 deletions source/GM-TE/GMTEImageMorph.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ GMTEImageMorph >> extent: anExtent [
self image: (self fullResolutionSprite scaledToSize: anExtent).
]

{
#category : #'as yet unclassified',
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:01'
}
GMTEImageMorph >> free [

(self owner) ifNotNil: [
self owner removeMorph: self].
self abandon
]

{
#category : #accessing,
#'squeak_changestamp' : 'TW 5/22/2024 17:15'
Expand Down
6 changes: 3 additions & 3 deletions source/GM-TE/GMTEStaticCoordinateMatrix.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Class {

{
#category : #private,
#'squeak_changestamp' : 'Ivo Zilkenat 5/19/2024 15:44'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:22'
}
GMTEStaticCoordinateMatrix class >> rows: aNumber1 columns: aNumber2 contents: anArray [
GMTEStaticCoordinateMatrix class >> rows: aNumber1 columns: aNumber2 [

| newMatrix |
newMatrix := super rows: aNumber1 columns: aNumber2 contents: anArray.
newMatrix := super rows: aNumber1 columns: aNumber2.
^ newMatrix coordinateSystem: (GMTECoordinateSystem
width: newMatrix columnCount
height: newMatrix rowCount)
Expand Down
77 changes: 58 additions & 19 deletions source/GM-TE/GMTETileMap.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,20 @@ GMTETileMap >> fullGridSizeWidth: anObject [
]

{
#category : #updating,
#category : #initialization,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 20:56'
}
GMTETileMap >> generateMatrixStacks [

self tileMatrixStack: (GMTETileMatrixStack fromWidth: self tileWidth height: self tileHeight).
self tileMatrixStackVisual: (GMTETileMatrixStack fromWidth: self tileWidth height: self tileHeight).
self tileMatrixStackVisual morphicLayerStartingOffset: 10. "TODO: Number ?"
self generateVisualTiles.
self enableVisualLayer
]

{
#category : #initialization,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 21:33'
}
GMTETileMap >> generateTileAtlayer: aLayer x: x y: y stack: aStack tileType: aTileClass [
Expand All @@ -228,20 +241,20 @@ GMTETileMap >> generateTileAtlayer: aLayer x: x y: y stack: aStack tileType: aTi
]

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 21:28'
#category : #initialization,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:13'
}
GMTETileMap >> generateVisualTiles [
"Create grid tile matrix datastructure"

1 to: self tileHeight do: [:y |
1 to: self tileWidth do: [:x |
1 to: self mapTileHeight do: [:y |
1 to: self mapTileWidth do: [:x |
self generateTileAtlayer: 1 x: x y: y stack: self tileMatrixStackVisual tileType: GMTETileVisual]].

]

{
#category : #'as yet unclassified',
#category : #initialization,
#'squeak_changestamp' : 'TW 5/22/2024 15:48'
}
GMTETileMap >> initialize [
Expand Down Expand Up @@ -395,27 +408,58 @@ GMTETileMap >> mapTileWidth: anObject [

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 20:27'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:58'
}
GMTETileMap >> rescaleMap [

self tileSelectionSet clearAllHighlightings.
"self tileSelectionSet clearAllHighlightings."
self rescaleMapWidth: self tileWidth height: self tileHeight padding: self mapPadding
]

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 5/20/2024 21:19'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:40'
}
GMTETileMap >> rescaleMapWidth: aWidth height: aHeigth [

self rescaleMapWidth: aWidth height: aHeigth padding: self mapPadding
]

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:59'
}
GMTETileMap >> rescaleMapWidth: aWidth height: aHeigth padding: aPadding [
"Rescale map & trigger update"

"Warning: If padding is to small relative to map, divisions by zero can occur"
| sizeChanged |
sizeChanged := (aWidth ~= self mapTileWidth) or: [aHeigth ~= self mapTileHeight].

self setDimensionsWidth: aWidth height: aHeigth padding: aPadding.
(sizeChanged) ifTrue: [
self rescaleMatrixStacks].
self updateMap
]

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:34'
}
GMTETileMap >> rescaleMatrixStacks [

(self tileMatrixStack) ifNotNil: [
self tileMatrixStack rescaleToWidth: self mapTileWidth height: self mapTileHeight].
"Rescaling returns a copy of the original tiles, which must be added as submorphs again. TODO: move responsibility to Stack?"
self tileMatrixStack doTiles: [ :tile |
self addMorph: tile].
(self tileMatrixStackVisual) ifNotNil: [
self tileMatrixStackVisual rescaleToWidth: self mapTileWidth height: self mapTileHeight.
self tileMatrixStackVisual reset.
self generateVisualTiles.
self enableVisualLayer]
]

{
#category : #conversion,
#'squeak_changestamp' : 'Ivo Zilkenat 5/21/2024 20:20'
Expand All @@ -428,10 +472,10 @@ GMTETileMap >> revertCorrectedTilePosition: aPoint [

{
#category : #accessing,
#'squeak_changestamp' : 'Ivo Zilkenat 5/19/2024 15:59'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:00'
}
GMTETileMap >> setDimensionsWidth: aWidth height: aHeigth padding: aPadding [
"Set dimensions (defines what is considered as part of 'dimenisions')"
"Set dimensions (defines what is considered as part of 'dimensions')"

self
mapTileWidth: aWidth;
Expand Down Expand Up @@ -689,20 +733,15 @@ GMTETileMap >> updateDimensions [

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 21:54'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 20:57'
}
GMTETileMap >> updateMap [
"Update map by: fitting grid to current screen, generating tile matrix, adding sprites"

self updateDimensions.
self tileMatrixStack
ifNil: [
self tileMatrixStack: (GMTETileMatrixStack fromWidth: self tileWidth height: self tileHeight).
self tileMatrixStackVisual: (GMTETileMatrixStack fromWidth: self tileWidth height: self tileHeight).
self tileMatrixStackVisual morphicLayerStartingOffset: 10. "TODO: Number ?"
self generateVisualTiles.
self enableVisualLayer]
ifNotNil: [self updateTiles].
ifNil: [self generateMatrixStacks] "TODO: shouldnt this be properly initialized?"
ifNotNil: [self updateTiles]
]

{
Expand Down
37 changes: 35 additions & 2 deletions source/GM-TE/GMTETileMatrixLayer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ Class {
#category : #'GM-TE-Core'
}

{
#category : #copying,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:26'
}
GMTETileMatrixLayer >> asRescaledToWidth: aWidth height: aHeight [

| newMatrix |
newMatrix := self class rows: aHeight columns: aWidth.

"Copy over tiles & handle tile removal"

1 to: (aHeight min: self rowCount) do: [:row |
1 to: (aWidth min: self columnCount) do: [:col |
newMatrix at: row at: col put: (self at: row at: col) veryDeepCopy]].

^newMatrix

]

{
#category : #enumerating,
#'squeak_changestamp' : 'Valentin Teutschbein 6/7/2024 20:55'
Expand All @@ -16,9 +35,23 @@ GMTETileMatrixLayer >> doTiles: aBlock [

{
#category : #removing,
#'squeak_changestamp' : 'Valentin Teutschbein 6/7/2024 20:50'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:23'
}
GMTETileMatrixLayer >> free [

self doTiles: [ :imageMorph | imageMorph abandon]
self doTiles: [ :tile |
tile free].

self setNil
]

{
#category : #removing,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:23'
}
GMTETileMatrixLayer >> setNil [

1 to: self rowCount do: [:row |
1 to: self columnCount do: [:col |
self at: row at: col put: nil]].
]
22 changes: 20 additions & 2 deletions source/GM-TE/GMTETileMatrixStack.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,29 @@ GMTETileMatrixStack >> removeLayerAt: idx [
removedLayer free.
]

{
#category : #updating,
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:33'
}
GMTETileMatrixStack >> rescaleToWidth: aWidth height: aHeight [
"This will rescale layers be recreating them"

| newLayers newLayer |
newLayers := LinkedList new.

self doLayers: [ :layer |
newLayer := layer asRescaledToWidth: aWidth height: aHeight.
newLayers addLast: newLayer.
layer free].

self matrixLayers: newLayers.
]

{
#category : #removing,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 22:22'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 21:31'
}
GMTETileMatrixStack >> setToPlaceholder [
GMTETileMatrixStack >> reset [

self layerCount to: 2 by: -1 do: [ :layer | self removeLayerAt: layer].
(self layer: 1) free
Expand Down
4 changes: 2 additions & 2 deletions source/GM-TE/GMTETileVisual.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Class {

{
#category : #highlighting,
#'squeak_changestamp' : 'Ivo Zilkenat 6/7/2024 21:25'
#'squeak_changestamp' : 'Ivo Zilkenat 6/8/2024 22:36'
}
GMTETileVisual >> applyBorderHighlighting [

self
borderColor: Color white;
borderWidth: 2.
borderWidth: 1.
]

{
Expand Down

0 comments on commit 9805238

Please sign in to comment.