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

	};
相关推荐
思麟呀12 分钟前
C++11并发编程:call_once一次性执行+atomic原子类型+CAS无锁编程+自旋锁
linux·开发语言·jvm·c++·windows
前端毕业班35 分钟前
uni-app 小程序样式隔离实践指南和原理分析
前端·javascript·vue.js
码不停蹄的玄黓36 分钟前
Java 生产者-消费者模型详解
java·开发语言·python
爱讲故事的39 分钟前
操作系统第一讲复习:为什么学习操作系统,以及操作系统到底在做什么?
linux·开发语言·windows·学习·ubuntu·c#
吃口巧乐兹41 分钟前
热加载与插件热插拔:Debug 模式 × E-Spi × H-Spi 全解析
javascript
笨蛋不要掉眼泪43 分钟前
Java并发编程:Executors框架类深度解析
java·开发语言·并发
winlife_1 小时前
让 AI 跑通“调跳跃手感“的完整闭环:funplay-unity-mcp 实战案例
人工智能·unity·游戏引擎·ai编程·mcp·游戏手感
_童年的回忆_1 小时前
【php】在linux下PHP安装amqp扩展
linux·开发语言·php
想不到ID了2 小时前
第八篇: 登录注册功能实现
java·javascript
ZC跨境爬虫2 小时前
跟着 MDN 学CSS day_37:(从文档流到粘性定位的底层原理)
前端·javascript·css·ui·html