Compare commits

...

4 Commits

Author SHA1 Message Date
a75f86cbc1 added redstone interface 2022-03-12 22:03:32 +01:00
35be02dbe2 changed how we handle colors 2022-03-12 21:48:09 +01:00
aee8c0fd3c polyfill exit fix 2022-03-12 21:45:31 +01:00
f3141a0f99 use ReadOnlyArray where it makes sense 2022-03-12 17:23:09 +01:00
14 changed files with 136 additions and 140 deletions

View File

@@ -60,7 +60,7 @@ class Log {
}
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("SILY",pos,msg), LightGrey);
writer.writeLn(logLine("SILY",pos,msg), LightGray);
#if webconsole
Debug.printWeb(logLine("SILY",pos,msg));
#end

View File

@@ -77,19 +77,19 @@ class MainTerm implements TermWriteable {
}
public function getTextColor():Color {
return ColorConvert.ccToColor(Term.getTextColor());
return Term.getTextColor();
}
public function setTextColor(colour:Color) {
Term.setTextColor(ColorConvert.colorToCC(colour));
public function setTextColor(color:Color) {
Term.setTextColor(color);
}
public function getBackgroundColor():Color {
return ColorConvert.ccToColor(Term.getBackgroundColor());
return Term.getBackgroundColor();
}
public function setBackgroundColor(color:Color) {
Term.setBackgroundColor(ColorConvert.colorToCC(color));
Term.setBackgroundColor(color);
}
public function isColor():Bool {

View File

@@ -1,5 +1,6 @@
package kernel.fs;
import haxe.ds.ReadOnlyArray;
import kernel.fs.FileHandler.WriteBinaryHandle;
import kernel.fs.FileHandler.ReadBinaryHandle;
import kernel.fs.FileHandler.WriteHandle;
@@ -12,7 +13,7 @@ using lua.Table;
Wrapper to interact with the filesystem.
**/
class FS {
public static inline function list(path: String):Array<String> {
public static inline function list(path: String):ReadOnlyArray<String> {
return FileSystem.list(path).toArray();
}
@@ -84,7 +85,7 @@ class FS {
return FileSystem.open(path,BinaryAppend);
}
public static inline function find(pattern: String):Array<String> {
public static inline function find(pattern: String):ReadOnlyArray<String> {
return FileSystem.find(pattern).toArray();
}

View File

@@ -1,5 +1,6 @@
package kernel.peripherals;
import haxe.ds.ReadOnlyArray;
import kernel.peripherals.Modem;
import kernel.peripherals.Screen;
@@ -19,12 +20,15 @@ class Peripheral {
private var screens: Array<Screen> = [];
private var modes: Array<Modem> = [];
private var drives:Array<Drive> = [];
private var redstone:Array<Redstone> = [];
@:allow(kernel.Init)
private function new() {
KernelEvents.instance.onPeripheral.handle(this.updatePeripherals);
KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals);
updatePeripherals();
redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ];
}
private function updatePeripherals() {
@@ -36,7 +40,7 @@ class Peripheral {
/**
Get all connected screens.
**/
public function getScreens():Array<Screen> {
public function getScreens():ReadOnlyArray<Screen> {
return this.screens;
}
@@ -52,7 +56,7 @@ class Peripheral {
/**
Get all connected modems.
**/
public function getModems():Array<Modem> {
public function getModems():ReadOnlyArray<Modem> {
return this.modes;
}
@@ -68,7 +72,7 @@ class Peripheral {
/**
Get all connected drives.
**/
public function getDrives(): Array<Drive> {
public function getDrives(): ReadOnlyArray<Drive> {
return this.drives;
}
@@ -80,4 +84,8 @@ class Peripheral {
var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive");
this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s));
}
public function getRedstone(side: String): Redstone {
return this.redstone.find(item -> item.getAddr() == side);
}
}

View File

@@ -0,0 +1,60 @@
package kernel.peripherals;
import util.Color;
typedef BundleMask = Color;
class Redstone implements IPeripheral {
private final addr:Side;
@:allow(kernel.peripherals)
private function new(side: Side) {
this.addr = side;
}
public function getAddr():String {
return this.addr;
}
public inline function setOutput(on:Bool):Void {
cc.Redstone.setOutput(this.addr,on);
}
public inline function getOutput(): Bool {
return cc.Redstone.getOutput(this.addr);
}
public inline function getInput(): Bool {
return cc.Redstone.getInput(this.addr);
}
public inline function setAnalogOutput(strength:Int): Void {
cc.Redstone.setAnalogOutput(this.addr,strength);
}
public inline function getAnalogOutput(): Int {
return cc.Redstone.getAnalogOutput(this.addr);
}
public inline function getAnalogInput(): Int {
return cc.Redstone.getAnalogInput(this.addr);
}
public inline function setBundledOutput(output: BundleMask) {
cc.Redstone.setBundledOutput(this.addr,output);
}
public inline function getBundledOutput():BundleMask {
return cc.Redstone.getBundledOutput(this.addr);
}
public inline function getBundledInput():BundleMask {
return cc.Redstone.getBundledInput(this.addr);
}
public inline function testBundledInput(mask: Color): Bool {
return cc.Redstone.testBundledInput(this.addr,mask);
}
}

View File

@@ -90,19 +90,19 @@ class Screen implements TermWriteable implements IPeripheral {
}
public function getTextColor():Color {
return ColorConvert.ccToColor(nativ.getTextColor());
return nativ.getTextColor();
}
public function setTextColor(colour:Color) {
nativ.setTextColor(ColorConvert.colorToCC(colour));
public function setTextColor(color:Color) {
nativ.setTextColor(color);
}
public function getBackgroundColor():Color {
return ColorConvert.ccToColor(nativ.getBackgroundColor());
return nativ.getBackgroundColor();
}
public function setBackgroundColor(color:Color) {
nativ.setBackgroundColor(ColorConvert.colorToCC(color));
nativ.setBackgroundColor(color);
}
public function isColor():Bool {

View File

@@ -0,0 +1,10 @@
package kernel.peripherals;
enum abstract Side(String) to String {
var Top = "top";
var Bottom = "bottom";
var Left = "left";
var Right = "right";
var Front = "front";
var Back = "back";
}

View File

@@ -158,8 +158,8 @@ class TermBuffer implements TermWriteable {
return currentTextColor;
}
public function setTextColor(colour:Color) {
currentTextColor = colour;
public function setTextColor(color:Color) {
currentTextColor = color;
}
public function getBackgroundColor():Color {

View File

@@ -31,12 +31,12 @@ interface TermWriteable {
public function clear():Void;
public function clearLine():Void;
public function getTextColor():Color;
public function setTextColor(colour:Color):Void;
public function setTextColor(color:Color):Void;
public function getBackgroundColor():Color;
public function setBackgroundColor(color:Color):Void;
public function isColor():Bool;
// public function setPaletteColor(...);
// public function getPaletteColor(colour);
// public function getPaletteColor(color);
/**
Clears the screen, resetes the courser to (0,0) and resetes the color to Black and White.

View File

@@ -150,12 +150,12 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer {
return super.getTextColor();
}
public override function setTextColor(colour:Color) {
public override function setTextColor(color:Color) {
if (isEnabled()) {
target.setTextColor(colour);
target.setTextColor(color);
}
super.setTextColor(colour);
super.setTextColor(color);
}
public override function getBackgroundColor():Color {

View File

@@ -114,8 +114,8 @@ class WindowContext implements TermWriteable {
return writer.getTextColor();
}
public function setTextColor(colour:Color) {
writer.setTextColor(colour);
public function setTextColor(color:Color) {
writer.setTextColor(color);
}
public function getBackgroundColor():Color {

View File

@@ -1,5 +1,6 @@
package kernel.ui;
import haxe.ds.ReadOnlyArray;
import kernel.ui.TermWriteable;
import kernel.peripherals.Peripherals.Peripheral;
@@ -73,7 +74,7 @@ class WindowManager {
return newContext;
}
public function getOutputs():Array<String> {
public function getOutputs():ReadOnlyArray<String> {
var arr = Peripheral.instance.getScreens().map(screen -> return screen.getAddr());
arr.push("main");
return arr;

View File

@@ -13,7 +13,7 @@ _G.require = function (module)
end
else
print("ERROR: Tell a dev to implement: " .. i)
os.exit()
error("ERROR: Tell a dev to implement: " .. i)
end
end
}
@@ -21,7 +21,7 @@ _G.require = function (module)
return module
end
print("ERROR: Tell a dev to implement: " .. module)
os.exit()
error("ERROR: Tell a dev to implement: " .. module)
end
-- Polyfill bit. see: http://bitop.luajit.org/api.html
@@ -36,23 +36,23 @@ end
bit.tobit = function(x)
print("ERROR: Tell a dev to implement: bit.tobi")
os.exit()
error()
end
bit.tohex = function(x,n)
print("ERROR: Tell a dev to implement: bit.tohex")
os.exit()
error()
end
bit.lshift = function(x,n)
print("ERROR: Tell a dev to implement: bit.lshift")
os.exit()
error()
end
bit.rshift = function(x,n)
print("ERROR: Tell a dev to implement: bit.rshift")
os.exit()
error()
end
bit.arshift = function(x,n)
@@ -62,15 +62,15 @@ end
bit.rol = function(x,n)
print("ERROR: Tell a dev to implement: bit.rol")
os.exit()
error()
end
bit.ror = function(x,n)
print("ERROR: Tell a dev to implement: bit.ror")
os.exit()
error()
end
bit.bswap = function(x)
print("ERROR: Tell a dev to implement: bit.bswap")
os.exit()
error()
end

View File

@@ -1,111 +1,27 @@
package util;
import haxe.Exception;
import cc.Colors;
import kernel.peripherals.Redstone.BundleMask;
enum Color {
White;
Orange;
Magenta;
LightBlue;
Yellow;
Lime;
Pink;
Gray;
Grey;
LightGray;
LightGrey;
Cyan;
Purple;
Blue;
Brown;
Green;
Red;
Black;
}
enum abstract Color(Int) from cc.Colors.Color to cc.Colors.Color {
var White = 0x1;
var Orange = 0x2;
var Magenta = 0x4;
var LightBlue = 0x8;
var Yellow = 0x10;
var Lime = 0x20;
var Pink = 0x40;
var Gray = 0x80;
var LightGray = 0x100;
var Cyan = 0x200;
var Purple = 0x400;
var Blue = 0x800;
var Brown = 0x1000;
var Green = 0x2000;
var Red = 0x4000;
var Black = 0x8000;
class ColorConvert {
public static function colorToCC(color:Color):cc.Colors.Color {
switch color {
case White:
return Colors.white;
case Orange:
return Colors.orange;
case Magenta:
return Colors.magenta;
case LightBlue:
return Colors.lightBlue;
case Yellow:
return Colors.yellow;
case Lime:
return Colors.lime;
case Pink:
return Colors.pink;
case Gray:
return Colors.gray;
case Grey:
return Colors.grey;
case LightGray:
return Colors.lightGray;
case LightGrey:
return Colors.lightGrey;
case Cyan:
return Colors.cyan;
case Purple:
return Colors.purple;
case Blue:
return Colors.blue;
case Brown:
return Colors.brown;
case Green:
return Colors.green;
case Red:
return Colors.red;
case Black:
return Colors.black;
};
}
public static function ccToColor(color:cc.Colors.Color):Color {
switch color {
case 0:
return White;
case 1:
return Orange;
case 2:
return Magenta;
case 3:
return LightBlue;
case 4:
return Yellow;
case 5:
return Lime;
case 6:
return Pink;
case 7:
return Gray;
case 8:
return Grey;
case 9:
return LightGray;
case 10:
return LightGrey;
case 11:
return Cyan;
case 12:
return Purple;
case 13:
return Blue;
case 14:
return Brown;
case 15:
return Green;
case 16:
return Red;
case 17:
return Black;
case _:
throw new Exception("Invalid input");
}
@:op(A + B)
public inline function combine(rhs: Color):BundleMask {
return this + rhs;
}
}