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();

	};
相关推荐
灯澜忆梦10 小时前
GO_面向对象_方法
开发语言·golang
布朗克16810 小时前
Go 入门到精通-16-字符串深入
开发语言·后端·golang·字符串
北冥you鱼10 小时前
Go flag 包详解:从命令行解析到实战应用
开发语言·后端·golang
To_OC11 小时前
LC 22 括号生成:刷完这道题,我终于搞懂回溯剪枝了
javascript·算法·leetcode
小二·11 小时前
Next.js 15 企业级实战:从项目搭建到CI/CD全链路(App Router + Server Components + Turbopack)
javascript·ci/cd·turbopack
web守墓人11 小时前
【python】uv解决依赖安装缓慢的问题
开发语言·python·uv
像我这样帅的人丶你还11 小时前
🚀大文件上传的那些事
前端·javascript·架构
To_OC11 小时前
LC 39 组合总和:回溯入门必刷题,我踩过的两个坑都在这了
javascript·算法·leetcode
橘子星11 小时前
从零理解流式输出 —— 一个 Vue + DeepSeek 的前端实战
前端·javascript·人工智能
geovindu12 小时前
CSharp: Bridge Pattern
开发语言·后端·桥接模式·结构型模式