Snake/src/Core/Crawl/controlpos.cpp

112 lines
2.3 KiB
C++
Raw Normal View History

//#
//# Copyright (C) 2021-2021 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "controlpos.h"
#include "clasteritem.h"
#include <QtMath>
namespace CRAWL {
ControlPos::ControlPos() {
}
void ControlPos::add(ClasterItem *object) {
Claster::add(object);
updatePosition();
}
void ControlPos::remove(ClasterItem *object) {
Claster::remove(object->guiId());
updatePosition();
}
void ControlPos::changeLayout(const Refresh &fig) {
_shape = fig;
updatePosition();
}
void ControlPos::setDistance(int dist) {
_distance = dist;
updatePosition();
}
void ControlPos::updatePosition() {
switch (_shape) {
case CIRCLE:
drawCircle();
break;
case SQUARE:
drawSquare();
break;
case LINE:
break;
default:
break;
}
}
void ControlPos::drawCircle() {
2021-09-09 22:37:05 +03:00
if (objects().size() == 0) {
QuasarAppUtils::Params::log(QString("The number of objects is zero. Add object.", QuasarAppUtils::Error));
2021-09-09 22:37:05 +03:00
return;
}
float step = 360 / objects().size();
2021-09-09 22:37:05 +03:00
int temp = 0;
for (ClasterItem* object: objects()) {
float x = _distance * qCos(step*temp);
float y = _distance * qSin(step*temp);
GroupObject::updatePosition(object->guiId(), {x, y, 0});
temp++;
}
}
void ControlPos::drawSquare() {
2021-09-09 22:37:05 +03:00
if (objects().size() == 0) {
QuasarAppUtils::Params::log(QString("The number of objects is zero. Add object."));
return;
}
2021-09-09 22:37:05 +03:00
int height = qFloor(qSqrt(objects().size()));
int width = qCeil(qSqrt(objects().size()));
2021-09-09 22:37:05 +03:00
int coutObj = 0;
for (int y = 0; y < height; y++) {
2021-09-09 22:37:05 +03:00
coutObj = coutObj+y;
for (int x = 0; x < width; x++) {
if (coutObj == objects().size()) {
return;
}
GroupObject::updatePosition(objects()[coutObj]->guiId(), {float(x + _distance),
float(y + _distance),
0});
coutObj++;
}
}
}
}