graphicscontext.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,799 行 · 第 1/4 页

CPP
1,799
字号
//GraphicsContext.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/GraphicsKit/GraphicsKit.h"#include "vcf/GraphicsKit/DrawUIState.h"#include "thirdparty/common/agg/include/agg_ellipse.h"#include "thirdparty/common/agg/include/agg_arc.h"namespace VCF {/*** The structure used to fully save and restore the state of a Graphics context.*@see GraphicsContext::saveState()*/class GraphicsState {public:	GraphicsState();	GraphicsState( const GraphicsState& rhs );	~GraphicsState();	GraphicsState& operator=( const GraphicsState& rhs );	void compositeMatrix();public:	Fill* fill_;	Stroke* stroke_;	Path* clippingPath_;	GraphicsContext* owningContext_;		double strokeWidth_;		Font font_;	Matrix2D transformMatrix_;	Point currentMoveTo_;	Color color_;	double rotation_;	double shearX_;	double shearY_;	double translateX_;	double translateY_;	double scaleX_;	double scaleY_;	};GraphicsState::GraphicsState():	fill_(NULL),	stroke_(NULL),	clippingPath_(NULL),	owningContext_(NULL),	strokeWidth_(1.0),	rotation_(0.0),	shearX_(0.0),	shearY_(0.0),	translateX_(0.0),	translateY_(0.0),	scaleX_(1.0),	scaleY_(1.0){	transformMatrix_.identity();}GraphicsState::GraphicsState( const GraphicsState& rhs ):	fill_(NULL),	stroke_(NULL),	clippingPath_(NULL),	owningContext_(NULL),	strokeWidth_(1.0),	rotation_(0.0),	shearX_(0.0),	shearY_(0.0),	translateX_(0.0),	translateY_(0.0),	scaleX_(1.0),	scaleY_(1.0){	transformMatrix_.identity();	*this = rhs;}GraphicsState::~GraphicsState(){	if ( NULL != clippingPath_ ) {		Object* pathObj = dynamic_cast<Object*>( clippingPath_ );		pathObj->release();	}	clippingPath_ = NULL;}GraphicsState& GraphicsState::operator=( const GraphicsState& rhs ){	strokeWidth_ = rhs.strokeWidth_;	fill_ = rhs.fill_;	stroke_ = rhs.stroke_;	font_ = rhs.font_;	if ( NULL != clippingPath_ ) { //release the underlying object instance		Object* pathObj = dynamic_cast<Object*>( clippingPath_ );		pathObj->release();	}	clippingPath_ = rhs.clippingPath_;	if ( NULL != clippingPath_ ) { //take ownership of the underlying object instance		Object* pathObj = dynamic_cast<Object*>( clippingPath_ );		pathObj->addRef();	}	owningContext_ = rhs.owningContext_;	transformMatrix_ = rhs.transformMatrix_;	currentMoveTo_ = rhs.currentMoveTo_;	color_ = rhs.color_;	rotation_ = rhs.rotation_;	shearX_ = rhs.shearX_;	shearY_ = rhs.shearY_;	translateX_ = rhs.translateX_;	translateY_ = rhs.translateY_;	scaleX_ = rhs.scaleX_;	scaleY_ = rhs.scaleY_;	return *this;}void GraphicsState::compositeMatrix(){	Matrix2D rotate;	rotate.rotate( rotation_ );	Matrix2D scale;	scale.scale( scaleX_, scaleY_ );	Matrix2D shear;	shear.shear( shearX_, shearY_ );	Matrix2D translation;	translation.translate( translateX_, translateY_ );	Matrix2D tmp;	tmp.multiply( &scale, &rotate );	tmp.multiply( &tmp, &shear );	tmp.multiply( &tmp, &translation );	transformMatrix_ = tmp;}GraphicsContext::GraphicsContext():	contextPeer_(NULL),	currentDrawingState_(GraphicsContext::gsNone),			drawingArea_(NULL),	renderBuffer_(NULL),	renderAreaDirty_(false),	graphicsStateIndex_(0),	currentGraphicsState_(NULL){	GraphicsState* newState = new GraphicsState();	newState->owningContext_ = this;	stateCollection_.push_back( newState );	currentGraphicsState_ = stateCollection_[graphicsStateIndex_];	Font& font = currentGraphicsState_->font_;	font.setGraphicsContext( this );	init();}GraphicsContext::GraphicsContext( const unsigned long& width, const unsigned long& height ):	contextPeer_(NULL),	currentDrawingState_(GraphicsContext::gsNone),			drawingArea_(NULL),	renderBuffer_(NULL),	renderAreaDirty_(false),	graphicsStateIndex_(0),	currentGraphicsState_(NULL){	GraphicsState* newState = new GraphicsState();	newState->owningContext_ = this;	stateCollection_.push_back( newState );	currentGraphicsState_ = stateCollection_[graphicsStateIndex_];	contextPeer_ = GraphicsToolkit::createContextPeer( width, height );	if ( NULL == contextPeer_ ){		//throw exception	}	contextPeer_->setContext( this );	init();	Font& font = currentGraphicsState_->font_;	font.setGraphicsContext( this );	font.setPointSize( font.getPointSize() );}GraphicsContext::GraphicsContext( OSHandleID contextID ):		contextPeer_(NULL),	currentDrawingState_(GraphicsContext::gsNone),	drawingArea_(NULL),	renderBuffer_(NULL),	renderAreaDirty_(false),	graphicsStateIndex_(0),	currentGraphicsState_(NULL){	GraphicsState* newState = new GraphicsState();	newState->owningContext_ = this;	stateCollection_.push_back( newState );	currentGraphicsState_ = stateCollection_[graphicsStateIndex_];	contextPeer_ = GraphicsToolkit::createContextPeer( contextID );	if ( NULL == contextPeer_ ){		//throw exception	}	contextPeer_->setContext( this );	init();	Font& font = currentGraphicsState_->font_;	font.setGraphicsContext( this );	font.setPointSize( font.getPointSize() );}GraphicsContext::~GraphicsContext(){	if ( NULL != contextPeer_ ){		delete contextPeer_;	}	contextPeer_ = NULL;	GraphicsStateCollection::iterator it = stateCollection_.begin();	while ( it != stateCollection_.end () ) {		delete *it;		it ++;	}	stateCollection_.clear();	if ( NULL != drawingArea_ ) {		delete drawingArea_;	}}void GraphicsContext::init(){	//currentFont_ = new Font();	//currentFont_->setGraphicsContext( this );	//currentFont_->setPointSize( currentFont_->getPointSize() );	//transformMatrix_.identity();}void GraphicsContext::setCurrentFont(Font * font){	Font& currentFont = currentGraphicsState_->font_;	currentFont = *font;	currentFont.setGraphicsContext( this );	currentFont.setPointSize( currentFont.getPointSize() );}void GraphicsContext::setCurrentFont( const Font * font ){	Font& currentFont = currentGraphicsState_->font_;	currentFont = *font;	currentFont.setGraphicsContext( this );	currentFont.setPointSize( currentFont.getPointSize() );}void GraphicsContext::setCurrentFill(Fill * fill){	if ( NULL != fill ){		fill->setContext( this );	}	currentGraphicsState_->fill_ = fill;}void GraphicsContext::setCurrentStroke(Stroke * stroke){	if ( NULL != stroke ){		stroke->setContext( this );	}	currentGraphicsState_->stroke_ = stroke;}void GraphicsContext::draw(Path * path){	Fill* fill = currentGraphicsState_->fill_;	Stroke* stroke = currentGraphicsState_->stroke_;	if ( NULL != fill ){		fill->render( path );	}	if ( NULL != stroke ){		stroke->render( path );	}}void GraphicsContext::drawImage( const double& x, const double& y, Image * image){	Rect bounds(0,0,image->getWidth(), image->getHeight() );	drawPartialImage( x, y, &bounds, image );}void GraphicsContext::drawImageWithState( const double& x, const double& y, Image * image, const bool& enabled ){	drawImage( x, y, image );	/*	if ( contextPeer_->prepareForDrawing( GraphicsContext::doImage ) ) {		Rect bounds(0,0,image->getWidth(), image->getHeight() );		contextPeer_->drawImage( x, y, &bounds, image );		contextPeer_->finishedDrawing( 	GraphicsContext::doImage );	}	*/}void GraphicsContext::drawImageWithinBounds( Rect* bounds, Image* image ){	Rect imageBounds( 0,0,bounds->getWidth(), bounds->getHeight() );	drawPartialImage( bounds->left_, bounds->top_, &imageBounds, image );}void GraphicsContext::drawPartialImage( const double& x, const double& y, Rect* imageBounds, Image* image ){	//contextPeer_->drawPartialImage( x, y, imageBounds, image );	if ( contextPeer_->prepareForDrawing( GraphicsContext::doImage ) ) {		contextPeer_->drawImage( x, y, imageBounds, image );		contextPeer_->finishedDrawing( 	GraphicsContext::doImage );	}}void GraphicsContext::textWithStateAt( const double& x, const double& y, const String& text, const bool& enabled ){	if ( enabled ) {		textAt( x, y, text );	}	else {		Color* shadow = GraphicsToolkit::getSystemColor( SYSCOLOR_SHADOW );		Color* hilight = GraphicsToolkit::getSystemColor( SYSCOLOR_HIGHLIGHT );		Color oldColor = *currentGraphicsState_->font_.getColor();		currentGraphicsState_->font_.setColor( hilight );		textAt( x+1, y+1, text );		currentGraphicsState_->font_.setColor( shadow );		textAt( x, y, text );		//reset the original color		currentGraphicsState_->font_.setColor( &oldColor );	}}void GraphicsContext::textAt(const double & x, const double & y, const String & text){	Rect bounds( x, y, x + getTextWidth(text), y + getTextHeight(text) );	Rect tmp = bounds;	double xx = bounds.left_ * (currentGraphicsState_->transformMatrix_[Matrix2D::mei00]) +							bounds.top_ * (currentGraphicsState_->transformMatrix_[Matrix2D::mei10]) +								(currentGraphicsState_->transformMatrix_[Matrix2D::mei20]);	double yy = bounds.left_ * (currentGraphicsState_->transformMatrix_[Matrix2D::mei01]) +							bounds.top_ * (currentGraphicsState_->transformMatrix_[Matrix2D::mei11]) +								(currentGraphicsState_->transformMatrix_[Matrix2D::mei21]);	tmp.offset( xx - tmp.left_, yy - tmp.top_ );	if ( contextPeer_->prepareForDrawing( GraphicsContext::doText ) ) {		contextPeer_->textAt( tmp, text );		contextPeer_->finishedDrawing( 	GraphicsContext::doText );	}}void GraphicsContext::textAt( const double & x, const double & y, const String& text, const long drawOptions ){	Rect bounds( x, y, x + getTextWidth(text), y + getTextHeight(text) );	double xx = x * (currentGraphicsState_->transformMatrix_[Matrix2D::mei00]) +							y * (currentGraphicsState_->transformMatrix_[Matrix2D::mei10]) +								(currentGraphicsState_->transformMatrix_[Matrix2D::mei20]);	double yy = x * (currentGraphicsState_->transformMatrix_[Matrix2D::mei01]) +							y * (currentGraphicsState_->transformMatrix_[Matrix2D::mei11]) +								(currentGraphicsState_->transformMatrix_[Matrix2D::mei21]);	bounds.offset( xx - bounds.left_, yy - bounds.top_ );	if ( contextPeer_->prepareForDrawing( GraphicsContext::doText ) ) {		contextPeer_->textAt( bounds, text, drawOptions );		contextPeer_->finishedDrawing( 	GraphicsContext::doText );	}}void GraphicsContext::rectangle(const double & x1, const double & y1, const double & x2, const double & y2){	checkPathOperations();	currentDrawingState_ = GraphicsContext::gsAddingGraphicsOps;	pathOperations_.push_back( PointOperation(x1,y1,PointOperation::ptRect) );	pathOperations_.push_back( PointOperation(x2,y2,PointOperation::ptRect) );}void GraphicsContext::roundRect(const double & x1, const double & y1, const double & x2, const double & y2,								const double & xc, const double & yc){	checkPathOperations();	currentDrawingState_ = GraphicsContext::gsAddingGraphicsOps;	pathOperations_.push_back( PointOperation(x1,y1,PointOperation::ptRoundRect) );	pathOperations_.push_back( PointOperation(x2,y2,PointOperation::ptRoundRect) );	pathOperations_.push_back( PointOperation(xc,yc,PointOperation::ptRoundRect) );}void GraphicsContext::circle(const double & x, const double & y, const double & radius){	ellipse( x - radius, y - radius, x + radius, y + radius );}void GraphicsContext::ellipse(const double & x1, const double & y1, const double & x2, const double & y2){	checkPathOperations();	currentDrawingState_ = GraphicsContext::gsAddingGraphicsOps;	pathOperations_.push_back( PointOperation(x1,y1,PointOperation::ptEllipse) );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?