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

multitasking and dragging icons?

Started by IsaacTBeast, 15 March 2014 - 08:53 AM
IsaacTBeast #1
Posted 15 March 2014 - 09:53 AM
Hey guys! I am just wondering that how to make a multitasking and dragging icons like CraftBang does, for ComputerCraft.. Please help me..


--Desktop
--Created By: IsaacTBeast
--Learned By: IsaacTBeast
--Open Source. You copy this code and make it to your own self  if you have my permission..
--fLib was created by: NDFJay
--I write the code by hand not by copying it to others..
os.loadAPI("/.astro/system/api/fLib")

slc = 0
tBarC=128
local tBartC = 2
textC = 2
backColor= 1
contextX = 0
iConText="File Manager"
iCon="/.astro/system/images/.icon"
iConLink="/Programs/filema"
iCon2="/.astro/system/images/unicon2"
iConText2="Simple Pastebin"
iConLink2="/Programs/pastebinsi"
systemFile="/.astro/system/.kernel/astro.lua"
backGroundC="/.astro/system/images/.background"
--Calling Variables

term.setBackgroundColor(backColor)
term.clear()
local paintutils = setmetatable(
  {
	loadImage = function(path)
	  local image = _G.paintutils.loadImage(path)
	  assert(image,"Attempted to load faulty image "..path.."!")
	  return image
	end
  },
  {
	__index = _G.paintutils
  }
)
--Title Bar
function titleBar()
term.setCursorPos(1,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
term.clearLine()
term.setCursorPos(2, 1)
print("[Astro]")
end
function titleBar2()
term.setCursorPos(36,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
print("[System Config]")
end
function titleBar3()
term.setCursorPos(1,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
term.clearLine()
term.setCursorPos(2,1)
print("[Exit]")
end
function drawDesktop()
  term.setBackgroundColor(backColor)
  term.clear()
  bground = paintutils.loadImage(backGroundC)
  icon = paintutils.loadImage(iCon)
  icon2 = paintutils.loadImage(iCon2)
  paintutils.drawImage(icon2, 2, 12)
  paintutils.drawImage(icon, 2, 3)
  paintutils.drawImage(bground,1,1)
  term.setBackgroundColor(backColor)
  term.setCursorPos(1,9)
  term.setTextColor(colors.red)
  print("+")
  term.setCursorPos(2,10)
  term.setTextColor(128)
  print(iConText)
  term.setTextColor(colors.red)
  term.setCursorPos(1,17)
  print("+")
  term.setTextColor(128)
  term.setCursorPos(2, 18)
  print(iConText2)
  titleBar()
  titleBar2()
end

function drawMenu1()
  term.setCursorPos(1,2)
  print("		  ")
  term.setCursorPos(1,3)
  print(" Shutdown ")
  term.setCursorPos(1,4)
  print(" Restart  ")
  term.setCursorPos(1,5)
  print("		  ")
end
function drawMenu2()
  term.setBackgroundColor(128)
  term.setTextColor(256)
  term.setCursorPos(contextX, contextY)
  print("			 ")
  term.setCursorPos(contextX, contextY+1)
  print("  Terminal   ")
  term.setCursorPos(contextX, contextY+2)
  print(" FileManager ")
  term.setCursorPos(contextX, contextY+3)
  print("			 ")

end
function drawMenu3(iX, iY)
  term.setBackgroundColor(128)
  term.setTextColor(256)
  term.setCursorPos(iX, iY)
  print("		 ")
  term.setCursorPos(iX, iY+1)
  print(" Rename  ")
  term.setCursorPos(iX, iY+2)
  print(" Chan-Icon ")
  term.setCursorPos(iX, iY+3)
  print(" Re-Link ")
  term.setCursorPos(iX, iY+4)
  print("		 ")
end
function drawMenu4()
term.setBackgroundColor(128)
term.setTextColor(256)
term.setCursorPos(36,2)
print("			   ")
term.setCursorPos(36,3)
print(" C-Background  ")
term.setCursorPos(36,4)
print(" Update Astro  ")
term.setCursorPos(36,5)
print("			   ")
end
drawDesktop()
while true do

local tEvent = {os.pulLEvent()}
if tEvent[1] == "mouse_click" then
-- Heres the mouse click event code
local event,button, X, Y = os.pullEventRaw("mouse_click")
  if slc == 0 then
	if event == "mouse_click" then
	  if X >= 2 and X <= 8 and Y == 1 and button == 1  then
		drawMenu1()
		slc = 1
		  elseif X >= 2 and X <= 5 and Y >= 3 and Y <= 9 and button == 1 then
		  shell.run(iConLink)		
			elseif X >= 1 and Y >= 2 and button == 2 then slc = 2
			  if  X >=38 then
			  contextX = 38
			  end
			  if Y >=14 then
			  contextY = 14
			  end
			  if X <= 38 then
			  contextX = X
			  end
			  if Y <= 14 then
			  contextY = Y
			  end
			  drawMenu2()
				elseif X == 1 and Y == 9 and button == 1 then slc = 3
				  drawMenu3(1,11)
					elseif X >= 2 and X <= 6 and Y >= 12 and Y <= 15 and button == 1 then
					  shell.run(iConLink2)
						elseif X == 1 and Y == 18 and button == 1 then slc = 3
						  drawMenu3(1,18)
						   elseif X >= 36 and X <= 50 and Y == 1 then slc = 4
							 drawMenu4()
								  
		else	
		drawDesktop()	  
	   end
	  end		  
   elseif slc == 1 then
	 if X >= 1 and X <= 11  and button == 1 and Y == 3 then slc = 0
	   os.shutdown()
	   elseif X >= 1 and X <= 11 and Y == 4 and button == 1 then slc = 0
	   os.reboot()
	   else
	   slc = 0
	   drawDesktop()
  end  
elseif slc == 2 then
	  if X >= contextX and X <= contextX+13 and Y == contextY+1 and button == 1 then slc = 0
		shell.run("/Programs/terminal")
		drawDesktop()
		  elseif X >= contextX and X <= contextX+13 and Y == contextY+2 and button == 1 then slc = 0
			shell.run(iConLink)
			drawDesktop()
			   else
				slc = 0
				drawDesktop()  
			
	   end
	elseif slc == 3 then
	  if X >= 1 and X <= 12 and Y == 12 and button == 1 then
	  term.setTextColor(256)
	  term.setBackgroundColor(backColor)
	  term.setCursorPos(2,10)
	  print("		 ")
	  term.setCursorPos(2,10)
	  input = read()
	  fLib.replaceLine(systemFile,16,'iConText="'..tostring(input)..'"')
	  return
	  shell.run(systemFile)
  
		  elseif X >= 1 and X <= 12 and Y == 13 and button == 1 then
			 term.setBackgroundColor(128)
			 lY = 8
			  for i =1,3 do
			  term.setCursorPos(18, lY)
			  print("					 ")
			  lY = lY+1
			  end
				term.setBackgroundColor(256)
				term.setTextColor(1)
				term.setCursorPos(19, 9)
				print("				   ")
				term.setCursorPos(20,9)
				input = read()
				fLib.replaceLine(systemFile,17,'iCon="'..tostring(input)..'"')
				return
				shell.run(systemFile)
				   elseif X >= 1 and X <= 12 and Y == 14 and button == 1 then
					 term.setBackgroundColor(128)
					   lY = 8
			
					   for i = 1,3 do
					   term.setCursorPos(18,lY)
						print("						")
					   lY = lY+1
					   end
					   term.setBackgroundColor(256)
					   term.setCursorPos(19, 9)
					   print("					  ")
					 term.setCursorPos(20,9)
					  term.setTextColor(1)
					  input = read()
					 fLib.replaceLine(systemFile, 18,'iConLink="'..tostring(input)..'"')
					 return
					 shell.run(systemFile)
				
				
							  
				else
				slc = 0
				drawDesktop()
			   end
				 elseif slc == 4 then
					if X >= 36 and X <= 50 and Y == 3 and button == 1 then
					   term.setBackgroundColor(128)
						 lY = 8
						
						 for i = 1, 3 do
						 term.setCursorPos(18,lY)
						 print("						 ")
						 lY = lY+1
						 end
						 term.setBackgroundColor(256)
						 term.setCursorPos(19,9)
						 print("					   ")
						 term.setCursorPos(20,9)
						 term.setTextColor(1)
						 input = read()
						 fLib.replaceLine(systemFile,23, 'backGroundC= "'..tostring(input)..'"')
	   return
	   shell.run(systemFile)
	   elseif X >= 36 and X <= 50 and Y == 4 and button == 1 then
			
	   term.setBackgroundColor(128)
	   lY = 8
	   for i = 1, 3 do
		  term.setCursorPos(18,lY)
		  print("					  ")
	   end
	   term.setCursorPos(20,9)
	   term.setTextColor(1)
	   print("  Updating Astro OS   ")	
	   while true do
														   local source = http.get("http://pastebin.com/raw.php?i=PMP5KhJm")
		  local update = fs.open("/.astro/system/.kernel/astro.lua", "w")
		  update.write(source.readAll())
		  update.close()
		  sleep(1)
		  os.reboot()
	   end
			
				
	  
	  
						 else
						 slc = 0
						 drawDesktop()
  
			  end
		
  
			
   elseif tEvent[2] == "mosue_drag" then
	  --[[Here's the mouse_drag code is.]]
		end
  
	 end
Edited on 17 March 2014 - 05:30 AM
CometWolf #2
Posted 15 March 2014 - 09:56 AM
It's all about events
http://computercraft.info/wiki/Os.pullEvent
I haven't looked at CraftBang, but chances are it's feeding all non-user events to all running programs even if you're on the desktop, thus allowing them to handle them while you're dragging your icon or whatever.
theoriginalbit #3
Posted 15 March 2014 - 10:52 AM
I believe that it has come that time again that I refer you to this post.
Edited on 15 March 2014 - 09:53 AM
IsaacTBeast #4
Posted 15 March 2014 - 11:09 AM
I know LUA! I just want to how to make multitasking and dragging icons just that simple thing!
theoriginalbit #5
Posted 15 March 2014 - 11:31 AM
See I don't think you do know Lua, since knowing Lua would allow you to read and understand how CraftBang works and translate the use of os.pullEvent — more specifically the events such as mouse_drag — across into your program.
IsaacTBeast #6
Posted 15 March 2014 - 03:27 PM
there's Mouse_Drag on os.pullEvent?? I don't know that.. and besides I self studied lua in all ages.. Just teach me please. I abandoned programming lua when I started playing in a band but I come back again!. but please help me.
Edited on 15 March 2014 - 02:27 PM
CometWolf #7
Posted 15 March 2014 - 03:34 PM
there's Mouse_Drag on os.pullEvent??

You clearyly haven't put in as much work as you claim, as just reading the link i posted would have told you as much…
IsaacTBeast #8
Posted 15 March 2014 - 03:39 PM
I will do whatever Comet Wolf but How do you add an dragging icons when theres a mouse clicking event on my program..

my program has mouse_click event. how can you add multiple events?
Edited on 15 March 2014 - 02:44 PM
IsaacTBeast #9
Posted 15 March 2014 - 03:48 PM
I saw the mouse drag event and tried the how to, but when I click the red pixel to move it it turns the screen red.
CometWolf #10
Posted 15 March 2014 - 03:48 PM

local tEvent = {os.pullEvent()} --store events in a table
if tEvent[1] == "mouse_click" then
  --mouse click code here
elseif tEvent[1] == "mouse_drag" then
  --mouse drag code here
end
IsaacTBeast #11
Posted 15 March 2014 - 03:50 PM
Where should I put it?

Here's the code


os.loadAPI("/.astro/system/api/fLib")

slc = 0
tBarC=128
local tBartC = 2
textC = 2
backColor= 1
contextX = 0
iConText="File Manager"
iCon="/.astro/system/images/.icon"
iConLink="/Programs/filema"
iCon2="/.astro/system/images/unicon2"
iConText2="Simple Pastebin"
iConLink2="/Programs/pastebinsi"
systemFile="/.astro/system/.kernel/astro.lua"
backGroundC="/.astro/system/images/.background"
term.setBackgroundColor(backColor)
term.clear()
local paintutils = setmetatable(
  {
    loadImage = function(path)
	  local image = _G.paintutils.loadImage(path)
	  assert(image,"Attempted to load faulty image "..path.."!")
	  return image
    end
  },
  {
    __index = _G.paintutils
  }
)
--Title Bar
function titleBar()
term.setCursorPos(1,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
term.clearLine()
term.setCursorPos(2, 1)
print("[Astro]")
end
function titleBar2()
term.setCursorPos(36,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
print("[System Config]")
end
function titleBar3()
term.setCursorPos(1,1)
term.setBackgroundColor(tBarC)
term.setTextColor(tBartC)
term.clearLine()
term.setCursorPos(2,1)
print("[Exit]")
end
function drawDesktop()
  term.setBackgroundColor(backColor)
  term.clear()
  bground = paintutils.loadImage(backGroundC)
  icon = paintutils.loadImage(iCon)
  icon2 = paintutils.loadImage(iCon2)
  paintutils.drawImage(icon2, 2, 12)
  paintutils.drawImage(icon, 2, 3)
  paintutils.drawImage(bground,1,1)
  term.setBackgroundColor(backColor)
  term.setCursorPos(1,9)
  term.setTextColor(colors.red)
  print("+")
  term.setCursorPos(2,10)
  term.setTextColor(128)
  print(iConText)
  term.setTextColor(colors.red)
  term.setCursorPos(1,17)
  print("+")
  term.setTextColor(128)
  term.setCursorPos(2, 18)
  print(iConText2)
  titleBar()
  titleBar2()
end
function drawMenu1()
  term.setCursorPos(1,2)
  print("		  ")
  term.setCursorPos(1,3)
  print(" Shutdown ")
  term.setCursorPos(1,4)
  print(" Restart  ")
  term.setCursorPos(1,5)
  print("		  ")
end
function drawMenu2()
  term.setBackgroundColor(128)
  term.setTextColor(256)
  term.setCursorPos(contextX, contextY)
  print("			 ")
  term.setCursorPos(contextX, contextY+1)
  print("  Terminal   ")
  term.setCursorPos(contextX, contextY+2)
  print(" FileManager ")
  term.setCursorPos(contextX, contextY+3)
  print("			 ")

end
function drawMenu3(iX, iY)
  term.setBackgroundColor(128)
  term.setTextColor(256)
  term.setCursorPos(iX, iY)
  print("		 ")
  term.setCursorPos(iX, iY+1)
  print(" Rename  ")
  term.setCursorPos(iX, iY+2)
  print(" Chan-Icon ")
  term.setCursorPos(iX, iY+3)
  print(" Re-Link ")
  term.setCursorPos(iX, iY+4)
  print("		 ")
end
function drawMenu4()
term.setBackgroundColor(128)
term.setTextColor(256)
term.setCursorPos(36,2)
print("			   ")
term.setCursorPos(36,3)
print(" C-Background  ")
term.setCursorPos(36,4)
print("			   ")
end
drawDesktop()
while true do
local event,button, X, Y = os.pullEventRaw("mouse_click")
  if slc == 0 then
    if event == "mouse_click" then
	  if X >= 2 and X <= 8 and Y == 1 and button == 1  then
	    drawMenu1()
	    slc = 1
		  elseif X >= 2 and X <= 5 and Y >= 3 and Y <= 9 and button == 1 then
		  shell.run(iConLink)		
		    elseif X >= 1 and Y >= 2 and button == 2 then slc = 2
			  if  X >=38 then
			  contextX = 38
			  end
			  if Y >=14 then
			  contextY = 14
			  end
			  if X <= 38 then
			  contextX = X
			  end
			  if Y <= 14 then
			  contextY = Y
			  end
			  drawMenu2()
			    elseif X == 1 and Y == 9 and button == 1 then slc = 3
				  drawMenu3(1,11)
				    elseif X >= 2 and X <= 6 and Y >= 12 and Y <= 15 and button == 1 then
					  shell.run(iConLink2)
					    elseif X == 1 and Y == 18 and button == 1 then slc = 3
						  drawMenu3(1,18)
						   elseif X >= 36 and X <= 50 and Y == 1 then slc = 4
							 drawMenu4()
								  
	    else	 
	    drawDesktop()	  
	   end
	  end		   
   elseif slc == 1 then
	 if X >= 1 and X <= 11  and button == 1 and Y == 3 then slc = 0
	   os.shutdown()
	   elseif X >= 1 and X <= 11 and Y == 4 and button == 1 then slc = 0
	   os.reboot()
	   else
	   slc = 0
	   drawDesktop()
  end   
elseif slc == 2 then
	  if X >= contextX and X <= contextX+13 and Y == contextY+1 and button == 1 then slc = 0
	    shell.run("/Programs/terminal")
	    drawDesktop()
		  elseif X >= contextX and X <= contextX+13 and Y == contextY+2 and button == 1 then slc = 0
		    shell.run(iConLink)
		    drawDesktop() 
			   else
			    slc = 0
			    drawDesktop()   
			 
	   end
    elseif slc == 3 then
	  if X >= 1 and X <= 12 and Y == 12 and button == 1 then
	  term.setTextColor(256)
	  term.setBackgroundColor(backColor)
	  term.setCursorPos(2,10)
	  print("		 ")
	  term.setCursorPos(2,10)
	  input = read()
	  fLib.replaceLine(systemFile,16,'iConText="'..tostring(input)..'"')
	  return
	  shell.run(systemFile)
  
		  elseif X >= 1 and X <= 12 and Y == 13 and button == 1 then
			 term.setBackgroundColor(128)
			 lY = 8
			  for i =1,3 do
			  term.setCursorPos(18, lY)
			  print("					 ")
			  lY = lY+1
			  end
			    term.setBackgroundColor(256)
			    term.setTextColor(1)
			    term.setCursorPos(19, 9)
			    print("				   ")
			    term.setCursorPos(20,9)
			    input = read()
			    fLib.replaceLine(systemFile,17,'iCon="'..tostring(input)..'"')
			    return
			    shell.run(systemFile)
				   elseif X >= 1 and X <= 12 and Y == 14 and button == 1 then
					 term.setBackgroundColor(128)
					   lY = 8
			 
					   for i = 1,3 do
					   term.setCursorPos(18,lY)
					    print("					    ")
					   lY = lY+1
					   end
					   term.setBackgroundColor(256)
					   term.setCursorPos(19, 9)
					   print("					  ")
					 term.setCursorPos(20,9)
					  term.setTextColor(1)
					  input = read()
					 fLib.replaceLine(systemFile, 18,'iConLink="'..tostring(input)..'"')
					 return
					 shell.run(systemFile)
				
				
							  
			    else
			    slc = 0
			    drawDesktop()
			   end
				 elseif slc == 4 then
				    if X >= 36 and X <= 50 and Y == 3 and button == 1 then
					   term.setBackgroundColor(128)
						 lY = 8
						
						 for i = 1, 3 do
						 term.setCursorPos(18,lY)
						 print("						 ")
						 lY = lY+1
						 end
						 term.setBackgroundColor(256)
						 term.setCursorPos(19,9)
						 print("					   ")
						 term.setCursorPos(20,9)
						 term.setTextColor(1)
						 input = read()
						 fLib.replaceLine(systemFile,23, 'backGroundC= "'..tostring(input)..'"')
	   return
	   shell.run(systemFile)
						 else
						 slc = 0
						 drawDesktop()
  
			
 
	    end
   
	 end
   end
Edited on 15 March 2014 - 02:51 PM
CometWolf #12
Posted 15 March 2014 - 03:52 PM
I can't help you if i don't know what you don't understand, but clearly you don't know much of Lua. Reading the link i posted earlier, and reading up on tables
http://www.lua.org/pil/2.5.html
is all i can really suggest.

Edit: i see you've changed your post. The code i posted is not something to copy and paste, it's an example to show how it's done. Unless otherwise stated, none of the code posted here ever will be just copy and paste. That defeats the entire purpose of this forum.
Edited on 15 March 2014 - 04:08 PM
IsaacTBeast #13
Posted 15 March 2014 - 04:10 PM
Is this a good person for teaching beginners? https://www.youtube....h?v=UddNxewWVTg
Edited on 15 March 2014 - 03:13 PM
SuicidalSTDz #14
Posted 16 March 2014 - 02:40 AM
The link posted above by CometWolf is a very good place for beginners. Start there, not on Youtube
theoriginalbit #15
Posted 16 March 2014 - 02:42 AM
Is this a good person for teaching beginners? https://www.youtube....h?v=UddNxewWVTg
Yeah definitely not. As SuicidalSTDz said, the link to the PIL that CometWolf posted is a very good resource. Although you'd have know that if you read what I said here, as I suggest good resources for Lua and ComputerCraft.
IsaacTBeast #16
Posted 16 March 2014 - 03:08 AM
I can't read.
theoriginalbit #17
Posted 16 March 2014 - 03:17 AM
I can't read.
Yeah 'cause that makes sense. That really explains why you'd come to a text-based forum and ask a question to which you'd get a textual response.
SuicidalSTDz #18
Posted 16 March 2014 - 03:23 AM
I can't read.
Yeah 'cause that makes sense. That really explains why you'd come to a text-based forum and ask a question to which you'd get a textual response.
Programming in general also requires a level of reading
:P/>




Edit: As does holding a conversation..
Edited on 16 March 2014 - 02:28 AM
IsaacTBeast #19
Posted 16 March 2014 - 03:27 AM
Just joking, I can read.. I am here to learn…. but I don't understand how the mouse drag cooperates with mouse click events..
Edited on 16 March 2014 - 02:30 AM
IsaacTBeast #20
Posted 16 March 2014 - 03:32 AM
I don't really understand for i =1 , #INPUT do foo foo foo end
theoriginalbit #21
Posted 16 March 2014 - 03:35 AM
I don't understand how the mouse drag cooperates with mouse click events..
mouse_drag doesn't cooperate with mouse_click, they're two completely different events. you must detect where they've clicked (which is fired JUST before the drag events) and decide what they've clicked on, then once you get drag events you can assume that they're trying to move whatever is at wherever they clicked. Take a look at the wiki to see what the other return values in mouse_click and mouse_drag mean.

I don't really understand for i =1 , #INPUT do foo foo foo end
that in Lua is a simple for loop. it says loop from 1 to the length of INPUT storing the number into the variable i.

Edit: As does holding a conversation..
well there are people that cannot read. but they generally spend all their life talking to people as opposed to doing something that requires reading.
SuicidalSTDz #22
Posted 16 March 2014 - 03:36 AM
Ninja'd

Edit:
well there are people that cannot read. but they generally spend all their life talking to people as opposed to doing something that requires reading.
I was referring to his earlier conversation with you ;)/>
Edited on 16 March 2014 - 02:42 AM
mrpoopy345 #23
Posted 16 March 2014 - 12:13 PM
"Here in Ask a Pro, the effort people are willing to expend helping you is usually about equal to the effort you are putting in." — Lyqyd
Read the PIL, like people said, and look at some other peoples code!
You want us to just spoon feed you? Go to stack overflow!
We have given you all the help you need, we have cited many resources available to you, so USE them!
Because your own man, not someone else's!