summaryrefslogtreecommitdiff
path: root/indra/llui/llslider.cpp
blob: 0507733fd809d3b8ee411d4aa2744abd4800b625 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
 * @file llslider.cpp
 * @brief LLSlider base class
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, 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$
 */

#include "linden_common.h"

#include "llslider.h"
#include "llui.h"

#include "llgl.h"
#include "llwindow.h"
#include "llfocusmgr.h"
#include "llkeyboard.h"         // for the MASK constants
#include "llcontrol.h"
#include "lluictrlfactory.h"

static LLDefaultChildRegistry::Register<LLSlider> r1("slider_bar");
//FIXME: make this into an unregistered template so that code constructed sliders don't
// have ambigious template lookup problem

LLSlider::Params::Params()
:   orientation ("orientation", std::string ("horizontal")),
    thumb_outline_color("thumb_outline_color"),
    thumb_center_color("thumb_center_color"),
    thumb_image("thumb_image"),
    thumb_image_pressed("thumb_image_pressed"),
    thumb_image_disabled("thumb_image_disabled"),
    track_image_horizontal("track_image_horizontal"),
    track_image_vertical("track_image_vertical"),
    track_highlight_horizontal_image("track_highlight_horizontal_image"),
    track_highlight_vertical_image("track_highlight_vertical_image"),
    mouse_down_callback("mouse_down_callback"),
    mouse_up_callback("mouse_up_callback")
{}

LLSlider::LLSlider(const LLSlider::Params& p)
:   LLF32UICtrl(p),
    mMouseOffset( 0 ),
    mOrientation ((p.orientation() == "horizontal") ? HORIZONTAL : VERTICAL),
    mThumbOutlineColor(p.thumb_outline_color()),
    mThumbCenterColor(p.thumb_center_color()),
    mThumbImage(p.thumb_image),
    mThumbImagePressed(p.thumb_image_pressed),
    mThumbImageDisabled(p.thumb_image_disabled),
    mTrackImageHorizontal(p.track_image_horizontal),
    mTrackImageVertical(p.track_image_vertical),
    mTrackHighlightHorizontalImage(p.track_highlight_horizontal_image),
    mTrackHighlightVerticalImage(p.track_highlight_vertical_image),
    mMouseDownSignal(NULL),
    mMouseUpSignal(NULL)
{
    mViewModel->setValue(p.initial_value);
    updateThumbRect();
    mDragStartThumbRect = mThumbRect;
    setControlName(p.control_name, NULL);
    setValue(getValueF32());

    if (p.mouse_down_callback.isProvided())
    {
        setMouseDownCallback(initCommitCallback(p.mouse_down_callback));
    }
    if (p.mouse_up_callback.isProvided())
    {
        setMouseUpCallback(initCommitCallback(p.mouse_up_callback));
    }
}

LLSlider::~LLSlider()
{
    delete mMouseDownSignal;
    delete mMouseUpSignal;
}

void LLSlider::setValue(F32 value, bool from_event)
{
    value = llclamp( value, mMinValue, mMaxValue );

    // Round to nearest increment (bias towards rounding down)
    value -= mMinValue;
    value += mIncrement/2.0001f;
    value -= fmod(value, mIncrement);
    value += mMinValue;

    if (!from_event && getValueF32() != value)
    {
        setControlValue(value);
    }

    LLF32UICtrl::setValue(value);
    updateThumbRect();
}

void LLSlider::updateThumbRect()
{
    const S32 DEFAULT_THUMB_SIZE = 16;
    F32 t = (getValueF32() - mMinValue) / (mMaxValue - mMinValue);

    S32 thumb_width = mThumbImage ? mThumbImage->getWidth() : DEFAULT_THUMB_SIZE;
    S32 thumb_height = mThumbImage ? mThumbImage->getHeight() : DEFAULT_THUMB_SIZE;

    if ( mOrientation == HORIZONTAL )
    {
        S32 left_edge = (thumb_width / 2);
        S32 right_edge = getRect().getWidth() - (thumb_width / 2);

        S32 x = left_edge + S32( t * (right_edge - left_edge) );
        mThumbRect.mLeft = x - (thumb_width / 2);
        mThumbRect.mRight = mThumbRect.mLeft + thumb_width;
        mThumbRect.mBottom = getLocalRect().getCenterY() - (thumb_height / 2);
        mThumbRect.mTop = mThumbRect.mBottom + thumb_height;
    }
    else
    {
        S32 top_edge = (thumb_height / 2);
        S32 bottom_edge = getRect().getHeight() - (thumb_height / 2);

        S32 y = top_edge + S32( t * (bottom_edge - top_edge) );
        mThumbRect.mLeft = getLocalRect().getCenterX() - (thumb_width / 2);
        mThumbRect.mRight = mThumbRect.mLeft + thumb_width;
        mThumbRect.mBottom = y  - (thumb_height / 2);
        mThumbRect.mTop = mThumbRect.mBottom + thumb_height;
    }
}


void LLSlider::setValueAndCommit(F32 value)
{
    F32 old_value = getValueF32();
    setValue(value);

    if (getValueF32() != old_value)
    {
        onCommit();
    }
}


bool LLSlider::handleHover(S32 x, S32 y, MASK mask)
{
    if( hasMouseCapture() )
    {
        if ( mOrientation == HORIZONTAL )
        {
            S32 thumb_half_width = mThumbImage->getWidth()/2;
            S32 left_edge = thumb_half_width;
            S32 right_edge = getRect().getWidth() - (thumb_half_width);

            x += mMouseOffset;
            x = llclamp( x, left_edge, right_edge );

            F32 t = F32(x - left_edge) / (right_edge - left_edge);
            setValueAndCommit(t * (mMaxValue - mMinValue) + mMinValue );
        }
        else // mOrientation == VERTICAL
        {
            S32 thumb_half_height = mThumbImage->getHeight()/2;
            S32 top_edge = thumb_half_height;
            S32 bottom_edge = getRect().getHeight() - (thumb_half_height);

            y += mMouseOffset;
            y = llclamp(y, top_edge, bottom_edge);

            F32 t = F32(y - top_edge) / (bottom_edge - top_edge);
            setValueAndCommit(t * (mMaxValue - mMinValue) + mMinValue );
        }
        getWindow()->setCursor(UI_CURSOR_ARROW);
        LL_DEBUGS("UserInput") << "hover handled by " << getName() << " (active)" << LL_ENDL;
    }
    else
    {
        getWindow()->setCursor(UI_CURSOR_ARROW);
        LL_DEBUGS("UserInput") << "hover handled by " << getName() << " (inactive)" << LL_ENDL;
    }
    return true;
}

bool LLSlider::handleMouseUp(S32 x, S32 y, MASK mask)
{
    bool handled = false;

    if( hasMouseCapture() )
    {
        gFocusMgr.setMouseCapture( NULL );

        if (mMouseUpSignal)
            (*mMouseUpSignal)( this, getValueF32() );

        handled = true;
        make_ui_sound("UISndClickRelease");
    }
    else
    {
        handled = true;
    }

    return handled;
}

bool LLSlider::handleMouseDown(S32 x, S32 y, MASK mask)
{
    // only do sticky-focus on non-chrome widgets
    if (!getIsChrome())
    {
        setFocus(true);
    }
    if (mMouseDownSignal)
        (*mMouseDownSignal)( this, getValueF32() );

    if (MASK_CONTROL & mask) // if CTRL is modifying
    {
        setValueAndCommit(mInitialValue);
    }
    else
    {
        // Find the offset of the actual mouse location from the center of the thumb.
        if (mThumbRect.pointInRect(x,y))
        {
            mMouseOffset = (mOrientation == HORIZONTAL)
                ? (mThumbRect.mLeft + mThumbImage->getWidth()/2) - x
                : (mThumbRect.mBottom + mThumbImage->getHeight()/2) - y;
        }
        else
        {
            mMouseOffset = 0;
        }

        // Start dragging the thumb
        // No handler needed for focus lost since this class has no state that depends on it.
        gFocusMgr.setMouseCapture( this );
        mDragStartThumbRect = mThumbRect;
    }
    make_ui_sound("UISndClick");

    return true;
}

bool LLSlider::handleKeyHere(KEY key, MASK mask)
{
    bool handled = false;
    switch(key)
    {
    case KEY_DOWN:
    case KEY_LEFT:
        setValueAndCommit(getValueF32() - getIncrement());
        handled = true;
        break;
    case KEY_UP:
    case KEY_RIGHT:
        setValueAndCommit(getValueF32() + getIncrement());
        handled = true;
        break;
    default:
        break;
    }
    return handled;
}

bool LLSlider::handleScrollWheel(S32 x, S32 y, S32 clicks)
{
    if ( mOrientation == VERTICAL )
    {
        F32 new_val = getValueF32() - clicks * getIncrement();
        setValueAndCommit(new_val);
        return true;
    }
    return LLF32UICtrl::handleScrollWheel(x,y,clicks);
}

void LLSlider::draw()
{
    F32 alpha = getDrawContext().mAlpha;

    // since thumb image might still be decoding, need thumb to accomodate image size
    updateThumbRect();

    // Draw background and thumb.

    // drawing solids requires texturing be disabled
    gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);

    // Track
    LLPointer<LLUIImage>& trackImage = ( mOrientation == HORIZONTAL )
        ? mTrackImageHorizontal
        : mTrackImageVertical;

    LLPointer<LLUIImage>& trackHighlightImage = ( mOrientation == HORIZONTAL )
        ? mTrackHighlightHorizontalImage
        : mTrackHighlightVerticalImage;

    LLRect track_rect;
    LLRect highlight_rect;

    if ( mOrientation == HORIZONTAL )
    {
        track_rect.set(mThumbImage->getWidth() / 2,
                       getLocalRect().getCenterY() + (trackImage->getHeight() / 2),
                       getRect().getWidth() - mThumbImage->getWidth() / 2,
                       getLocalRect().getCenterY() - (trackImage->getHeight() / 2) );
        highlight_rect.set(track_rect.mLeft, track_rect.mTop, mThumbRect.getCenterX(), track_rect.mBottom);
    }
    else
    {
        track_rect.set(getLocalRect().getCenterX() - (trackImage->getWidth() / 2),
                       getRect().getHeight(),
                       getLocalRect().getCenterX() + (trackImage->getWidth() / 2),
                       0);
        highlight_rect.set(track_rect.mLeft, track_rect.mTop, track_rect.mRight, track_rect.mBottom);
    }

    LLColor4 color = isInEnabledChain() ? LLColor4::white % alpha : LLColor4::white % (0.6f * alpha);
    trackImage->draw(track_rect, color);
    trackHighlightImage->draw(highlight_rect, color);

    // Thumb
    if (hasFocus())
    {
        // Draw focus highlighting.
        mThumbImage->drawBorder(mThumbRect, gFocusMgr.getFocusColor() % alpha, gFocusMgr.getFocusFlashWidth());
    }

    if( hasMouseCapture() ) // currently clicking on slider
    {
        // Show ghost where thumb was before dragging began.
        if (mThumbImage.notNull())
        {
            mThumbImage->draw(mDragStartThumbRect, mThumbCenterColor.get() % (0.3f * alpha));
        }
        if (mThumbImagePressed.notNull())
        {
            mThumbImagePressed->draw(mThumbRect, mThumbOutlineColor % alpha);
        }
    }
    else if (!isInEnabledChain())
    {
        if (mThumbImageDisabled.notNull())
        {
            mThumbImageDisabled->draw(mThumbRect, mThumbCenterColor % alpha);
        }
    }
    else
    {
        if (mThumbImage.notNull())
        {
            mThumbImage->draw(mThumbRect, mThumbCenterColor % alpha);
        }
    }

    LLUICtrl::draw();
}

boost::signals2::connection LLSlider::setMouseDownCallback( const commit_signal_t::slot_type& cb )
{
    if (!mMouseDownSignal) mMouseDownSignal = new commit_signal_t();
    return mMouseDownSignal->connect(cb);
}

boost::signals2::connection LLSlider::setMouseUpCallback(   const commit_signal_t::slot_type& cb )
{
    if (!mMouseUpSignal) mMouseUpSignal = new commit_signal_t();
    return mMouseUpSignal->connect(cb);
}