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

Button Help

Started by bblewittAG, 06 March 2014 - 04:26 AM
bblewittAG #1
Posted 06 March 2014 - 05:26 AM
I have very little experience with LUA programming language so when you read this it gives a brief overview on me.

Code that I have written so far:
SpoilerMain Program (startup)

local mon = peripheral.wrap("top")
mon.setTextScale(1)
function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end
function Buttons()
  os.loadAPI("ButtonAPI")
  mon = peripheral.wrap("top")
  function FillTable()
	ButtonAPI.setTable("Rules", Rules, 10, 14, 6, 6)
ButtonAPI.setTable("Infomation", Info, 10, 19, 8, 8)
ButtonAPI.setTable("Plugins", Plugins, 9, 17, 10, 10)
ButtonAPI.setTable("How to get started", Htgs, 9, 28, 12, 12)
  end
  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end
  function Rules()
	ButtonAPI.flash("Rules")
print("Rules")
  end
  function Info()
	ButtonAPI.flash("Information")
print("Information")
  end
  function Plugins()
	ButtonAPI.flash("Plugins")
print("Plugins")
  end
  function Htgs()
	ButtonAPI.flash("How to get started")
print("How to get started")
  end
  FillTable()
  while true do
	GetClick()
  end
end
Title("bblewittAG's Server")
Buttons()
Button API (API)

local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)

function setTable(name, func, xmin, xmax, ymin, ymax)
  button[name] = {}
  button[name]["func"] = func
  button[name]["active"] = false
  button[name]["xmin"] = xmin
  button[name]["ymin"] = ymin
  button[name]["xmax"] = xmax
  button[name]["ymax"] = ymax
end

function fill(text, color, bData)
  mon.setBackgroundColor(color)
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  local xspot = math.floor((bData["xmax"] - bData["xmin"] - #text) /2) +1
  for j = bData["ymin"], bData["ymax"] do
	mon.setCursorPos(bData["xmin"], j)
	if j == yspot then
	  for k = 0, bData["xmax"] - bData["xmin"] - #text +1 do
		if k == xspot then
		  mon.write(text)
		else
		  mon.write(" ")
		end
	  end
	else
	  for i = bData["xmin"], bData["xmax"] do
		mon.write(" ")
	  end
	end
  end
  mon.setBackgroundColor(colors.black)
end

function screen()
  local currColor
  for name,data in pairs(button) do
	local on = data["active"]
	if on == true then currColor = colors.black else currColor = colors.black end
	fill(name, currColor, data)
  end
end

function toggleButton(name)
  button[name]["active"] = not button[name]["active"]
  screen()
end	

function flash(name)
  toggleButton(name)
  screen()
  sleep(0.15)
  toggleButton(name)
  screen()
end

function checkxy(x, y)
  for name, data in pairs(button) do
	 if y>=data["ymin"] and  y <= data["ymax"] then
		if x>=data["xmin"] and x<= data["xmax"] then
		   data["func"]()
		   return true
		end
	 end
  end
  return false
end
The problems I have been having are:
The information button outputs an error message: ButtonAPI:50: attempt to index ? (a nill value)
When computer is turned on it is a blank screen until one button is clicked (not including the title)
Bomb Bloke #2
Posted 06 March 2014 - 11:18 AM
You wrote that API? Clever.

Your information button is called "Infomation". Adding the missing letter should solve your first problem.

The "screen()" function in the button API has to be called to get the display drawn. Clicking a button calls it automatically, but you'll want to call it once manually directly after defining your buttons.
bblewittAG #3
Posted 06 March 2014 - 10:30 PM
You wrote that API? Clever.

Your information button is called "Infomation". Adding the missing letter should solve your first problem.

The "screen()" function in the button API has to be called to get the display drawn. Clicking a button calls it automatically, but you'll want to call it once manually directly after defining your buttons.

Ok thanks for that now that is fixed I wrote more of the program that I want now I am not 100% happy with the code because I am kinda a beginner could you go over the code and find parts to shorten or even make better so that it is easier to write without having to repeat code over and over.

Current Code:
SpoilerMain program

local mon = peripheral.wrap("top")
os.loadAPI("ButtonAPI")
mon.setTextScale(1)

function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end

function Buttons()
  mon.setTextColor(colors.white)

  function FillTable()
	ButtonAPI.setTable("Rules", Rules, 10, 14, 6, 6)
ButtonAPI.setTable("Information", Infomation, 10, 20, 8, 8)
ButtonAPI.setTable("Plugins", Plugins, 10, 16, 10, 10)
ButtonAPI.setTable("How to get started", Htgs, 10, 27, 12, 12)
  end

  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end

  function Rules()
	shell.run("Rules")
  end

  function Infomation()
	shell.run("Information")
  end

  function Plugins()
	shell.run("Plugins")
  end

  function Htgs()
	shell.run("How_to_get_started")
  end

  FillTable()
  while true do
	ButtonAPI.screen()
	GetClick()
  end
end

mon.clear()
sleep(0.1)
Title("bblewittAG's Server")
Buttons()
Button API


local mon = peripheral.wrap("top")
mon.setTextScale(1)
local button={}
mon.setBackgroundColor(colors.black)

function setTable(name, func, xmin, xmax, ymin, ymax)
  button[name] = {}
  button[name]["func"] = func
  button[name]["active"] = false
  button[name]["xmin"] = xmin
  button[name]["ymin"] = ymin
  button[name]["xmax"] = xmax
  button[name]["ymax"] = ymax
end

function fill(text, color, bData)
  mon.setBackgroundColor(color)
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  local xspot = math.floor((bData["xmax"] - bData["xmin"] - #text) /2) +1
  for j = bData["ymin"], bData["ymax"] do
	mon.setCursorPos(bData["xmin"], j)
	if j == yspot then
	  for k = 0, bData["xmax"] - bData["xmin"] - #text +1 do
		if k == xspot then
		  mon.write(text)
		else
		  mon.write(" ")
		end
	  end
	else
	  for i = bData["xmin"], bData["xmax"] do
		mon.write(" ")
	  end
	end
  end
  mon.setBackgroundColor(colors.black)
end

function screen()
  local currColor
  for name,data in pairs(button) do
	local on = data["active"]
	if on == true then currColor = colors.black else currColor = colors.black end
	fill(name, currColor, data)
  end
end

function toggleButton(name)
  button[name]["active"] = not button[name]["active"]
  screen()
end

function flash(name)
  toggleButton(name)
  screen()
  sleep(0.15)
  toggleButton(name)
  screen()
end

function checkxy(x, y)
  for name, data in pairs(button) do
	 if y>=data["ymin"] and  y <= data["ymax"] then
		if x>=data["xmin"] and x<= data["xmax"] then
		   data["func"]()
		   return true
		end
	 end
  end
  return false
end
Rules Page


local mon = peripheral.wrap("top")
os.loadAPI("ButtonAPI")
mon.setTextScale(1)

function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end

function Buttons()
  mon.setTextColor(colors.white)

  function FillTable()
	ButtonAPI.setTable("Back", Back, 1, 4, 19, 19)
  end

  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end

  function Back()
	shell.run("startup")
  end

  FillTable()
  while true do
	ButtonAPI.screen()
	GetClick()
  end
end
function Text()
  mon.setTextColor(colors.yellow)
  mon.setCursorPos(1, 5)
  mon.write("1. Do not grief")
  mon.setCursorPos(1, 6)
  mon.write("2. Do not steal")
  mon.setCursorPos(1, 7)
  mon.write("3. Have fun")
end
mon.clear()
sleep(0.1)
Title("bblewittAG's Server Rules")
Text()
Buttons()
Information Page


local mon = peripheral.wrap("top")
os.loadAPI("ButtonAPI")
mon.setTextScale(1)

function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end

function Buttons()
  mon.setTextColor(colors.white)

  function FillTable()
	ButtonAPI.setTable("Back", Back, 1, 4, 19, 19)
  end

  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end

  function Back()
	shell.run("startup")
  end

  FillTable()
  while true do
	ButtonAPI.screen()
	GetClick()
  end
end
function Text()
  mon.setTextColor(colors.yellow)
  mon.setCursorPos(1, 5)
  mon.write("This server is to give people a chance to work")
  mon.setCursorPos(1, 6)
  mon.write("together to build a house a factory or what ever")
  mon.setCursorPos(1, 7)
  mon.write("you want but with an added bonus of mods to give")
  mon.setCursorPos(1, 8)
  mon.write("you more options to build and use with.")
end
mon.clear()
sleep(0.1)
Title("bblewittAG's Server Information")
Text()
Buttons()
Plugins Page

local mon = peripheral.wrap("top")
os.loadAPI("ButtonAPI")
mon.setTextScale(1)

function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end

function Buttons()
  mon.setTextColor(colors.white)

  function FillTable()
	ButtonAPI.setTable("Back", Back, 1, 4, 19, 19)
  end

  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end

  function Back()
	shell.run("startup")
  end

  FillTable()
  while true do
	ButtonAPI.screen()
	GetClick()
  end
end
function Text()
  mon.setTextColor(colors.yellow)
  mon.setCursorPos(1, 5)
  mon.write("Essentials")
  mon.setCursorPos(1, 6)
  mon.write("Grief Prevention")
  mon.setCursorPos(1, 7)
  mon.write("Group Manager")
  mon.setCursorPos(1, 8)
  mon.write("World Edit")
end
mon.clear()
sleep(0.1)
Title("bblewittAG's Server Plugins")
Text()
Buttons()
How to get started page


local mon = peripheral.wrap("top")
os.loadAPI("ButtonAPI")
mon.setTextScale(1)

function Title(text)
  mon.setTextColor(colors.red)
  w, h = mon.getSize()
  mon.setCursorPos((w-#text)/2+1, 1)
  mon.write(text)
end

function Buttons()
  mon.setTextColor(colors.white)

  function FillTable()
	ButtonAPI.setTable("Back", Back, 1, 4, 19, 19)
  end

  function GetClick()
	event, side, x, y = os.pullEvent("monitor_touch")
ButtonAPI.checkxy(x, y)
  end

  function Back()
	shell.run("startup")
  end

  FillTable()
  while true do
	ButtonAPI.screen()
	GetClick()
  end
end
function Text()
  mon.setTextColor(colors.yellow)
  mon.setCursorPos(1, 3)
  mon.write("Go to a range of 500 or more to build a home")
  mon.setCursorPos(1, 4)
  mon.write("start like any other minecraft world but use")
  mon.setCursorPos(1, 5)
  mon.write("Tinkers Construct and NEI for tools and recipes")
  mon.setCursorPos(1, 6)
  mon.write("Once you have a house begin with 1 of 4 mods")
  mon.setCursorPos(1, 7)
  mon.write("Thermal Expansion, IC2, Factorization and")
  mon.setCursorPos(1, 8)
  mon.write("Tinkers Construct for double or triple ore output.")
end
mon.clear()
sleep(0.1)
Title("bblewittAG's Server How to get started")
Text()
Buttons()