Skip to content

Commit

Permalink
added rectangular fill
Browse files Browse the repository at this point in the history
  • Loading branch information
valteu committed Jul 9, 2024
1 parent 7d5b848 commit ca714c0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 36 deletions.
113 changes: 83 additions & 30 deletions source/GM-TE/GMTEBrush.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Class {
#instVars : [
'currentBrush',
'radius',
'firstMatrixIndex',
'startMatrixIndex',
'layer'
'layer',
'currentMatrixIndex',
'firstMatrixIndex'
],
#category : #'GM-TE-UI'
}
Expand All @@ -27,34 +27,50 @@ GMTEBrush >> currentBrush: anObject [
currentBrush := anObject
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:47'
}
GMTEBrush >> currentMatrixIndex [
^ currentMatrixIndex
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:47'
}
GMTEBrush >> currentMatrixIndex: anObject [
currentMatrixIndex := anObject
]

{
#category : #'as yet unclassified',
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 14:55'
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:47'
}
GMTEBrush >> executeWithMatrixIndex: anIndex andLayer: aLayer [

self startMatrixIndex: anIndex.
self currentMatrixIndex: anIndex.
self layer: aLayer.
^ self currentBrush value.
]

{
#category : #forms,
#'squeak_changestamp' : 'JS 7/6/2024 16:46'
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:47'
}
GMTEBrush >> fillBrush [

| collection startTile visited |
self startMatrixIndex ifNil: [^nil].
self currentMatrixIndex ifNil: [^nil].

visited := Matrix rows: (self layer rowCount) columns: self layer columnCount.
collection := OrderedCollection new.
startTile := layer at: self startMatrixIndex y at: self startMatrixIndex x.
startTile := layer at: self currentMatrixIndex y at: self currentMatrixIndex x.

collection add: startMatrixIndex.
visited at: self startMatrixIndex y at: self startMatrixIndex x put: true.
collection add: currentMatrixIndex.
visited at: self currentMatrixIndex y at: self currentMatrixIndex x put: true.

self fillDfsWithVisited: visited andIndex: self startMatrixIndex andOriginTile: startTile andCollection: collection.
self fillDfsWithVisited: visited andIndex: self currentMatrixIndex andOriginTile: startTile andCollection: collection.

^ collection

Expand Down Expand Up @@ -85,6 +101,22 @@ GMTEBrush >> fillDfsWithVisited: aVisitedMatrix andIndex: anIndex andOriginTile:
self fillDfsWithVisited: aVisitedMatrix andIndex: newIndex andOriginTile: anOriginTile andCollection: aCollection]]]]
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:49'
}
GMTEBrush >> firstMatrixIndex [
^ firstMatrixIndex
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:49'
}
GMTEBrush >> firstMatrixIndex: anObject [
firstMatrixIndex := anObject
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 14:51'
Expand Down Expand Up @@ -119,24 +151,24 @@ GMTEBrush >> radius: anObject [

{
#category : #forms,
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 12:50'
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 09:01'
}
GMTEBrush >> radiusBrush [

| collection xMin xMax yMin yMax |
self startMatrixIndex ifNil: [^nil].
self currentMatrixIndex ifNil: [^nil].

collection := OrderedCollection new.

xMin := self startMatrixIndex x - self radius.
xMax := self startMatrixIndex x + self radius.
yMin := self startMatrixIndex y - self radius.
yMax := self startMatrixIndex y + self radius.
xMin := self currentMatrixIndex x - self radius.
xMax := self currentMatrixIndex x + self radius.
yMin := self currentMatrixIndex y - self radius.
yMax := self currentMatrixIndex y + self radius.

(xMin to: xMax) do: [:x |
(yMin to: yMax) do: [:y |
(( self startMatrixIndex x - x) squared + ( self startMatrixIndex y - y) squared <= self radius squared) ifTrue: [
collection add: (x@y)
(( self currentMatrixIndex x - x) squared + ( self currentMatrixIndex y - y) squared <= self radius squared) ifTrue: [
collection add: x@y
].
].
].
Expand All @@ -145,6 +177,34 @@ GMTEBrush >> radiusBrush [

]

{
#category : #forms,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 09:09'
}
GMTEBrush >> rectangleBrush [

| collection startRow endRow startCol endCol |
(self currentMatrixIndex isNil or: [self firstMatrixIndex isNil]) ifTrue: [^nil].
collection := OrderedCollection new.

"Determine the starting and ending rows and columns"

startRow := (self currentMatrixIndex x min: self firstMatrixIndex x).
endRow := (self currentMatrixIndex x max: self firstMatrixIndex x).
startCol := (self currentMatrixIndex y min: self firstMatrixIndex y).
endCol := (self currentMatrixIndex y max: self firstMatrixIndex y).

"Fill the collection with all indices within the rectangle"
startRow to: endRow do: [:row |
startCol to: endCol do: [:col |
collection add: (row@col)
].
].

^ collection

]

{
#category : #select,
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 15:47'
Expand All @@ -164,17 +224,10 @@ GMTEBrush >> selectRadiusBrush [
]

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 12:48'
#category : #select,
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 08:55'
}
GMTEBrush >> startMatrixIndex [
^ startMatrixIndex
]
GMTEBrush >> selectRectangleBrush [

{
#category : #accessing,
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 12:48'
}
GMTEBrush >> startMatrixIndex: anObject [
startMatrixIndex := anObject
self currentBrush: [self rectangleBrush]
]
15 changes: 9 additions & 6 deletions source/GM-TE/GMTEEditorTileMap.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ GMTEEditorTileMap >> model: anObject [

{
#category : #'event handling',
#'squeak_changestamp' : 'Valentin Teutschbein 7/6/2024 14:54'
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 09:26'
}
GMTEEditorTileMap >> mouseDown: anEvent [
"Implements placement of tiles"

| selectedCoordinates activeLayer |
| selectedCoordinates activeLayer selectedIndex |
self flag: 'refactor; method extraction for "Add tiles to layer" to minimize redundancy with mouseMove?'.
self model singleLayerSelected ifFalse: [^nil].
activeLayer := self model selectedLayers anyOne.

selectedCoordinates := self model brush executeWithMatrixIndex: (self tileIndexFromPosition: anEvent position) andLayer: (self tileMatrixStack layer: activeLayer).
selectedIndex := self tileIndexFromPosition: anEvent position.
self model brush firstMatrixIndex: selectedIndex.
selectedCoordinates := self model brush executeWithMatrixIndex: selectedIndex andLayer: (self tileMatrixStack layer: activeLayer).
self updateTiles: selectedCoordinates inLayer: activeLayer FromEvent: anEvent.

^ true
Expand Down Expand Up @@ -155,11 +157,12 @@ GMTEEditorTileMap >> mouseMove: anEvent [

{
#category : #'event handling',
#'squeak_changestamp' : 'Alex M 6/28/2024 19:57'
#'squeak_changestamp' : 'Valentin Teutschbein 7/9/2024 09:04'
}
GMTEEditorTileMap >> mouseUp: anEvent [

(self previousTileStates size > 0) ifTrue: [self saveTileEditChanges]
(self previousTileStates size > 0) ifTrue: [self saveTileEditChanges].
self model brush firstMatrixIndex: nil.
]

{
Expand Down

0 comments on commit ca714c0

Please sign in to comment.