Jump to content




How to make Custom Cursors (Like in my WinOS)


  • You cannot reply to this topic
15 replies to this topic

#1 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 06 April 2012 - 03:13 AM

If you wan't to make a cursor, you need term.setCursorPos() and some variables. Here is a example:
local currentX = 1
local currentY = 1
These will set the cursor position.
function drawCursor()
  term.clear()
  term.setCursorPos(currentX, currentY)
  write(">")
end
This will draw the cursor at the current x/y, now that we got that, we need to call drawCursor() and add controls like this:
while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

If you put all this code together like this:

local currentX = 1 --Saves the current X of the cursor
local currentY = 1 --Saves the current Y of the cursor


function drawCursor()
  term.clear() --Clears the screen
  term.setCursorPos(currentX, currentY)  --Draws a cursor at the current X/Y
  write(">")
end


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

It will create a cursor that can move left and right.

If you wan't to make options in a menu, remove the left and right and keep up/down and make it so if the cursor is at the same position of the button, make it open the button.

This is how I did it in my WinOS. You can look at that source for more information.

To change the cursor, in drawCursor(), edit write(">") and change it to write("your cursor")

#2 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 07 April 2012 - 11:33 PM

I tried this, Thanks!

#3 Justy

  • Members
  • 210 posts
  • LocationCLGD

Posted 10 April 2012 - 09:59 PM

Thanks I Used This In My New OS. Find It Here

#4 djblocksaway

    Epic Coderz

  • New Members
  • 397 posts
  • LocationAustralia

Posted 11 April 2012 - 04:46 AM

View PostComputerCraftFan11, on 06 April 2012 - 03:13 AM, said:

If you wan't to make a cursor, you need term.setCursorPos() and some variables. Here is a example:
local currentX = 1
local currentY = 1
These will set the cursor position.
function drawCursor()
  term.clear()
  term.setCursorPos(currentX, currentY)
  write(">")
end
This will draw the cursor at the current x/y, now that we got that, we need to call drawCursor() and add controls like this:
while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

If you put all this code together like this:

local currentX = 1 --Saves the current X of the cursor
local currentY = 1 --Saves the current Y of the cursor


function drawCursor()
  term.clear() --Clears the screen
  term.setCursorPos(currentX, currentY)  --Draws a cursor at the current X/Y
  write(">")
end


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

It will create a cursor that can move left and right.

If you wan't to make options in a menu, remove the left and right and keep up/down and make it so if the cursor is at the same position of the button, make it open the button.

This is how I did it in my WinOS. You can look at that source for more information.

To change the cursor, in drawCursor(), edit write(">") and change it to write("your cursor")
o.O i might try this later

#5 BaconHawk101

  • Members
  • 9 posts
  • LocationMy Computer

Posted 08 March 2013 - 02:17 PM

Awesome

#6 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 09 March 2013 - 09:29 AM

You can probably use term.getCursorPos() for this
currentx, currenty = term.getCursorPos()


#7 Icabob4

  • Members
  • 7 posts

Posted 09 March 2013 - 10:19 AM

let's see...If I use copy and paste I could have a ☺ as my cursor. ☺= :) ...and since it's linux, another way to do it would be to use
\xe2\x98\xba
instead of the smile

#8 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 09 March 2013 - 11:04 AM

Was expecting a tutorial on .Cur files lol

#9 Shnupbups

  • Members
  • 596 posts
  • LocationThat place over there. Y'know. The one where I am.

Posted 09 March 2013 - 12:23 PM

THE NECROMANCY!!

#10 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 09 March 2013 - 12:40 PM

View PostShnupbups100, on 09 March 2013 - 12:23 PM, said:

THE NECROMANCY!!

I don't mind tutorial necromancy.

#11 no9name909

  • Members
  • 5 posts

Posted 13 March 2013 - 07:17 AM

Nice tutorial. However, when you use term.clear() the whole screen will clear.
How do you move the cursor without clearing the screen?

#12 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 13 March 2013 - 03:55 PM

View Postno9name909, on 13 March 2013 - 07:17 AM, said:

Nice tutorial. However, when you use term.clear() the whole screen will clear.
How do you move the cursor without clearing the screen?

term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.

#13 Shnupbups

  • Members
  • 596 posts
  • LocationThat place over there. Y'know. The one where I am.

Posted 13 March 2013 - 07:29 PM

View PostPharap, on 13 March 2013 - 03:55 PM, said:

term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.
He's not THAT stupid, he's asking how do you remove the previous cursor from the screen without clearing the entire screen. And he probably wouldn't like term.clearLine() either, because that still clears the entire line.

#14 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 14 March 2013 - 04:42 AM

View PostShnupbups100, on 13 March 2013 - 07:29 PM, said:

View PostPharap, on 13 March 2013 - 03:55 PM, said:

term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.
He's not THAT stupid, he's asking how do you remove the previous cursor from the screen without clearing the entire screen. And he probably wouldn't like term.clearLine() either, because that still clears the entire line.
"Whatever you write gets written there" hence if you write an empty space, it will remove the previous cursor.
Regardless, not being experienced with programming does not make someone 'stupid'. Learning to read code is a like learning a new language and struggling with it or not understanding what bits are doing what does not mean someone is stupid, it means they are inexperienced.

#15 no9name909

  • Members
  • 5 posts

Posted 14 March 2013 - 08:43 AM

When I looked in the lua manual I saw that \b represents a backspace. Does this also work with CC?
If it does, it can be used to remove the cursor without clearing the whole screen.

#16 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 14 March 2013 - 09:12 AM

View Postno9name909, on 14 March 2013 - 08:43 AM, said:

When I looked in the lua manual I saw that \b represents a backspace. Does this also work with CC?
If it does, it can be used to remove the cursor without clearing the whole screen.

You can just write over top of the cursor and it will replace the cursor(">").
eg:
if key == 17 or key == 200 then --up
term.setCursorPos(currentX, currentY)
term.write(" ")--clears previous > 
	    currentY = currentY -1
  elseif key == 31 or key == 208 then --down






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users