summaryrefslogtreecommitdiff
path: root/indra/llcommon/llkeybind.cpp
blob: ff88a9c9aadb479551e93949f979f7a4c7362957 (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
/** 
 * @file llkeybind.cpp
 * @brief Information about key combinations.
 *
 * $LicenseInfo:firstyear=2019&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2019, 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 "llkeybind.h"

#include "llsd.h"
#include "llsdutil.h"

LLKeyData::LLKeyData()
    :
    mMouse(CLICK_NONE),
    mKey(KEY_NONE),
    mMask(MASK_NONE),
    mIgnoreMasks(false)
{
}

LLKeyData::LLKeyData(EMouseClickType mouse, KEY key, MASK mask)
    :
    mMouse(mouse),
    mKey(key),
    mMask(mask),
    mIgnoreMasks(false)
{
}

LLKeyData::LLKeyData(EMouseClickType mouse, KEY key, bool ignore_mask)
    :
    mMouse(mouse),
    mKey(key),
    mMask(MASK_NONE),
    mIgnoreMasks(ignore_mask)
{
}

LLKeyData::LLKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignore_mask)
    :
    mMouse(mouse),
    mKey(key),
    mMask(mask),
    mIgnoreMasks(ignore_mask)
{
}

LLKeyData::LLKeyData(const LLSD &key_data)
{
    if (key_data.has("mouse"))
    {
        mMouse = (EMouseClickType)key_data["mouse"].asInteger();
    }
    if (key_data.has("key"))
    {
        mKey = key_data["key"].asInteger();
    }
    if (key_data.has("ignore_accelerators"))
    {
        mIgnoreMasks = key_data["ignore_accelerators"];
    }
    if (key_data.has("mask"))
    {
        mMask = key_data["mask"].asInteger();
    }
}

LLSD LLKeyData::asLLSD() const
{
    LLSD data;
    data["mouse"] = (LLSD::Integer)mMouse;
    data["key"] = (LLSD::Integer)mKey;
    if (mIgnoreMasks)
    {
        data["ignore_accelerators"] = (LLSD::Boolean)mIgnoreMasks;
    }
    else
    {
        data["mask"] = (LLSD::Integer)mMask;
    }
    return data;
}

bool LLKeyData::isEmpty() const
{
    return mMouse == CLICK_NONE && mKey == KEY_NONE;
}

void LLKeyData::reset()
{
    mMouse = CLICK_NONE;
    mKey = KEY_NONE;
    mMask = MASK_NONE;
    mIgnoreMasks = false;
}

LLKeyData& LLKeyData::operator=(const LLKeyData& rhs)
{
    mMouse = rhs.mMouse;
    mKey = rhs.mKey;
    mMask = rhs.mMask;
    mIgnoreMasks = rhs.mIgnoreMasks;
    return *this;
}

bool LLKeyData::operator==(const LLKeyData& rhs)
{
    if (mMouse != rhs.mMouse) return false;
    if (mKey != rhs.mKey) return false;
    if (mMask != rhs.mMask) return false;
    if (mIgnoreMasks != rhs.mIgnoreMasks) return false;
    return true;
}

bool LLKeyData::operator!=(const LLKeyData& rhs)
{
    if (mMouse != rhs.mMouse) return true;
    if (mKey != rhs.mKey) return true;
    if (mMask != rhs.mMask) return true;
    if (mIgnoreMasks != rhs.mIgnoreMasks) return true;
    return false;
}

bool LLKeyData::canHandle(const LLKeyData& data) const
{
    if (data.mKey == mKey
        && data.mMouse == mMouse
        && (mIgnoreMasks || data.mMask == mMask))
    {
        return true;
    }
    return false;
}

bool LLKeyData::canHandle(EMouseClickType mouse, KEY key, MASK mask) const
{
    if (mouse == mMouse
        && key == mKey
        && (mIgnoreMasks || mask == mMask))
    {
        return true;
    }
    return false;
}

// LLKeyBind

LLKeyBind::LLKeyBind(const LLSD &key_bind)
{
    if (key_bind.isArray())
    {
        for (LLSD::array_const_iterator data = key_bind.beginArray(), endLists = key_bind.endArray();
            data != endLists;
            data++
            )
        {
            mData.push_back(LLKeyData(*data));
        }
    }
}

bool LLKeyBind::operator==(const LLKeyBind& rhs)
{
    U32 size = mData.size();
    if (size != rhs.mData.size()) return false;

    for (U32 i = 0; i < size; i++)
    {
        if (mData[i] != rhs.mData[i]) return false;
    }

    return true;
}

bool LLKeyBind::operator!=(const LLKeyBind& rhs)
{
    U32 size = mData.size();
    if (size != rhs.mData.size()) return true;

    for (U32 i = 0; i < size; i++)
    {
        if (mData[i] != rhs.mData[i]) return true;
    }

    return false;
}

bool LLKeyBind::isEmpty() const
{
    for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++)
    {
        if (!iter->isEmpty()) return false;
    }
    return true;
}

LLSD LLKeyBind::asLLSD() const
{
    S32 last = mData.size() - 1;
    while (mData[last].empty())
    {
        last--;
    }

    LLSD data;
    for (S32 i = 0; i <= last; ++i)
    {
        // append even if empty to not affect visual representation
        data.append(mData[i].asLLSD());
    }
    return data;
}

bool LLKeyBind::canHandle(EMouseClickType mouse, KEY key, MASK mask) const
{
    if (mouse == CLICK_NONE && key == KEY_NONE)
    {
        // assume placeholder
        return false;
    }

    for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++)
    {
        if (iter->canHandle(mouse, key, mask))
        {
            return true;
        }
    }
    return false;
}

bool LLKeyBind::canHandleKey(KEY key, MASK mask) const
{
    return canHandle(CLICK_NONE, key, mask);
}

bool LLKeyBind::canHandleMouse(EMouseClickType mouse, MASK mask) const
{
    return canHandle(mouse, KEY_NONE, mask);
}

bool LLKeyBind::hasKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignore) const
{
    if (mouse != CLICK_NONE || key != KEY_NONE)
    {
        for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++)
        {
            if (iter->mKey == key
                && iter->mMask == mask
                && iter->mMouse == mouse
                && iter->mIgnoreMasks == ignore)
            {
                return true;
            }
        }
    }
    return false;
}

bool LLKeyBind::hasKeyData(const LLKeyData& data) const
{
    return hasKeyData(data.mMouse, data.mKey, data.mMask, data.mIgnoreMasks);
}

bool LLKeyBind::hasKeyData(U32 index) const
{
    return mData.size() > index;
}

S32 LLKeyBind::findKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignore) const
{
    if (mouse != CLICK_NONE || key != KEY_NONE)
    {
        for (S32 i = 0; i < mData.size(); ++i)
        {
            if (mData[i].mKey == key
                && mData[i].mMask == mask
                && mData[i].mMouse == mouse
                && mData[i].mIgnoreMasks == ignore)
            {
                return i;
            }
        }
    }
    return -1;
}

S32 LLKeyBind::findKeyData(const LLKeyData& data) const
{
    return findKeyData(data.mMouse, data.mKey, data.mMask, data.mIgnoreMasks);
}

LLKeyData LLKeyBind::getKeyData(U32 index) const
{
    if (mData.size() > index)
    {
        return mData[index];
    }
    return LLKeyData();
}

bool LLKeyBind::addKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignore)
{
    if (!hasKeyData(mouse, key, mask, ignore))
    {
        mData.push_back(LLKeyData(mouse, key, mask, ignore));
        return true;
    }
    return false;
}

bool LLKeyBind::addKeyData(const LLKeyData& data)
{
    if (!hasKeyData(data))
    {
        mData.push_back(data);
        return true;
    }
    return false;
}

void LLKeyBind::replaceKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignore, U32 index)
{
    if (mouse != CLICK_NONE || key != KEY_NONE )
    {
        // if both click and key are none, we are inserting a placeholder, we don't want to reset anything
        // otherwise reset identical key
        for (data_vector_t::iterator iter = mData.begin(); iter != mData.end(); iter++)
        {
            if (iter->mKey == key
                && iter->mMouse == mouse
                && iter->mIgnoreMasks == ignore
                && (iter->mIgnoreMasks || iter->mMask == mask))
            {
                iter->reset();
                break;
            }
        }
    }
    if (mData.size() > index)
    {
        mData[index] = LLKeyData(mouse, key, mask, ignore);
    }
    else
    {
        mData.push_back(LLKeyData(mouse, key, mask, ignore));
    }
}

void LLKeyBind::replaceKeyData(const LLKeyData& data, U32 index)
{
    if (!data.isEmpty())
    {
        for (data_vector_t::iterator iter = mData.begin(); iter != mData.end(); iter++)
        {
            if (iter->mKey == data.mKey
                && iter->mMouse == data.mMouse
                && iter->mIgnoreMasks == data.mIgnoreMasks
                && (iter->mIgnoreMasks || iter->mMask == data.mMask))
            {
                iter->reset();
                break;
            }
        }
    }
    if (mData.size() <= index)
    {
        mData.resize(index + 1);
    }
    mData[index] = data;
}

void LLKeyBind::resetKeyData(S32 index)
{
    if (mData.size() > index)
    {
        mData[index].reset();
    }
}

void LLKeyBind::trimEmpty()
{
    S32 last = mData.size() - 1;
    while (last >= 0 && mData[last].empty())
    {
        mData.erase(mData.begin() + last);
        last--;
    }
}

U32 LLKeyBind::getDataCount()
{
    return mData.size();
}