Text_button.cc

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001 The Exult Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 
00023 #include "Text_button.h"
00024 #include "gamewin.h"
00025 #include "font.h"
00026 #include "iwin8.h"
00027 #include "Gump.h"
00028 
00029 #define TB_FONTNUM      2
00030 
00031 // Palette Indices
00032 #define TB_OUTER_BORDER     133
00033 #define TB_OUTER_BORDER_CORNER    142
00034 #define TB_OUTER_BORDER_PUSHED_TOP  144
00035 #define TB_OUTER_BORDER_PUSHED_LEFT 140
00036 
00037 #define TB_INNER_BORDER_HIGHLIGHT 138
00038 #define TB_INNER_BORDER_LOWLIGHT  142
00039 #define TB_INNER_BORDER_CORNER    141
00040 #define TB_INNER_BORDER_TR_HIGH   137
00041 #define TB_INNER_BORDER_TR_CORNER 138
00042 #define TB_INNER_BORDER_BL_CORNER 144
00043 
00044 #define TB_BACKGROUND     140
00045 #define TB_RT_HIGHLIGHT     139
00046 
00047 Text_button::Text_button(Gump *p, const std::string &str, int x, int y, int w, int h)
00048   : Gump_button(p, 0, x, y, SF_OTHER), text(str), width(w), height(h)
00049 {
00050   init();
00051 }
00052 
00053 void Text_button::init()
00054 {
00055   // Must be at least 11 units high
00056   if (height < 11) height = 11;
00057 
00058   // Text y is based on gump height of 11
00059   text_y = 2 + (height - 11)/2;
00060 
00061   // We will get the text width
00062   int text_width = sman->get_font(TB_FONTNUM)->get_text_width(
00063                 text.c_str());
00064 
00065   if (width < text_width + 4) width = text_width + 4;
00066 
00067   // We want to find the starting point for the text (horizontal)
00068   text_x = (width - text_width) >> 1;
00069 }
00070 
00071 void Text_button::paint()
00072 {
00073   Image_window8 *iwin = gwin->get_win();
00074 
00075   int offset = 0;
00076   int px = x;
00077   int py = y;
00078 
00079   if (parent)
00080   {
00081     px += parent->get_x();
00082     py += parent->get_y();
00083   }
00084 
00085   // The the push dependant edges
00086   if (pushed)
00087   {
00088     // Top left corner
00089     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py);
00090     // Bottom left corner
00091     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py+height-1);
00092     // Top right corner
00093     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py);
00094     // Top edge
00095     iwin->fill8(TB_OUTER_BORDER_PUSHED_TOP, width-2, 1, px+1, py);
00096     // Left edge
00097     iwin->fill8(TB_OUTER_BORDER_PUSHED_TOP, 1, height-2, px, py+1);
00098 
00099     offset = 1;
00100   }
00101   else
00102   {
00103     // Bottom right corner
00104     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py+height-1);
00105     // Bottom left corner
00106     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py+height-1);
00107     // Top right corner
00108     iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py+height-1);
00109     // Bottom edge
00110     iwin->fill8(TB_OUTER_BORDER, width-2, 1, px+1, py+height-1);
00111     // Right edge
00112     iwin->fill8(TB_OUTER_BORDER, 1, height-2, px+width-1, py+1);
00113   }
00114 
00115   // 'Outer' Top and Left Edges
00116 
00117   // Top left corner
00118   iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+offset, py+offset);
00119   // Top edge
00120   iwin->fill8(TB_OUTER_BORDER, width-2, 1, px+1+offset, py+offset);
00121   // Left edge
00122   iwin->fill8(TB_OUTER_BORDER, 1, height-2, px+offset, py+1+offset);
00123   
00124   // 'Inner' Edges
00125 
00126   // Top left corner
00127   iwin->fill8(TB_INNER_BORDER_CORNER, 1, 1, px+offset+1, py+offset+1);
00128   // Top Right corner
00129   iwin->fill8(TB_INNER_BORDER_TR_CORNER, 1, 1, px+width+offset-2, py+offset+1);
00130   // Top Right Highlight 1
00131   iwin->fill8(TB_INNER_BORDER_TR_HIGH, 1, 1, px+width+offset-3, py+offset+1);
00132   // Top Right Highlight 1
00133   iwin->fill8(TB_INNER_BORDER_TR_HIGH, 1, 1, px+width+offset-2, py+offset+2);
00134   // Bottom left corner
00135   iwin->fill8(TB_INNER_BORDER_BL_CORNER, 1, 1, px+offset+1, py+height+offset-2);
00136 
00137   // Top edge
00138   iwin->fill8(TB_INNER_BORDER_HIGHLIGHT, width-5, 1, px+2+offset, py+offset+1);
00139   // Left edge
00140   iwin->fill8(TB_INNER_BORDER_LOWLIGHT, 1, height-4, px+offset+1, py+2+offset);
00141   // Right edge
00142   iwin->fill8(TB_INNER_BORDER_HIGHLIGHT, 1, height-5, px+width+offset-2, py+3+offset);
00143   // Bottom edge
00144   iwin->fill8(TB_INNER_BORDER_LOWLIGHT, width-4, 1, px+2+offset, py+height+offset-2);
00145 
00146   // Background Fill 
00147   iwin->fill8(TB_BACKGROUND, width-4, height-4, px+2+offset, py+2+offset);
00148   // Top Right Highligh on Background 
00149   iwin->fill8(TB_RT_HIGHLIGHT, 1, 1, px+width+offset-3, py+offset+2);
00150 
00151   sman->paint_text(TB_FONTNUM, text.c_str(), px+text_x+offset, 
00152               py+text_y+offset);
00153 }
00154 
00155 int Text_button::on_widget(int mx, int my)
00156 {
00157   int px = x;
00158   int py = y;
00159 
00160   if (parent)
00161   {
00162     px += parent->get_x();
00163     py += parent->get_y();
00164   }
00165 
00166   if (mx < px || mx >= px + width) return 0;
00167   if (my < py || my >= py + height) return 0;
00168   return 1;
00169 }

Generated on Mon Jul 9 14:42:47 2007 for ExultEngine by  doxygen 1.5.1