void HeightMapEditor::onViewChanged(const QRectF & rect) { if (!myGraph) return; if (myUi->renderView->scene()->children().count() > 50) { QList<QGraphicsItem *> items = myUi->renderView->scene()->items(); for (QGraphicsItem * i : items) if (!i->isVisible()) myUi->renderView->scene()->removeItem(i); } QRectF trect; trect.setLeft(rect.left() - fmod(rect.left(), myCellSize.width())); trect.setTop(rect.top() - fmod(rect.top(), myCellSize.height())); trect.setRight(rect.right() - fmod(rect.right(), myCellSize.width())); trect.setBottom(rect.bottom() - fmod(rect.bottom(), myCellSize.height())); for (double y = trect.top() - fmod(trect.top(), myCellSize.height()); y < trect.bottom() + myCellSize.height() - fmod(trect.bottom(), myCellSize.height()); y += myCellSize.height()) for (double x = trect.left() - fmod(trect.left(), myCellSize.width()); x < trect.right() + myCellSize.width() - fmod(trect.right(), myCellSize.width()); x += myCellSize.width()) { QRectF r(x,y,myCellSize.width(), myCellSize.height()); generateRect(r); } }
void HeightMapEditor::onRenderButtonClicked() { { boost::recursive_mutex::scoped_lock guard(myMutex); for (auto task : myTasks) task->cancellationFlag.store(true); myTasks.clear(); } myUi->renderView->scene()->clear(); Json::Value graphJson = myUi->graphBuilder->toJson(); myGraph.reset(new NoiseGraph); PhysFS::ifstream fs("map/heightmap.png"); std::vector<char> data; data.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>()); QImage heightmapImage; heightmapImage.loadFromData((const uchar *)data.data(), data.size()); boost::multi_array<double, 2> heights; heights.resize(boost::extents[heightmapImage.width()][heightmapImage.height()]); for (int y = 0; y < heightmapImage.height(); y++) for (int x = 0; x < heightmapImage.width(); x++) { QColor col(heightmapImage.pixel(x, y)); int height = col.red() | col.green() | col.blue(); heights[x][y] = (height / 256.0) * 2 - 1; } std::shared_ptr<HeightMapModule> heightmap(new HeightMapModule(heights, 400, 240)); try { myGraph->module = buildNoiseGraph(graphJson, myGraph->modules, myGraph->defs, heightmap, 0); } catch (std::exception & e) { QMessageBox::critical(this, "Error", e.what()); myGraph.reset(); return; } catch (noise::ExceptionInvalidParam & e) { QMessageBox::critical(this, "Error", "Invalid parameter"); myGraph.reset(); return; } catch (noise::ExceptionNoModule & e) { QMessageBox::critical(this, "Error", "Missing module"); myGraph.reset(); return; } catch (noise::ExceptionOutOfMemory & e) { QMessageBox::critical(this, "Error", "Out of memory"); myGraph.reset(); return; } catch (noise::ExceptionUnknown & e) { QMessageBox::critical(this, "Error", "Unknown exception"); myGraph.reset(); return; } generateRect(myUi->renderView->sceneRect()); }
std::shared_ptr<TextField> TextField::create(std::shared_ptr<Skin> skin, std::shared_ptr<WrappableText> default_text, std::shared_ptr<WrappableText> typed_text, glm::vec4 background_color, float padding, float screen_width, float screen_height, float x_pos, float y_pos, float width, float height, const unsigned int layer) { auto vertex_data = VertexData(); vertex_data.addVec(VertexData::DATA_TYPE::GEOMETRY, generateRect(screen_width, screen_height, 0, 0, width, height)); vertex_data.addVec(VertexData::DATA_TYPE::TEX_COORDS, basisTexCoords()); default_text->setSize(width - (2.0 * padding), height - (2.0 * padding)); typed_text->setSize(width - (2.0 * padding), height - (2.0 * padding)); typed_text->setText(""); auto field = std::make_shared<TextField>(default_text, typed_text, vertex_data, skin, layer); field->setColor(background_color); field->setAnchorPoint(glm::vec2(x_pos, y_pos)); field->setWidth(width); field->setHeight(height); field->setTextPadding(padding); return field; }
void HeightMapEditor::generateRect(const QRectF & rect) { QRegion region(rect.toRect(), QRegion::RegionType::Rectangle); for (QGraphicsItem * i : myUi->renderView->scene()->items()) region -= i->boundingRegion(i->sceneTransform()); if (region.isEmpty()) return; { boost::recursive_mutex::scoped_lock guard(myMutex); for (auto task = myTasks.begin(); task != myTasks.end(); task++) { if ((*task)->cancellationFlag || (*task)->completeFlag) continue; if ((*task)->area.intersects(rect)) { QRegion sub((*task)->area.toRect(), QRegion::RegionType::Rectangle); region -= sub; if (region.rectCount() > 1) for (auto r : region.rects()) generateRect(r); else (*task)->priority = 0; return; } } } std::shared_ptr<AreaGenerationTask> task(new AreaGenerationTask(myEngine, myGraph, rect)); task->priority = 0; QObject::connect(task.get(), SIGNAL(generationCompleted(QRectF, QImage)), this, SLOT(onAreaGenerated(QRectF, QImage)), Qt::QueuedConnection); myEngine->scheduler()->schedule((std::bind(&AreaGenerationTask::operator(), task))); myTasks.push_back(task); }