Lua OpenTX 自定义菜单

我正在制作一个用于 OpenTX 遥控发射器的 Lua 遥测脚本,我想在用户按下按钮之后创建一个自定义菜单。我已经找到了如何检查按钮点击的方法,但我想知道是否有任何函数可以为我创建自定义菜单。

我在 opentx GitHub 文档中找到了函数“popupInput(title, event, input, min, max)”,但是当我调用此函数时似乎没有发生任何事情。

我想要像 OpenTX 系统中的标准菜单一样的东西 enter image description here

在这里,您可以看到一个带有选项重置遥测和重置飞行的菜单,我想制作类似的东西。

所以,有没有创建自定义菜单的方法?还是我必须自己处理绘制和输入处理??

点赞
用户7698088
用户7698088

我放弃了寻找能够完成我的任务的函数,反而自己编写了一个。

以下是函数:

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; -- 菜单框架的宽度
    local height = (itemCount * 9) + 2; -- 菜单框架的高度
  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

它接受包含以字符串索引方法的函数数组作为参数。索引是菜单中的名称,在用户选择时执行相应方法。菜单的尺寸编码较差,有时可能会过宽。

你还需要一个控制菜单可见性的全局变量menu。你应该通过以下方式打开菜单:

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

此外,你还需要一个全局变量selected。

local selected = 0;

但此函数并不完整,它不支持滚动,当传递太多的项时可能会表现奇怪。

屏幕截图:enter image description here

2017-07-27 18:25:17