1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Add term.setColour/term.getColour

This commit is contained in:
Lignum 2017-05-05 15:52:21 +02:00
parent 936a531cd5
commit 56c9dec687
No known key found for this signature in database
GPG Key ID: 0889206F5A8A700D
3 changed files with 106 additions and 33 deletions

View File

@ -29,7 +29,7 @@ public class WidgetTerminal extends Widget
private static final ResourceLocation background = new ResourceLocation( "computercraft", "textures/gui/termBackground.png" );
private static float TERMINATE_TIME = 0.5f;
private IComputerContainer m_computer;
private final IComputerContainer m_computer;
private float m_terminateTimer;
private float m_rebootTimer;
@ -369,26 +369,27 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
int startX = xOrigin + getXPosition();
int startY = yOrigin + getYPosition();
// Draw the screen contents
IComputer computer = m_computer.getComputer();
Terminal terminal = (computer != null) ? computer.getTerminal() : null;
if( terminal != null )
synchronized( m_computer )
{
// Draw the terminal
boolean greyscale = !computer.isColour();
synchronized( terminal )
// Draw the screen contents
IComputer computer = m_computer.getComputer();
Terminal terminal = ( computer != null ) ? computer.getTerminal() : null;
if( terminal != null )
{
// Draw the terminal
boolean greyscale = !computer.isColour();
Palette palette = terminal.getPalette();
// Get the data from the terminal first
// Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us.
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer)ComputerCraft.getFixedWidthFontRenderer();
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer) ComputerCraft.getFixedWidthFontRenderer();
boolean tblink = m_focus && terminal.getCursorBlink() && ComputerCraft.getGlobalCursorBlink();
int tw = terminal.getWidth();
int th = terminal.getHeight();
int tx = terminal.getCursorX();
int ty = terminal.getCursorY();
int x = startX + m_leftMargin;
int y = startY + m_topMargin;
@ -404,9 +405,9 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
}
// Draw lines
for( int line=0; line<th; ++line )
for( int line = 0; line < th; ++line )
{
TextBuffer text = terminal.getLine(line);
TextBuffer text = terminal.getLine( line );
TextBuffer colour = terminal.getTextColourLine( line );
TextBuffer backgroundColour = terminal.getBackgroundColourLine( line );
fontRenderer.drawString( text, x, y, colour, backgroundColour, m_leftMargin, m_rightMargin, greyscale, palette );
@ -428,21 +429,19 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
palette
);
}
}
}
else
{
// Draw a black background
mc.getTextureManager().bindTexture( background );
Colour black = Colour.Black;
GlStateManager.color( black.getR(), black.getG(), black.getB(), 1.0f );
try
} else
{
drawTexturedModalRect( startX, startY, 0, 0, getWidth(), getHeight() );
}
finally
{
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
// Draw a black background
mc.getTextureManager().bindTexture( background );
Colour black = Colour.Black;
GlStateManager.color( black.getR(), black.getG(), black.getB(), 1.0f );
try
{
drawTexturedModalRect( startX, startY, 0, 0, getWidth(), getHeight() );
} finally
{
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
}
}
}
}

View File

@ -10,11 +10,12 @@
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.computer.IComputerEnvironment;
import dan200.computercraft.core.terminal.Terminal;
import org.apache.commons.lang3.ArrayUtils;
public class TermAPI implements ILuaAPI
{
private Terminal m_terminal;
private IComputerEnvironment m_environment;
private final Terminal m_terminal;
private final IComputerEnvironment m_environment;
public TermAPI( IAPIEnvironment _environment )
{
@ -67,13 +68,19 @@ public String[] getMethodNames()
"getTextColor",
"getBackgroundColour",
"getBackgroundColor",
"blit"
"blit",
"setColour",
"setColor",
"getColour",
"getColor",
"resetColour",
"resetColor"
};
}
public static int parseColour( Object[] args, boolean _enableColours ) throws LuaException
{
if( args.length != 1 || args[0] == null || !(args[0] instanceof Double) )
if( args.length < 1 || args[0] == null || !(args[0] instanceof Double) )
{
throw new LuaException( "Expected number" );
}
@ -160,7 +167,7 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
{
throw new LuaException( "Expected boolean" );
}
boolean b = ((Boolean)args[0]).booleanValue();
boolean b = (Boolean) args[ 0 ];
synchronized( m_terminal )
{
m_terminal.setCursorBlink( b );
@ -270,6 +277,55 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
}
return null;
}
case 19:
case 20:
{
// setColour/setColor
if ( args.length < 4 || !(args[0] instanceof Double) || !(args[1] instanceof Double) || !(args[2] instanceof Double) || !(args[3] instanceof Double) ) // toil and trouble
{
throw new LuaException( "Expected number, number, number, number" );
}
if ( !m_environment.isColour() )
{
// Make sure you can't circumvent greyscale terminals with this function.
throw new LuaException( "Colour not supported" );
}
int colour = 15 - parseColour( args, m_environment.isColour() );
float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue();
synchronized( m_terminal )
{
if( m_terminal.getPalette() != null )
{
m_terminal.getPalette().setColour( colour, r, g, b );
}
}
return null;
}
case 21:
case 22:
{
// getColour/getColor
if (args.length < 1 || !(args[0] instanceof Double))
{
throw new LuaException( "Expected number" );
}
int colour = 15 - parseColour( args, m_environment.isColour() );
synchronized( m_terminal )
{
if ( m_terminal.getPalette() != null )
{
return ArrayUtils.toObject( m_terminal.getPalette().getColour64( colour ) );
}
}
return null;
}
default:
{
return null;

View File

@ -68,12 +68,30 @@ public float[] getColour( int i )
return null;
}
public void resetColours()
public double[] getColour64( int i )
{
for(int i = 0; i < Colour.values().length; ++i)
if( i >= 0 && i < colours.length )
{
PaletteColour c = colours[ i ];
return new double[] { (double)c.m_r, (double)c.m_g, (double)c.m_b };
}
return null;
}
public void resetColour( int i )
{
if(i >= 0 && i < colours.length )
{
Colour c = Colour.values()[ i ];
colours[i] = new PaletteColour( c.getR(), c.getG(), c.getB() );
}
}
public void resetColours()
{
for(int i = 0; i < Colour.values().length; ++i)
{
resetColour( i );
}
}
}