vcbfm

IUP+LUA_多层血量进度条.

Oct 28th, 2024 (edited)
41
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. --2024.10.28发布pastebin.
  2. --2024.10.29 修复进度条无法处理过大的数值的BUG.
  3. --编译:使用 luapack 打包为可执行文件.
  4.  
  5. --代码相当屎山,使用 iup-3.31-Lua51_Win32_bin 的 iuplua51.exe 运行可以编译.
  6. --iup-3.31-Lua51_Win32_bin 中 iuplua51.exe 通过编译.
  7.  
  8. package.cpath = package.cpath .. ";./?.dll"  -- 添加当前目录下的所有 DLL 文件
  9. local iup = require("iuplua")                -- 加载 IUP 库
  10.  
  11. --iup = require("iup")
  12.  
  13. -- 定义进度条初始配置
  14. local gaugeConfig = {
  15.     totalLayers = 3,        -- 总共3层
  16.     currentValue = 300,     -- 当前值
  17.     decreaseStep = 20,      -- 每次减少的值
  18. }
  19.  
  20. -- 定义颜色数组
  21. local progressColors = {
  22.     { -- 第一层
  23.         bar = "0 255 0",    -- 绿色进度条
  24.         bg = "255 255 0"    -- 黄色背景
  25.     },
  26.     { -- 第二层
  27.         bar = "255 255 0",  -- 黄色进度条
  28.         bg = "255 0 0"      -- 红色背景
  29.     },
  30.     { -- 第三层
  31.         bar = "255 0 0",    -- 红色进度条
  32.         bg = "0 0 0"  -- 白色背景
  33.     },
  34.     { -- 余数层
  35.         bar = "255 255 255",  -- 白色前景色
  36.         bg = "0 255 0"         -- 黑色背景色
  37.     }
  38. }
  39.  
  40. -- 创建配置输入框
  41. local stepInput = iup.text{
  42.     value = tostring(gaugeConfig.decreaseStep),
  43.     size = "50x"
  44. }
  45.  
  46. local maxValueInput = iup.text{
  47.     value = tostring(gaugeConfig.currentValue),
  48.     size = "50x"
  49. }
  50.  
  51. -- 创建标签控件
  52. local label = iup.label{
  53.     title = string.format("总进度: %d", gaugeConfig.currentValue),
  54.     font = "Arial, 12"
  55. }
  56.  
  57. -- 创建层值标签
  58. local layerLabel = iup.label{
  59.     title = string.format("当前层进度: %d", gaugeConfig.currentValue),
  60.     font = "Arial, 12"
  61. }
  62.  
  63. -- 创建 gauge
  64. local gauge = iup.gauge{
  65.     min = "0",
  66.     max = "100",
  67.     value = "100",
  68.     size = "200x30",
  69.     SHOW_TEXT = "YES",
  70.     DASHED = "NO",
  71.     FLAT = "NO",
  72.     EXPAND = "HORIZONTAL",
  73.     BORDER = "YES"
  74. }
  75.  
  76. local function getCurrentLayerInfo(totalValue)
  77.     local baseLayerPoints = math.floor(gaugeConfig.currentValue / gaugeConfig.totalLayers)
  78.     local remainder = gaugeConfig.currentValue % gaugeConfig.totalLayers
  79.    
  80.     local baseTotal = baseLayerPoints * gaugeConfig.totalLayers
  81.    
  82.     if remainder > 0 and totalValue > baseTotal then
  83.         local extraValue = totalValue - baseTotal
  84.         return 4, extraValue, remainder, true
  85.     end
  86.    
  87.     local currentLayer = 3  
  88.     local layerValue = totalValue
  89.    
  90.     if totalValue > (baseLayerPoints * 2) then
  91.         currentLayer = 1  
  92.         layerValue = totalValue - (baseLayerPoints * 2)
  93.     elseif totalValue > baseLayerPoints then
  94.         currentLayer = 2  
  95.         layerValue = totalValue - baseLayerPoints
  96.     end
  97.    
  98.     return currentLayer, layerValue, baseLayerPoints, false
  99. end
  100.  
  101.  
  102. local function updateProgressColors(gauge, totalValue)
  103.     local currentLayer, layerValue, maxLayerValue, isRemainderLayer = getCurrentLayerInfo(totalValue)
  104.     local colors = progressColors[currentLayer]
  105.    
  106.     -- 打印调试信息
  107.     -- print(string.format("总值: %d, 商: %d, 余数: %d",
  108.         -- totalValue,
  109.         -- math.floor(totalValue / gaugeConfig.layerPoints),
  110.         -- totalValue % gaugeConfig.layerPoints
  111.     -- ))
  112.     -- print("当前层:", currentLayer, (isRemainderLayer and "(余数层)" or ""))
  113.     -- print("层内值:", layerValue)
  114.     -- print("层最大值:", maxLayerValue)
  115.     -- print("背景色:", colors.bg)
  116.     -- print("前景色:", colors.bar)
  117.    
  118.     gauge.max = tostring(maxLayerValue)
  119.     gauge.value = tostring(layerValue)
  120.     gauge.FGCOLOR = colors.bar
  121.     gauge.BACKCOLOR = colors.bg
  122. end
  123.  
  124. -- 创建配置标签
  125. local configBox = iup.hbox{
  126.     iup.label{title = "减少步长:"},
  127.     stepInput,
  128.     iup.label{title = "最大值:"},
  129.     maxValueInput,
  130.     gap = "5"
  131. }
  132.  
  133. -- 创建应用配置按钮来改变血量.
  134. local applyButton = iup.button{
  135.     title = "应用配置",
  136.     size = "80x30"
  137. }
  138.  
  139. function applyButton:action()
  140.     local newStep = tonumber(stepInput.value)
  141.     local newMax = tonumber(maxValueInput.value)
  142.    
  143.     if newStep and newStep > 0 and newMax and newMax > 0 then
  144.         gaugeConfig.decreaseStep = newStep
  145.         gaugeConfig.currentValue = newMax  -- 保存初始最大值
  146.        
  147.         label.title = string.format("总进度: %d", newMax)
  148.         updateProgressColors(gauge, newMax)
  149.     else
  150.         iup.Message("错误", "请输入有效的数值")
  151.     end
  152. end
  153.  
  154. -- 创建减少按钮改变血量
  155. local button = iup.button{
  156.     title = "Decrease Progress",
  157.     size = "80x30"
  158. }
  159.  
  160. function button:action()
  161.     local totalValue = tonumber(label.title:match("%d+"))
  162.     if totalValue > 0 then
  163.         local newValue = totalValue - gaugeConfig.decreaseStep
  164.         if newValue < 0 then newValue = 0 end
  165.        
  166.         label.title = string.format("总进度: %d", newValue)
  167.        
  168.         local currentLayer, layerValue, maxLayerValue, isRemainderLayer = getCurrentLayerInfo(newValue)
  169.         local layerText = string.format("当前层进度: %d/%d", layerValue, maxLayerValue)
  170.         if isRemainderLayer then
  171.             layerText = layerText .. " (余数层)"
  172.         end
  173.         layerLabel.title = layerText
  174.        
  175.         updateProgressColors(gauge, newValue)
  176.     end
  177. end
  178.  
  179. local box = iup.vbox{
  180.     configBox,
  181.     applyButton,
  182.     label,
  183.     layerLabel,
  184.     gauge,
  185.     button,
  186.     gap = "10",
  187.     alignment = "ACENTER",
  188.     margin = "10x10"
  189. }
  190.  
  191. -- 创建对话框
  192. local dlg = iup.dialog{
  193.     title = "Progress Bar Example",
  194.     size = "400x250",
  195.     box
  196. }
  197.  
  198. -- 初始化颜色和进度
  199. updateProgressColors(gauge, gaugeConfig.currentValue)
  200.  
  201. dlg:show()
  202. iup.MainLoop()
Advertisement
Comments
Add Comment
Please, Sign In to add comment