首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lua OpenTX自定义菜单

Lua OpenTX自定义菜单
EN

Stack Overflow用户
提问于 2017-07-27 20:13:30
回答 1查看 436关注 0票数 0

我正在为OpenTX RC发射器制作一个lua遥测脚本,我想在用户按下按钮后创建一个自定义菜单。我想知道如何检查按钮点击,但我想知道是否有为我创建自定义菜单的功能。

我在opentx github文档中找到了函数popupInput(title, event, input, min, max),但是当我调用这个函数时,似乎什么都没有发生。

我想要像OpenTX系统中的库存菜单那样的东西

在这里你可以看到一个菜单,里面有重置遥测和重置飞行的选项,我想做一些类似的事情。

那么有没有办法创建自定义菜单呢??或者我必须自己做所有的绘图和输入处理??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-28 02:25:18

我放弃了寻找能做我想做的事情的函数,而是自己做

下面是函数:

代码语言:javascript
复制
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

它接受带有字符串索引的函数数组。索引是菜单中的名称,当用户选择它时,函数将执行。菜单的大小编码很差,所以有时可能太宽了。

为了让它工作,你还需要一个全局变量菜单,它应该控制菜单的可见性。您应该通过以下方式打开菜单:

代码语言:javascript
复制
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

而且您还需要选择全局变量。

代码语言:javascript
复制
local selected = 0;

但是这个函数并不完整,它不支持滚动,当传递给这个函数的项目太多时,它的行为可能会很奇怪

截图:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45350520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档