1. 规范了一些Live2D实例代码的内容
2. 增加了窗口大小随着模型大小变化而变化的功能
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include <QOpenGLWidget>
|
||||
#include "menu.h"
|
||||
|
||||
class GLCore : public QOpenGLWidget
|
||||
class GLCore final : public QOpenGLWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -19,15 +19,18 @@ public:
|
||||
// 删除移动运算符
|
||||
GLCore& operator=(GLCore&&) = delete;
|
||||
|
||||
~GLCore();
|
||||
~GLCore() override;
|
||||
|
||||
// 帧率控制
|
||||
void setFrameRate(double fps);
|
||||
double getFrameRate();
|
||||
[[nodiscard]] double getFrameRate() const;
|
||||
// 帧率表
|
||||
static QMap<QString, double> getFrameRateMap();
|
||||
static QStringList getFrameRateList();
|
||||
|
||||
// 安全地设置窗口的实际像素尺寸
|
||||
void setWindowSize(int w, int h);
|
||||
|
||||
void mouseMoveEvent(QMouseEvent* event) override;
|
||||
void mousePressEvent(QMouseEvent* event) override;
|
||||
void mouseReleaseEvent(QMouseEvent* event) override;
|
||||
|
||||
+26
-11
@@ -61,7 +61,7 @@ GLCore::GLCore(int w, int h, QWidget *parent)
|
||||
TextRenderer::getInstance()->setGravity(600.0f); // 更快的下坠速度
|
||||
TextRenderer::getInstance()->setDampFactor(0.85f); // 更强的弹性效果
|
||||
|
||||
this->setWindowFlag(Qt::FramelessWindowHint); // 设置无边框窗口
|
||||
// this->setWindowFlag(Qt::FramelessWindowHint); // 设置无边框窗口
|
||||
this->setWindowFlag(Qt::WindowStaysOnTopHint); // 设置窗口始终在顶部
|
||||
this->setWindowFlag(Qt::Tool); // 隐藏应用程序图标
|
||||
this->setAttribute(Qt::WA_TranslucentBackground); // 设置窗口背景透明
|
||||
@@ -75,7 +75,7 @@ GLCore::GLCore(int w, int h, QWidget *parent)
|
||||
frameTimer->start(static_cast<int>((1.0 / frameRate) * 1000)); // 使用成员变量计算间隔
|
||||
|
||||
|
||||
// 启用鼠标跟踪,不启用鼠标按下才会回调mouseMoveEvent函数
|
||||
// 启用鼠标跟踪,不启用的话鼠标按下才会回调mouseMoveEvent函数
|
||||
this->setMouseTracking(true);
|
||||
|
||||
// 连接一些必要的信号与槽
|
||||
@@ -114,7 +114,7 @@ void GLCore::setFrameRate(double fps)
|
||||
}
|
||||
|
||||
// 获取当前帧率
|
||||
double GLCore::getFrameRate()
|
||||
double GLCore::getFrameRate() const
|
||||
{
|
||||
return frameRate;
|
||||
}
|
||||
@@ -163,8 +163,8 @@ void GLCore::closeEvent(QCloseEvent* event)
|
||||
void GLCore::mouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
LAppDelegate::GetInstance()->GetView()->OnTouchesMoved(
|
||||
event->position().x(),
|
||||
event->position().y()
|
||||
static_cast<float>(event->position().x()),
|
||||
static_cast<float>(event->position().y())
|
||||
);
|
||||
|
||||
if (isLeftPressed) {
|
||||
@@ -176,8 +176,8 @@ void GLCore::mouseMoveEvent(QMouseEvent* event)
|
||||
void GLCore::mousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
LAppDelegate::GetInstance()->GetView()->OnTouchesBegan(
|
||||
event->position().x(),
|
||||
event->position().y()
|
||||
static_cast<float>(event->position().x()),
|
||||
static_cast<float>(event->position().y())
|
||||
);
|
||||
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
@@ -198,8 +198,8 @@ void GLCore::mousePressEvent(QMouseEvent* event)
|
||||
void GLCore::mouseReleaseEvent(QMouseEvent* event)
|
||||
{
|
||||
LAppDelegate::GetInstance()->GetView()->OnTouchesEnded(
|
||||
event->position().x(),
|
||||
event->position().y()
|
||||
static_cast<float>(event->position().x()),
|
||||
static_cast<float>(event->position().y())
|
||||
);
|
||||
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
@@ -218,16 +218,31 @@ void GLCore::initializeGL()
|
||||
|
||||
void GLCore::paintGL()
|
||||
{
|
||||
LAppDelegate::GetInstance()->update();
|
||||
LAppDelegate::GetInstance()->update(); // Live2D画面渲染
|
||||
// 渲染文本
|
||||
TextRenderer::getInstance()->update();
|
||||
TextRenderer::getInstance()->render();
|
||||
}
|
||||
|
||||
void GLCore::resizeGL(int w, int h)
|
||||
void GLCore::resizeGL(const int w, const int h)
|
||||
{
|
||||
// 设置文本渲染器窗口大小
|
||||
TextRenderer::getInstance()->setWindowSize(w, h);
|
||||
|
||||
LAppDelegate::GetInstance()->resize(w, h);
|
||||
}
|
||||
|
||||
// 设置窗口大小,并触发 resizeGL 事件
|
||||
void GLCore::setWindowSize(const int w, const int h)
|
||||
{
|
||||
// 检查是否需要更新,避免重复调用
|
||||
if (this->width() == w && this->height() == h) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用 QWidget::resize 或 setFixedSize 来改变窗口的实际尺寸
|
||||
setFixedSize(w, h);
|
||||
|
||||
// 调用 setFixedSize 会自动触发 QOpenGLWidget 的 resizeEvent,
|
||||
// 进而调用 resizeGL(w, h),无需手动调用 resizeGL
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
//
|
||||
// Created by Administrator on 2025/2/16.
|
||||
//
|
||||
|
||||
#ifndef AIRI_DESKTOPGRIL_TEXTRENDERER_H
|
||||
#define AIRI_DESKTOPGRIL_TEXTRENDERER_H
|
||||
#pragma once
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
#include <QVector>
|
||||
@@ -99,5 +97,3 @@ private:
|
||||
float gravity; /// 重力加速度(像素/秒²)
|
||||
float dampFactor; /// 碰撞阻尼系数
|
||||
};
|
||||
|
||||
#endif //AIRI_DESKTOPGRIL_TEXTRENDERER_H
|
||||
|
||||
@@ -198,8 +198,8 @@ void TextRenderer::update()
|
||||
|
||||
// 最终居中定位
|
||||
int totalWidth = 0;
|
||||
for (int w : instance.charWidths) totalWidth += w;
|
||||
int startX = (windowWidth - totalWidth) / 2;
|
||||
for (const int w : instance.charWidths) totalWidth += w;
|
||||
const int startX = (windowWidth - totalWidth) / 2;
|
||||
int currentX = startX;
|
||||
|
||||
instance.charPositions.clear();
|
||||
|
||||
Reference in New Issue
Block a user