Exemple #1
0
//------------------------------------------------------------------------
void CTextButton::draw (CDrawContext* context)
{
	bool highlight = value > 0.5 ? true : false;
	context->setDrawMode (kAntiAliasing);
	context->setLineWidth (frameWidth);
	context->setLineStyle (CLineStyle (CLineStyle::kLineCapRound, CLineStyle::kLineJoinRound));
	context->setFrameColor (highlight ? frameColorHighlighted : frameColor);
	CRect r (getViewSize ());
	r.inset (frameWidth / 2., frameWidth / 2.);
	CGraphicsPath* path = getPath (context);
	if (path)
	{
		CColor color1 = highlight ? gradientStartColorHighlighted : gradientStartColor;
		CColor color2 = highlight ? gradientEndColorHighlighted : gradientEndColor;
		CGradient* gradient = path->createGradient (0.2, 1, color1, color2);
		if (gradient)
		{
			context->fillLinearGradient (path, *gradient, r.getTopLeft (), r.getBottomLeft (), false);
			gradient->forget ();
		}
		else
		{
			context->setFillColor (highlight ? gradientStartColorHighlighted : gradientStartColor);
			context->drawGraphicsPath (path, CDrawContext::kPathFilled);
		}
		context->drawGraphicsPath (path, CDrawContext::kPathStroked);
	}
	else
	{
		context->setFillColor (highlight ? gradientStartColorHighlighted : gradientStartColor);
		context->drawRect (getViewSize (), kDrawFilledAndStroked);
	}
	CRect titleRect = getViewSize ();
	titleRect.inset (frameWidth / 2., frameWidth / 2.);

	CBitmap* iconToDraw = highlight ? (iconHighlighted ? iconHighlighted : icon) : (icon ? icon : iconHighlighted);
	if (iconToDraw)
	{
		CRect iconRect (0, 0, iconToDraw->getWidth (), iconToDraw->getHeight ());
		iconRect.offset (titleRect.left, titleRect.top);
		switch (iconPosition)
		{
			case kLeft:
			{
				iconRect.offset (textMargin, titleRect.getHeight () / 2. - iconRect.getHeight () / 2.);
				titleRect.left = iconRect.right;
				titleRect.right -= textMargin;
				if (getTextAlignment () == kLeftText)
					titleRect.left += textMargin;
				break;
			}
			case kRight:
			{
				iconRect.offset (titleRect.getWidth () - (textMargin + iconRect.getWidth ()), titleRect.getHeight () / 2. - iconRect.getHeight () / 2.);
				titleRect.right = iconRect.left;
				titleRect.left += textMargin;
				if (getTextAlignment () == kRightText)
					titleRect.right -= textMargin;
				break;
			}
			case kCenterAbove:
			{
				iconRect.offset (titleRect.getWidth () / 2. - iconRect.getWidth () / 2., 0);
				if (title.size () > 0)
				{
					iconRect.offset (0, titleRect.getHeight () / 2. - (iconRect.getHeight () / 2. + (textMargin + font->getSize ()) / 2.));
					titleRect.top = iconRect.bottom + textMargin;
					titleRect.setHeight (font->getSize ());
					if (getTextAlignment () == kLeftText)
						titleRect.left += textMargin;
					else if (getTextAlignment () == kRightText)
						titleRect.right -= textMargin;
				}
				else
				{
					iconRect.offset (0, titleRect.getHeight () / 2. - iconRect.getHeight () / 2.);
				}
				break;
			}
			case kCenterBelow:
			{
				iconRect.offset (titleRect.getWidth () / 2. - iconRect.getWidth () / 2., 0);
				if (title.size () > 0)
				{
					iconRect.offset (0, titleRect.getHeight () / 2. - (iconRect.getHeight () / 2.) + (textMargin + font->getSize ()) / 2.);
					titleRect.top = iconRect.top - (textMargin + font->getSize ());
					titleRect.setHeight (font->getSize ());
					if (getTextAlignment () == kLeftText)
						titleRect.left += textMargin;
					else if (getTextAlignment () == kRightText)
						titleRect.right -= textMargin;
				}
				else
				{
					iconRect.offset (0, titleRect.getHeight () / 2. - iconRect.getHeight () / 2.);
				}
				break;
			}
		}
		context->drawBitmap (iconToDraw, iconRect);
	}
	else
	{
		if (getTextAlignment () == kLeftText)
			titleRect.left += textMargin;
		else if (getTextAlignment () == kRightText)
			titleRect.right -= textMargin;
	}
	if (title.size () > 0)
	{
		context->setFont (font);
		context->setFontColor (highlight ? textColorHighlighted : textColor);
		context->drawString (title.c_str (), titleRect, horiTxtAlign);
	}
	setDirty (false);
}