Layout Manager

A. First Edition
This is first edition of my Layout Manager. And it is quite hard to imagine its usefulness.
B.The problem
In Comp354 we need a kind of text mode displayer to show the board, score bar etc. And is it
 
convenient if we can have a kind of display layout manager to universally handle all displaying
 
job?
 
C.The idea of program
 
In text mode to display something it is a tedious job as you have to calculate the cell on screen.
 
My idea is that if we can have a sort of manager to draw the whole screen for a set of objects which
 
are all rectangle ones and the manager will at each rectangle starting coordinate call the
 
rectangle's displaying method, say "displayRow()". Then we can simply place all rectangle at any
 
place we want. And for each rectangle, it is easy to implement a line-by-line display method.
 
Each rectangle should inherited from an abstract class---"Display" which defined two pure virtual
 
method:"display()" and "displayRow()". The displayRow will return an integer to indicate how many
 
line left to finish display the rectangle.
 
Layout class is just a kind of scheduler who draw the background of screen and check if its cursor
 
enters one of "rectangle" where he should call the rectangle's "displayRow" method. After exiting
 
the area of rectangle, Layout should continue to draw the background.
D.The major functions
E.Further improvement
F.File listing
1. layout.h
2. layout.cpp  
3. main.cpp (main)
 
file name: layout.h
const int BoardSize=10;
const int ScreenWidth=70;
const int ScreenHeight=40;
const int MaxDisplayCount=5;
const char DefaultChar='*';

struct Coord
{
	int x;
	int y;
};

class Display
{
protected:
	Coord topLeft;
	Coord bottomRight;
	void setTop(int row, int col);
	void setBottom(int row, int col);
	int rowLeft;
public:
	
	virtual int displayRow()=0;
	virtual void display()=0;
	const Coord& getTop(){ return topLeft;}
	const Coord& getBottom(){ return bottomRight;}
};

class Board: public Display
{
protected:
	char board[BoardSize][BoardSize];
	void initialize(int x, int y);
	void setOrigin(int row, int col){ initialize(col, row);}
	void setBoard();
public:
	Board(int x, int y);
	virtual int displayRow();
	virtual void display();
};

class Layout
{
protected:
	Display* items[MaxDisplayCount];
	int itemCount;
	bool validate(Display* newItem);
	void defaultDraw();
	bool startDrawing(int row,int col, int& index);
public:
	Layout();
	void addItem(Display* item);
	void display();
};








file name: layout.cpp 
#include <iostream>
#include "Layout.h"

using namespace std;

void Display::setBottom(int row, int col)
{
	bottomRight.x=col;
	bottomRight.y=row;
}

void Display::setTop(int row, int col)
{
	topLeft.x=col;
	topLeft.y=row;
}

Board::Board(int x, int y)
{
	initialize(x, y);
}

void Board::initialize(int x, int y)
{
	Display::topLeft.x =x;
	Display::topLeft.y =y;
	Display::bottomRight.x=x+BoardSize;
	Display::bottomRight.y=y+BoardSize;
	rowLeft=BoardSize;
	setBoard();
}

void Board::setBoard()
{
	for (int r=0; r<BoardSize; r++)
	{
		for (int c=0; c<BoardSize; c++)
		{
			board[c][r]='b'+c+r;
		}
	}
}

int Board::displayRow()
{
	for (int c=0; c<BoardSize; c++)
	{
		cout<<board[c][BoardSize-rowLeft];
	}
	return --rowLeft;
}

void Board::display()
{
	for (int r=0; r<BoardSize; r++)
	{
		for (int c=0; c<BoardSize; c++)
		{
			cout<<board[c][r];
		}
	}
}

Layout::Layout()
{
	itemCount=0;
}

bool Layout::validate(Display* newItem)
{
	return true;
}
	
void Layout::addItem(Display* item)
{
	if (validate(item))
	{
		if (itemCount<MaxDisplayCount)
		{
			items[itemCount]=item;
			itemCount++;
		}
	}
}

void Layout::display()
{
	bool drawing;
	int count, index, rowLeft=-1;
	for (int r=0; r<ScreenHeight; r++)
	{
		drawing=false;
		for (int c=0; c<ScreenWidth; c++)
		{
			if (!drawing)
			{
				if (startDrawing(r,c, index))
				{
					drawing=true;
					rowLeft=items[index]->displayRow();
					count=items[index]->getBottom().x-items[index]->getTop().x;
					count--;
				}
				else
				{
					defaultDraw();
				}
			}
			else
			{
				if (rowLeft>=0)
				{
					count--;
					if (count==0)
					{
						if (rowLeft==0)
						{
							rowLeft=-1;
						}
						drawing=false;
					}					
				}
				else
				{
					drawing=false;
				}
			}
		}
		cout<<endl;
	}
}

bool Layout::startDrawing(int row, int col, int& index)
{
	for (index=0; index<itemCount; index++)
	{
		if (row>=items[index]->getTop().y&&row<items[index]->getBottom().y
			&&col>=items[index]->getTop().x&&col<items[index]->getBottom().x)
		{
			return true;
		}
	}
	return false;
}


void Layout::defaultDraw()
{
	cout<<DefaultChar;
}




file name: main.cpp (main)
#include <iostream>
#include "layout.h"

using namespace std;

int main()
{

	Display* D=new Board(10, 10);
	Display* E=new Board(40, 11);
	Display* F=new Board(55, 15);
	Layout L;
	L.addItem(D);
	L.addItem(E);
	L.addItem(F);
	L.display();
	return 0;
}
 
 





Here is the result: Do you know what this means? It means that we have three board which is 10x10. And we
want it to be placed on some particular co-ordinate. As long as the Board or what ever "rectangle" object
is inherited from abstract class "Display", we can easily display them by setting up its top-left and bottom-
right coordinate.  
 
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********bcdefghijk**************************************************
**********cdefghijkl********************bcdefghijk********************
**********defghijklm********************cdefghijkl********************
**********efghijklmn********************defghijklm********************
**********fghijklmno********************efghijklmn********************
**********ghijklmnop********************fghijklmno*****bcdefghijk*****
**********hijklmnopq********************ghijklmnop*****cdefghijkl*****
**********ijklmnopqr********************hijklmnopq*****defghijklm*****
**********jklmnopqrs********************ijklmnopqr*****efghijklmn*****
**********klmnopqrst********************jklmnopqrs*****fghijklmno*****
****************************************klmnopqrst*****ghijklmnop*****
*******************************************************hijklmnopq*****
*******************************************************ijklmnopqr*****
*******************************************************jklmnopqrs*****
*******************************************************klmnopqrst*****
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
Press any key to continue





                                 back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)