diff --git a/src/main/java/dan200/computercraft/core/apis/TermAPI.java b/src/main/java/dan200/computercraft/core/apis/TermAPI.java index 8a655c9a0..639f13aeb 100644 --- a/src/main/java/dan200/computercraft/core/apis/TermAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/TermAPI.java @@ -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 colours) throws LuaException + { + final double lg2 = Math.log( 2 ); + + for(Map.Entry 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 colour = (HashMap) 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" ); } - 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(args.length >= 1 && args[0] instanceof HashMap) { - if( m_terminal.getPalette() != null ) - { - m_terminal.getPalette().setColour( colour, r, g, b ); - m_terminal.setChanged(); - } + @SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :( + HashMap colourTbl = (HashMap)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" ); + } + return null; } case 21: diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorPeripheral.java index 21a48ec38..78ef373d9 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorPeripheral.java @@ -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" ); } - 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 ) + if(args.length >= 1 && args[0] instanceof HashMap ) { - palette.setColour( colour, r, g, b ); + @SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :( + HashMap colourTbl = (HashMap)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(); + 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: diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/window b/src/main/resources/assets/computercraft/lua/rom/apis/window index bd5c61945..c1b7c7594 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/window +++ b/src/main/resources/assets/computercraft/lua/rom/apis/window @@ -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 ) - local tCol = tPalette[ colour ] - tCol[1] = r - tCol[2] = g - tCol[3] = 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()