4
1
mirror of https://github.com/QuasarApp/Snake.git synced 2025-05-06 06:29:47 +00:00

fix: guiobject

This commit is contained in:
usermeme 2018-11-14 23:15:36 +03:00
parent 57a305553b
commit a4b46aa914
15 changed files with 231 additions and 13 deletions

@ -9,7 +9,6 @@ Controller::Controller() {
timer = new QTimer();
timer->setInterval(33);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
startTimer();
}
void Controller::setDeviceSize(QPoint deviceSize) {
@ -53,12 +52,16 @@ void Controller::generateDiff(const QMap<int, GuiObject *>& objs) {
void Controller::update() {
world.render();
if(world.isDefiat()) {
stopTimer();
}
}
void Controller::newGame() {
WorldRules newGameRules = lvls.first();
lvl = 0;
generateDiff(world.init(newGameRules));
startTimer();
}
QObject *Controller::getGameObject(int id) {

@ -36,6 +36,18 @@ int GuiObject::guiId() const {
return m_guiId;
}
QString GuiObject::color() const {
return m_color;
}
void GuiObject::setColor(QString color) {
if (m_color == color)
return;
m_color = color;
emit colorChanged(m_color);
}
void GuiObject::setRect(QRectF rect) {
if (m_rect == rect)
return;

@ -9,7 +9,8 @@
class GuiObject:public QObject, public BaseClass
{
Q_OBJECT
// @todo: add color
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(double angle READ angle NOTIFY angleChanged)
Q_PROPERTY(QString texture READ texture NOTIFY textureChanged)
Q_PROPERTY(QRectF rect READ rect WRITE setRect NOTIFY rectChanged)
@ -18,10 +19,13 @@ class GuiObject:public QObject, public BaseClass
private:
void generateId();
protected:
int m_guiId;
double m_angle;
QString m_texture;
QString m_color;
QRectF m_rect;
void setTexture(const QString &texture);
@ -41,11 +45,16 @@ public:
void setAngle(double angle);
int guiId() const;
QString color() const;
void setColor(QString color);
signals:
void angleChanged(double angle);
void textureChanged(QString texture);
void rectChanged(QRectF rect);
void guiIdChanged(int guiId);
void colorChanged(QString color);
};
#endif // GUIOBJECT_H

@ -1,5 +1,6 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "controller.h"
#include "diff.h"
@ -11,13 +12,21 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine;
qmlRegisterType <Controller> ("Controller", 1, 0,"Controller");
//qmlRegisterType <Controller> ("Controller", 1, 0,"Controller");
qmlRegisterType <GuiObject> ();
qmlRegisterType <Diff> ();
engine.load(QUrl(QStringLiteral("qrc:/front-end/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
auto root = engine.rootContext();
if (!root)
return -1;
Controller contr;
root->setContextProperty("contr", &contr);
contr.newGame();
return app.exec();
}

@ -13,7 +13,7 @@ const QVector<Head *> &Snake::getItems() const {
}
void Snake::render() {
for (int i = items.length(); i >= 0; --i) {
for (int i = items.length() - 1; i >= 0; --i) {
if(i == 0){
if(isClick){
if(countClick & 1){

@ -89,7 +89,7 @@ void World::render() {
snake.render();
const QRectF &rig = snake.getRiger();
for (int i = items.length(); i >= 0; --i) {
for (int i = items.length() - 1; i >= 0; --i) {
defiat |= items[i]->move(rig, dx);
items[i]->render();
}

22
front-end/GraphicItem.qml Normal file

@ -0,0 +1,22 @@
import QtQuick 2.9
Rectangle {
id: graphicItem
property var model: null
property real angle: (model) ? model.angle : 0;
property string texture: (model) ? model.texture : "";
property rect rec: (model) ? model.rect : Qt.rect(0, 0, 0, 0);
property int guiId: (model) ? model.color : -1;
color: (model) ? model.color : "#11ff32";
width: rec.width * mainWindow.point;
height: rec.height * mainWindow.point;
x: rec.x * mainWindow.point;
y: rec.y * mainWindow.point;
transform: Rotation {
origin.x: 0;
origin.y: 0;
angle: graphicItem.angle;
}
}

8
front-end/Image.qml Normal file

@ -0,0 +1,8 @@
import QtQuick 2.9
import QtQuick.Controls.Material 2.0
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
Image {
id: image
}

72
front-end/MainMenu.qml Normal file

@ -0,0 +1,72 @@
import QtQuick 2.9
import QtQuick.Controls.Material 2.0
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
Item {
id: item1
visible: true
ColumnLayout {
id: columnLayout
width: 642
anchors.topMargin: 0
transformOrigin: Item.Center
spacing: -100
anchors.fill: parent
Button {
id: play
text: qsTr("Play game")
font.pixelSize: 20
spacing: 4
focusPolicy: Qt.StrongFocus
display: AbstractButton.TextBesideIcon
Layout.preferredHeight: 75
Layout.preferredWidth: 500
anchors.horizontalCenter: parent.horizontalCenter
}
Button {
id: level
text: qsTr("Select level")
font.pixelSize: 20
Layout.preferredWidth: 500
Layout.preferredHeight: 75
spacing: 2
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
Button {
id: exit
text: qsTr("Exit")
font.pixelSize: 20
Layout.preferredHeight: 75
Layout.preferredWidth: 500
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
Qt.quit();
}
}
}
RoundButton {
id: roundButton
x: 569
y: 20
height: 20
anchors.right: parent.right
anchors.rightMargin: 23
anchors.top: parent.top
anchors.topMargin: 20
}
}
/*##^## Designer {
D{i:0;autoSize:true;height:480;width:640}D{i:4;anchors_height:100;anchors_width:100;anchors_x:284;anchors_y:167}
D{i:11;anchors_y:31}
}
##^##*/

61
front-end/Scene.qml Normal file

@ -0,0 +1,61 @@
import QtQuick 2.9
Item {
id: scene;
property var model: contr;
property var arrayObjects: []
function add (cppObjId) {
if (!model) {
console.log("create object fail")
return;
}
var temp = Qt.createComponent("GraphicItem.qml")
if (temp.status === Component.Ready) {
var obj = temp.createObject(parent) // parent - это обьект на который будет помещен соззданный элемент
obj.model = model.getGameObject(cppObjId);
if (!obj.model) {
console.log("object model not found");
return;
}
arrayObjects.push(obj)
}
}
// todo: array object delete;
function remove(id) {
if (typeof id !== "number" || id < 0) {
console.log("id not found");
return;
}
for (var i = 0; i < arrayObjects.length; ++i) {
if (id === arrayObjects[i].guiId) {
arrayObjects[i].guiId = null;
}
}
}
Connections {
target: model;
onGameObjectsChanged: {
if (!dif) {
console.log("dif not found");
return;
}
var tempDifRem = [];
tempDifRem = dif.getRemoveIds();
var tempDifAdd = [];
tempDifAdd = dif.getAddedIds();
for (var i = 0; i < tempDifAdd.length; ++i) {
add(tempDifAdd[i]);
}
for (i = 0; i < tempDifRem.length; ++i) {
remove(tempDifRem[i]);
}
}
}
}

@ -1,19 +1,22 @@
import QtQuick 2.9
import QtQuick.Window 2.2
import Controller 1.0
import QtQuick.Controls 2.2
Window {
visible: true
width: 1140
height: 540
title: qsTr("SnakeOnTheRoad")
ApplicationWindow {
id: mainWindow;
visible: true;
width: 1140;
height: 540;
title: qsTr("SnakeOnTheRoad");
readonly property int pointCount: 100;
property real point: (width < height) ? width/pointCount : height/pointCount;
onPointChanged: {
console.log(point);
Scene {
anchors.fill: parent;
}
}

BIN
img/Settings-icon.png Normal file

Binary file not shown.

After

(image error) Size: 28 KiB

1
img/cogs-solid.svg Normal file

@ -0,0 +1 @@
<svg aria-hidden="true" data-prefix="fas" data-icon="cogs" class="svg-inline--fa fa-cogs fa-w-20" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path fill="currentColor" d="M512.1 191l-8.2 14.3c-3 5.3-9.4 7.5-15.1 5.4-11.8-4.4-22.6-10.7-32.1-18.6-4.6-3.8-5.8-10.5-2.8-15.7l8.2-14.3c-6.9-8-12.3-17.3-15.9-27.4h-16.5c-6 0-11.2-4.3-12.2-10.3-2-12-2.1-24.6 0-37.1 1-6 6.2-10.4 12.2-10.4h16.5c3.6-10.1 9-19.4 15.9-27.4l-8.2-14.3c-3-5.2-1.9-11.9 2.8-15.7 9.5-7.9 20.4-14.2 32.1-18.6 5.7-2.1 12.1.1 15.1 5.4l8.2 14.3c10.5-1.9 21.2-1.9 31.7 0L552 6.3c3-5.3 9.4-7.5 15.1-5.4 11.8 4.4 22.6 10.7 32.1 18.6 4.6 3.8 5.8 10.5 2.8 15.7l-8.2 14.3c6.9 8 12.3 17.3 15.9 27.4h16.5c6 0 11.2 4.3 12.2 10.3 2 12 2.1 24.6 0 37.1-1 6-6.2 10.4-12.2 10.4h-16.5c-3.6 10.1-9 19.4-15.9 27.4l8.2 14.3c3 5.2 1.9 11.9-2.8 15.7-9.5 7.9-20.4 14.2-32.1 18.6-5.7 2.1-12.1-.1-15.1-5.4l-8.2-14.3c-10.4 1.9-21.2 1.9-31.7 0zm-10.5-58.8c38.5 29.6 82.4-14.3 52.8-52.8-38.5-29.7-82.4 14.3-52.8 52.8zM386.3 286.1l33.7 16.8c10.1 5.8 14.5 18.1 10.5 29.1-8.9 24.2-26.4 46.4-42.6 65.8-7.4 8.9-20.2 11.1-30.3 5.3l-29.1-16.8c-16 13.7-34.6 24.6-54.9 31.7v33.6c0 11.6-8.3 21.6-19.7 23.6-24.6 4.2-50.4 4.4-75.9 0-11.5-2-20-11.9-20-23.6V418c-20.3-7.2-38.9-18-54.9-31.7L74 403c-10 5.8-22.9 3.6-30.3-5.3-16.2-19.4-33.3-41.6-42.2-65.7-4-10.9.4-23.2 10.5-29.1l33.3-16.8c-3.9-20.9-3.9-42.4 0-63.4L12 205.8c-10.1-5.8-14.6-18.1-10.5-29 8.9-24.2 26-46.4 42.2-65.8 7.4-8.9 20.2-11.1 30.3-5.3l29.1 16.8c16-13.7 34.6-24.6 54.9-31.7V57.1c0-11.5 8.2-21.5 19.6-23.5 24.6-4.2 50.5-4.4 76-.1 11.5 2 20 11.9 20 23.6v33.6c20.3 7.2 38.9 18 54.9 31.7l29.1-16.8c10-5.8 22.9-3.6 30.3 5.3 16.2 19.4 33.2 41.6 42.1 65.8 4 10.9.1 23.2-10 29.1l-33.7 16.8c3.9 21 3.9 42.5 0 63.5zm-117.6 21.1c59.2-77-28.7-164.9-105.7-105.7-59.2 77 28.7 164.9 105.7 105.7zm243.4 182.7l-8.2 14.3c-3 5.3-9.4 7.5-15.1 5.4-11.8-4.4-22.6-10.7-32.1-18.6-4.6-3.8-5.8-10.5-2.8-15.7l8.2-14.3c-6.9-8-12.3-17.3-15.9-27.4h-16.5c-6 0-11.2-4.3-12.2-10.3-2-12-2.1-24.6 0-37.1 1-6 6.2-10.4 12.2-10.4h16.5c3.6-10.1 9-19.4 15.9-27.4l-8.2-14.3c-3-5.2-1.9-11.9 2.8-15.7 9.5-7.9 20.4-14.2 32.1-18.6 5.7-2.1 12.1.1 15.1 5.4l8.2 14.3c10.5-1.9 21.2-1.9 31.7 0l8.2-14.3c3-5.3 9.4-7.5 15.1-5.4 11.8 4.4 22.6 10.7 32.1 18.6 4.6 3.8 5.8 10.5 2.8 15.7l-8.2 14.3c6.9 8 12.3 17.3 15.9 27.4h16.5c6 0 11.2 4.3 12.2 10.3 2 12 2.1 24.6 0 37.1-1 6-6.2 10.4-12.2 10.4h-16.5c-3.6 10.1-9 19.4-15.9 27.4l8.2 14.3c3 5.2 1.9 11.9-2.8 15.7-9.5 7.9-20.4 14.2-32.1 18.6-5.7 2.1-12.1-.1-15.1-5.4l-8.2-14.3c-10.4 1.9-21.2 1.9-31.7 0zM501.6 431c38.5 29.6 82.4-14.3 52.8-52.8-38.5-29.6-82.4 14.3-52.8 52.8z"></path></svg>

After

(image error) Size: 2.5 KiB

@ -1,6 +1,11 @@
<RCC>
<qresource prefix="/">
<file>front-end/main.qml</file>
<file>front-end/GraphicItem.qml</file>
<file>front-end/MainMenu.qml</file>
<file>qtquickcontrols2.conf</file>
<file>front-end/Scene.qml</file>
<file>front-end/Image.qml</file>
</qresource>
<qresource prefix="/texture">
<file alias="box-texture">img/box-texture.png</file>

13
qtquickcontrols2.conf Normal file

@ -0,0 +1,13 @@
; This file can be edited to change the style of the application
; Read "Qt Quick Controls 2 Configuration File" for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-configuration.html
[Controls]
Style=Material
[Material]
Theme=Dark
;Accent=BlueGrey
;Primary=BlueGray
;Foreground=Brown
;Background=Grey