Ejemplo n.º 1
0
	void Enemy::render()
	{
		renderShadow();
		kuma_.render();
		renderLifeBar();
	}
Ejemplo n.º 2
0
void TextBoxContent::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
	DEBUG_TSTART();
	// paint parent
	AbstractContent::paint(painter, option, widget);

	
	painter->save();
	
	//TODO should we clip to the rect or FORCE resize the rect? probably clip...
	//painter->setClipRect(contentsRect());
	//if(option)
	//	painter->setClipRect(option->exposedRect);
	painter->translate(contentsRect().topLeft()); // + QPoint(p.width(),p.width()));

	if(sceneContextHint() == MyGraphicsScene::StaticPreview || !modelItem()->shadowEnabled())
	{
		// If we're drawing in a Preview scene, then we render directly with the painter
		// (rather than caching the results in a pixmap) because this allows the painter 
		// to scale the text glyphs directly (vector scaling), rather than scaling bits 
		// in a pixmap (bitmap scaling), producing more legible results at lower scalings
		
// 		qDebug() << modelItem()->itemName()<<"TextBoxContent::paint: Rendering either preview or no shadow";
		
		QAbstractTextDocumentLayout::PaintContext pCtx;

		// What was this for - improving performance?
		// I've removed it because it seems to be causing the issue reported in Issue #51 on the google code issues tracker.
		// I'll keep an eye on performance to see if it suffers at all. For now, closing issue #51.
		//pCtx.clip = option->exposedRect;
		
		bool needRestore = false;
		
		if(m_zoomEnabled && sceneContextHint() == MyGraphicsScene::Live)
		{
			needRestore = true;
			painter->save();
			double xf = (1/m_zoomDestPoint.x());
			double yf = (1/m_zoomDestPoint.y());
			double sx = m_zoomCurSize.x() / m_zoomStartSize.x();
			double sy = m_zoomCurSize.y() / m_zoomStartSize.y();
			QRect cRect = contentsRect();
			painter->translate(cRect.width()/xf - m_zoomCurSize.x()/xf,cRect.height()/yf - m_zoomCurSize.y()/yf);
			painter->scale(sx,sy);
			
// 			qDebug() << modelItem()->itemName()<<"TextBoxContent::paint: Enabling tranlate & scale. Scale:"<<sx<<","<<sy<<". xf/yf:"<<xf<<","<<yf;
		}
			
			
	
		if(modelItem()->shadowEnabled())
		{
// 			qDebug() << modelItem()->itemName()<<"TextBoxContent::paint: Drawing m_shadowText";
							
			painter->save();
			
			painter->translate(modelItem()->shadowOffsetX(),modelItem()->shadowOffsetY());
			m_shadowText->documentLayout()->draw(painter, pCtx);
	
			painter->restore();
		}
		
		m_text->documentLayout()->draw(painter, pCtx);
		
		if(needRestore)
			painter->restore();
		
	}
	else
	{
		QPixmap cache;
// 		qDebug() << modelItem()->itemName()<<"TextBoxContent::paint: Rendering either live or with shadow";
		
		// The primary and only reason we cache the text rendering is inorder
		// to paint the text and shadow as a single unit (e.g. composite the
		// shadow+text BEFORE applying opacity rather than setting the opacity
		// before rendering the shaodw.) If we didnt cache the text as a pixmap
		// (e.g. render text directly) then when crossfading, the shadow
		// "apperas" to fade out last, after the text.
		
		// Update 20091015: Implemented very aggressive caching across TextBoxContent instances
		// that share the same modelItem() (see ::cacheKey()) inorder to avoid re-rendering 
		// potentially expensive drop shadows, below.
		
		
		
		QString key = cacheKey();
		if(m_text->toPlainText().trimmed().isEmpty())
		{
			// "<< m_text->toHtml()<<"
			//qDebug() << modelItem()->itemName()<<": Not rendering cache because:"<< QPixmapCache::find(key,cache)<< " or ...";//plain "<<m_text->toPlainText()<<" is empty";
			cache = QPixmap(contentsRect().size());
			cache.fill(Qt::transparent);
		}
		else
		{
			if(!QPixmapCache::find(key,cache))
			{
				if(QFile(key).exists())
				{
					cache.load(key);
					QPixmapCache::insert(key,cache);
					//qDebug()<<"TextBoxContent::paint(): modelItem:"<<modelItem()->itemName()<<": Cache load from"<<key;
				}
				else
				{
					qDebug()<<"TextBoxContent::paint(): modelItem:"<<modelItem()->itemName()<<": Cache redraw";

					QSizeF shadowSize = modelItem()->shadowEnabled() ? QSizeF(modelItem()->shadowOffsetX(),modelItem()->shadowOffsetY()) : QSizeF(0,0);
					cache = QPixmap((contentsRect().size()+shadowSize).toSize());

					cache.fill(Qt::transparent);
					QPainter textPainter(&cache);

					QAbstractTextDocumentLayout::PaintContext pCtx;

					#if QT46_SHADOW_ENAB == 0
					if(modelItem()->shadowEnabled())
						renderShadow(&textPainter,&pCtx);
					#endif

					// If we're zooming, we want to render the text straight to the painter
					// so it can transform the raw vectors instead of scaling the bitmap.
					// But if we're not zooming, we cache the text with the shadow since it
					// looks better that way when we're crossfading.
					if(!m_zoomEnabled)
						m_text->documentLayout()->draw(&textPainter, pCtx);

					cache.save(key,"PNG");
					QPixmapCache::insert(key, cache);
				}
			}
		}
	
		// Draw a rectangular outline in the editor inorder to visually locate empty text blocks
		if(sceneContextHint() == MyGraphicsScene::Editor &&
			m_text->toPlainText().trimmed() == "")
		{
			QPen p = modelItem() ? modelItem()->outlinePen() : QPen(Qt::black,1.5);
			painter->setPen(p);
			painter->setBrush(Qt::NoBrush);
	
			painter->drawRect(QRect(QPoint(0,0),contentsRect().size()));
		}
		else
		{
			if(m_zoomEnabled)
			{
				double xf = (1/m_zoomDestPoint.x());
				double yf = (1/m_zoomDestPoint.y());
				double sx = m_zoomCurSize.x() / m_zoomStartSize.x();
				double sy = m_zoomCurSize.y() / m_zoomStartSize.y();
				painter->save();
				QRect cRect = contentsRect();
				painter->translate(cRect.width()/xf - m_zoomCurSize.x()/xf,cRect.height()/yf - m_zoomCurSize.y()/yf);
				painter->scale(sx,sy);
				painter->drawPixmap(0,0,cache);
				QAbstractTextDocumentLayout::PaintContext pCtx;
				m_text->documentLayout()->draw(painter, pCtx);
				painter->restore();
				
			}
			else
			{
				painter->drawPixmap(0,0,cache);
				if(sceneContextHint() != MyGraphicsScene::Live && modelItem()->zoomEffectEnabled())
				{
					// cache may not contain the actual text, just shadow, since its not live,
					// so render the text
					QAbstractTextDocumentLayout::PaintContext pCtx;
					m_text->documentLayout()->draw(painter, pCtx);
				}
					
			}
		}
	}


	painter->restore();
	
	//qDebug() << "TextBoxContent::paint(): \t \t Elapsed:"<<(((double)total.elapsed())/1000.0)<<" sec";
}