00001
00002
00003
00004 # include "console.h"
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00022 Console::Console()
00023 {
00024 skinRegistered = false;
00025 }
00026
00027
00031 Console::~Console()
00032 {
00033 skinRegistered = false;
00034 }
00035
00036
00042 int Console::translateColor(int color)
00043 {
00044 if(color>=10) color = color -10;
00045 return color;
00046 }
00047
00048
00055 bool Console::isBrightColor(int color)
00056 {
00057 if(color>=10)
00058 {
00059 return true;
00060 }
00061 else
00062 {
00063 return false;
00064 }
00065 }
00066
00067
00068
00073 void Console::clearConsole(int bgcolor)
00074 {
00075 int setColor = translateColor(bgcolor);
00076
00077 if(isBrightColor(bgcolor)==true)
00078 {
00079 printf("\x1b[4%i;1m",setColor);
00080 }
00081 else
00082 {
00083 printf("\x1b[4%im",setColor);
00084 }
00085 printf("\x1b[2J");
00086 }
00087
00088
00098 void Console::clearArea(int startX,int startY, int rows, int cols, int bgcolor)
00099 {
00100
00101 int i,j;
00102 for(i=0;i<cols;i++)
00103 {
00104 for(j=1;j<rows;j++)
00105 {
00106 drawString(startX+i,startY+j,bgcolor,bgcolor," ");
00107 }
00108 }
00109
00110 }
00111
00112
00116 void Console::backspace()
00117 {
00118 printf("\b");
00119 }
00120
00121
00127 void Console::moveCursor (int row, int column)
00128 {
00129 printf("\x1b[%i;%iH",row,column);
00130 }
00131
00132
00138 void Console::setColor(int fgcolor,int bgcolor)
00139 {
00140
00141 int fgBright = isBrightColor(fgcolor);
00142 int bgBright = isBrightColor(bgcolor);
00143
00144
00145 int actualFgColor = translateColor(fgcolor);
00146 int actualBgColor = translateColor(bgcolor);
00147
00148
00149 if(bgBright==true)
00150 {
00151 printf("\x1b[4%i;1m",actualBgColor);
00152 }
00153 else
00154 {
00155 printf("\x1b[4%im",actualBgColor);
00156 }
00157
00158 if(fgBright==true)
00159 {
00160 printf("\x1b[3%i;1m",actualFgColor);
00161 }
00162 else
00163 {
00164 printf("\x1b[3%im",actualFgColor);
00165 }
00166 }
00167
00168
00177 void Console::drawString (int x, int y, int color, int bgcolor, const char *msg)
00178 {
00179 moveCursor(y,x);
00180 setColor(color,bgcolor);
00181 printf(msg);
00182 setColor(WHITE,BLACK);
00183 }
00184
00185
00194 void Console::centerString (int y, int color, int bgcolor, const char *msg)
00195 {
00196
00197 int stringLen = strlen(msg);
00198 int centrePos = 40;
00199 int titleLenDiv2 = stringLen / 2;
00200 int x = (centrePos - titleLenDiv2);
00201 drawString(x,y,color,bgcolor,msg);
00202 }
00203
00204
00205
00220 void Console::drawWindow (int startX,int startY,int rows,int cols, int bgColor,int title_textColor,int title_textlineColor,int title_bgColor, int shadowColor, int declColor, const char* windowTitle)
00221 {
00222 int i=0;
00223 int j=0;
00224
00225 for(i=0;i<cols;i++)
00226 {
00227 drawString(startX+i,startY,title_bgColor,title_bgColor," ");
00228 }
00229
00230 int titleLen = strlen(windowTitle);
00231 int centrePos = cols/2+startX;
00232 int titleLenDiv2 = titleLen / 2;
00233
00234 int titlePos = (centrePos - titleLenDiv2);
00235 int titlePosMinusTwo = (titlePos -2);
00236
00237
00239 i=0;
00240 for(i=startX;i<titlePosMinusTwo;i++)
00241 {
00242 drawString(i,startY,title_textlineColor,title_bgColor,"-");
00243 }
00244 drawString(titlePosMinusTwo,startY,title_textlineColor,title_bgColor,"|");
00245
00246 drawString(titlePos,startY,title_textColor,title_bgColor,windowTitle);
00247
00248 drawString(titlePos + titleLen + 1,startY,title_textlineColor,title_bgColor,"|");
00249 i=0;
00250 for(i=titlePos + titleLen + 2;i<cols+startX;i++)
00251 {
00252 drawString(i,startY,title_textlineColor,title_bgColor,"-");
00253 }
00254
00255
00256 for(i=0;i<cols;i++)
00257 {
00258 for(j=1;j<rows;j++)
00259 {
00260 drawString(startX+i,startY+j,bgColor,bgColor," ");
00261 }
00262 }
00263
00264
00265 for(i=0;i<cols;i++)
00266 {
00267 drawString(startX+i,startY+rows-1,declColor,bgColor,"-");
00268 }
00269
00270
00271 for(j=1;j<rows-1;j++)
00272 {
00273 drawString(startX,startY+j,declColor,bgColor,"|");
00274 drawString(startX+cols-1,startY+j,declColor,bgColor,"|");
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 for(i=1;i<=rows;i++)
00292 {
00293 drawString(startX+cols,startY+i,shadowColor,shadowColor," ");
00294 }
00295
00296 for(i=1;i<=cols;i++)
00297 {
00298 drawString(startX+i,startY+rows,shadowColor,shadowColor," ");
00299 }
00300
00301 moveCursor(startY + cols + 3,2);
00302 }
00303
00304
00305
00306
00316 void Console::drawPrimaryWindow (int startX,int startY,int rows,int cols, const char* windowTitle)
00317 {
00318 if(!isSkinRegistered())handleSkinRegistrationError();
00319 drawWindow(startX,startY,rows,cols,
00320 registeredSkin.getSkinPrimaryBgColor(),
00321 registeredSkin.getSkinPrimaryTitleTextColor(),
00322 registeredSkin.getSkinPrimaryTitleRowColor(),
00323 registeredSkin.getSkinPrimaryBgColor(),
00324 registeredSkin.getSkinPrimaryShadow(),
00325 registeredSkin.getSkinPrimaryDecalsColour(),
00326 windowTitle);
00327 }
00328
00329
00339 void Console::drawSecondaryWindow (int startX,int startY,int rows,int cols, const char* windowTitle)
00340 {
00341 if(!isSkinRegistered())handleSkinRegistrationError();
00342 drawWindow(startX,startY,rows,cols,
00343 registeredSkin.getSkinSecondaryBgColor(),
00344 registeredSkin.getSkinSecondaryTitleTextColor(),
00345 registeredSkin.getSkinSecondaryTitleRowColor(),
00346 registeredSkin.getSkinSecondaryBgColor(),
00347 registeredSkin.getSkinSecondaryShadow(),
00348 registeredSkin.getSkinSecondaryDecalsColour(),
00349 windowTitle);
00350 }
00351
00352
00353
00358 void Console::clearConsole()
00359 {
00360 if(!isSkinRegistered())handleSkinRegistrationError();
00361 clearConsole(registeredSkin.getSkinConsoleBgColor());
00362 }
00363
00364
00375 void Console::clearConsoleArea(int startX,int startY, int rows, int cols)
00376 {
00377 if(!isSkinRegistered())handleSkinRegistrationError();
00378 clearArea(startX,startY,rows,cols,registeredSkin.getSkinConsoleBgColor());
00379 }
00380
00381
00392 void Console::clearPrimaryArea(int startX,int startY, int rows, int cols)
00393 {
00394 if(!isSkinRegistered())handleSkinRegistrationError();
00395 clearArea(startX,startY,rows,cols,registeredSkin.getSkinPrimaryBgColor());
00396 }
00397
00398
00409 void Console::clearSecondaryArea(int startX,int startY, int rows, int cols)
00410 {
00411 if(!isSkinRegistered())handleSkinRegistrationError();
00412 clearArea(startX,startY,rows,cols,registeredSkin.getSkinSecondaryBgColor());
00413 }
00414
00415
00416
00424 void Console::drawStringPrimary (int x, int y, const char *msg)
00425 {
00426 if(!isSkinRegistered())handleSkinRegistrationError();
00427 drawString(x,y,registeredSkin.getSkinPrimaryTextColor(),registeredSkin.getSkinPrimaryBgColor(),msg);
00428 }
00429
00430
00438 void Console::drawStringPrimaryAlternate (int x, int y, const char *msg)
00439 {
00440 if(!isSkinRegistered())handleSkinRegistrationError();
00441 drawString(x,y,registeredSkin.getSkinPrimaryTextColorAlternate(),registeredSkin.getSkinPrimaryBgColor(),msg);
00442 }
00443
00444
00452 void Console::drawStringSecondary (int x, int y, const char *msg)
00453 {
00454 if(!isSkinRegistered())handleSkinRegistrationError();
00455 drawString(x,y,registeredSkin.getSkinSecondaryTextColor(),registeredSkin.getSkinSecondaryBgColor(),msg);
00456 }
00457
00458
00466 void Console::drawStringSecondaryAlternate (int x, int y, const char *msg)
00467 {
00468 if(!isSkinRegistered())handleSkinRegistrationError();
00469 drawString(x,y,registeredSkin.getSkinSecondaryTextColorAlternate(),registeredSkin.getSkinSecondaryBgColor(),msg);
00470 }
00471
00472
00479 void Console::centerStringPrimary (int y, const char *msg)
00480 {
00481 if(!isSkinRegistered())handleSkinRegistrationError();
00482 centerString(y,registeredSkin.getSkinPrimaryTextColor(),registeredSkin.getSkinPrimaryBgColor(),msg);
00483 }
00484
00485
00492 void Console::centerStringPrimaryAlternate (int y, const char *msg)
00493 {
00494 if(!isSkinRegistered())handleSkinRegistrationError();
00495 centerString(y,registeredSkin.getSkinPrimaryTextColorAlternate(),registeredSkin.getSkinPrimaryBgColor(),msg);
00496 }
00497
00498
00505 void Console::centerStringSecondary (int y, const char *msg)
00506 {
00507 if(!isSkinRegistered())handleSkinRegistrationError();
00508 centerString(y,registeredSkin.getSkinSecondaryTextColor(),registeredSkin.getSkinSecondaryBgColor(),msg);
00509 }
00510
00511
00518 void Console::centerStringSecondaryAlternate (int y, const char *msg)
00519 {
00520 if(!isSkinRegistered())handleSkinRegistrationError();
00521 centerString(y,registeredSkin.getSkinSecondaryTextColorAlternate(),registeredSkin.getSkinSecondaryBgColor(),msg);
00522 }
00523
00524
00529 void Console::registerSkin(Skin skin)
00530 {
00531 registeredSkin = skin;
00532 skinRegistered = true;
00533 }
00534
00535 void Console::handleSkinRegistrationError()
00536 {
00537 drawString(2,2,Console::RED,Console::BLACK,"SKIN NOT REGISTERED WITH CONSOLE CLASS");
00538 }
00539
00540
00541