summaryrefslogtreecommitdiff
path: root/indra/newview/llfloateremojipicker.cpp
blob: 98fe1e7ca19f55ddca59822811bf16ea09500b84 (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
/**
 * @file llfloateremojipicker.cpp
 *
 * $LicenseInfo:firstyear=2003&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"

#include "llfloateremojipicker.h"

#include "llcombobox.h"
#include "llemojidictionary.h"
#include "llfloaterreg.h"
#include "lllineeditor.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "lltextbox.h" 
#include "llviewerchat.h" 

std::string LLFloaterEmojiPicker::mSelectedCategory;
std::string LLFloaterEmojiPicker::mSearchPattern;
int LLFloaterEmojiPicker::mSelectedEmojiIndex;

class LLEmojiScrollListItem : public LLScrollListItem
{
public:
	LLEmojiScrollListItem(const llwchar emoji, const LLScrollListItem::Params& params)
		: LLScrollListItem(params)
		, mEmoji(emoji)
	{
	}

	llwchar getEmoji() const { return mEmoji; }

	virtual void draw(const LLRect& rect,
		const LLColor4& fg_color,
		const LLColor4& hover_color, // highlight/hover selection of whole item or cell
		const LLColor4& select_color, // highlight/hover selection of whole item or cell
		const LLColor4& highlight_color, // highlights contents of cells (ex: text)
		S32 column_padding) override
	{
		LLScrollListItem::draw(rect, fg_color, hover_color, select_color, highlight_color, column_padding);

		LLWString wstr(1, mEmoji);
		S32 width = getColumn(0)->getWidth();
		F32 x = rect.mLeft + width / 2;
		F32 y = rect.getCenterY();
		LLFontGL::getFontEmoji()->render(
			wstr,                       // wstr
			0,                          // begin_offset
			x,                          // x
			y,                          // y
			LLColor4::white,            // color
			LLFontGL::HCENTER,          // halign
			LLFontGL::VCENTER,          // valign
			LLFontGL::NORMAL,           // style
			LLFontGL::DROP_SHADOW_SOFT, // shadow
			1,                          // max_chars
			S32_MAX,                    // max_pixels
			nullptr,                    // right_x
			false,                      // use_ellipses
			true);                      // use_color
	}

private:
	llwchar mEmoji;
};

LLFloaterEmojiPicker* LLFloaterEmojiPicker::getInstance()
{
	LLFloaterEmojiPicker* floater = LLFloaterReg::getTypedInstance<LLFloaterEmojiPicker>("emoji_picker");
	if (!floater)
		LL_ERRS() << "Cannot instantiate emoji picker" << LL_ENDL;
	return floater;
}

LLFloaterEmojiPicker* LLFloaterEmojiPicker::showInstance(pick_callback_t pick_callback, close_callback_t close_callback)
{
	LLFloaterEmojiPicker* floater = getInstance();
	floater->show(pick_callback, close_callback);
	return floater;
}

void LLFloaterEmojiPicker::show(pick_callback_t pick_callback, close_callback_t close_callback)
{
	mEmojiPickCallback = pick_callback;
	mFloaterCloseCallback = close_callback;
	openFloater(mKey);
	setFocus(TRUE);
}

LLFloaterEmojiPicker::LLFloaterEmojiPicker(const LLSD& key)
: LLFloater(key)
{
}

BOOL LLFloaterEmojiPicker::postBuild()
{
	// Should be initialized first
	mPreviewEmoji = getChild<LLButton>("PreviewEmoji");
	mPreviewEmoji->setClickedCallback([this](LLUICtrl*, const LLSD&) { onPreviewEmojiClick(); });

	mCategory = getChild<LLComboBox>("Category");
	mCategory->setCommitCallback([this](LLUICtrl*, const LLSD&) { onCategoryCommit(); });
	const LLEmojiDictionary::cat2descrs_map_t& cat2Descrs = LLEmojiDictionary::instance().getCategory2Descrs();
	mCategory->clearRows();
	for (const LLEmojiDictionary::cat2descrs_item_t item : cat2Descrs)
	{
		std::string value = item.first;
		std::string name = value;
		LLStringUtil::capitalize(name);
		mCategory->add(name, value);
	}
	mCategory->setSelectedByValue(mSelectedCategory, true);

	mSearch = getChild<LLLineEditor>("Search");
	mSearch->setKeystrokeCallback([this](LLLineEditor*, void*) { onSearchKeystroke(); }, NULL);
	mSearch->setFont(LLViewerChat::getChatFont());
	mSearch->setText(mSearchPattern);

	mEmojis = getChild<LLScrollListCtrl>("Emojis");
	mEmojis->setCommitCallback([this](LLUICtrl*, const LLSD&) { onEmojiSelect(); });
	mEmojis->setDoubleClickCallback([this]() { onEmojiPick(); });
	fillEmojis();

	return TRUE;
}

LLFloaterEmojiPicker::~LLFloaterEmojiPicker()
{
	gFocusMgr.releaseFocusIfNeeded( this );
}

void LLFloaterEmojiPicker::fillEmojis()
{
	mEmojis->clearRows();

	const LLEmojiDictionary::emoji2descr_map_t& emoji2Descr = LLEmojiDictionary::instance().getEmoji2Descr();
	for (const LLEmojiDictionary::emoji2descr_item_t it : emoji2Descr)
	{
		const LLEmojiDescriptor* descr = it.second;

		if (!mSelectedCategory.empty() && !matchesCategory(descr))
			continue;

		if (!mSearchPattern.empty() && !matchesPattern(descr))
			continue;

		LLScrollListItem::Params params;
		// The following line adds default monochrome view of the emoji (is shown as an example)
		//params.columns.add().column("look").value(wstring_to_utf8str(LLWString(1, it.first)));
		params.columns.add().column("name").value(descr->Name);
		mEmojis->addRow(new LLEmojiScrollListItem(it.first, params), params);
	}

	if (mEmojis->getItemCount())
	{
		if (mSelectedEmojiIndex > 0 && mSelectedEmojiIndex < mEmojis->getItemCount())
			mEmojis->selectNthItem(mSelectedEmojiIndex);
		else
			mEmojis->selectFirstItem();

		mEmojis->scrollToShowSelected();
	}
	else
	{
		onEmojiEmpty();
	}
}

bool LLFloaterEmojiPicker::matchesCategory(const LLEmojiDescriptor* descr)
{
	return std::find(descr->Categories.begin(), descr->Categories.end(), mSelectedCategory) != descr->Categories.end();
}

bool LLFloaterEmojiPicker::matchesPattern(const LLEmojiDescriptor* descr)
{
	if (descr->Name.find(mSearchPattern) != std::string::npos)
		return true;
	for (const std::string& shortCode : descr->ShortCodes)
		if (shortCode.find(mSearchPattern) != std::string::npos)
			return true;
	for (const std::string& category : descr->Categories)
		if (category.find(mSearchPattern) != std::string::npos)
			return true;
	return false;
}

void LLFloaterEmojiPicker::onCategoryCommit()
{
	mSelectedCategory = mCategory->getSelectedValue().asString();
	mSelectedEmojiIndex = 0;
	fillEmojis();
}

void LLFloaterEmojiPicker::onSearchKeystroke()
{
	mSearchPattern = mSearch->getText();
	mSelectedEmojiIndex = 0;
	fillEmojis();
}

void LLFloaterEmojiPicker::onPreviewEmojiClick()
{
	if (mEmojiPickCallback)
	{
		if (LLEmojiScrollListItem* item = dynamic_cast<LLEmojiScrollListItem*>(mEmojis->getFirstSelected()))
		{
			mEmojiPickCallback(item->getEmoji());
		}
	}
}

void LLFloaterEmojiPicker::onEmojiSelect()
{
	const LLEmojiScrollListItem* item = dynamic_cast<LLEmojiScrollListItem*>(mEmojis->getFirstSelected());
	if (item)
	{
		mSelectedEmojiIndex = mEmojis->getFirstSelectedIndex();
		LLUIString text;
		text.insert(0, LLWString(1, item->getEmoji()));
		mPreviewEmoji->setLabel(text);
		return;
	}

	onEmojiEmpty();
}

void LLFloaterEmojiPicker::onEmojiEmpty()
{
	mSelectedEmojiIndex = 0;
	mPreviewEmoji->setLabel(LLUIString());
}

void LLFloaterEmojiPicker::onEmojiPick()
{
	if (mEmojiPickCallback)
	{
		if (LLEmojiScrollListItem* item = dynamic_cast<LLEmojiScrollListItem*>(mEmojis->getFirstSelected()))
		{
			mEmojiPickCallback(item->getEmoji());
			closeFloater();
		}
	}
}

// virtual
BOOL LLFloaterEmojiPicker::handleKeyHere(KEY key, MASK mask)
{
	if (mask == MASK_NONE)
	{
		switch (key)
		{
		case KEY_RETURN:
			if (mCategory->hasFocus())
				break;
			onEmojiPick();
			return TRUE;
		case KEY_ESCAPE:
			closeFloater();
			return TRUE;
		case KEY_UP:
			mEmojis->selectPrevItem();
			mEmojis->scrollToShowSelected();
			return TRUE;
		case KEY_DOWN:
			mEmojis->selectNextItem();
			mEmojis->scrollToShowSelected();
			return TRUE;
		}
	}

	return LLFloater::handleKeyHere(key, mask);
}

// virtual
void LLFloaterEmojiPicker::closeFloater(bool app_quitting)
{
	LLFloater::closeFloater(app_quitting);
	if (mFloaterCloseCallback)
		mFloaterCloseCallback();
}