nux-1.16.0
|
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 "ColorGradient.h" 00027 00028 namespace nux 00029 { 00030 00031 ColorGradient::ColorGradient (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_BackgroundColor = Color (0xff202020); 00037 m_CTRL_KEY = 0; 00038 m_color_format = color::FLOAT; 00039 00040 InitializeLayout(); 00041 InitializeWidgets(); 00042 SetColorFormat (m_color_format); 00043 SetValue (Value); 00044 } 00045 00046 ColorGradient::~ColorGradient() 00047 { 00048 } 00049 00050 void ColorGradient::InitializeWidgets() 00051 { 00053 // Set Signals // 00055 m_Percentage->mouse_down.connect ( sigc::mem_fun (this, &ColorGradient::OnReceiveMouseDown) ); 00056 m_Percentage->mouse_up.connect ( sigc::mem_fun (this, &ColorGradient::OnReceiveMouseUp) ); 00057 m_Percentage->mouse_drag.connect ( sigc::mem_fun (this, &ColorGradient::OnReceiveMouseDrag) ); 00058 00059 m_ValueString->sigValidateKeyboardEntry.connect (sigc::mem_fun (this, &ColorGradient::OnValidateKeyboardEntry) ); 00060 00062 // Set Geometry // 00064 m_ValueString->SetMinimumSize (DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT); 00065 m_ValueString->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) ); 00066 00067 m_Percentage->SetMinimumSize (2 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT); 00068 m_Percentage->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) ); 00069 00070 // Set the minimum size of this widget. 00071 // This is use by RangeValuePropertyItem::GetItemBestHeight 00072 SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT); 00073 00074 //SetMaximumHeight(20); 00075 // Set layout 00076 00077 hlayout->AddView (m_ValueString, 0, eCenter, eFull); 00078 hlayout->AddView (m_Percentage, 4, eCenter, eFull); 00079 //hlayout->AddLayout(&vlayout, 4); 00080 hlayout->SetHorizontalExternalMargin (0); 00081 hlayout->SetHorizontalInternalMargin (2); 00082 hlayout->SetVerticalExternalMargin (0); 00083 SetCompositionLayout (hlayout); 00084 } 00085 00086 void ColorGradient::InitializeLayout() 00087 { 00088 hlayout = new HLayout (NUX_TRACKER_LOCATION); 00089 m_Percentage = new InputArea (NUX_TRACKER_LOCATION); 00090 m_ValueString = new EditTextBox (TEXT (""), NUX_TRACKER_LOCATION); 00091 } 00092 00093 long ColorGradient::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo) 00094 { 00095 m_CTRL_KEY = ievent.GetVirtualKeyState (NUX_VK_LCONTROL); 00096 00097 long ret; 00098 ret = m_Percentage->OnEvent (ievent, TraverseInfo, ProcessEventInfo); 00099 ret = m_ValueString->ProcessEvent (ievent, ret, ProcessEventInfo); 00100 ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo); 00101 00102 if (m_ValueString->IsRedrawNeeded() ) 00103 { 00104 QueueDraw(); 00105 } 00106 00107 return ret; 00108 } 00109 00110 void ColorGradient::DrawMarker (GraphicsEngine &GfxContext) 00111 { 00112 int marker_position_x; 00113 int marker_position_y; 00114 00115 GfxContext.PushClippingRectangle (m_Percentage->GetGeometry() ); 00116 00117 marker_position_x = m_Percentage->GetBaseX() + (m_Value - m_min) * m_Percentage->GetBaseWidth() * 1 / (m_max - m_min); 00118 marker_position_y = m_Percentage->GetBaseY() + m_Percentage->GetBaseHeight(); 00119 GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 5, marker_position_y, 00120 marker_position_x, marker_position_y - 5, 00121 marker_position_x + 5, marker_position_y, Color (0.0f, 0.0f, 0.0f, 1.0f) ); 00122 00123 GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 4, marker_position_y, 00124 marker_position_x, marker_position_y - 4, 00125 marker_position_x + 4, marker_position_y, Color (0.7f, 0.7f, 0.7f, 1.0f) ); 00126 00127 GfxContext.PopClippingRectangle(); 00128 } 00129 00130 00131 void ColorGradient::Draw (GraphicsEngine &GfxContext, bool force_draw) 00132 { 00133 Geometry base = GetGeometry(); 00134 00135 Geometry P = m_Percentage->GetGeometry(); 00136 00137 int NumColorMark = m_ColorMarkGroup.GetNumColorMark(); 00138 int X = P.x; 00139 00140 if (NumColorMark < 2) 00141 { 00142 GetPainter().Paint2DQuadColor (GfxContext, P, m_BackgroundColor); 00143 } 00144 else 00145 { 00146 for (int i = 0; i < NumColorMark; i++) 00147 { 00148 ColorMarkGroup::ColorMark cmark = m_ColorMarkGroup.GetColorMark (i); 00149 float coeff = cmark.GetX(); 00150 Color color = cmark.GetColor(); 00151 00152 if ( (i == 0) && (coeff > 0) ) 00153 { 00154 Geometry geo (X, P.y, P.GetWidth() *coeff, P.GetHeight() ); 00155 GetPainter().Paint2DQuadColor (GfxContext, geo, color); 00156 X += P.GetWidth() * coeff; 00157 } 00158 else if ( (i == NumColorMark - 1) && (coeff < 1.0f) ) 00159 { 00160 nuxAssert (P.x + P.GetWidth() > X); 00161 Geometry geo (X, P.y, P.x + P.GetWidth() - X, P.GetHeight() ); 00162 GetPainter().Paint2DQuadColor (GfxContext, geo, color); 00163 X = P.GetWidth(); 00164 } 00165 else if (i < NumColorMark - 1) 00166 { 00167 float coeff1 = m_ColorMarkGroup.GetColorMark (i + 1).GetX(); 00168 Color color1 = m_ColorMarkGroup.GetColorMark (i + 1).GetColor(); 00169 Geometry geo (X, P.y, P.GetWidth() *coeff1 - P.GetWidth() *coeff, P.GetHeight() ); 00170 00171 if ( (i == NumColorMark - 2) && (coeff1 == 1.0f) ) 00172 { 00173 // correct rounding errors 00174 geo.SetWidth (P.x + P.GetWidth() - X); 00175 } 00176 00177 GetPainter().Paint2DQuadColor (GfxContext, geo, color, color, color1, color1); 00178 X += P.GetWidth() * coeff1 - P.GetWidth() * coeff; 00179 } 00180 } 00181 } 00182 00183 m_ValueString->ProcessDraw (GfxContext, true); 00184 DrawMarker (GfxContext); 00185 } 00186 00187 void ColorGradient::DrawContent (GraphicsEngine &GfxContext, bool force_draw) 00188 { 00189 m_ValueString->ProcessDraw (GfxContext, force_draw); 00190 } 00191 00192 void ColorGradient::PostDraw (GraphicsEngine &GfxContext, bool force_draw) 00193 { 00194 00195 } 00196 00198 // RECEIVERS // 00200 void ColorGradient::SetRange (float min_value, float max_value) 00201 { 00202 if (min_value < max_value) 00203 { 00204 m_min = min_value; 00205 m_max = max_value; 00206 } 00207 else 00208 { 00209 m_min = max_value; 00210 m_max = min_value; 00211 } 00212 00213 if (m_Value < m_min) 00214 m_Value = m_min; 00215 else if (m_Value > m_max) 00216 m_Value = m_max; 00217 00218 SetValue (m_Value); 00219 } 00220 00221 void ColorGradient::SetValue (float value) 00222 { 00223 if (value < m_min) 00224 m_Value = m_min; 00225 else if (value > m_max) 00226 m_Value = m_max; 00227 else 00228 m_Value = value; 00229 00230 if (m_color_format == color::FLOAT) 00231 { 00232 m_ValueString->SetText (NString::Printf ("%.3f", m_Value ) ); 00233 } 00234 00235 if (m_color_format == color::INT) 00236 { 00237 m_ValueString->SetText (NString::Printf ("%d", (int) (m_Value * 255) ) ); 00238 } 00239 00240 if (m_color_format == color::HEX) 00241 { 00242 m_ValueString->SetText (NString::Printf ("%x", (int) (m_Value * 255) ) ); 00243 } 00244 00245 //m_ValueString->SetText(NString::Printf("%.3f", m_Value)); 00246 QueueDraw(); 00247 } 00248 00249 float ColorGradient::GetValue() const 00250 { 00251 return m_Value; 00252 } 00253 00255 // EMITTERS // 00257 void ColorGradient::OnReceiveMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags) 00258 { 00259 if (x < 0) 00260 m_Value = m_min; 00261 else if (x > m_Percentage->GetBaseWidth() ) 00262 m_Value = m_max; 00263 else 00264 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00265 00266 SetValue (m_Value); 00267 //m_ValueString->SetText(NString::Printf("%.3f", m_Value)); 00268 sigValueChanged.emit (this); 00269 sigFloatChanged.emit (m_Value); 00270 sigMouseDown.emit (m_Value); 00271 00272 QueueDraw(); 00273 } 00274 00275 void ColorGradient::OnReceiveMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags) 00276 { 00277 00278 if (x < 0) 00279 m_Value = m_min; 00280 else if (x > m_Percentage->GetBaseWidth() ) 00281 m_Value = m_max; 00282 else 00283 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00284 00285 SetValue (m_Value); 00286 //m_ValueString->SetText(NString::Printf("%.3f", m_Value)); 00287 sigValueChanged.emit (this); 00288 sigFloatChanged.emit (m_Value); 00289 sigMouseUp.emit (m_Value); 00290 00291 QueueDraw(); 00292 } 00293 00294 void ColorGradient::OnReceiveMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags) 00295 { 00296 if (x < 0) 00297 m_Value = m_min; 00298 else if (x > m_Percentage->GetBaseWidth() ) 00299 m_Value = m_max; 00300 else 00301 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00302 00303 SetValue (m_Value); 00304 //m_ValueString->SetText(NString::Printf("%.3f", m_Value)); 00305 sigValueChanged.emit (this); 00306 sigFloatChanged.emit (m_Value); 00307 sigMouseDrag.emit (m_Value); 00308 00309 QueueDraw(); 00310 } 00311 00312 void ColorGradient::OnKeyboardFocus() 00313 { 00314 00315 } 00316 00317 void ColorGradient::OnLostKeyboardFocus() 00318 { 00319 00320 } 00321 00322 void ColorGradient::OnValidateKeyboardEntry (EditTextBox *textbox, const NString &text) 00323 { 00324 float f = 0; 00325 00326 if ( (m_color_format == color::HEX) && (m_HexRegExp.Validate (text.GetTCharPtr() ) == Validator::Acceptable) ) 00327 { 00328 f = (float) m_HexRegExp.ToInteger (text.GetTCharPtr() ) / 255.0f; 00329 } 00330 else if ( (m_color_format == color::INT) && (m_IntRegExp.Validate (text.GetTCharPtr() ) == Validator::Acceptable) ) 00331 { 00332 f = (float) m_IntRegExp.ToInteger (text.GetTCharPtr() ) / 255.0f; 00333 } 00334 else 00335 { 00336 f = (float) m_DoubleRegExp.ToDouble (text.GetTCharPtr() ); 00337 } 00338 00339 //inlCharToFloat(text.c_str(), f); 00340 SetValue (f); 00341 sigValueChanged.emit (this); 00342 sigFloatChanged.emit (m_Value); 00343 sigSetTypedValue.emit (f); 00344 QueueDraw(); 00345 } 00346 00347 void ColorGradient::EmitFloatChangedSignal() 00348 { 00349 sigFloatChanged.emit (m_Value); 00350 } 00351 00352 void ColorGradient::AddColorMark (DOUBLE x, Color color, bool selected) 00353 { 00354 m_ColorMarkGroup.AddColorMark (x, color, selected); 00355 QueueDraw(); 00356 } 00357 00358 void ColorGradient::Reset() 00359 { 00360 m_ColorMarkGroup.Reset(); 00361 } 00362 00363 void ColorGradient::SetColorFormat (color::Format cf) 00364 { 00365 m_color_format = cf; 00366 if (cf == color::FLOAT) 00367 { 00368 m_ValueString->SetKeyEntryType (BaseKeyboardHandler::eAlphaNumeric); 00369 m_ValueString->SetPrefix (TEXT ("") ); 00370 } 00371 else if (cf == color::INT) 00372 { 00373 m_ValueString->SetKeyEntryType (BaseKeyboardHandler::eIntegerNumber); 00374 m_ValueString->SetPrefix (TEXT ("") ); 00375 } 00376 else if (cf == color::HEX) 00377 { 00378 m_ValueString->SetKeyEntryType (BaseKeyboardHandler::eHexadecimalNumber); 00379 m_ValueString->SetPrefix (TEXT ("0x") ); 00380 } 00381 00382 SetValue (m_Value); 00383 } 00384 00385 00386 }