1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-30 17:13:20 +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 final ResourceLocation background = new ResourceLocation( "computercraft", "textures/gui/termBackground.png" );
private static float TERMINATE_TIME = 0.5f; private static float TERMINATE_TIME = 0.5f;
private IComputerContainer m_computer; private final IComputerContainer m_computer;
private float m_terminateTimer; private float m_terminateTimer;
private float m_rebootTimer; private float m_rebootTimer;
@ -369,6 +369,8 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
int startX = xOrigin + getXPosition(); int startX = xOrigin + getXPosition();
int startY = yOrigin + getYPosition(); int startY = yOrigin + getYPosition();
synchronized( m_computer )
{
// Draw the screen contents // Draw the screen contents
IComputer computer = m_computer.getComputer(); IComputer computer = m_computer.getComputer();
Terminal terminal = ( computer != null ) ? computer.getTerminal() : null; Terminal terminal = ( computer != null ) ? computer.getTerminal() : null;
@ -376,8 +378,7 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
{ {
// Draw the terminal // Draw the terminal
boolean greyscale = !computer.isColour(); boolean greyscale = !computer.isColour();
synchronized( terminal )
{
Palette palette = terminal.getPalette(); Palette palette = terminal.getPalette();
// Get the data from the terminal first // Get the data from the terminal first
@ -428,9 +429,7 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
palette palette
); );
} }
} } else
}
else
{ {
// Draw a black background // Draw a black background
mc.getTextureManager().bindTexture( background ); mc.getTextureManager().bindTexture( background );
@ -439,13 +438,13 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY
try try
{ {
drawTexturedModalRect( startX, startY, 0, 0, getWidth(), getHeight() ); drawTexturedModalRect( startX, startY, 0, 0, getWidth(), getHeight() );
} } finally
finally
{ {
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f ); GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
} }
} }
} }
}
@Override @Override
public boolean suppressKeyPress( char c, int k ) public boolean suppressKeyPress( char c, int k )

View File

@ -10,11 +10,12 @@
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.computer.IComputerEnvironment; import dan200.computercraft.core.computer.IComputerEnvironment;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import org.apache.commons.lang3.ArrayUtils;
public class TermAPI implements ILuaAPI public class TermAPI implements ILuaAPI
{ {
private Terminal m_terminal; private final Terminal m_terminal;
private IComputerEnvironment m_environment; private final IComputerEnvironment m_environment;
public TermAPI( IAPIEnvironment _environment ) public TermAPI( IAPIEnvironment _environment )
{ {
@ -67,13 +68,19 @@ public String[] getMethodNames()
"getTextColor", "getTextColor",
"getBackgroundColour", "getBackgroundColour",
"getBackgroundColor", "getBackgroundColor",
"blit" "blit",
"setColour",
"setColor",
"getColour",
"getColor",
"resetColour",
"resetColor"
}; };
} }
public static int parseColour( Object[] args, boolean _enableColours ) throws LuaException 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" ); throw new LuaException( "Expected number" );
} }
@ -160,7 +167,7 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
{ {
throw new LuaException( "Expected boolean" ); throw new LuaException( "Expected boolean" );
} }
boolean b = ((Boolean)args[0]).booleanValue(); boolean b = (Boolean) args[ 0 ];
synchronized( m_terminal ) synchronized( m_terminal )
{ {
m_terminal.setCursorBlink( b ); m_terminal.setCursorBlink( b );
@ -270,6 +277,55 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
} }
return null; 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: default:
{ {
return null; return null;

View File

@ -68,12 +68,30 @@ public float[] getColour( int i )
return null; 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 ]; Colour c = Colour.values()[ i ];
colours[i] = new PaletteColour( c.getR(), c.getG(), c.getB() ); colours[i] = new PaletteColour( c.getR(), c.getG(), c.getB() );
} }
} }
public void resetColours()
{
for(int i = 0; i < Colour.values().length; ++i)
{
resetColour( i );
}
}
} }