~3v1n0/nux/x11-conffile-on-unity-only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#include "Nux.h"
#include "HLayout.h"
#include "EditTextBox.h"
#include "DoubleValidator.h"
#include "NumericValuator.h"

#include <string>

namespace nux
{

  const int BTN_WIDTH = 14;
  const int BTN_HEIGHT = 14;

  NumericValuator::NumericValuator()
    :   m_DoubleValidator(0.0, 100.0)
    ,   m_Step(0.1f)
  {
    InitializeLayout();
    InitializeWidgets();
  }

  NumericValuator::~NumericValuator()
  {
  }

  void NumericValuator::InitializeWidgets()
  {
    m_EditLine->SetValidator(&m_DoubleValidator);
    m_EditLine->SetText(std::to_string((long double)m_DoubleValidator.GetMinimum()));

    m_EditLine->SetMinimumSize(2 * DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
    m_EditLine->SetGeometry(Geometry(0, 0, 2 * DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT));

    m_SpinnerDownBtn->SetMinimumSize(BTN_WIDTH, BTN_HEIGHT);
    m_SpinnerDownBtn->SetGeometry(Geometry(0, 0, BTN_WIDTH, BTN_HEIGHT));
    m_SpinnerUpBtn->SetMinimumSize(BTN_WIDTH, BTN_HEIGHT);
    m_SpinnerUpBtn->SetGeometry(Geometry(0, 0, BTN_WIDTH, BTN_HEIGHT));

    hlayout->AddView(m_SpinnerDownBtn, 0);
    hlayout->AddView(m_EditLine, 1);
    hlayout->AddView(m_SpinnerUpBtn, 0);
    hlayout->SetContentDistribution(eStackLeft);

    SetCompositionLayout(hlayout);
  }

  void NumericValuator::InitializeLayout()
  {
    hlayout = new HLayout(NUX_TRACKER_LOCATION);
  }

  void NumericValuator::Draw(GraphicsEngine &graphics_engine, bool /* force_draw */)
  {
    GeometryPositioning gp(eHALeft, eVACenter);
    Geometry GeoPo = ComputeGeometryPositioning(m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry(eTRIANGLE_RIGHT), gp);
    GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eTRIANGLE_RIGHT);

    GeoPo = ComputeGeometryPositioning(m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry(eTRIANGLE_LEFT), gp);
    GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eTRIANGLE_LEFT);

    m_EditLine->QueueDraw();
  }

  void NumericValuator::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
  {
    m_EditLine->ProcessDraw(graphics_engine, force_draw);
  }

  void NumericValuator::SetValue(float value)
  {
    m_fValue = value;

    if (m_fValue < m_DoubleValidator.GetMinimum())
      m_fValue = m_DoubleValidator.GetMinimum();

    if (m_fValue > m_DoubleValidator.GetMaximum())
      m_fValue = m_DoubleValidator.GetMaximum();

    m_EditLine->SetText(std::to_string((long double)m_fValue));
  }

  float NumericValuator::GetValue() const
  {
    return m_fValue;
  }

  void NumericValuator::SetStep(float f)
  {
    m_Step = f;
  }

  float NumericValuator::GetStep()
  {
    return m_Step;
  }


  void NumericValuator::ImplementIncrementBtn()
  {
    SetValue(m_fValue + m_Step);
    sigIncrement.emit();
    sigValueChanged.emit(m_fValue);

    if (m_fValue < m_DoubleValidator.GetMaximum())
    {
      m_UpTimerHandler = GetTimer().AddOneShotTimer(100, m_UpTimerCallback, 0);
      QueueDraw();
    }
  }

  void NumericValuator::ImplementDecrementBtn()
  {
    SetValue(m_fValue - m_Step);
    sigDecrement.emit();
    sigValueChanged.emit(m_fValue);

    if (m_fValue > m_DoubleValidator.GetMinimum())
    {
      m_DownTimerHandler = GetTimer().AddOneShotTimer(100, m_DownTimerCallback, 0);
      QueueDraw();
    }
  }

  void NumericValuator::ImplementValidateEntry()
  {
    double ret = 0;
    ret = CharToDouble(m_EditLine->GetCleanText().c_str());
    {
      m_fValue = ret;

      if (m_fValue < m_DoubleValidator.GetMinimum())
      {
        m_fValue = m_DoubleValidator.GetMinimum();
        m_EditLine->SetText(std::to_string((long double)m_fValue));
      }

      if (m_fValue > m_DoubleValidator.GetMaximum())
      {
        m_fValue = m_DoubleValidator.GetMaximum();
        m_EditLine->SetText(std::to_string((long double)m_fValue));
      }
    }
//     else
//     {
//         m_EditLine->SetText(std::string::Printf("%f", m_fValue));
//     }
  }

}