pagedworldspacewidget.cpp 28.4 KB
Newer Older
1 2
#include "pagedworldspacewidget.hpp"

3
#include <memory>
4 5
#include <sstream>

6
#include <QMouseEvent>
7
#include <QApplication>
8

9
#include <components/esm/loadland.hpp>
Marek Kochanowicz's avatar
Marek Kochanowicz committed
10

11 12
#include <components/misc/constants.hpp>

13 14
#include "../../model/prefs/shortcut.hpp"

15 16 17
#include "../../model/world/tablemimedata.hpp"
#include "../../model/world/idtable.hpp"

18
#include "../widget/scenetooltoggle2.hpp"
19
#include "../widget/scenetoolmode.hpp"
20
#include "../widget/scenetooltoggle2.hpp"
21

22
#include "editmode.hpp"
23
#include "mask.hpp"
24
#include "cameracontroller.hpp"
25
#include "cellarrow.hpp"
26
#include "terraintexturemode.hpp"
27

28 29 30 31
bool CSVRender::PagedWorldspaceWidget::adjustCells()
{
    bool modified = false;

32 33
    const CSMWorld::IdCollection<CSMWorld::Cell>& cells = mDocument.getData().getCells();

34
    {
35
        // remove/update
36 37 38 39
        std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());

        while (iter!=mCells.end())
        {
40
            if (!mSelection.has (iter->first))
41
            {
42
                // remove
Marc Zinnschlag's avatar
Marc Zinnschlag committed
43 44 45
                delete iter->second;
                mCells.erase (iter++);

46 47 48
                modified = true;
            }
            else
49
            {
50 51 52 53 54 55 56 57 58 59
                // update
                int index = cells.searchId (iter->first.getId (mWorldspace));

                bool deleted = index==-1 ||
                    cells.getRecord (index).mState==CSMWorld::RecordBase::State_Deleted;

                if (deleted!=iter->second->isDeleted())
                {
                    modified = true;

60
                    std::unique_ptr<Cell> cell (new Cell (mDocument.getData(), mRootNode,
61
                        iter->first.getId (mWorldspace), deleted));
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76
                    delete iter->second;
                    iter->second = cell.release();
                }
                else if (!deleted)
                {
                    // delete state has not changed -> just update

                    // TODO check if name or region field has changed (cell marker)
                    // FIXME: config setting
                    //std::string name = cells.getRecord(index).get().mName;
                    //std::string region = cells.getRecord(index).get().mRegion;

                    modified = true;
                }
77

78
                ++iter;
79
            }
80 81 82 83 84 85 86
        }
    }

    // add
    for (CSMWorld::CellSelection::Iterator iter (mSelection.begin()); iter!=mSelection.end();
        ++iter)
    {
87
        if (mCells.find (*iter)==mCells.end())
88
        {
89
            addCellToScene (*iter);
90 91 92 93
            modified = true;
        }
    }

94
    if (modified)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    {
        for (std::map<CSMWorld::CellCoordinates, Cell *>::const_iterator iter (mCells.begin());
            iter!=mCells.end(); ++iter)
        {
            int mask = 0;

            for (int i=CellArrow::Direction_North; i<=CellArrow::Direction_East; i *= 2)
            {
                CSMWorld::CellCoordinates coordinates (iter->second->getCoordinates());

                switch (i)
                {
                    case CellArrow::Direction_North: coordinates = coordinates.move (0, 1); break;
                    case CellArrow::Direction_West: coordinates = coordinates.move (-1, 0); break;
                    case CellArrow::Direction_South: coordinates = coordinates.move (0, -1); break;
                    case CellArrow::Direction_East: coordinates = coordinates.move (1, 0); break;
                }

                if (!mSelection.has (coordinates))
                    mask |= i;
            }

            iter->second->setCellArrows (mask);
        }
    }

121
    return modified;
122 123
}

124 125 126 127
void CSVRender::PagedWorldspaceWidget::addVisibilitySelectorButtons (
    CSVWidget::SceneToolToggle2 *tool)
{
    WorldspaceWidget::addVisibilitySelectorButtons (tool);
128 129
    tool->addButton (Button_Terrain, Mask_Terrain, "Terrain");
    tool->addButton (Button_Fog, Mask_Fog, "Fog", "", true);
130 131
}

132 133 134 135 136 137 138
void CSVRender::PagedWorldspaceWidget::addEditModeSelectorButtons (
    CSVWidget::SceneToolMode *tool)
{
    WorldspaceWidget::addEditModeSelectorButtons (tool);

    /// \todo replace EditMode with suitable subclasses
    tool->addButton (
139
        new EditMode (this, QIcon (":placeholder"), Mask_Reference, "Terrain shape editing"),
140 141
        "terrain-shape");
    tool->addButton (
142
        new TerrainTextureMode (this, tool),
143 144
        "terrain-texture");
    tool->addButton (
145
        new EditMode (this, QIcon (":placeholder"), Mask_Reference, "Terrain vertex paint editing"),
146 147
        "terrain-vertex");
    tool->addButton (
148
        new EditMode (this, QIcon (":placeholder"), Mask_Reference, "Terrain movement"),
149 150 151
        "terrain-move");
}

152
void CSVRender::PagedWorldspaceWidget::handleInteractionPress (const WorldspaceHitResult& hit, InteractionType type)
153
{
154
    if (hit.tag && hit.tag->getMask()==Mask_CellArrow)
155
    {
156
        if (CellArrowTag *cellArrowTag = dynamic_cast<CSVRender::CellArrowTag *> (hit.tag.get()))
157
        {
158
            CellArrow *arrow = cellArrowTag->getCellArrow();
159

160
            CSMWorld::CellCoordinates coordinates = arrow->getCoordinates();
161

162
            CellArrow::Direction direction = arrow->getDirection();
163

164 165
            int x = 0;
            int y = 0;
166

167 168 169 170 171 172 173
            switch (direction)
            {
                case CellArrow::Direction_North: y = 1; break;
                case CellArrow::Direction_West: x = -1; break;
                case CellArrow::Direction_South: y = -1; break;
                case CellArrow::Direction_East: x = 1; break;
            }
174

175
            bool modified = false;
176

177 178 179 180 181 182 183 184 185 186 187 188 189
            if (type == InteractionType_PrimarySelect)
            {
                addCellSelection (x, y);
                modified = true;
            }
            else if (type == InteractionType_SecondarySelect)
            {
                moveCellSelection (x, y);
                modified = true;
            }
            else // Primary/SecondaryEdit
            {
                CSMWorld::CellCoordinates newCoordinates = coordinates.move (x, y);
190

191 192 193 194
                if (mCells.find (newCoordinates)==mCells.end())
                {
                    addCellToScene (newCoordinates);
                    mSelection.add (newCoordinates);
195 196 197
                    modified = true;
                }

198 199 200
                if (type == InteractionType_SecondaryEdit)
                {
                    if (mCells.find (coordinates)!=mCells.end())
201
                    {
202 203
                        removeCellFromScene (coordinates);
                        mSelection.remove (coordinates);
204 205 206
                        modified = true;
                    }
                }
207
            }
208

209 210
            if (modified)
                adjustCells();
211

212
            return;
213 214 215
        }
    }

216
    WorldspaceWidget::handleInteractionPress (hit, type);
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
void CSVRender::PagedWorldspaceWidget::referenceableDataChanged (const QModelIndex& topLeft,
    const QModelIndex& bottomRight)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
        if (iter->second->referenceableDataChanged (topLeft, bottomRight))
            flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::referenceableAboutToBeRemoved (
    const QModelIndex& parent, int start, int end)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
        if (iter->second->referenceableAboutToBeRemoved (parent, start, end))
            flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::referenceableAdded (const QModelIndex& parent,
    int start, int end)
{
    CSMWorld::IdTable& referenceables = dynamic_cast<CSMWorld::IdTable&> (
        *mDocument.getData().getTableModel (CSMWorld::UniversalId::Type_Referenceables));

    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
    {
        QModelIndex topLeft = referenceables.index (start, 0);
        QModelIndex bottomRight =
            referenceables.index (end, referenceables.columnCount());

        if (iter->second->referenceableDataChanged (topLeft, bottomRight))
            flagAsModified();
    }
}

void CSVRender::PagedWorldspaceWidget::referenceDataChanged (const QModelIndex& topLeft,
    const QModelIndex& bottomRight)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
        if (iter->second->referenceDataChanged (topLeft, bottomRight))
            flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::referenceAboutToBeRemoved (const QModelIndex& parent,
    int start, int end)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
        if (iter->second->referenceAboutToBeRemoved (parent, start, end))
            flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::referenceAdded (const QModelIndex& parent, int start,
    int end)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
        if (iter->second->referenceAdded (parent, start, end))
            flagAsModified();
}

282 283 284 285
void CSVRender::PagedWorldspaceWidget::pathgridDataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
    const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mDocument.getData().getPathgrids();

286 287 288
    int rowStart = -1;
    int rowEnd = -1;

289 290
    if (topLeft.parent().isValid())
    {
291 292
        rowStart = topLeft.parent().row();
        rowEnd = bottomRight.parent().row();
293
    }
294
    else
295
    {
296 297 298
        rowStart = topLeft.row();
        rowEnd = bottomRight.row();
    }
299

300 301
    for (int row = rowStart; row <= rowEnd; ++row)
    {
302 303 304 305 306 307
        const CSMWorld::Pathgrid& pathgrid = pathgrids.getRecord(row).get();
        CSMWorld::CellCoordinates coords = CSMWorld::CellCoordinates(pathgrid.mData.mX, pathgrid.mData.mY);

        std::map<CSMWorld::CellCoordinates, Cell*>::iterator searchResult = mCells.find(coords);
        if (searchResult != mCells.end())
        {
308
            searchResult->second->pathgridModified();
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            flagAsModified();
        }
    }
}

void CSVRender::PagedWorldspaceWidget::pathgridAboutToBeRemoved (const QModelIndex& parent, int start, int end)
{
    const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mDocument.getData().getPathgrids();

    if (!parent.isValid())
    {
        // Pathgrid going to be deleted
        for (int row = start; row <= end; ++row)
        {
            const CSMWorld::Pathgrid& pathgrid = pathgrids.getRecord(row).get();
            CSMWorld::CellCoordinates coords = CSMWorld::CellCoordinates(pathgrid.mData.mX, pathgrid.mData.mY);

            std::map<CSMWorld::CellCoordinates, Cell*>::iterator searchResult = mCells.find(coords);
            if (searchResult != mCells.end())
            {
                searchResult->second->pathgridRemoved();
                flagAsModified();
            }
        }
    }
}

void CSVRender::PagedWorldspaceWidget::pathgridAdded(const QModelIndex& parent, int start, int end)
{
338
   const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mDocument.getData().getPathgrids();
339 340 341 342 343 344 345 346 347 348 349

    if (!parent.isValid())
    {
        for (int row = start; row <= end; ++row)
        {
            const CSMWorld::Pathgrid& pathgrid = pathgrids.getRecord(row).get();
            CSMWorld::CellCoordinates coords = CSMWorld::CellCoordinates(pathgrid.mData.mX, pathgrid.mData.mY);

            std::map<CSMWorld::CellCoordinates, Cell*>::iterator searchResult = mCells.find(coords);
            if (searchResult != mCells.end())
            {
350
                searchResult->second->pathgridModified();
351 352 353 354 355 356
                flagAsModified();
            }
        }
    }
}

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
void CSVRender::PagedWorldspaceWidget::landDataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
    for (int r = topLeft.row(); r <= bottomRight.row(); ++r)
    {
        std::string id = mDocument.getData().getLand().getId(r);

        auto cellIt = mCells.find(CSMWorld::CellCoordinates::fromId(id).first);
        if (cellIt != mCells.end())
        {
            cellIt->second->landDataChanged(topLeft, bottomRight);
            flagAsModified();
        }
    }
}

void CSVRender::PagedWorldspaceWidget::landAboutToBeRemoved (const QModelIndex& parent, int start, int end)
{
    for (int r = start; r <= end; ++r)
    {
        std::string id = mDocument.getData().getLand().getId(r);

        auto cellIt = mCells.find(CSMWorld::CellCoordinates::fromId(id).first);
        if (cellIt != mCells.end())
        {
            cellIt->second->landAboutToBeRemoved(parent, start, end);
            flagAsModified();
        }
    }
}

void CSVRender::PagedWorldspaceWidget::landAdded (const QModelIndex& parent, int start, int end)
{
    for (int r = start; r <= end; ++r)
    {
        std::string id = mDocument.getData().getLand().getId(r);

        auto cellIt = mCells.find(CSMWorld::CellCoordinates::fromId(id).first);
        if (cellIt != mCells.end())
        {
            cellIt->second->landAdded(parent, start, end);
            flagAsModified();
        }
    }
}

void CSVRender::PagedWorldspaceWidget::landTextureDataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
    for (auto cellIt : mCells)
        cellIt.second->landTextureChanged(topLeft, bottomRight);
406
    flagAsModified();
407 408 409 410 411 412
}

void CSVRender::PagedWorldspaceWidget::landTextureAboutToBeRemoved (const QModelIndex& parent, int start, int end)
{
    for (auto cellIt : mCells)
        cellIt.second->landTextureAboutToBeRemoved(parent, start, end);
413
    flagAsModified();
414 415 416 417 418 419
}

void CSVRender::PagedWorldspaceWidget::landTextureAdded (const QModelIndex& parent, int start, int end)
{
    for (auto cellIt : mCells)
        cellIt.second->landTextureAdded(parent, start, end);
420
    flagAsModified();
421 422
}

423

424 425
std::string CSVRender::PagedWorldspaceWidget::getStartupInstruction()
{
426 427 428 429
    osg::Vec3d eye, center, up;
    mView->getCamera()->getViewMatrixAsLookAt(eye, center, up);
    osg::Vec3d position = eye;

430 431 432 433
    std::ostringstream stream;

    stream
        << "player->position "
434
        << position.x() << ", " << position.y() << ", " << position.z()
435 436 437 438 439
        << ", 0";

    return stream.str();
}

440 441 442 443 444 445 446 447 448 449
void CSVRender::PagedWorldspaceWidget::addCellToScene (
    const CSMWorld::CellCoordinates& coordinates)
{
    const CSMWorld::IdCollection<CSMWorld::Cell>& cells = mDocument.getData().getCells();

    int index = cells.searchId (coordinates.getId (mWorldspace));

    bool deleted = index==-1 ||
        cells.getRecord (index).mState==CSMWorld::RecordBase::State_Deleted;

450
    std::unique_ptr<Cell> cell (
Marc Zinnschlag's avatar
Marc Zinnschlag committed
451 452 453 454
        new Cell (mDocument.getData(), mRootNode, coordinates.getId (mWorldspace),
        deleted));
    EditMode *editMode = getEditMode();
    cell->setSubMode (editMode->getSubMode(), editMode->getInteractionMask());
455

Marc Zinnschlag's avatar
Marc Zinnschlag committed
456
    mCells.insert (std::make_pair (coordinates, cell.release()));
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
}

void CSVRender::PagedWorldspaceWidget::removeCellFromScene (
    const CSMWorld::CellCoordinates& coordinates)
{
    std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.find (coordinates);

    if (iter!=mCells.end())
    {
        delete iter->second;
        mCells.erase (iter);
    }
}

void CSVRender::PagedWorldspaceWidget::addCellSelection (int x, int y)
{
    CSMWorld::CellSelection newSelection = mSelection;
    newSelection.move (x, y);

    for (CSMWorld::CellSelection::Iterator iter (newSelection.begin()); iter!=newSelection.end();
        ++iter)
    {
        if (mCells.find (*iter)==mCells.end())
        {
            addCellToScene (*iter);
            mSelection.add (*iter);
        }
    }
}

void CSVRender::PagedWorldspaceWidget::moveCellSelection (int x, int y)
{
    CSMWorld::CellSelection newSelection = mSelection;
    newSelection.move (x, y);

    for (CSMWorld::CellSelection::Iterator iter (mSelection.begin()); iter!=mSelection.end();
        ++iter)
    {
        if (!newSelection.has (*iter))
            removeCellFromScene (*iter);
    }

    for (CSMWorld::CellSelection::Iterator iter (newSelection.begin()); iter!=newSelection.end();
        ++iter)
    {
        if (!mSelection.has (*iter))
            addCellToScene (*iter);
    }

    mSelection = newSelection;
}

509 510 511 512 513
void CSVRender::PagedWorldspaceWidget::addCellToSceneFromCamera (int offsetX, int offsetY)
{
    osg::Vec3f eye, center, up;
    getCamera()->getViewMatrixAsLookAt(eye, center, up);

514 515
    int cellX = (int)std::floor(center.x() / Constants::CellSizeInUnits) + offsetX;
    int cellY = (int)std::floor(center.y() / Constants::CellSizeInUnits) + offsetY;
516 517 518

    CSMWorld::CellCoordinates cellCoordinates(cellX, cellY);

519
    if (!mSelection.has(cellCoordinates))
520 521
    {
        addCellToScene(cellCoordinates);
522 523 524
        mSelection.add(cellCoordinates);

        adjustCells();
525 526 527
    }
}

528
CSVRender::PagedWorldspaceWidget::PagedWorldspaceWidget (QWidget* parent, CSMDoc::Document& document)
cc9cii's avatar
cc9cii committed
529
: WorldspaceWidget (document, parent), mDocument (document), mWorldspace ("std::default"),
530
  mControlElements(NULL), mDisplayCellCoord(true)
531 532 533 534 535 536 537 538 539 540
{
    QAbstractItemModel *cells =
        document.getData().getTableModel (CSMWorld::UniversalId::Type_Cells);

    connect (cells, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (cellDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (cells, SIGNAL (rowsRemoved (const QModelIndex&, int, int)),
        this, SLOT (cellRemoved (const QModelIndex&, int, int)));
    connect (cells, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (cellAdded (const QModelIndex&, int, int)));
541

Kyle Cooley's avatar
Kyle Cooley committed
542 543 544
    connect (&document.getData(), SIGNAL (assetTablesChanged ()),
        this, SLOT (assetTablesChanged ()));

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    QAbstractItemModel *lands = document.getData().getTableModel (CSMWorld::UniversalId::Type_Lands);

    connect (lands, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (landDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (lands, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (landAboutToBeRemoved (const QModelIndex&, int, int)));
    connect (lands, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (landAdded (const QModelIndex&, int, int)));

    QAbstractItemModel *ltexs = document.getData().getTableModel (CSMWorld::UniversalId::Type_LandTextures);

    connect (ltexs, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (landTextureDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (ltexs, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (landTextureAboutToBeRemoved (const QModelIndex&, int, int)));
    connect (ltexs, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (landTextureAdded (const QModelIndex&, int, int)));

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    // Shortcuts
    CSMPrefs::Shortcut* loadCameraCellShortcut = new CSMPrefs::Shortcut("scene-load-cam-cell", this);
    connect(loadCameraCellShortcut, SIGNAL(activated()), this, SLOT(loadCameraCell()));

    CSMPrefs::Shortcut* loadCameraEastCellShortcut = new CSMPrefs::Shortcut("scene-load-cam-eastcell", this);
    connect(loadCameraEastCellShortcut, SIGNAL(activated()), this, SLOT(loadEastCell()));

    CSMPrefs::Shortcut* loadCameraNorthCellShortcut = new CSMPrefs::Shortcut("scene-load-cam-northcell", this);
    connect(loadCameraNorthCellShortcut, SIGNAL(activated()), this, SLOT(loadNorthCell()));

    CSMPrefs::Shortcut* loadCameraWestCellShortcut = new CSMPrefs::Shortcut("scene-load-cam-westcell", this);
    connect(loadCameraWestCellShortcut, SIGNAL(activated()), this, SLOT(loadWestCell()));

    CSMPrefs::Shortcut* loadCameraSouthCellShortcut = new CSMPrefs::Shortcut("scene-load-cam-southcell", this);
    connect(loadCameraSouthCellShortcut, SIGNAL(activated()), this, SLOT(loadSouthCell()));
578
}
579

580 581 582 583
CSVRender::PagedWorldspaceWidget::~PagedWorldspaceWidget()
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter (mCells.begin());
        iter!=mCells.end(); ++iter)
584 585 586
    {
        delete iter->second;
    }
587 588
}

589 590 591 592
void CSVRender::PagedWorldspaceWidget::useViewHint (const std::string& hint)
{
    if (!hint.empty())
    {
593 594
        CSMWorld::CellSelection selection;

595 596
        if (hint[0]=='c')
        {
597 598
            // syntax: c:#x1 y1; #x2 y2 (number of coordinate pairs can be 0 or larger)
            char ignore;
599 600

            std::istringstream stream (hint.c_str());
601
            if (stream >> ignore)
602
            {
603 604
                char ignore1; // : or ;
                char ignore2; // #
605
                // Current coordinate
606 607
                int x, y;

608
                // Loop through all the coordinates to add them to selection
609 610
                while (stream >> ignore1 >> ignore2 >> x >> y)
                    selection.add (CSMWorld::CellCoordinates (x, y));
Kyle Cooley's avatar
Kyle Cooley committed
611

LohikaarmeHav's avatar
LohikaarmeHav committed
612 613
                // Mark that camera needs setup
                mCamPositionSet=false;
614 615
            }
        }
616 617 618 619
        else if (hint[0]=='r')
        {
            /// \todo implement 'r' type hints
        }
620 621

        setCellSelection (selection);
622 623 624
    }
}

625
void CSVRender::PagedWorldspaceWidget::setCellSelection (const CSMWorld::CellSelection& selection)
626
{
627
    mSelection = selection;
628 629 630 631

    if (adjustCells())
        flagAsModified();

632
    emit cellSelectionChanged (mSelection);
Marek Kochanowicz's avatar
Marek Kochanowicz committed
633 634
}

635 636 637 638 639
const CSMWorld::CellSelection& CSVRender::PagedWorldspaceWidget::getCellSelection() const
{
    return mSelection;
}

640 641
std::pair< int, int > CSVRender::PagedWorldspaceWidget::getCoordinatesFromId (const std::string& record) const
{
Marek Kochanowicz's avatar
Marek Kochanowicz committed
642 643 644
    std::istringstream stream (record.c_str());
    char ignore;
    int x, y;
645
    stream >> ignore >> x >> y;
646 647 648
    return std::make_pair(x, y);
}

649
bool CSVRender::PagedWorldspaceWidget::handleDrop (
Allofich's avatar
Allofich committed
650
    const std::vector< CSMWorld::UniversalId >& universalIdData, DropType type)
651
{
Allofich's avatar
Allofich committed
652
    if (WorldspaceWidget::handleDrop (universalIdData, type))
653 654 655 656 657
        return true;

    if (type!=Type_CellsExterior)
        return false;

658
    bool selectionChanged = false;
Allofich's avatar
Allofich committed
659
    for (unsigned i = 0; i < universalIdData.size(); ++i)
660
    {
Allofich's avatar
Allofich committed
661
        std::pair<int, int> coordinates(getCoordinatesFromId(universalIdData[i].getId()));
662 663 664 665 666 667 668
        if (mSelection.add(CSMWorld::CellCoordinates(coordinates.first, coordinates.second)))
        {
            selectionChanged = true;
        }
    }
    if (selectionChanged)
    {
669 670 671
        if (adjustCells())
            flagAsModified();

672 673
        emit cellSelectionChanged(mSelection);
    }
674 675

    return true;
676
}
Marek Kochanowicz's avatar
Marek Kochanowicz committed
677

678
CSVRender::WorldspaceWidget::dropRequirments CSVRender::PagedWorldspaceWidget::getDropRequirements (CSVRender::WorldspaceWidget::DropType type) const
Marek Kochanowicz's avatar
Marek Kochanowicz committed
679
{
680 681 682 683 684
    dropRequirments requirements = WorldspaceWidget::getDropRequirements (type);

    if (requirements!=ignored)
        return requirements;

Marek Kochanowicz's avatar
Marek Kochanowicz committed
685 686
    switch (type)
    {
687
        case Type_CellsExterior:
Marek Kochanowicz's avatar
Marek Kochanowicz committed
688 689
            return canHandle;

690
        case Type_CellsInterior:
Marek Kochanowicz's avatar
Marek Kochanowicz committed
691 692 693 694 695
            return needUnpaged;

        default:
            return ignored;
    }
696 697
}

698
unsigned int CSVRender::PagedWorldspaceWidget::getVisibilityMask() const
699
{
700
    return WorldspaceWidget::getVisibilityMask() | mControlElements->getSelectionMask();
701 702
}

703 704 705 706 707
void CSVRender::PagedWorldspaceWidget::clearSelection (int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->setSelection (elementMask, Cell::Selection_Clear);
708 709

    flagAsModified();
710 711
}

712 713 714 715 716 717 718 719 720
void CSVRender::PagedWorldspaceWidget::invertSelection (int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->setSelection (elementMask, Cell::Selection_Invert);

    flagAsModified();
}

721 722 723 724 725 726 727 728 729
void CSVRender::PagedWorldspaceWidget::selectAll (int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->setSelection (elementMask, Cell::Selection_All);

    flagAsModified();
}

730 731 732 733 734 735 736 737 738
void CSVRender::PagedWorldspaceWidget::selectAllWithSameParentId (int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->selectAllWithSameParentId (elementMask);

    flagAsModified();
}

739 740 741
std::string CSVRender::PagedWorldspaceWidget::getCellId (const osg::Vec3f& point) const
{
    CSMWorld::CellCoordinates cellCoordinates (
742 743
        static_cast<int> (std::floor (point.x() / Constants::CellSizeInUnits)),
        static_cast<int> (std::floor (point.y() / Constants::CellSizeInUnits)));
744 745 746 747

    return cellCoordinates.getId (mWorldspace);
}

748 749 750
CSVRender::Cell* CSVRender::PagedWorldspaceWidget::getCell(const osg::Vec3d& point) const
{
    CSMWorld::CellCoordinates coords(
751 752
        static_cast<int> (std::floor (point.x() / Constants::CellSizeInUnits)),
        static_cast<int> (std::floor (point.y() / Constants::CellSizeInUnits)));
753 754 755 756 757 758 759 760

    std::map<CSMWorld::CellCoordinates, Cell*>::const_iterator searchResult = mCells.find(coords);
    if (searchResult != mCells.end())
        return searchResult->second;
    else
        return 0;
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
std::vector<osg::ref_ptr<CSVRender::TagBase> > CSVRender::PagedWorldspaceWidget::getSelection (
    unsigned int elementMask) const
{
    std::vector<osg::ref_ptr<CSVRender::TagBase> > result;

    for (std::map<CSMWorld::CellCoordinates, Cell *>::const_iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
    {
        std::vector<osg::ref_ptr<CSVRender::TagBase> > cellResult =
            iter->second->getSelection (elementMask);

        result.insert (result.end(), cellResult.begin(), cellResult.end());
    }

    return result;
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
std::vector<osg::ref_ptr<CSVRender::TagBase> > CSVRender::PagedWorldspaceWidget::getEdited (
    unsigned int elementMask) const
{
    std::vector<osg::ref_ptr<CSVRender::TagBase> > result;

    for (std::map<CSMWorld::CellCoordinates, Cell *>::const_iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
    {
        std::vector<osg::ref_ptr<CSVRender::TagBase> > cellResult =
            iter->second->getEdited (elementMask);

        result.insert (result.end(), cellResult.begin(), cellResult.end());
    }

    return result;
}

Marc Zinnschlag's avatar
Marc Zinnschlag committed
795 796 797 798 799 800 801
void CSVRender::PagedWorldspaceWidget::setSubMode (int subMode, unsigned int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::const_iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->setSubMode (subMode, elementMask);
}

802 803 804 805 806 807 808
void CSVRender::PagedWorldspaceWidget::reset (unsigned int elementMask)
{
    for (std::map<CSMWorld::CellCoordinates, Cell *>::const_iterator iter = mCells.begin();
        iter!=mCells.end(); ++iter)
        iter->second->reset (elementMask);
}

809
CSVWidget::SceneToolToggle2 *CSVRender::PagedWorldspaceWidget::makeControlVisibilitySelector (
810 811
    CSVWidget::SceneToolbar *parent)
{
812 813
    mControlElements = new CSVWidget::SceneToolToggle2 (parent,
        "Controls & Guides Visibility", ":scenetoolbar/scene-view-marker-c", ":scenetoolbar/scene-view-marker-");
814

815 816 817
    mControlElements->addButton (1, Mask_CellMarker, "Cell Marker");
    mControlElements->addButton (2, Mask_CellArrow, "Cell Arrows");
    mControlElements->addButton (4, Mask_CellBorder, "Cell Border");
818

819
    mControlElements->setSelectionMask (0xffffffff);
820 821 822 823 824 825 826

    connect (mControlElements, SIGNAL (selectionChanged()),
        this, SLOT (elementSelectionChanged()));

    return mControlElements;
}

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
void CSVRender::PagedWorldspaceWidget::cellDataChanged (const QModelIndex& topLeft,
    const QModelIndex& bottomRight)
{
    /// \todo check if no selected cell is affected and do not update, if that is the case
    if (adjustCells())
        flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::cellRemoved (const QModelIndex& parent, int start,
    int end)
{
    if (adjustCells())
        flagAsModified();
}

void CSVRender::PagedWorldspaceWidget::cellAdded (const QModelIndex& index, int start,
    int end)
{
    /// \todo check if no selected cell is affected and do not update, if that is the case
    if (adjustCells())
        flagAsModified();
848
}
849

Kyle Cooley's avatar
Kyle Cooley committed
850 851 852 853 854 855 856 857 858
void CSVRender::PagedWorldspaceWidget::assetTablesChanged()
{
    std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter = mCells.begin();
    for ( ; iter != mCells.end(); ++iter)
    {
        iter->second->reloadAssets();
    }
}

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
void CSVRender::PagedWorldspaceWidget::loadCameraCell()
{
    addCellToSceneFromCamera(0, 0);
}

void CSVRender::PagedWorldspaceWidget::loadEastCell()
{
    addCellToSceneFromCamera(1, 0);
}

void CSVRender::PagedWorldspaceWidget::loadNorthCell()
{
    addCellToSceneFromCamera(0, 1);
}

void CSVRender::PagedWorldspaceWidget::loadWestCell()
{
    addCellToSceneFromCamera(-1, 0);
}

void CSVRender::PagedWorldspaceWidget::loadSouthCell()
{
    addCellToSceneFromCamera(0, -1);
}