RPGMZ游戏引擎 一个窗口 文本居中显示

个人纪录保存

project1论坛 小圈子 人才 不得学习技术

javascript 复制代码
    function _New_Window() {
        this.initialize(...arguments);
    };
     
	_New_Window.prototype = Object.create(Window_Base.prototype);
    _New_Window.prototype.constructor = _New_Window;
    // 初始化窗口
    _New_Window.prototype.initialize = function(rect) {
        Window_Base.prototype.initialize.call(this, rect);
		this.npc_text="";
		this._lastText = '';
		this.paddingScale = 2;
		this.isWindow = 1; //是什么窗口; 
    };
    _New_Window.prototype.refresh = function() {
        if (this._lastText === this.npc_text) return;

        // 1. 切割文本
        let lines = this.npc_text.split(/\r?\n/); 
        let newLines = [];
        lines.forEach(row => {
            if (row.length > 0) {
                newLines.push(row);
            }
        });
		lines = newLines;
		if (lines.length === 0) lines = [""];
        // 2. 必须先创建画布,才能测量宽度
        if (!this.contents) {
            this.createContents();
        }

        // 3. 计算最长行宽度
        let maxTextW = 0;
        for (let i = 0; i < lines.length; i++) {
            let w = this.contents.measureTextWidth(lines[i]);
            if (w > maxTextW) maxTextW = w;
        }

        let lineH = this.lineHeight();
        let pad = this.padding * this.paddingScale;

        // 4. 计算窗口真实大小
        this.width = maxTextW + pad ;
        this.height = lines.length * lineH + pad;

        // 5. 重置画布 + 清空(你要的 clear 在这里)
        this.createContents();
        this.contents.clear();

        // 6. 垂直居中绘制
        let totalTextH = lines.length * lineH;
        let startY = (this.contents.height - totalTextH) / 2;

        for (let i = 0; i < lines.length; i++) {
            let y = startY + i * lineH;
            this.drawText(lines[i], 0, y, this.contents.width, "center");
        }

        // 7. 窗口定位
        switch (this.isWindow) {
            case 0: //整体窗口居中显示
                const cx = (Graphics.width - this.width) / 2;
                const cy = (Graphics.height - this.height) / 2;
                this.move(cx, cy, this.width, this.height);
            break;
            case 1://某个精灵顶部定位
                this.move(0 - this.width / 2, 0 - this.height - 74, this.width, this.height);
            break;
        }

        this._lastText = this.npc_text;
    };
	
	_New_Window.prototype.update = function(){
		this.refresh();

	};
相关推荐
小此方3 小时前
「C++AI大模型接入SDK」(一) API接入与本地两种方式对比、API Key获取、API报文详解与简单API的构建
开发语言·c++·人工智能
wuyk5553 小时前
Python实战项目02:学生成绩管理系统(控制台|CSV导出|完整落地)
开发语言·python
狂人开飞机10 小时前
23、实战平台跳跃游戏
游戏·游戏引擎·godot
浪子明X10 小时前
十六位账本怎样发现传输差错:Internet Checksum 生活类比
开发语言
尾善爱看海10 小时前
Vue 面试收官篇:SSR、性能优化落地、30 道高频面试题精讲(附标准答案)
前端·javascript·vue.js·面试·vue
xiangyun6113 小时前
【408数据结构 08】队列:循环队列判空判满,408年年考,一次讲清
c语言·开发语言·数据结构·c++·算法
xiangyun6113 小时前
【408数据结构 06】双链表、循环链表、静态链表
c语言·开发语言·数据结构·c++·算法
白远山15 小时前
家政服务家政社区派单实战:调度系统架构设计与实现指南
java·开发语言·小程序·架构·需求分析
DevRay15 小时前
Java 21虚拟线程深度实战:告别线程池焦虑,百万并发吞吐量直接翻倍(原理+代码+避坑)
java·开发语言
李明卫杭州15 小时前
React 复合事件系统对比解析
前端·javascript·react.js