00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include <SDL.h>
00024
#include "guitypedefs.h"
00025
#include "guicolors.h"
00026
#ifdef _WIN32
00027
#pragma warning(push)
00028
00029
#include <yvals.h>
00030
00031
#pragma warning(disable: 4251)
00032
#pragma warning(disable: 4786) // identifier was truncated to 'number' characters in the debug information
00033
00034
00035
00036
00037
00038
#include <sge.h>
00039
00040
#pragma warning(pop)
00041
#else
00042
#include <sge.h>
00043
#endif
00044
00045
#include "SDL_prim.h"
00046
00047
#include "TTFont.h"
00048
#include "SDLControl.h"
00049
#include "Surface.h"
00050
#include "Static.h"
00051
#include "Button.h"
00052
00053
00054
namespace gui
00055 {
00056
00057
00058 Button::Button(
Surface *s,
coord_t x,coord_t y,coord_t w,coord_t h,
SurfaceHelper *pclHelper)
00059 :
Surface(s,x,y,w,h,pclHelper)
00060 ,m_screen_inverted(NULL)
00061 ,m_screen_inactive(NULL)
00062 ,m_style(0)
00063 {
00064 SetBgColor(
BUTTON_DEFAULT_BG_R,
BUTTON_DEFAULT_BG_G,
BUTTON_DEFAULT_BG_B);
00065 SetFgColor(
BUTTON_DEFAULT_FG_R,
BUTTON_DEFAULT_FG_G,
BUTTON_DEFAULT_FG_B);
00066 SetFrameWidth(
BUTTON_DEFAULT_FW);
00067 SetFrameColor(
BUTTON_DEFAULT_FC_R,
BUTTON_DEFAULT_FC_G,
BUTTON_DEFAULT_FC_B);
00068
00069
m_screen_inverted = CreateSDLSurface(
true);
00070
m_screen_inactive = CreateSDLSurface(
true);
00071 }
00072
00073
00074 Button::Button(
Surface *s,coord_t x,coord_t y,buttonstyle_t st,
SurfaceHelper *pclHelper)
00075 :
Surface(s,x,y,
SBSIZE,
SBSIZE,pclHelper)
00076 ,m_screen_inverted(NULL)
00077 ,m_screen_inactive(NULL)
00078 ,m_style(st)
00079 {
00080 SetBgColor(
BUTTON_DEFAULT_BG_R,
BUTTON_DEFAULT_BG_G,
BUTTON_DEFAULT_BG_B);
00081 SetFgColor(
BUTTON_DEFAULT_FG_R,
BUTTON_DEFAULT_FG_G,
BUTTON_DEFAULT_FG_B);
00082 SetFrameWidth(
BUTTON_DEFAULT_FW);
00083 SetFrameColor(
BUTTON_DEFAULT_FC_R,
BUTTON_DEFAULT_FC_G,
BUTTON_DEFAULT_FC_B);
00084
00085
m_screen_inverted = CreateSDLSurface(
true);
00086
m_screen_inactive = CreateSDLSurface(
true);
00087 }
00088
00089
00090 Button::~Button()
00091 {
00092
if (
m_screen_inverted)
00093 {
00094 SDL_FreeSurface(
m_screen_inverted);
00095 }
00096
if (
m_screen_inactive)
00097 {
00098 SDL_FreeSurface(
m_screen_inactive);
00099 }
00100 }
00101
00102
00103 void Button::Draw()
00104 {
00105 SDL_Rect *area =
GetClientRectPtr();
00106
00107
TTFont *font =
GetTTFont();
00108 SDL_Color fg;
00109 SDL_Color bg;
00110
coord_t x1;
00111
coord_t y1;
00112
coord_t x2;
00113
coord_t y2;
00114
coord_t x3;
00115
coord_t y3;
00116
00117
00118
00119
00120
DEB(printf(
" ******** Button redraw\n");)
00121
00122
GetFgColor(fg);
00123
GetBgColor(bg);
00124
00125
#define SPACED 3
00126
00127
switch (
m_style)
00128 {
00129
case BUTTONSTYLE_SBUP:
00130 x1 = area -> w / 2;
00131 y1 =
SPACED;
00132 x2 =
SPACED;
00133 y2 = area -> h -
SPACED;
00134 x3 = area -> w -
SPACED;
00135 y3 = area -> h -
SPACED;
00136
00137 SDL_FillRect(m_screen, area,
GetBgColor());
00138
SDL_fillTriangle(m_screen, x1,y1, x2,y2, x3,y3,
GetFgColor());
00139
00140 SDL_FillRect(
m_screen_inverted, area,
GetFgColor());
00141
SDL_fillTriangle(
m_screen_inverted, x1,y1, x2,y2, x3,y3,
GetBgColor());
00142
00143
break;
00144
case BUTTONSTYLE_SBDOWN:
00145 x1 =
SPACED;
00146 y1 =
SPACED;
00147 x2 = area -> w -
SPACED;
00148 y2 =
SPACED;
00149 x3 = area -> w / 2;
00150 y3 = area -> h -
SPACED;
00151
00152 SDL_FillRect(m_screen, area,
GetBgColor());
00153
SDL_fillTriangle(m_screen, x1,y1, x2,y2, x3,y3,
GetFgColor());
00154
00155 SDL_FillRect(
m_screen_inverted, area,
GetFgColor());
00156
SDL_fillTriangle(
m_screen_inverted, x1,y1, x2,y2, x3,y3,
GetBgColor());
00157
00158
break;
00159
case BUTTONSTYLE_SBRIGHT:
00160 x1 =
SPACED;
00161 y1 =
SPACED;
00162 x2 = area -> w -
SPACED;
00163 y2 = area -> h / 2;
00164 x3 =
SPACED;
00165 y3 = area -> h -
SPACED;
00166
00167 SDL_FillRect(m_screen, area,
GetBgColor());
00168
SDL_fillTriangle(m_screen, x1,y1, x2,y2, x3,y3,
GetFgColor());
00169
00170 SDL_FillRect(
m_screen_inverted, area,
GetFgColor());
00171
SDL_fillTriangle(
m_screen_inverted, x1,y1, x2,y2, x3,y3,
GetBgColor());
00172
00173
break;
00174
case BUTTONSTYLE_SBLEFT:
00175 x1 =
SPACED;
00176 y1 = area -> h / 2;
00177 x2 = area -> w -
SPACED;
00178 y2 =
SPACED;
00179 x3 = area -> w -
SPACED;
00180 y3 = area -> h -
SPACED;
00181
00182 SDL_FillRect(m_screen, area,
GetBgColor());
00183
SDL_fillTriangle(m_screen, x1,y1, x2,y2, x3,y3,
GetFgColor());
00184
00185 SDL_FillRect(m_screen, area,
GetFgColor());
00186
SDL_fillTriangle(m_screen, x1,y1, x2,y2, x3,y3,
GetBgColor());
00187
00188
break;
00189
00190
default:
00191 {
00192
00193 SDL_FillRect(m_screen, area,
GetBgColor());
00194
00195
if (font && font -> GetFont())
00196 {
00197 SDL_Rect rect = sge_TTF_TextSize(font -> GetFont(), (
char *)
GetText());
00198
coord_t x =
GetW() / 2 - rect.w / 2;
00199
coord_t y =
GetH() / 2 + font ->
GetH() / 2;
00200
00201 sge_tt_textoutf(m_screen, font -> GetFont(),
00202 x,y,
00203 fg.r,fg.g,fg.b,
00204 bg.r,bg.g,bg.b,
00205 SDL_ALPHA_OPAQUE,
00206
"%s",GetText() );
00207 }
00208 DrawFrame(m_screen,
GetFrameWidth(),
GetFrameColor());
00209
00210
00211 SDL_FillRect(
m_screen_inverted, area,
GetFgColor());
00212
00213
if (font && font -> GetFont())
00214 {
00215 SDL_Rect rect = sge_TTF_TextSize(font -> GetFont(), (
char *)
GetText());
00216
coord_t x =
GetW() / 2 - rect.w / 2;
00217
coord_t y =
GetH() / 2 + font ->
GetH() / 2;
00218
00219 sge_tt_textoutf(
m_screen_inverted, font -> GetFont(),
00220 x,y,
00221 bg.r,bg.g,bg.b,
00222 fg.r,fg.g,fg.b,
00223 SDL_ALPHA_OPAQUE,
00224
"%s",GetText() );
00225 }
00226 DrawFrame(
m_screen_inverted,
GetFrameWidth(),
GetFrameColor());
00227
00228
00229 }
00230
break;
00231 }
00232
00233 }
00234
00235
00236 SDL_Surface *Button::GetScreen()
00237 {
00238
if (
IsInverted())
00239 {
00240
return m_screen_inverted;
00241 }
00242
00243
00244
00245
00246
00247
00248
return m_screen;
00249 }
00250
00251
00252 }
00253
00254
00255