nux-1.16.0
Matrix4Editor.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 "Matrix4Editor.h"
00025 
00026 namespace nux
00027 {
00028 
00029   static void ThreadMatrix4EditorDialog (NThread *thread, void *InitData)
00030   {
00031     VLayout *MainLayout (new VLayout (NUX_TRACKER_LOCATION) );
00032     Matrix4Editor *matrixeditor (new Matrix4Editor (Matrix4::IDENTITY(), NUX_TRACKER_LOCATION) );
00033     matrixeditor->ComputeChildLayout(); // necessary so all element of the widget get their rightful size.
00034     Matrix4DialogProxy *matrixeditorproxy = static_cast<Matrix4DialogProxy *> (InitData);
00035 
00036     if (matrixeditorproxy)
00037     {
00038       matrixeditor->SetMatrix (matrixeditorproxy->GetMatrix() );
00039       matrixeditor->sigMatrixChanged.connect (sigc::mem_fun (matrixeditorproxy, &Matrix4DialogProxy::RecvDialogChange) );
00040     }
00041 
00042     HLayout *ButtonLayout (new HLayout (TEXT ("Dialog Buttons") ) );
00043 
00044     ToggleButton *OkButton (new ToggleButton ("OK"));
00045     OkButton->SetMinimumWidth (60);
00046     OkButton->SetMinimumHeight (20);
00047 
00048     ToggleButton *CancelButton (new ToggleButton ("Cancel"));
00049     CancelButton->SetMinimumWidth (60);
00050     CancelButton->SetMinimumHeight (20);
00051 
00052     //FIXME - OkButton->sigClick.connect (sigc::mem_fun (static_cast<WindowThread *> (thread), &WindowThread::TerminateThread) );
00053     //FIXME - OkButton->sigClick.connect (sigc::bind (sigc::mem_fun (matrixeditorproxy, &Matrix4DialogProxy::RecvDialogOk), matrixeditor) );
00054     //FIXME - CancelButton->sigClick.connect (sigc::bind (sigc::mem_fun (matrixeditorproxy, &Matrix4DialogProxy::RecvDialogCancel), matrixeditor) );
00055     //FIXME - CancelButton->sigClick.connect (sigc::mem_fun (static_cast<WindowThread *> (thread), &WindowThread::TerminateThread) );
00056 
00057     ButtonLayout->SetHorizontalInternalMargin (6);
00058     ButtonLayout->SetVerticalExternalMargin (2);
00059     ButtonLayout->AddView (OkButton, 0);
00060     ButtonLayout->AddView (CancelButton, 0);
00061 
00062     MainLayout->AddView (matrixeditor);
00063     MainLayout->AddLayout (ButtonLayout, 0);
00064     static_cast<WindowThread *> (thread)->SetLayout (MainLayout);
00065 
00066     MainLayout->SetBaseWidth (1);
00067     MainLayout->SetBaseHeight (1);
00068     MainLayout->ComputeLayout2();
00069     static_cast<WindowThread *> (thread)->SetWindowSize (MainLayout->GetBaseWidth(), MainLayout->GetBaseHeight() );
00070 
00071     // Call StopThreadMonitoring in case the dialog was close by clicking the window close button.
00072     //matrixeditorproxy->StopThreadMonitoring();
00073     //delete CancelButton;
00074     //delete OkButton;
00075     //delete matrixeditor;
00076   }
00077 
00078   Matrix4DialogProxy::Matrix4DialogProxy (bool ModalWindow)
00079     :   m_bDialogChange (false)
00080     ,   m_bDialogRunning (false)
00081     ,   m_ModalWindow (ModalWindow)
00082   {
00083 
00084   }
00085 
00086   Matrix4DialogProxy::~Matrix4DialogProxy()
00087   {
00088   }
00089 
00090   void Matrix4DialogProxy::Start()
00091   {
00092     m_PreviousMatrix = m_Matrix;
00093     m_Thread = CreateModalWindowThread (WINDOWSTYLE_TOOL, TEXT ("Matrix Editor"), 290, 230, GetWindowThread (),
00094                                         ThreadMatrix4EditorDialog,
00095                                         this);
00096 
00097     if (m_Thread)
00098     {
00099       m_DialogThreadID = m_Thread->GetThreadId();
00100       m_Thread->Start (0);
00101     }
00102 
00103     m_bDialogRunning = true;
00104   }
00105 
00106   bool Matrix4DialogProxy::IsActive()
00107   {
00108     return (m_Thread && (m_Thread->GetThreadState() != THREADSTOP) && m_bDialogRunning);
00109   }
00110 
00111   void Matrix4DialogProxy::RecvDialogOk (Matrix4Editor *matrixeditor)
00112   {
00113     m_Matrix = matrixeditor->GetMatrix();
00114     m_bDialogChange = true;
00115     m_bDialogRunning = false;
00116   }
00117 
00118   void Matrix4DialogProxy::RecvDialogCancel (Matrix4Editor *matrixeditor)
00119   {
00120     m_Matrix = m_PreviousMatrix;
00121     m_bDialogChange = true;
00122     m_bDialogRunning = false;
00123   }
00124 
00125   void Matrix4DialogProxy::RecvDialogChange (Matrix4Editor *matrixeditor)
00126   {
00127     m_Matrix = matrixeditor->GetMatrix();
00128     m_bDialogChange = true;
00129   }
00130 
00131   void Matrix4DialogProxy::StopThreadMonitoring()
00132   {
00133     m_Matrix = m_PreviousMatrix;
00134     m_bDialogChange = true;
00135     m_bDialogRunning = false;
00136     m_DialogThreadID = 0;
00137     m_Thread = 0;
00138   }
00139 
00140   Matrix4Editor::Matrix4Editor (Matrix4 matrix, NUX_FILE_LINE_DECL)
00141     :   View (NUX_FILE_LINE_PARAM)
00142     ,   m_Matrix (matrix)
00143   {
00144     m_vlayout           = new VLayout ("Matrix4", NUX_TRACKER_LOCATION);
00145     mtx_layout          = new VLayout ("MatrixEdit", NUX_TRACKER_LOCATION);
00146     m_MtxFunctionLayout = new HLayout (NUX_TRACKER_LOCATION);
00147 
00148     mtx_row_layout[0]   = new HLayout (NUX_TRACKER_LOCATION);
00149     mtx_row_layout[1]   = new HLayout (NUX_TRACKER_LOCATION);
00150     mtx_row_layout[2]   = new HLayout (NUX_TRACKER_LOCATION);
00151     mtx_row_layout[3]   = new HLayout (NUX_TRACKER_LOCATION);
00152 
00153     m_IdentityMtxBtn    = new ToggleButton ("", NUX_TRACKER_LOCATION);
00154     m_ZeroMtxBtn        = new ToggleButton ("", NUX_TRACKER_LOCATION);
00155     m_InverseMtxBtn     = new ToggleButton ("", NUX_TRACKER_LOCATION);
00156     m_NegateMtxBtn      = new ToggleButton ("", NUX_TRACKER_LOCATION);
00157 
00158     //FIXME - m_IdentityMtxBtn->sigClick.connect (sigc::mem_fun (this, &Matrix4Editor::RecvIdentityMatrixCmd) );
00159     //FIXME - m_ZeroMtxBtn->sigClick.connect (sigc::mem_fun (this, &Matrix4Editor::RecvZeroMatrixCmd) );
00160     //FIXME - m_InverseMtxBtn->sigClick.connect (sigc::mem_fun (this, &Matrix4Editor::RecvInverseMatrixCmd) );
00161     //FIXME - m_NegateMtxBtn->sigClick.connect (sigc::mem_fun (this, &Matrix4Editor::RecvNegateMatrixCmd) );
00162 
00163     for (int i = 0; i < 4; i++)
00164     {
00165       for (int j = 0; j < 4; j++)
00166       {
00167         m_MtxInput[i][j] = new EditTextBox (TEXT (""), NUX_TRACKER_LOCATION);
00168         m_MtxInput[i][j]->SetMinimumSize (DEFAULT_WIDGET_WIDTH + 5, PRACTICAL_WIDGET_HEIGHT);
00169         m_MtxInput[i][j]->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT) );
00170         m_MtxInput[i][j]->sigValidateKeyboardEntry.connect (
00171           sigc::bind (sigc::mem_fun (this, &Matrix4Editor::RecvComponentInput), i * 4 + j) );
00172 
00173       }
00174     }
00175 
00176     for (int i = 0; i < 4; i++)
00177     {
00178       mtx_row_layout[i]->SetHorizontalInternalMargin (4);
00179       mtx_row_layout[i]->SetContentDistribution (eStackLeft);
00180     }
00181 
00182     for (int i = 0; i < 4; i++)
00183     {
00184       for (int j = 0; j < 4; j++)
00185       {
00186         mtx_row_layout[i]->AddView (m_MtxInput[i][j], 0, eCenter, eMatchContent);
00187       }
00188     }
00189 
00190     for (int i = 0; i < 4; i++)
00191     {
00192       mtx_layout->AddLayout (mtx_row_layout[i], 0, eCenter);
00193     }
00194 
00195     mtx_layout->SetContentDistribution (eStackExpand);
00196 
00197     //FIXME - m_IdentityMtxBtn->SetCaption (TEXT ("Id") );
00198     //FIXME - m_ZeroMtxBtn->SetCaption (TEXT ("Zero") );
00199     //FIXME - m_InverseMtxBtn->SetCaption (TEXT ("Inv") );
00200     //FIXME - m_NegateMtxBtn->SetCaption (TEXT ("+/-") );
00201 
00202     m_MtxFunctionLayout->AddView (m_IdentityMtxBtn, 0);
00203     m_MtxFunctionLayout->AddView (m_ZeroMtxBtn, 0);
00204     m_MtxFunctionLayout->AddView (m_InverseMtxBtn, 0);
00205     m_MtxFunctionLayout->AddView (m_NegateMtxBtn, 0);
00206     m_MtxFunctionLayout->SetVerticalExternalMargin (4);
00207     m_MtxFunctionLayout->SetHorizontalExternalMargin (0);
00208     m_MtxFunctionLayout->SetHorizontalInternalMargin (2);
00209     m_MtxFunctionLayout->SetContentDistribution (eStackLeft);
00210     //mtx_layout->AddLayout(&m_MtxFunctionLayout, 1,  eCenter, eMatchContent);
00211     mtx_layout->SetContentDistribution (eStackCenter);
00212     mtx_layout->SetHorizontalExternalMargin (4);
00213     mtx_layout->SetVerticalExternalMargin (4);
00214     mtx_layout->SetVerticalInternalMargin (4);
00215 
00216     m_vlayout->AddLayout(mtx_layout, 0, eCenter, eMatchContent);
00217     m_vlayout->AddLayout(m_MtxFunctionLayout, 0, eCenter, eMatchContent);
00218     m_vlayout->SetContentDistribution(MAJOR_POSITION_CENTER);
00219 
00220     SetCompositionLayout (m_vlayout);
00221     WriteMatrix();
00222   }
00223 
00224   Matrix4Editor::~Matrix4Editor()
00225   {
00226   }
00227 
00228   void Matrix4Editor::SetMatrix (Matrix4 matrix)
00229   {
00230     m_Matrix = matrix;
00231 
00232     for (int i = 0; i < 4; i++)
00233     {
00234       for (int j = 0; j < 4; j++)
00235       {
00236         m_MtxInput[i][j]->SetText (NString::Printf (TEXT ("%.3f"), m_Matrix.m[i][j]) );
00237       }
00238     }
00239   }
00240 
00241   Matrix4 Matrix4Editor::GetMatrix() const
00242   {
00243     return m_Matrix;
00244   }
00245 
00246   void Matrix4Editor::RecvComponentInput (EditTextBox *textbox, const NString &text, int componentIndex)
00247   {
00248     int i = componentIndex / 4;
00249     int j = componentIndex - 4 * i;
00250 
00251     float f = 0;
00252     f = CharToDouble (text.GetTCharPtr() );
00253     m_MtxInput[i][j]->SetText (NString::Printf (TEXT ("%.3f"), f) );
00254     m_Matrix.m[i][j] = f;
00255 
00256     sigMatrixChanged.emit (this);
00257   }
00258 
00259   void Matrix4Editor::WriteMatrix()
00260   {
00261     for (int i = 0; i < 4; i++)
00262     {
00263       for (int j = 0; j < 4; j++)
00264       {
00265         m_MtxInput[i][j]->SetText (NString::Printf (TEXT ("%.3f"), m_Matrix.m[i][j]) );
00266       }
00267     }
00268   }
00269 
00270   Area* Matrix4Editor::FindAreaUnderMouse(const Point& mouse_position, NuxEventType event_type)
00271   {
00272     return View::FindAreaUnderMouse(mouse_position, event_type);
00273   }
00274 
00275   void Matrix4Editor::Draw (GraphicsEngine &GfxContext, bool force_draw)
00276   {
00277     Geometry base = GetGeometry();
00278 
00279     //GetPainter().PushShapeBackground(GfxContext, base, eSHAPE_CORNER_ROUND4, 0xFF4D4D4D, true, false);
00280 
00281     for (int i = 0; i < 4; i++)
00282     {
00283       for (int j = 0; j < 4; j++)
00284       {
00285         m_MtxInput[i][j]->QueueDraw();
00286       }
00287     }
00288 
00289     m_IdentityMtxBtn->QueueDraw();
00290     m_ZeroMtxBtn->QueueDraw();
00291     m_InverseMtxBtn->QueueDraw();
00292     m_NegateMtxBtn->QueueDraw();
00293     //GetPainter().PopBackground();
00294 
00295     //GetPainter().PopBackground();
00296   }
00297 
00298   void Matrix4Editor::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00299   {
00300     Geometry base = GetGeometry();
00301 
00302     if (force_draw)
00303       GetPainter().PushDrawShapeLayer (GfxContext, mtx_layout->GetGeometry(), eSHAPE_CORNER_ROUND4, Color (0xFF000000), eAllCorners);
00304     else
00305       GetPainter().PushShapeLayer (GfxContext, mtx_layout->GetGeometry(), eSHAPE_CORNER_ROUND4, Color (0xFF000000), eAllCorners);
00306 
00307     for (int i = 0; i < 4; i++)
00308     {
00309       for (int j = 0; j < 4; j++)
00310       {
00311         m_MtxInput[i][j]->ProcessDraw (GfxContext, force_draw);
00312       }
00313     }
00314 
00315     m_IdentityMtxBtn->ProcessDraw (GfxContext, force_draw);
00316     m_ZeroMtxBtn->ProcessDraw (GfxContext, force_draw);
00317     m_InverseMtxBtn->ProcessDraw (GfxContext, force_draw);
00318     m_NegateMtxBtn->ProcessDraw (GfxContext, force_draw);
00319     GetPainter().PopBackground();
00320   }
00321 
00322   void Matrix4Editor::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00323   {
00324 
00325   }
00326 
00327   void Matrix4Editor::SetParameterName (const char *parameter_name)
00328   {
00329     QueueDraw();
00330   }
00331 
00333 //  RECEIVERS  //
00335   void Matrix4Editor::EmitIncrementComponent (int index)
00336   {
00337 
00338     //sigSetVector.emit(m_x, m_y, m_z, m_w);
00339   }
00340 
00341   void Matrix4Editor::EmitDecrementComponent (int index)
00342   {
00343 
00344     //sigSetVector.emit(m_x, m_y, m_z, m_w);
00345   }
00346 
00347   void Matrix4Editor::EmitComponentValue (float f, int index)
00348   {
00349 
00350     //sigSetVector.emit(m_x, m_y, m_z, m_w);
00351   }
00352 
00353 
00354   void Matrix4Editor::RecvIdentityMatrixCmd()
00355   {
00356     m_Matrix.Identity();
00357     WriteMatrix();
00358     sigMatrixChanged.emit (this);
00359 
00360     QueueDraw();
00361   }
00362 
00363   void Matrix4Editor::RecvZeroMatrixCmd()
00364   {
00365     m_Matrix.Zero();
00366     WriteMatrix();
00367     sigMatrixChanged.emit (this);
00368 
00369     QueueDraw();
00370   }
00371 
00372   void Matrix4Editor::RecvInverseMatrixCmd()
00373   {
00374     m_Matrix.Inverse();
00375     WriteMatrix();
00376     sigMatrixChanged.emit (this);
00377 
00378     QueueDraw();
00379   }
00380 
00381   void Matrix4Editor::RecvNegateMatrixCmd()
00382   {
00383     m_Matrix = -m_Matrix;
00384     WriteMatrix();
00385     sigMatrixChanged.emit (this);
00386 
00387     QueueDraw();
00388   }
00389 
00390 
00391 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends