Skip to content

Commit

Permalink
Mousecklicks work; rebuild listener when deleted & clicking rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeAtHPI committed Dec 7, 2023
1 parent eae4e4e commit bc079cc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 20 deletions.
97 changes: 81 additions & 16 deletions packages/Sandblocks-Babylonian/SBInputBroadcaster.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Class {
#instVars : [
'listeners',
'mice',
'miceContainers'
'miceContainers',
'stateHands'
],
#category : #'Sandblocks-Babylonian'
}
Expand All @@ -20,13 +21,15 @@ SBInputBroadcaster >> addListener: aMorph [

self listeners add: aMorph.
self mice add: CircleMorph newPin hide.
self stateHands add: HandMorph new.

"adding mice as our own morphs will cause rattling"
self containers add: (Morph new
color: Color transparent;
extent: aMorph extent).
self containers last addMorph: aMorph.
self containers last addMorph: mice last.
extent: aMorph extent;
setProperty: #sbListener toValue: aMorph;
yourself).
self containers last addAllMorphsBack: {mice last. aMorph.}.

^ self containers last

Expand All @@ -38,23 +41,38 @@ SBInputBroadcaster >> containers [
^ miceContainers
]

{ #category : #events }
SBInputBroadcaster >> eventProcessMouse: aMouse goingTo: aPosition in: aMorph [

aMouse position: aPosition.
(aMorph containsPoint: aPosition)
ifTrue: [aMouse show]
ifFalse: [aMouse hide]
]

{ #category : #events }
SBInputBroadcaster >> filterEvent: anEvent for: aMorph [

self listeners withIndexDo: [:aListener :i | | targetPosition newEvent |
targetPosition := self translatedPositionOf: anEvent from: aMorph to: aListener.
self listeners withIndexDo: [:aListener :i |
| localTarget newEvent delta |
localTarget := self translatedPositionOf: anEvent from: aMorph to: aListener.
delta := localTarget - anEvent position.
newEvent := anEvent copy.

anEvent isDropEvent
ifTrue: [newEvent translateTo: targetPosition]
ifFalse: [newEvent translateBy: targetPosition - anEvent position].
newEvent translateBy: delta.
anEvent isDropEvent ifTrue: [newEvent translateTo: localTarget].

aListener processEvent: newEvent.
(self mice at: i) position: targetPosition.
(self needsHandHandling: newEvent)
ifTrue: [
self simulateGlobalHand: (self stateHands at: i)
for: newEvent
in: aListener
pos: self activeHand position + delta.]
ifFalse: [aListener processEvent: newEvent.].

(aListener containsPoint: targetPosition)
ifTrue: [(self mice at: i) show]
ifFalse: [(self mice at: i) hide]].
self eventProcessMouse: (self mice at: i)
goingTo: localTarget
in: aListener.].

^ anEvent
]
Expand Down Expand Up @@ -82,6 +100,7 @@ SBInputBroadcaster >> initialize [
listeners := OrderedCollection new.
mice := OrderedCollection new.
miceContainers := OrderedCollection new.
stateHands := OrderedCollection new.

self
addEventCaptureFilter: self;
Expand All @@ -92,6 +111,22 @@ SBInputBroadcaster >> initialize [

]

{ #category : #accessing }
SBInputBroadcaster >> insertListener: aListener into: aContainer [

| index |
index := self containers indexOf: aContainer ifAbsent: [^ self].

(self containers at: index)
addMorphBack: aListener;
extent: aListener extent;
setProperty: #sbListener toValue: aListener.

aListener topLeft: (self containers at: index) topLeft.
self listeners at: index put: aListener.

]

{ #category : #accessing }
SBInputBroadcaster >> listeners [

Expand All @@ -111,16 +146,45 @@ SBInputBroadcaster >> mouseEnter: anEvent [
anEvent hand newKeyboardFocus: self.
]

{ #category : #events }
SBInputBroadcaster >> needsHandHandling: anEvent [

"rechte maustaste im sbeditor ausschließen?"
^ anEvent isMouse
and: [anEvent isMouseDown or: [anEvent isMouseUp]]
]

{ #category : #accessing }
SBInputBroadcaster >> replaceListener: oldListener with: newListener [

| index |
index := self listeners indexOf: oldListener ifAbsent: [^ self].

(self containers at: index) replaceSubmorph: (self listeners at: index) by: newListener.
(self containers at: index)
replaceSubmorph: (self listeners at: index)
by: newListener;
extent: newListener extent;
setProperty: #sbListener toValue: newListener.
self listeners at: index put: newListener.
(self containers at: index) extent: newListener extent.
]

{ #category : #events }
SBInputBroadcaster >> simulateGlobalHand: aHand for: anEvent in: aMorph pos: aPosition [

aMorph owner ifNotNil: [:theOwner | theOwner addMorphBack: aHand].

anEvent
position: aPosition;
setHand: aHand.
aHand handleEvent: anEvent.

aMorph owner ifNotNil: [:theOwner | theOwner removeMorph: aHand].
]

{ #category : #accessing }
SBInputBroadcaster >> stateHands [

^ stateHands
]

{ #category : #events }
Expand All @@ -129,6 +193,7 @@ SBInputBroadcaster >> translatedPositionOf: anEvent from: anOriginMorph to: aTar
| localAbsPoint localRelPoint targetAbsPoint |
localAbsPoint := anEvent position - anOriginMorph position.
localRelPoint := localAbsPoint / anOriginMorph extent.
localRelPoint := (localRelPoint x clampLow: 0 high: 1)@(localRelPoint y clampLow: 0 high: 1).
targetAbsPoint := (localRelPoint * aTargetMorph extent) + aTargetMorph position.

^ targetAbsPoint rounded
Expand Down
11 changes: 7 additions & 4 deletions packages/Sandblocks-Babylonian/SBLiveView.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ SBLiveView >> newRegisteredListenerFor: aPermutation [
]

{ #category : #initialization }
SBLiveView >> newRegisteredListenerFor: aPermutation replacing: anOldListener [
SBLiveView >> newRegisteredListenerFor: aPermutation in: aContainer [

| listener |
| listener oldListener |
listener := self evaluateSetUp.
broadcaster replaceListener: anOldListener with: listener.
oldListener := aContainer valueOfProperty: #sbListener.
oldListener owner
ifNil: [broadcaster insertListener: listener into: aContainer]
ifNotNil: [broadcaster replaceListener: oldListener with: listener].

listener allMorphsDo: [:aSubMorph |
SBExploriants objectToPermutation at: aSubMorph put: aPermutation].
Expand All @@ -165,7 +168,7 @@ SBLiveView >> rebuildButtonIn: aContainer applying: aPermutation [
^ SBButton new
icon: (SBIcon iconRotateRight size: 8.0 sbScaled)
label: 'Re-Build This'
do: [self newRegisteredListenerFor: aPermutation replacing: aContainer lastSubmorph];
do: [self newRegisteredListenerFor: aPermutation in: aContainer];
makeSmall;
cornerStyle: #squared
]
Expand Down

0 comments on commit bc079cc

Please sign in to comment.