|
1 | 1 | #include "src/ui/CEGUIGraphicsScene.h" |
2 | 2 | #include "src/cegui/CEGUIManager.h" |
3 | 3 | #include "src/cegui/CEGUIProject.h" |
| 4 | +#include "src/Application.h" |
4 | 5 | #include <CEGUI/RendererModules/OpenGL/RendererBase.h> |
5 | 6 | #include <CEGUI/RendererModules/OpenGL/ViewportTarget.h> |
6 | 7 | #include <CEGUI/System.h> |
7 | 8 | #include <CEGUI/GUIContext.h> |
| 9 | +#include <CEGUI/FontManager.h> |
8 | 10 | #include <qdatetime.h> |
9 | 11 | #include <qgraphicsitem.h> |
10 | 12 | #include <qgraphicssceneevent.h> |
11 | 13 | #include <qopenglcontext.h> |
12 | 14 | #include <qopenglfunctions.h> |
13 | 15 | #include <qopenglframebufferobject.h> |
| 16 | +#include <qmessagebox.h> |
| 17 | +#include <qdir.h> |
| 18 | +#include <qdom.h> |
| 19 | +#include <qtextstream.h> |
14 | 20 |
|
15 | 21 | static void validateResolution(float& width, float& height) |
16 | 22 | { |
@@ -130,6 +136,80 @@ QList<QGraphicsItem*> CEGUIGraphicsScene::topLevelItems() const |
130 | 136 | return ret; |
131 | 137 | } |
132 | 138 |
|
| 139 | +bool CEGUIGraphicsScene::ensureDefaultFontExists() |
| 140 | +{ |
| 141 | + if (ceguiContext && ceguiContext->getDefaultFont()) return true; |
| 142 | + |
| 143 | + const auto* currentProject = CEGUIManager::Instance().getCurrentProject(); |
| 144 | + if (!currentProject) return false; |
| 145 | + |
| 146 | + auto& fontManager = CEGUI::FontManager::getSingleton(); |
| 147 | + const auto& fontRegistry = fontManager.getRegisteredFonts(); |
| 148 | + CEGUI::Font* defaultFont = fontRegistry.empty() ? nullptr : fontRegistry.begin()->second; |
| 149 | + |
| 150 | + // If project has no fonts, offer the user to autocreate a default font |
| 151 | + if (!defaultFont) |
| 152 | + { |
| 153 | + auto ret = QMessageBox::question(qobject_cast<Application*>(qApp)->getMainWindow(), |
| 154 | + "Project has no fonts", |
| 155 | + "Do you want CEED to create a default font? Note that some widgets may render wrong without a font.\n" |
| 156 | + "NOTE: You must add a new font into your scheme file by hand to get it loaded in the future. Will be fixed later.", |
| 157 | + QMessageBox::Yes | QMessageBox::No, |
| 158 | + QMessageBox::Yes); |
| 159 | + if (ret != QMessageBox::Yes) return false; |
| 160 | + |
| 161 | + // Copy font source file if not exist |
| 162 | + const auto dstFontPath = currentProject->getResourceFilePath("DejaVuSans.ttf", "fonts"); |
| 163 | + if (!QFileInfo(dstFontPath).exists()) |
| 164 | + { |
| 165 | + const auto srcFontPath = QDir::current().filePath("data/fonts/DejaVuSans.ttf"); |
| 166 | + if (!QFile(srcFontPath).copy(dstFontPath)) return false; |
| 167 | + } |
| 168 | + |
| 169 | + // Create CEGUI font object |
| 170 | + const QSizeF& resolution = currentProject->getDefaultResolution(); |
| 171 | + try |
| 172 | + { |
| 173 | + defaultFont = &fontManager.createFreeTypeFont("Default", 14.f, CEGUI::FontSizeUnit::Pixels, |
| 174 | + true, "DejaVuSans.ttf", "fonts", CEGUI::AutoScaledMode::Disabled, |
| 175 | + CEGUI::Sizef(static_cast<float>(resolution.width()), static_cast<float>(resolution.height()))); |
| 176 | + } |
| 177 | + catch (...) |
| 178 | + { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + // Save an XML font description to the project |
| 183 | + QDomDocument doc; |
| 184 | + auto xmlHeader = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); |
| 185 | + doc.appendChild(xmlHeader); |
| 186 | + auto xmlRoot = doc.createElement("Font"); |
| 187 | + xmlRoot.setAttribute("version", "3"); |
| 188 | + xmlRoot.setAttribute("name", "Default"); |
| 189 | + xmlRoot.setAttribute("filename", "DejaVuSans.ttf"); |
| 190 | + xmlRoot.setAttribute("type", "FreeType"); |
| 191 | + xmlRoot.setAttribute("size", "14"); |
| 192 | + xmlRoot.setAttribute("nativeHorzRes", QString::number(static_cast<int>(resolution.width()))); |
| 193 | + xmlRoot.setAttribute("nativeVertRes", QString::number(static_cast<int>(resolution.height()))); |
| 194 | + xmlRoot.setAttribute("autoScaled", "disabled"); // CEGUI::PropertyHelper<CEGUI::AutoScaledMode>::toString(CEGUI::AutoScaledMode::Disabled)); |
| 195 | + doc.appendChild(xmlRoot); |
| 196 | + |
| 197 | + const auto dstFontDescPath = currentProject->getResourceFilePath("Default.font", "fonts"); |
| 198 | + QFile file(dstFontDescPath); |
| 199 | + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 200 | + { |
| 201 | + QTextStream stream(&file); |
| 202 | + doc.save(stream, 4); |
| 203 | + file.close(); |
| 204 | + } |
| 205 | + |
| 206 | + // TODO: add to scheme |
| 207 | + } |
| 208 | + |
| 209 | + if (ceguiContext) ceguiContext->setDefaultFont(defaultFont); |
| 210 | + return true; |
| 211 | +} |
| 212 | + |
133 | 213 | void CEGUIGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event) |
134 | 214 | { |
135 | 215 | // Ignore item interaction for rubber band selection in a CEGUIGraphicsView |
|
0 commit comments