After I had a look again into my application, I was more confused because my class design is pretty crappy and confusing even for me. Because I needed a specific answer, so I'll try to explain my situation here.
I have a GUI of keyboard buttons in the screen with SDL. The buttons are images. If I mouse clicked the area inside the button, the corresponding button in the screen blinks. The buttons of alphabet, month or numbers weren't showed at the same time. There's an enumeration state of ALPHA, MONTH and NUMS for the current one being showed and active in the screen. Beside that, it will add a char to a string data. The data themself are consisting of 4 input textbox with each input box declaring a spesific state (let's call it NAME, MONTH, DAY, and YEAR. In the object that handles the data, there's a bitmap font to show it in the screen. And the last object is a blinking cursor like in a text editor. So basicaly I'm trying to make a similar UI like MFC but in SDL.
Here's what I've done (BTW, I'll just show you only the declaration of methods because the implementation of the methods are too many to write. Let's just assume that they worked because, well, they were. ):
The class for keyboard buttons
Code:
class CKeyboard
{
private:
std::map <char, CSprites *> mButtonsMap; //The map of buttons sprites
int mState; //ALPHA, MONTH and NUM state;
public:
CKeyboard(); //Constructor. This is were we instantiated the buttons and put it in the map.
~CKeyboard(); //Destructor
char checkClickArea(int xx, int yy); //Checks the click area for a button. Returned the char of the button if successful, or NULL if there's no button in the area. The area depends on the current active state.
void blinkButton(char button); //Blinking animation of the button
void setState(int state); //Set the active state
};
The class for character data
Code:
class CCharacterData
{
private:
CBitmapFont *mFont; //The current bitmap font
CSprites *mMonthSprite; //Image representation of the month
std::string mName; /Name
int mMonth; //Month: 1 - 12
std::string mDay; //Age
std::string mYear; //Job
bool nameShow; //To set whether the first name string will be displayed in the screen
bool monthShow; //To set whether the first name string will be displayed in the screen
bool dayShow; //To set whether the first name string will be displayed in the screen
bool jobShow; //To set whether the first name string will be displayed in the screen
int mActiveField; //Set the current active record that can be inputted. The options are NAME, MONTH, DAY, and YEAR.
public:
CCharacterData(CBitmapFont *font); //Constructor. Also set the current font
~CCharacter(); //Destructor
void nameAdd(char theChar); //Add one char to name
void nameDeleteLastChar(); //Delete the last char
int nameGetPixelLength(); //Get the pixel length of the name string to determine the current position of the cursor in name state
void monthPick(int month); //Pick the month
void monthDelete(); //Month = 0
int monthGetValue(); //Get the value of the month
void dayAdd(char theChar); //Add one char to day
void dayDeleteLastChar(); //Delete the last char
int dayGetPixelLength(); //Get the pixel length of the day string to determine the current position of the cursor in day state
void yearAdd(char theChar); //Add one char to year
void yearDeleteLastChar(); //Delete the last char
int yearGetPixelLength(); //Get the pixel length of the year string to determine the current position of the cursor in year state
void setVisible(int record, bool state); //State the showing boolean of the corresponding record
void setActiveField(int state); //Set the current active field
};
The cursor class:
Code:
class CCursor
{
private:
CSprites *mCursor; //The sprites of a blinking cursor
int mPosX; //Current X Position of the cursor
int mPosY; //Current Y Position of the cursor
const int mNamePosX; // name X Position constants
const int mNamePosY; // name Y Position constants
const int mMonthPosX; //month X Position constants
const int mMonthPosY; //month Y Position constants
const int mDayPosX; //day X Position constants
const int mDayPosY; //day Y Position constants
const int mYearPosX; //year X Position constants
const int mYearPosY; //year Y Position constants
int mState; //The current position state. The options are NAME, MONTH, DAY, and YEAR.
bool mShow; //Boolean for showing / hiding cursor
public:
CCursor(); //Constructor.
~CCursor(); //Destructor
inline void setState(int state); /Set the current state
inline int getState(); //Get the current state
inline void setVisible(bool show); //Set show /hide cursor
void moveCursor(int xx); //Move the cursor based on the current state of the cursor. If the state is NAME, the current x of the cursor will be (mNamePosX+xx) where xx = CharHandler->nameGetPixelLength() and the current y wil be mNamePosY, for month, in MONTH state the x and y position will be mMonthPosX and Y if =0, and invisible if !=0, etc.
};
In the main class:
Code:
class CMainApp
{
private:
int mState; //Current active record that can be inputted. The options are NAME, MONTH, DAY, and YEAR.
CKeyboard *mKeyb;
CCharacterData *mCharData;
CCursor *mCursor;
bool done; //The flag to indicate the quit parameter of the main loop
public:
CMainApp(); //Constructor
~CMainApp(); //Destructor
void mainLoop(); //The main loop of the program
void setState(int state); //Set the current state
....
};
void CMainApp::mainLoop()
{
while (!done)
{
if (eventMouseClick())
{
char mInputChar;
if(mInputChar=mKeyb->checkClickArea(getMousePos()->x,getMousePos()->y) != NULL)
{
switch(mState)
{
case 1:
mCharData->nameAdd(mInputChar);
mCursor->moveCursor(mCharData->nameGetPixelLength());
break;
case 2:
mCharData->monthPick(atoi(mInputChar));
if (mCharData->monthGetValue()!=0)
mCursor->setVisible(true); //Set show
else
mCursor->setVisible(false); //Set hide
break;
case 3:
mCharData->dayAdd(mInputChar);
mCursor->moveCursor(mCharData->dayGetPixelLength());
break;
case 4:
mCharData->jobAdd(mInputChar);
mCursor->moveCursor(mCharData->yearGetPixelLength());
break;
}
}
if (eventKeyPress())
{
if (getKey()==13)
{
if (mState!=4)
setState(mState+1);
else
setState(1);
}
}
draw(); //Draw with SDL
}
}
void CMainApp::setState(int state)
{
switch(state)
{
case 1:
mKeyb->setState(1); //ALPHA
mCharData->setActiveField(1); //NAME
mCursor->setState(1); //NAME
break;
case 2:
mKeyb->setState(2); //MONTH
mCharData->setActiveField(2); //MONTH
mCursor->setState(2); //MONTH
break;
case 3:
mKeyb->setState(3); //NUM
mCharData->setActiveField(3); //DAY
mCursor->setState(3); //DAY
break;
case 4:
mKeyb->setState(3); //NUM
mCharData->setActiveField(4); //YEAR
mCursor->setState(4); //YEAR
break;
}
}
As you can see my coding is pretty crappy. What I wanted to do is to make the char data, keyboard, and cursor classes to interact one another. What do you suggest to better my code? Any input will be appreciated. Thanks in advance.