#include "ad_graphics.h" #include #include AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(parent) { data = adcore->addata; core = adcore; setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); setAlignment(Qt::AlignLeft | Qt::AlignTop); scene = new QGraphicsScene(); scene->setItemIndexMethod(QGraphicsScene::NoIndex); core->setScene(scene); setScene(scene); //setCacheMode(); //setViewportUpdateMode(QGraphicsView::FullViewportUpdate); mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32); //setDragMode(RubberBandDrag); QPainter p(mapimg); for (int i=0; imap->cells().size(); i++) { for (int j=0; jmap->cells().at(i).size(); j++) { QPen pen; QBrush brush; pen.setColor(Qt::white); brush.setColor(Qt::white); brush.setStyle(Qt::SolidPattern); int cel = data->map->cells().at(i).at(j); if (cel == Map::Wall) { pen.setColor(Qt::black); brush.setColor(Qt::black); } if (cel >= Map::Free || (cel <= Map::PlayerTower && cel !=Map::Wall)) { pen.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,150)); brush.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,80)); } p.setPen(pen); p.setBrush(brush); p.drawRect(i*cellSize,j*cellSize,cellSize,cellSize); } } p.end(); scene->setSceneRect(mapimg->rect()); scene->setBackgroundBrush(*mapimg); setCacheMode(QGraphicsView::CacheBackground); resize(mapimg->size()); startTimer(50); } AD_Graphics::~AD_Graphics() { delete scene; } void AD_Graphics::mousePressEvent(QMouseEvent *event) { scene->clearSelection(); if (event->button() == Qt::LeftButton) { if (m_building) emit add_tow((QPointF(event->pos())/cellSize).toPoint()); else { QList list = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::AscendingOrder); ADItem * al = 0; foreach(QGraphicsItem* x, list) { ADItem * i = qgraphicsitem_cast(x); if (i->itemType() == ADItem::Tower) { i->setSelected(true); emit tower_select(i->tid()); return; } if (i->itemType() == ADItem::Alien) { al = i; } } if (al != 0) { al->setSelected(true); emit alien_select(al->id()); return; } } } if (event->button() == Qt::RightButton) emit cancel(); } void AD_Graphics::timerEvent(QTimerEvent * ) { if (!core->isWaveEnd()) scene->update(); } void AD_Graphics::drawBackground(QPainter * p, const QRectF & ) { p->drawImage(0,0,*mapimg); } void AD_Graphics::drawForeground(QPainter *p, const QRectF &rect) { QList l = scene->items(); foreach(QGraphicsItem * g,l) { ADItem * i = qgraphicsitem_cast(g); if (i->isBarVisible()) { p->setPen(Qt::black); p->setBrush(QBrush(QColor(qRound(255*(1-i->value())),qRound(255*i->value()),0))); p->drawRect(i->pos().x()-cellSize,i->pos().y()-cellSize*1.25,cellSize*2*i->value(),cellSize/4); } } }