core/vgui/vgui_clear_tableau.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_clear_tableau.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Philip C. Pritchett, RRG, University of Oxford
00008 // \date   11 Nov 99
00009 // \brief  See vgui_clear_tableau.h for a description of this class.
00010 
00011 #include "vgui_clear_tableau.h"
00012 #include <vgui/vgui_event.h>
00013 #include <vgui/vgui_menu.h>
00014 #include <vgui/vgui_command.h>
00015 #include <vgui/vgui_dialog.h>
00016 #include <vgui/internals/vgui_accelerate.h>
00017 #include <vcl_sstream.h>
00018 
00019 //-----------------------------------------------------------------------------
00020 //: Constructor - don't use this, use vgui_clear_tableau_new.
00021 //  A vgui_clear_tableau does not have any children.
00022 vgui_clear_tableau::vgui_clear_tableau()
00023 {
00024   // These are the initial glClear() colour
00025   // values given in the OpenGL man pages.
00026   colour[0] = 0;
00027   colour[1] = 0;
00028   colour[2] = 0;
00029   colour[3] = 0;
00030 
00031   clearing_ = true;
00032   mask = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT;
00033   depth = 1;
00034   stencil = 0;
00035 }
00036 
00037 //-----------------------------------------------------------------------------
00038 //: Handle events sent to this tableau - use draw to perform OpenGL clearing.
00039 bool vgui_clear_tableau::handle(const vgui_event& event)
00040 {
00041   if (event.type==vgui_DRAW && clearing_)
00042   {
00043     if (mask & GL_COLOR_BUFFER_BIT)
00044       glClearColor(colour[0], colour[1], colour[2], colour[3]);
00045 
00046     if (mask & GL_DEPTH_BUFFER_BIT)
00047       glClearDepth(depth);
00048 
00049     if (mask & GL_ACCUM_BUFFER_BIT)
00050       glClearAccum(accum[0], accum[1], accum[2], accum[3]);
00051 
00052     if (mask & GL_STENCIL_BUFFER_BIT)
00053       glClearStencil(stencil);
00054 
00055     if (mask)
00056       vgui_accelerate::instance()->vgui_glClear(mask);
00057 
00058     return true;
00059   }
00060 
00061   return false;
00062 }
00063 
00064 //-----------------------------------------------------------------------------
00065 //: Set colour of clear tableau to the given red, green, blue values.
00066 void vgui_clear_tableau::set_colour(float r, float g, float b, float a)
00067 {
00068   colour[0] = r;
00069   colour[1] = g;
00070   colour[2] = b;
00071   colour[3] = a;
00072 }
00073 
00074 //-----------------------------------------------------------------------------
00075 //: Make the given menu the default pop-up menu.
00076 void vgui_clear_tableau::add_popup(vgui_menu &menu)
00077 {
00078   menu.add("Configure",
00079            new vgui_command_simple<vgui_clear_tableau>(this, &vgui_clear_tableau::config_dialog));
00080 
00081   vcl_string clear_label("Toggle clearing ");
00082   if (clearing_) clear_label+="[on]";
00083   else clear_label+="[off]";
00084 
00085   menu.add(clear_label.c_str(),
00086            new vgui_command_simple<vgui_clear_tableau>(this, &vgui_clear_tableau::toggle_clearing));
00087 }
00088 
00089 //-----------------------------------------------------------------------------
00090 //: Toggle clearing on and off.
00091 void vgui_clear_tableau::toggle_clearing()
00092 {
00093   clearing_ = !clearing_;
00094   post_redraw();
00095 }
00096 
00097 //-----------------------------------------------------------------------------
00098 //: Display a dialog box to get data (colour, etc) for the clear tableau.
00099 void vgui_clear_tableau::config_dialog()
00100 {
00101   bool colour_val = (mask & GL_COLOR_BUFFER_BIT) != 0;
00102   bool depth_val = (mask & GL_DEPTH_BUFFER_BIT) != 0;
00103   bool accum_val = (mask & GL_ACCUM_BUFFER_BIT) != 0;
00104   bool stencil_val = (mask & GL_STENCIL_BUFFER_BIT) != 0;
00105 
00106   vgui_dialog mydialog("Clear Config");
00107   mydialog.checkbox("Colour",  colour_val);
00108   mydialog.checkbox("Depth", depth_val);
00109   mydialog.checkbox("Accum", accum_val);
00110   mydialog.checkbox("Stencil", stencil_val);
00111 
00112   vcl_stringstream color_stm;
00113   color_stm << colour[0] << ' ' << colour[1] << ' ' << colour[2];
00114   vcl_string color = color_stm.str();
00115   mydialog.inline_color("Clear Colour",color);
00116   mydialog.field("Alpha", colour[3] );
00117 
00118   if (mydialog.ask())
00119   {
00120     mask = 0;
00121     if (colour_val) mask |= GL_COLOR_BUFFER_BIT;
00122     if (depth_val) mask |= GL_DEPTH_BUFFER_BIT;
00123     if (accum_val) mask |= GL_ACCUM_BUFFER_BIT;
00124     if (stencil_val) mask |= GL_STENCIL_BUFFER_BIT;
00125     
00126     color_stm.str(color);
00127     color_stm >> colour[0] >> colour[1] >> colour[2];
00128   }
00129 }