1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-15 14:24:55 +00:00

Add setColour overload with table parameter

This commit is contained in:
Lignum 2017-05-06 20:22:16 +02:00
parent bfa5f6ec9c
commit ac2382a861
No known key found for this signature in database
GPG Key ID: 0889206F5A8A700D
3 changed files with 92 additions and 38 deletions

View File

@ -10,8 +10,13 @@ import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.computer.IComputerEnvironment;
import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.util.Palette;
import org.apache.commons.lang3.ArrayUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TermAPI implements ILuaAPI
{
private final Terminal m_terminal;
@ -106,6 +111,46 @@ public class TermAPI implements ILuaAPI
};
}
public static void setColour( Terminal terminal, int colour, float r, float g, float b )
{
if( terminal.getPalette() != null )
{
terminal.getPalette().setColour( colour, r, g, b );
terminal.setChanged();
}
}
public static void setColour( Terminal terminal, HashMap<Object, Object> colours) throws LuaException
{
final double lg2 = Math.log( 2 );
for(Map.Entry<Object, Object> e : colours.entrySet())
{
if(e.getKey() instanceof Double)
{
int index = 15 - (int)( Math.log( (Double)e.getKey() ) / lg2 );
try
{
@SuppressWarnings({ "unchecked" }) // There isn't really a nice way around this :(
HashMap<Object, Object> colour = (HashMap<Object, Object>) e.getValue();
setColour(
terminal,
index,
( (Double)colour.get( 1.0 ) ).floatValue(),
( (Double)colour.get( 2.0 ) ).floatValue(),
( (Double)colour.get( 3.0 ) ).floatValue()
);
}
catch(ClassCastException cce)
{
throw new LuaException( "Malformed colour table" );
}
}
}
}
@Override
public Object[] callMethod( ILuaContext context, int method, Object[] args ) throws LuaException
{
@ -279,30 +324,31 @@ public class TermAPI implements ILuaAPI
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" );
}
if(args.length >= 1 && args[0] instanceof HashMap)
{
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
setColour( m_terminal, colourTbl );
}
else if (args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
{
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();
setColour( m_terminal, colour, r, g, b );
}
else
{
throw new LuaException( "Expected table or number, number, number, number" );
}
synchronized( m_terminal )
{
if( m_terminal.getPalette() != null )
{
m_terminal.getPalette().setColour( colour, r, g, b );
m_terminal.setChanged();
}
}
return null;
}
case 21:

View File

@ -14,6 +14,8 @@ import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.util.Palette;
import org.apache.commons.lang3.ArrayUtils;
import java.util.HashMap;
public class MonitorPeripheral implements IPeripheral
{
private final TileMonitor m_monitor;
@ -229,14 +231,6 @@ public class MonitorPeripheral implements IPeripheral
{
// setColour/setColor
Terminal terminal = m_monitor.getTerminal().getTerminal();
Palette palette = terminal.getPalette();
// setColour/setColor
if( args.length < 4 || !(args[0] instanceof Double) || !(args[1] instanceof Double) || !(args[2] instanceof Double) || !(args[3] instanceof Double) )
{
throw new LuaException( "Expected number, number, number, number" );
}
boolean isColour = m_monitor.getTerminal().isColour();
if( !isColour )
@ -244,15 +238,25 @@ public class MonitorPeripheral implements IPeripheral
throw new LuaException( "Colour not supported" );
}
if(args.length >= 1 && args[0] instanceof HashMap )
{
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colourTbl );
}
else if (args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
{
int colour = 15 - dan200.computercraft.core.apis.TermAPI.parseColour( args, true );
float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue();
if( palette != null )
{
palette.setColour( colour, r, g, b );
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colour, r, g, b );
}
else
{
throw new LuaException( "Expected table or number, number, number, number" );
}
return null;
}
case 22:

View File

@ -107,9 +107,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
local function updatePalette()
for k,v in pairs(tPalette) do
parent.setColour( k, table.unpack( v ) )
end
return parent.setColour( tPalette )
end
local function internalBlit( sText, sTextColor, sBackgroundColor )
@ -289,10 +287,16 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
function window.setColour( colour, r, g, b )
if type(colour) == "table" then
for k,v in pairs(colour) do
tPalette[k] = v
end
else
local tCol = tPalette[ colour ]
tCol[1] = r
tCol[2] = g
tCol[3] = b
end
if bVisible then
return updatePalette()