GLICT/button.cpp

Go to the documentation of this file.
00001 /*
00002         GLICT - Graphics Library Interface Creation Toolkit
00003         Copyright (C) 2006-2007 OBJECT Networks
00004 
00005         This library is free software; you can redistribute it and/or
00006         modify it under the terms of the GNU Library General Public
00007         License as published by the Free Software Foundation; either
00008         version 2 of the License, or (at your option) any later version.
00009 
00010         This library is distributed in the hope that it will be useful,
00011         but WITHOUT ANY WARRANTY; without even the implied warranty of
00012         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013         Library General Public License for more details.
00014 
00015         You should have received a copy of the GNU Library General Public
00016         License along with this library; if not, write to the Free
00017         Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include "button.h"
00030 #include "globals.h"
00031 #include "fonts.h"
00032 
00036 glictButton::glictButton() {
00037         //printf("init panele\n");
00038         this->bgcolor.r = 0.0;
00039         this->bgcolor.g = 0.0;
00040         this->bgcolor.b = 0.0;
00041         this->bgcolor.a = 1.0;
00042 
00043         this->fgcolor.r = 1.0;
00044         this->fgcolor.g = 1.0;
00045         this->fgcolor.b = 1.0;
00046         this->fgcolor.a = 1.0;
00047 
00048 
00049         strcpy(this->objtype, "Button");
00050 
00051         this->highlighted = false;
00052         this->parent = NULL;
00053 
00054         this->focusable = true;
00055 
00056         this->caption = "Button";
00057 }
00058 
00062 glictButton::~glictButton() {
00063 
00064 }
00065 
00082 bool glictButton::CastEvent(glictEvents evt, void* wparam, long lparam, void* returnvalue) {
00083         if (!GetVisible() || !GetEnabled()) return false;
00084 
00085         //printf("Event of type %s passing through %s (%s)\n", EvtTypeDescriptor(evt), objtype, parent ? parent->objtype : "NULL");
00086         switch (evt) {
00087                 case GLICT_KEYPRESS:
00088                         switch (*((char*)wparam)) {
00089                                 case 9:
00090                                         if (next)
00091                                                 next->Focus(NULL);
00092                                                 break;
00093                         }
00094                         break;
00095                 case GLICT_MOUSEUP:
00096                 case GLICT_MOUSEDOWN:
00097                 case GLICT_MOUSECLICK:
00098                         if (((glictPos*)wparam)->x >= this->clipleft &&
00099                                 ((glictPos*)wparam)->x <= this->clipright &&
00100                                 ((glictPos*)wparam)->y >= this->cliptop &&
00101                                 ((glictPos*)wparam)->y <= this->clipbottom) {
00102                                 //printf("Within button\n");
00103                                 if (evt == GLICT_MOUSECLICK) {
00104                                         //this->Focus(NULL);
00105                                         //printf("Caught button click!\n",0,0);
00106                                 }
00107 
00108                                 if (evt == GLICT_MOUSEUP) { // the trick is that button doesnt need to be dereleased inside window to be dereleased! however it also doesnt do default click behaviour
00109                                         //printf("Dehighlighting button\n");
00110                                         highlighted = false;
00111                                 }
00112                                 if (evt == GLICT_MOUSEDOWN) {
00113                                         //printf("Highlighting button\n",0,0);
00114                                         highlighted = true;
00115                                 }
00116 
00117                                 if (DefaultCastEvent(evt, wparam, lparam, returnvalue)) { // if a child caught click, we dont handle it otherwise
00118                                         //printf("Default even was cast\n");
00119                                         return true; // we simply return
00120                                 }
00121                                 // otherwise we could handle it mroe ...
00122                                 //return false;
00123                                 return true;
00124                         } else {
00125                             //printf("BUTTON DID NOT FIND THIS THING. X, Y: %d %d Clip: %d %d %d %d\n", ((glictPos*)wparam)->x, ((glictPos*)wparam)->y, clipleft, clipright, cliptop, clipbottom);
00126                         }
00127 
00128 
00129                         if (evt == GLICT_MOUSEUP) { // the trick is that button doesnt need to be dereleased inside window to be dereleased! however it also doesnt do default click behaviour
00130                                 //printf("Dehighlighting button while outside its borders\n");
00131                                 highlighted = false;
00132                                 return false;
00133                         }
00134                         break;
00135         }
00136 
00137 
00138 
00139         return false;
00140 }
00141 
00148 void glictButton::Paint() {
00149         if (!GetVisible()) return;
00150 
00151 
00152         if (glictGlobals.debugCallback) {
00153                 glictGlobals.debugCallback(strlen("glictButton::Paint()"), "glictButton::Paint()");
00154         }
00155 
00156         this->SetScissor();
00157 
00158         if (!((glictGlobals.buttonSkin && !highlighted) || (glictGlobals.buttonHighlightSkin && highlighted))) {
00159                 // if skin is not set, then let's draw a plain rectangle
00160                 glictColor col;
00161         if (!highlighted) {
00162             col = bgcolor;
00163         } else {
00164             col.r = this->bgcolor.r < .5 ? (float)this->bgcolor.r * 1.5 : (float)this->bgcolor.r / 1.5;
00165                         col.g = this->bgcolor.g < .5 ? (float)this->bgcolor.g * 1.5 : (float)this->bgcolor.g / 1.5;
00166                         col.b = this->bgcolor.b < .5 ? (float)this->bgcolor.b * 1.5 : (float)this->bgcolor.b / 1.5;
00167                         col.a = this->bgcolor.a;
00168         }
00169         glictGlobals.PaintRect(this->x+glictGlobals.translation.x, this->x+this->width+glictGlobals.translation.x,
00170                                                                 this->y+glictGlobals.translation.y, this->y+this->height+glictGlobals.translation.y,
00171                                                                 col);
00172 
00173         } else {
00174                 // if skin is set, then draw appropriate skin
00175         glictSize s = {this->width, this->height};
00176 
00177         glictGlobals.Translatef(this->x, this->y, 0);
00178         if (!highlighted)
00179             glictGlobals.buttonSkin->Paint(&s);
00180         else
00181             glictGlobals.buttonHighlightSkin->Paint(&s);
00182         glictGlobals.Translatef(-this->x, -this->y, 0);
00183         }
00184 
00185         // restore color
00186     if (!highlighted) {
00187         glictGlobals.SetColor(
00188             glictGlobals.buttonTextColor.r,
00189             glictGlobals.buttonTextColor.g,
00190             glictGlobals.buttonTextColor.b,
00191             glictGlobals.buttonTextColor.a
00192         );
00193     } else {
00194         glictGlobals.SetColor(
00195             glictGlobals.buttonHighlightTextColor.r,
00196             glictGlobals.buttonHighlightTextColor.g,
00197             glictGlobals.buttonHighlightTextColor.b,
00198             glictGlobals.buttonHighlightTextColor.a
00199         );
00200     }
00201 
00202 
00203         {
00204                 // if focused, draw the "focused" line
00205                 glictColor col;
00206 
00207                 col.r = this->bgcolor.r < .5 ? (float)this->bgcolor.r * 1.6 : (float)this->bgcolor.r / 1.6;
00208                 col.g = this->bgcolor.g < .5 ? (float)this->bgcolor.g * 1.6 : (float)this->bgcolor.g / 1.6;
00209                 col.b = this->bgcolor.b < .5 ? (float)this->bgcolor.b * 1.6 : (float)this->bgcolor.b / 1.6;
00210                 col.a = this->bgcolor.a;
00211 
00212                 glictGlobals.PaintRectLines(this->x+glictGlobals.translation.x, this->x+this->width+glictGlobals.translation.x,
00213                                                                 this->y+glictGlobals.translation.y, this->y+this->height+glictGlobals.translation.y,
00214                                                                 col);
00215         }
00216 
00217         //glPushMatrix();
00218         if (highlighted) glictGlobals.Translatef(2.,2.,0.);
00219 
00220 
00221 
00222         glictFontRender(
00223                 this->caption.c_str(),
00224                 fontname.c_str(),
00225                 fontsize,
00226                 this->x + this->width / 2. - glictFontSize(this->caption.c_str(), fontname.c_str(), fontsize) / 2. +glictGlobals.translation.x,
00227                 (this->y + this->height / 2. - (fontsize/2.)*((float)glictFontNumberOfLines(this->caption.c_str())))*+1. +glictGlobals.translation.y
00228                 );
00229 
00230 
00231     if (highlighted) glictGlobals.Translatef(-2.,-2.,0.);
00232         //glPopMatrix();
00233 
00234         this->CPaint();
00235 }
00245 void glictButton::SetBGColor(float r, float g, float b, float a) {
00246         this->bgcolor.r = r;
00247         this->bgcolor.g = g;
00248         this->bgcolor.b = b;
00249         this->bgcolor.a = a;
00250 }
00251 
00261 void glictButton::SetFGColor(float r, float g, float b, float a) {
00262         this->fgcolor.r = r;
00263         this->fgcolor.g = g;
00264         this->fgcolor.b = b;
00265         this->fgcolor.a = a;
00266 }

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007