/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   GuiUpgrade
// Purpose:      Widget that handles the buying and selling of upgrades
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GuiUpgrade.h"

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::GuiUpgrade()
Purpose:  Constructor.
Params:   Pointer to the game.
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

GuiUpgrade::GuiUpgrade( Game * game )
{
    // Save the pointer to the game:

    this->game = game;

    // Set the positions of the buy / sell missile and emp buttons:

    buttonBuyMissile.setLabel("Buy");
    buttonBuyEmp.setLabel("Buy");
    buttonSellMissile.setLabel("Sell");
    buttonSellEmp.setLabel("Sell");

    buttonBuyMissile.setSize(92,32);
    buttonBuyEmp.setSize(92,32);
    buttonSellMissile.setSize(92,32);
    buttonSellEmp.setSize(92,32);

    buttonBuyMissile.setPosition(288,172);
    buttonBuyEmp.setPosition(288,376);
    buttonSellMissile.setPosition(384,172);
    buttonSellEmp.setPosition(384,376);
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::mouseMoved()
Purpose:  Mouse moved event for the widget
Params:   new mouse position
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiUpgrade::mouseMoved( int x , int y )
{
    //--------------------------------------------------------------------------
    // See if the mouse is over the buy missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyMissile.getPositionX()
            &&
            y >= buttonBuyMissile.getPositionY()
            &&
            x <  buttonBuyMissile.getPositionX() + buttonBuyMissile.getSizeX()
            &&
            y <  buttonBuyMissile.getPositionY() + buttonBuyMissile.getSizeY()
        )
    {
        // If the button is not in focus then send the gained focus event:

        if ( buttonBuyMissile.getFocus() == false )
        {
            buttonBuyMissile.setFocus(true);
            buttonBuyMissile.gainedFocus(x,y);
        }

        // Send the mouse moved event to the button:

        buttonBuyMissile.mouseMoved(x,y);
    }
    else
    {
        // If the button has focus then make it loose it:

        if ( buttonBuyMissile.getFocus() == true )
        {
            buttonBuyMissile.setFocus(false);
            buttonBuyMissile.lostFocus();
        }
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellMissile.getPositionX()
            &&
            y >= buttonSellMissile.getPositionY()
            &&
            x <  buttonSellMissile.getPositionX() + buttonSellMissile.getSizeX()
            &&
            y <  buttonSellMissile.getPositionY() + buttonSellMissile.getSizeY()
        )
    {
        // If the button is not in focus then send the gained focus event:

        if ( buttonSellMissile.getFocus() == false )
        {
            buttonSellMissile.setFocus(true);
            buttonSellMissile.gainedFocus(x,y);
        }

        // Send the mouse moved event to the button:

        buttonSellMissile.mouseMoved(x,y);
    }
    else
    {
        // If the button has focus then make it loose it:

        if ( buttonSellMissile.getFocus() == true )
        {
            buttonSellMissile.setFocus(false);
            buttonSellMissile.lostFocus();
        }
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the buy emp button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyEmp.getPositionX()
            &&
            y >= buttonBuyEmp.getPositionY()
            &&
            x <  buttonBuyEmp.getPositionX() + buttonBuyEmp.getSizeX()
            &&
            y <  buttonBuyEmp.getPositionY() + buttonBuyEmp.getSizeY()
        )
    {
        // If the button is not in focus then send the gained focus event:

        if ( buttonBuyEmp.getFocus() == false )
        {
            buttonBuyEmp.setFocus(true);
            buttonBuyEmp.gainedFocus(x,y);
        }

        // Send the mouse moved event to the button:

        buttonBuyEmp.mouseMoved(x,y);
    }
    else
    {
        // If the button has focus then make it loose it:

        if ( buttonBuyEmp.getFocus() == true )
        {
            buttonBuyEmp.setFocus(false);
            buttonBuyEmp.lostFocus();
        }
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell emp button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellEmp.getPositionX()
            &&
            y >= buttonSellEmp.getPositionY()
            &&
            x <  buttonSellEmp.getPositionX() + buttonSellEmp.getSizeX()
            &&
            y <  buttonSellEmp.getPositionY() + buttonSellEmp.getSizeY()
        )
    {
        // If the button is not in focus then send the gained focus event:

        if ( buttonSellEmp.getFocus() == false )
        {
            buttonSellEmp.setFocus(true);
            buttonSellEmp.gainedFocus(x,y);
        }

        // Send the mouse moved event to the button:

        buttonSellEmp.mouseMoved(x,y);
    }
    else
    {
        // If the button has focus then make it loose it:

        if ( buttonSellEmp.getFocus() == true )
        {
            buttonSellEmp.setFocus(false);
            buttonSellEmp.lostFocus();
        }
    }
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::mousePressed()
Purpose:  Mouse pressed event for this widget.
Params:   Mouse position, button pressed
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiUpgrade::mousePressed( int x , int y , GLWindow::MouseButton button )
{
    //--------------------------------------------------------------------------
    // See if the mouse is over the buy missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyMissile.getPositionX()
            &&
            y >= buttonBuyMissile.getPositionY()
            &&
            x <  buttonBuyMissile.getPositionX() + buttonBuyMissile.getSizeX()
            &&
            y <  buttonBuyMissile.getPositionY() + buttonBuyMissile.getSizeY()
        )
    {
        buttonBuyMissile.mousePressed(x,y,button);
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellMissile.getPositionX()
            &&
            y >= buttonSellMissile.getPositionY()
            &&
            x <  buttonSellMissile.getPositionX() + buttonSellMissile.getSizeX()
            &&
            y <  buttonSellMissile.getPositionY() + buttonSellMissile.getSizeY()
        )
    {
        buttonSellMissile.mousePressed(x,y,button);
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the buy emp button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyEmp.getPositionX()
            &&
            y >= buttonBuyEmp.getPositionY()
            &&
            x <  buttonBuyEmp.getPositionX() + buttonBuyEmp.getSizeX()
            &&
            y <  buttonBuyEmp.getPositionY() + buttonBuyEmp.getSizeY()
        )
    {
        // Send the mouse pressed event to this button

        buttonBuyEmp.mousePressed(x,y,button);
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell emp button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellEmp.getPositionX()
            &&
            y >= buttonSellEmp.getPositionY()
            &&
            x <  buttonSellEmp.getPositionX() + buttonSellEmp.getSizeX()
            &&
            y <  buttonSellEmp.getPositionY() + buttonSellEmp.getSizeY()
        )
    {
        // Send the mouse pressed event to this button

        buttonSellEmp.mousePressed(x,y,button);
    }
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::mouseReleased()
Purpose:  Mouse released event for this widget.
Params:   Mouse position, button pressed
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiUpgrade::mouseReleased( int x , int y , GLWindow::MouseButton button )
{
    //--------------------------------------------------------------------------
    // Get the player from the game:
    //--------------------------------------------------------------------------

    Player * player = game->getPlayer();

    //--------------------------------------------------------------------------
    // See if the mouse is over the buy missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyMissile.getPositionX()
            &&
            y >= buttonBuyMissile.getPositionY()
            &&
            x <  buttonBuyMissile.getPositionX() + buttonBuyMissile.getSizeX()
            &&
            y <  buttonBuyMissile.getPositionY() + buttonBuyMissile.getSizeY()
        )
    {
        //----------------------------------------------------------------------
        // If the mouse was released over the button when it was depressed then
        // try to buy a missile for the player
        //----------------------------------------------------------------------

        if ( buttonBuyMissile.isDepressed() )
        {
            //------------------------------------------------------------------
            // Get the players ship first:
            //------------------------------------------------------------------

            GameObject_Ship * ship = (GameObject_Ship*) game->getPlayer()->getShipHook()->getObject();

            //------------------------------------------------------------------
            // See if it exists:
            //------------------------------------------------------------------

            if ( ship )
            {
                // See if the player has enough money to buy a missile:

                if ( player->getCredits() >= Game::MISSILE_COST )
                {
                    // See if there is room on the players ship for another missile:

                    if ( ship->getNumMissiles() < ship->getMaxMissiles() )
                    {
                        // All good: subtract the cost of a missile from the players credits:

                        player->setCredits( player->getCredits() - Game::MISSILE_COST );

                        // Add an extra missile to the players ship:

                        ship->setNumMissiles( ship->getNumMissiles() + 1 );

                        // Play the trade buy/sell noise

                        SoundSystem::playSound( SoundManager::getSndTradeBuySell() );
                    }
                    else
                    {
                        // No room for any more missiles: warn player

                        GuiScreen::showMessageDialog("Cannot buy missile","Your ship cannot hold any more missiles.");
                    }
                }
                else
                {
                    // Player hasnt enough money for missile: warn

                    GuiScreen::showMessageDialog("Cannot buy missile","You do not have enough credits.");
                }

            }   // end if player ship exists

            //------------------------------------------------------------------
            // Send the mouse released event to the buy missile button:
            //------------------------------------------------------------------

            buttonBuyMissile.mouseReleased(x,y,button);

        }   // end if button is depressed

    }   // end if mouse released over button

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell missile button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellMissile.getPositionX()
            &&
            y >= buttonSellMissile.getPositionY()
            &&
            x <  buttonSellMissile.getPositionX() + buttonSellMissile.getSizeX()
            &&
            y <  buttonSellMissile.getPositionY() + buttonSellMissile.getSizeY()
        )
    {
        //----------------------------------------------------------------------
        // If the mouse was released over the button when it was depressed then
        // try to sell a missile for the player
        //----------------------------------------------------------------------

        if ( buttonSellMissile.isDepressed() )
        {
            //------------------------------------------------------------------
            // Get the players ship first:
            //------------------------------------------------------------------

            GameObject_Ship * ship = (GameObject_Ship*) game->getPlayer()->getShipHook()->getObject();

            //------------------------------------------------------------------
            // See if it exists:
            //------------------------------------------------------------------

            if ( ship )
            {
                // See if the players ship has any missiles:

                if ( ship->getNumMissiles() > 0 )
                {
                    // Reduce the players missiles by 1

                    ship->setNumMissiles( ship->getNumMissiles() - 1 );

                    // Give the player the cost of a missile:

                    player->setCredits( player->getCredits() + Game::MISSILE_COST );

                    // Play the trade buy/sell noise

                    SoundSystem::playSound( SoundManager::getSndTradeBuySell() );
                }
                else
                {
                    // Player has no missiles to sell:

                    GuiScreen::showMessageDialog("Cannot sell missile","You have no missiles on board.");
                }

            }   // end if player ship exists

            //------------------------------------------------------------------
            // Send the mouse released event to the sell missile button:
            //------------------------------------------------------------------

            buttonSellMissile.mouseReleased(x,y,button);
        }
    }

    //--------------------------------------------------------------------------
    // See if the mouse is over the buy emp bomb button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonBuyEmp.getPositionX()
            &&
            y >= buttonBuyEmp.getPositionY()
            &&
            x <  buttonBuyEmp.getPositionX() + buttonBuyEmp.getSizeX()
            &&
            y <  buttonBuyEmp.getPositionY() + buttonBuyEmp.getSizeY()
        )
    {
        //----------------------------------------------------------------------
        // If the mouse was released over the button when it was depressed then
        // try to buy an emp bomb for the player
        //----------------------------------------------------------------------

        if ( buttonBuyEmp.isDepressed() )
        {
            //------------------------------------------------------------------
            // Get the players ship first:
            //------------------------------------------------------------------

            GameObject_Ship * ship = (GameObject_Ship*) game->getPlayer()->getShipHook()->getObject();

            //------------------------------------------------------------------
            // See if it exists:
            //------------------------------------------------------------------

            if ( ship )
            {
                // See if the player has enough money to buy a missile:

                if ( player->getCredits() >= Game::EMP_COST )
                {
                    // See if there is room on the players ship for another emp bomb:

                    if ( ship->getNumEmpBombs() < ship->getMaxEmpBombs() )
                    {
                        // All good: subtract the cost of an emp bomb from the players credits:

                        player->setCredits( player->getCredits() - Game::EMP_COST );

                        // Add an extra emp bomb to the players ship:

                        ship->setNumEmpBombs( ship->getNumEmpBombs() + 1 );

                        // Play the trade buy/sell noise

                        SoundSystem::playSound( SoundManager::getSndTradeBuySell() );
                    }
                    else
                    {
                        // No room for any more emp bombs: warn player

                        GuiScreen::showMessageDialog("Cannot buy emp bomb","Your ship cannot hold any more emp bombs.");
                    }
                }
                else
                {
                    // Player hasnt enough money for emp bomb: warn

                    GuiScreen::showMessageDialog("Cannot buy emp bomb","You do not have enough credits.");
                }

            }   // end if player ship exists

            //------------------------------------------------------------------
            // Send the mouse released event to the buy emp button:
            //------------------------------------------------------------------

            buttonBuyEmp.mouseReleased(x,y,button);

        }   // end if button is depressed

    }   // end if mouse released over button

    //--------------------------------------------------------------------------
    // See if the mouse is over the sell emp button:
    //--------------------------------------------------------------------------

    if (
            x >= buttonSellEmp.getPositionX()
            &&
            y >= buttonSellEmp.getPositionY()
            &&
            x <  buttonSellEmp.getPositionX() + buttonSellEmp.getSizeX()
            &&
            y <  buttonSellEmp.getPositionY() + buttonSellEmp.getSizeY()
        )
    {
        //----------------------------------------------------------------------
        // If the mouse was released over the button when it was depressed then
        // try to sell a missile for the player
        //----------------------------------------------------------------------

        if ( buttonSellEmp.isDepressed() )
        {
            //------------------------------------------------------------------
            // Get the players ship first:
            //------------------------------------------------------------------

            GameObject_Ship * ship = (GameObject_Ship*) game->getPlayer()->getShipHook()->getObject();

            //------------------------------------------------------------------
            // See if it exists:
            //------------------------------------------------------------------

            if ( ship )
            {
                // See if the players ship has any emp bombs:

                if ( ship->getNumEmpBombs() > 0 )
                {
                    // Reduce the players emp bombs by 1

                    ship->setNumEmpBombs( ship->getNumEmpBombs() - 1 );

                    // Give the player the cost of a missile:

                    player->setCredits( player->getCredits() + Game::EMP_COST );

                    // Play the trade buy/sell noise

                    SoundSystem::playSound( SoundManager::getSndTradeBuySell() );
                }
                else
                {
                    // Player has no emp bombs to sell:

                    GuiScreen::showMessageDialog("Cannot sell emp bomb","You have no emp bombs on board.");
                }

            }   // end if player ship exists

            //------------------------------------------------------------------
            // Send the mouse released event to the sell emp button:
            //------------------------------------------------------------------

            buttonSellEmp.mouseReleased(x,y,button);
        }
    }
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::windowRedrawing()
Purpose:  Redraw event for the upgrade buy widget.
Params:   none
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiUpgrade::windowRedrawing()
{
    //--------------------------------------------------------------------------
    // Clear the widget background to black:
    //--------------------------------------------------------------------------

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    glColor4f
    (
        0,
        0,
        0,
        1
    );

    glBegin(GL_QUADS);

        glVertex2f( float( this->position_x					)	, float( this->position_y                 )	);
        glVertex2f( float( this->position_x + this->size_x  )	, float( this->position_y                 )	);
        glVertex2f( float( this->position_x + this->size_x  )	, float( this->position_y + this->size_y  )	);
        glVertex2f( float( this->position_x                 )	, float( this->position_y + this->size_y  )	);

    glEnd();

    //--------------------------------------------------------------------------
    // Get the big and small font from the font manager:
    //--------------------------------------------------------------------------

    // Get the big and small font

    GLFont * bigFont    = GLFontManager::getFont_big();
    GLFont * smallFont  = GLFontManager::getFont_main();

    // Use this buffer to hold stuff to print:

    char buffer[256];

    //--------------------------------------------------------------------------
    // Get the players ship from the game: abort drawing on failure
    //--------------------------------------------------------------------------

    // Get the player first and validate:

    PlayerHuman * player = game->getPlayer();

    if ( player == 0 ) return;

    // Get the players ship and validate:

    GameObject_Ship * ship = (GameObject_Ship*) player->getShipHook()->getObject();

    if ( ship == 0 ) return;

    //--------------------------------------------------------------------------
    // Draw the missile section of the screen
    //--------------------------------------------------------------------------

    // Draw the title

    bigFont->drawString
    (
        "Missile"							,
        float( this->position_x + 8		)	,
        float( this->position_y + 10	)	,
        16.0f	,
        1.0f	,
        1.0f	,
        1.0f	
    );

    // Draw a rule under this title

    rule.setPosition
    (
        this->position_x + 8 ,
        this->position_y + 34
    );

    rule.setSize
    (
        this->size_x - 16,
        4
    );

    rule.windowRedrawing();

    // Show the missile mesh:

    meshViewer.setSize(256,160);
    meshViewer.setPosition(this->position_x + 8, this->position_y + 42);
    meshViewer.setMesh(MeshManager::getMeshMissile());
    meshViewer.windowRedrawing();

    // Draw the buy and sell missile buttons

    buttonBuyMissile.windowRedrawing();
    buttonSellMissile.windowRedrawing();

    // Show the cost of the missile upgrade:

    sprintf_s(buffer, 256 , "Cost:      Cr %1.2f", Game::MISSILE_COST );

    smallFont->drawString
    (
        buffer								,
        float( this->position_x + 288	)	,
        float( this->position_y + 128	)	,
        8.0f	,
        1.0f	,
        1.0f	,
        1.0f
    );

    // Show how many missiles the player has on board:

    if ( ship->getMaxMissiles() >= 10 )
    {
        sprintf_s(buffer, 256 , "On board:  %1i / %2i", ship->getNumMissiles() , ship->getMaxMissiles() );
    }
    else
    {
        // Sprintf sticks in an extra white space if the second number is less than 10 - why ?

        sprintf_s(buffer, 256 , "On board:  %1i /%2i", ship->getNumMissiles() , ship->getMaxMissiles() );
    }

    smallFont->drawString
    (
        buffer,
        float( this->position_x + 288	),
        float( this->position_y + 140	),
        8.0f,
        1.0f,
        1.0f,
        1.0f
    );

    //--------------------------------------------------------------------------
    // Draw the emp section of the screen
    //--------------------------------------------------------------------------

    // Draw a rule above the title

    rule.setPosition
    (
        this->position_x + 8 ,
        this->position_y + 208
    );

    rule.setSize
    (
        this->size_x - 16,
        4
    );

    rule.windowRedrawing();

    // Draw the title

    bigFont->drawString
    (
        "Emp"								,
        float( this->position_x + 8		)	,
        float( this->position_y + 216	)	,
        16.0f	,
        1.0f	,
        1.0f	,
        1.0f
    );

    // Draw a rule under this title

    rule.setPosition
    (
        this->position_x + 8 ,
        this->position_y + 240
    );

    rule.setSize
    (
        this->size_x - 16,
        4
    );

    rule.windowRedrawing();

    // Show the emp mesh:

    meshViewer.setSize(256,160);
    meshViewer.setPosition(this->position_x + 8, this->position_y + 248);
    meshViewer.setMesh(MeshManager::getMeshEmp());
    meshViewer.windowRedrawing();

    // Draw the buy and sell emp buttons

    buttonBuyEmp.windowRedrawing();
    buttonSellEmp.windowRedrawing();

    // Show the cost of the emp upgrade:

    sprintf_s(buffer,256,"Cost:      Cr %1.2f", Game::EMP_COST );

    smallFont->drawString
    (
        buffer								,
        float( this->position_x + 288	)	,
        float( this->position_y + 334	)	,
        8.0f	,
        1.0f	,
        1.0f	,
        1.0f
    );

    // Show how many emp bombs the player has on board:

    if ( ship->getMaxEmpBombs() >= 10 )
    {
        sprintf_s(buffer,256,"On board:  %1i / %2i", ship->getNumEmpBombs() , ship->getMaxEmpBombs() );
    }
    else
    {
        // Sprintf sticks in an extra white space if the second number is less than 10 - why ?

        sprintf_s(buffer,256,"On board:  %1i /%2i", ship->getNumEmpBombs() , ship->getMaxEmpBombs() );
    }

    smallFont->drawString
    (
        buffer								,
        float( this->position_x + 288	)	,
        float( this->position_y + 346	)	,
        8.0f	,
        1.0f	,
        1.0f	,
        1.0f	
    );

    //--------------------------------------------------------------------------
    // Show the players credits
    //--------------------------------------------------------------------------

    // Draw a rule under the emp section

    rule.setPosition
    (
        this->position_x + 8 ,
        this->position_y + 412
    );

    rule.setSize
    (
        this->size_x - 16,
        4
    );

    rule.windowRedrawing();

    // Show the credits icon:

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    GLTextureManager::getTexMoney()->bind();

    glBegin(GL_QUADS);

        glColor4f(1,1,1,1);

        glTexCoord2f(0,0);  glVertex2f( float( this->position_x + 8		) , float( this->position_y + 424 )	);
        glTexCoord2f(1,0);  glVertex2f( float( this->position_x + 40	) , float( this->position_y + 424 )	);
        glTexCoord2f(1,1);  glVertex2f( float( this->position_x + 40	) , float( this->position_y + 456 )	);
        glTexCoord2f(0,1);  glVertex2f( float( this->position_x + 8		) , float( this->position_y + 456 )	);

    glEnd();

    // Show the player's credits:

    sprintf_s( buffer, 256 , "Cr %1.2f", player->getCredits() );

    bigFont->drawString
    (
        buffer,
        float( this->position_x + 64	),
        float( this->position_y + 432	),
        16.0f,
        1.0f,
        1.0f,
        1.0f
    );
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiUpgrade::lostFocus()
Purpose:  Lost focus event for this widget.
Params:   none
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiUpgrade::lostFocus()
{
    // Cause all the buttons to loose focus:

    if ( buttonBuyMissile.getFocus() )
    {
        buttonBuyMissile.setFocus(false);
        buttonBuyMissile.lostFocus();
    }

    if ( buttonSellMissile.getFocus() )
    {
        buttonSellMissile.setFocus(false);
        buttonSellMissile.lostFocus();
    }

    if ( buttonBuyEmp.getFocus() )
    {
        buttonBuyEmp.setFocus(false);
        buttonBuyEmp.lostFocus();
    }

    if ( buttonSellEmp.getFocus() )
    {
        buttonSellEmp.setFocus(false);
        buttonSellEmp.lostFocus();
    }
}
