void MechLabScreen::removeComponent( long i, long j )
{

	long tmpX, tmpY;
	LogisticsComponent* pComp = pVariant->getCompAtLocation(i, j, tmpX, tmpY );
	if ( pComp )
	{
		if ( !pComp->isAvailable() )
		{
			LogisticsOneButtonDialog::instance()->setText(IDS_MC_UNAVAILABLE_TECH, IDS_DIALOG_OK, IDS_DIALOG_OK);
			LogisticsOneButtonDialog::instance()->begin();
			bErrorDlg = true;
		}
	}
	long tmpHeat = pVariant->getHeat();
	long tmpArmor = pVariant->getArmor();
	pVariant->removeComponent( i, j );

	if ( tmpHeat != pVariant->getHeat() )
	{
		oldHeat = tmpHeat;
		heatTime = .0001f;
	}
	else if ( tmpArmor != pVariant->getArmor() )
	{
		oldArmor = tmpArmor;
		armorTime = .0001f;
	}
	updateDiagram();
			
}
CustomView::CustomView(QWidget *parent) :
    QCustomPlot(parent)
{    
    this->addGraph();
    this->graph(0)->setPen(QColor(50, 50, 50, 255));
    this->graph(0)->setLineStyle(QCPGraph::lsNone);
    this->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));


    ellipse = new QCPItemEllipse(this);
    ellipse->setPen(QColor(255, 0, 0, 255));
    ellipse->setBrush(QBrush(QColor(255, 0, 0, 32)));
    this->addItem(ellipse);

    ellipseLM = new QCPItemEllipse(this);
    ellipseLM->setPen(QColor(255, 0, 0, 255));
    ellipseLM->setBrush(QBrush(QColor(0, 255, 0, 64)));
    this->addItem(ellipseLM);

    xAxisType = SPEED;
    yAxisType = VELOCITY;
    colorization = NONE;

    updateDiagram();
}
int MechLabScreen::addComponent( LogisticsComponent* pComponent, long& x, long& y )
{
	if ( !pComponent )
		return -1;

	int retVal = -1;

	if ( pComponent->getHeat() || pComponent->getType() == COMPONENT_FORM_HEATSINK )
	{
		oldHeat = pVariant->getHeat();
		if ( pComponent->getType() == COMPONENT_FORM_HEATSINK )
		{
			oldHeat -= 1;
			flashHeatRange = true;
		}
		else
			flashHeatRange = false;
		heatTime = .0001f;
	}
	else if ( pComponent->getType() == COMPONENT_FORM_BULK )
	{
		oldArmor = pVariant->getArmor();
		armorTime = .0001f;
	}
	
	if ( pComponent )
		retVal = pVariant->addComponent( pComponent, x, y );

	updateDiagram();

	return retVal;

}
void MechLabScreen::endDrag( )
{

	float mouseX = userInput->getMouseX();
	float mouseY = userInput->getMouseY();
	if ( pDragComponent && rects[13].pointInside( mouseX, mouseY ) )
	{
		long x, y;

		// use selRect, it already has proper coords
		getMouseDiagramCoords( selRect->left(), selRect->top(), x, y );
		
		if ( x == -2 && pDragComponent->getType() == COMPONENT_FORM_JUMPJET )
		{
			x = -2; 
			y = -2;
			addComponent( pDragComponent, x, y );				
		}
		else
		{
			if ( NO_ERR == addComponent( pDragComponent, x, y) )
			{
				componentListBox.SelectItem( -1 );
				selI = x;
				selJ = y;
				pSelectedComponent = pDragComponent;
			}
			if ( bDragLeft ) // put it bag, if we didn't drag far enough away
			{
				if ( NO_ERR == addComponent( pDragComponent, selI, selJ) )
				{
					componentListBox.SelectItem( -1 );
					pSelectedComponent = pDragComponent;
				}
			}
		}

		updateDiagram();
 	
	}
	else
	{
		if ( -1 == componentListBox.GetSelectedItem() )
		{
			if ( -1 == selI )
				selectFirstLBComponent();
		}
	}

	pDragComponent = NULL;
	bDragLeft = 0;

	
}
void CustomView::contextMenuEvent(QContextMenuEvent *event) {
    QMenu xMenu;
    xMenu.setTitle(tr("x-Axis"));
    QAction *xSpeed = xMenu.addAction(tr("Speed"));
    QAction *xVelocity = xMenu.addAction(tr("Velocity"));
    QAction *xOverlap = xMenu.addAction(tr("Overlap"));
    QAction *xOffset = xMenu.addAction(tr("Offset"));

    QMenu yMenu;
    yMenu.setTitle(tr("y-Axis"));
    QAction *ySpeed = yMenu.addAction(tr("Speed"));
    QAction *yVelocity = yMenu.addAction(tr("Velocity"));
    QAction *yOverlap = yMenu.addAction(tr("Overlap"));
    QAction *yOffset = yMenu.addAction(tr("Offset"));

    QMenu colorizationMenu;
    colorizationMenu.setTitle(tr("Colorization"));
    QAction *noneColorization = colorizationMenu.addAction(tr("None"));
    QAction *fingerColorization = colorizationMenu.addAction(tr("Finger"));

    QMenu contextMenu;
    contextMenu.addMenu(&xMenu);
    contextMenu.addMenu(&yMenu);
    contextMenu.addMenu(&colorizationMenu);

    QAction* selectedItem = contextMenu.exec(event->pos());
    if (selectedItem) {
        if (selectedItem == xSpeed) {
            xAxisType = SPEED;
        } else if (selectedItem == xVelocity) {
            xAxisType = VELOCITY;
        } else if (selectedItem == xOverlap) {
            xAxisType = OVERLAP;
        } else if (selectedItem == xOffset) {
            xAxisType = OFFSET;
        } else if (selectedItem == ySpeed) {
            yAxisType = SPEED;
        } else if (selectedItem == yVelocity) {
            yAxisType = VELOCITY;
        } else if (selectedItem == yOverlap) {
            yAxisType = OVERLAP;
        } else if (selectedItem == yOffset) {
            yAxisType = OFFSET;
        } else if (selectedItem == noneColorization) {
            colorization = NONE;
        } else if (selectedItem == fingerColorization) {
            colorization = FINGER;
        }
    }

    updateDiagram();
}
void MechLabScreen::swapVariant()
{
	// variant changed
	variantList.EditBox().getEntry(varName);

	LogisticsVariant* pNewVariant = LogisticsData::instance->getVariant( varName );
	if ( pVariant && pNewVariant)
	{
		EString oldName = pVariant->getName();
		*pVariant = *pNewVariant;
		pVariant->setName( oldName );
	}
	updateDiagram();
	selectFirstDiagramComponent();
}
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();


}
void MechLabScreen::begin()
{
	componentCount = 0;
	bSaveDlg = 0;
	status = RUNNING;
	bDragLeft= 0;
	bErrorDlg = 0;

	if ( !pVariant ) // if we are coming directly from the main menu
	{
		pVariant = LogisticsData::instance->getMechToModify()->getVariant();

		variantList.ListBox().removeAllItems(true);

		int maxCount = 0;
		LogisticsVariant** pVar = NULL;
		int addedCount = 0;

		LogisticsData::instance->getChassisVariants( pVariant->getChassis(),pVar, maxCount );
		if ( maxCount )
		{
			maxCount ++;
			pVar = (LogisticsVariant**)_alloca( maxCount* sizeof (LogisticsVariant*) );
			LogisticsData::instance->getChassisVariants( pVariant->getChassis(),pVar, maxCount );

			for ( int i = 0; i < maxCount; i++ )
			{
				if ( pVar[i]->allComponentsAvailable() )
				{
					variantList.AddItem( pVar[i]->getName(), rects[1].getColor() );
					if ( pVar[i]->getName().Compare( pVariant->getName() )  == 0 )
					{
						variantList.SelectItem( i );
						variantList.EditBox().setEntry( pVar[i]->getName() );
					}

					addedCount++;
				}
			}

		}
		if ( !addedCount ) // this is a mech we don't have access to yet
		{
			variantList.AddItem( pVariant->getName(), rects[1].getColor() );
			variantList.SelectItem( 0 );
			variantList.EditBox().setEntry(pVariant->getName() );

		}
		


		

		// no changes, set cost to 0
		textObjects[5].setText( "0" );
	}
	

	if ( pVariant )
	{
		textObjects[3].setText( pVariant->getChassisName() );
		originalCost = pVariant->getCost();


		char path[256];
		strcpy( path, artPath );
		strcat( path, "MCL_MC_" );
		char mechName[64];
		EString fileName = pVariant->getFileName( );
		_splitpath( fileName, NULL, NULL, mechName, NULL );
		strcat( path, mechName );
		strcat( path, "_B.tga" );
		CharLower( path );

		for ( int i = 51; i < 54; i++ )
			statics[i].setTexture( path );

		strcpy( path, artPath );
		strcat( path, "MCL_MC_" );
		strcat( path, mechName );
		strcat( path, "_A.tga" );
		CharLower( path );

		statics[50].setTexture( path );


		for ( i = COMPONENT_FORM_WEAPON_ENERGY; i < COMPONENT_FORM_JUMPJET + 1; i++ )
		{
			aButton* pButton = getButton( i );
			if ( pButton )
			{
				pButton->press( 0 );
			}
		}
		componentListBox.removeAllItems( 0 );

		componentListBox.setType( COMPONENT_FORM_WEAPON_BALLISTIC, -1, -1 );
		getButton( COMPONENT_FORM_WEAPON_BALLISTIC )->press( true );

		updateDiagram();

		if ( -1 == selectFirstDiagramComponent() )
			selectFirstLBComponent();

		oldCBillsAmount = 0;
	}

	// init CBills
	char text[32];
	sprintf( text, "%ld ", LogisticsData::instance->getCBills() );
	textObjects[1].setText( text );


	variantList.EditBox().getEntry(varName);

	if ( pVariant->getChassis()->jumpJetsAllowed() )
	{
		statics[54].showGUIWindow( true );
	}
	else
		statics[54].showGUIWindow( false );



}
int	MechLabScreen::handleMessage( unsigned long msg, unsigned long who)
{
	int i;

	if ( msg == aMSG_SELCHANGED )
	{
		swapVariant(); 
		return 1;
	}
	switch( who )
	{
		case MB_MSG_NEXT: // actuall accept
			{
				// make sure we have the money...
				float newCost = pVariant->getCost();
				// figure out change
				float costChange = newCost - originalCost;

				if ( costChange > LogisticsData::instance->getCBills() )
				{
					LogisticsOneButtonDialog::instance()->setText( IDS_MC_INSUFFICIENT_CBILLS, IDS_DIALOG_OK,
						IDS_DIALOG_OK );
					LogisticsOneButtonDialog::instance()->begin();
					bErrorDlg = true;
				}
				else
				{				
					// see if it's the same as the current variant...
					LogisticsVariant* pCurVar = LogisticsData::instance->getVariant( varName );
					if ( pCurVar && *pCurVar == *pVariant )
					{
						LogisticsData::instance->acceptMechModificationsUseOldVariant( pCurVar->getName() );
						status = UP;
					}
					else
					{	
						if ( pCurVar && !pCurVar->allComponentsAvailable() )
							pCurDialog = acceptDlg;
						else if ( pVariant && !pVariant->allComponentsAvailable() )
							pCurDialog = acceptDlg;
						else
							pCurDialog = saveDlg; 
						
						pCurDialog->begin();
						bSaveDlg = true;
					}
				}
			}
			break;
		case MB_MSG_PREV: // actually cancel
			LogisticsData::instance->cancelMechModfications();
			pVariant = NULL;
			status = UP;
			break;

		case	COMPONENT_FORM_WEAPON_ENERGY:
		case	COMPONENT_FORM_WEAPON_BALLISTIC:
		case	COMPONENT_FORM_WEAPON_MISSILE:
			for ( i = COMPONENT_FORM_WEAPON_ENERGY; i < COMPONENT_FORM_JUMPJET + 1; i++ )
			{
				aButton* pButton = getButton( i );
				if ( pButton )
				{
					pButton->press( 0 );
				}
			}
			getButton( who )->press( 1 );
			componentListBox.setType( who, -1, -1);
			break;
		case	COMPONENT_FORM_JUMPJET:
			for ( i = COMPONENT_FORM_WEAPON_ENERGY; i < COMPONENT_FORM_JUMPJET + 1; i++ )
			{
				aButton* pButton = getButton( i );
				if ( pButton )
				{
					pButton->press( 0 );
				}
			}
			getButton( who )->press( 1 );
			componentListBox.setType( COMPONENT_FORM_JUMPJET, COMPONENT_FORM_BULK, COMPONENT_FORM_HEATSINK );
			break;

		case MB_MSG_ADD:
			{
			long x = -1; 
			long y = -1;
			addComponent( componentListBox.getComponent(), x, y );
			}
			break;

		case MB_MSG_REMOVE:
			if ( pSelectedComponent && (selI != -1 && selJ != -1) )
			{
				removeComponent( selI, selJ );
				pSelectedComponent = 0;
				if ( -1 == selectFirstDiagramComponent() )
					selectFirstLBComponent();
			}

			updateDiagram();

			break;

		case MB_MSG_MAINMENU:
			status = MAINMENU;
			break;



	
	}

	return 0;
}