Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --2024.10.28发布pastebin.
- --2024.10.29 修复进度条无法处理过大的数值的BUG.
- --编译:使用 luapack 打包为可执行文件.
- --代码相当屎山,使用 iup-3.31-Lua51_Win32_bin 的 iuplua51.exe 运行可以编译.
- --iup-3.31-Lua51_Win32_bin 中 iuplua51.exe 通过编译.
- package.cpath = package.cpath .. ";./?.dll" -- 添加当前目录下的所有 DLL 文件
- local iup = require("iuplua") -- 加载 IUP 库
- --iup = require("iup")
- -- 定义进度条初始配置
- local gaugeConfig = {
- totalLayers = 3, -- 总共3层
- currentValue = 300, -- 当前值
- decreaseStep = 20, -- 每次减少的值
- }
- -- 定义颜色数组
- local progressColors = {
- { -- 第一层
- bar = "0 255 0", -- 绿色进度条
- bg = "255 255 0" -- 黄色背景
- },
- { -- 第二层
- bar = "255 255 0", -- 黄色进度条
- bg = "255 0 0" -- 红色背景
- },
- { -- 第三层
- bar = "255 0 0", -- 红色进度条
- bg = "0 0 0" -- 白色背景
- },
- { -- 余数层
- bar = "255 255 255", -- 白色前景色
- bg = "0 255 0" -- 黑色背景色
- }
- }
- -- 创建配置输入框
- local stepInput = iup.text{
- value = tostring(gaugeConfig.decreaseStep),
- size = "50x"
- }
- local maxValueInput = iup.text{
- value = tostring(gaugeConfig.currentValue),
- size = "50x"
- }
- -- 创建标签控件
- local label = iup.label{
- title = string.format("总进度: %d", gaugeConfig.currentValue),
- font = "Arial, 12"
- }
- -- 创建层值标签
- local layerLabel = iup.label{
- title = string.format("当前层进度: %d", gaugeConfig.currentValue),
- font = "Arial, 12"
- }
- -- 创建 gauge
- local gauge = iup.gauge{
- min = "0",
- max = "100",
- value = "100",
- size = "200x30",
- SHOW_TEXT = "YES",
- DASHED = "NO",
- FLAT = "NO",
- EXPAND = "HORIZONTAL",
- BORDER = "YES"
- }
- local function getCurrentLayerInfo(totalValue)
- local baseLayerPoints = math.floor(gaugeConfig.currentValue / gaugeConfig.totalLayers)
- local remainder = gaugeConfig.currentValue % gaugeConfig.totalLayers
- local baseTotal = baseLayerPoints * gaugeConfig.totalLayers
- if remainder > 0 and totalValue > baseTotal then
- local extraValue = totalValue - baseTotal
- return 4, extraValue, remainder, true
- end
- local currentLayer = 3
- local layerValue = totalValue
- if totalValue > (baseLayerPoints * 2) then
- currentLayer = 1
- layerValue = totalValue - (baseLayerPoints * 2)
- elseif totalValue > baseLayerPoints then
- currentLayer = 2
- layerValue = totalValue - baseLayerPoints
- end
- return currentLayer, layerValue, baseLayerPoints, false
- end
- local function updateProgressColors(gauge, totalValue)
- local currentLayer, layerValue, maxLayerValue, isRemainderLayer = getCurrentLayerInfo(totalValue)
- local colors = progressColors[currentLayer]
- -- 打印调试信息
- -- print(string.format("总值: %d, 商: %d, 余数: %d",
- -- totalValue,
- -- math.floor(totalValue / gaugeConfig.layerPoints),
- -- totalValue % gaugeConfig.layerPoints
- -- ))
- -- print("当前层:", currentLayer, (isRemainderLayer and "(余数层)" or ""))
- -- print("层内值:", layerValue)
- -- print("层最大值:", maxLayerValue)
- -- print("背景色:", colors.bg)
- -- print("前景色:", colors.bar)
- gauge.max = tostring(maxLayerValue)
- gauge.value = tostring(layerValue)
- gauge.FGCOLOR = colors.bar
- gauge.BACKCOLOR = colors.bg
- end
- -- 创建配置标签
- local configBox = iup.hbox{
- iup.label{title = "减少步长:"},
- stepInput,
- iup.label{title = "最大值:"},
- maxValueInput,
- gap = "5"
- }
- -- 创建应用配置按钮来改变血量.
- local applyButton = iup.button{
- title = "应用配置",
- size = "80x30"
- }
- function applyButton:action()
- local newStep = tonumber(stepInput.value)
- local newMax = tonumber(maxValueInput.value)
- if newStep and newStep > 0 and newMax and newMax > 0 then
- gaugeConfig.decreaseStep = newStep
- gaugeConfig.currentValue = newMax -- 保存初始最大值
- label.title = string.format("总进度: %d", newMax)
- updateProgressColors(gauge, newMax)
- else
- iup.Message("错误", "请输入有效的数值")
- end
- end
- -- 创建减少按钮改变血量
- local button = iup.button{
- title = "Decrease Progress",
- size = "80x30"
- }
- function button:action()
- local totalValue = tonumber(label.title:match("%d+"))
- if totalValue > 0 then
- local newValue = totalValue - gaugeConfig.decreaseStep
- if newValue < 0 then newValue = 0 end
- label.title = string.format("总进度: %d", newValue)
- local currentLayer, layerValue, maxLayerValue, isRemainderLayer = getCurrentLayerInfo(newValue)
- local layerText = string.format("当前层进度: %d/%d", layerValue, maxLayerValue)
- if isRemainderLayer then
- layerText = layerText .. " (余数层)"
- end
- layerLabel.title = layerText
- updateProgressColors(gauge, newValue)
- end
- end
- local box = iup.vbox{
- configBox,
- applyButton,
- label,
- layerLabel,
- gauge,
- button,
- gap = "10",
- alignment = "ACENTER",
- margin = "10x10"
- }
- -- 创建对话框
- local dlg = iup.dialog{
- title = "Progress Bar Example",
- size = "400x250",
- box
- }
- -- 初始化颜色和进度
- updateProgressColors(gauge, gaugeConfig.currentValue)
- dlg:show()
- iup.MainLoop()
Advertisement
Comments
-
- BUG:
- 只定义了4层血条,无法处理过大的血量.
-
- 2024.10.29:
- 修复进度条无法处理过大的数值的BUG.
Add Comment
Please, Sign In to add comment