// class to handle a viewport and parameters
#ifndef VIEWPORT_H
#define VIEWPORT_H
#include <cassert>
// handles conversion of zoom, position between world space and view space
class Viewport
	{
public:
	// default view
	void Reset(void)
		{
		zoom_ = 4;
		cx_ = 0;
		cy_ = 0;
		}
	Viewport(void)
		{
		Reset();
		}

	// get zoom ratio of cells to screen pixels
	// each value is log base 2
	void GetZoom(int & numerator, int & denominator) const 
		{ 
		if (zoom_ < 5)
			{
			numerator = 0;
			denominator = 5-zoom_;
			}
		else
			{
			numerator = zoom_-5;
			denominator = 0;
			}
		} // GetZoom

	// set zoom ratio of cells to screen pixels
	// each value is log base 2. One value must be 0
	void SetZoom(int numerator, int denominator) 
		{ 
		if (0 == numerator)
			{ 
			if (denominator >= 5) 
				denominator = 5;
			zoom_ = 5-denominator;
			}
		else if (0 == denominator)
			{
			zoom_ = numerator + 5;
			}
		else
			assert(!"Error in set zoom");
		} // GetZoom

	// in world coords, set center
	void SetCenter(int x, int y)
		{
		cx_ = x;
		cy_ = y;
		}

	// in world coords, get center
	void GetCenter(int & x, int & y) const
		{
		x = cx_;
		y = cy_;
		}

	// given the pixel size of the view screen, get the coords in world (i.e. cell) coords
	void GetViewportWorldCoords(int pixWidth, int pixHeight, int & x1, int & y1, int & x2, int & y2)
		{
		int num, den;
		GetZoom(num,den);
		num = 1<<num;
		den = 1<<den;
		x1 = ((cx_ - pixWidth*num/den/2));
		x2 = ((cx_ + pixWidth*num/den/2));
		y1 = ((cy_ - pixHeight*num/den/2));
		y2 = ((cy_ + pixHeight*num/den/2));
		} // GetViewportWorldCoords

	// given scroll steps, do it in proper coords
	void ScrollCenter(int dx, int dy)
		{
		int num, den;
		GetZoom(num,den);
		num = 1<<num;
		den = 1<<den;
		cx_ += dx*2*num/den; // todo - in pixels
		cy_ += dy*2*num/den;
		}

	// steps is power of 2
	void Zoom(int steps) 
		{ 
		if ((steps < 0) && (zoom_ >= -steps))
			zoom_ += steps; 
		else if (steps > 0)
			zoom_ += steps;
		}


private:
	// zoom is 1:32 (zoomed in pixels) to 256:1 (or more) (zoomed out)
	int zoom_;          // powers of 2 stores here, gives 1:16 to super big!
	int cx_, cy_;       // world coords on screen center
	}; // Viewport

#endif // VIEWPORT_H


// end - Viewport.h