Esempio n. 1
0
void MapView::dropEvent(QDropEvent *event)
{
    qdbg << "dropEvent: text=" << event->mimeData()->formats().first() << endl;
    if (!event->mimeData()->hasFormat(levelObjectNameMimeType))
        return;
    event->acceptProposedAction();

    QPoint dragOffset;
    QByteArray nameData;

    QByteArray encodedData = event->mimeData()->data(levelObjectNameMimeType);
    QDataStream dataStream(&encodedData, QIODevice::ReadOnly);
    dataStream >> nameData >> dragOffset;
    QString name = QString::fromUtf8(nameData);
    LevelObject *obj = LevelObjectsModel::sharedInstance()->levelObjectByName(name);
    if (!obj)
        return;

    QSizeF objSize = obj->size();
    QPoint dragOffsetShifted(dragOffset.x() - objSize.width() / 2,
                             dragOffset.y() - objSize.height() / 2);

    QPoint viewportTargetPos(event->pos() - dragOffsetShifted);

    LevelObject *newObject = obj->clone();
    newObject->setPosition(mapToScene(viewportTargetPos));
    connect(newObject, SIGNAL(modified()),
            mapScene(), SLOT(setModified()));
    MapItem *item = new MapItem(newObject);
    MapScene *scene = mapScene();
    //scene->addItem(item);
    scene->setModified(true);
    NewItemCommand *command = new NewItemCommand(scene, item);
    scene->undoStack()->push(command);
}
Esempio n. 2
0
void LevelFactory::generate()
{
	enum directions { X_INC, X_DEC, Z_INC, Z_DEC };

	for (int i = 0; i < 20; i++)
	{
		LevelObject obj = chooseNextObject(Block());
		obj.setPosition(0, 0, 0);
		obj.setSize(10, 10, 10);
		level.addObject(obj);
	}
}