Back to index

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