Фигуры: группировка (Shift+ЛКМ, Ctrl+G), линии связи группы

This commit is contained in:
DverkuOff 2026-11-25 08:40:32 +03:00
commit 8d3c7f7501

View file

@ -103,26 +103,82 @@ TFigure* TFigureManager::FindAt(int px, int py)
return NULL;
}
// поднять фигуру наверх, true - если порядок поменялся
bool TFigureManager::ToTop(TFigure *f)
// f и все фигуры из её группы
void TFigureManager::CollectGroup(TFigure *f, std::vector<TFigure*> &out)
{
if (list.empty() || list.back() == f)
return false;
out.clear();
for (unsigned i = 0; i < list.size(); i++)
{
if (list[i] == f)
{
list.erase(list.begin() + i);
list.push_back(f);
return true;
}
bool same = (list[i] == f) || (f->GetGroup() != 0 && list[i]->GetGroup() == f->GetGroup());
if (same)
out.push_back(list[i]);
}
}
// свободный номер для новой группы
int TFigureManager::FreeGroup()
{
int g = 1;
for (unsigned i = 0; i < list.size(); i++)
if (list[i]->GetGroup() >= g)
g = list[i]->GetGroup() + 1;
return g;
}
static bool InVector(std::vector<TFigure*> &v, TFigure *f)
{
for (unsigned i = 0; i < v.size(); i++)
if (v[i] == f)
return true;
return false;
}
// поднять фигуры наверх, true - если порядок поменялся
bool TFigureManager::ToTop(std::vector<TFigure*> &figs)
{
std::vector<TFigure*> others, upper;
for (unsigned i = 0; i < list.size(); i++)
{
if (InVector(figs, list[i]))
upper.push_back(list[i]);
else
others.push_back(list[i]);
}
for (unsigned i = 0; i < upper.size(); i++)
others.push_back(upper[i]);
if (others == list)
return false;
list = others;
return true;
}
// полиморфизм: Draw() вызывается не зная, какая именно фигура
void TFigureManager::PaintAll(TCanvas *c)
{
// сзади - еле заметные линии от центра группы к каждой её фигуре
for (int g = 1; g < FreeGroup(); g++)
{
int n = 0, sx = 0, sy = 0;
for (unsigned i = 0; i < list.size(); i++)
if (list[i]->GetGroup() == g)
{
n++;
sx += list[i]->GetX() + list[i]->GetW() / 2;
sy += list[i]->GetY() + list[i]->GetH() / 2;
}
if (n < 2)
continue;
c->Pen->Color = (TColor)RGB(205, 215, 230);
c->Pen->Width = 1;
c->Pen->Style = psDash;
c->Brush->Style = bsClear;
for (unsigned i = 0; i < list.size(); i++)
if (list[i]->GetGroup() == g)
{
c->MoveTo(sx / n, sy / n);
c->LineTo(list[i]->GetX() + list[i]->GetW() / 2, list[i]->GetY() + list[i]->GetH() / 2);
}
}
for (unsigned i = 0; i < list.size(); i++)
list[i]->Draw(c);
}
@ -132,7 +188,7 @@ void TFigureManager::Save(String fileName)
TStringList *sl = new TStringList();
try
{
sl->Add(L"# Фигуры: вид номер X Y ширина высота заливка обводка");
sl->Add(L"# Фигуры: вид номер X Y ширина высота заливка обводка [группа]");
sl->Add(FILE_HEADER);
for (unsigned i = 0; i < list.size(); i++)
{
@ -145,6 +201,8 @@ void TFigureManager::Save(String fileName)
s += " " + IntToStr(f->GetH());
s += " " + ColorToText(f->GetFill());
s += " " + ColorToText(f->GetLine());
if (f->GetGroup() > 0)
s += " " + IntToStr(f->GetGroup());
sl->Add(s);
}
sl->SaveToFile(fileName, TEncoding::UTF8);
@ -183,8 +241,14 @@ void TFigureManager::Load(String fileName)
continue;
}
words->DelimitedText = line;
if (words->Count != 8)
throw Exception(pos + L"должно быть 8 значений, а их " + IntToStr(words->Count));
if (words->Count < 8 || words->Count > 9)
throw Exception(pos + L"должно быть 8 значений (9 - если есть группа), а их " + IntToStr(words->Count));
int group = 0;
if (words->Count == 9)
{
if (!TryStrToInt(words->Strings[8], group) || group < 0)
throw Exception(pos + L"\"" + words->Strings[8] + L"\" — неверный номер группы");
}
int n[5]; // номер, X, Y, ширина, высота
for (int j = 0; j < 5; j++)
@ -210,6 +274,7 @@ void TFigureManager::Load(String fileName)
f->SetBounds(n[1], n[2], n[3], n[4]);
f->SetFill(fill);
f->SetLine(stroke);
f->SetGroup(group);
temp.push_back(f);
if (n[0] > maxNum)
maxNum = n[0];