Back to index

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