Jump to content




Checkboxes don't work quite as they should

api

9 replies to this topic

#1 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 25 April 2018 - 04: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!

#2 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 25 April 2018 - 05: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 by EveryOS, 25 April 2018 - 05:03 PM.


#3 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 25 April 2018 - 05:06 PM

EDIT: NVM, just found out it only reacts to clicking the actual cube.

Edited by Windows10User, 25 April 2018 - 05:08 PM.


#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 25 April 2018 - 05: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()


#5 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 26 April 2018 - 12:32 PM

View PostDog, on 25 April 2018 - 05:34 PM, said:

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.

#6 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 26 April 2018 - 03: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

#7 Dave-ee Jones

  • Members
  • 456 posts
  • LocationVan Diemen's Land

Posted 27 April 2018 - 04:30 AM

View PostLupus590, on 26 April 2018 - 03:29 PM, said:

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 by Dave-ee Jones, 27 April 2018 - 04:30 AM.


#8 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 27 April 2018 - 11:13 AM

Thanks ppl, but by the way, what should I do next speaking of my GUI API (http://www.computerc...ui-drawing-api/)? I have buttons, labels, checkboxes and background images so far, but I didn't update the paste and I need ideas?

#9 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 27 April 2018 - 04: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.

#10 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 28 April 2018 - 02:03 PM

View PostLupus590, on 27 April 2018 - 04:33 PM, said:

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users