lltoolview.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file lltoolview.cpp
  3. * @brief A UI contains for tool palette tools
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, 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 "lltoolview.h"
  34. #include "llbutton.h"
  35. #include "llstl.h" // For DeletePointer()
  36. #include "lltextbox.h"
  37. #include "lltool.h"
  38. #include "lltoolmgr.h"
  39. LLToolContainer::LLToolContainer(LLToolView* parent)
  40. : mParent(parent),
  41. mButton(NULL),
  42. mPanel(NULL),
  43. mTool(NULL)
  44. {
  45. }
  46. LLToolContainer::~LLToolContainer()
  47. {
  48. // mParent is a pointer to the tool view
  49. // mButton is owned by the tool view
  50. // mPanel is owned by the tool view
  51. delete mTool;
  52. mTool = NULL;
  53. }
  54. LLToolView::LLToolView(const std::string& name, const LLRect& rect)
  55. : LLView(name, rect, true),
  56. mButtonCount(0)
  57. {
  58. }
  59. LLToolView::~LLToolView()
  60. {
  61. std::for_each(mContainList.begin(), mContainList.end(), DeletePointer());
  62. mContainList.clear();
  63. }
  64. #if 0
  65. void LLToolView::addTool(const std::string& icon_off,
  66. const std::string& icon_on, LLPanel* panel,
  67. LLTool* tool, LLView*, const char* label)
  68. {
  69. llassert(tool);
  70. LLToolContainer* contain = new LLToolContainer(this);
  71. LLRect btn_rect = getButtonRect(mButtonCount);
  72. contain->mButton = new LLButton("ToolBtn", btn_rect, icon_off, icon_on,
  73. "", &LLToolView::onClickToolButton,
  74. contain, LLFontGL::getFontSansSerif ());
  75. contain->mPanel = panel;
  76. contain->mTool = tool;
  77. addChild(contain->mButton);
  78. ++mButtonCount;
  79. constexpr S32 LABEL_TOP_SPACING = 0;
  80. static const LLFontGL* font = LLFontGL::getFontSansSerifSmall();
  81. S32 label_width = font->getWidth(label);
  82. LLRect label_rect;
  83. label_rect.setLeftTopAndSize(btn_rect.mLeft + btn_rect.getWidth() / 2 -
  84. label_width / 2,
  85. btn_rect.mBottom - LABEL_TOP_SPACING,
  86. label_width, llfloor(font->getLineHeight()));
  87. addChild(new LLTextBox("tool label", label_rect, label, font));
  88. // Can optionally ignore panel
  89. if (contain->mPanel)
  90. {
  91. contain->mPanel->setBackgroundVisible(false);
  92. contain->mPanel->setBorderVisible(false);
  93. addChild(contain->mPanel);
  94. }
  95. mContainList.push_back(contain);
  96. }
  97. #endif
  98. LLRect LLToolView::getButtonRect(S32 button_index)
  99. {
  100. constexpr S32 HPAD = 7;
  101. constexpr S32 VPAD = 7;
  102. constexpr S32 TOOL_SIZE = 32;
  103. constexpr S32 HORIZ_SPACING = TOOL_SIZE + 5;
  104. constexpr S32 VERT_SPACING = TOOL_SIZE + 14;
  105. S32 tools_per_row = getRect().getWidth() / HORIZ_SPACING;
  106. S32 row = button_index / tools_per_row;
  107. S32 column = button_index % tools_per_row;
  108. // Build the rectangle, recalling the origin is at lower left and we want
  109. // the icons to build down from the top.
  110. LLRect rect;
  111. rect.setLeftTopAndSize(HPAD + column * HORIZ_SPACING,
  112. -VPAD + getRect().getHeight() - row * VERT_SPACING,
  113. TOOL_SIZE, TOOL_SIZE);
  114. return rect;
  115. }
  116. void LLToolView::draw()
  117. {
  118. // Turn off highlighting for all containers and hide all option panels
  119. // except for the selected one.
  120. LLTool* selected = gToolMgr.getCurrentToolset()->getSelectedTool();
  121. for (contain_list_t::iterator iter = mContainList.begin(),
  122. end = mContainList.end();
  123. iter != end; ++iter)
  124. {
  125. LLToolContainer* contain = *iter;
  126. bool state = contain->mTool == selected;
  127. contain->mButton->setToggleState(state);
  128. if (contain->mPanel)
  129. {
  130. contain->mPanel->setVisible(state);
  131. }
  132. }
  133. // Draw children normally
  134. LLView::draw();
  135. }
  136. LLToolContainer* LLToolView::findToolContainer(LLTool* tool)
  137. {
  138. // Find the container for this tool
  139. llassert(tool);
  140. for (contain_list_t::iterator iter = mContainList.begin();
  141. iter != mContainList.end(); ++iter)
  142. {
  143. LLToolContainer* contain = *iter;
  144. if (contain->mTool == tool)
  145. {
  146. return contain;
  147. }
  148. }
  149. llerrs << "Tool not found !" << llendl;
  150. return NULL;
  151. }
  152. //static
  153. void LLToolView::onClickToolButton(void* userdata)
  154. {
  155. LLToolContainer* clicked = (LLToolContainer*)userdata;
  156. if (!clicked) return;
  157. // Switch to this one
  158. gToolMgr.getCurrentToolset()->selectTool(clicked->mTool);
  159. }