I am trying to develop a PianoKeyboardMorph-like toy with Tweak. My
preliminary design idea is that each key signal MouseDown/Up event
and only one "central-handler" handles event from all keys. But I hit
difficulty: Tweak annotation syntax would force to declare trigger in
"pull event from one specific source" manner in handler side, that
means many many handlers with annotation would be needed. How to
declare "one central handler for many event sources" ?
--
I know at least two approaches for situations like this:
a) You can use #startScript: explicitly with many event sources, for
example:
self startScript: #onMouseEvent when:{
self. #mouseDown.
self. #mouseUp.
self. #mouseMove.
}.
This of course can be used to create a variable number of event sources
for example:
eventSpec := Array streamContents:[:s|
elements do:[:each| s nextPutAll:{each. #mouseDown}].
].
self startScript: #onElementMouseDown when: eventSpec.
b) You can use players which signal events in a well-known place. For
your situation, this is the design I would prefer. You could do this for
example by making a "PianoNote" player that knows its "piano" and then
has an event handler like:
PianoNote>>onMouseDown
<on: mouseDown>
piano signal: #playNote with: midiKey.
Piano>>onPlayNote: midiKey
"Play the given note"
<on: playNote>
Cheers,
- Andreas
--
CList
onCursorChanged: newCursor
"Resignal changes from our elements so that clients can listen to them."
<on: cursorChanged in: items>
self signal: #cursorChanged with: newCursor