Wiring stuff - CableMorph

We continue with "Cable" now.

We need draw one line what stay in place , could be selected and provide logical conection between diferent components.

PolygonMorph subclass: #CableMorph
instanceVariableNames: 'conectarCon state '
classVariableNames: ''
poolDictionaries: ''
category: 'LogicCircus'

In method categories panel, add a new category named initialization..

This time, we not have a initialize for CableMorph, I choose what this particular object have not existence alone.

from: aConnectionEnd to: anotherConnectionEnd
super initialize.
self makeOpen.
self makeForwardArrow.
"set my connectionEnds"
self conectarCon: (Array with: aConnectionEnd with: anotherConnectionEnd) asOrderedCollection.
self openInWorld

conectarCon is a place where we can have several objects what receive the changes in state.

In method categories panel, add a new category named accessing.

IRemember the convention on having accesing methods with same name of instance variables.

state
^ state.

state: aBoolean
state _ aBoolean

conectarCon
^ conectarCon

conectarCon: aCollection
conectarCon _ aCollection.
aCollection
do: [:aConnectionEnd | aConnectionEnd connection: self]

In method categories panel, add a new category named change reporting

Clickling in this category , add the following code.

remoteControl: aBoolean

"This is the method used when this object receive a message inorming what state should change "
state _ aBoolean.
aBoolean
ifTrue: [self color: Color red]
ifFalse: [self color: Color black].
self doRemoteControl

 

doRemoteControl
| notificarA |

"And when this objectchange my state, all objects logically conected to "
notificarA _ conectarCon copyWithout: conectarCon first.
notificarA
do: [:each | each remoteControl: self state]

 

In method categories panel, add a new category named connection

Clickling in this category , add the following code

connection: aConnection
| newConectar |
(aConnection conectarCon
anySatisfy: [:any | any = self])
ifTrue: [^ self].
newConectar _ self conectarCon asOrderedCollection.
newConectar insert: aConnection before: 2.
self conectarCon: newConectar

removeOthers

"This method is used when this object should be deleted"
(conectarCon isKindOf: OrderedCollection)
ifTrue: [conectarCon
do: [:each | (each owner isKindOf: Switch2Morph orOf: LightMorph)
ifTrue: [each delete]]]
ifFalse: [(conectarCon owner isKindOf: Switch2Morph orOf: LightMorph)
ifTrue: [conectarCon delete]]

Ending method categories , add a new category named stepping and presenter

In Morphic, all morphs could do something in predefinid intervals.

The message what start the action is step

step
| newVerts |

"This code magically redraw Cable, having a less messing "
newVerts _ OrderedCollection new.
newVerts add: conectarCon first center.
newVerts add: conectarCon first x @ conectarCon last center y.
newVerts add: conectarCon last center.
self setVertices: newVerts asArray.
submorphs size > 0
ifTrue: [submorphs
do: [:each |
each center: (self closestPointTo: each center).
]]


stepTime

"This means what step repeats each 500 milliseconds or half a second "
^500

,

 

 

 


1
Hosted by www.Geocities.ws