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

Checkboxes don't work quite as they should

Started by Windows10User, 25 April 2018 - 03:40 PM
Windows10User #1
Posted 25 April 2018 - 05:40 PM
I am working on my GUI API (Graphics, if you looked at the APIs subforum of the Programs forum lately). I already done the buttons and labels, so I went like "Hey, why not work on checkboxes?". So I indeed did checkboxes. However, it doesn't work as it should. It nicely accepts all the data it is made to accept and properly uses it when actually drawing the checkbox, but clicking on it doesn't seem to change its state. Thing is, I drawed a checkbox that's not checked by default. When I try to click it so I can check it, it just doesn't do its thing! I even tried making the checkbox checked by default, but it still won't work (while I can't see any sense-making error even after double-checking).

Checkbox part of the API:

checkbox = {
   text = "Check Box",
   x = 2,
   y = 2,
   checked = false,
   w = 0,
   h = 0,

   new = function(self)
	  local new = {}
	  setmetatable(new, {__index = self})
	  return new
   end,

   draw = function(self)
	  local oldBkgColor = term.getBackgroundColor()
	  local text = self.text
	  local x = self.x
	  local y = self.y
	  term.setCursorPos(x, y)
	  if self.checked == false then
		 term.setBackgroundColor(colors.gray)
		 term.write(" ")
	 else
		 term.setBackgroundColor(colors.blue)
		 term.write(" ")
	 end
	 term.setBackgroundColor(oldBkgColor)
	 term.write(" ")
	 term.write(text)
	 w = 2 + string.len(text)
	 h = 1
   end,

   detect = function(self, x, y)
	  if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then
		 if checked == false then
			checked = true
			self:draw()
		 else
			checked = false
			self:draw()
		 end
	  end
   end
}

Checkbox program:

term.clear()
term.setCursorPos(1, 1)
os.loadAPI("DoorOS/apis/graphics")
checkbox = graphics.checkbox:new()
checkbox.x = 2
checkbox.y = 2
checkbox.text = "I have read the EULA and accept it."
checkbox:draw()
while true do
   local event, btn, x, y = os.pullEvent("mouse_click")
   checkbox:detect(x, y)
end

Any help is appreciated!
EveryOS #2
Posted 25 April 2018 - 06:01 PM

detect = function(self, x, y)
		  if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then
				 if self.checked == false then
						self.checked = true
						self:draw()
				 else
						self.checked = false
						self:draw()
				 end
		  end
   end

That should help, but may not be the full fix

Also,


new = function()
		  return checkbox
   end
With your old code, you would not be able to change the variables within self i think
Edited on 25 April 2018 - 04:03 PM
Windows10User #3
Posted 25 April 2018 - 06:06 PM
EDIT: NVM, just found out it only reacts to clicking the actual cube.
Edited on 25 April 2018 - 04:08 PM
Dog #4
Posted 25 April 2018 - 06:34 PM
One thing you can do that would simply your code a bit. Instead of this…

if self.checked == false then
  self.checked = true
  self:draw()
else
  self.checked = false
  self:draw()
end

Do this instead…

self.checked = not self.checked --# flip the boolean (from true to false or false to true)
self:draw()
Windows10User #5
Posted 26 April 2018 - 01:32 PM
One thing you can do that would simply your code a bit. Instead of this…

if self.checked == false then
  self.checked = true
  self:draw()
else
  self.checked = false
  self:draw()
end

Do this instead…

self.checked = not self.checked --# flip the boolean (from true to false or false to true)
self:draw()

I just need a code which will trigger the checkbox even when its text is clicked. At the moment, I need to click the cube.
Lupus590 #6
Posted 26 April 2018 - 04:29 PM
The issue is this line of your detect function then:
if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then
Dave-ee Jones #7
Posted 27 April 2018 - 05:30 AM
The issue is this line of your detect function then:
if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then

Remember to take 1 away from an X + W in a <= relationship. Otherwise it's always 1 over, and you'll be able to click 1 pixel to the right side of the button, and no one wants that :)/> Same with Y + H.
Edited on 27 April 2018 - 03:30 AM
Windows10User #8
Posted 27 April 2018 - 12:13 PM
Thanks ppl, but by the way, what should I do next speaking of my GUI API (http://www.computercraft.info/forums2/index.php?/topic/29533-graphics-unfinished-buttons-only-so-far-a-gui-drawing-api/)? I have buttons, labels, checkboxes and background images so far, but I didn't update the paste and I need ideas?
Lupus590 #9
Posted 27 April 2018 - 05:33 PM
Have a look at what other GUI APIs have and take ideas from those. Also, have a look at real-life GUIs and see if you could mimic them.
Windows10User #10
Posted 28 April 2018 - 03:03 PM
Have a look at what other GUI APIs have and take ideas from those. Also, have a look at real-life GUIs and see if you could mimic them.

OK, by the way should I update the API on my topic in APIs and Utilities?