Building a Small Circuit Simulator

All pictures, last versions .cs are in http://ar.groups.yahoo.com/group/squeakRos/.

You must register in Yahoo first and in group later for having access to files ("Archivos")

I assume you know how to operate Squeak, but not how build something new.

Systems is loaded with many code what you could addapt to your needs.

Today , I start building a Switch.

I choose subclasing ImageMorph , and have the following definition

ImageMorph subclass: #SwitchMorph
instanceVariableNames: 'imageON imageOFF state connection '
classVariableNames: ''
poolDictionaries: ''
category: 'LogicCircus'

Why ? . Well, you must have a visual difference when swicht are in ON or in OFF

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

Clickling in this category , add the following code.

initialize
super initialize.
imageON _ Form fromFileNamed: 'SwitchON2.gif'.
imageOFF _ Form fromFileNamed: 'SwitchOFF2.gif'.
state _ false.
self image: imageOFF.
self isPartsBin.
self openInHand

In this moment you could test your code.

In a Workspace type newSwitch _ SwitchMorph new.

And a green switch (OFF) are attached to cursor.

If you put in a Flap like Widgets, you have how to get many Switchs .

Later we create our new repository Flap "Logic Circus".

We need how to know internals affairs.

In method categories panel, add a new category named accessing

state
^ state

Now , we add a new category named event handling

click: evt
state _ state not.
self state
ifTrue: [self image: imageON]
ifFalse: [self image: imageOFF].
self notifyConnection

With this code, each time you click on switch, change from green to red or viceversa.

Also, start sending a message to all objects connected about if is ON or OFF.

firstClickTimedOut: evt
| root |
root _ owner rootForGrabOf: self.
root == nil
ifFalse: [evt hand grabMorph: root]

mouseDown: evt
evt yellowButtonPressed
ifFalse: [^ evt hand waitForClicksOrDrag: self event: evt]

This methods are for safe draggin.

handlesMouseDown: anEvent
^ self isPartsDonor not

For use when is on partsBin , like a Flap or pasteUpMorph. In this way you can dupplicate morphs .

 

startDrag: evt
"We'll get a mouseDown first, some mouseMoves, and a mouseUp event
last"
self isSticky
ifTrue: [^ self].
evt isMouseDown
ifTrue: [self showBalloon: 'drag (mouse down)' hand: evt hand.
self world displayWorld.
(Delay forMilliseconds: 750) wait].
evt isMouseUp
ifTrue: [self showBalloon: 'drag (mouse up)' hand: evt hand].
(evt isMouseUp
or: [evt isMouseDown])
ifFalse: [self showBalloon: 'drag (mouse still down)' hand: evt hand.
evt hand grabMorph: self]

 

No comments. It's stolen from somewhere in Squeak and provide visual clue when you drag, showing ballon.

Now you add new category named connections

connection
"Answer the object that connects the receiver to other Switches."
^ connection

connection: anObject
"Set anObject to be the connection among two or more Switches. Make the
receiver a dependent of the argument, anObject."
connection _ anObject.
connection addDependent: self

isConnectionSet
"Answer whether the receiver is connected to an object that coordinates
updates among switches."
connection == nil
ifTrue: [state _ true.
^ false]
ifFalse: [^ true]

notifyConnection
"Send the receiver's connection (if it exists) the message 'changed: self'
in order for the connection to broadcast the change to other objects
connected by the connection."
self isConnectionSet
ifTrue: [self connection remoteControl: self state]

removeConnection
connection __ nil

last category to add is halos and balloon help

balloonText
^ 'I `m a Switch and my state is ' , state asString

Now, you have a finished Swtich

 

 

 

 

 

,

 

 

 


1
Hosted by www.Geocities.ws