コード例 #1
0
void ButtonWidget::drawAt ( Drawer &drawer, MenuWidget* parent, int x, int y, Align align )
{
	if ( !show )
	{
		return;
	}

	// Choose correct style
	Style * currentStyle = getCurrentStyle ();

	if ( this->textCache == nullptr )
	{
		this->prepare ( drawer );
		lastStyle = style;
	}
	else
	{
		if ( currentStyle != lastStyle )
		{
			prepare ( drawer, *currentStyle );
			lastStyle = currentStyle;
		}
	}

	// draw
	if ( currentStyle == nullptr )
	{
		SDL_Point alignedPos = this->getAlignedPos( x, y, align );
		lastPos = alignedPos;
		this->textCache->drawAt( alignedPos );
	}
	else
	{
		SDL_Point alignedPos = this->getAlignedPos( x, y, align );
		SDL_Rect size = { alignedPos.x, alignedPos.y, this->getWidth(), this->getHeight() };
		lastPos = alignedPos;

		drawer.setClipRect( size );

		// draw background
		currentStyle->drawBackground( drawer, size );

		// draw text
		DirSize padding = style->getPadding();
		SDL_Point innerPos = { alignedPos.x + padding.left, alignedPos.y + padding.top };
		if ( !hasDynamicWidth() )
		{
			int innerWidth = style->getWidth() - padding.left - padding.right;
			innerPos.x += (innerWidth - textCache->getWidth()) / 2;
		}
		if ( !hasDynamicHeight() )
		{
			int innerHeight = style->getHeight() - padding.top - padding.bottom;
			innerPos.y += (innerHeight - textCache->getHeight()) / 2;
		}
		
		this->textCache->drawAt( innerPos );

		// draw border
		SDL_Rect border = size;

		drawer.setDrawingColor( currentStyle->getBorderColor() );
		for ( int i = 0; i < currentStyle->getBorderThickness(); i++ )
		{
			drawer.drawRect( border );
			border.x++;
			border.y++;
			border.w -= 2;
			border.h -= 2;
		}

		drawer.cancelClipRect();
	}
}
コード例 #2
0
void TextInputWidget::drawAt( Drawer & drawer, MenuWidget * parent, int x, int y, Align align )
{
	if ( !show )
	{
		return;
	}

	if ( active && !SDL_IsTextInputActive() )
	{
		SDL_StartTextInput();
	}

	// Choose correct style
	Style * currentStyle = getCurrentStyle();

	if ( this->textCache == nullptr )
	{
		this->prepare( drawer, *currentStyle );
		lastStyle = style;
	}
	else
	{
		if ( currentStyle != lastStyle || textChanged )
		{
			prepare( drawer, *currentStyle );
			lastStyle = currentStyle;
		}
	}

	// draw
	if ( currentStyle == nullptr )
	{
		SDL_Point alignedPos = this->getAlignedPos( x, y, align );
		lastPos = alignedPos;
		this->textCache->drawAt( alignedPos );
	}
	else
	{
		SDL_Point alignedPos = this->getAlignedPos( x, y, align );
		SDL_Rect size = { alignedPos.x, alignedPos.y, this->getWidth(), this->getHeight() };
		lastPos = alignedPos;

		drawer.setClipRect( size );

		// draw background
		currentStyle->drawBackground( drawer, size );

		// draw text
		DirSize padding = style->getPadding();
		SDL_Point innerPos = { alignedPos.x + padding.left, alignedPos.y + padding.top };
		int innerWidth = style->getWidth() - padding.left - padding.right;
		drawer.setClipRect( { innerPos.x, innerPos.y, innerWidth, style->getHeight() } );
		innerPos.y += ( style->getHeight() - padding.top - padding.bottom - textCache->getHeight() ) / 2;

		int width = drawer.getTextSize( this->text.substr( 0, this->cursorPosition ),
										style->getFontSize(), style->getFontStyle() ).w + 1;

		int offset = 0;

		if ( width > innerWidth )
		{
			offset = width - innerWidth;
		}

		if ( text != "" )
			this->textCache->drawAt( innerPos.x - offset, innerPos.y );

		if ( active )
		{
			drawer.setDrawingColor( style->getFontColor() );
			drawer.drawLine( { width + innerPos.x - offset - 1, innerPos.y - 2 },
			{ width + innerPos.x - offset - 1, innerPos.y + textCache->getHeight() + 2 } );
		}

		drawer.setClipRect( size );

		// draw border
		SDL_Rect border = size;

		drawer.setDrawingColor( currentStyle->getBorderColor() );
		for ( int i = 0; i < currentStyle->getBorderThickness(); i++ )
		{
			drawer.drawRect( border );
			border.x++;
			border.y++;
			border.w -= 2;
			border.h -= 2;
		}

		drawer.cancelClipRect();
	}
}