Skip to content

Commit

Permalink
Gloom: Splitting lines
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent d30fffe commit 2d39135
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
5 changes: 5 additions & 0 deletions doomsday/tests/test_gloom/gloom/geo/geomath.h
Expand Up @@ -138,6 +138,11 @@ struct Line
if (n.dot(d2) > 0) deg = 360 - deg;
return deg;
}

T nearestPoint(const T &p) const
{
return start + dir() * dir().dot(p - start);
}
};

using Line2d = Line<de::Vec2d>;
Expand Down
37 changes: 37 additions & 0 deletions doomsday/tests/test_gloom/gloom/world/map.cpp
Expand Up @@ -576,6 +576,43 @@ bool Map::buildSector(Edge startSide,
return true;
}

ID Map::splitLine(ID lineId, const Point &splitPoint)
{
const ID newPoint = append(points(), splitPoint);
const ID newLine = append(lines(), line(lineId));

for (auto s = d->sectors.begin(), end = d->sectors.end(); s != end; ++s)
{
Sector &sector = s.value();

for (int i = 0; i < sector.walls.size(); ++i)
{
if (sector.walls[i] == lineId)
{
sector.walls.insert(i + 1, newLine);

const int side = line(lineId).sectorSide(s.key());

// Find the corresponding corner points.
for (int j = 0; j < sector.points.size(); ++j)
{
if (line(lineId).points[side] == sector.points[j])
{
sector.points.insert(j + 1, newPoint);
break;
}
}
break;
}
}
}

line(lineId) .points[1] = newPoint;
line(newLine).points[0] = newPoint;

return newPoint;
}

std::pair<ID, ID> Map::findSectorAndVolumeAt(const de::Vec3d &pos) const
{
for (ID sectorId : d->sectors.keys())
Expand Down
4 changes: 4 additions & 0 deletions doomsday/tests/test_gloom/gloom/world/map.h
Expand Up @@ -32,6 +32,7 @@
namespace gloom {

typedef de::Vec2d Point;
class Map;

struct Line
{
Expand All @@ -46,6 +47,7 @@ struct Line
bool isSelfRef() const { return sectors[Front] == sectors[Back]; }
bool isOneSided() const { return !sectors[Front] || !sectors[Back]; }
bool isTwoSided() const { return sectors[Front] && sectors[Back]; }
Side sectorSide(ID sector) const { return sectors[Front] == sector? Front : Back; }
};

struct Plane
Expand All @@ -70,6 +72,7 @@ struct Sector
IDList volumes; // must be ascending and share planes; bottom plane of first volume is the
// sector floor, top plane of last volume is the sector ceiling
void replaceLine(ID oldId, ID newId);
ID splitLine(ID lineId, Map &map);
};

struct Edge
Expand Down Expand Up @@ -168,6 +171,7 @@ class Map
IDList & sectorPoints,
IDList & sectorWalls,
QList<Edge> &sectorEdges);
ID splitLine(ID lineId, const Point &splitPoint);

de::Block serialize() const;
void deserialize(const de::Block &data);
Expand Down
25 changes: 19 additions & 6 deletions doomsday/tests/test_gloom/src/editor.cpp
Expand Up @@ -782,6 +782,13 @@ DENG2_PIMPL(Editor)
}
}

void splitLine(ID line, const Vec2d &where)
{
pushUndo();
map.splitLine(line, map.geoLine(line).nearestPoint(where));
self().update();
}

void build()
{
emit self().buildMapRequested();
Expand Down Expand Up @@ -824,8 +831,8 @@ DENG2_PIMPL(Editor)
{
if (!askSaveFile()) return;

if (String openPath = QFileDialog::getOpenFileName(thisPublic, "Open File", filePath.fileNamePath(),
"Gloom Map (*.gloommap)"))
if (String openPath = QFileDialog::getOpenFileName(
thisPublic, "Open File", filePath.fileNamePath(), "Gloom Map (*.gloommap)"))
{
loadMap(openPath);
self().update();
Expand Down Expand Up @@ -929,6 +936,7 @@ void Editor::closeEvent(QCloseEvent *event)
event->ignore();
return;
}
d->isModified = false;
QSettings().setValue("editorGeometry", saveGeometry());
QWidget::closeEvent(event);
}
Expand Down Expand Up @@ -1182,9 +1190,9 @@ void Editor::mouseMoveEvent(QMouseEvent *event)
// Check what the mouse is hovering on.
{
const auto pos = d->viewToWorld(event->pos());
d->hoverPoint = d->findPointAt(pos);
d->hoverLine = d->findLineAt(pos);
d->hoverSector = (d->mode == Impl::EditSectors? d->findSectorAt(pos) : 0);
d->hoverPoint = d->findPointAt(pos);
d->hoverLine = d->findLineAt(pos);
d->hoverSector = (d->mode == Impl::EditSectors ? d->findSectorAt(pos) : 0);
d->hoverEntity = d->findEntityAt(pos);
}

Expand Down Expand Up @@ -1348,8 +1356,13 @@ void Editor::mouseReleaseEvent(QMouseEvent *event)
}
}

void Editor::mouseDoubleClickEvent(QMouseEvent *)
void Editor::mouseDoubleClickEvent(QMouseEvent *event)
{
event->accept();
if (d->hoverLine && d->mode == Impl::EditLines)
{
d->splitLine(d->hoverLine, d->viewToWorld(event->pos()));
}
}

void Editor::wheelEvent(QWheelEvent *event)
Expand Down

0 comments on commit 2d39135

Please sign in to comment.