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

How to make something fade instead of just clear?

Started by PlotTwistGamer, 02 September 2017 - 05:37 PM
PlotTwistGamer #1
Posted 02 September 2017 - 07:37 PM
Is there anyway to make something fade into another program instead of just clear?
KingofGamesYami #2
Posted 02 September 2017 - 08:14 PM
Not easily… it could be done in the latest beta (1.80)* but there are some special cases you would have to consider. Clearing is definitely the way to go.

You obviously couldn't start fading until the program's drawn stuff, but how do you know that? The program would have to signal the fade effect somehow. Also, what do you do in the case of the program drawing on the same pixel multiple times? What if the program has on-screen movement? How does that fading work? What about controls? Is the program going to recieve events during the fade? etc.

*Similar to this effect.
Edited on 02 September 2017 - 06:15 PM
PlotTwistGamer #3
Posted 03 September 2017 - 01:08 AM
Not easily… it could be done in the latest beta (1.80)* but there are some special cases you would have to consider. Clearing is definitely the way to go.

You obviously couldn't start fading until the program's drawn stuff, but how do you know that? The program would have to signal the fade effect somehow. Also, what do you do in the case of the program drawing on the same pixel multiple times? What if the program has on-screen movement? How does that fading work? What about controls? Is the program going to recieve events during the fade? etc.

*Similar to this effect.

Makes sense, do you need me to upload the install for the program Im making to pastebin so you could check it out??
Bomb Bloke #4
Posted 03 September 2017 - 03:59 AM
A direct fade into another script is a bit tricky, but fading the old screen-content to black / white is trivial. You could then additionally draw your script's new content before fading back to a regular palette.

Spoiler
local fadeTime = 1  --# Time to spend fading, in seconds.

--# Record current palette:
local pal = {}
for i = 0, 15 do pal[2^i] = {term.getPaletteColour(2^i)} end

fadeTime = fadeTime * 20 - 0.05

--# Fade to black:
for i = 0, fadeTime do
	local thisCol = 1
	
	for j = 0, 15 do
		local r, g, b = pal[thisCol][1], pal[thisCol][2], pal[thisCol][3]
		term.setPaletteColour(thisCol, r - (r / fadeTime * i), g - (g / fadeTime * i), b - (b / fadeTime * i))
		thisCol = thisCol * 2
	end
	
	sleep(0.05)
end

term.clear()

--# Draw something new here, if you like.

--# Restore original palette here. Either fade back the other way  (repeat the above
--# with "for i = fadeTime, 0, -1 do"), or just snap straight to it with:
for i = 0, 15 do term.setPaletteColour(2^i, table.unpack(pal[2^i])) end
PlotTwistGamer #5
Posted 03 September 2017 - 05:13 PM
A direct fade into another script is a bit tricky, but fading the old screen-content to black / white is trivial. You could then additionally draw your script's new content before fading back to a regular palette.

Spoiler
local fadeTime = 1  --# Time to spend fading, in seconds.

--# Record current palette:
local pal = {}
for i = 0, 15 do pal[2^i] = {term.getPaletteColour(2^i)} end

fadeTime = fadeTime * 20 - 0.05

--# Fade to black:
for i = 0, fadeTime do
	local thisCol = 1
	
	for j = 0, 15 do
		local r, g, b = pal[thisCol][1], pal[thisCol][2], pal[thisCol][3]
		term.setPaletteColour(thisCol, r - (r / fadeTime * i), g - (g / fadeTime * i), b - (b / fadeTime * i))
		thisCol = thisCol * 2
	end
	
	sleep(0.05)
end

term.clear()

--# Draw something new here, if you like.

--# Restore original palette here. Either fade back the other way  (repeat the above
--# with "for i = fadeTime, 0, -1 do"), or just snap straight to it with:
for i = 0, 15 do term.setPaletteColour(2^i, table.unpack(pal[2^i])) end
To be honest I'm still doing "If Statements" and using them for most of the OS Im making. I did end up learning to code buttons. Which was somewhat complicated for me. But thanks for showing me this, I'll for sure use this :3
Dave-ee Jones #6
Posted 04 September 2017 - 01:47 AM
Actually this could be possible however the programs need to be run in a 'wedding cake' formation. E.g. running a base program that modifies the colour palette inbetween programs, making a fade-out/in screen in-between programs.
So it would start to modify the palette (fade-out) when you exit one program, then modify the palette again (fade-in) when the next program is run.

The only inconvenience is having to run the 2 programs above a base program that modifies it, having the other 2 programs running as coroutines. Also the annoyance of having a few seconds' delay between running programs. Would be interesting for a custom shell though.

EDIT: Just saw you mention that you are a bit of a beginner with CC - probably don't try doing this until later on, haha! Coroutines are tricky.
Edited on 03 September 2017 - 11:48 PM
KingofGamesYami #7
Posted 04 September 2017 - 02:54 AM
I tried making a shell-like program using the function provided by BombBloke, but almost every program you'd actually want to consider fading cleans up after itself… For anyone who wants to mess around with what I did, here it is:
Spoiler


local originalPal = {}
for i = 0, 15 do originalPal[2^i] = {term.getPaletteColour(2^i)} end

local function fadeToBlack()
    local fadeTime = 1  --# Time to spend fading, in seconds.

    --# Record current palette:
    local pal = {}
    for i = 0, 15 do pal[2^i] = {term.getPaletteColour(2^i)} end

    fadeTime = fadeTime * 20 - 0.05

    --# Fade to black:
    for i = 0, fadeTime do
            local thisCol = 1

            for j = 0, 15 do
                    local r, g, b = pal[thisCol][1], pal[thisCol][2], pal[thisCol][3]
                    term.setPaletteColour(thisCol, r - (r / fadeTime * i), g - (g / fadeTime * i), b - (b / fadeTime * i))
                    thisCol = thisCol * 2
            end

            sleep(0.05)
    end
    term.setBackgroundColor( colors.black )
    term.clear()
end

local function resetPalette()
    --reset palette
    for i = 0, 15 do term.setPaletteColour(2^i, table.unpack(originalPal[2^i])) end
end

local function fadeInCursor()
    local fadeTime = 20 - 0.05
    term.setPaletteColour( colors.yellow, 0, 0, 0 )
    local r, g, b = table.unpack( originalPal[ colors.yellow ] )
    for i = 0, fadeTime do
        term.setPaletteColour( colors.yellow, r / fadeTime * i, g / fadeTime * i, g / fadeTime * i )
        sleep( 0.05 )
    end
end

term.clear()
term.setCursorPos( 1, 1 )
term.setTextColor( colors.yellow )
term.write( "> " )
term.setTextColor( colors.white )
term.setCursorBlink( true )

	while true do
	local s_input = read()
	local time = os.clock()	
	shell.run( s_input )
	local do_fade = os.clock() - time > 1
	if do_fade then
	    fadeToBlack()
	    term.setCursorPos( 1, 1 )
	end
	term.setTextColor( colors.yellow )
	term.write( "> " )
	term.setTextColor( colors.white )
	if do_fade then
	    fadeInCursor()
	    resetPalette()
	end
	term.setCursorBlink( true )
end

Edit: Apologies for the inconsistent formatting, blame Sublime Text.
Edited on 04 September 2017 - 12:58 AM