Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

TextButton.cpp

Go to the documentation of this file.
00001 /*
00002  * $Id: TextButton.cpp,v 1.13 2002/08/08 09:49:11 virtualcasino Exp $
00003  */
00004 #include<TextButton.h>
00005 #include<Resource.h>
00006 #include<Font.h>
00007 #include<SDL.h>
00008 #include<iostream.h>
00009 
00010 extern Font *SystemFont;
00011 
00012 
00013 namespace clawsoft{
00014 
00015         TextButton::TextButton(){
00016                 setClassName("TextButton");
00017                 caption = "OK";
00018                 H(32);
00019                 W(106);
00020         }
00021 
00022         TextButton::TextButton(Rect &r, const char *ccaption):BaseButton(r){
00023                 setClassName("TextButton");
00024                 caption = ccaption;
00025         }
00026 
00027         TextButton::TextButton(Rect &r, String ccaption):BaseButton(r){
00028                 setClassName("TextButton");
00029                 caption = ccaption;
00030         }
00031 
00032 
00033         void TextButton::draw(){ 
00034                 SDL_Rect src;
00035                 SDL_Rect dest, tsrc, tdst;
00036                 SDL_Surface *label;
00037                 int width;
00038                 int bl, bm, br;
00039                 int temp;
00040                 dest.x = X();
00041                 dest.y = Y();
00042                 label = SystemFont[DEFAULT_FONT].render(caption);
00043                 width = label->w + 70;
00044                 W(width);
00045                 H(32);
00046                 tsrc.x = 0;
00047                 tsrc.y = 0;
00048                 src.w = W();
00049                 src.h = H();
00050                 dest.w = W();
00051                 dest.h = H();
00052                 src.x = 0;
00053                 src.y = 0;
00054                 src.w = width;
00055                 src.h = 32;
00056                 tsrc.w = bRes[BUTTON_UP_LEFT]->w;
00057                 tsrc.h = bRes[BUTTON_UP_LEFT]->h;
00058                 tdst.x = X();
00059                 tdst.y = Y();
00060                 bl = BUTTON_UP_LEFT;
00061                 br = BUTTON_UP_RIGHT;
00062                 bm = BUTTON_UP_MIDDLE;
00063                 if(mouse.inFocus((Rect)*this)){ //Draw with focus
00064                         if(mouse.leftPressed()){
00065                                 //Draw Pressed
00066                                 bl = BUTTON_DOWN_LEFT;
00067                                 br = BUTTON_DOWN_RIGHT;
00068                                 bm = BUTTON_DOWN_MIDDLE;
00069                         }
00070                         else{
00071                                 //Draw focused
00072                                 bl = BUTTON_LIT_LEFT;
00073                                 br = BUTTON_LIT_RIGHT;
00074                                 bm = BUTTON_LIT_MIDDLE;
00075                         }
00076                 }
00077                 else{
00078                         if(!drawn){
00079                                 //Draw Unpressed
00080                                 drawn = true;
00081                         }
00082                 }
00083                 SDL_BlitSurface(bRes[bl], &tsrc, Screen, &tdst);
00084                 tdst.x = getRight() - bRes[br]->w;
00085                 SDL_BlitSurface(bRes[br], &tsrc, Screen, &tdst);
00086                 temp = tdst.x;
00087                 tsrc.w = 1;
00088                 rect.X(X() + 37);
00089                 rect.Y(Y());
00090                 rect.W(temp - rect.X() + 1);
00091                 rect.H(H());
00092                 for(tdst.x = 37 + X(); tdst.x < temp; tdst.x++){
00093                         SDL_BlitSurface(bRes[bm], &tsrc, Screen, &tdst);
00094                 }
00095                 tsrc.w = label->w;
00096                 tsrc.h = label->h;
00097                 tdst.x = X() + (W() - label->w) / 2;
00098                 tdst.y = Y() + (H() - label->h) / 2;
00099                 SDL_BlitSurface(label, &tsrc, Screen, &tdst);
00100                 SDL_FreeSurface(label);
00101                 //SDL_UpdateRect(Screen, dest.x, dest.y, dest.w, dest.h);
00102         }
00103 
00104         bool TextButton::checkMouseMotionEvents(){
00105                 if(drawn){
00106                         if(!mouse.inFocus(rect)){
00107                                 drawn = false;
00108                         }
00109                 }
00110                 draw();
00111                 return false;
00112         }
00113 
00114         bool TextButton::checkMouseEvents(){
00115                 int xx, yy, bb;
00116                 mouse.getStatus(&xx, &yy, &bb);
00117                 if(mouse.inFocus(rect) && mouse.leftPressed()){
00118                         draw();
00119                         action(xx, yy, bb);
00120                         return true;
00121                 }
00122                 return false;
00123         }
00124 
00125         bool TextButton::checkKeyEvents(){
00126                 int xx, yy, bb;
00127                 mouse.getStatus(&xx, &yy, &bb);
00128                 if(keyboard.keyPressed(SDLK_RETURN) && mouse.inFocus(rect)){
00129                         mouse.getStatus(&xx, &yy, &bb);
00130                         draw();
00131                         action(xx, yy, bb);
00132                         return true;
00133                 }
00134                 return false;
00135         }
00136 
00137         void TextButton::setCaption(const char *s){
00138                 caption = s;
00139         }
00140 
00141         void TextButton::setCaption(String s){
00142                 caption = s;
00143         }
00144 
00145         void TextButton::map(){
00146                 int width;
00147                 SDL_Surface *label;
00148                 label = SystemFont[DEFAULT_FONT].render(caption);
00149                 width = label->w + 70;
00150                 W(width);
00151                 H(32);
00152                 SDL_FreeSurface(label);
00153         }
00154 }

Powered by:

SourceForge Logo