core/vgui/vgui_text_tableau.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_text_tableau.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief  See vgui_text_tableau.h for a description of this file.
00008 // \author Philip C. Pritchett, RRG, University of Oxford
00009 // \date   19 Oct 1999
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   19-OCT-1999 P.Pritchett - Initial version.
00014 // \endverbatim
00015 
00016 #include "vgui_text_tableau.h"
00017 #include <vgui/vgui_text_put.h>
00018 #include <vgui/vgui_event.h>
00019 #include <vgui/vgui_gl.h>
00020 #include <vcl_cassert.h>
00021 
00022 vgui_text_tableau::vgui_text_tableau()
00023   : cur_r_( 1 ), cur_g_( 0 ), cur_b_( 0 ),
00024     cur_sz_( 24 ),
00025     first_empty(0)
00026 {
00027   // Default text colour is red:
00028   cur_r_ = 1; cur_g_ = 0; cur_b_ = 0;
00029 }
00030 
00031 vcl_string vgui_text_tableau::type_name() const { return "vgui_text_tableau"; }
00032 
00033 unsigned vgui_text_tableau::size() const
00034 {
00035   unsigned N=xs.size();
00036 #if 0
00037   assert(N == ys.size()); // just to
00038   assert(N == ts.size()); // make sure.
00039 #endif // 0
00040   return N;
00041 }
00042 
00043 void vgui_text_tableau::clear()
00044 {
00045   if (size() > 0) {
00046     xs.clear();
00047     ys.clear();
00048     ts.clear();
00049     post_redraw();
00050     first_empty = 0;
00051   }
00052 }
00053 
00054 int vgui_text_tableau::add(float x, float y, char const *text)
00055 {
00056   int return_val = first_empty;
00057   if (first_empty < size()) {
00058     xs[first_empty] = x;
00059     ys[first_empty] = y;
00060     ts[first_empty] = text;
00061     r_[first_empty] = cur_r_;
00062     g_[first_empty] = cur_g_;
00063     b_[first_empty] = cur_b_;
00064     sz_[first_empty] = cur_sz_;
00065     // Find next empty slot:
00066     while (first_empty < size()  &&  r_[first_empty] != -1)
00067       first_empty++;
00068   }
00069   else {
00070     xs.push_back(x);
00071     ys.push_back(y);
00072     ts.push_back(text);
00073     r_.push_back(cur_r_);
00074     g_.push_back(cur_g_);
00075     b_.push_back(cur_b_);
00076     sz_.push_back(cur_sz_);
00077     first_empty++;
00078   }
00079   post_redraw();
00080   return return_val;
00081 }
00082 
00083 void vgui_text_tableau::move(int hndl, float nx, float ny)
00084 {
00085   assert(hndl >= 0);
00086   assert((unsigned int)hndl < size());
00087   xs[hndl] = nx;
00088   ys[hndl] = ny;
00089   post_redraw();
00090 }
00091 
00092 void vgui_text_tableau::set_colour(float r, float g, float b)
00093 {
00094   if (0 <= r && r <= 1 && 0 <= g && g <= 1 && 0 <= b && b <= 1)
00095   {
00096     cur_r_ = r;
00097     cur_g_ = g;
00098     cur_b_ = b;
00099   }
00100 }
00101 
00102 void vgui_text_tableau::set_size( unsigned s )
00103 {
00104   cur_sz_ = s;
00105 }
00106 
00107 float vgui_text_tableau::get_posx(int hndl) const
00108 {
00109   assert(hndl >= 0);
00110   assert((unsigned int)hndl < size());
00111   return xs[hndl];
00112 }
00113 
00114 float vgui_text_tableau::get_posy(int hndl) const
00115 {
00116   assert(hndl >= 0);
00117   assert((unsigned int)hndl < size());
00118   return ys[hndl];
00119 }
00120 
00121 vcl_string const &vgui_text_tableau::get_text(int hndl) const
00122 {
00123   assert(hndl >= 0);
00124   assert((unsigned int)hndl < size());
00125   return ts[hndl];
00126 }
00127 
00128 void vgui_text_tableau::remove(int hndl)
00129 {
00130   assert(hndl >= 0);
00131   assert((unsigned int)hndl < size());
00132   // Using r_ to indicate empty is okay because valid values for r are in [0,1].
00133   r_[hndl] = -1;
00134 
00135 #if 0 // kym - don't do this because it changes handles of values remaining in list:
00136   xs.erase(xs.begin() + hndl);
00137   ys.erase(ys.begin() + hndl);
00138   ts.erase(ts.begin() + hndl);
00139 #endif // 0
00140 
00141   post_redraw();
00142   if ((unsigned int)hndl < first_empty)
00143     first_empty = hndl;
00144 }
00145 
00146 void vgui_text_tableau::change(int hndl, char const *ntext)
00147 {
00148   assert(hndl >= 0);
00149   assert((unsigned int)hndl < size());
00150   ts[hndl] = ntext;
00151   post_redraw();
00152 }
00153 
00154 bool vgui_text_tableau::handle(vgui_event const &e)
00155 {
00156   if (e.type != vgui_DRAW)
00157     return false;
00158 
00159   // set OpenGL state suitable for rendering 2D bitmap fonts :
00160   glDisable(GL_CULL_FACE);
00161   glDisable(GL_DEPTH_TEST);
00162   glDisable(GL_TEXTURE_2D);
00163   glDisable(GL_LIGHTING);
00164   glShadeModel(GL_FLAT);
00165 
00166   for (unsigned i=0; i<size(); ++i) {
00167     if (r_[i] != -1) {
00168       glColor3f(r_[i],g_[i],b_[i]);
00169       glRasterPos2f(xs[i], ys[i]);
00170       ::vgui_text_put(ts[i].c_str(),sz_[i]);
00171     }
00172   }
00173   return true;
00174 }