nux-1.16.0
Histogram.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "Histogram.h"
00025 
00026 namespace nux
00027 {
00028 
00029   Histogram::Histogram (NUX_FILE_LINE_DECL)
00030     :   View (NUX_FILE_LINE_PARAM)
00031     ,   m_minX (0)
00032     ,   m_minY (0)
00033     ,   m_maxX (100)
00034     ,   m_maxY (20)
00035   {
00036 
00037     SetMinimumSize (100, 100);
00038     //SetMinMaxSize(100, 100);
00039     SetBaseSize (200, 100);
00040 
00041     for (int i = 0; i < m_maxX - m_minX; i++)
00042       m_HistogramData.push_back (/*5*pow(i * Pi/(300.0f)/1.3f, 0.9f)*/ + cos (i * Const::Pi / (100.0f) / 1.7f) * (15 + RandomUInt (3 + 7 * i / 100.0f) ) );
00043 
00044     m_DrawFunctionShader = new GLSh_DrawFunction();
00045 
00046     NString Path = NUX_FINDRESOURCELOCATION (TEXT ("Data/UITextures/FunctionGraphBackground.tga"));
00047     BaseTexture* BackgroundTexture = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture ();
00048     BackgroundTexture->Update (Path.GetTCharPtr());
00049 
00050     TexCoordXForm texxform;
00051     texxform.SetTexCoordType (TexCoordXForm::OFFSET_COORD);
00052     texxform.SetWrap (TEXWRAP_REPEAT, TEXWRAP_REPEAT);
00053     m_BackgroundLayer = new TextureLayer (BackgroundTexture->GetDeviceTexture(), texxform, color::White);
00054 
00055     BackgroundTexture->UnReference ();
00056   }
00057 
00058   Histogram::~Histogram()
00059   {
00060     NUX_SAFE_DELETE (m_DrawFunctionShader);
00061     NUX_SAFE_DELETE (m_BackgroundLayer);
00062   }
00063 
00064   long Histogram::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00065   {
00066     return TraverseInfo;
00067   }
00068 
00069   void Histogram::Draw (GraphicsEngine &GfxContext, bool force_draw)
00070   {
00071     Geometry base = GetGeometry();
00072 
00073     GetPainter().PaintBackground (GfxContext, base);
00074     GetPainter().Paint2DQuadWireframe (GfxContext, base, Color (COLOR_BACKGROUND_SECONDARY) );
00075 
00076     int W = GetBaseWidth() - 2;
00077     int H = GetBaseHeight() - 2;
00078     int X = GetBaseX() + 1;
00079     int Y = GetBaseY() + 1;
00080 
00081     float dX = float (m_maxX - m_minX) / W;
00082 
00083     float x0, y0;
00084     x0 = m_minX;
00085     y0 = m_HistogramData[0];
00086 
00087     base.OffsetPosition (1, 1);
00088     base.OffsetSize (-2, -2);
00089 
00090     GfxContext.PushClippingRectangle (base);
00091 
00092     if (Texture.IsNull() || (Texture->GetWidth() != (t_s32) m_HistogramData.size() ) )
00093       Texture = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture ( (t_s32) m_HistogramData.size(), 1, 0, BITFMT_A8);
00094 
00095     float tex_dx = (m_maxX - m_minX) / Texture->GetWidth();
00096 
00097     SURFACE_LOCKED_RECT lockrect;
00098 
00099     Texture->LockRect (0, &lockrect, 0);
00100     BYTE *dest = (BYTE *) lockrect.pBits;
00101 
00102     for (t_s32 i = 0; i < Texture->GetWidth(); i++)
00103     {
00104       float y = 0;
00105 
00106       if (m_HistogramData.size() == 0)
00107         y = 0;
00108       else
00109         y = m_HistogramData[Clamp<int> (int (m_minX + i*tex_dx), 0, (t_s32) m_HistogramData.size()-1) ];
00110 
00111       y = float (y - m_minY) / float (m_maxY - m_minY);
00112 
00113       for (t_s32 j = 0; j < Texture->GetHeight(); j++)
00114       {
00115         dest[1*i + 0 + j *lockrect.Pitch] = 255 * Clamp<float> (y, 0.0f, 1.0f);
00116 //            dest[4*i + 1 + j * lockrect.Pitch] = 255 * Clamp<float>(y, 0.0f, 1.0f);
00117 //            dest[4*i + 2 + j * lockrect.Pitch] = 255 * Clamp<float>(y, 0.0f, 1.0f);
00118 //            dest[4*i + 3 + j * lockrect.Pitch] = 255 * Clamp<float>(y, 0.0f, 1.0f);
00119       }
00120     }
00121 
00122     Texture->UnlockRect (0);
00123 
00124     m_BackgroundLayer->SetGeometry (base);
00125     m_BackgroundLayer->Renderlayer (GfxContext);
00126 
00127     GfxContext.GetRenderStates().SetBlend (TRUE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00128 
00129     m_DrawFunctionShader->SetTextureFunction (Texture);
00130     m_DrawFunctionShader->SetBackgroundColor (Color(0.1f, 0.1f, 0.1f, 0.6f));
00131     m_DrawFunctionShader->Render (X, Y, 0, W, H, GfxContext.GetWindowWidth(), GfxContext.GetWindowHeight() );
00132 
00133     GfxContext.GetRenderStates().EnableLineSmooth (TRUE, 1, GL_FASTEST);   //You need this blending formula to get anti-aliased lines
00134 
00135     for (int i = 1; i < GetBaseWidth(); i++)
00136     {
00137       float x1, y1;
00138       x1 = x0 + dX;
00139 
00140       if (m_HistogramData.size() == 0)
00141         y1 = 0;
00142       else
00143         y1 = m_HistogramData[Clamp<int> (int (x1), 0, (t_s32) m_HistogramData.size()-1) ];
00144 
00145       int X0, Y0, X1, Y1;
00146       X0 = X + W * (x0 - m_minX) / (m_maxX - m_minX);
00147       Y0 = Y + H * ( 1 - (y0 - m_minY) / (m_maxY - m_minY) );
00148       X1 = X + W * (x1 - m_minX) / (m_maxX - m_minX);
00149       Y1 = Y + H * ( 1 - (y1 - m_minY) / (m_maxY - m_minY) );
00150       GetPainter().Draw2DLine (GfxContext, X0, Y0, X1, Y1, Color (0xFFFFFFFF) );
00151 
00152       x0 = x1;
00153       y0 = y1;
00154     }
00155 
00156     GfxContext.GetRenderStates().EnableLineSmooth (FALSE);
00157     GfxContext.GetRenderStates().SetBlend (GL_FALSE);
00158 
00159     GfxContext.PopClippingRectangle();
00160   }
00161 
00162   void Histogram::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00163   {
00164 
00165   }
00166 
00167   void Histogram::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00168   {
00169 
00170   }
00171 
00172   void Histogram::setXAxisBounds (int minX, int maxX)
00173   {
00174     //m_minX = minX;
00175     //m_maxX = maxX;
00176 
00177     //Texture = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(m_maxX - m_minX, 4, 0, BITFMT_R8G8B8A8);
00178     QueueDraw();
00179   }
00180 
00181   void Histogram::setYAxisBounds (int minY, int maxY)
00182   {
00183     m_minY = minY;
00184     m_maxY = maxY;
00185     QueueDraw();
00186   }
00187 
00188   void Histogram::SetHistogram (std::vector<int>& HistogramData)
00189   {
00190     if (HistogramData.size() == 0)
00191       return;
00192 
00193     m_HistogramData = HistogramData;
00194     m_minX = 0;
00195     m_maxX = (t_s32) m_HistogramData.size();
00196     QueueDraw();
00197   }
00198 
00199 
00200 
00201 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends