home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-25 | 7.3 KB | 237 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CNeuron.cp ©1996 Timo Eloranta
- // ===========================================================================
- // An abstract neuron class - derived from LLink to be queueable
-
- #include "CNeuron.h"
- #include "CNeuralNet.h" // AddToLightQ
- #include "NS_Utils.h" // PlayOneSnd
-
- #include <Icons.h> // Universal header
- #include <LListIterator.h> // PowerPlant header
-
- CNeuralNet * CNeuron::sNet = NULL; // Initialize the static member
-
- // ---------------------------------------------------------------------------
- // • SetNeuronState
- //
- // Called by: CNeuron::CNeuron()
- // CNeuron::IncState
- // CStdNeuron::DoClickAction
- // CStdNeuron::SetPostLightUpState
- // ---------------------------------------------------------------------------
- // Set the state of the neuron to the given value and mark this neuron
- // as "dirty" so that it will be redrawn when the display is next updated.
-
- inline void
- CNeuron::SetNeuronState( Uint16 inState )
- {
- mState = inState;
- ForceRedraw();
- }
-
- // ---------------------------------------------------------------------------
- // • CNeuron()
- //
- // Called by: Constructors of subclasses
- // ---------------------------------------------------------------------------
- // Default constructor. Can't be called with 'new', since CNeuron is
- // an abstract class.
-
- CNeuron::CNeuron()
- {
- SetNeuronState( 0 );
- mMaxState = 0;
- mConnDirty = true;
- mConnLit = false;
- mDestination = false;
- }
-
- // ---------------------------------------------------------------------------
- // • IncState
- //
- // Called by: CStdNeuron::DoLightUpAction
- // ---------------------------------------------------------------------------
- // Increase the state of the neuron by one, unless the state is already
- // high enough to cause the neuron to light up. If the increasing causes
- // the neuron to light up, we add the neuron to the "light up queue".
-
- void
- CNeuron::IncState()
- {
- if ( ! ShouldLightUp() ) {
- SetNeuronState( mState + 1 );
- if ( ShouldLightUp() ) {
- sNet -> AddToLightQ( (LLink *) this );
- PlayOneSnd( snd_Zap, true );
- }
- }
- }
-
- // ---------------------------------------------------------------------------
- // • SetNeuronPos
- //
- // Called by: CNeuralNet::InitMatrix
- // ---------------------------------------------------------------------------
- // Set this neuron's position (column & row) in the matrix.
-
- inline void
- CNeuron::SetNeuronPos( Uint16 inCol, Uint16 inRow )
- {
- mCol = inCol;
- mRow = inRow;
- }
-
- // ---------------------------------------------------------------------------
- // • GetNeuronPos
- //
- // Called by: CNeuralNet::GenerateConnections
- // ---------------------------------------------------------------------------
- // Get this neuron's position (column & row) in the matrix.
-
- inline void
- CNeuron::GetNeuronPos( Uint16 & outCol, Uint16 & outRow) const
- {
- outCol = mCol;
- outRow = mRow;
- }
-
- // ---------------------------------------------------------------------------
- // • IsReceptor
- //
- // Called by: CStdNeuron::DoClickAction
- // ---------------------------------------------------------------------------
- // A neuron is a receptor if it can be manually lit by the user.
- // By default the neurons in the first column are receptors.
-
- inline Boolean
- CNeuron::IsReceptor() const
- {
- return mCol == 1;
- }
-
- // ---------------------------------------------------------------------------
- // • AddToNeuronList
- //
- // Called by: CNeuralNet::GenerateConnections
- // ---------------------------------------------------------------------------
- // Add the given neuron to the end of this neurons list of connected
- // neurons. Also update the mMaxState attribute!
-
- inline void
- CNeuron::AddToNeuronList( const CNeuronPtr inNeuronPtr )
- {
- mNextOnes.InsertItemsAt( 1, arrayIndex_Last, &inNeuronPtr );
- mMaxState++;
- }
-
- // ---------------------------------------------------------------------------
- // • IsConnectedTo
- //
- // Called by: CNeuralNet::GenerateConnections
- // ---------------------------------------------------------------------------
- // Return true if this neuron has a connection to the neuron which
- // inNeuronPtr points to. Otherwise return false.
-
- Boolean
- CNeuron::IsConnectedTo( const CNeuronPtr inNeuronPtr )
- {
- LArrayIterator theScanner( mNextOnes, iterate_FromStart );
- CNeuronPtr theNeuron;
-
- while ( theScanner.Next( &theNeuron )) {
- if ( theNeuron == inNeuronPtr )
- return true;
- }
-
- return false;
- }
-
- // ---------------------------------------------------------------------------
- // • SetNet
- //
- // Called by: CNeuralNet::InitMatrix
- // ---------------------------------------------------------------------------
- // Set the value of the static member sNet which points to the
- // CNeuralNet object which owns this and all the other neurons.
-
- void
- CNeuron::SetNet( CNeuralNet *inNet )
- {
- sNet = inNet;
- }
-
- // ---------------------------------------------------------------------------
- // • Draw
- //
- // Called by: CNeuralNet::DrawNeurons
- // ---------------------------------------------------------------------------
- // Draw this neuron (without the connections!) with a number and a color
- // which show the state of the neuron. If the neuron is below its maximum
- // state its color is either green (if state = 0) or blue (state > 0).
- // If the neuron is in its maximum state, it's color is red. Yellow is the
- // color of a neuron which is over its maximum state (when lit up).
- // The icon resources are numbered as follows:
- // green: 200, blue: 201-208, red: 300-309, yellow: 400
-
- void
- CNeuron::Draw( const Rect & inRect )
- {
- Int16 theResID; // ID of the icon resource
-
- if ( mState < mMaxState )
- theResID = 200 + mState;
- else if ( mState == mMaxState )
- theResID = 300 + mState;
- else // mState > mMaxState
- theResID = 400; // (= the neuron is lit up)
-
- ::PlotIconID( &inRect, // Rect
- atNone, // AlignmentType
- ttNone, // IconTransformType
- theResID); // Icon resource ID
-
- // The following has to do with the case where the user checks
- // which neurons are connected to a certain neuron by clicking
- // a "normal" neuron or option-clicking a receptor. We draw a
- // yellow ring around the connected neurons, because simply
- // lighting the connections isn't enough in the case of multiple
- // connected neurons being in the exact same direction...
- // The resource ID of the "yellow ring" icon is 500.
-
- if ( mDestination )
- ::PlotIconID( &inRect, atNone, ttNone, 500 );
-
- mRedrawNeeded = false; // Just drew it, so it's fine now
- }
-
- // ---------------------------------------------------------------------------
- // • DrawConnections
- //
- // Called by: CNeuralNet::DrawConnections
- // ---------------------------------------------------------------------------
- // Draw the connections of this neuron by going through the mNextOnes list.
- // If the connections are lit up, they are drawn in yellow color, otherwise
- // in the color set by CNeuroSimPane::DrawConnections().
-
- void
- CNeuron::DrawConnections( )
- {
- LArrayIterator theScanner( mNextOnes, iterate_FromStart );
- CNeuronPtr theNeuron;
- const RGBColor kYellowRGB = { 0xFC00, 0xF37D, 0x052F };
-
- if ( mConnLit )
- ::RGBForeColor( &kYellowRGB );
-
- while ( theScanner.Next( &theNeuron )) {
-
- ::MoveTo( mCenter.h, mCenter.v );
-
- ::LineTo( theNeuron -> mCenter.h,
- theNeuron -> mCenter.v );
- }
-
- mConnDirty = false; // Just drew 'em, so they are "clean" now...
- }
-