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

Creating buttons

Started by MarioBG, 08 February 2013 - 08:56 AM
MarioBG #1
Posted 08 February 2013 - 09:56 AM
Hello, everyone!
While programming my OS-like thing (not quite an OS, only a graphical interface, but useful nonetheless) I came over a certain issue when getting to icons in the desktop. I would like a function that helped me to create, with only one piece of code, several buttons with a part of its name untouched and the other one altered, like so:
  • Button1=1 (first x coordinate), 4 (second x coordinate), 1 (first y coordinate), 3 (second y coordinate)
  • Button2=1,4,4,7
  • Button3=1,4,8,11
And so forth, until I ran out of elements in the "/usuarios/"..user.."/escritorio" folder (yes, its file structure is Spanish-based). I would also like to get a grasp on some way of adding language packs to the OS, if you were so kind. ¡Thanks, and look forward to the OS!
Senmori #2
Posted 08 February 2013 - 10:33 AM
Use tables, I used this code to make buttons before.

local menu={}

function setTable(name, xmin, xmax, ymin, ymax)
   menu[name] = {}
   menu[name]["xmin"] = xmin
   menu[name]["ymin"] = ymin
   menu[name]["xmax"] = xmax
   menu[name]["ymax"] = ymax
end

function fillTable()
   setTable("Names", 17, 24, 2, 3)
   setTable("Go", 17, 20, 6, 7)
   setTable("Here", 17, 20, 4, 5)
end

Just call 'fillTable' and all the relevant information and there's your button.
If you want to have each button have a different background try this code:

--Function to fill the desired space
function fill(x,y,color,text)
m.setBackgroundColor(color)
m.setCursorPos(x,y)
m.write(text)
m.setBackgroundColor(colors.black)
end

--Function to change button background
--from one color to another
function bColor()
local currentColor = nil

  for name,option in pairs(menu) do
   currentColor = (option["active"] and colors.lime or colors.blue)
   fill(option["xmin"],option["ymin"],currentColor,name)
  end
end

I'm sure there are better ways of making tables but I like this setup for how easy it is to read and edit.
As for languages, I have no idea sorry :/
theoriginalbit #3
Posted 08 February 2013 - 04:28 PM
Use tables, I used this code to make buttons before.

local menu={}

function setTable(name, xmin, xmax, ymin, ymax)
   menu[name] = {}
   menu[name]["xmin"] = xmin
   menu[name]["ymin"] = ymin
   menu[name]["xmax"] = xmax
   menu[name]["ymax"] = ymax
end

function fillTable()
   setTable("Names", 17, 24, 2, 3)
   setTable("Go", 17, 20, 6, 7)
   setTable("Here", 17, 20, 4, 5)
end
That seems very… ummm… long… damn Direwolf20…

This is the exact same thing and still readable

local menu = {
  ["Names"] = { xmin = 17, xmax = 24, ymin = 2, ymax = 3 },
  ["Go"] = { xmin = 17, xmax = 20, ymin = 6, ymax = 7 },
  ["Here"] = { xmin = 17, xmax = 20, ymin = 4, ymax = 5 },
}

As for the OP. Language packs are a lot of work, but not impossible. You would need to know where the language file is, and have a setting to change it… Then you would need to load it out and when when writing have some way to translate it… VERY basic example

local langs = {
  ["EN-US"] = "some/path/enus",
  ["EN-AU"] = "some/path/enau",
  ["FR"] = "some/path/fr",
}
local currLang = "EN-US"

local function readFromCurrentLang()
  local file = fs.open( langs[currLang], "r" )
  welcomeMsg = file.readLine()
  file.close()
end
Edited on 08 February 2013 - 03:35 PM
Engineer #4
Posted 09 February 2013 - 05:39 AM
As far for the language packs, you should have an one-time config that asks what the language is. Then you can print it in the language they want.

NOTE: THIS IS A SUGGESTION
theoriginalbit #5
Posted 09 February 2013 - 05:43 AM
As far for the language packs, you should have an one-time config
-snip-
Hey you know what would work nice for storing config settings? *cough* CConfig by me *cough* actually it might even work ok for the language files themselves [/end shameless plug] :P/>
JokerRH #6
Posted 09 February 2013 - 12:25 PM
the best way would probably be creating objects:


button = {}
button._index = button --That way you can refer to some data using yourObject.someData

function button:new(x1, x2, y1, y2)
   return setmetatable({posX1 = x1, posX2 = x2, posY1 = y1, posY2 = y2}, button) --returns an object containing your    information. It stores it in the table button(you refered to that 3 lines above)
end

function button:example()
   print(self.posX1)    --just as an example of how it should work. self refers to your object
end

So using this is really simple:


function createMyButtons()
   button1 = button:new(...) --creates a new object. You can have as many of them as you want
   button2 = button:new(...)
   button3 = ..

   print(button1.posX1) --prints the value of button1's x1 position. (therefor you need the button._index line!!!)
   
   button2:example() --prints button2's x1 position using a function
end

Another awesome point of this method is, that if you say


testButton = button1
testButton.posY2 = 15

it will also change button1's posY2 (they are 'linked' together)

I hope this will help you…:)/>
Joker
theoriginalbit #7
Posted 09 February 2013 - 12:52 PM
the best way would probably be creating objects:
-snip-
Functionally it is NO different to a non-object way. and an OO way is ONLY better in this case if your using lots of sets of buttons. but if you only have one table… no point
MarioBG #8
Posted 12 February 2013 - 10:53 AM
Well, actually, I am looking for a way that let me create an infinite number of buttons according to a certain variable (within the limits of the screen, obviously), which would be the number of files and folders inside the desktop folder, so the first solution TheOriginalBIT gave seems to me the best option, the one with tables, as I could easily work my way to what I am looking for. His solution for the language packs also seems the most reasonable one, so I think I'm trying his way. Thanks a lot to everyone!