Add custom editor style
This commit is contained in:
parent
707396f384
commit
7331723bad
5 changed files with 228 additions and 1 deletions
|
@ -2,7 +2,8 @@ find_package(Qt5Widgets CONFIG REQUIRED)
|
||||||
|
|
||||||
set(INCLUDE_FILES
|
set(INCLUDE_FILES
|
||||||
include/mainwindow.h
|
include/mainwindow.h
|
||||||
include/renderwindow.h)
|
include/renderwindow.h
|
||||||
|
include/editorstyle.h)
|
||||||
|
|
||||||
qt5_wrap_cpp(EDITOR_SRC ${INCLUDE_FILES})
|
qt5_wrap_cpp(EDITOR_SRC ${INCLUDE_FILES})
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ add_executable(LevelEditor
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/mainwindow.cpp
|
src/mainwindow.cpp
|
||||||
src/renderwindow.cpp
|
src/renderwindow.cpp
|
||||||
|
src/editorstyle.cpp
|
||||||
${EDITOR_SRC})
|
${EDITOR_SRC})
|
||||||
target_include_directories(LevelEditor PRIVATE include)
|
target_include_directories(LevelEditor PRIVATE include)
|
||||||
target_link_libraries(LevelEditor Qt5::Widgets Engine)
|
target_link_libraries(LevelEditor Qt5::Widgets Engine)
|
||||||
|
|
13
tools/leveleditor/include/editorstyle.h
Normal file
13
tools/leveleditor/include/editorstyle.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QProxyStyle>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
139
tools/leveleditor/src/editorstyle.cpp
Normal file
139
tools/leveleditor/src/editorstyle.cpp
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
#include "editorstyle.h"
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QStyleOption>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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<const QStyleOptionMenuItem*>(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<const QStyleOptionMenuItem*>(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<const QStyleOptionMenuItem *>(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));
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include "worldmanager.h"
|
#include "worldmanager.h"
|
||||||
#include "assetmanager.h"
|
#include "assetmanager.h"
|
||||||
#include "ecs.h"
|
#include "ecs.h"
|
||||||
|
#include "editorstyle.h"
|
||||||
|
|
||||||
static std::vector<std::string> extensionList;
|
static std::vector<std::string> extensionList;
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ uint32_t platform::getTime() {
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
app.setStyle(new EditorStyle());
|
||||||
|
|
||||||
GraphicsConfig config;
|
GraphicsConfig config;
|
||||||
config.shadowResolution = 256;
|
config.shadowResolution = 256;
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
|
||||||
|
#include <QMenuBar>
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QAction>
|
||||||
#include <QVulkanWindow>
|
#include <QVulkanWindow>
|
||||||
|
|
||||||
#include "renderwindow.h"
|
#include "renderwindow.h"
|
||||||
|
@ -7,6 +10,74 @@
|
||||||
|
|
||||||
MainWindow::MainWindow(Context& context) {
|
MainWindow::MainWindow(Context& context) {
|
||||||
setWindowTitle("Level Editor");
|
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();
|
QVulkanInstance* instance = new QVulkanInstance();
|
||||||
instance->setVkInstance(context.renderer->getInstance());
|
instance->setVkInstance(context.renderer->getInstance());
|
||||||
|
|
Reference in a new issue