-- This is a comment. It's for humans only. All
-- the green text is not run by the program

mouseWidth = 0
mouseHeight = 0
-- this creates two variables called mouseWidth
-- and mouseHeight and sets them to 0. We will
-- use them later

monitor = peripheral.wrap("top")
-- you need this line! It tells the computer
-- the monitor is on top. Change it if you want 
-- the monitor on a different side of the computer

monitor.clear()
-- this clears the monitor screen

monitor.setCursorPos(1,1)
-- this sets the cursor position to the top left
-- corner of the monitor

w,h=monitor.getSize()
-- gets the width and the height of the monitor
-- and stores the numbers as w and h. 
-- w and h are variables

print(w)
print(h)
-- prints the w and h to the computer screen.
-- You can see the monitor width is 7, height is 5
-- It starts in the top left corner like a book.  


-- Now to draw the two buttons
-- Im english so I write colour but you can change
-- it to color. It works the same.


monitor.setBackgroundColour((colours.lime))
-- this changes the background colour of the text
-- to lime green. 

monitor.setCursorPos(2,2)
-- this sets the start position for writing the 1st
-- button on the monitor. It puts it 2 in from the 
-- left and 2 down from the top.

monitor.write(" ON  ")
-- this writes the word ON on the monitor. See the
-- blank spaces before and after. These will be 
-- green. Our button is 5 letters long

monitor.setCursorPos(2,4)
-- this sets the next writing postition to 2 from 
-- the left and 4 down from the top. Just under 
-- the 1st button

monitor.write(" OFF ")
-- this writes OFF but again its 5 long in total
-- with the spaces

monitor.setBackgroundColour((colours.black))
-- now we have drawn our buttons we should set 
-- the text background colour back to black


-- Now we need to check if the button is clicked

-- First we are going to create a function called
-- checkClickPosition(). A function will not run 
-- until you ask for it.

-- We know the first button starts at 2 from the 
-- top and 2 from the left. We also know it is 5
-- spaces long. This means the button ends 
-- at width 7

-- We will be told which width and
-- height the click happened at. 
-- If the width position is greater than 1 AND 
-- less than 8 we have clicked somewhere between
-- 2 and 7. 

-- If this is true we can then check the height
-- position. Button one is at height 2 and button
-- two is at height 4. 

-- This means that if the width is greater than 1
-- AND the width is less than 8 AND the height
-- equals 2 we have clicked button 1

-- If the the width is greater than 1 AND the width
-- is less than 8 AND the height equals 4 we have 
-- clicked button 2

-- now to write this as a function
-- Functions are written like this

--     function exampleFunction()
--       print("Hello")
--       sleep(10)
--       print("Goodbye")
--     end

-- Now when you write exampleFunction() the program
-- will print hello, sleep for 10 ticks and then
-- print Goodbye. 
-- This is useful for making your programs easier 
-- to understand

function checkClickPosition()
  if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 2 then
    -- button one clicked
    rs.setOutput("right",true)
    -- turns redstone connected to the right on
  elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 4 then
    -- button two clicked
    rs.setOutput("right",false)
    -- turns redstone connected to the left off
  end -- ends the if loop
end -- ends the function
   
-- this function does nothing until you write
-- checkClickPostion(). We will be doing this below
-- It then checks the click position and turns the
-- lamp on if button one is clicked or turns the 
-- lamp off if button two is clicked   
  
-- OK. Now we need to check if a click happens
-- we will use a repeat-until loop.
-- In the loop we we use a os.pullEvent(). 
-- an os.pullEvent() gives you different info
-- depending on the event type. We will mainly
-- check the "monitor_touch" event.
 
-- In the second line you will see 
-- event,p1,p2,p3 = os.pullEvent()
-- if the event is a click on the monitor it
-- will give us 4 bits of info:
--    event will be "monitor_touch"    
--    p1 will be the side the monitor is on (top)
--    p2 is the width postion of the click
--    p3 is the height postition of the click  



repeat 
-- repeat runs a loop of code.

  event,p1,p2,p3 = os.pullEvent()
  -- this line tells the computer to wait until
  -- an event happens. We are waiting for a 
  -- touchscreen event
  
   if event=="monitor_touch" then
   -- this checks to see if the event was a 
   -- touchscreen event
   
     mouseWidth = p2 -- sets mouseWidth 
     mouseHeight = p3 -- and mouseHeight 
     checkClickPosition() -- this runs our function
     
   end
   -- the end of the "if loop".
   
    
until event=="char" and p1==("x")
-- this is the end of the "repeat loop". This will
-- stop the repeat loop if a "char" event happens
-- A char event means you press a character on
-- the keyboard. This line is looking for the x key