This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xcrafter_40's profile picture

Defective Program

Started by xcrafter_40, 12 January 2017 - 02:18 AM
xcrafter_40 #1
Posted 12 January 2017 - 03:18 AM
Hi,

I was recently working on a OS called CPOS, and the menu wasn't working.
Here is my code:
Source Code

term.setCursorPos(1,1)
term.setBackgroundColor(colors.black)
term.clear()
if not term.isColor() then
  print("Requires advanced compter!")
  return
end
version = "b0.1"
versionr = "Open beta 0.1"
dbg = "/cpos_tmp/desktopbg"
local steel_fr = [[
-- Steel Framework v1.0
-- 2017 xcrafter_40

local function fgbg(fg,bg)
  term.setTextColor(fg)
  term.setBackgroundColor(bg)
end
function writeText(txt,fg,bg)
  fgbg(fg,bg)
  term.write(txt)
end
function writeAt(txt,fg,bg,x,y)
  term.setCursorPos(x,y)
  fgbg(fg,bg)
  term.write(txt)
end
function goto(x,y)
  term.setCursorPos(x,y)
end
function bg(bg)
  term.setBackgroundColor(bg)
end
function fg(fg)
  term.setTextColor(fg)
end
function setAll(fg,bg,x,y)
  fgbg(fg,bg)
  term.setTextColor(x,y)
end
function resetColors()
  fgbg(1,32768)
end
function resetPos()
  term.setCursorPos(1,1)
end
function resetAll()
  fgbg(1,32768)
  term.setCursorPos(1,1)
end
function clear()
  term.clear()
end
function clearLine()
  term.clearLine()
end
function resetClear()
  fgbg(1,32768)
  term.setCursorPos(1,1)
  term.clear()
end
function fillBg(bg)
  term.setCursorPos(1,1)
  term.setBackgroundColor(bg)
  term.clear()
end
function fillLine(y,bg)
  term.setCursorPos(1,y)
  term.setBackgroundColor(bg)
  term.clearLine()
end
function image(x,y,image)
  paintutils.drawImage(paintutils.loadImage(image),x,y)
  resetAll()
end
function writeD(txt)
  term.write(txt)
end
function clickedAt(x1,y1,x2,y2)
  local event, AA, AB, AC = os.pullEventRaw()
  if event == "mouse_click" and AA == 1 and AB >= x1 and AB <= x2 and AC >= y1 and AC <= y2 then
	return true
  else
	return false
  end
end
]]
local desktop = [[
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000
888008880777077700000000000000000000000000000000000
800008080707070000000000000000000000000000000000000
800008880707077700000000000000000000000000000000000
800008000707000700000000000000000000000000000000000
888008000777077700000000000000000000000000000000000
]]
local function unpackfile(str,file)
  shell.setDir("")
  fs.makeDir("cpos_tmp")
  x = fs.open("cpos_tmp/" .. file,"w")
  x.write(str)
  x.close()
end
unpackfile(steel_fr,"steel")
os.loadAPI("/cpos_tmp/steel")
if not steel then
  print("FATAL: STEEL API NOT LOADED!")
  print("Contact xcrafter_40 for help!")
  sleep(10)
  exit()
end
steel.fillBg(colors.white)
steel.writeAt("CP",colors.gray,colors.white,1,1)
steel.writeAt("OS",colors.lightGray,colors.white,3,1)
steel.writeAt("Loading...",colors.red,colors.white,1,2)
unpackfile(desktop,"desktopbg")
sleep(2)
function drawDesktop()
  screen = "desktop"
  steel.resetClear()
  steel.image(1,2,dbg)
  steel.fillLine(1,colors.lightBlue)
  steel.writeAt("#",colors.black,colors.lightBlue,1,1)
  steel.goto(1,1)
end
run = true
screen = "desktop"
drawDesktop()
while run do
  if os.pullEventRaw() == "terminate" then
	steel.resetClear()
	run = false
  elseif screen == "desktop" then
	if steel.clickedAt(1,1,1,1) then
	  steel.setAll(colors.black,colors.lightBlue,1,1)
	  steel.clearLine()
	  steel.writeD("Enter Filename: ")
	  pgname = read()
	  current = term.current()
	  a = window.create(term.current(),1,1,51,18)
	  a_coro = coroutine.create(loadfile(pgname))
	  term.redirect(a)
	  ok, err = coroutine.resume(a_coro)
	  if ok then
		while  coroutine.status(a_coro) ~= "dead" do
		  local event = {coroutine.yield()}
		  coroutine.resume(a_coro, unpack(event))
		end
	  end
	  term.redirect(current)
	  drawDesktop()
	end
  end
end
os.unloadAPI("steel")
shell.setDir("")
fs.delete("cpos_tmp")
Pastebin code:

pastebin get 9rfeG3em cpos
Now when I click the # to open the run menu, it does nothing
I need help quick!
Lupus590 #2
Posted 12 January 2017 - 08:03 PM
That's not how to best use os.pullEvent

I would recommend looking for a tutorial on event pulling.
Bomb Bloke #3
Posted 12 January 2017 - 11:22 PM
while run do
  if os.pullEventRaw() == "terminate" then
        .
        .
        .
  elseif screen == "desktop" then
        if steel.clickedAt(1,1,1,1) then
          .
          .
          .
        end
  end
end

When this code runs, on each iteration it'll stop and wait for an event (to see if it's a terminate event - it'll ignore it if it isn't), then stop and wait for another event (to see if it's a click at a certain screen location - it'll ignore it if it isn't).

So let's say you click your #. The first event into the queue is a mouse_click event, which will be ignored because it's not a terminate event. The second event into the queue will be a mouse_up event (as the user lets go of the button), which will be ignored because it's not a mouse_click event. Then your loop repeats…

It's better to pull one event, then check it against all event types you're interested in before pulling another. Eg, something like:

while true do
  local myEvent = {os.pullEventRaw()}

  if myEvent[1] == "terminate" then
    -- do exiting stuff.

  elseif clickedAt(myEvent, 1,1,1,1) then  -- Pass the event data we've already got to the function.
    -- do mouse-clicky stuff.

  end
end