nux-1.16.0
CurveControl.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 "NuxCore/Math/Bezier.h"
00025 #include "CurveControl.h"
00026 
00027 namespace nux
00028 {
00029 
00030   CurveControl::CurveControl (NUX_FILE_LINE_DECL)
00031     :   View (NUX_FILE_LINE_PARAM)
00032     ,   m_minX (0.0f),
00033     m_minY (0.0f),
00034     m_maxX (1.0f),
00035     m_maxY (1.0f),
00036     m_FunctionCallback (0)
00037   {
00038     SetMinimumSize (200, 100);
00039     SetBaseSize (200, 100);
00040   }
00041 
00042   CurveControl::~CurveControl()
00043   {
00044 
00045 
00046   }
00047 
00048 
00049   long CurveControl::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00050   {
00051 
00052     return TraverseInfo;
00053   }
00054 
00055 
00056   void CurveControl::Draw (GraphicsEngine &GfxContext, bool force_draw)
00057   {
00058     Geometry base = GetGeometry();
00059 
00060     GetPainter().PaintBackground (GfxContext, base);
00061     GetPainter().Paint2DQuadWireframe (GfxContext, base, Color (COLOR_BACKGROUND_SECONDARY) );
00062 
00063 #define N 2
00064     int i;
00065     int nsample = 10;
00066     double t;
00067     double xcon[N+1] = { 0.0, 0.5, 1.0 };
00068     double xval;
00069     double ycon[N+1] = { 0.0, 1.0,  0.0 };
00070     double yval;
00071 
00072 
00073     int W = GetBaseWidth() - 2;
00074     int H = GetBaseHeight() - 2;
00075     int X = GetBaseX() + 1;
00076     int Y = GetBaseY() + 1;
00077 
00078     double xprev, yprev;
00079     Bezier_XY (N, 0.0, xcon, ycon, &xprev, &yprev);
00080 
00081     //GetPainter().Draw2DLine(X, Y, X+W, Y+H, Color(0xFFFF0000));
00082 
00083     base.OffsetPosition (1, 1);
00084     base.OffsetSize (-2, -2);
00085 
00086     GfxContext.PushClippingRectangle (base);
00087 
00088     for ( i = 1; i < nsample; i++ )
00089     {
00090       t = ( double ) ( i ) / ( double ) ( nsample - 1 );
00091       Bezier_XY ( N, t, xcon, ycon, &xval, &yval );
00092 
00093       int X0, Y0, X1, Y1;
00094       X0 = X + W * (xprev - m_minX) / (m_maxX - m_minX);
00095       Y0 = Y + H * ( 1 - (yprev - m_minY) / (m_maxY - m_minY) );
00096       X1 = X + W * (xval - m_minX) / (m_maxX - m_minX);
00097       Y1 = Y + H * ( 1 - (yval - m_minY) / (m_maxY - m_minY) );
00098 
00099       GetPainter().Draw2DLine (GfxContext, X0, Y0, X1, Y1, Color (0xFFFF0000) );
00100 
00101       xprev = xval;
00102       yprev = yval;
00103     }
00104 
00105 //    for(int i = 1; i < GetWidth(); i++)
00106 //    {
00107 //        float x1, y1;
00108 //
00109 //        x1 = x0 + dX;
00110 //        y1 = EvalFunction(x1);
00111 //
00112 //        int X0, Y0, X1, Y1;
00113 //        X0 = X + W * (x0 - m_minX) / (m_maxX - m_minX);
00114 //        Y0 = Y - H * (y0 + m_minY) / (m_maxY - m_minY);
00115 //        X1 = X + W * (x1 - m_minX) / (m_maxX - m_minX);
00116 //        Y1 = Y - H * (y1 + m_minY) / (m_maxY - m_minY);
00117 //        GetPainter().Draw2DLine(X0, Y0, X1, Y1, Color(0xFFFF0000));
00118 //
00119 //        x0 = x1;
00120 //        y0 = y1;
00121 //
00122 //    }
00123     GfxContext.PopClippingRectangle();
00124   }
00125 
00126 
00127   void CurveControl::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00128   {
00129 
00130   }
00131 
00132   void CurveControl::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00133   {
00134 
00135   }
00136 
00137 
00138   void CurveControl::SetXAxisBounds (float minX, float maxX)
00139   {
00140     m_minX = minX;
00141     m_maxX = maxX;
00142     QueueDraw();
00143   }
00144 
00145   void CurveControl::SetYAxisBounds (float minY, float maxY)
00146   {
00147     m_minY = minY;
00148     m_maxY = maxY;
00149     QueueDraw();
00150   }
00151 
00152   void CurveControl::SetFunctionCallback (FunctionCallback f)
00153   {
00154     m_FunctionCallback = f;
00155     QueueDraw();
00156   }
00157 
00158   float CurveControl::EvalFunction (float x)
00159   {
00160     if (m_FunctionCallback != 0)
00161       return (*m_FunctionCallback) (x);
00162 
00163     return 0;
00164   }
00165 
00166   void CurveControl::UpdateGraph()
00167   {
00168     QueueDraw();
00169   }
00170 
00171 
00172 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends