From bc079ccd8d71ecc2a265ad500c1c4e4f8bef9ca8 Mon Sep 17 00:00:00 2001 From: Joana Bergsiek Date: Thu, 7 Dec 2023 18:08:15 +0100 Subject: [PATCH] Mousecklicks work; rebuild listener when deleted & clicking rebuild --- .../SBInputBroadcaster.class.st | 97 ++++++++++++++++--- .../Sandblocks-Babylonian/SBLiveView.class.st | 11 ++- 2 files changed, 88 insertions(+), 20 deletions(-) diff --git a/packages/Sandblocks-Babylonian/SBInputBroadcaster.class.st b/packages/Sandblocks-Babylonian/SBInputBroadcaster.class.st index 0d50009b..19a3598f 100644 --- a/packages/Sandblocks-Babylonian/SBInputBroadcaster.class.st +++ b/packages/Sandblocks-Babylonian/SBInputBroadcaster.class.st @@ -4,7 +4,8 @@ Class { #instVars : [ 'listeners', 'mice', - 'miceContainers' + 'miceContainers', + 'stateHands' ], #category : #'Sandblocks-Babylonian' } @@ -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 @@ -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 ] @@ -82,6 +100,7 @@ SBInputBroadcaster >> initialize [ listeners := OrderedCollection new. mice := OrderedCollection new. miceContainers := OrderedCollection new. + stateHands := OrderedCollection new. self addEventCaptureFilter: self; @@ -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 [ @@ -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 } @@ -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 diff --git a/packages/Sandblocks-Babylonian/SBLiveView.class.st b/packages/Sandblocks-Babylonian/SBLiveView.class.st index 38c4c7a8..046b3733 100644 --- a/packages/Sandblocks-Babylonian/SBLiveView.class.st +++ b/packages/Sandblocks-Babylonian/SBLiveView.class.st @@ -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]. @@ -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 ]