北大侠客行MUD论坛

 找回密码
 注册
搜索
热搜: 新手 wiki 升级
查看: 469|回复: 0

[Mudlet]换行函数(基于宽度)

[复制链接]
发表于 2023-11-25 23:02:21 | 显示全部楼层 |阅读模式
function wrapLineBasedOnWidth(maxWidth, windows, lineToWrap)
  --三个参数分别为:每行宽度,一个含窗口名的表,该行的文本
  lineToWrap = lineToWrap or getCurrentLine()
  local wrapAt = {}
  local index = 1
  local lineWidth = 0
  local length = utf8.len(lineToWrap)
  local totalWidth = utf8.width(lineToWrap, true, 0)
  local wrapCount = totalWidth / maxWidth
  if wrapCount <= 1 then
    return
  end
  --开始计算换行位置
  while wrapCount > 1 and index <= length do
    local char = utf8.sub(lineToWrap, index, index)
    local charWidth = utf8.width(char, true, 0)
    lineWidth = charWidth + lineWidth
    if lineWidth > maxWidth then
     table.insert(wrapAt, 1, (index - 1))
     wrapCount = wrapCount - 1
     lineWidth = charWidth
    end
    index = index + 1
  end
  --开始换行
  if wrapAt then
    for i = 1, #wrapAt do
      for j = 1, #windows do
        local latestline = getLastLineNumber(windows[j])
        moveCursor(windows[j], wrapAt[i], latestline - i)
        insertText(windows[j], "\n")
        echo(windows[j], "")
      end
    end
  end
end

行首触发器例子:【闲聊】
local currentLine = line
geyser_GUI.chat_window.cut()
上面这个函数里有copy()和appendBuffer()

local miniconsoles = {"all_miniconsole", "chat_miniconsole"}
上面这个表是我需要换行的窗口

wrapLineBasedOnWidth(utility.chat_console_width, miniconsoles, currentLine)
utility.chat_console_width是我用getColumnCount()算好的变量


北大侠客行Mud(pkuxkx.net),最好的中文Mud游戏!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|北大侠客行MUD ( 京ICP备16065414号-1 )

GMT+8, 2024-4-28 04:19 AM , Processed in 0.008508 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表