PageRenderTime 47ms CodeModel.GetById 23ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lldebugmessagebox.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 291 lines | 233 code | 29 blank | 29 comment | 8 complexity | 7a61c05c0e8271efdca10fc76196e18e MD5 | raw file
  1/** 
  2 * @file lldebugmessagebox.cpp
  3 * @brief Implementation of a simple, non-modal message box.
  4 *
  5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "lldebugmessagebox.h"
 30
 31#include "llresmgr.h"
 32#include "llfontgl.h"
 33#include "llbutton.h"
 34#include "llsliderctrl.h"
 35#include "llcheckboxctrl.h"
 36#include "lltextbox.h"
 37#include "lllineeditor.h"
 38#include "llfocusmgr.h"
 39
 40///----------------------------------------------------------------------------
 41/// Class LLDebugVarMessageBox
 42///----------------------------------------------------------------------------
 43
 44std::map<std::string, LLDebugVarMessageBox*> LLDebugVarMessageBox::sInstances;
 45
 46LLDebugVarMessageBox::LLDebugVarMessageBox(const std::string& title, EDebugVarType var_type, void *var) : 
 47	LLFloater(LLSD()),
 48	mVarType(var_type), mVarData(var), mAnimate(FALSE)
 49{
 50	setRect(LLRect(10,160,400,10));
 51	
 52	LLSliderCtrl::Params slider_p;
 53	slider_p.label(title);
 54	slider_p.label_width(70);
 55	slider_p.text_width(40);
 56	slider_p.can_edit_text(true);
 57	slider_p.show_text(true);
 58
 59	mSlider1 = NULL;
 60	mSlider2 = NULL;
 61	mSlider3 = NULL;
 62
 63	switch(var_type)
 64	{
 65	case VAR_TYPE_F32:
 66		slider_p.name("slider 1");
 67		slider_p.rect(LLRect(20,130,190,110));
 68		slider_p.initial_value(*((F32*)var));
 69		slider_p.min_value(-100.f);
 70		slider_p.max_value(100.f);
 71		slider_p.increment(0.1f);
 72		slider_p.decimal_digits(3);
 73		mSlider1 = LLUICtrlFactory::create<LLSliderCtrl>(slider_p);
 74		addChild(mSlider1);
 75		break;
 76	case VAR_TYPE_S32:
 77		slider_p.name("slider 1");
 78		slider_p.rect(LLRect(20,100,190,80));
 79		slider_p.initial_value((F32)*((S32*)var));
 80		slider_p.min_value(-255.f);
 81		slider_p.max_value(255.f);
 82		slider_p.increment(1.f);
 83		slider_p.decimal_digits(0);
 84		mSlider1 = LLUICtrlFactory::create<LLSliderCtrl>(slider_p);
 85		addChild(mSlider1);
 86		break;
 87	case VAR_TYPE_VEC3:
 88		slider_p.name("slider 1");
 89		slider_p.label("x: ");
 90		slider_p.rect(LLRect(20,130,190,110));
 91		slider_p.initial_value(((LLVector3*)var)->mV[VX]);
 92		slider_p.min_value(-100.f);
 93		slider_p.max_value(100.f);
 94		slider_p.increment(0.1f);
 95		slider_p.decimal_digits(3);
 96		mSlider1 = LLUICtrlFactory::create<LLSliderCtrl>(slider_p);
 97
 98		slider_p.name("slider 2");
 99		slider_p.label("y: ");
100		slider_p.rect(LLRect(20,100,190,80));
101		slider_p.initial_value(((LLVector3*)var)->mV[VY]);
102		mSlider2 = LLUICtrlFactory::create<LLSliderCtrl>(slider_p);
103
104		slider_p.name("slider 3");
105		slider_p.label("z: ");
106		slider_p.rect(LLRect(20,70,190,50));
107		slider_p.initial_value(((LLVector3*)var)->mV[VZ]);
108		mSlider2 = LLUICtrlFactory::create<LLSliderCtrl>(slider_p);
109
110		addChild(mSlider1);
111		addChild(mSlider2);
112		addChild(mSlider3);
113		break;
114	default:
115		llwarns << "Unhandled var type " << var_type << llendl;
116		break;
117	}
118
119	LLButton::Params p;
120	p.name(std::string("Animate"));
121	p.label(std::string("Animate"));
122	p.rect(LLRect(20, 45, 180, 25));
123	p.click_callback.function(boost::bind(&LLDebugVarMessageBox::onAnimateClicked, this, _2));
124	mAnimateButton = LLUICtrlFactory::create<LLButton>(p);
125	addChild(mAnimateButton);
126
127	LLTextBox::Params params;
128	params.name("value");
129	params.initial_value(params.name());
130	params.rect(LLRect(20,20,190,0));
131	mText = LLUICtrlFactory::create<LLTextBox> (params);
132	addChild(mText);
133
134	//disable hitting enter closes dialog
135	setDefaultBtn();
136}
137
138LLDebugVarMessageBox::~LLDebugVarMessageBox()
139{
140	sInstances.erase(mTitle);
141}
142
143void LLDebugVarMessageBox::show(const std::string& title, F32 *var, F32 max_value, F32 increment)
144{
145#ifndef LL_RELEASE_FOR_DOWNLOAD
146	LLDebugVarMessageBox* box = show(title, VAR_TYPE_F32, (void*)var);
147	max_value = llabs(max_value);
148	box->mSlider1->setMaxValue(max_value);
149	box->mSlider1->setMinValue(-max_value);
150	box->mSlider1->setIncrement(increment);
151	if (!gFocusMgr.childHasKeyboardFocus(box))
152	{
153		box->mSlider1->setValue(*var);
154	}
155	box->mSlider1->setCommitCallback(boost::bind(&LLDebugVarMessageBox::sliderChanged, box, _2));
156#endif
157}
158
159void LLDebugVarMessageBox::show(const std::string& title, S32 *var, S32 max_value, S32 increment)
160{
161#ifndef LL_RELEASE_FOR_DOWNLOAD
162	LLDebugVarMessageBox* box = show(title, VAR_TYPE_S32, (void*)var);
163	F32 max_val = llabs((F32)max_value);
164	box->mSlider1->setMaxValue(max_val);
165	box->mSlider1->setMinValue(-max_val);
166	box->mSlider1->setIncrement((F32)increment);
167	if (!gFocusMgr.childHasKeyboardFocus(box))
168	{
169		box->mSlider1->setValue((F32)*var);
170	}
171	box->mSlider1->setCommitCallback(boost::bind(&LLDebugVarMessageBox::sliderChanged, box, _2));
172#endif
173}
174
175void LLDebugVarMessageBox::show(const std::string& title, LLVector3 *var, LLVector3 max_value, LLVector3 increment)
176{
177#ifndef LL_RELEASE_FOR_DOWNLOAD
178	LLDebugVarMessageBox* box = show(title, VAR_TYPE_VEC3, (LLVector3*)var);
179	max_value.abs();
180	box->mSlider1->setMaxValue(max_value.mV[VX]);
181	box->mSlider1->setMinValue(-max_value.mV[VX]);
182	box->mSlider1->setIncrement(increment.mV[VX]);
183	box->mSlider1->setCommitCallback(boost::bind(&LLDebugVarMessageBox::sliderChanged, box, _2));
184
185	box->mSlider2->setMaxValue(max_value.mV[VX]);
186	box->mSlider2->setMinValue(-max_value.mV[VX]);
187	box->mSlider2->setIncrement(increment.mV[VX]);
188	box->mSlider2->setCommitCallback(boost::bind(&LLDebugVarMessageBox::sliderChanged, box, _2));
189
190	box->mSlider3->setMaxValue(max_value.mV[VX]);
191	box->mSlider3->setMinValue(-max_value.mV[VX]);
192	box->mSlider3->setIncrement(increment.mV[VX]);
193	box->mSlider3->setCommitCallback(boost::bind(&LLDebugVarMessageBox::sliderChanged, box, _2));
194#endif
195}
196
197LLDebugVarMessageBox* LLDebugVarMessageBox::show(const std::string& title, EDebugVarType var_type, void *var)
198{
199	std::string title_string(title);
200
201	LLDebugVarMessageBox *box = sInstances[title_string];
202	if (!box)
203	{
204		box = new LLDebugVarMessageBox(title, var_type, var);
205		sInstances[title_string] = box;
206		gFloaterView->addChild(box);
207		box->reshape(200,150);
208		box->openFloater();
209		box->mTitle = title_string;
210	}
211
212	return box;
213}
214
215void LLDebugVarMessageBox::sliderChanged(const LLSD& data)
216{
217	if (!mVarData)
218		return;
219
220	switch(mVarType)
221	{
222	case VAR_TYPE_F32:
223		*((F32*)mVarData) = (F32)mSlider1->getValue().asReal();
224		break;
225	case VAR_TYPE_S32:
226		*((S32*)mVarData) = (S32)mSlider1->getValue().asInteger();
227		break;
228	case VAR_TYPE_VEC3:
229	{
230		LLVector3* vec_p = (LLVector3*)mVarData;
231		vec_p->setVec((F32)mSlider1->getValue().asReal(), 
232			(F32)mSlider2->getValue().asReal(), 
233			(F32)mSlider3->getValue().asReal());
234		break;
235	}
236	default:
237		llwarns << "Unhandled var type " << mVarType << llendl;
238		break;
239	}
240}
241
242void LLDebugVarMessageBox::onAnimateClicked(const LLSD& data)
243{
244	mAnimate = !mAnimate;
245	mAnimateButton->setToggleState(mAnimate);
246}
247
248void LLDebugVarMessageBox::draw()
249{
250	std::string text;
251	switch(mVarType)
252	{
253	  case VAR_TYPE_F32:
254		text = llformat("%.3f", *((F32*)mVarData));
255		break;
256	  case VAR_TYPE_S32:
257		text = llformat("%d", *((S32*)mVarData));
258		break;
259	  case VAR_TYPE_VEC3:
260	  {
261		  LLVector3* vec_p = (LLVector3*)mVarData;
262		  text= llformat("%.3f %.3f %.3f", vec_p->mV[VX], vec_p->mV[VY], vec_p->mV[VZ]);
263		  break;
264	  }
265	  default:
266		llwarns << "Unhandled var type " << mVarType << llendl;
267		break;
268	}
269	mText->setText(text);
270
271	if(mAnimate)
272	{
273		if (mSlider1)
274		{
275			F32 animated_val = clamp_rescale(fmodf((F32)LLFrameTimer::getElapsedSeconds() / 5.f, 1.f), 0.f, 1.f, 0.f, mSlider1->getMaxValue());
276			mSlider1->setValue(animated_val);
277			sliderChanged(LLSD());
278			if (mSlider2)
279			{
280				mSlider2->setValue(animated_val);
281				sliderChanged(LLSD());
282			}
283			if (mSlider3)
284			{
285				mSlider3->setValue(animated_val);
286				sliderChanged(LLSD());
287			}
288		}
289	}
290	LLFloater::draw();
291}