/*
	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.
*/

#ifndef GUIGALAXYMAP_H
#define GUIGALAXYMAP_H

//##############################################################################
// C Includes
//##############################################################################

#include <Windows.h>
#include <gl/gl.h>
#include <cmath>

//##############################################################################
// Forward references
//##############################################################################

class Game;
class Galaxy;
class GLFontManager;
class GuiScreen;
class Player;
class GameObjectHook;
class GameObject_Ship;
class SoundManager;
class SoundSystem;

//##############################################################################
// C++ Includes: cant forward reference these because the compiler complains
//##############################################################################

#include "GuiWidget.h"
#include "StarSystem.h"

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   GuiGalaxyMap
// Purpose:      A little widget that draws the galaxy map. also allows star
//               system information to be viewed as well as allowing the player
//               to travel to other star systems in the galaxy.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class GuiGalaxyMap : public GuiWidget
{

 public:

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------

        // Constructor: needs pointer to current game and gui screen

        GuiGalaxyMap( Game * game , GuiScreen * screen , bool travelEnabled );

        // Draw event: draws the map

        void windowRedrawing();

        // Widget mouse events:

        void mouseMoved     ( int x , int y );
        void mousePressed   ( int x , int y , GLWindow::MouseButton button );
        void mouseReleased  ( int x , int y , GLWindow::MouseButton button );

        // Lost focus event:

        void lostFocus();

        // Ceneters the map view over the system the player is currently in:

        void centerView();

        // Get the currently selected star system in the map: returns a null system if none is selected

        inline StarSystem getSelectedSystem(){ return selectedSystem; }

        // Gets the distance in light years from the players system to the selected system:

        double getDistanceToSelectedSystem();

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

 protected:


        //----------------------------------------------------------------------
        // Constants
        //----------------------------------------------------------------------

        static const float BACKGROUND_R;    // background color for map: r
        static const float BACKGROUND_G;    // background color for map: g
        static const float BACKGROUND_B;    // background color for map: b

        static const float GRID_LINE_R;     // map grid line color: r
        static const float GRID_LINE_G;     // map grid line color: g
        static const float GRID_LINE_B;     // map grid line color: b

        static const unsigned STAR_RADIUS;  // radius of stars in the display

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------

        // Draw all the grid lines in the map view:

        void drawGridLines();

        // Draw all the star systems in the map view:

        void drawStarSystems();

        // Draw the range of the players ship hyperspace engines

        void drawHyperspaceRange();

        // Draw the home button:

        void drawHomeButton();

        // Checks to see what system, if any is selected by the player and sets the selected system:

        void checkSelectedSystem( int mouseX , int mouseY );
        
        // Computes the coordinates of the screen space rectangle to clip mouse movement to for the map
        
        void getMouseDragBoxScreenCoords( int & centerX, int & centerY, int & halfSizeX, int & halfSizeY );

        //----------------------------------------------------------------------
        // Variables
        //----------------------------------------------------------------------

        // game who's galaxy will be shown in the map

        Game * game;

        // Gui screen this widget belongs to:

        GuiScreen * guiScreen;

        // Is the view being dragged ?

        bool dragging;

        // Is the mouse over the home button ?

        bool homeButtonSelected;

        // Allow travel from this map ?

        bool travelEnabled;

        // Star system currently selected by the player:

        StarSystem selectedSystem;

        // Position of the mouse before dragging of the view commenced:

        int mouseBeforeDragX;
        int mouseBeforeDragY;

        // position of the top left corner of galaxay map view in the galaxy:

        unsigned mapX;
        unsigned mapY;
};

//##############################################################################
// C++ Includes
//##############################################################################

#include "Game.h"
#include "Galaxy.h"
#include "GLFontManager.h"
#include "GuiScreen.h"
#include "Player.h"
#include "GameObjectHook.h"
#include "GameObject_Ship.h"
#include "SoundManager.h"
#include "SoundSystem.h"

#endif // ifndef GUIGALAXYMAP_H
