From 7331723bad4351c514684252115a5ab93a01db63 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 24 Dec 2018 09:32:05 -0500 Subject: [PATCH] Add custom editor style --- tools/leveleditor/CMakeLists.txt | 4 +- tools/leveleditor/include/editorstyle.h | 13 +++ tools/leveleditor/src/editorstyle.cpp | 139 ++++++++++++++++++++++++ tools/leveleditor/src/main.cpp | 2 + tools/leveleditor/src/mainwindow.cpp | 71 ++++++++++++ 5 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 tools/leveleditor/include/editorstyle.h create mode 100644 tools/leveleditor/src/editorstyle.cpp diff --git a/tools/leveleditor/CMakeLists.txt b/tools/leveleditor/CMakeLists.txt index 9aaa62c..77fb5cc 100644 --- a/tools/leveleditor/CMakeLists.txt +++ b/tools/leveleditor/CMakeLists.txt @@ -2,7 +2,8 @@ find_package(Qt5Widgets CONFIG REQUIRED) set(INCLUDE_FILES include/mainwindow.h - include/renderwindow.h) + include/renderwindow.h + include/editorstyle.h) qt5_wrap_cpp(EDITOR_SRC ${INCLUDE_FILES}) @@ -10,6 +11,7 @@ add_executable(LevelEditor src/main.cpp src/mainwindow.cpp src/renderwindow.cpp + src/editorstyle.cpp ${EDITOR_SRC}) target_include_directories(LevelEditor PRIVATE include) target_link_libraries(LevelEditor Qt5::Widgets Engine) diff --git a/tools/leveleditor/include/editorstyle.h b/tools/leveleditor/include/editorstyle.h new file mode 100644 index 0000000..8392927 --- /dev/null +++ b/tools/leveleditor/include/editorstyle.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class EditorStyle : public QProxyStyle { +public: + void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *widget = nullptr) const override; + void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override; + void drawComplexControl(ComplexControl control, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const override; + QSize sizeFromContents(ContentsType type, const QStyleOption *opt, const QSize &size, const QWidget *widget) const override; + int pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget) const override; + void polish(QPalette& palette) override; +}; diff --git a/tools/leveleditor/src/editorstyle.cpp b/tools/leveleditor/src/editorstyle.cpp new file mode 100644 index 0000000..2b0e94f --- /dev/null +++ b/tools/leveleditor/src/editorstyle.cpp @@ -0,0 +1,139 @@ +#include "editorstyle.h" + +#include +#include +#include + +void EditorStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *widget) const { + int x, y, width, height; + opt->rect.getRect(&x, &y, &width, &height); + + switch(pe) { + case PE_FrameMenu: + { + p->fillRect(x, y, width, height, QColor(75, 75, 80)); + } + break; + case PE_PanelMenu: + { + p->fillRect(x, y, width, height, QColor(50, 50, 55)); + } + break; + case PE_PanelMenuBar: + { + p->fillRect(x, y, width, height, QColor(75, 75, 80)); + } + break; + default: + QProxyStyle::drawPrimitive(pe, opt, p, widget); + break; + } +} + +void EditorStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { + int x, y, width, height; + option->rect.getRect(&x, &y, &width, &height); + + switch(element) { + case CE_MenuBarEmptyArea: + painter->fillRect(option->rect, QColor(50, 50, 55)); + break; + case CE_MenuBarItem: + { + const QStyleOptionMenuItem* menuopt = qstyleoption_cast(option); + + const bool selected = menuopt->state & State_Selected; + const bool hovered = menuopt->state & State_MouseOver; + const bool enabled = menuopt->state & State_Enabled; + + if(enabled && (selected || hovered)) + painter->fillRect(option->rect, option->palette.brush(QPalette::Highlight)); + + drawItemText(painter, option->rect, Qt::AlignCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine, menuopt->palette, true, menuopt->text, QPalette::ButtonText); + } + break; + case CE_MenuItem: + { + const QStyleOptionMenuItem* menuopt = qstyleoption_cast(option); + + if(menuopt->menuItemType == QStyleOptionMenuItem::Separator) { + painter->setPen(QColor(75, 75, 80)); + painter->drawLine(x + 5, y + 1, x + width - 5, y + 1); + } else { + const bool selected = menuopt->state & State_Selected; + const bool hovered = menuopt->state & State_MouseOver; + const bool enabled = menuopt->state & State_Enabled; + + QPixmap pix = menuopt->icon.pixmap(16, 16, (menuopt->state & State_Enabled) ? QIcon::Normal : QIcon::Disabled); + + QRect rect = option->rect; + rect.adjust(0, 4, 0, 0); + + if(enabled && (selected || hovered)) + painter->fillRect(rect, option->palette.brush(QPalette::Highlight)); + + if(!pix.isNull()) { + const QRect iconRect = option->rect.adjusted(5, 5, 0, 0); + + drawItemPixmap(painter, iconRect, 0, pix); + + rect.adjust(27, 0, 0, 0); + } + + auto index = menuopt->text.indexOf("CTRL", 0, Qt::CaseInsensitive); + QString label = menuopt->text.left(index); + QString shortcut; + if(index != -1) + shortcut = menuopt->text.right(menuopt->text.length() - index + 1); + + drawItemText(painter, rect, Qt::AlignLeft | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine, menuopt->palette, true, label, QPalette::ButtonText); + + if(index != -1) { + painter->setPen(QColor(150, 150, 150)); + drawItemText(painter, rect.adjusted(0, 0, -10, 0), Qt::AlignRight | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine, menuopt->palette, true, shortcut); + } + } + } + break; + default: + QProxyStyle::drawControl(element, option, painter, widget); + break; + } +} + +void EditorStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const { + QProxyStyle::drawComplexControl(control, opt, p, widget); +} + +QSize EditorStyle::sizeFromContents(ContentsType type, const QStyleOption *opt, const QSize &size, const QWidget *widget) const { + switch(type) { + case QStyle::CT_MenuBarItem: + { + const QStyleOptionMenuItem* menuopt = qstyleoption_cast(opt); + return menuopt->fontMetrics.size(Qt::TextShowMnemonic, menuopt->text) + QSize(15, 5); + } + break; + default: + return QProxyStyle::sizeFromContents(type, opt, size, widget); + } +} + +int EditorStyle::pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget) const { + switch (metric) { + case PM_MenuBarPanelWidth: + return 1; + case PM_MenuPanelWidth: + return 1; + case PM_MenuBarVMargin: + return 2; + case PM_MenuBarHMargin: + return 2; + default: + return QProxyStyle::pixelMetric(metric, option, widget); + } +} + +void EditorStyle::polish(QPalette& palette) { + palette.setBrush(QPalette::Button, QColor(50, 50, 50)); +} + diff --git a/tools/leveleditor/src/main.cpp b/tools/leveleditor/src/main.cpp index 9699570..01c4a5b 100644 --- a/tools/leveleditor/src/main.cpp +++ b/tools/leveleditor/src/main.cpp @@ -8,6 +8,7 @@ #include "worldmanager.h" #include "assetmanager.h" #include "ecs.h" +#include "editorstyle.h" static std::vector extensionList; @@ -33,6 +34,7 @@ uint32_t platform::getTime() { int main(int argc, char* argv[]) { QApplication app(argc, argv); + app.setStyle(new EditorStyle()); GraphicsConfig config; config.shadowResolution = 256; diff --git a/tools/leveleditor/src/mainwindow.cpp b/tools/leveleditor/src/mainwindow.cpp index d236026..f3f9271 100644 --- a/tools/leveleditor/src/mainwindow.cpp +++ b/tools/leveleditor/src/mainwindow.cpp @@ -1,5 +1,8 @@ #include "mainwindow.h" +#include +#include +#include #include #include "renderwindow.h" @@ -7,6 +10,74 @@ MainWindow::MainWindow(Context& context) { setWindowTitle("Level Editor"); + resize(1280, 720); + + QMenuBar* mainMenuBar = new QMenuBar(); + setMenuBar(mainMenuBar); + + QMenu* fileMenu = new QMenu("File"); + mainMenuBar->addMenu(fileMenu); + + // file + { + QAction* newAction = new QAction("New"); + newAction->setIcon(QIcon::fromTheme("document-new")); + newAction->setShortcut(QKeySequence("CTRL+N")); + fileMenu->addAction(newAction); + + QAction* openAction = new QAction("Open"); + openAction->setIcon(QIcon::fromTheme("document-open")); + openAction->setShortcut(QKeySequence("CTRL+O")); + fileMenu->addAction(openAction); + + QAction* openRecentAction = new QAction("Open Recent..."); + openRecentAction->setIcon(QIcon::fromTheme("document-open-recent")); + fileMenu->addAction(openRecentAction); + + QAction* saveAction = new QAction("Save"); + saveAction->setIcon(QIcon::fromTheme("document-save")); + saveAction->setShortcut(QKeySequence("CTRL+S")); + fileMenu->addAction(saveAction); + + QAction* saveAsAction = new QAction("Save As..."); + saveAsAction->setIcon(QIcon::fromTheme("document-save-as")); + saveAsAction->setShortcut(QKeySequence("CTRL+SHIFT+S")); + fileMenu->addAction(saveAsAction); + + fileMenu->addSeparator(); + + QAction* quitAction = new QAction("Quit"); + quitAction->setIcon(QIcon::fromTheme("application-exit")); + quitAction->setShortcut(QKeySequence("CTRL+Q")); + fileMenu->addAction(quitAction); + } + + QMenu* editMenu = new QMenu("Edit"); + mainMenuBar->addMenu(editMenu); + + // edit + { + QAction* undoAction = new QAction("Undo"); + undoAction->setIcon(QIcon::fromTheme("edit-undo")); + undoAction->setShortcut(QKeySequence("CTRL+Z")); + editMenu->addAction(undoAction); + + QAction* redoAction = new QAction("Redo"); + redoAction->setIcon(QIcon::fromTheme("edit-redo")); + redoAction->setShortcut(QKeySequence("CTRL+SHIFT+Z")); + editMenu->addAction(redoAction); + + editMenu->addSeparator(); + + QAction* userPreferenes = new QAction("User Preferences..."); + userPreferenes->setIcon(QIcon::fromTheme("document-properties")); + editMenu->addAction(userPreferenes); + } + + mainMenuBar->addSeparator(); + + QMenu* helpMenu = new QMenu("Help"); + mainMenuBar->addMenu(helpMenu); QVulkanInstance* instance = new QVulkanInstance(); instance->setVkInstance(context.renderer->getInstance());