This is the start of my reuseable GUI (only button included) by 3d point in space17th Sep 2009 16:27
|
---|
Summary This creates a button that can be used and checks if the button is pressed. This also can create a sprite and dismiss a certain collar as a button. Description You can create a button useing Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com //////////////////////////////////////darkgui.h button//////////////////////// #include "DarkGDK.h" class button { private: int xposb; int yposb; int sizexb; char *buttontextb; char *texttype; int red; int green; int blue; char *spriteb; int sizetext; int noredb; int nogreenb; int noblueb; int textheight; int textred; int textgreen; int textblue; int bottomboxx; int bottomboxy; int buttonbordersize; int buttonborderred; int buttonbordergreen; int buttonborderblue; int bnum; public: button(int buttonnum); button(); ~button(); void button1(int xpos,int ypos, char *buttontext);// change defalt button void changetextcol(int redtemp, int greentemp, int bluetemp);//change text collar void changebuttoncol(int redtemp, int greentemp, int bluetemp);//change button collar void changetext(char *texttype1, int sizetext1); void changebuttonborder(int size, int buttonbordred,int buttonbordgreen, int buttonbordblue); //void button1(int xpos,int ypos,int sizex, char *buttontext, char *texttype, int sizetext, int red, int green, int blue ); void button2(int xpos,int ypos, char *sprite); void SetImageColorKey(int red,int green, int blue); bool checkbuttonpressed(int mousex, int mousey); bool checkbuttonmouseover(int mousex, int mousey); }; //////////////////////////////////////darkgui.cpp button//////////////////////// #include "DarkGDK.h" #include "darkgui.h" button::button(int buttonnum) { bnum=buttonnum; spriteb=NULL; } button::button() { xposb=0; yposb=0; sizexb=20; buttontextb="No Text"; texttype="Arial"; textheight=16; red=0; green=0; blue=255; noredb=0; nogreenb=0; noblueb=0; spriteb=NULL; textred=255; textgreen=255; textblue=255; bottomboxx=xposb+dbTextWidth("No Text")+30; bottomboxy=yposb+textheight+4; buttonbordersize=1; buttonborderred=100; buttonbordergreen=100; buttonborderblue=100; spriteb=NULL; } void button::changebuttonborder(int size, int buttonbordred,int buttonbordgreen, int buttonbordblue) { buttonbordersize=size; buttonborderred=buttonbordred; buttonbordergreen=buttonbordgreen; buttonborderblue=buttonbordblue; } void button::button1(int xpos,int ypos, char *buttontext)//change default button { xposb=xpos; yposb=ypos; //int bottomboxx=10; bottomboxx=xpos+dbTextWidth(buttontext)+30; //int bottomboxy=10; bottomboxy=ypos+textheight+4+buttonbordersize; buttontextb=buttontextb; dbInk(dbRgb (red,green,blue), 0); dbBox (xpos,ypos,bottomboxx,bottomboxy); dbInk(dbRgb(buttonborderred,buttonbordergreen,buttonborderblue),0); dbBox(xpos,ypos,bottomboxx,ypos+buttonbordersize); dbBox(bottomboxx,ypos,bottomboxx+buttonbordersize,bottomboxy); dbBox(xpos,bottomboxy-buttonbordersize,bottomboxx,bottomboxy); dbBox(xpos,ypos,xpos+buttonbordersize,bottomboxy); //dbBox(bottomboxx,bottomboxy,xpos,bottomboxy+buttonbordersize); //dbBox(xpos,bottomboxy+buttonbordersize,xpos,ypos); dbInk(dbRgb(textred,textgreen,textblue),0); dbText(xpos+15,ypos+buttonbordersize+1,buttontext); } void button::changetextcol(int redtemp, int greentemp, int bluetemp) { textred=redtemp; textgreen=greentemp; textblue=bluetemp; } void button::changebuttoncol(int redtemp, int greentemp, int bluetemp) { red=redtemp; green=greentemp; blue=bluetemp; } void button::changetext(char *texttype1, int sizetext1) { texttype=texttype1; textheight=sizetext1; } void button::button2(int xpos,int ypos, char *sprite) { dbLoadImage (sprite,bnum); dbSprite (bnum,xpos,ypos,bnum); xposb=xpos; yposb=ypos; spriteb=sprite; bottomboxx=dbSpriteWidth(bnum); bottomboxy=dbSpriteHeight (bnum); } void button::SetImageColorKey(int red,int green, int blue) { noredb=red; nogreenb=green; noblueb=blue; dbSetImageColorKey(red,green,blue); } button::~button() { dbDeleteSprite ( bnum ); } bool button::checkbuttonpressed(int mousex, int mousey) { bool retbuttonstate=false; if(mousex>=xposb&& mousey>=yposb&&mousex<=bottomboxx&&mousey<=bottomboxy&&dbMouseClick()==1) { retbuttonstate=true; } return retbuttonstate; } bool button::checkbuttonmouseover(int mousex, int mousey) { bool retbuttonstate=false; if(mousex>=xposb&& mousey>=yposb&&mousex<=bottomboxx&&mousey<=bottomboxy) { retbuttonstate=true; } return retbuttonstate; } //////////////////////////////////////main////////////////////////////////////// // Dark GDK - The Game Creators - www.thegamecreators.com // the wizard has created a very simple 2D project that uses Dark GDK // it can be used as a starting point in making your own 2D games // whenever using Dark GDK you must ensure you include the header file #include "DarkGDK.h" #include "darkgui.h" // the main entry point for the application is this function void DarkGDK ( void ) { // in this application a backdrop is loaded and then several // animated sprites are displayed on screen // when starting a Dark GDK program it is useful to set global // application properties, we begin by turning the sync rate on, // this means we control when the screen is updated, we also set // the maximum rate to 60 which means the maximum frame rate will // be set at 60 frames per second dbSyncOn ( ); dbSyncRate ( 60 ); button *b1= new button(1); b1->SetImageColorKey(255,255,255); b1->button2(10,10,"image.bmp"); //b1->changebuttoncol(100,100,100); //b1->changebuttonborder(1,200,200,200); //b1->button1(100,20,"hi"); button *b2=new button(); while ( LoopGDK ( ) ) { b2->button1(100,10,"start"); b2->changebuttoncol(100,100,100); b2->changebuttonborder(1,200,200,200); //b2->button1(100,20,"hi"); //dbText(0,100,dbStr(dbMouseX())); if(dbEscapeKey()) { break; } if(b1->checkbuttonpressed(dbMouseX(),dbMouseY())==true) { dbText(100,100,"true"); } if(b2->checkbuttonpressed(dbMouseX(),dbMouseY())==true) { dbText(100,100,"true"); } dbSync ( ); } return; } |