int32_t LogisticsVariant::init(CSVFile* file, LogisticsChassis* pChassis, int32_t Variant)
{
	bDesignerMech = true;
	int32_t offset = 97 * Variant;
	fileID = Variant;
	gosASSERT(pChassis);
	chassis = pChassis;
	ID = (pChassis->refCount << 16) | pChassis->ID;
	\
	pChassis->refCount++;
	file->readBoolean(21 + offset, 4, bHidden);
	char buffer[256];
	if(NO_ERROR == file->readString(23 + offset, 2, buffer, 256))
	{
		variantName = buffer;
	}
	LogisticsComponent*  pComps[128];
	int32_t xLocs[128];
	int32_t yLocs[128];
	memset(pComps, 0, sizeof(LogisticsComponent*) * 128);
	int32_t componentCount = 0;
	int32_t i, j, k;
	for(i = 26; i < 97; i++)
	{
		int32_t componentID;
		if(NO_ERROR == file->readLong(offset + i, 5, componentID) && componentID != 0xff)
		{
			LogisticsComponent* pComp = LogisticsData::instance->getComponent(componentID);
			if(pComp)
			{
				file->readLong(offset + i, 4, yLocs[componentCount]);
				file->readLong(offset + i, 3, xLocs[componentCount]);
				pComps[componentCount++] = pComp;
			}
		}
	}
	for(j = 4; j > 0; j--)
	{
		for(k = 3; k > 0; k--)
		{
			for(i = 0; i < componentCount; i++)
			{
				LogisticsComponent*
				pComponent = pComps[i];
				if(pComponent && pComponent->getComponentWidth() == k
						&& pComponent->getComponentHeight() == j)
				{
					if(!addComponent(pComps[i]->getID(), xLocs[i], yLocs[i]))
					{
						char errorString[256];
						sprintf(errorString, "Couldn't add component with id %ld",
								pComponent->getID());
					}
				}
			}
		}
	}
	return 0;
}
Exemplo n.º 2
0
void MechLabScreen::updateDiagramInput()
{
	long mouseX = userInput->getMouseX();
	long mouseY = userInput->getMouseY();


	if ( rects[13].pointInside( mouseX, mouseY ) )
	{
		long x, y, x2, y2;
		getMouseDiagramCoords( x, y );	
		
		if ( x != -2 )
		{
			// now offset by size of the component
			if ( pDragComponent )
			{
				long screenX, screenY;
				diagramToScreen( x, y, screenX, screenY );

				x -= pDragComponent->getComponentWidth()/2;
				y -= pDragComponent->getComponentHeight()/2;

				if ( mouseX - screenX > LogisticsComponent::XICON_FACTOR/2
					&& pDragComponent->getComponentWidth()/2)
				{
					x += 1;
				}	
			}

			if ( x < 0 )
				x = 0;

			if ( x >= pVariant->getComponentAreaWidth() - 1 ) 
				x = pVariant->getComponentAreaWidth() - 1;

			if ( y < 0 )
				y = 0;

			if ( y >= pVariant->getComponentAreaHeight() - 1 )
				y = pVariant->getComponentAreaHeight() - 1;

			
			if ( pDragComponent )
			{
				x2 = x + pDragComponent->getComponentWidth();
				y2 = y + pDragComponent->getComponentHeight();	

				if ( x2 > pVariant->getComponentAreaWidth() )
				{
					x = pVariant->getComponentAreaWidth() - pDragComponent->getComponentWidth();
					x2 = pVariant->getComponentAreaWidth();
				}

				if ( y2 > pVariant->getComponentAreaHeight() )
				{
					y = pVariant->getComponentAreaHeight() - pDragComponent->getComponentHeight();
					y2 = pVariant->getComponentAreaHeight();
				}
			}
			else
			{
				x2 = x + 1;
				y2 = y + 1;
			}
		
			long tmpX, tmpY;

			diagramToScreen( x, y, tmpX, tmpY );			

			// update outline rect
			if ( selRect )
				selRect->moveTo( tmpX, tmpY );			


			// highlight text if appropriate
			LogisticsComponent* pComp = pVariant->getCompAtLocation(x, y, tmpX, tmpY );
			if ( pComp )
			{
				long compX, compY, compX2, compY2;
				diagramToScreen( tmpX, tmpY, compX, compY );
				diagramToScreen( tmpX + pComp->getComponentWidth(), tmpY + pComp->getComponentHeight(), compX2, compY2 );
				if ( (compX <= userInput->getMouseX() && compX2 >= userInput->getMouseX()
					&& compY <= userInput->getMouseY() && compY2 >= userInput->getMouseY())
					|| tmpX == -2 )
				{
					::helpTextID = IDS_HELP_COMP0 + pComp->getID();
				}
			}
		}
		else if ( selRect )
		{
			selRect->moveTo( rects[6].left() + rects[6].width()/2 - selRect->width()/2,
							rects[6].top() + rects[6].height()/2 - selRect->height()/2);
		}
		// check for jump jet hot text
		if ( x == -2 && y == -2 )
		{
			long tmpX, tmpY;
			LogisticsComponent* pComp = pVariant->getCompAtLocation(x, y, tmpX, tmpY );
			if ( pComp )
			{
				long compX, compY, compX2, compY2;
				diagramToScreen( tmpX, tmpY, compX, compY );
				diagramToScreen( tmpX + pComp->getComponentWidth(), tmpY + pComp->getComponentHeight(), compX2, compY2 );
				if ( (compX <= userInput->getMouseX() && compX2 >= userInput->getMouseX()
					&& compY <= userInput->getMouseY() && compY2 >= userInput->getMouseY())
					|| tmpX == -2 )
				{
					::helpTextID = IDS_HELP_COMP0 + pComp->getID();
				}
			}
		}
		

		if ( pDragComponent )
		{
			if (NO_ERR == pVariant->canAddComponent( pDragComponent, x, y )
				&& x != -1 && y != -1 )
			{
				selRect->setColor( 0xffffffff );
			}
			else
				selRect->setColor( 0xffff0000 );			
		}			
		else if ( userInput->isLeftDrag() )
		{
			long i, j;
			getMouseDiagramCoords( i, j );

			if ( i != -1 && j != -1 )
			{
				LogisticsComponent* pComp = pVariant->getCompAtLocation(i, j, selI, selJ );
				if ( pComp && pComp == pSelectedComponent )
				{
					if ( canRemoveComponent( pComp ) )
					{
						componentListBox.SelectItem( -1 );
						beginDrag( pComp );
						bDragLeft = true;
						removeComponent( i, j );
						setComponent( pComp );
						updateDiagram();
					}
					else
						soundSystem->playDigitalSample( LOG_WRONGBUTTON );
				}
				else
					pSelectedComponent = 0;
			}

		}
		else if ( userInput->isLeftDoubleClick() )
		{
			long tmpI, tmpJ;
			getMouseDiagramCoords( tmpI, tmpJ );

			if ( tmpI != -1 && tmpJ != -1 )
			{
				selI = tmpI;
				selJ = tmpJ;
				LogisticsComponent* pComp = pVariant->getCompAtLocation(tmpI, tmpJ, selI, selJ );
				if ( pComp )
				{
					if ( canRemoveComponent( pComp ) )
					{
						removeComponent( selI, selJ );
						componentListBox.SelectItem( -1 );
						if ( -1 == selectFirstDiagramComponent() )
							selectFirstLBComponent();				
					}
					else
						soundSystem->playDigitalSample( LOG_WRONGBUTTON );
				}
			}
		
		}
		else if ( userInput->isLeftClick() )
		{
			long tmpI, tmpJ;
			getMouseDiagramCoords( tmpI, tmpJ );

			if ( tmpI != -1 && tmpJ != -1 )
			{
				selI = tmpI;
				selJ = tmpJ;
				LogisticsComponent* pComp = pVariant->getCompAtLocation(tmpI, tmpJ, selI, selJ );
				if ( pComp )
				{
					pSelectedComponent = pComp;
					setComponent( pComp );
					soundSystem->playDigitalSample( LOG_SELECT );
					componentListBox.SelectItem( -1 );
				}
			}
		}		
	}

	if ( userInput->leftMouseReleased() && pDragComponent )
			endDrag();


}