Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0

Question

So, I've written my own menu API in CC, called 'Jenisis' named after what I called my software company on me and a few friends servers, (did quite well actually seeing as how I'm the only one who could code) and I ported it over to OpenComputers and then continued development on this, seeing as how IMO OC is better. Which brings me to my problem. I've been trying to add a feature where the API takes a table full of functions to run whenever an event is triggered, like for receiving modem messages whilst still in the menu's loop. I can't quite get it working and just need some new ideas on how.

 

Here's the API:

local term = require("term")
local shell = require("shell")
local internet = require("internet")
local keyboard = require("keyboard")
local event = require("event")
local os = require("os")
local component = require("component")
local gpu = component.gpu
 
jenisis = {}
 
function jenisis.drawScreen(staticScreen, tableScreen, handlerTable)
  local running = true
  local rkeyp = true
  local cselected = 1
  local num = 1
  local static = true
  local isPrint = true
  local staticNum = 1
  local param = "f"
 
  while running == true do
    term.clear()
    term.setCursor(1, 1)
   
    rkeyp = true
    isPrint = true
    num = 1
    static = true
    staticNum = 1
    param = "nulll"
 
    while static == true do
      local line = staticScreen[staticNum]
      if line ~= nil then
        print(line)
        staticNum = staticNum + 1
      else
        static = false
      end
    end
 
    num = 1
    print(" ")
 
    while isPrint == true do
      local line = tableScreen[num]
      if line ~= nil and num ~= cselected then
        print(line)
        num = num + 1
      elseif line ~= nil and num == cselected then
        print("["..line.."]")
        num = num + 1
      else
        isPrint = false
      end
    end
   
    while rkeyp == true do
      local ev = {event.pull()}
      if ev[1] == "key_down" then
        local key = ev[4]

        if key == 200 then
          cselected = cselected - 1
          if cselected <= 0 then
            cselected = num - 1
          end
          rkeyp = false
        elseif key == 208 then
          cselected = cselected + 1
          if cselected >= num then
            cselected = 1
          end
          rkeyp = false
        elseif key == 28 then
          return cselected
        elseif key == 15 then
          return 999
        end
      else
        for k, v in ipairs(handlerTable) do
          if k == ev[1] then
            k(ev)
          end
        end
      end
    rkeyp = false
    end
  end
end
 
return jenisis 

 

And here's the test program:

local jenisis = require("jenisis")
local component = require("component")
local gpu = component.gpu
local os = require("os")

local jnhandletable = {
  touch = function(eventinfo) print("dont touch") end,
}

local jnmenu = {
  "This is some test stuff", 
  "Dont worry about it", 
  "Seriosuyl thooo"
}
local jnstatic = {
  "This is a test",
  "yeah"
}

--jenisis.drawScreen(jnstatic, jnmenu, jnhandletable)

for k, v in ipairs(jnhandletable) do
  print(k, v)
end 

 

This isn't really my preferred way of doing it, I'd prefer something along the lines of:

handleTable = {}

function handleTouch(eventInfo) -- eventInfo is passed to the function by the API. It's the table returned from the actual event.
  -- things that happen when you touch it
end

handleTable["touch"] = handleTouch

cselected = jenisis.drawScreen(static, menu, handleTable)

Anyways, sorry for the wall of text, and thanks in advance for any help!

Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.