llavatarlistitem.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:16k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file llavatarlistitem.cpp
  3.  * @avatar list item source file
  4.  *
  5.  * $LicenseInfo:firstyear=2009&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2009-2010, Linden Research, Inc.
  8.  * 
  9.  * Second Life Viewer Source Code
  10.  * The source code in this file ("Source Code") is provided by Linden Lab
  11.  * to you under the terms of the GNU General Public License, version 2.0
  12.  * ("GPL"), unless you have obtained a separate licensing agreement
  13.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  14.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16.  * 
  17.  * There are special exceptions to the terms and conditions of the GPL as
  18.  * it is applied to this Source Code. View the full text of the exception
  19.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  20.  * online at
  21.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22.  * 
  23.  * By copying, modifying or distributing this software, you acknowledge
  24.  * that you have read and understood your obligations described above,
  25.  * and agree to abide by those obligations.
  26.  * 
  27.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29.  * COMPLETENESS OR PERFORMANCE.
  30.  * $/LicenseInfo$
  31.  */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llavataractions.h"
  34. #include "llavatarlistitem.h"
  35. #include "llfloaterreg.h"
  36. #include "llagent.h"
  37. #include "lloutputmonitorctrl.h"
  38. #include "llavatariconctrl.h"
  39. #include "lltextutil.h"
  40. #include "llbutton.h"
  41. bool LLAvatarListItem::sStaticInitialized = false;
  42. S32 LLAvatarListItem::sLeftPadding = 0;
  43. S32 LLAvatarListItem::sRightNamePadding = 0;
  44. S32 LLAvatarListItem::sChildrenWidths[LLAvatarListItem::ALIC_COUNT];
  45. static LLWidgetNameRegistry::StaticRegistrar sRegisterAvatarListItemParams(&typeid(LLAvatarListItem::Params), "avatar_list_item");
  46. LLAvatarListItem::Params::Params()
  47. : default_style("default_style"),
  48. voice_call_invited_style("voice_call_invited_style"),
  49. voice_call_joined_style("voice_call_joined_style"),
  50. voice_call_left_style("voice_call_left_style"),
  51. online_style("online_style"),
  52. offline_style("offline_style")
  53. {};
  54. LLAvatarListItem::LLAvatarListItem(bool not_from_ui_factory/* = true*/)
  55. : LLPanel(),
  56. mAvatarIcon(NULL),
  57. mAvatarName(NULL),
  58. mLastInteractionTime(NULL),
  59. mSpeakingIndicator(NULL),
  60. mInfoBtn(NULL),
  61. mProfileBtn(NULL),
  62. mOnlineStatus(E_UNKNOWN),
  63. mShowInfoBtn(true),
  64. mShowProfileBtn(true)
  65. {
  66. if (not_from_ui_factory)
  67. {
  68. LLUICtrlFactory::getInstance()->buildPanel(this, "panel_avatar_list_item.xml");
  69. }
  70. // *NOTE: mantipov: do not use any member here. They can be uninitialized here in case instance
  71. // is created from the UICtrlFactory
  72. }
  73. LLAvatarListItem::~LLAvatarListItem()
  74. {
  75. if (mAvatarId.notNull())
  76. LLAvatarTracker::instance().removeParticularFriendObserver(mAvatarId, this);
  77. }
  78. BOOL  LLAvatarListItem::postBuild()
  79. {
  80. mAvatarIcon = getChild<LLAvatarIconCtrl>("avatar_icon");
  81. mAvatarName = getChild<LLTextBox>("avatar_name");
  82. mLastInteractionTime = getChild<LLTextBox>("last_interaction");
  83. mSpeakingIndicator = getChild<LLOutputMonitorCtrl>("speaking_indicator");
  84. mInfoBtn = getChild<LLButton>("info_btn");
  85. mProfileBtn = getChild<LLButton>("profile_btn");
  86. mInfoBtn->setVisible(false);
  87. mInfoBtn->setClickedCallback(boost::bind(&LLAvatarListItem::onInfoBtnClick, this));
  88. mProfileBtn->setVisible(false);
  89. mProfileBtn->setClickedCallback(boost::bind(&LLAvatarListItem::onProfileBtnClick, this));
  90. if (!sStaticInitialized)
  91. {
  92. // Remember children widths including their padding from the next sibling,
  93. // so that we can hide and show them again later.
  94. initChildrenWidths(this);
  95. sStaticInitialized = true;
  96. }
  97. return TRUE;
  98. }
  99. S32 LLAvatarListItem::notifyParent(const LLSD& info)
  100. {
  101. if (info.has("visibility_changed"))
  102. {
  103. updateChildren();
  104. }
  105. return 0;
  106. }
  107. void LLAvatarListItem::onMouseEnter(S32 x, S32 y, MASK mask)
  108. {
  109. childSetVisible("hovered_icon", true);
  110. mInfoBtn->setVisible(mShowInfoBtn);
  111. mProfileBtn->setVisible(mShowProfileBtn);
  112. LLPanel::onMouseEnter(x, y, mask);
  113. updateChildren();
  114. }
  115. void LLAvatarListItem::onMouseLeave(S32 x, S32 y, MASK mask)
  116. {
  117. childSetVisible("hovered_icon", false);
  118. mInfoBtn->setVisible(false);
  119. mProfileBtn->setVisible(false);
  120. LLPanel::onMouseLeave(x, y, mask);
  121. updateChildren();
  122. }
  123. // virtual, called by LLAvatarTracker
  124. void LLAvatarListItem::changed(U32 mask)
  125. {
  126. // no need to check mAvatarId for null in this case
  127. setOnline(LLAvatarTracker::instance().isBuddyOnline(mAvatarId));
  128. }
  129. void LLAvatarListItem::setOnline(bool online)
  130. {
  131. // *FIX: setName() overrides font style set by setOnline(). Not an issue ATM.
  132. if (mOnlineStatus != E_UNKNOWN && (bool) mOnlineStatus == online)
  133. return;
  134. mOnlineStatus = (EOnlineStatus) online;
  135. // Change avatar name font style depending on the new online status.
  136. setState(online ? IS_ONLINE : IS_OFFLINE);
  137. }
  138. void LLAvatarListItem::setName(const std::string& name)
  139. {
  140. setNameInternal(name, mHighlihtSubstring);
  141. }
  142. void LLAvatarListItem::setHighlight(const std::string& highlight)
  143. {
  144. setNameInternal(mAvatarName->getText(), mHighlihtSubstring = highlight);
  145. }
  146. void LLAvatarListItem::setState(EItemState item_style)
  147. {
  148. const LLAvatarListItem::Params& params = LLUICtrlFactory::getDefaultParams<LLAvatarListItem>();
  149. switch(item_style)
  150. {
  151. default:
  152. case IS_DEFAULT:
  153. mAvatarNameStyle = params.default_style();
  154. break;
  155. case IS_VOICE_INVITED:
  156. mAvatarNameStyle = params.voice_call_invited_style();
  157. break;
  158. case IS_VOICE_JOINED:
  159. mAvatarNameStyle = params.voice_call_joined_style();
  160. break;
  161. case IS_VOICE_LEFT:
  162. mAvatarNameStyle = params.voice_call_left_style();
  163. break;
  164. case IS_ONLINE:
  165. mAvatarNameStyle = params.online_style();
  166. break;
  167. case IS_OFFLINE:
  168. mAvatarNameStyle = params.offline_style();
  169. break;
  170. }
  171. // *NOTE: You cannot set the style on a text box anymore, you must
  172. // rebuild the text.  This will cause problems if the text contains
  173. // hyperlinks, as their styles will be wrong.
  174. setNameInternal(mAvatarName->getText(), mHighlihtSubstring);
  175. icon_color_map_t& item_icon_color_map = getItemIconColorMap();
  176. mAvatarIcon->setColor(item_icon_color_map[item_style]);
  177. }
  178. void LLAvatarListItem::setAvatarId(const LLUUID& id, bool ignore_status_changes)
  179. {
  180. if (mAvatarId.notNull())
  181. LLAvatarTracker::instance().removeParticularFriendObserver(mAvatarId, this);
  182. mAvatarId = id;
  183. mAvatarIcon->setValue(id);
  184. mSpeakingIndicator->setSpeakerId(id);
  185. // We'll be notified on avatar online status changes
  186. if (!ignore_status_changes && mAvatarId.notNull())
  187. LLAvatarTracker::instance().addParticularFriendObserver(mAvatarId, this);
  188. // Set avatar name.
  189. gCacheName->get(id, FALSE, boost::bind(&LLAvatarListItem::onNameCache, this, _2, _3));
  190. }
  191. void LLAvatarListItem::showLastInteractionTime(bool show)
  192. {
  193. if (show)
  194. return;
  195. mLastInteractionTime->setVisible(false);
  196. updateChildren();
  197. }
  198. void LLAvatarListItem::setLastInteractionTime(U32 secs_since)
  199. {
  200. mLastInteractionTime->setValue(formatSeconds(secs_since));
  201. }
  202. void LLAvatarListItem::setShowInfoBtn(bool show)
  203. {
  204. // Already done? Then do nothing.
  205. if(mShowInfoBtn == show)
  206. return;
  207. mShowInfoBtn = show;
  208. }
  209. void LLAvatarListItem::setShowProfileBtn(bool show)
  210. {
  211. // Already done? Then do nothing.
  212. if(mShowProfileBtn == show)
  213. return;
  214. mShowProfileBtn = show;
  215. }
  216. void LLAvatarListItem::showSpeakingIndicator(bool visible)
  217. {
  218. // Already done? Then do nothing.
  219. if (mSpeakingIndicator->getVisible() == (BOOL)visible)
  220. return;
  221. // Disabled to not contradict with SpeakingIndicatorManager functionality. EXT-3976
  222. // probably this method should be totally removed.
  223. // mSpeakingIndicator->setVisible(visible);
  224. // updateChildren();
  225. }
  226. void LLAvatarListItem::setAvatarIconVisible(bool visible)
  227. {
  228. // Already done? Then do nothing.
  229. if (mAvatarIcon->getVisible() == (BOOL)visible)
  230. return;
  231. // Show/hide avatar icon.
  232. mAvatarIcon->setVisible(visible);
  233. updateChildren();
  234. }
  235. void LLAvatarListItem::onInfoBtnClick()
  236. {
  237. LLFloaterReg::showInstance("inspect_avatar", LLSD().with("avatar_id", mAvatarId));
  238. }
  239. void LLAvatarListItem::onProfileBtnClick()
  240. {
  241. LLAvatarActions::showProfile(mAvatarId);
  242. }
  243. BOOL LLAvatarListItem::handleDoubleClick(S32 x, S32 y, MASK mask)
  244. {
  245. if(mInfoBtn->getRect().pointInRect(x, y))
  246. {
  247. onInfoBtnClick();
  248. return TRUE;
  249. }
  250. if(mProfileBtn->getRect().pointInRect(x, y))
  251. {
  252. onProfileBtnClick();
  253. return TRUE;
  254. }
  255. return LLPanel::handleDoubleClick(x, y, mask);
  256. }
  257. void LLAvatarListItem::setValue( const LLSD& value )
  258. {
  259. if (!value.isMap()) return;;
  260. if (!value.has("selected")) return;
  261. childSetVisible("selected_icon", value["selected"]);
  262. }
  263. const LLUUID& LLAvatarListItem::getAvatarId() const
  264. {
  265. return mAvatarId;
  266. }
  267. const std::string LLAvatarListItem::getAvatarName() const
  268. {
  269. return mAvatarName->getValue();
  270. }
  271. //== PRIVATE SECITON ==========================================================
  272. void LLAvatarListItem::setNameInternal(const std::string& name, const std::string& highlight)
  273. {
  274. LLTextUtil::textboxSetHighlightedVal(mAvatarName, mAvatarNameStyle, name, highlight);
  275. mAvatarName->setToolTip(name);
  276. }
  277. void LLAvatarListItem::onNameCache(const std::string& first_name, const std::string& last_name)
  278. {
  279. std::string name = first_name + " " + last_name;
  280. setName(name);
  281. }
  282. // Convert given number of seconds to a string like "23 minutes", "15 hours" or "3 years",
  283. // taking i18n into account. The format string to use is taken from the panel XML.
  284. std::string LLAvatarListItem::formatSeconds(U32 secs)
  285. {
  286. static const U32 LL_ALI_MIN = 60;
  287. static const U32 LL_ALI_HOUR = LL_ALI_MIN * 60;
  288. static const U32 LL_ALI_DAY = LL_ALI_HOUR * 24;
  289. static const U32 LL_ALI_WEEK = LL_ALI_DAY * 7;
  290. static const U32 LL_ALI_MONTH = LL_ALI_DAY * 30;
  291. static const U32 LL_ALI_YEAR = LL_ALI_DAY * 365;
  292. std::string fmt; 
  293. U32 count = 0;
  294. if (secs >= LL_ALI_YEAR)
  295. {
  296. fmt = "FormatYears"; count = secs / LL_ALI_YEAR;
  297. }
  298. else if (secs >= LL_ALI_MONTH)
  299. {
  300. fmt = "FormatMonths"; count = secs / LL_ALI_MONTH;
  301. }
  302. else if (secs >= LL_ALI_WEEK)
  303. {
  304. fmt = "FormatWeeks"; count = secs / LL_ALI_WEEK;
  305. }
  306. else if (secs >= LL_ALI_DAY)
  307. {
  308. fmt = "FormatDays"; count = secs / LL_ALI_DAY;
  309. }
  310. else if (secs >= LL_ALI_HOUR)
  311. {
  312. fmt = "FormatHours"; count = secs / LL_ALI_HOUR;
  313. }
  314. else if (secs >= LL_ALI_MIN)
  315. {
  316. fmt = "FormatMinutes"; count = secs / LL_ALI_MIN;
  317. }
  318. else
  319. {
  320. fmt = "FormatSeconds"; count = secs;
  321. }
  322. LLStringUtil::format_map_t args;
  323. args["[COUNT]"] = llformat("%u", count);
  324. return getString(fmt, args);
  325. }
  326. // static
  327. LLAvatarListItem::icon_color_map_t& LLAvatarListItem::getItemIconColorMap()
  328. {
  329. static icon_color_map_t item_icon_color_map;
  330. if (!item_icon_color_map.empty()) return item_icon_color_map;
  331. item_icon_color_map.insert(
  332. std::make_pair(IS_DEFAULT,
  333. LLUIColorTable::instance().getColor("AvatarListItemIconDefaultColor", LLColor4::white)));
  334. item_icon_color_map.insert(
  335. std::make_pair(IS_VOICE_INVITED,
  336. LLUIColorTable::instance().getColor("AvatarListItemIconVoiceInvitedColor", LLColor4::white)));
  337. item_icon_color_map.insert(
  338. std::make_pair(IS_VOICE_JOINED,
  339. LLUIColorTable::instance().getColor("AvatarListItemIconVoiceJoinedColor", LLColor4::white)));
  340. item_icon_color_map.insert(
  341. std::make_pair(IS_VOICE_LEFT,
  342. LLUIColorTable::instance().getColor("AvatarListItemIconVoiceLeftColor", LLColor4::white)));
  343. item_icon_color_map.insert(
  344. std::make_pair(IS_ONLINE,
  345. LLUIColorTable::instance().getColor("AvatarListItemIconOnlineColor", LLColor4::white)));
  346. item_icon_color_map.insert(
  347. std::make_pair(IS_OFFLINE,
  348. LLUIColorTable::instance().getColor("AvatarListItemIconOfflineColor", LLColor4::white)));
  349. return item_icon_color_map;
  350. }
  351. // static
  352. void LLAvatarListItem::initChildrenWidths(LLAvatarListItem* avatar_item)
  353. {
  354. //speaking indicator width + padding
  355. S32 speaking_indicator_width = avatar_item->getRect().getWidth() - avatar_item->mSpeakingIndicator->getRect().mLeft;
  356. //profile btn width + padding
  357. S32 profile_btn_width = avatar_item->mSpeakingIndicator->getRect().mLeft - avatar_item->mProfileBtn->getRect().mLeft;
  358. //info btn width + padding
  359. S32 info_btn_width = avatar_item->mProfileBtn->getRect().mLeft - avatar_item->mInfoBtn->getRect().mLeft;
  360. // last interaction time textbox width + padding
  361. S32 last_interaction_time_width = avatar_item->mInfoBtn->getRect().mLeft - avatar_item->mLastInteractionTime->getRect().mLeft;
  362. // icon width + padding
  363. S32 icon_width = avatar_item->mAvatarName->getRect().mLeft - avatar_item->mAvatarIcon->getRect().mLeft;
  364. sLeftPadding = avatar_item->mAvatarIcon->getRect().mLeft;
  365. sRightNamePadding = avatar_item->mLastInteractionTime->getRect().mLeft - avatar_item->mAvatarName->getRect().mRight;
  366. S32 index = ALIC_COUNT;
  367. sChildrenWidths[--index] = icon_width;
  368. sChildrenWidths[--index] = 0; // for avatar name we don't need its width, it will be calculated as "left available space"
  369. sChildrenWidths[--index] = last_interaction_time_width;
  370. sChildrenWidths[--index] = info_btn_width;
  371. sChildrenWidths[--index] = profile_btn_width;
  372. sChildrenWidths[--index] = speaking_indicator_width;
  373. }
  374. void LLAvatarListItem::updateChildren()
  375. {
  376. LL_DEBUGS("AvatarItemReshape") << LL_ENDL;
  377. LL_DEBUGS("AvatarItemReshape") << "Updating for: " << getAvatarName() << LL_ENDL;
  378. S32 name_new_width = getRect().getWidth();
  379. S32 ctrl_new_left = name_new_width;
  380. S32 name_new_left = sLeftPadding;
  381. // iterate through all children and set them into correct position depend on each child visibility
  382. // assume that child indexes are in back order: the first in Enum is the last (right) in the item
  383. // iterate & set child views starting from right to left
  384. for (S32 i = 0; i < ALIC_COUNT; ++i)
  385. {
  386. // skip "name" textbox, it will be processed out of loop later
  387. if (ALIC_NAME == i) continue;
  388. LLView* control = getItemChildView((EAvatarListItemChildIndex)i);
  389. LL_DEBUGS("AvatarItemReshape") << "Processing control: " << control->getName() << LL_ENDL;
  390. // skip invisible views
  391. if (!control->getVisible()) continue;
  392. S32 ctrl_width = sChildrenWidths[i]; // including space between current & left controls
  393. // decrease available for 
  394. name_new_width -= ctrl_width;
  395. LL_DEBUGS("AvatarItemReshape") << "width: " << ctrl_width << ", name_new_width: " << name_new_width << LL_ENDL;
  396. LLRect control_rect = control->getRect();
  397. LL_DEBUGS("AvatarItemReshape") << "rect before: " << control_rect << LL_ENDL;
  398. if (ALIC_ICON == i)
  399. {
  400. // assume that this is the last iteration,
  401. // so it is not necessary to save "ctrl_new_left" value calculated on previous iterations
  402. ctrl_new_left = sLeftPadding;
  403. name_new_left = ctrl_new_left + ctrl_width;
  404. }
  405. else
  406. {
  407. ctrl_new_left -= ctrl_width;
  408. }
  409. LL_DEBUGS("AvatarItemReshape") << "ctrl_new_left: " << ctrl_new_left << LL_ENDL;
  410. control_rect.setLeftTopAndSize(
  411. ctrl_new_left,
  412. control_rect.mTop,
  413. control_rect.getWidth(),
  414. control_rect.getHeight());
  415. LL_DEBUGS("AvatarItemReshape") << "rect after: " << control_rect << LL_ENDL;
  416. control->setShape(control_rect);
  417. }
  418. // set size and position of the "name" child
  419. LLView* name_view = getItemChildView(ALIC_NAME);
  420. LLRect name_view_rect = name_view->getRect();
  421. LL_DEBUGS("AvatarItemReshape") << "name rect before: " << name_view_rect << LL_ENDL;
  422. // apply paddings
  423. name_new_width -= sLeftPadding;
  424. name_new_width -= sRightNamePadding;
  425. name_view_rect.setLeftTopAndSize(
  426. name_new_left,
  427. name_view_rect.mTop,
  428. name_new_width,
  429. name_view_rect.getHeight());
  430. name_view->setShape(name_view_rect);
  431. LL_DEBUGS("AvatarItemReshape") << "name rect after: " << name_view_rect << LL_ENDL;
  432. }
  433. LLView* LLAvatarListItem::getItemChildView(EAvatarListItemChildIndex child_view_index)
  434. {
  435. LLView* child_view = mAvatarName;
  436. switch (child_view_index)
  437. {
  438. case ALIC_ICON:
  439. child_view = mAvatarIcon;
  440. break;
  441. case ALIC_NAME:
  442. child_view = mAvatarName;
  443. break;
  444. case ALIC_INTERACTION_TIME:
  445. child_view = mLastInteractionTime;
  446. break;
  447. case ALIC_SPEAKER_INDICATOR:
  448. child_view = mSpeakingIndicator; 
  449. break;
  450. case ALIC_INFO_BUTTON:
  451. child_view = mInfoBtn;
  452. break;
  453. case ALIC_PROFILE_BUTTON:
  454. child_view = mProfileBtn;
  455. break;
  456. default:
  457. LL_WARNS("AvatarItemReshape") << "Unexpected child view index is passed: " << child_view_index << LL_ENDL;
  458. // leave child_view untouched
  459. }
  460. return child_view;
  461. }
  462. // EOF