我正在为OpenTX RC发射器制作一个lua遥测脚本,我想在用户按下按钮后创建一个自定义菜单。我想知道如何检查按钮点击,但我想知道是否有为我创建自定义菜单的功能。
我在opentx github文档中找到了函数popupInput(title, event, input, min, max),但是当我调用这个函数时,似乎什么都没有发生。
我想要像OpenTX系统中的库存菜单那样的东西

在这里你可以看到一个菜单,里面有重置遥测和重置飞行的选项,我想做一些类似的事情。
那么有没有办法创建自定义菜单呢??或者我必须自己做所有的绘图和输入处理??
发布于 2017-07-28 02:25:18
我放弃了寻找能做我想做的事情的函数,而是自己做
下面是函数:
local function drawMenu(items, event)
local maxLen = 0;
local itemCount = 0;
for index, action in pairs(items) do
itemCount = itemCount + 1;
if string.len(index) > maxLen then
maxLen = string.len(index);
end
end
local width = maxLen * 5; -- width of the menu frame
local height = (itemCount * 9) + 2; -- height of the menu frame
if event == EVT_EXIT_BREAK then
menu = false;
end
if event == EVT_PLUS_BREAK or event == EVT_ROT_LEFT then
selected = selected + 1;
end
if event == EVT_MINUS_BREAK or event ==EVT_ROT_RIGHT then
selected = selected - 1;
end
count = 0;
actions = {};
lcd.drawFilledRectangle((LCD_W/2) - (width/2), (LCD_H/2) - (height/2), width, height, ERASE);
lcd.drawRectangle((LCD_W/2) - (width/2), (LCD_H/2) - (height/2), width, height, SOLID);
for index, action in pairs(items) do
actions[count] = action;
if count == selected then
lcd.drawText((LCD_W/2) - (width/2) + 3, (LCD_H/2) - (height/2) + (count * 8) + 2, index, 0+ INVERS);
else
lcd.drawText((LCD_W/2) - (width/2) + 3, (LCD_H/2) - (height/2) + (count * 8) + 2, index, 0);
end
count = count + 1;
end
if event == EVT_ROT_BREAK or event == EVT_ENTER_BREAK then
actions[selected]();
menu = false;
end
if selected >= count then
selected = 0;
end
if selected < 0 then
selected = count - 1;
end
end它接受带有字符串索引的函数数组。索引是菜单中的名称,当用户选择它时,函数将执行。菜单的大小编码很差,所以有时可能太宽了。
为了让它工作,你还需要一个全局变量菜单,它应该控制菜单的可见性。您应该通过以下方式打开菜单:
local menu = false;
local function run_func(event)
if event == EVT_MENU_LONG then
menu = true;
end
if menu then
drawMenu(items, event);
end
end而且您还需要选择全局变量。
local selected = 0;但是这个函数并不完整,它不支持滚动,当传递给这个函数的项目太多时,它的行为可能会很奇怪
截图:

https://stackoverflow.com/questions/45350520
复制相似问题