123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- #define COMPONENTLISTBOX_CPP
- /*************************************************************************************************\
- ComponentListBox.cpp : Implementation of the ComponentListBox component.
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- \*************************************************************************************************/
- #include "ComponentListBox.h"
- #include "LogisticsVariant.h"
- #include "LogisticsComponent.h"
- #include "..\resource.h"
- #include "malloc.h"
- #include "mcLib.h"
- #include "LogisticsData.h"
- #include "MechLabScreen.h"
- #include "gamesound.h"
- #include "logisticserrors.h"
- ComponentListItem* ComponentListItem::s_templateItem = NULL;
- ComponentIconListBox* ComponentIconListBox::s_instance = NULL;
- ///////////////////////////////////////////////////////
- ComponentListItem::ComponentListItem( LogisticsComponent* pComp )
- {
- if ( s_templateItem )
- *this = *s_templateItem;
- pComponent = pComp;
- if ( pComp )
- {
- name.setText( pComponent->getName() );
- char numericText[32];
- sprintf( numericText, "%ld", pComponent->getCost() );
- costText.setText( numericText );
- sprintf( numericText, "%.0lf", pComponent->getHeat() );
- heatText.setText( numericText );
- int sizeX = pComponent->getComponentWidth();
- int sizeY = pComponent->getComponentHeight();
-
- const char* pFile = pComponent->getIconFileName();
- FullPathFileName path;
- path.init( artPath, pFile, "tga" );
- icon.setTexture( path );
- icon.resize( sizeX * LogisticsComponent::XICON_FACTOR, sizeY * LogisticsComponent::YICON_FACTOR);
- icon.setUVs( 0.f, 0.f, sizeX * 48.f, sizeY * 32.f );
- // figure out the difference between this thing's size and
- // the template objects
- int deltaY = icon.height() - s_templateItem->icon.height();
- int deltaX = icon.width() - s_templateItem->icon.width();
- // resize outlines and icon outlines
- outline.resize(outline.width(), outline.height()+deltaY);
- iconOutline.resize(iconOutline.width()+deltaX, iconOutline.height()+deltaY);
- disabledText.resize( outline.width(), outline.height() );
- disabledText.moveTo( 0, 0 );
- disabledText.alignment = 3;
- aObject::init( outline.left(), outline.top(), outline.width(), outline.height() );
- addChild( &icon );
- addChild( &name );
- addChild( &costText );
- addChild( &heatText );
- addChild( &costIcon );
- addChild( &heatIcon );
- addChild( &disabledText );
- if ( s_templateItem )
- {
- for ( int i = 0; i < 6; i++ )
- {
- for ( int j = 0; j < COMP_ANIMATION_COUNT; j++ )
- {
- if ( s_templateItem->pChildAnims[i] == &s_templateItem->animations[j] )
- {
- pChildAnims[i] = &animations[j];
- }
- }
- }
- }
- }
- }
- ComponentListItem::~ComponentListItem()
- {
- }
- int ComponentListItem::init( FitIniFile& file )
- {
- if ( !s_templateItem )
- {
- s_templateItem = new ComponentListItem( NULL );
- char animName[COMP_ANIMATION_COUNT][32];
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- {
- sprintf( animName[i], "Animation%ld", i );
- s_templateItem->animations[i].init( &file, animName[i] );
-
- }
-
- s_templateItem->icon.init( &file, "ComponentIcon" );
- assignAnimation( file, 0, animName, &s_templateItem->icon );
- s_templateItem->iconOutline.init( &file, "ComponentIconOutlineRect" );
- s_templateItem->outline.init( &file, "ComponentEntryBox" );
- s_templateItem->name.init( &file, "ComponentNameText" );
- assignAnimation( file, 1, animName, &s_templateItem->name );
- s_templateItem->costText.init( &file, "ComponentCostText" );
- assignAnimation( file, 2, animName, &s_templateItem->costText );
- s_templateItem->heatText.init( &file, "ComponentHeatText" );
- assignAnimation( file, 3, animName, &s_templateItem->heatText );
- s_templateItem->costIcon.init( &file, "ComponentCostIcon" );
- assignAnimation( file, 4, animName, &s_templateItem->costIcon );
- s_templateItem->heatIcon.init( &file, "ComponentHeatIcon" );
- assignAnimation( file, 5, animName, &s_templateItem->heatIcon );
- s_templateItem->disabledText.init( &file, "ComponentDisabledText" );
-
- }
- return 0;
- }
- void ComponentListItem::assignAnimation( FitIniFile& file, int whichChild, char animNames[COMP_ANIMATION_COUNT][32],
- aObject* object )
- {
- s_templateItem->pChildAnims[whichChild] = 0;
- char tmpAnimName[32];
- file.readIdString("Animation", tmpAnimName, 31);
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- {
- if ( stricmp( animNames[i], tmpAnimName ) == 0 )
- {
- s_templateItem->pChildAnims[whichChild] = &s_templateItem->animations[i];
- break;
- }
- }
- s_templateItem->addChild( object );
- }
- void ComponentListItem::render( )
- {
- aListItem::render();
- // draw rects, these aren't children
- outline.moveTo(globalX(), globalY());
- outline.setColor( animations[0].getCurrentColor( animations[0].getState() ) );
- outline.render();
- iconOutline.moveTo( globalX() + s_templateItem->iconOutline.left()
- , globalY() + s_templateItem->iconOutline.top());
- iconOutline.setColor( animations[0].getCurrentColor( animations[0].getState() ) );
- iconOutline.render();
- for ( int i = 0; i < 6; i++ )
- {
- if ( pChildAnims[i] )
- {
- long color = pChildAnims[i]->getCurrentColor( pChildAnims[i]->getState() );
- child(i)->setColor( color );
- }
- }
- if ( MechLabScreen::instance()->canAddComponent( pComponent ) )
- {
- icon.setColor( 0xff404040 );
- }
- else
- icon.setColor( 0xffffffff );
- }
- void ComponentListItem::update()
- {
- int bCanAdd = MechLabScreen::instance()->canAddComponent( pComponent );
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- {
- animations[i].update();
- }
- bool isInside = pointInside( userInput->getMouseX(), userInput->getMouseY() );
- if ( state == aListItem::SELECTED )
- {
- if ( (userInput->isLeftClick() && isInside)
- || ( animations[0].getState() != aAnimGroup::PRESSED )
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
- {
- setComponent();
- if ( bCanAdd )
- {
- soundSystem->playDigitalSample( LOG_WRONGBUTTON );
- }
- ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
- }
- if ( !bCanAdd )
- {
- if ( animations[0].getState() != aAnimGroup::PRESSED )
- {
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- {
- animations[i].setState( aAnimGroup::PRESSED );
-
- }
- }
- if ( isInside && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
- {
- ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
- }
- disabledText.setText( "" );
- }
- else
- {
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- animations[i].setState( aAnimGroup::DISABLED );
- if ( COMPONENT_TOO_HOT == bCanAdd )
- {
- disabledText.setText( IDS_MC_COMPONENT_TOO_HOT );
- }
- else if ( NO_MORE_ARMOR == bCanAdd )
- {
- disabledText.setText( IDS_MC_COMPONENT_TOO_MUCH_ARMOR );
- }
- else
- disabledText.setText( "" );
- }
-
- if ( !bCanAdd )
- {
- if ( userInput->isLeftDrag() && isInside &&
- pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() )
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
- startDrag();
- }
- }
- else if ( isInside && !bCanAdd
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
- {
- if ( animations[0].getState() != aAnimGroup::HIGHLIGHT )
- {
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- {
- animations[i].setState( aAnimGroup::HIGHLIGHT );
- }
- soundSystem->playDigitalSample( LOG_HIGHLIGHTBUTTONS );
- }
- state = aListItem::HIGHLITE;
- ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
- }
- else if ( !bCanAdd )
- {
- state = aListItem::ENABLED;
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- animations[i].setState( aAnimGroup::NORMAL );
- disabledText.setText( "" );
- }
- else
- {
- state = DISABLED;
- if ( isInside
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
- ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
- if ( (userInput->isLeftClick() && isInside
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) ) )
- {
- setComponent();
- soundSystem->playDigitalSample( LOG_WRONGBUTTON );
- }
- for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
- animations[i].setState( aAnimGroup::DISABLED );
- if ( COMPONENT_TOO_HOT == bCanAdd )
- {
- disabledText.setText( IDS_MC_COMPONENT_TOO_HOT );
- }
- else if ( NO_MORE_ARMOR == bCanAdd )
- {
- disabledText.setText( IDS_MC_COMPONENT_TOO_MUCH_ARMOR );
- }
- else
- disabledText.setText( "" );
- }
- if ( userInput->isLeftDoubleClick() && isInside
- && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
- doAdd();
- aObject::update();
- }
- void ComponentListItem::doAdd()
- {
- long x = -1;
- long y = -1;
- MechLabScreen::instance()->addComponent( pComponent, x, y);
- }
- void ComponentListItem::setComponent()
- {
- MechLabScreen::instance()->setComponent( pComponent, 1 );
- }
- void ComponentListItem::startDrag()
- {
- MechLabScreen::instance()->beginDrag( pComponent );
- }
- //*************************************************************************************************
- void ComponentIconListBox::setType( int newType, int otherNewType, int orThis )
- {
- if ( newType == type && itemCount )
- return;
- type = newType;
- scrollBar->setGreen();
-
- removeAllItems( 0 );
- itemSelected = -1;
-
- if ( !masterComponentList.Count() )
- {
- int count = 256;
- LogisticsComponent* pComp[256];
- LogisticsData::instance->getAllComponents( pComp, count );
- //for ( int j = 0; j < 2048; j++ )
- //{
- for ( int i = 0; i < count; i++ )
- {
- ComponentListItem* pItem = new ComponentListItem( pComp[i] );
- masterComponentList.Append( pItem );
- }
-
- // for ( EList< ComponentListItem*, ComponentListItem* >::EIterator iter = masterComponentList.Begin();
- // !iter.IsDone(); iter++ )
- // {
- /// delete *iter;
- // turn++;
- // }
- // masterComponentList.Clear();
- // }
- }
- for ( EList< ComponentListItem*, ComponentListItem* >::EIterator iter = masterComponentList.Begin();
- !iter.IsDone(); iter++ )
- {
- if ( (*iter)->getComponent()->isAvailable() )
- {
- if ( (*iter)->getComponent()->getType() == type ||
- (*iter)->getComponent()->getType() == otherNewType ||
- (*iter)->getComponent()->getType() == orThis )
- {
- if ( (*iter)->globalX() > globalX() )
- {
- (*iter)->move( -globalX(), 0 );
- }
- addSortedItem( (*iter) );
- }
- }
- }
- selectFirstAvailableComponent();
- }
- void ComponentIconListBox::update()
- {
- aListBox::update();
- if ( itemSelected != -1 )
- {
- if ( items[itemSelected]->getState() == aListItem::DISABLED
- || MechLabScreen::instance()->canAddComponent(
- ((ComponentListItem*)items[itemSelected])->getComponent() ) )
- {
- selectFirstAvailableComponent();
- }
- }
- }
- int ComponentIconListBox::selectFirstAvailableComponent()
- {
- bool bFound = 0;
- for ( int i = 0; i < itemCount; i++ )
- {
- if ( items[i]->getState() != aListItem::DISABLED
- && !MechLabScreen::instance()->canAddComponent(
- ((ComponentListItem*)items[i])->getComponent() ))
- {
- SelectItem( i );
- ((ComponentListItem*)items[i])->setComponent();
- bFound = true;
- return i;
- }
- }
- if ( !bFound )
- SelectItem( -1 );
- return -1;
- }
- ComponentIconListBox::ComponentIconListBox()
- {
- skipAmount = 5;
- type = -1;
- s_instance = this;
- }
- ComponentIconListBox::~ComponentIconListBox()
- {
- masterComponentList.Clear();
- s_instance = NULL;
- delete ComponentListItem::s_templateItem;
- ComponentListItem::s_templateItem = NULL;
- }
- LogisticsComponent* ComponentIconListBox::getComponent()
- {
- if ( itemSelected != -1 )
- {
- return ((ComponentListItem*)items[itemSelected])->pComponent;
- }
- return NULL;
- }
- void ComponentIconListBox::addSortedItem( ComponentListItem* pItem )
- {
- int size = pItem->getComponent()->getComponentHeight() *
- pItem->getComponent()->getComponentWidth();
- for ( int i = 0; i < itemCount; i++ )
- {
- LogisticsComponent* pTmp = ((ComponentListItem*)items[i])->getComponent();
- long tmpSize = pTmp->getComponentHeight() * pTmp->getComponentWidth();
- if ( size > tmpSize )
- {
- InsertItem( pItem, i );
- return;
- }
- else if ( size == tmpSize &&
- stricmp( pItem->getComponent()->getName(), pTmp->getName() ) < 0 )
- {
- InsertItem( pItem, i );
- return;
- }
- }
-
- aListBox::AddItem( pItem );
- }
-
- //*************************************************************************************************
- // end of file ( ComponentListBox.cpp )
|