CompuertaMorph

We continue with "Compuerta" now.

This is one smart morph

ImageMorph subclass: #CompuertaMorph
instanceVariableNames: 'gateKind output '
classVariableNames: ''
poolDictionaries: ''
category: 'LogicCircus'

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

initialize
super initialize.
self isPartsBin.
self openInWorld

initialize: path gateKind: aSymbol
| myFile |
myFile _ path.
gateKind _ aSymbol.
self
image: (Form fromFileNamed: myFile).
self drawPins.

This methods are not used in normal use, we have one flag or another container with one instance of each kind of gate.

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

output
^ output

 

In method categories panel, add a new category named event handling

handlesMouseDown: evt
^ Smalltalk isMorphic not
or: [evt yellowButtonPressed]

mouseDown: evt
| menu |
evt yellowButtonPressed
ifFalse: [^ evt hand waitForClicksOrDrag: self event: evt].
menu _ MenuMorph new defaultTarget: self.
self addMenuItemsTo: menu hand: evt hand.
menu popUpEvent: evt in: self world

This two are for having yellow button ( right-mouse in Win option-mouse in Mac)

And with this code , we can have a menu .

addMenuItemsTo: aMenu hand: aHandMorph
| menu |

"In this example , you see how to have color , transparency and fonts in menus"
menu _ MenuMorph new.
menu color: Color blue.
menu
color: (menu color alpha: 0.3).
menu
add: 'descablear '
target: self
action: #descablear.
menu items
do: [:i | i color: Color yellow;

font: (StrikeFont
familyName: 'Comic Bold'
size: 18
emphasized: 1)].
menu invokeModal

In method categories panel, add a new category named testing

isAnd
^gateKind = #AND

isNand
^ gateKind = #NAND

isNor
^ gateKind = #NOR

isNot
^ gateKind = #NOT

isOr
^ gateKind = #OR

isXor
^ gateKind = #XOR

The above methods are very simple, we initialize all by hand and end with six instances of CompuertaMorph, each of a different kind.

Some object people could say this implementation is not very clean, but works for me, you could subclass.

isWired
^submorphs anySatisfy:[:each |
each isConnectionSet ]

This one search submorphs or esch "Compuerta:, what should be "Patas" and "Patas" are asked is connected or not

In method categories panel, add a new category named drawing

drawPins
| x y pataE pataS pataE1 pataE2 |
self isNot
ifTrue: [pataE _ PataMorph new.
x _ self left - pataE width.
y _ self center y.
pataE position: x @ y.
self addMorph: pataE.
pataS _ pataE copy.
x _ self right.
pataS position: x @ y.
self addMorphBack: pataS]
ifFalse: [pataE1 _ PataMorph new.
x _ self left - pataE1 width.
y _ self bottom - self top / 4 + self top.
pataE1 position: x @ y.
self addMorph: pataE1.
pataE2 _ pataE1 copy.
y _ self bottom - (self bottom - self top / 4).
pataE2 position: x @ y.
self addMorphBack: pataE2.
pataS _ pataE1 copy.
x _ self right.
y _ self center y.
pataS position: x @ y.
self addMorphBack: pataS]

 

Again this method is not used by the final user, only when we create the first instance of each kind of gate.

 

In method categories panel, add a new category named halos and balloon help

balloonText
| t |
t _ self isWired.
t
ifTrue: [self balloonColor: Color lightRed]
ifFalse: [self balloonColor: Color veryLightGray].
^ 'I `m a ' , gateKind asString , ' gate '

Here you could see how have visual feedback about some object, in this example changing ballon color if Compuerta is wired or not and also having a message what say what kind is.

Think what someone could not know what usual drawings of electronic means.

 

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

doRemoteControl

"We receive this message from other object"
self calculateOutput

calculateOutput

"And respond using rules of true tables"
self isAnd
ifTrue: [output _ submorphs first color = Color red & (submorphs second color = Color red)].
self isOr
ifTrue: [output _ submorphs first color = Color red | (submorphs second color = Color red)].
self isNot
ifTrue: [output _ submorphs first color ~~ Color red].
self isXor
ifTrue: [output _ submorphs first color ~~ submorphs second color].
self isNor
ifTrue: [output _ submorphs first color = Color black & (submorphs second color = Color black)].
self isNand
ifTrue: [output _ submorphs first color ~~ Color red & (submorphs second color = Color red)].
self changedOutput


changedOutput

"Finally this object change and send messages to other objects"
self output
ifTrue: [submorphs last color: Color red]
ifFalse: [submorphs last color: Color black].
submorphs last state: output.
submorphs last notifyConnection

This last is used when we do mistakes and wish to "unwire" the gate. For category name, I choose miscellaneous


descablear
submorphs
do: [:each |
each isConnectionSet
ifTrue: [each notifyRemoveMe].
each color: Color yellow]

 

,

 

 

 


1
Hosted by www.Geocities.ws