Layout Manager
A. First Edition
This is first edition of my Layout Manager. And it is quite hard to imagine its usefulness.
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