nux-1.14.0
RangeValue.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 "EditTextBox.h"
00025 #include "HLayout.h"
00026 #include "RangeValue.h"
00027 
00028 namespace nux
00029 {
00030 
00031   RangeValue::RangeValue (float Value, float MinValue, float MaxValue, NUX_FILE_LINE_DECL)
00032     :   View (NUX_FILE_LINE_PARAM)
00033   {
00034     m_min           = MinValue;
00035     m_max           = MaxValue;
00036     m_StartColor    = Color (0xff202020);
00037     m_EndColor      = Color (0xff202020);
00038     m_ProgressColor = Color (0xff606060);
00039     m_EnableDrawProgress    = true;
00040     m_CTRL_KEY      = 0;
00041 
00042     InitializeLayout();
00043     InitializeWidgets();
00044     SetValue (Value);
00045   }
00046 
00047   RangeValue::~RangeValue()
00048   {
00049   }
00050 
00051   void RangeValue::InitializeWidgets()
00052   {
00054     // Set Signals  //
00056     m_Percentage->mouse_down.connect ( sigc::mem_fun (this, &RangeValue::OnReceiveMouseDown) );
00057     m_Percentage->mouse_up.connect ( sigc::mem_fun (this, &RangeValue::OnReceiveMouseUp) );
00058     m_Percentage->mouse_drag.connect ( sigc::mem_fun (this, &RangeValue::OnReceiveMouseDrag) );
00059 
00060     m_ValueString->sigValidateKeyboardEntry.connect (sigc::mem_fun (this, &RangeValue::OnValidateKeyboardEntry) );
00061 
00063     // Set Geometry //
00065     m_ValueString->SetMinimumSize (DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT);
00066     m_ValueString->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) );
00067 
00068     m_Percentage->SetMinimumSize (2 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT);
00069     m_Percentage->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) );
00070 
00071     // Set the minimum size of this widget.
00072     // This is use by RangeValuePropertyItem::GetItemBestHeight
00073     SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00074 
00075     hlayout->AddView (m_ValueString, 0, eCenter, eFull);
00076     hlayout->AddView (m_Percentage, 4, eCenter, eFull);
00077     //hlayout->AddLayout(&vlayout, 4);
00078     hlayout->SetHorizontalExternalMargin (0);
00079     hlayout->SetHorizontalInternalMargin (2);
00080     hlayout->SetVerticalExternalMargin (0);
00081     SetCompositionLayout (hlayout);
00082   }
00083 
00084   void RangeValue::InitializeLayout()
00085   {
00086     hlayout         = new HLayout (NUX_TRACKER_LOCATION);
00087     m_Percentage    = new InputArea (NUX_TRACKER_LOCATION);
00088     m_ValueString   = new EditTextBox (TEXT (""), NUX_TRACKER_LOCATION);
00089   }
00090 
00091   long RangeValue::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00092   {
00093     m_CTRL_KEY = ievent.GetVirtualKeyState (NUX_VK_LCONTROL);
00094 
00095     long ret;
00096     ret = m_Percentage->OnEvent (ievent, TraverseInfo, ProcessEventInfo);
00097     ret = m_ValueString->ProcessEvent (ievent, ret, ProcessEventInfo);
00098     ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
00099 
00100     if (m_ValueString->IsRedrawNeeded() )
00101     {
00102       QueueDraw();
00103     }
00104 
00105     return ret;
00106   }
00107 
00108   void RangeValue::DrawMarker (GraphicsEngine &GfxContext)
00109   {
00110     int marker_position_x;
00111     int marker_position_y;
00112 
00113     GfxContext.PushClippingRectangle (m_Percentage->GetGeometry() );
00114 
00115     marker_position_x = m_Percentage->GetBaseX() + (m_Value - m_min) * m_Percentage->GetBaseWidth() * 1 / (m_max - m_min);
00116     marker_position_y = m_Percentage->GetBaseY() + m_Percentage->GetBaseHeight();
00117     GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 5, marker_position_y,
00118                                   marker_position_x, marker_position_y - 5,
00119                                   marker_position_x + 5, marker_position_y, Color (0.0f, 0.0f, 0.0f, 1.0f) );
00120 
00121     GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 4, marker_position_y,
00122                                   marker_position_x, marker_position_y - 4,
00123                                   marker_position_x + 4, marker_position_y, Color (0.7f, 0.7f, 0.7f, 1.0f) );
00124 
00125     GfxContext.PopClippingRectangle();
00126   }
00127 
00128 
00129   void RangeValue::Draw (GraphicsEngine &GfxContext, bool force_draw)
00130   {
00131     Geometry base = GetGeometry();
00132 
00133     // Percentage
00134     Geometry P = m_Percentage->GetGeometry();
00135     GetPainter().Paint2DQuadColor (GfxContext, P, m_StartColor, m_StartColor, m_EndColor, m_EndColor);
00136 
00137     if (m_EnableDrawProgress)
00138     {
00139       P.SetWidth ( (m_Value - m_min) * (float) P.GetWidth() / (m_max - m_min) );
00140       GetPainter().Paint2DQuadColor (GfxContext, P, m_ProgressColor);
00141     }
00142 
00143     m_ValueString->ProcessDraw (GfxContext, true);
00144 
00145     DrawMarker (GfxContext);
00146   }
00147 
00148   void RangeValue::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00149   {
00150     m_ValueString->ProcessDraw (GfxContext, force_draw);
00151   }
00152 
00153   void RangeValue::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00154   {
00155 
00156   }
00157 
00159 //  RECEIVERS  //
00161   void RangeValue::SetRange (float min_value, float max_value)
00162   {
00163     if (min_value < max_value)
00164     {
00165       m_min = min_value;
00166       m_max = max_value;
00167     }
00168     else
00169     {
00170       m_min = max_value;
00171       m_max = min_value;
00172     }
00173 
00174     if (m_Value < m_min)
00175       m_Value = m_min;
00176     else if (m_Value > m_max)
00177       m_Value = m_max;
00178 
00179     SetValue (m_Value);
00180   }
00181 
00182   void RangeValue::SetValue (float value)
00183   {
00184     if (value < m_min)
00185       m_Value = m_min;
00186     else if (value > m_max)
00187       m_Value = m_max;
00188     else
00189       m_Value = value;
00190 
00191     m_ValueString->SetText (NString::Printf ("%.3f", m_Value) );
00192     QueueDraw();
00193   }
00194 
00195   float RangeValue::GetValue() const
00196   {
00197     return m_Value;
00198   }
00199 
00201 //  EMITTERS  //
00203   void RangeValue::OnReceiveMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
00204   {
00205     if (x < 0)
00206       m_Value = m_min;
00207     else if (x > m_Percentage->GetBaseWidth() )
00208       m_Value = m_max;
00209     else
00210       m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth();
00211 
00212     m_ValueString->SetText (NString::Printf ("%.3f", m_Value) );
00213     sigValueChanged.emit (this);
00214     sigFloatChanged.emit (m_Value);
00215     sigMouseDown.emit (m_Value);
00216 
00217     QueueDraw();
00218   }
00219 
00220   void RangeValue::OnReceiveMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
00221   {
00222 
00223     if (x < 0)
00224       m_Value = m_min;
00225     else if (x > m_Percentage->GetBaseWidth() )
00226       m_Value = m_max;
00227     else
00228       m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth();
00229 
00230     m_ValueString->SetText (NString::Printf ("%.3f", m_Value) );
00231     sigValueChanged.emit (this);
00232     sigFloatChanged.emit (m_Value);
00233     sigMouseUp.emit (m_Value);
00234 
00235     QueueDraw();
00236   }
00237 
00238   void RangeValue::OnReceiveMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
00239   {
00240     if (x < 0)
00241       m_Value = m_min;
00242     else if (x > m_Percentage->GetBaseWidth() )
00243       m_Value = m_max;
00244     else
00245       m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth();
00246 
00247     m_ValueString->SetText (NString::Printf ("%.3f", m_Value) );
00248     sigValueChanged.emit (this);
00249     sigFloatChanged.emit (m_Value);
00250     sigMouseDrag.emit (m_Value);
00251 
00252     QueueDraw();
00253   }
00254 
00255   void RangeValue::OnKeyboardFocus()
00256   {
00257 
00258   }
00259 
00260   void RangeValue::OnLostKeyboardFocus()
00261   {
00262 
00263   }
00264 
00265   void RangeValue::OnValidateKeyboardEntry (EditTextBox *textbox, const NString &text)
00266   {
00267     float f;
00268     f = CharToDouble (text.GetTCharPtr() );
00269     SetValue (f);
00270     sigValueChanged.emit (this);
00271     sigFloatChanged.emit (m_Value);
00272     sigSetTypedValue.emit (f);
00273     QueueDraw();
00274   }
00275 
00276   void RangeValue::EmitFloatChangedSignal()
00277   {
00278     sigFloatChanged.emit (m_Value);
00279   }
00280 
00281   void RangeValue::SetBackgroundColor (const Color &color)
00282   {
00283     m_ValueString->SetTextBackgroundColor (color);
00284   }
00285 
00286   const Color RangeValue::GetBackgroundColor() const
00287   {
00288     return m_ValueString->GetTextBackgroundColor();
00289   }
00290 
00291 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends