Commit 2e922e27 authored by Alexander A. Maly's avatar Alexander A. Maly

Astyling IsometricRobot

parent 72a6dc3f
Pipeline #2518 failed with stages
in 2 minutes and 6 seconds
......@@ -4,41 +4,40 @@
#include "robotitem.h"
#include "util.h"
#define NO_WALL_OPACITY 0.5
#define WALL_OPACITY 0.8
namespace Robot25D {
CellGraphicsItem::CellGraphicsItem(const QPolygonF &poly,
const QPolygonF &polySouth,
const QPolygonF &polyEast,
const QPolygonF &polyNorth,
const QPolygonF &polyWest,
bool editable,
int cellX,
int cellY,
RobotView *view,
QGraphicsItem *parent)
: QGraphicsPolygonItem(poly, parent)
, p_south(polySouth)
, p_east(polyEast)
, p_north(polyNorth)
, p_west(polyWest)
, i_cellX(cellX)
, i_cellY(cellY)
, m_view(view)
namespace Robot25D
{
CellGraphicsItem::CellGraphicsItem(
const QPolygonF &poly,
const QPolygonF &polySouth,
const QPolygonF &polyEast,
const QPolygonF &polyNorth,
const QPolygonF &polyWest,
bool editable,
int cellX,
int cellY,
RobotView *view,
QGraphicsItem *parent
) :
QGraphicsPolygonItem(poly, parent),
p_south(polySouth),
p_east(polyEast),
p_north(polyNorth),
p_west(polyWest),
i_cellX(cellX),
i_cellY(cellY),
m_view(view)
{
}
void CellGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsPolygonItem::paint(painter, option, widget);
void CellGraphicsItem::paint(
QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget
) {
QGraphicsPolygonItem::paint(painter, option, widget);
}
}
} // namespace Robot25D
#ifndef CELLGRAPHICSITEM_H
#define CELLGRAPHICSITEM_H
#include <QtCore>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
#include <QGraphicsPolygonItem>
namespace Robot25D {
namespace Robot25D
{
class RobotView;
class CellGraphicsItem :
public QGraphicsPolygonItem
class CellGraphicsItem : public QGraphicsPolygonItem
{
public:
CellGraphicsItem(const QPolygonF &poly,
const QPolygonF &polySouth,
const QPolygonF &polyEast,
const QPolygonF &polyNorth,
const QPolygonF &polyWest,
bool editable,
int cellX,
int cellY,
RobotView *view,
QGraphicsItem *parent = 0);
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
int whichWall(const QPointF &p) const;
QPolygonF p_south;
QPolygonF p_east;
QPolygonF p_north;
QPolygonF p_west;
bool b_editable;
int i_cellX;
int i_cellY;
RobotView *m_view;
CellGraphicsItem(
const QPolygonF &poly,
const QPolygonF &polySouth,
const QPolygonF &polyEast,
const QPolygonF &polyNorth,
const QPolygonF &polyWest,
bool editable,
int cellX,
int cellY,
RobotView *view,
QGraphicsItem *parent = 0
);
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
//int whichWall(const QPointF &p) const;
QPolygonF p_south;
QPolygonF p_east;
QPolygonF p_north;
QPolygonF p_west;
bool b_editable;
int i_cellX;
int i_cellY;
RobotView *m_view;
};
}
} // namespace Robot25D
#endif // CELLGRAPHICSITEM_H
#ifndef FP_H
#define FP_H
#include <QtCore>
#include <QList>
template<typename T, typename R> extern inline QList<R> MAP( R (*function)(const T&), const QList<T> &iterable)
template<typename T, typename R> extern inline QList<R> MAP(R(*function)(const T &), const QList<T> &iterable)
{
QList<R> result;
for (int i=0; i<iterable.size(); i++) {
result << (*function)(iterable[i]);
}
return result;
QList<R> result;
for (int i = 0; i < iterable.size(); i++) {
result << (*function)(iterable[i]);
}
return result;
}
template<typename T> extern inline T REDUCE( T (*function)(const T&, const T&), const QList<T> &iterable)
template<typename T> extern inline T REDUCE(T(*function)(const T &, const T &), const QList<T> &iterable)
{
Q_ASSERT(!iterable.isEmpty());
T result = iterable.first();
for (int i=1; i<iterable.size(); i++) {
result = (*function)(result, iterable[i]);
}
return result;
Q_ASSERT(!iterable.isEmpty());
T result = iterable.first();
for (int i = 1; i < iterable.size(); i++) {
result = (*function)(result, iterable[i]);
}
return result;
}
template<typename T> extern inline QList<T> FILTER( bool (*function)(const T&), const QList<T> &iterable)
template<typename T> extern inline QList<T> FILTER(bool (*function)(const T &), const QList<T> &iterable)
{
QList<T> result;
for (int i=0; i<iterable.size(); i++) {
if ((*function)(iterable[i])) {
result << iterable[i];
}
}
return result;
QList<T> result;
for (int i = 0; i < iterable.size(); i++) {
if ((*function)(iterable[i])) {
result << iterable[i];
}
}
return result;
}
#endif // FP_H
#include "graphicsimageitem.h"
#include <QMutex>
#include <QPainter>
namespace Robot25D
{
namespace Robot25D {
GraphicsImageItem::GraphicsImageItem()
{
_mutex_image = new QMutex;
_mutex_image = new QMutex;
}
QRectF GraphicsImageItem::boundingRect() const
{
_mutex_image->lock();
QRectF result;
if (!_image.isNull())
result = QRectF(QPointF(0.0, 0.0), _image.size()).adjusted(-0.5, -0.5, 0.5, 0.5);
_mutex_image->unlock();
return result;
_mutex_image->lock();
QRectF result;
if (!_image.isNull()) {
result = QRectF(QPointF(0.0, 0.0), _image.size()).adjusted(-0.5, -0.5, 0.5, 0.5);
}
_mutex_image->unlock();
return result;
}
void GraphicsImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
_mutex_image->lock();
Q_UNUSED(option);
Q_UNUSED(widget);
_mutex_image->lock();
// painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
// bool antialiasing = scene()->views()[0]->renderHints().testFlag(QPainter::Antialiasing);
painter->drawImage(0,0,_image);
painter->drawImage(0, 0, _image);
// painter->setPen(QColor("blue"));
// painter->drawRect(0,0,m_image.width(),m_image.height());
_mutex_image->unlock();
_mutex_image->unlock();
}
QImage GraphicsImageItem::image() const
{
_mutex_image->lock();
QImage result = _image;
_mutex_image->unlock();
return result;
_mutex_image->lock();
QImage result = _image;
_mutex_image->unlock();
return result;
}
void GraphicsImageItem::setImage(const QImage &image)
{
_mutex_image->lock();
_image = image;
_mutex_image->unlock();
_mutex_image->lock();
_image = image;
_mutex_image->unlock();
// update();
}
GraphicsImageItem::~GraphicsImageItem()
{
delete _mutex_image;
delete _mutex_image;
}
}
} // namespace Robot25D
#ifndef GRAPHICSIMAGEITEM_H
#define GRAPHICSIMAGEITEM_H
#include <QtCore>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
#include <QGraphicsItem>
class QMutex;
namespace Robot25D {
namespace Robot25D
{
class GraphicsImageItem : public QGraphicsItem
{
public:
GraphicsImageItem();
QImage image() const;
void setImage(const QImage &image);
QRectF boundingRect() const;
~GraphicsImageItem();
GraphicsImageItem();
QImage image() const;
void setImage(const QImage &image);
QRectF boundingRect() const;
~GraphicsImageItem();
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
QImage _image;
QMutex *_mutex_image;
QImage _image;
QMutex *_mutex_image;
};
}
} // namespace Robot25D
#endif // GRAPHICSIMAGEITEM_H
......@@ -2,267 +2,263 @@
This file is generated, but you can safely change it
until you run "gen_actor_source.py" with "--project" flag.
Generated file is just a skeleton for module contents.
Generated file is just a skeleton for module contents.
You should change it corresponding to functionality.
*/
#include <QtCore>
#include <QtGui>
#include <iostream>
#include "isometricrobotmodule.h"
#include "sch_environment.h"
#include "sch_game.h"
#include "remotecontrol.h"
namespace ActorIsometricRobot {
#include <QScriptEngine>
namespace ActorIsometricRobot
{
IsometricRobotModule::IsometricRobotModule(ExtensionSystem::KPlugin * parent)
: IsometricRobotModuleBase(parent)
, _window(0)
, _robotView(0)
, _parentObject(parent)
, _remoteControl(0)
, _remoteControlWidget(0)
{
IsometricRobotModule::IsometricRobotModule(ExtensionSystem::KPlugin *parent)
: IsometricRobotModuleBase(parent)
, _window(0)
, _robotView(0)
, _parentObject(parent)
, _remoteControl(0)
, _remoteControlWidget(0)
{
}
void IsometricRobotModule::createGui()
{
_window = new Robot25DWindow(_model, _parentObject->myResourcesDir(), 0);
_remoteControlWidget = new QWidget(0);
QVBoxLayout* rcLayout = new QVBoxLayout;
_remoteControlWidget->setLayout(rcLayout);
const QString rcFileName = _parentObject->myResourcesDir().absoluteFilePath("robot25d-rc.svg");
_remoteControl = new SvgRemoteControl(_parentObject, this, rcFileName, _remoteControlWidget);
rcLayout->addWidget(_remoteControl);
_robotView = _window->robotView();
connect(m_actionRobot25DLoadEnvironment, SIGNAL(triggered()),
_window, SLOT(handleLoadAction()));
connect(m_actionRobot25DResetEnvironment, SIGNAL(triggered()),
this, SLOT(reset()));
_window = new Robot25DWindow(_model, _parentObject->myResourcesDir(), 0);
_remoteControlWidget = new QWidget(0);
QVBoxLayout *rcLayout = new QVBoxLayout;
_remoteControlWidget->setLayout(rcLayout);
const QString rcFileName = _parentObject->myResourcesDir().absoluteFilePath("robot25d-rc.svg");
_remoteControl = new SvgRemoteControl(_parentObject, this, rcFileName, _remoteControlWidget);
rcLayout->addWidget(_remoteControl);
_robotView = _window->robotView();
connect(m_actionRobot25DLoadEnvironment, SIGNAL(triggered()),
_window, SLOT(handleLoadAction()));
connect(m_actionRobot25DResetEnvironment, SIGNAL(triggered()),
this, SLOT(reset()));
}
QString IsometricRobotModule::loadEnvironmentFromFile(const QString &fileName)
{
QString message;
QFile f(fileName);
if (f.open(QIODevice::ReadOnly|QIODevice::Text)) {
QString script = "a = ";
script += QString::fromUtf8(f.readAll());
f.close();
Schema::Environment env;
QScriptEngine engine;
QScriptValue v = engine.evaluate(script);
if (Schema::parceJSON(v, env)) {
_model->loadEnvironment(env);
}
else {
message = QString::fromUtf8("Невозможно загрузить %1: это не обстановка Вертуна").arg(QFileInfo(fileName).fileName());
}
}
else {
message = QString::fromUtf8("Невозможно загрузить %1: файл не читается").arg(QFileInfo(fileName).fileName());
}
return message;
QString message;
QFile f(fileName);
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString script = "a = ";
script += QString::fromUtf8(f.readAll());
f.close();
Schema::Environment env;
QScriptEngine engine;
QScriptValue v = engine.evaluate(script);
if (Schema::parceJSON(v, env)) {
_model->loadEnvironment(env);
} else {
message = QString::fromUtf8("Невозможно загрузить %1: это не обстановка Вертуна").arg(QFileInfo(fileName).fileName());
}
} else {
message = QString::fromUtf8("Невозможно загрузить %1: файл не читается").arg(QFileInfo(fileName).fileName());
}
return message;
}
void IsometricRobotModule::loadDefaultEnvironment()
{
static const QString DefaultGameFileName = myResourcesDir().absoluteFilePath("default.pm.json");
QFile f(DefaultGameFileName);
if (f.open(QIODevice::ReadOnly|QIODevice::Text)) {
QString script = "a = ";
script += QString::fromUtf8(f.readAll());
f.close();
Schema::Game g;
QScriptEngine engine;
QScriptValue v = engine.evaluate(script);
if (Schema::parceJSON(v, g)) {
Schema::Environment env = g.tasks.first().environment;
_model->loadEnvironment(env);
}
}
static const QString DefaultGameFileName = myResourcesDir().absoluteFilePath("default.pm.json");
QFile f(DefaultGameFileName);
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString script = "a = ";
script += QString::fromUtf8(f.readAll());
f.close();
Schema::Game g;
QScriptEngine engine;
QScriptValue v = engine.evaluate(script);
if (Schema::parceJSON(v, g)) {
Schema::Environment env = g.tasks.first().environment;
_model->loadEnvironment(env);
}
}
}
QString IsometricRobotModule::initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &cmdLine)
{
if (!configurationParameters.contains("tablesOnly")) {
_model = new Robot25D::RobotModel;
if (!configurationParameters.contains("tablesOnly")) {
_model = new Robot25D::RobotModel;
bool hasGui=true;
bool hasGui = true;
#ifdef Q_OS_LINUX
hasGui = 0 != getenv("DISPLAY");
hasGui = 0 != getenv("DISPLAY");
#endif
if (hasGui) {
createGui();
}
else {
const QString envFileName = cmdLine.value('e').toString();
if (envFileName.isEmpty()) {
loadDefaultEnvironment();
}
else {
loadEnvironmentFromFile(envFileName);
}
}
}
return "";
if (hasGui) {
createGui();
} else {
const QString envFileName = cmdLine.value('e').toString();
if (envFileName.isEmpty()) {
loadDefaultEnvironment();
} else {
loadEnvironmentFromFile(envFileName);
}
}
}
return "";
}
QList<ExtensionSystem::CommandLineParameter> IsometricRobotModule::acceptableCommandLineParameters()
{
return QList<ExtensionSystem::CommandLineParameter>();
return QList<ExtensionSystem::CommandLineParameter>();
}
void IsometricRobotModule::reset()
{
_model->reset();
if (_robotView) {
bool wasAnimated = _robotView->isAnimated();
_robotView->reset();
_robotView->setAnimated(wasAnimated);
}
_model->reset();
if (_robotView) {
bool wasAnimated = _robotView->isAnimated();
_robotView->reset();
_robotView->setAnimated(wasAnimated);
}
}
void IsometricRobotModule::loadActorData(QIODevice *source)
{
QTextStream ts(source);
ts.setCodec("UTF-8");
ts.setAutoDetectUnicode(true);
const QString data = ts.readAll();
Schema::Environment env;
Schema::parceJSON(data, env);
_model->loadEnvironment(env);
QTextStream ts(source);
ts.setCodec("UTF-8");
ts.setAutoDetectUnicode(true);
const QString data = ts.readAll();
Schema::Environment env;
Schema::parceJSON(data, env);
_model->loadEnvironment(env);
}
void IsometricRobotModule::setAnimationEnabled(bool enabled)
{
if (_robotView) {
_robotView->setAnimated(false); // TODO improve me, then fix
}
if (_robotView) {
_robotView->setAnimated(false); // TODO improve me, then fix
}
}
QWidget* IsometricRobotModule::mainWidget() const
QWidget *IsometricRobotModule::mainWidget() const
{
return _window;
return _window;
}
void IsometricRobotModule::runGoForward()