Commit 98ca8bb3 authored by Alexander Martynov's avatar Alexander Martynov

Merge branch '21-new-add-ui-module' into 'master'

Resolve "add UI module"

Closes #21

See merge request !23
parents 372d3f4f 2d09c289
Pipeline #19050067 passed with stage
in 1 minute and 31 seconds
#app, lib, ...
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app
TARGET = game
DESTDIR = ./bin
OBJECTS_DIR = ./.obj
QT -= gui
QT -= core
CONFIG += c++11
QT += core gui
INCLUDEPATH += src
QMAKE_CXXFLAGS += -std=c++11
SOURCES += \
Game.cpp \
Cell.cpp \
main.cpp \
CowHouse.cpp \
Application.cpp \
CellTerminal.cpp \
CowHouseTerminal.cpp
./src/core/Game.cpp \
./src/core/Cell.cpp \
./src/main.cpp \
./src/core/CowHouse.cpp \
./src/core/Application.cpp \
./src/cli/CellTerminal.cpp \
./src/cli/CowHouseTerminal.cpp \
./src/qt/GameWindow.cpp \
./src/qt/CellWindow.cpp \
./src/qt/CowHouseWindow.cpp \
HEADERS += \
Cell.h \
Game.h \
CowHouse.h \
CellsVector.h \
Application.h \
ActionsMainMenu.h \
Actions.h \
CellTerminal.h \
CowHouseTerminal.h
./src/core/Cell.h \
./src/core/Game.h \
./src/core/CowHouse.h \
./src/core/CellsVector.h \
./src/core/Application.h \
./src/core/ActionsMainMenu.h \
./src/core/Actions.h \
./src/cli/CellTerminal.h \
./src/cli/CowHouseTerminal.h \
./src/qt/GameWindow.h \
./src/qt/CellWindow.h \
./src/qt/CowHouseWindow.h \
RESOURCES += \
src/pictures.qrc
#include "Application.h"
int main(int argc, char** argv)
{
Application app;
app.run();
return 0;
}
#ifndef CELLTERMINAL_H
#define CELLTERMINAL_H
#include "Cell.h"
#include "./src/core/Cell.h"
class CellTerminal: public Cell
{
......
#ifndef COWHOUSETERMINAL_H
#define COWHOUSETERMINAL_H
#include "CowHouse.h"
#include "./src/core/CowHouse.h"
class CowHouseTerminal: public CowHouse
{
......
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Application.h"
#include "ActionsMainMenu.h"
#define CELL_COUNT 3
#define BUFFER_MAX 1024
#define ERROR -1
Application::Application():
......
#ifndef APPLICATION_H
#define APPLICATION_H
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Game.h"
#include "ActionsMainMenu.h"
#define CELL_COUNT 3
#define BUFFER_MAX 1024
class Application
{
......
......@@ -5,6 +5,7 @@ class Cell {
public:
Cell();
Cell(bool chickenExist, int seedsCount, bool eggExist);
Cell(const Cell& temp) = default;
int seedsCount() const;
void addSeeds();
bool isEggExist() const;
......
......@@ -18,7 +18,6 @@ public:
~CellsVector(){
delete[] this->m_cells;
this->m_cells = nullptr;
}
CellsVector(const CellsVector &obj) {
......@@ -45,9 +44,8 @@ public:
return *this;
}
X& operator[](int ID) const
{
return this->m_cells[ID];
X *cell(int cellID) const{
return (&this->m_cells[cellID]);
}
int size() const{
......@@ -65,7 +63,6 @@ public:
}
delete[] currentCells;
currentCells = nullptr;
this->m_cells = tempCells;
}
......
......@@ -6,6 +6,7 @@ public:
CowHouse();
CowHouse(bool cowExist, int hayStacksCount, bool milkExist);
CowHouse(bool cowExist, int hayStacksCount, bool milkExist, int daysNotMilked);
CowHouse(const CowHouse& tmp) = default;
int hayStacksCount() const;
void addHayStack();
bool isMilkExist() const;
......
......@@ -88,7 +88,7 @@ void Game::showInfo() const
for(int cellID = 0; cellID < this->m_cellsVector.size(); ++cellID)
{
this->m_cellsVector[cellID].displayState(cellID);
this->m_cellsVector.cell(cellID)->displayState(cellID);
}
int cowHouseID = this->m_cellsVector.size();
this->m_cowHouse.displayState(cowHouseID);
......@@ -110,14 +110,14 @@ void Game::gameActions(bool &shouldExit, int choice)
{
int selectedItem = getNumberSelectedItem();
std::cout << "You select ADD_SEED to " << selectedItem << " cell\n";
this->m_cellsVector[selectedItem].addSeeds();
this->m_cellsVector.cell(selectedItem)->addSeeds();
break;
}
case GRAB_EGG:
{
int selectedItem = getNumberSelectedItem();
bool isGrabbedEgg = false;
isGrabbedEgg = this->m_cellsVector[selectedItem].grabEgg();
isGrabbedEgg = this->m_cellsVector.cell(selectedItem)->grabEgg();
if(isGrabbedEgg)
{
std::cout << "You grab egg from " << selectedItem << " cell\n";
......@@ -176,7 +176,7 @@ bool Game::checkGameOver() const
for(int cellID = 0; cellID < this->m_cellsVector.size(); ++cellID)
{
if(this->m_cellsVector[cellID].isChickenExist())
if(this->m_cellsVector.cell(cellID)->isChickenExist())
{
isGameOver = false;
}
......@@ -209,8 +209,8 @@ void Game::actionsAboveCell()
{
for(int cellID = 0; cellID < this->m_cellsVector.size(); ++cellID)
{
this->m_cellsVector[cellID].tick();
this->m_cellsVector[cellID].displayActions(cellID);
this->m_cellsVector.cell(cellID)->tick();
this->m_cellsVector.cell(cellID)->displayActions(cellID);
}
}
......
#ifndef GAME_H
#define GAME_H
#include "CellTerminal.h"
#include "cli/CellTerminal.h"
#include "CellsVector.h"
#include "CowHouseTerminal.h"
#include "cli/CowHouseTerminal.h"
class Game {
public:
......
src/cow.png

7.81 KB

src/egg.png

5.76 KB

#include <QApplication>
#include "qt/GameWindow.h"
#include "core/Application.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
GameWindow game;
game.show();
int result = app.exec();
return result;
}
<RCC>
<qresource prefix="/images">
<file>cancel.ico</file>
<file>chicken.png</file>
<file>dead.png</file>
<file>egg.png</file>
<file>noegg.png</file>
<file>seeds.png</file>
<file>milk.png</file>
<file>cow.png</file>
<file>nomilk.png</file>
</qresource>
</RCC>
#include "CellWindow.h"
#include <QVBoxLayout>
#include <QPainter>
#include <QPaintEvent>
#include <QImage>
#include <QFont>
#define MINIMUM_HEIGHT 150
#define MINIMUM_WIDHT 100
#define IMAGE_WIDTH 45
#define IMAGE_HEIGHT 45
#define SIZE_FONT 10
#define START_X 10
#define START_Y 10
#define INCREMENT_Y 10
#define INCREMENT_BEFORE_LABEL 60
CellWindow::CellWindow(Cell chickenCell, QWidget *parent) :
QPushButton(parent),
m_chickenCell(chickenCell),
m_chickenState(":/images/chicken.png"),
m_eggState(":/images/egg.png")
{
QVBoxLayout* cellLayout = new QVBoxLayout;
this->setMinimumSize(QSize(MINIMUM_WIDHT, MINIMUM_HEIGHT));
this->setLayout(cellLayout);
repaint();
}
void CellWindow::tryToGrabEgg()
{
bool isGrabbed = this->m_chickenCell.isEggExist();
if(isGrabbed)
{
this->m_chickenCell.grabEgg();
emit eggGrabbed();
}
repaint();
}
void CellWindow::AddSeeds()
{
this->m_chickenCell.addSeeds();
repaint();
}
void CellWindow::onTick()
{
this->m_chickenCell.tick();
repaint();
}
void CellWindow::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
QFont titleFont("Arial", SIZE_FONT, QFont::Bold);
QSize sizeImages(IMAGE_WIDTH, IMAGE_HEIGHT);
QPoint positionItems(START_X, START_Y);
painter.setFont(titleFont);
QImage chickenState = m_chickenState.scaled(sizeImages, Qt::KeepAspectRatio);
positionItems.ry() += INCREMENT_Y;
if(m_chickenCell.isChickenExist())
{
painter.drawImage(positionItems, chickenState);
}
else
{
m_chickenState.load(":/images/dead.png");
chickenState = m_chickenState.scaled(sizeImages, Qt::KeepAspectRatio);
painter.drawImage(positionItems, chickenState);
}
positionItems.ry() += INCREMENT_BEFORE_LABEL;
painter.drawText(positionItems, QString("Seeds: %1").arg(this->m_chickenCell.seedsCount()));
positionItems.ry() += INCREMENT_Y;
if(m_chickenCell.isEggExist())
{
m_eggState.load(":/images/egg.png");
QImage eggState = m_eggState.scaled(sizeImages, Qt::AspectRatioMode::KeepAspectRatio);
painter.drawImage(positionItems, eggState);
}
else
{
m_eggState.load(":/images/noegg.png");
QImage eggState = m_eggState.scaled(sizeImages, Qt::AspectRatioMode::KeepAspectRatio);
painter.drawImage(positionItems, eggState);
}
}
#ifndef CELLWINDOW_H
#define CELLWINDOW_H
#include <QPushButton>
#include "core/Cell.h"
class QImage;
class CellWindow : public QPushButton
{
Q_OBJECT
public:
explicit CellWindow(Cell chickenCell, QWidget *parent = 0);
signals:
void eggGrabbed();
public slots:
void tryToGrabEgg();
void AddSeeds();
void onTick();
protected:
virtual void paintEvent(QPaintEvent *event);
private:
Cell m_chickenCell;
QImage m_chickenState;
QImage m_eggState;
};
#endif // CELLWINDOW_H
#include "CowHouseWindow.h"
#include <QVBoxLayout>
#include <QPainter>
#include <QPoint>
#include <QFont>
#include <QSize>
#define MINIMUM_HEIGHT 180
#define MINIMUM_WIDTH 100
#define IMAGE_WIDTH 45
#define IMAGE_HEIGHT 45
#define SIZE_FONT 10
#define START_X 10
#define START_Y 10
#define INCREMENT_Y 10
#define INCREMENT_BEFORE_LABEL 60
#define INCREMENT_BEETWEEN_LABEL 20
CowHouseWindow::CowHouseWindow(CowHouse cowHouse, QPushButton *parent) :
QPushButton(parent),
m_cowHouse(cowHouse),
m_cowState(":/images/cow.png"),
m_milkState(":/images/nomilk.png")
{
QVBoxLayout*cowLayout = new QVBoxLayout;
this->setMinimumSize(QSize(MINIMUM_WIDTH, MINIMUM_HEIGHT));
this->setLayout(cowLayout);
repaint();
}
void CowHouseWindow::tryToGrabMilk()
{
bool isMilked = m_cowHouse.isMilkExist();
if(isMilked)
{
this->m_cowHouse.grabMilk();
emit cowMilked();
}
repaint();
}
void CowHouseWindow::addHayStack()
{
m_cowHouse.addHayStack();
repaint();
}
void CowHouseWindow::onTick()
{
m_cowHouse.tick();
repaint();
}
void CowHouseWindow::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
QFont titleFont("Arial", SIZE_FONT, QFont::Bold);
QSize sizeImage(IMAGE_WIDTH, IMAGE_HEIGHT);
QPoint positionItems(START_X, START_Y);
painter.setFont(titleFont);
painter.drawText(positionItems, QString("CowHouse"));
positionItems.ry() += INCREMENT_Y;
if(m_cowHouse.isCowExist())
{
QImage cowState = m_cowState.scaled(sizeImage, Qt::KeepAspectRatio);
painter.drawImage(positionItems, cowState);
}
else
{
m_cowState.load(":/images/dead.png");
QImage cowState = m_cowState.scaled(sizeImage, Qt::KeepAspectRatio);
painter.drawImage(positionItems, cowState);
}
positionItems.ry() += INCREMENT_BEFORE_LABEL;
painter.drawText(positionItems, QString("HayStack: %1").arg(m_cowHouse.hayStacksCount()));
positionItems.ry() += INCREMENT_BEETWEEN_LABEL;
painter.drawText(positionItems, QString("no milked: %1").arg(m_cowHouse.getDaysNotMilked()));
positionItems.ry() += INCREMENT_Y;
if(m_cowHouse.isMilkExist())
{
m_milkState.load(":/images/milk.png");
QImage milkNotExist = m_milkState.scaled(sizeImage, Qt::KeepAspectRatio);
painter.drawImage(positionItems, milkNotExist);
}
else
{
m_milkState.load(":/images/nomilk.png");
QImage milkExist = m_milkState.scaled(sizeImage, Qt::KeepAspectRatio);
painter.drawImage(positionItems, milkExist);
}
}
#ifndef COWHOUSEWINDOW_H
#define COWHOUSEWINDOW_H
#include <QPushButton>
#include "core/CowHouse.h"
class QPaintEvent;
class QImage;
class CowHouseWindow : public QPushButton
{
Q_OBJECT
public:
explicit CowHouseWindow(CowHouse cowHouse, QPushButton *parent = 0);
signals:
void cowMilked();
public slots:
void tryToGrabMilk();
void addHayStack();
void onTick();
protected:
virtual void paintEvent(QPaintEvent *event);
private:
CowHouse m_cowHouse;
QImage m_cowState;
QImage m_milkState;
};
#endif // COWHOUSEWINDOW_H
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QSignalMapper>
#include <QTimer>
#include "core/Actions.h"
#include "GameWindow.h"
#include "CellWindow.h"
#include "CowHouseWindow.h"
#define CELLS_COUNT 3
#define DEFAULT_HEIGHT 200
#define DEFAULT_WIDTH 250
#define INTERVAL_TICK 5000
GameWindow::GameWindow(QWidget *parent): QWidget(parent),
m_cellWindows(new CellWindow*[CELLS_COUNT]),
m_cowWindow(new CowHouseWindow(CowHouse())),
m_eggs(0),
m_milk(0),
m_eggsCountInfo(new QLabel("no egg")),
m_milkCountInfo(new QLabel("no milk"))
{
this->setWindowTitle("FARM");
setMinimumHeight(DEFAULT_HEIGHT);
setMinimumWidth(DEFAULT_WIDTH);
QTimer* timerTick = new QTimer(this);
timerTick->setInterval(INTERVAL_TICK);
timerTick->setSingleShot(false);
connect(timerTick, &QTimer::timeout, this, &GameWindow::onTick);
timerTick->start();
QHBoxLayout* mainLayout = new QHBoxLayout;
this->setLayout(mainLayout);
QPushButton* feed = new QPushButton("&feed");
QPushButton* grab = new QPushButton("&grab");
QPushButton* addHay = new QPushButton("&addHay");
QPushButton* milk = new QPushButton("&milk");
QPushButton* rest = new QPushButton("&rest");
QPushButton* exit = new QPushButton("&EXIT");
QVBoxLayout* buttonsLayout = new QVBoxLayout;
buttonsLayout->addWidget(feed);
buttonsLayout->addWidget(grab);
buttonsLayout->addWidget(addHay);
buttonsLayout->addWidget(milk);
buttonsLayout->addWidget(rest);
buttonsLayout->addWidget(exit);
mainLayout->addLayout(buttonsLayout);
QSignalMapper* buttons = new QSignalMapper(this);
buttons->setMapping(feed, Actions::ADD_SEED);
buttons->setMapping(grab, Actions::GRAB_EGG);
buttons->setMapping(addHay, Actions::ADD_HAY);
buttons->setMapping(milk, Actions::GRAB_MILK);
connect(feed, SIGNAL(clicked(bool)), buttons, SLOT(map()));
connect(grab, SIGNAL(clicked(bool)), buttons, SLOT(map()));
connect(addHay, SIGNAL(clicked(bool)), buttons, SLOT(map()));
connect(milk, SIGNAL(clicked(bool)), buttons, SLOT(map()));
connect(buttons, SIGNAL(mapped(int)), this, SLOT(saveActions(int)));
connect(rest, &QPushButton::clicked, this, &GameWindow::rest);
connect(this, &GameWindow::rest, this, &GameWindow::onTick);
connect(exit, &QPushButton::clicked, this, &GameWindow::close);
QSignalMapper* cells = new QSignalMapper(this);
QHBoxLayout* cellsLayout = new QHBoxLayout;
for(int cellId = 0; cellId < CELLS_COUNT; ++cellId)
{
CellWindow *cellWindow = new CellWindow(Cell());
cellsLayout->addWidget(cellWindow);
m_cellWindows[cellId] = cellWindow;
cells->setMapping(cellWindow, cellId);
connect(cellWindow, &CellWindow::eggGrabbed, this, &GameWindow::onEggGrabbed);
connect(cellWindow, SIGNAL(clicked(bool)), cells, SLOT(map()));
}
connect(cells, SIGNAL(mapped(int)), this, SLOT(selectActionsCell(int)));
QHBoxLayout* cowLayout = new QHBoxLayout;
cowLayout->addWidget(m_cowWindow);
connect(m_cowWindow, &CowHouseWindow::cowMilked, this, &GameWindow::onCowMilked);
connect(m_cowWindow, &CowHouseWindow::clicked, this, &GameWindow::selectActionsCow);
QVBoxLayout* animalsLayout = new QVBoxLayout;
animalsLayout->addLayout(cellsLayout);
animalsLayout->addLayout(cowLayout);
mainLayout->addLayout(animalsLayout);
QVBoxLayout* grabbedLayout = new QVBoxLayout;
grabbedLayout->addWidget(m_eggsCountInfo);
grabbedLayout->addWidget(m_milkCountInfo);
mainLayout->addLayout(grabbedLayout);
}
GameWindow::~GameWindow()
{
delete[] m_cellWindows;
}
void GameWindow::onEggGrabbed()
{
m_eggs++;
updateGrabbedEggs();
}
void GameWindow::onCowMilked()
{
m_milk++;
updateGrabbedMilk();
}
void GameWindow::grabEggSelectedCell(int cellId)
{
m_cellWindows[cellId]->tryToGrabEgg();
}
void GameWindow::grabMilk()
{
m_cowWindow->tryToGrabMilk();
}
void GameWindow::feedCell(int cellId)
{
m_cellWindows[cellId]->AddSeeds();
onTick();
}
void GameWindow::feedCow()
{
m_cowWindow->addHayStack();
}
void GameWindow::updateGrabbedMilk()
{
m_milkCountInfo->setText(QString("milk: %1").arg(m_milk));
}
void GameWindow::saveActions(int action)
{
this->m_action = (Actions)action;
emit selectedActions(m_action);
}
void GameWindow::selectActionsCell(int cellId)
{
switch (m_action)
{
case Actions::ADD_SEED:
{
m_cellWindows[cellId]->AddSeeds();
break;
}
case Actions::GRAB_EGG:
m_cellWindows[cellId]->tryToGrabEgg();
break;
}
}
void GameWindow::selectActionsCow()
{
switch (m_action)
{
case Actions::ADD_HAY:
{
m_cowWindow->addHayStack();
break;
}
case Actions::GRAB_MILK:
{
m_cowWindow->tryToGrabMilk();
break;
}
}
}
void GameWindow::updateGrabbedEggs()
{
m_eggsCountInfo->setText(QString("Eggs: %1").arg(m_eggs));
}
void GameWindow::onTick()
{
for (int cellId = 0; cellId < CELLS_COUNT; ++cellId)
{
m_cellWindows[cellId]->onTick();
}