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

	};
相关推荐
爱滑雪的码农7 小时前
详细说说React大型项目结构以及日常开发核心语法
前端·javascript·react.js
@大迁世界8 小时前
43.HTML 事件处理和 React 事件处理有什么区别?
前端·javascript·react.js·html·ecmascript
代钦塔拉9 小时前
Qt4 vs Qt5 带参数信号槽的连接方式详解
开发语言·数据库·qt
ZC跨境爬虫9 小时前
跟着 MDN 学 HTML day_38:(DocumentFragment 文档片段接口详解)
前端·javascript·ui·html·音视频
tohand9 小时前
Unity 完美假阴影实现文档
unity·游戏引擎
@大迁世界10 小时前
41.ShadCN 是什么?它如何和 Tailwind CSS 集成,从而更容易构建可访问且可自定义的 React 组件?
前端·javascript·css·react.js·前端框架
InfinteJustice11 小时前
踩坑分享C 语言文件操作全攻略:从基础读写到随机访问与缓冲区原理
c语言·开发语言·microsoft
码云数智-大飞11 小时前
滥用Lombok的@EqualsAndHashCode导致线上事故复盘
开发语言
yong999011 小时前
C# 实时查看硬件使用率(CPU 内存 硬盘 网络)
开发语言·网络·c#