194 lines
6.0 KiB
C++
194 lines
6.0 KiB
C++
#include "ad_graphics.h"
|
|
#include <QGLWidget>
|
|
#include <QGraphicsItem>
|
|
|
|
|
|
|
|
AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(parent) , color_curve(QEasingCurve::InQuad)
|
|
{
|
|
focus_item = select_tow = select_al = 0;
|
|
data = adcore->addata;
|
|
core = adcore;
|
|
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
|
|
setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
|
setMouseTracking(true);
|
|
scene = new QGraphicsScene();
|
|
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
core->setScene(scene);
|
|
setScene(scene);
|
|
selection_al = new ADItem(-1, ADItem::Other, core->addata->alienSelect.images);
|
|
selection_al->hide();
|
|
selection_tow = new ADItem(-1, ADItem::Other, core->addata->towerSelect.images);
|
|
selection_tow->hide();
|
|
scene->addItem(selection_al);
|
|
scene->addItem(selection_tow);
|
|
//setCacheMode();
|
|
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
|
|
mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32);
|
|
//setDragMode(RubberBandDrag);
|
|
QPainter p(mapimg);
|
|
for (int i=0; i<data->map->cells().size(); i++) {
|
|
for (int j=0; j<data->map->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);
|
|
setRenderHints(QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
|
|
resize(mapimg->size());
|
|
startTimer(50);
|
|
}
|
|
|
|
|
|
AD_Graphics::~AD_Graphics()
|
|
{
|
|
delete selection_tow;
|
|
delete selection_al;
|
|
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<QGraphicsItem*> list = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::AscendingOrder);
|
|
ADItem * al = 0;
|
|
foreach(QGraphicsItem* x, list)
|
|
{
|
|
ADItem * i = qgraphicsitem_cast<ADItem*>(x);
|
|
if (i->itemType() == ADItem::Tower)
|
|
{
|
|
// i->setSelected(true);
|
|
select_tow = i;
|
|
emit selected_tower_changed(i->tid());
|
|
return;
|
|
}
|
|
if (i->itemType() == ADItem::Alien)
|
|
{
|
|
al = i;
|
|
}
|
|
}
|
|
if (al != 0)
|
|
{
|
|
// al->setSelected(true);
|
|
select_al = al;
|
|
core->addata->players.at(0)->selectAlienId = select_al->id();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (event->button() == Qt::RightButton)
|
|
{
|
|
select_al = 0;
|
|
select_tow = 0;
|
|
emit selected_tower_changed(QPoint());
|
|
emit cancel();
|
|
}
|
|
}
|
|
|
|
|
|
void AD_Graphics::timerEvent(QTimerEvent * )
|
|
{
|
|
if (select_al != 0)
|
|
{
|
|
selection_al->setPos(select_al->pos());
|
|
selection_al->show();
|
|
} else selection_al->hide();
|
|
if (select_tow != 0)
|
|
{
|
|
selection_tow->setPos(select_tow->pos());
|
|
selection_tow->show();
|
|
} else selection_tow->hide();
|
|
selection_tow->next();
|
|
selection_al->next();
|
|
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 <QGraphicsItem *> l = scene->items();
|
|
foreach(QGraphicsItem * g,l)
|
|
{
|
|
ADItem * i = qgraphicsitem_cast<ADItem *>(g);
|
|
if (i->isBarVisible())
|
|
{
|
|
p->setPen(Qt::black);
|
|
p->setBrush(Qt::NoBrush);
|
|
p->drawRect(i->pos().x()-cellSize-1,i->pos().y()-cellSize*1.25-1,cellSize*2+2,cellSize/4+2);
|
|
p->setPen(Qt::NoPen);
|
|
p->setBrush(QBrush(QColor::fromHsv(120*color_curve.valueForProgress(i->value()),230,230)));
|
|
p->drawRect(i->pos().x()-cellSize,i->pos().y()-cellSize*1.25,cellSize*2*i->value(),cellSize/4);
|
|
}
|
|
}
|
|
if (focus_item != 0)
|
|
{
|
|
if (focus_item->itemType() == ADItem::Tower)
|
|
{
|
|
float r = core->addata->curTowers[focus_item->tid()].src->radius*cellSize;
|
|
p->setPen(Qt::black);
|
|
p->setBrush(Qt::NoBrush);
|
|
p->drawEllipse(focus_item->pos(), r, r);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void AD_Graphics::mouseMoveEvent(QMouseEvent *event)
|
|
{
|
|
QList<QGraphicsItem * > li = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::DescendingOrder);
|
|
foreach (QGraphicsItem * i, li) {
|
|
if (qgraphicsitem_cast<ADItem *>(i)->itemType() == ADItem::Tower) {
|
|
focus_item = qgraphicsitem_cast<ADItem *>(i);
|
|
// selection_tow->setPos(focus_item->pos());
|
|
// selection_tow->show();
|
|
return;
|
|
}
|
|
}
|
|
// selection_tow->hide();
|
|
focus_item = 0;
|
|
}
|
|
|
|
void AD_Graphics::alienKilled(int id)
|
|
{
|
|
if (select_al != 0) if (select_al->id() == id) select_al = 0;
|
|
QPainter p(mapimg);
|
|
p.translate((core->addata->aliens->curAliens[id].pos+QPointF(0.5,0.5))*cellSize);
|
|
p.drawImage(core->addata->bloodrect, *(core->addata->blood));
|
|
p.end();
|
|
resetCachedContent();
|
|
}
|
|
|