llviewborder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @file llviewborder.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewergpl$
  5. *
  6. * Copyright (c) 2001-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include "llviewborder.h"
  33. #include "llrender.h"
  34. static const std::string LL_VIEW_BORDER_TAG = "view_border";
  35. static LLRegisterWidget<LLViewBorder> r31(LL_VIEW_BORDER_TAG);
  36. LLViewBorder::LLViewBorder(const std::string& name, const LLRect& rect,
  37. EBevel bevel, EStyle style, S32 width)
  38. : LLView(name, rect, false),
  39. mBevel(bevel),
  40. mStyle(style),
  41. mHighlightLight(LLUI::sDefaultHighlightLight),
  42. mHighlightDark(LLUI::sDefaultHighlightDark),
  43. mShadowLight(LLUI::sDefaultShadowLight),
  44. mShadowDark(LLUI::sDefaultShadowDark),
  45. mBorderWidth(width),
  46. mTexture(NULL),
  47. mHasKeyboardFocus(false)
  48. {
  49. setFollowsAll();
  50. }
  51. void LLViewBorder::setColors(const LLColor4& shadow_dark,
  52. const LLColor4& highlight_light)
  53. {
  54. mShadowDark = shadow_dark;
  55. mHighlightLight = highlight_light;
  56. }
  57. void LLViewBorder::setColorsExtended(const LLColor4& shadow_light,
  58. const LLColor4& shadow_dark,
  59. const LLColor4& highlight_light,
  60. const LLColor4& highlight_dark)
  61. {
  62. mShadowDark = shadow_dark;
  63. mShadowLight = shadow_light;
  64. mHighlightLight = highlight_light;
  65. mHighlightDark = highlight_dark;
  66. }
  67. void LLViewBorder::setTexture(const LLUUID& image_id)
  68. {
  69. mTexture = LLUI::getUIImageByID(image_id);
  70. }
  71. void LLViewBorder::draw()
  72. {
  73. if (STYLE_LINE == mStyle)
  74. {
  75. if (0 == mBorderWidth)
  76. {
  77. // no visible border
  78. }
  79. else if (1 == mBorderWidth)
  80. {
  81. drawOnePixelLines();
  82. }
  83. else if (2 == mBorderWidth)
  84. {
  85. drawTwoPixelLines();
  86. }
  87. else
  88. {
  89. llassert(false); // not implemented
  90. }
  91. }
  92. // draw the children
  93. LLView::draw();
  94. }
  95. void LLViewBorder::drawOnePixelLines()
  96. {
  97. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  98. LLColor4 top_color = mHighlightLight;
  99. LLColor4 bottom_color = mHighlightLight;
  100. switch (mBevel)
  101. {
  102. case BEVEL_OUT:
  103. top_color = mHighlightLight;
  104. bottom_color = mShadowDark;
  105. break;
  106. case BEVEL_IN:
  107. top_color = mShadowDark;
  108. bottom_color = mHighlightLight;
  109. break;
  110. case BEVEL_NONE:
  111. // use defaults
  112. break;
  113. default:
  114. llassert(0);
  115. }
  116. if (mHasKeyboardFocus)
  117. {
  118. top_color = gFocusMgr.getFocusColor();
  119. bottom_color = top_color;
  120. LLUI::setLineWidth(lerp(1.f, 3.f, gFocusMgr.getFocusFlashAmt()));
  121. }
  122. constexpr S32 left = 0;
  123. constexpr S32 bottom = 0;
  124. S32 top = getRect().getHeight();
  125. S32 right = getRect().getWidth();
  126. gGL.color4fv(top_color.mV);
  127. gl_line_2d(left, bottom, left, top);
  128. gl_line_2d(left, top, right, top);
  129. gGL.color4fv(bottom_color.mV);
  130. gl_line_2d(right, top, right, bottom);
  131. gl_line_2d(left, bottom, right, bottom);
  132. LLUI::setLineWidth(1.f);
  133. }
  134. void LLViewBorder::drawTwoPixelLines()
  135. {
  136. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  137. LLColor4 focus_color = gFocusMgr.getFocusColor();
  138. F32* top_in_color = mShadowDark.mV;
  139. F32* top_out_color = mShadowDark.mV;
  140. F32* bottom_in_color = mShadowDark.mV;
  141. F32* bottom_out_color = mShadowDark.mV;
  142. switch (mBevel)
  143. {
  144. case BEVEL_OUT:
  145. top_in_color = mHighlightLight.mV;
  146. top_out_color = mHighlightDark.mV;
  147. bottom_in_color = mShadowLight.mV;
  148. bottom_out_color = mShadowDark.mV;
  149. break;
  150. case BEVEL_IN:
  151. top_in_color = mShadowDark.mV;
  152. top_out_color = mShadowLight.mV;
  153. bottom_in_color = mHighlightDark.mV;
  154. bottom_out_color = mHighlightLight.mV;
  155. break;
  156. case BEVEL_BRIGHT:
  157. top_in_color = mHighlightLight.mV;
  158. top_out_color = mHighlightLight.mV;
  159. bottom_in_color = mHighlightLight.mV;
  160. bottom_out_color = mHighlightLight.mV;
  161. break;
  162. case BEVEL_NONE:
  163. // use defaults
  164. break;
  165. default:
  166. llassert(0);
  167. }
  168. if (mHasKeyboardFocus)
  169. {
  170. top_out_color = focus_color.mV;
  171. bottom_out_color = focus_color.mV;
  172. }
  173. S32 left = 0;
  174. S32 top = getRect().getHeight();
  175. S32 right = getRect().getWidth();
  176. S32 bottom = 0;
  177. // draw borders
  178. gGL.color3fv(top_out_color);
  179. gl_line_2d(left, bottom, left, top - 1);
  180. gl_line_2d(left, top - 1, right, top - 1);
  181. gGL.color3fv(top_in_color);
  182. gl_line_2d(left + 1, bottom + 1, left + 1, top - 2);
  183. gl_line_2d(left + 1, top - 2, right - 1, top - 2);
  184. gGL.color3fv(bottom_out_color);
  185. gl_line_2d(right - 1, top - 1, right - 1, bottom);
  186. gl_line_2d(left, bottom, right, bottom);
  187. gGL.color3fv(bottom_in_color);
  188. gl_line_2d(right - 2, top - 2, right - 2, bottom + 1);
  189. gl_line_2d(left + 1, bottom + 1, right - 1, bottom + 1);
  190. }
  191. bool LLViewBorder::getBevelFromAttribute(LLXMLNodePtr node,
  192. LLViewBorder::EBevel& bevel_style)
  193. {
  194. if (node->hasAttribute("bevel_style"))
  195. {
  196. std::string bevel_string;
  197. node->getAttributeString("bevel_style", bevel_string);
  198. LLStringUtil::toLower(bevel_string);
  199. if (bevel_string == "none")
  200. {
  201. bevel_style = LLViewBorder::BEVEL_NONE;
  202. }
  203. else if (bevel_string == "in")
  204. {
  205. bevel_style = LLViewBorder::BEVEL_IN;
  206. }
  207. else if (bevel_string == "out")
  208. {
  209. bevel_style = LLViewBorder::BEVEL_OUT;
  210. }
  211. else if (bevel_string == "bright")
  212. {
  213. bevel_style = LLViewBorder::BEVEL_BRIGHT;
  214. }
  215. return true;
  216. }
  217. return false;
  218. }
  219. //virtual
  220. const std::string& LLViewBorder::getTag() const
  221. {
  222. return LL_VIEW_BORDER_TAG;
  223. }
  224. //virtual
  225. LLXMLNodePtr LLViewBorder::getXML(bool save_children) const
  226. {
  227. LLXMLNodePtr nodep = LLView::getXML();
  228. nodep->setName(LL_VIEW_BORDER_TAG);
  229. return nodep;
  230. }
  231. //static
  232. LLView* LLViewBorder::fromXML(LLXMLNodePtr node, LLView* parent,
  233. LLUICtrlFactory* factory)
  234. {
  235. std::string name = LL_VIEW_BORDER_TAG;
  236. node->getAttributeString("name", name);
  237. LLViewBorder::EBevel bevel_style = LLViewBorder::BEVEL_IN;
  238. getBevelFromAttribute(node, bevel_style);
  239. S32 border_thickness = 1;
  240. node->getAttributeS32("border_thickness", border_thickness);
  241. LLViewBorder* border = new LLViewBorder(name, LLRect(), bevel_style,
  242. LLViewBorder::STYLE_LINE,
  243. border_thickness);
  244. border->initFromXML(node, parent);
  245. return border;
  246. }