Jump to content




Mouse Click Flicker-How To Remove?



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

#1 makerimages

  • Members
  • 236 posts

Posted 21 September 2013 - 05:46 AM

How can I remove the print statement flicker when I click the mouse? a sleep statement somewhere?

code(Delicate bits ab my project x-ed out)

os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
os.loadAPI("Apis/windowSystem")
DesignUtil.setDesign("whiteLightGray")
local background,topBar =DesignUtil.getDesign()
local window =windowSystem.createWindow("xxxxxxxxxxxx",2,2,48,16,false)
local iHave={
text="xxxxxxxxxxx";
x=48/2-6;
y=5;
W=18;
H=1;
}
function registerClick(x,y)

end
while true do
paintutils.drawImage(background,1,1)
windowSystem.drawWindow(window)
term.setBackgroundColor(colors.white)
term.setCursorPos(iHave.x,iHave.y)
print(iHave.text)

   event, button, xPos, yPos = os.pullEvent("mouse_click")
   registerClick(xPos,yPos)
end



#2 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 06:02 AM

Yes, you are right!

Right after the print that vanishes or flickers put
os.sleep(1)


#3 makerimages

  • Members
  • 236 posts

Posted 21 September 2013 - 06:26 AM

that makes every second click flicker...

#4 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 06:44 AM

In order to help you further we'll need a pastebin to all of your code including the private bits with the xxx's. There is clearly something else happening over and above what your snippet indicates.

#5 makerimages

  • Members
  • 236 posts

Posted 21 September 2013 - 06:53 AM

os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
os.loadAPI("Apis/windowSystem")
DesignUtil.setDesign("whiteLightGray")
local background,topBar =DesignUtil.getDesign()
local window =windowSystem.createWindow("xxxxxx",2,2,48,16,false)
local iHave={
text="xxxxxxx";
x=48/2-6;
y=5;
W=18;
H=1;
}
function registerClick(x,y)
   if x >= iHave.x and x <= iHave.x + iHave.W - 1 and y >= iHave.y and y <=  iHave.y + iHave.H then
	shell.run("xxx")
   end
end
while true do
paintutils.drawImage(background,1,1)
windowSystem.drawWindow(window)
term.setBackgroundColor(colors.white)
term.setCursorPos(iHave.x,iHave.y)
print(iHave.text)

   event, button, xPos, yPos = os.pullEvent("mouse_click")
   registerClick(xPos,yPos)
end

Edit: amoun tof "x"-s in the iHave.text is not right, it should be as many x-s as W is in the table

this is all, the x strings are some that will easily give away what im working on, just text, nothing else

#6 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 09:02 AM

what about those apis? where do we get them?

#7 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 09:05 AM

these two lines of code seem like they could be the source of the flicker
paintutils.drawImage(background,1,1)
and
term.setBackgroundColor(colors.white)


#8 makerimages

  • Members
  • 236 posts

Posted 21 September 2013 - 09:14 AM

The apis are:
designutils
local fileColorConfig=nil
local topBar=nil
local backGround=nil

local fileData = {}

function getDesign()

fileColorConfig=fs.open("data/colorCombos/current","r");
local line = fileColorConfig.readLine()
repeat
table.insert(fileData,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()
local fileColorSetting=fs.open("data/colorCombos/"..fileData[1],"r");

local fileDataSetting = {}
local lineSetting = fileColorSetting.readLine()
repeat
table.insert(fileDataSetting,lineSetting)
lineSetting = fileColorSetting.readLine()
until lineSetting == nil
fileColorSetting.close()
if fileDataSetting[1]=="0" then backGround=paintutils.loadImage("graphics/backgrounds/whiteBack.nfp") end
if fileDataSetting[1]=="1" then backGround=paintutils.loadImage("graphics/backgrounds/orangeBack.nfp") end
if fileDataSetting[1]=="2" then backGround=paintutils.loadImage("graphics/backgrounds/darkPinkBack.nfp") end
if fileDataSetting[1]=="3" then backGround=paintutils.loadImage("graphics/backgrounds/lightBlueBack.nfp") end
if fileDataSetting[1]=="4" then backGround=paintutils.loadImage("graphics/backgrounds/goldenBack.nfp") end
if fileDataSetting[1]=="5" then backGround=paintutils.loadImage("graphics/backgrounds/limeBack.nfp") end
if fileDataSetting[1]=="6" then backGround=paintutils.loadImage("graphics/backgrounds/pinkBack.nfp") end
if fileDataSetting[1]=="7" then backGround=paintutils.loadImage("graphics/backgrounds/grayBack.nfp") end
if fileDataSetting[1]=="9" then backGround=paintutils.loadImage("graphics/backgrounds/cyanBack.nfp") end --because 8 is topbar color
if fileDataSetting[1]=="a" then backGround=paintutils.loadImage("graphics/backgrounds/purpleBack.nfp") end
if fileDataSetting[1]=="b" then backGround=paintutils.loadImage("graphics/backgrounds/blueBack.nfp") end
if fileDataSetting[1]=="c" then backGround=paintutils.loadImage("graphics/backgrounds/brownBack.nfp") end
if fileDataSetting[1]=="d" then backGround=paintutils.loadImage("graphics/backgrounds/greenBack.nfp") end
if fileDataSetting[1]=="e" then backGround=paintutils.loadImage("graphics/backgrounds/redBack.nfp") end


if fileDataSetting[2]=="8"then topBar=paintutils.loadImage("graphics/topBars/topBarLightGray.nfp") end
fileColorConfig.close()
return backGround, topBar;
end
function setDesign(design)
fileColorConfig=fs.open("data/colorCombos/current","w");
fileColorConfig.write(design)
fileColorConfig.close()
end
basically it loads a image according to numbers loaded from a file

window api:
local window=nil;
function createWindow(title,x,y,w,h,closeable)
window=
{
  titlew=title;
  x=x;
  y=y;
  W=w;
  H=h;
  ca=closeable;
}
return window
end
function drawWindow(windowp)
term.setCursorPos(window.x,window.y)
term.setBackgroundColor(colors.lightGray)
for i=0, windowp.W do
  term.setCursorPos(windowp.x+i,windowp.y)
  print(" ")
  term.setCursorPos(windowp.x+i,windowp.y+windowp.H)
  print(" ")

end
for j=0, windowp.H do
  term.setCursorPos(windowp.x,windowp.y+j)
  print(" ")
  term.setCursorPos(windowp.x+windowp.W,windowp.y+j)
  print(" ")

end
  term.setCursorPos(windowp.x+windowp.W/2-string.len(windowp.titlew)/2,windowp.y)
  term.setTextColor(colors.gray)
  print(windowp.titlew)
  if(windowp.ca) then
  
  term.setBackgroundColor(colors.red)
term.setCursorPos(windowp.x+windowp.W+1,windowp.y)
print("X")
end
sleep(0.1)

end

current program:
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
os.loadAPI("Apis/windowSystem")
DesignUtil.setDesign("whiteLightGray")
local background,topBar =DesignUtil.getDesign()
local window =windowSystem.createWindow("Select OSOne ID identification method",2,2,48,16,false)
local iHave={
text="I have an OSOne ID";
x=48/2-6;
y=5;
W=18;
H=1;
}
function registerClick(x,y)
   if x >= iHave.x and x <= iHave.x + iHave.W - 1 and y >= iHave.y and y <=  iHave.y + iHave.H then
    shell.run("OSCore/setupCycle/haveID.stup")
   end
end
while true do
paintutils.drawImage(background,1,1)
windowSystem.drawWindow(window)
--term.setBackgroundColor(colors.white)
term.setCursorPos(iHave.x,iHave.y)
print(iHave.text)
sleep(1)
   event, button, xPos, yPos = os.pullEvent("mouse_click")
   registerClick(xPos,yPos)
end



#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 September 2013 - 09:43 AM

View Postkreezxil, on 21 September 2013 - 06:02 AM, said:

Yes, you are right!

Right after the print that vanishes or flickers put
os.sleep(1)
*facepalm* no. that is not the ideal solution, one should not have to resort to a sleep in order to fix a problem, it has lots of other implications by doing it!


To avoid a flicker or screen tear you should only update changes to the screen. Carelessly redrawing GUIs is what causes flicker, as such to reduce on it you should only draw what is different. The easiest method of doing this is to make a screen buffer that acts between the terminal and your code. It should have a table containing the information about the current screen and then when code attempts to write to it, only actually pass that through to the terminal if it needs to be (i.e. its different to whats currently there)! There are a few other strategies you can add on top of this as well to reduce flicker and tearing, but the one I outlined here is the simplest.

#10 makerimages

  • Members
  • 236 posts

Posted 22 September 2013 - 09:32 AM

why does http://pastebin.com/XJX34S08 say on the registerClick if statement that is is trying to index ?(a number value) ?
this has worked form me earlier, but now it shows this....

#11 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 22 September 2013 - 10:20 AM

For the record, post the full error message, line number and the other APIs, please. So we can actually help.

#12 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 22 September 2013 - 10:21 AM

This line:
event, button, xPos, yPos = os.pullEvent("mouse_click")
is overwriting the button variable with a number (the button number from the mouse_click event).

#13 makerimages

  • Members
  • 236 posts

Posted 22 September 2013 - 10:24 AM

yeah, got it thanks to the IRC again

#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 12:17 PM

Threads merged. Stick to one topic for all questions about a given piece of code.

#15 makerimages

  • Members
  • 236 posts

Posted 23 September 2013 - 09:07 AM

Hi, I have a program
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
os.loadAPI("Apis/windowSystem")
DesignUtil.setDesign("whiteLightGray")
local background,topBar =DesignUtil.getDesign()
local showOS1Menu = false;
local routine=nil;
programCache={}
table.insert(programCache,function() shell.run("OSCore/aboutW.app") end)
function updateCache()
for i=1, #programCache,1 do
  routine=coroutine.create(programCache[i])
  ok, err=coroutine.resume(routine)
end
end

term.setTextColor(colors.gray)
function registerClickEvent(x,y)
if x >=0 and x <=1+3-1 and y >=0 and y <=1+1-1 then
  showOS1Menu= not showOS1Menu
end
end
function printOS1Menu()
if showOS1Menu then
  term.setBackgroundColor(colors.lightGray)
  term.setCursorPos(1,2)
  print("About this device")
  term.setCursorPos(1,3)
  print("Software update  ")
  term.setCursorPos(1,4)
  print("Restart device   ")
  term.setCursorPos(1,5)
  print("Shut down	    ")
end
end
while true do

paintutils.drawImage(background,1,1)
paintutils.drawImage(topBar,1,1)
updateCache()
printOS1Menu()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightGray)
print("O1|")
event, button, xPos, yPos = os.pullEvent("mouse_click")
registerClickEvent(xPos,yPos)
sleep(0.15)
end

which runs this program as a coroutine
os.loadAPI("Apis/windowSystem")
local afData = {}
local x,y=0;
function exit()
print(programCache[1])

end
local SysU=
{
text="Software update";
x=22;
y=7;
W=15;
H=1;
}

function loadAbout()
local abF=fs.open("data/osInfo","r")
local line = abF.readLine()
repeat
  table.insert(afData,line) -- Puts the value of the current line into the table we have.
  line = abF.readLine() -- read the next line
until line == nil -- readLine() returns nil when the end of the file is reached.
abF.close() -- Close up the file ready for use again.
end
loadAbout()
function registerClick(xPos,yPos)
shell.run("OSCore/loginUtil")
if xPos==window.x+window.W+1 and yPos==window.y then
   exit()
  end
if xPos >= SysU.x and xPos <= SysU.x + SysU.W - 1 and yPos >= SysU.y and yPos <=  SysU.y + SysU.H then
   shell.run("OSCore/updateManager.app")
  end
end
local window=windowSystem.createWindow("About this Device",21,3,18,13,true,"Version:"..afData[1].." \n \n \n System: "..afData[2])
while true do
windowSystem.drawWindow(window)
term.setBackgroundColor(colors.white)
 
local event, button, XPos, YPos=os.pullEvent("mouse_click")
	    registerClick(XPos,YPos)
  sleep(0.15)
end

It uses apis:

windowSystem:
local window=nil;
function createWindow(title,x,y,w,h,closeable,text)
window=
{
  titlew=title;
  x=x;
  y=y;
  W=w;
  H=h;
  ca=closeable;
  text=text;
}
return window
end
function drawWindow(windowp)
term.setCursorPos(window.x,window.y)
term.setBackgroundColor(colors.lightGray)
for i=0, windowp.W do
  term.setCursorPos(windowp.x+i,windowp.y)
  print(" ")
  term.setCursorPos(windowp.x+i,windowp.y+windowp.H)
  print(" ")

end
for j=0, windowp.H do
  term.setCursorPos(windowp.x,windowp.y+j)
  print(" ")
  term.setCursorPos(windowp.x+windowp.W,windowp.y+j)
  print(" ")
 
end
  term.setCursorPos(windowp.x+windowp.W/2-string.len(windowp.titlew)/2,windowp.y)
  term.setTextColor(colors.gray)
  print(windowp.titlew)
  if(windowp.ca) then
  
  term.setBackgroundColor(colors.red)
term.setCursorPos(windowp.x+windowp.W+1,windowp.y)
print("X")
term.setBackgroundColor(colors.white)
end
term.setCursorPos(windowp.x+2,windowp.y+2)

for i in string.gmatch(windowp.text,"[^\n]+") do
    print(i)
    x,y=term.getCursorPos()
    if i== " " then
	 term.setCursorPos(windowp.x+1,y)
	  end
end
sleep(0.1)
end

and DesignUtil

local fileColorConfig=nil
local topBar=nil
local backGround=nil

local fileData = {}

function getDesign()

fileColorConfig=fs.open("data/colorCombos/current","r");
local line = fileColorConfig.readLine()
repeat
table.insert(fileData,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()
local fileColorSetting=fs.open("data/colorCombos/"..fileData[1],"r");

local fileDataSetting = {}
local lineSetting = fileColorSetting.readLine()
repeat
table.insert(fileDataSetting,lineSetting)
lineSetting = fileColorSetting.readLine()
until lineSetting == nil
fileColorSetting.close()
if fileDataSetting[1]=="0" then backGround=paintutils.loadImage("graphics/backgrounds/whiteBack.nfp") end
if fileDataSetting[1]=="1" then backGround=paintutils.loadImage("graphics/backgrounds/orangeBack.nfp") end
if fileDataSetting[1]=="2" then backGround=paintutils.loadImage("graphics/backgrounds/darkPinkBack.nfp") end
if fileDataSetting[1]=="3" then backGround=paintutils.loadImage("graphics/backgrounds/lightBlueBack.nfp") end
if fileDataSetting[1]=="4" then backGround=paintutils.loadImage("graphics/backgrounds/goldenBack.nfp") end
if fileDataSetting[1]=="5" then backGround=paintutils.loadImage("graphics/backgrounds/limeBack.nfp") end
if fileDataSetting[1]=="6" then backGround=paintutils.loadImage("graphics/backgrounds/pinkBack.nfp") end
if fileDataSetting[1]=="7" then backGround=paintutils.loadImage("graphics/backgrounds/grayBack.nfp") end
if fileDataSetting[1]=="9" then backGround=paintutils.loadImage("graphics/backgrounds/cyanBack.nfp") end --because 8 is topbar color
if fileDataSetting[1]=="a" then backGround=paintutils.loadImage("graphics/backgrounds/purpleBack.nfp") end
if fileDataSetting[1]=="b" then backGround=paintutils.loadImage("graphics/backgrounds/blueBack.nfp") end
if fileDataSetting[1]=="c" then backGround=paintutils.loadImage("graphics/backgrounds/brownBack.nfp") end
if fileDataSetting[1]=="d" then backGround=paintutils.loadImage("graphics/backgrounds/greenBack.nfp") end
if fileDataSetting[1]=="e" then backGround=paintutils.loadImage("graphics/backgrounds/redBack.nfp") end


if fileDataSetting[2]=="8"then topBar=paintutils.loadImage("graphics/topBars/topBarLightGray.nfp") end
fileColorConfig.close()
return backGround, topBar;
end
function setDesign(design)
fileColorConfig=fs.open("data/colorCombos/current","w");
fileColorConfig.write(design)
fileColorConfig.close()
end

What I would really like is for the coroutine program to be able to register the click and run exit(), not return control back to the program that called it, how could I do this?

#16 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 23 September 2013 - 09:17 AM

Raging.
Spoiler
That's not how coroutines work. Here, have this document about them. You'll know what to do once you understand how to manage coroutines properly.

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 23 September 2013 - 09:29 AM

Threads merged. If I have to do this again for you, you're going on moderator preview.

LBPHacker, use the report button instead of talking about it in a reply.

#18 makerimages

  • Members
  • 236 posts

Posted 23 September 2013 - 11:15 AM

Form what I saw quickly having an initial read on the doc, i could create a separate program, to handle running of coroutines and have that do click checks and pass the x and y to the routine, in coroutine.resume and have actual position check in the routine, could I?

#19 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 23 September 2013 - 11:53 AM

View Postmakerimages, on 23 September 2013 - 11:15 AM, said:

-snip-
Yep. If you manage the coroutines, you yield the coroutine manager as well, so you'll have the event data which you can pass to the coroutines. That means you can process that event data before passing it.

#20 makerimages

  • Members
  • 236 posts

Posted 23 September 2013 - 12:34 PM

brilliant, will try that out asap.

edit:you should put that doc in the tutorials section, it lacks on coroutines, if I get this working, I might write one aswell.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users