summaryrefslogtreecommitdiff
path: root/indra/newview/llhints.cpp
blob: d480274691083183fd788b4be67b07a76280ca37 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
 * @file llhints.cpp
 * @brief Hint popups for displaying context sensitive help in a UI overlay
 *
 * $LicenseInfo:firstyear=2000&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 "llviewerprecompiledheaders.h" // must be first include

#include "llhints.h"

#include "llbutton.h"
#include "lltextbox.h"
#include "llviewerwindow.h"
#include "llviewercontrol.h"
#include "lliconctrl.h"
#include "llsdparam.h"

class LLHintPopup : public LLPanel
{
public:

    typedef enum e_popup_direction
    {
        LEFT,
        TOP,
        RIGHT,
        BOTTOM,
        TOP_RIGHT
    } EPopupDirection;

    struct PopupDirections : public LLInitParam::TypeValuesHelper<LLHintPopup::EPopupDirection, PopupDirections>
    {
        static void declareValues()
        {
            declare("left", LLHintPopup::LEFT);
            declare("right", LLHintPopup::RIGHT);
            declare("top", LLHintPopup::TOP);
            declare("bottom", LLHintPopup::BOTTOM);
            declare("top_right", LLHintPopup::TOP_RIGHT);
        }
    };

    struct TargetParams : public LLInitParam::Block<TargetParams>
    {
        Mandatory<std::string>  target;
        Mandatory<EPopupDirection, PopupDirections> direction;

        TargetParams()
        :   target("target"),
            direction("direction")
        {}
    };

    struct Params : public LLInitParam::Block<Params, LLPanel::Params>
    {
        Mandatory<LLNotificationPtr>    notification;
        Optional<TargetParams>          target_params;
        Optional<S32>                   distance;
        Optional<LLUIImage*>            left_arrow,
                                        up_arrow,
                                        right_arrow,
                                        down_arrow,
                                        lower_left_arrow,
                                        hint_image;

        Optional<S32>                   left_arrow_offset,
                                        up_arrow_offset,
                                        right_arrow_offset,
                                        down_arrow_offset;
        Optional<F32>                   fade_in_time,
                                        fade_out_time;

        Params()
        :   distance("distance"),
            left_arrow("left_arrow"),
            up_arrow("up_arrow"),
            right_arrow("right_arrow"),
            down_arrow("down_arrow"),
            lower_left_arrow("lower_left_arrow"),
            hint_image("hint_image"),
            left_arrow_offset("left_arrow_offset"),
            up_arrow_offset("up_arrow_offset"),
            right_arrow_offset("right_arrow_offset"),
            down_arrow_offset("down_arrow_offset"),
            fade_in_time("fade_in_time"),
            fade_out_time("fade_out_time")
        {}
    };

    LLHintPopup(const Params&);

    /*virtual*/ bool postBuild();

    void onClickClose()
    {
        if (!mHidden)
        {
            hide();
            LLNotifications::instance().cancel(mNotification);
        }
    }
    void draw();
    void hide() { if(!mHidden) {mHidden = true; mFadeTimer.reset();} }

private:
    LLNotificationPtr   mNotification;
    std::string         mTarget;
    EPopupDirection     mDirection;
    S32                 mDistance;
    LLUIImagePtr        mArrowLeft,
                        mArrowUp,
                        mArrowRight,
                        mArrowDown,
                        mArrowDownAndLeft;
    S32                 mArrowLeftOffset,
                        mArrowUpOffset,
                        mArrowRightOffset,
                        mArrowDownOffset;
    LLFrameTimer        mFadeTimer;
    F32                 mFadeInTime,
                        mFadeOutTime;
    bool                mHidden;
};

static LLDefaultChildRegistry::Register<LLHintPopup> r("hint_popup");


LLHintPopup::LLHintPopup(const LLHintPopup::Params& p)
:   mNotification(p.notification),
    mDirection(TOP),
    mDistance(p.distance),
    mArrowLeft(p.left_arrow),
    mArrowUp(p.up_arrow),
    mArrowRight(p.right_arrow),
    mArrowDown(p.down_arrow),
    mArrowDownAndLeft(p.lower_left_arrow),
    mArrowLeftOffset(p.left_arrow_offset),
    mArrowUpOffset(p.up_arrow_offset),
    mArrowRightOffset(p.right_arrow_offset),
    mArrowDownOffset(p.down_arrow_offset),
    mHidden(false),
    mFadeInTime(p.fade_in_time),
    mFadeOutTime(p.fade_out_time),
    LLPanel(p)
{
    if (p.target_params.isProvided())
    {
        mDirection = p.target_params.direction;
        mTarget = p.target_params.target;
    }
    if (p.hint_image.isProvided())
    {
        buildFromFile("panel_hint_image.xml", p);
        getChild<LLIconCtrl>("hint_image")->setImage(p.hint_image());
    }
    else
    {
        buildFromFile( "panel_hint.xml", p);
    }
}

bool LLHintPopup::postBuild()
{
    LLTextBox& hint_text = getChildRef<LLTextBox>("hint_text");
    hint_text.setText(mNotification->getMessage());

    getChild<LLButton>("close")->setClickedCallback(boost::bind(&LLHintPopup::onClickClose, this));
    getChild<LLTextBox>("hint_title")->setText(mNotification->getLabel());

    LLRect text_bounds = hint_text.getTextBoundingRect();
    S32 delta_height = text_bounds.getHeight() - hint_text.getRect().getHeight();
    reshape(getRect().getWidth(), getRect().getHeight() + delta_height);
    hint_text.reshape(hint_text.getRect().getWidth(), hint_text.getRect().getHeight() + delta_height);
//  hint_text.translate(0, -delta_height);
    return true;
}

void LLHintPopup::draw()
{
    F32 alpha = 1.f;
    if (mHidden)
    {
        alpha = clamp_rescale(mFadeTimer.getElapsedTimeF32(), 0.f, mFadeOutTime, 1.f, 0.f);
        if (alpha == 0.f)
        {
            die();
            return;
        }
    }
    else
    {
        alpha = clamp_rescale(mFadeTimer.getElapsedTimeF32(), 0.f, mFadeInTime, 0.f, 1.f);
    }

    LLIconCtrl* hint_icon = findChild<LLIconCtrl>("hint_image");

    if (hint_icon)
    {
        LLUIImagePtr hint_image = hint_icon->getImage();
        S32 image_height = hint_image.isNull() ? 0 : hint_image->getHeight();
        S32 image_width = hint_image.isNull() ? 0 : hint_image->getWidth();

        LLView* layout_stack = hint_icon->getParent()->getParent();
        S32 delta_height = image_height - layout_stack->getRect().getHeight();
        hint_icon->getParent()->reshape(image_width, hint_icon->getParent()->getRect().getHeight());
        layout_stack->reshape(layout_stack->getRect().getWidth(), image_height);
        layout_stack->translate(0, -delta_height);

        LLRect hint_rect = getLocalRect();
        reshape(hint_rect.getWidth(), hint_rect.getHeight() + delta_height);
    }

    {   LLViewDrawContext context(alpha);

        if (mTarget.empty())
        {
            // just draw contents, no arrow, in default position
            LLPanel::draw();
        }
        else
        {
            LLView* targetp = LLHints::getInstance()->getHintTarget(mTarget).get();
            if (!targetp)
            {
                // target widget is no longer valid, go away
                die();
            }
            else if (!targetp->isInVisibleChain())
            {
                // if target is invisible, don't draw, but keep alive in case widget comes back
                // but do make it so that it allows mouse events to pass through
                setEnabled(false);
                setMouseOpaque(false);
            }
            else
            {
                // revert back enabled and mouse opaque state in case we disabled it before
                setEnabled(true);
                setMouseOpaque(true);

                LLRect target_rect;
                targetp->localRectToOtherView(targetp->getLocalRect(), &target_rect, getParent());

                LLRect my_local_rect = getLocalRect();
                LLRect my_rect;
                LLRect arrow_rect;
                LLUIImagePtr arrow_imagep;

                switch(mDirection)
                {
                case LEFT:
                    my_rect.setCenterAndSize(   target_rect.mLeft - (my_local_rect.getWidth() / 2 + mDistance),
                                                target_rect.getCenterY(),
                                                my_local_rect.getWidth(),
                                                my_local_rect.getHeight());
                    if (mArrowRight)
                    {
                        arrow_rect.setCenterAndSize(my_local_rect.mRight + mArrowRight->getWidth() / 2 + mArrowRightOffset,
                                                    my_local_rect.getCenterY(),
                                                    mArrowRight->getWidth(),
                                                    mArrowRight->getHeight());
                        arrow_imagep = mArrowRight;
                    }
                    break;
                case TOP:
                    my_rect.setCenterAndSize(   target_rect.getCenterX(),
                                                target_rect.mTop + (my_local_rect.getHeight() / 2 + mDistance),
                                                my_local_rect.getWidth(),
                                                my_local_rect.getHeight());
                    if (mArrowDown)
                    {
                        arrow_rect.setCenterAndSize(my_local_rect.getCenterX(),
                                                    my_local_rect.mBottom - mArrowDown->getHeight() / 2 + mArrowDownOffset,
                                                    mArrowDown->getWidth(),
                                                    mArrowDown->getHeight());
                        arrow_imagep = mArrowDown;
                    }
                    break;
                case RIGHT:
                    my_rect.setCenterAndSize(   target_rect.mRight + (my_local_rect.getWidth() / 2 + mDistance),
                                                target_rect.getCenterY(),
                                                my_local_rect.getWidth(),
                                                my_local_rect.getHeight());
                    if (mArrowLeft)
                    {
                        arrow_rect.setCenterAndSize(my_local_rect.mLeft - mArrowLeft->getWidth() / 2 + mArrowLeftOffset,
                                                    my_local_rect.getCenterY(),
                                                    mArrowLeft->getWidth(),
                                                    mArrowLeft->getHeight());
                        arrow_imagep = mArrowLeft;
                    }
                    break;
                case BOTTOM:
                    my_rect.setCenterAndSize(   target_rect.getCenterX(),
                                                target_rect.mBottom - (my_local_rect.getHeight() / 2 + mDistance),
                                                my_local_rect.getWidth(),
                                                my_local_rect.getHeight());
                    if (mArrowUp)
                    {
                        arrow_rect.setCenterAndSize(my_local_rect.getCenterX(),
                                                    my_local_rect.mTop + mArrowUp->getHeight() / 2 + mArrowUpOffset,
                                                    mArrowUp->getWidth(),
                                                    mArrowUp->getHeight());
                        arrow_imagep = mArrowUp;
                    }
                    break;
                case TOP_RIGHT:
                    my_rect.setCenterAndSize(   target_rect.mRight + (my_local_rect.getWidth() / 2),
                                                target_rect.mTop + (my_local_rect.getHeight() / 2 + mDistance),
                                                my_local_rect.getWidth(),
                                                my_local_rect.getHeight());
                    if (mArrowDownAndLeft)
                    {
                        arrow_rect.setCenterAndSize(my_local_rect.mLeft + mArrowDownAndLeft->getWidth() / 2 + mArrowLeftOffset,
                                                    my_local_rect.mBottom - mArrowDownAndLeft->getHeight() / 2 + mArrowDownOffset,
                                                    mArrowDownAndLeft->getWidth(),
                                                    mArrowDownAndLeft->getHeight());
                        arrow_imagep = mArrowDownAndLeft;
                    }
                }
                setShape(my_rect);
                LLPanel::draw();

                if (arrow_imagep) arrow_imagep->draw(arrow_rect, LLColor4(1.f, 1.f, 1.f, alpha));
            }
        }
    }
}


/// LLHints

LLHints::LLHints()
{
    LLControlVariablePtr control = gSavedSettings.getControl("EnableUIHints");
    mControlConnection = control->getSignal()->connect(boost::bind(&LLHints::showHints, this, _2));
    gViewerWindow->getHintHolder()->setVisible(control->getValue().asBoolean());
}

LLHints::~LLHints()
{
    mControlConnection.disconnect();
}

void LLHints::show(LLNotificationPtr hint)
{
    LLHintPopup::Params p(LLUICtrlFactory::getDefaultParams<LLHintPopup>());

    LLParamSDParser parser;
    parser.readSD(hint->getPayload(), p, true);
    p.notification = hint;

    if (p.validateBlock())
    {
        LLHintPopup* popup = new LLHintPopup(p);

        mHints[hint] = popup;

        LLView* hint_holder = gViewerWindow->getHintHolder();
        if (hint_holder)
        {
            hint_holder->addChild(popup);
            popup->centerWithin(hint_holder->getLocalRect());
        }
    }
}

void LLHints::hide(LLNotificationPtr hint)
{
    hint_map_t::iterator found_it = mHints.find(hint);
    if (found_it != mHints.end())
    {
        found_it->second->hide();
        mHints.erase(found_it);
    }
}

void LLHints::registerHintTarget(const std::string& name, LLHandle<LLView> target)
{
    mTargetRegistry.defaultRegistrar().replace(name, target);
}

LLHandle<LLView> LLHints::getHintTarget(const std::string& name)
{
    LLHandle<LLView>* handlep = mTargetRegistry.getValue(name);
    if (handlep)
    {
        return *handlep;
    }
    else
    {
        return LLHandle<LLView>();
    }
}

void LLHints::showHints(const LLSD& show)
{
    bool visible = show.asBoolean();
    gViewerWindow->getHintHolder()->setVisible(visible);
}