summaryrefslogtreecommitdiff
path: root/indra/llui/llxyvector.cpp
blob: 94dc72c86f65a287fcabea8933a92bd4048f296c (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @file llxyvector.cpp
* @author Andrey Lihatskiy
* @brief Implementation for LLXYVector
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2018, Linden Research, Inc.
*
* This library 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;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

// A control that allows to set two related vector magnitudes by manipulating a single vector on a plane. 

#include "linden_common.h"

#include "llxyvector.h"

#include "llstring.h"
#include "llrect.h"

#include "lluictrlfactory.h"
#include "llrender.h"

#include "llmath.h"

// Globals
static LLDefaultChildRegistry::Register<LLXYVector> register_xy_vector("xy_vector");


const F32 CENTER_CIRCLE_RADIUS = 2;
const S32 ARROW_ANGLE = 30;
const S32 ARROW_LENGTH_LONG = 10;
const S32 ARROW_LENGTH_SHORT = 6;

LLXYVector::Params::Params()
  : x_entry("x_entry"),
    y_entry("y_entry"),
    touch_area("touch_area"),
    border("border"),
    edit_bar_height("edit_bar_height", 18),
    padding("padding", 4),
    min_val_x("min_val_x", -1.0f),
    max_val_x("max_val_x", 1.0f),
    increment_x("increment_x", 0.05f),
    min_val_y("min_val_y", -1.0f),
    max_val_y("max_val_y", 1.0f),
    increment_y("increment_y", 0.05f),
    label_width("label_width", 16),
    arrow_color("arrow_color", LLColor4::white),
    ghost_color("ghost_color"),
    area_color("area_color", LLColor4::grey4),
    grid_color("grid_color", LLColor4::grey % 0.25f),
    logarithmic("logarithmic", FALSE)
{
}

LLXYVector::LLXYVector(const LLXYVector::Params& p)
  : LLUICtrl(p),
    mArrowColor(p.arrow_color()),
    mAreaColor(p.area_color),
    mGridColor(p.grid_color),
    mMinValueX(p.min_val_x),
    mMaxValueX(p.max_val_x),
    mIncrementX(p.increment_x),
    mMinValueY(p.min_val_y),
    mMaxValueY(p.max_val_y),
    mIncrementY(p.increment_y),
    mLogarithmic(p.logarithmic),
    mValueX(0),
    mValueY(0)
{
    mGhostColor = p.ghost_color.isProvided() ? p.ghost_color() % 0.3f : p.arrow_color() % 0.3f;

    LLRect border_rect = getLocalRect();
    LLViewBorder::Params params = p.border;
    params.rect(border_rect);
    mBorder = LLUICtrlFactory::create<LLViewBorder>(params);
    addChild(mBorder);

    LLTextBox::Params x_label_params;
    x_label_params.initial_value(p.x_entry.label());
    x_label_params.rect = LLRect(p.padding,
                                 border_rect.mTop - p.padding,
                                 p.label_width,
                                 border_rect.getHeight() - p.edit_bar_height);
    mXLabel = LLUICtrlFactory::create<LLTextBox>(x_label_params);
    addChild(mXLabel);
    LLLineEditor::Params x_params = p.x_entry;
    x_params.rect = LLRect(p.padding + p.label_width,
                           border_rect.mTop - p.padding,
                           border_rect.getCenterX(),
                           border_rect.getHeight() - p.edit_bar_height);
    x_params.commit_callback.function(boost::bind(&LLXYVector::onEditChange, this));
    mXEntry = LLUICtrlFactory::create<LLLineEditor>(x_params);
    mXEntry->setPrevalidateInput(LLTextValidate::validateFloat);
    addChild(mXEntry);

    LLTextBox::Params y_label_params;
    y_label_params.initial_value(p.y_entry.label());
    y_label_params.rect = LLRect(border_rect.getCenterX() + p.padding,
                                 border_rect.mTop - p.padding,
                                 border_rect.getCenterX() + p.label_width,
                                 border_rect.getHeight() - p.edit_bar_height);
    mYLabel = LLUICtrlFactory::create<LLTextBox>(y_label_params);
    addChild(mYLabel);
    LLLineEditor::Params y_params = p.y_entry;
    y_params.rect = LLRect(border_rect.getCenterX() + p.padding + p.label_width,
                           border_rect.getHeight() - p.padding,
                           border_rect.getWidth() - p.padding,
                           border_rect.getHeight() - p.edit_bar_height);
    y_params.commit_callback.function(boost::bind(&LLXYVector::onEditChange, this));
    mYEntry = LLUICtrlFactory::create<LLLineEditor>(y_params);
    mYEntry->setPrevalidateInput(LLTextValidate::validateFloat);
    addChild(mYEntry);

    LLPanel::Params touch_area = p.touch_area;
    touch_area.rect = LLRect(p.padding,
                             border_rect.mTop - p.edit_bar_height - p.padding,
                             border_rect.getWidth() - p.padding,
                             p.padding);
    mTouchArea = LLUICtrlFactory::create<LLPanel>(touch_area);
    addChild(mTouchArea);
}

LLXYVector::~LLXYVector()
{
}

BOOL LLXYVector::postBuild()
{
    mLogScaleX = (2 * log(mMaxValueX)) / mTouchArea->getRect().getWidth();
    mLogScaleY = (2 * log(mMaxValueY)) / mTouchArea->getRect().getHeight();

    return TRUE;
}

void drawArrow(S32 tailX, S32 tailY, S32 tipX, S32 tipY, LLColor4 color)
{
    gl_line_2d(tailX, tailY, tipX, tipY, color);

    S32 dx = tipX - tailX;
    S32 dy = tipY - tailY;

    S32 arrowLength = (abs(dx) < ARROW_LENGTH_LONG && abs(dy) < ARROW_LENGTH_LONG) ? ARROW_LENGTH_SHORT : ARROW_LENGTH_LONG;
   
    F32 theta = std::atan2(dy, dx);

    F32 rad = ARROW_ANGLE * std::atan(1) * 4 / 180;
    F32 x = tipX - arrowLength * cos(theta + rad);
    F32 y = tipY - arrowLength * sin(theta + rad);
    F32 rad2 = -1 * ARROW_ANGLE * std::atan(1) * 4 / 180;
    F32 x2 = tipX - arrowLength * cos(theta + rad2);
    F32 y2 = tipY - arrowLength * sin(theta + rad2);
    gl_triangle_2d(tipX, tipY, x, y, x2, y2, color, true);
}

void LLXYVector::draw()
{
    S32 centerX = mTouchArea->getRect().getCenterX();
    S32 centerY = mTouchArea->getRect().getCenterY();
    S32 pointX;
    S32 pointY;

    if (mLogarithmic)
    {
        pointX = (log(llabs(mValueX) + 1)) / mLogScaleX;
        pointX *= (mValueX < 0) ? -1 : 1;
        pointX += centerX;

        pointY = (log(llabs(mValueY) + 1)) / mLogScaleY;
        pointY *= (mValueY < 0) ? -1 : 1;
        pointY += centerY;
    }
    else // linear
    {
        pointX = centerX + (mValueX * mTouchArea->getRect().getWidth() / (2 * mMaxValueX));
        pointY = centerY + (mValueY * mTouchArea->getRect().getHeight() / (2 * mMaxValueY));
    }

    // fill
    gl_rect_2d(mTouchArea->getRect(), mAreaColor, true);

    // draw grid
    gl_line_2d(centerX, mTouchArea->getRect().mTop, centerX, mTouchArea->getRect().mBottom, mGridColor);
    gl_line_2d(mTouchArea->getRect().mLeft, centerY, mTouchArea->getRect().mRight, centerY, mGridColor);

    // draw ghost
    if (hasMouseCapture())
    {
        drawArrow(centerX, centerY, mGhostX, mGhostY, mGhostColor);
    }
    else
    {
        mGhostX = pointX;
        mGhostY = pointY;
    }

    if (abs(mValueX) >= mIncrementX || abs(mValueY) >= mIncrementY)
    {
        // draw the vector arrow
        drawArrow(centerX, centerY, pointX, pointY, mArrowColor);
    }
    else
    {
        // skip the arrow, set color for center circle
        gGL.color4fv(mArrowColor.get().mV);
    }

    // draw center circle
    gl_circle_2d(centerX, centerY, CENTER_CIRCLE_RADIUS, 12, true);

    LLView::draw();
}

void LLXYVector::onEditChange()
{
    if (getEnabled())
    {
        setValueAndCommit(mXEntry->getValue().asReal(), mYEntry->getValue().asReal());
    }
}

void LLXYVector::setValue(const LLSD& value)
{
    if (value.isArray())
    {
        setValue(value[0].asReal(), value[1].asReal());
    }
}

void LLXYVector::setValue(F32 x, F32 y)
{
    mValueX = ll_round(llclamp(x, mMinValueX, mMaxValueX), mIncrementX);
    mValueY = ll_round(llclamp(y, mMinValueY, mMaxValueY), mIncrementY);

    update();
}

void LLXYVector::setValueAndCommit(F32 x, F32 y)
{
    if (mValueX != x || mValueY != y)
    {
        setValue(x, y);
        onCommit();
    }
}

LLSD LLXYVector::getValue() const
{
    LLSD value;
    value.append(mValueX);
    value.append(mValueY);
    return value;
}

void LLXYVector::update()
{
    mXEntry->setValue(mValueX);
    mYEntry->setValue(mValueY);
}

bool LLXYVector::handleHover(S32 x, S32 y, MASK mask)
{
    if (hasMouseCapture())
    {
        if (mLogarithmic)
        {
            F32 valueX = llfastpow(F_E, mLogScaleX*(llabs(x - mTouchArea->getRect().getCenterX()))) - 1;
            valueX *= (x < mTouchArea->getRect().getCenterX()) ? -1 : 1;

            F32 valueY = llfastpow(F_E, mLogScaleY*(llabs(y - mTouchArea->getRect().getCenterY()))) - 1;
            valueY *= (y < mTouchArea->getRect().getCenterY()) ? -1 : 1;

            setValueAndCommit(valueX, valueY);
        }
        else //linear
        {
            F32 valueX = 2 * mMaxValueX * F32(x - mTouchArea->getRect().getCenterX()) / mTouchArea->getRect().getWidth();
            F32 valueY = 2 * mMaxValueY * F32(y - mTouchArea->getRect().getCenterY()) / mTouchArea->getRect().getHeight();

            setValueAndCommit(valueX, valueY);
        }
    }

    return true;
}

bool LLXYVector::handleMouseUp(S32 x, S32 y, MASK mask)
{
    if (hasMouseCapture())
    {
        gFocusMgr.setMouseCapture(NULL);
        make_ui_sound("UISndClickRelease");
    }

    if (mTouchArea->getRect().pointInRect(x, y))
    {
        return true;
    }
    else
    {
        return LLUICtrl::handleMouseUp(x, y, mask);
    }
}

bool LLXYVector::handleMouseDown(S32 x, S32 y, MASK mask)
{

    if (mTouchArea->getRect().pointInRect(x, y))
    {
        gFocusMgr.setMouseCapture(this);
        make_ui_sound("UISndClick");

        return true;
    }
    else
    {
        return LLUICtrl::handleMouseDown(x, y, mask);
    }
}