Compare commits

...

4 Commits

Author SHA1 Message Date
b305594ea4 improved networking 2024-10-15 01:19:36 +02:00
df7991763d added PaN 2024-10-14 21:42:52 +02:00
e0f8d274e7 renamed Pos and Pos3 2024-10-14 21:40:26 +02:00
08f41ccb0b constrained vec to only accept numbers 2024-10-14 12:11:14 +02:00
30 changed files with 339 additions and 162 deletions

View File

@@ -2,7 +2,7 @@ package bin;
import lib.CLIAppBase; import lib.CLIAppBase;
import kernel.gps.INS; import kernel.gps.INS;
import lib.Pos3; import lib.WorldPos;
import lib.Vec.Vec3; import lib.Vec.Vec3;
using tink.CoreApi; using tink.CoreApi;
@@ -15,7 +15,7 @@ class GPS extends CLIAppBase {
var y:Float = args.getFloat("y"); var y:Float = args.getFloat("y");
var z:Float = args.getFloat("z"); var z:Float = args.getFloat("z");
var pos:Pos3 = new Vec3<Float>(x, y, z); var pos:WorldPos = new Vec3<Float>(x, y, z);
kernel.gps.GPS.setManualPosition(pos); kernel.gps.GPS.setManualPosition(pos);

View File

@@ -1,6 +1,6 @@
package bin.pathfinder; package bin.pathfinder;
import lib.Pos3; import lib.WorldPos;
import lib.ui.elements.IUIElement; import lib.ui.elements.IUIElement;
import lib.ui.elements.TextElement; import lib.ui.elements.TextElement;
import lib.ui.elements.RootElement; import lib.ui.elements.RootElement;
@@ -36,7 +36,7 @@ class PFClient implements IProcess {
private function render() { private function render() {
var acc = kernel.gps.GPS.getAccuracy(); var acc = kernel.gps.GPS.getAccuracy();
var pos:Pos3 = kernel.gps.GPS.getPosition() ?? {x: 0, y: 0, z: 0}; var pos:WorldPos = kernel.gps.GPS.getPosition() ?? {x: 0, y: 0, z: 0};
var childre:Array<IUIElement> = [ var childre:Array<IUIElement> = [
new TextElement('Acc: ${acc}'), new TextElement('Acc: ${acc}'),

View File

@@ -4,7 +4,7 @@ import kernel.turtle.TurtleMutex;
import kernel.turtle.Turtle; import kernel.turtle.Turtle;
import kernel.peripherals.Peripherals.Peripheral; import kernel.peripherals.Peripherals.Peripheral;
import kernel.log.Log; import kernel.log.Log;
import lib.Pos; import lib.ScreenPos;
import cc.HTTP.HTTPResponse; import cc.HTTP.HTTPResponse;
import lua.TableTools; import lua.TableTools;
import lua.Coroutine; import lua.Coroutine;
@@ -38,11 +38,11 @@ class KernelEvents {
distance:Null<Float> distance:Null<Float>
}>; }>;
public static var onMonitorResize(default, null):Signal<String>; public static var onMonitorResize(default, null):Signal<String>;
public static var onMonitorTouch(default, null):Signal<{addr:String, pos:Pos}>; public static var onMonitorTouch(default, null):Signal<{addr:String, pos:ScreenPos}>;
public static var onMouseClick(default, null):Signal<{button:ButtonType, pos:Pos}>; public static var onMouseClick(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public static var onMouseDrag(default, null):Signal<{button:ButtonType, pos:Pos}>; public static var onMouseDrag(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public static var onMouseScroll(default, null):Signal<{dir:Int, pos:Pos}>; public static var onMouseScroll(default, null):Signal<{dir:Int, pos:ScreenPos}>;
public static var onMouseUp(default, null):Signal<{button:ButtonType, pos:Pos}>; public static var onMouseUp(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public static var onPaste(default, null):Signal<String>; public static var onPaste(default, null):Signal<String>;
public static var onPeripheral(default, null):Signal<String>; public static var onPeripheral(default, null):Signal<String>;
public static var onPeripheralDetach(default, null):Signal<String>; public static var onPeripheralDetach(default, null):Signal<String>;
@@ -75,11 +75,11 @@ class KernelEvents {
distance:Null<Float> distance:Null<Float>
}> = Signal.trigger(); }> = Signal.trigger();
private static final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger(); private static final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger();
private static final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger(); private static final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:ScreenPos}> = Signal.trigger();
private static final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); private static final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}> = Signal.trigger();
private static final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); private static final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}> = Signal.trigger();
private static final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}> = Signal.trigger(); private static final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:ScreenPos}> = Signal.trigger();
private static final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); private static final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}> = Signal.trigger();
private static final onPasteTrigger:SignalTrigger<String> = Signal.trigger(); private static final onPasteTrigger:SignalTrigger<String> = Signal.trigger();
private static final onPeripheralTrigger:SignalTrigger<String> = Signal.trigger(); private static final onPeripheralTrigger:SignalTrigger<String> = Signal.trigger();
private static final onPeripheralDetachTrigger:SignalTrigger<String> = Signal.trigger(); private static final onPeripheralDetachTrigger:SignalTrigger<String> = Signal.trigger();

View File

@@ -1,6 +1,6 @@
package kernel; package kernel;
import lib.Pos; import lib.ScreenPos;
import kernel.ui.ITermWriteable; import kernel.ui.ITermWriteable;
import cc.Term; import cc.Term;
import lib.Vec.Vec2; import lib.Vec.Vec2;
@@ -39,7 +39,7 @@ class MainTerm implements ITermWriteable {
Term.scroll(y); Term.scroll(y);
} }
public function getCursorPos():Pos { public function getCursorPos():ScreenPos {
var rtn = Term.getCursorPos(); var rtn = Term.getCursorPos();
return { return {
x: rtn.x - 1, x: rtn.x - 1,
@@ -60,7 +60,7 @@ class MainTerm implements ITermWriteable {
Term.setCursorBlink(blink); Term.setCursorBlink(blink);
} }
public function getSize():Pos { public function getSize():ScreenPos {
var rtn = Term.getSize(); var rtn = Term.getSize();
return { return {
x: rtn.width, x: rtn.width,

View File

@@ -5,7 +5,7 @@ import lib.KVStore;
import kernel.net.Net; import kernel.net.Net;
import kernel.net.INetworkInterface; import kernel.net.INetworkInterface;
import kernel.net.Package; import kernel.net.Package;
import lib.Pos3; import lib.WorldPos;
using tink.CoreApi; using tink.CoreApi;
@@ -18,17 +18,17 @@ class GPS {
private static var shouldRespond = true; private static var shouldRespond = true;
private static var shouldDoWholeNumberCheck = true; private static var shouldDoWholeNumberCheck = true;
private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed) private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
private static var cachedPosition:Pos3; private static var cachedPosition:WorldPos;
private static var lastPositionResponse:Array<{pos:Pos3, dist:Float}> = []; private static var lastPositionResponse:Array<{pos:WorldPos, dist:Float}> = [];
private static var futureResolve:(pos:Null<Pos3>) -> Void = null; private static var futureResolve:(pos:Null<WorldPos>) -> Void = null;
@:allow(kernel.Init) @:allow(kernel.Init)
private static function init() { private static function init() {
loadCachedPosition(); loadCachedPosition();
} }
public static function setManualPosition(pos:Pos3) { public static function setManualPosition(pos:WorldPos) {
var kvstore = new KVStore("gps"); var kvstore = new KVStore("gps");
kvstore.set("mpos", pos); kvstore.set("mpos", pos);
kvstore.save(); kvstore.save();
@@ -40,12 +40,12 @@ class GPS {
} }
@:allow(kernel.gps.INS) @:allow(kernel.gps.INS)
private static function setINSPosition(pos:Pos3) { private static function setINSPosition(pos:WorldPos) {
cachedPosition = pos; cachedPosition = pos;
posAccuracy = 1; posAccuracy = 1;
} }
public static function getPosition():Null<Pos3> { public static function getPosition():Null<WorldPos> {
return cachedPosition; return cachedPosition;
} }
@@ -58,17 +58,17 @@ class GPS {
posAccuracy = 0; posAccuracy = 0;
} }
public static function locate():Future<Null<Pos3>> { public static function locate():Future<Null<WorldPos>> {
// TODO: implenet a timeout // TODO: implenet a timeout
// TODO: dont send a request twice if the last one is still pending or we moved // TODO: dont send a request twice if the last one is still pending or we moved
return new Future<Null<Pos3>>((resolve) -> { return new Future<Null<WorldPos>>((resolve) -> {
futureResolve = resolve; futureResolve = resolve;
sendPositionRequest(); sendPositionRequest();
return null; return null;
}); });
} }
private static function resolveFuture(pos:Null<Pos3>) { private static function resolveFuture(pos:Null<WorldPos>) {
futureResolve(pos); futureResolve(pos);
} }
@@ -85,8 +85,8 @@ class GPS {
var kvstore = new KVStore("gps"); var kvstore = new KVStore("gps");
kvstore.load(); kvstore.load();
var mPos:Null<Pos3> = kvstore.get("mpos"); // Manual set position var mPos:Null<WorldPos> = kvstore.get("mpos"); // Manual set position
var cPos:Null<Pos3> = kvstore.get("cpos"); // Cached position var cPos:Null<WorldPos> = kvstore.get("cpos"); // Cached position
if (mPos != null && cPos != null && mPos == cPos) { if (mPos != null && cPos != null && mPos == cPos) {
// Both are the same, so we can use the cached position // Both are the same, so we can use the cached position
@@ -163,7 +163,7 @@ class GPS {
} }
} }
private static function calculatePosition():Null<Pos3> { private static function calculatePosition():Null<WorldPos> {
if (lastPositionResponse.length < 3) if (lastPositionResponse.length < 3)
return null; return null;
@@ -219,7 +219,7 @@ class GPS {
/** /**
Determines the position(s) of a point given 3 other points and the distance to each of them. Determines the position(s) of a point given 3 other points and the distance to each of them.
**/ **/
private static function trilateration(p1:Pos3, p2:Pos3, p3:Pos3, r1:Float, r2:Float, r3:Float):Pair<Pos3, Pos3> { private static function trilateration(p1:WorldPos, p2:WorldPos, p3:WorldPos, r1:Float, r2:Float, r3:Float):Pair<WorldPos, WorldPos> {
var a2b = p2 - p1; var a2b = p2 - p1;
var a2c = p3 - p1; var a2c = p3 - p1;

View File

@@ -2,12 +2,12 @@ package kernel.gps;
import kernel.log.Log; import kernel.log.Log;
import kernel.turtle.Turtle; import kernel.turtle.Turtle;
import lib.Pos3; import lib.WorldPos;
using tink.CoreApi; using tink.CoreApi;
class INS { class INS {
private static var heading:Null<Pos3> = null; private static var heading:Null<WorldPos> = null;
private static var alingment:Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned private static var alingment:Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
@@ -68,7 +68,7 @@ class INS {
} }
} }
private static function move(dir:Null<Pos3>) { private static function move(dir:Null<WorldPos>) {
var pos = GPS.getPosition(); var pos = GPS.getPosition();
if (pos == null || dir == null) if (pos == null || dir == null)
return; return;
@@ -76,7 +76,7 @@ class INS {
GPS.setINSPosition(newPos); GPS.setINSPosition(newPos);
} }
public static function getHeading():Null<Pos3> { public static function getHeading():Null<WorldPos> {
return heading; return heading;
} }
@@ -150,7 +150,7 @@ class INS {
} }
} }
private static function calcHeading(pos1:Pos3, pos2:Pos3, moved:Int):Null<Pos3> { private static function calcHeading(pos1:WorldPos, pos2:WorldPos, moved:Int):Null<WorldPos> {
if (moved == 0) { if (moved == 0) {
return pos1 - pos2; return pos1 - pos2;
} else if (moved == 1) { } else if (moved == 1) {
@@ -184,7 +184,7 @@ class INS {
} }
} }
private static function rotatePos3ToRight(pos:Pos3):Pos3 { private static function rotatePos3ToRight(pos:WorldPos):WorldPos {
if (pos.x == 0 && pos.z == -1) { if (pos.x == 0 && pos.z == -1) {
return {x: 1, y: 0, z: 0}; return {x: 1, y: 0, z: 0};
} else if (pos.x == -1 && pos.z == 0) { } else if (pos.x == -1 && pos.z == 0) {
@@ -198,7 +198,7 @@ class INS {
} }
} }
private static function rotatePos3ToLeft(pos3:Pos3):Pos3 { private static function rotatePos3ToLeft(pos3:WorldPos):WorldPos {
return rotatePos3ToRight(rotatePos3ToRight(rotatePos3ToRight(pos3))); return rotatePos3ToRight(rotatePos3ToRight(rotatePos3ToRight(pos3)));
} }
} }

View File

@@ -39,6 +39,35 @@ class Net {
setupInterf(interf); setupInterf(interf);
} }
// Handle when a modem has been removed
KernelEvents.onPeripheralDetach.handle((addr) -> {
var idx = interfaces.findIndex((i) -> {
return i.name() == addr;
});
if (idx == -1) {
return;
}
Log.debug('Removed modem on addr: $addr');
interfaces.splice(idx, 1);
Routing.removeInterface(addr);
});
// Handle when a new modem is connected
KernelEvents.onPeripheral.handle((addr) -> {
if (!Peripheral.getTypes(addr).contains("modem")) {
return;
}
Log.debug('Added modem on addr: $addr');
var modem = Peripheral.getModem(addr);
interfaces.push(modem);
setupInterf(modem);
Routing.addInterface(modem);
});
setupPingHandle(); setupPingHandle();
Routing.setup(); Routing.setup();
@@ -62,6 +91,9 @@ class Net {
}); });
} }
/**
Enables the interface to receive messages.
**/
private static function setupInterf(interf:INetworkInterface) { private static function setupInterf(interf:INetworkInterface) {
interf.onMessage.handle(e -> handle(e.pack, interf, e.dist)); interf.onMessage.handle(e -> handle(e.pack, interf, e.dist));
interf.listen(networkID); interf.listen(networkID);
@@ -135,12 +167,13 @@ class Net {
if (!sendRaw(pack)) { if (!sendRaw(pack)) {
// Cant forward // Cant forward
Log.silly('Received a message that could not be forwarded to ${pack.toID}');
} }
} }
public static function respondTo(pack:GenericPackage, data:Dynamic) { public static function respondTo(pack:GenericPackage, data:Dynamic) {
if (pack.type.match(DataNoResponse(_))) { if (!pack.type.match(Data(_))) {
Log.warn("Responed to a no response package. Ignoring"); Log.warn("Responding to a package that does require responding. Ignoring.");
return; return;
} }
@@ -212,6 +245,8 @@ class Net {
if (timeout != null) { if (timeout != null) {
timeout.cancle(); timeout.cancle();
} }
responseBus.remove(pack.msgID);
}); });
timeout = new Timer(MESSAGE_TIMEOUT, () -> { timeout = new Timer(MESSAGE_TIMEOUT, () -> {

View File

@@ -1,6 +1,6 @@
package kernel.net; package kernel.net;
import lib.Pos3; import lib.WorldPos;
typedef NetworkID = Int; typedef NetworkID = Int;
typedef GenericPackage = Package<Dynamic>; typedef GenericPackage = Package<Dynamic>;
@@ -12,7 +12,7 @@ enum PackageTypes {
RouteDiscover(reachableIDs:Array<{id:NetworkID, cost:Int}>); RouteDiscover(reachableIDs:Array<{id:NetworkID, cost:Int}>);
RouteDiscoverResponse(reachableIDs:Array<{id:NetworkID, cost:Int}>); RouteDiscoverResponse(reachableIDs:Array<{id:NetworkID, cost:Int}>);
RouteDiscoverUpdate(reachableIDs:Array<{id:NetworkID, cost:Int}>); RouteDiscoverUpdate(reachableIDs:Array<{id:NetworkID, cost:Int}>);
GPSResponse(pos:Pos3); GPSResponse(pos:WorldPos);
GPSRequest(); GPSRequest();
} }

View File

@@ -74,42 +74,37 @@ class Routing {
private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void { private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void {
addPossibleRoute(pack.fromID, interf, 0, pack.fromID); addPossibleRoute(pack.fromID, interf, 0, pack.fromID);
var shouldRespond:Bool = switch pack.type { switch pack.type {
case RouteDiscoverResponse(routes): case RouteDiscoverResponse(routes):
// A request for routes has been answerd
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
false;
case RouteDiscover(routes): case RouteDiscover(routes):
// Received a request for available routes
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
true;
// Respond to peer
var response:Package<Noise> = {
toID: pack.fromID,
fromID: Net.networkID,
msgID: null,
type: RouteDiscoverResponse(genRouteList()),
data: null,
ttl: 0, // Prevent forwarding
}
interf.send(response.toID, Net.networkID, response);
case RouteDiscoverUpdate(routes): case RouteDiscoverUpdate(routes):
// Received an update of routes
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
false;
default: default:
Log.error("Expected package to be a Route discover package"); Log.silly("Expected package to be a Route discover package");
false;
}; };
if (!shouldRespond) {
return;
}
// Respond to peer
var response:Package<Noise> = {
toID: pack.fromID,
fromID: Net.networkID,
msgID: null,
type: RouteDiscoverResponse(genRouteList()),
data: null,
ttl: 0, // Prevent forwarding
}
interf.send(response.toID, Net.networkID, response);
} }
/** /**
@@ -126,6 +121,9 @@ class Routing {
return routes; return routes;
} }
/**
Create a new packate to brodcast our routes.
**/
private static function newRoutDiscoverPackage():Package<Noise> { private static function newRoutDiscoverPackage():Package<Noise> {
var pack:Package<Noise> = { var pack:Package<Noise> = {
type: RouteDiscover(genRouteList()), type: RouteDiscover(genRouteList()),
@@ -179,4 +177,28 @@ class Routing {
public static function getRouteTable():Map<NetworkID, Route> { public static function getRouteTable():Map<NetworkID, Route> {
return routingTable; return routingTable;
} }
/**
Call when an interface is no longer available.
Will rebrodcast routing requests.
**/
@:allow(kernel.net.Net)
private static function removeInterface(addr:String) {
// Remove the interface from the routing table
for (to => modem in routingTable) {
if (modem.interf.name() == addr) {
routingTable.remove(to);
}
}
// Since some routes to some host could be lost
// we rebrodcast on all interfaces again
brodcastRoutingTable();
}
@:allow(kernel.net.Net)
private static function addInterface(iface:INetworkInterface) {
var pack = newRoutDiscoverPackage();
iface.send(Net.BRODCAST_PORT, Net.networkID, pack);
}
} }

View File

@@ -2,7 +2,7 @@ package kernel.peripherals;
import cc.Peripheral; import cc.Peripheral;
import lib.Rect; import lib.Rect;
import lib.Pos; import lib.ScreenPos;
class Printer implements IPeripheral { class Printer implements IPeripheral {
public static inline final TYPE_NAME:String = "printer"; public static inline final TYPE_NAME:String = "printer";
@@ -25,11 +25,11 @@ class Printer implements IPeripheral {
public function write(text:String) {} public function write(text:String) {}
public function getCurserPos():Pos { public function getCurserPos():ScreenPos {
return new Pos({x: 0, y: 0}); return new ScreenPos({x: 0, y: 0});
} }
public function setCurserPos(pos:Pos) { public function setCurserPos(pos:ScreenPos) {
this.native.setCursorPos(pos.x, pos.y); this.native.setCursorPos(pos.x, pos.y);
} }

View File

@@ -1,7 +1,7 @@
package kernel.peripherals; package kernel.peripherals;
import cc.Peripheral; import cc.Peripheral;
import lib.Pos; import lib.ScreenPos;
import cc.Term.TerminalSize; import cc.Term.TerminalSize;
import kernel.ui.ITermWriteable; import kernel.ui.ITermWriteable;
import lib.Vec.Vec2; import lib.Vec.Vec2;
@@ -59,7 +59,7 @@ class Screen implements ITermWriteable implements IPeripheral {
nativ.scroll(y); nativ.scroll(y);
} }
public function getCursorPos():Pos { public function getCursorPos():ScreenPos {
var rtn = nativ.getCursorPos(); var rtn = nativ.getCursorPos();
return { return {
x: rtn.x - 1, x: rtn.x - 1,

View File

@@ -1,6 +1,6 @@
package kernel.ui; package kernel.ui;
import lib.Pos; import lib.ScreenPos;
import lib.Vec.Vec2; import lib.Vec.Vec2;
import lib.Color; import lib.Color;
import kernel.ui.ITermWriteable; import kernel.ui.ITermWriteable;
@@ -86,7 +86,7 @@ class BufferedVirtualTermWriter implements IVirtualTermWriter extends TermBuffer
super.scroll(y); super.scroll(y);
} }
public override function getCursorPos():Pos { public override function getCursorPos():ScreenPos {
if (isEnabled()) { if (isEnabled()) {
return target.getCursorPos(); return target.getCursorPos();
} else { } else {

View File

@@ -1,6 +1,6 @@
package kernel.ui; package kernel.ui;
import lib.Pos; import lib.ScreenPos;
import lib.Color; import lib.Color;
import lib.Vec.Vec2; import lib.Vec.Vec2;
@@ -18,7 +18,7 @@ interface ITermWriteable {
/** /**
Even though CC is 1 based we use a 0 based index. Even though CC is 1 based we use a 0 based index.
**/ **/
public function getCursorPos():Pos; public function getCursorPos():ScreenPos;
/** /**
Even though CC is 1 based we use a 0 based index. Even though CC is 1 based we use a 0 based index.

View File

@@ -1,7 +1,7 @@
package kernel.ui; package kernel.ui;
import kernel.log.Log; import kernel.log.Log;
import lib.Pos; import lib.ScreenPos;
import lib.Vec.Vec2; import lib.Vec.Vec2;
import lib.Color; import lib.Color;
@@ -110,8 +110,8 @@ class StatelessVirtualTermWriter implements IVirtualTermWriter {
target.scroll(y); target.scroll(y);
} }
public inline function getCursorPos():Pos { public inline function getCursorPos():ScreenPos {
return enabled ? target.getCursorPos() : new Pos({x: 0, y: 0}); return enabled ? target.getCursorPos() : new ScreenPos({x: 0, y: 0});
} }
public inline function setCursorPos(x:Int, y:Int) { public inline function setCursorPos(x:Int, y:Int) {

View File

@@ -1,6 +1,6 @@
package kernel.ui; package kernel.ui;
import lib.Pos; import lib.ScreenPos;
import lib.Vec.Vec2; import lib.Vec.Vec2;
import lib.Color; import lib.Color;
import kernel.ui.ITermWriteable; import kernel.ui.ITermWriteable;
@@ -18,7 +18,7 @@ class TermBuffer implements ITermWriteable {
**/ **/
private var screenBuffer:Array<Array<Pixel>>; private var screenBuffer:Array<Array<Pixel>>;
private var cursorPos:Pos = {x: 0, y: 0}; private var cursorPos:ScreenPos = {x: 0, y: 0};
private var currentTextColor:Color = White; private var currentTextColor:Color = White;
private var currentBgColor:Color = Black; private var currentBgColor:Color = Black;
private var cursorBlink:Bool = false; private var cursorBlink:Bool = false;
@@ -91,7 +91,7 @@ class TermBuffer implements ITermWriteable {
target.setCursorBlink(cursorBlink); target.setCursorBlink(cursorBlink);
} }
private function safeWriteScreenBuffer(pos:Pos, char:String) { private function safeWriteScreenBuffer(pos:ScreenPos, char:String) {
if (screenBuffer.length > pos.y && screenBuffer[pos.y].length > pos.x) { if (screenBuffer.length > pos.y && screenBuffer[pos.y].length > pos.x) {
screenBuffer[pos.y][pos.x].char = char; screenBuffer[pos.y][pos.x].char = char;
screenBuffer[pos.y][pos.x].bg = currentBgColor; screenBuffer[pos.y][pos.x].bg = currentBgColor;
@@ -121,7 +121,7 @@ class TermBuffer implements ITermWriteable {
]); ]);
} }
public function getCursorPos():Pos { public function getCursorPos():ScreenPos {
return cursorPos; return cursorPos;
} }

View File

@@ -1,7 +1,7 @@
package kernel.ui; package kernel.ui;
import lib.ui.rendere.IUIEventDelegate; import lib.ui.rendere.IUIEventDelegate;
import lib.Pos; import lib.ScreenPos;
import lib.Color; import lib.Color;
import kernel.ButtonType; import kernel.ButtonType;
import lib.Vec.Vec2; import lib.Vec.Vec2;
@@ -17,21 +17,21 @@ class WindowContext implements ITermWriteable {
@:allow(kernel.ui.WindowManager) private var eventDelegate:Null<IUIEventDelegate>; @:allow(kernel.ui.WindowManager) private var eventDelegate:Null<IUIEventDelegate>;
public var onClick(default, null):Signal<{button:ButtonType, pos:Pos}>; public var onClick(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public var onKey(default, null):Signal<{keyCode:Int, isHeld:Bool}>; public var onKey(default, null):Signal<{keyCode:Int, isHeld:Bool}>;
public var onKeyUp(default, null):Signal<Int>; public var onKeyUp(default, null):Signal<Int>;
public var onMouseDrag(default, null):Signal<{button:ButtonType, pos:Pos}>; public var onMouseDrag(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public var onMouseScroll(default, null):Signal<{dir:Int, pos:Pos}>; public var onMouseScroll(default, null):Signal<{dir:Int, pos:ScreenPos}>;
public var onMouseUp(default, null):Signal<{button:ButtonType, pos:Pos}>; public var onMouseUp(default, null):Signal<{button:ButtonType, pos:ScreenPos}>;
public var onPaste(default, null):Signal<String>; public var onPaste(default, null):Signal<String>;
public var onChar(default, null):Signal<String>; public var onChar(default, null):Signal<String>;
@:allow(kernel.ui.WindowManager) private final onClickTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>; @:allow(kernel.ui.WindowManager) private final onClickTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}>;
@:allow(kernel.ui.WindowManager) private final onKeyTrigger:SignalTrigger<{keyCode:Int, isHeld:Bool}>; @:allow(kernel.ui.WindowManager) private final onKeyTrigger:SignalTrigger<{keyCode:Int, isHeld:Bool}>;
@:allow(kernel.ui.WindowManager) private final onKeyUpTrigger:SignalTrigger<Int>; @:allow(kernel.ui.WindowManager) private final onKeyUpTrigger:SignalTrigger<Int>;
@:allow(kernel.ui.WindowManager) private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>; @:allow(kernel.ui.WindowManager) private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}>;
@:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}>; @:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:ScreenPos}>;
@:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>; @:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:ScreenPos}>;
@:allow(kernel.ui.WindowManager) private final onPasteTrigger:SignalTrigger<String>; @:allow(kernel.ui.WindowManager) private final onPasteTrigger:SignalTrigger<String>;
@:allow(kernel.ui.WindowManager) private final onCharTrigger:SignalTrigger<String>; @:allow(kernel.ui.WindowManager) private final onCharTrigger:SignalTrigger<String>;
@@ -89,7 +89,7 @@ class WindowContext implements ITermWriteable {
writer.scroll(y); writer.scroll(y);
} }
public inline function getCursorPos():Pos { public inline function getCursorPos():ScreenPos {
return writer.getCursorPos(); return writer.getCursorPos();
} }

103
src/lib/BlockPos.hx Normal file
View File

@@ -0,0 +1,103 @@
package lib;
import lib.Vec.Vec3;
/**
Represents a Point in a 3D `Int` System.
Basicly a wrapper for Vec3<Int> with some extra functions.
`Y` represents the height of the point.
**/
@:forward(x, y, z)
abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
public inline function new(x:Int, y:Int, z:Int) {
this = new Vec3(x, y, z);
}
@:op(A + B)
public function add(rhs:BlockPos):BlockPos {
return {
y: this.y + rhs.y,
x: this.x + rhs.x,
z: this.z + rhs.z
};
}
@:op(A - B)
public function sub(rhs:BlockPos):BlockPos {
return {
y: this.y - rhs.y,
x: this.x - rhs.x,
z: this.z - rhs.z
};
}
@:op(A * B)
public function multiplyScalar(rhs:Int):BlockPos {
return {
y: this.y * rhs,
x: this.x * rhs,
z: this.z * rhs
};
}
@:op(-A)
public function negate():BlockPos {
return {
y: -this.y,
x: -this.x,
z: -this.z
};
}
@:op(A == B)
public function equals(rhs:BlockPos):Bool {
return this.x == rhs.x && this.y == rhs.y && this.z == rhs.z;
}
@:op(A != B)
public function notEquals(rhs:BlockPos):Bool {
return !equals(rhs);
}
public function dot(rhs:BlockPos):Float {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
public function cross(rhs:BlockPos):BlockPos {
return {
x: this.x * rhs.y - this.y * rhs.x,
y: this.y * rhs.z - this.z * rhs.y,
z: this.z * rhs.x - this.x * rhs.z,
};
}
public function length():Float {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
public function hashCode():Int {
return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791);
}
public function toString():String {
return 'BlockPos(${this.x}, ${this.y}, ${this.z})';
}
public function distance(rhs:BlockPos):Float {
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
}
/**
Returns a list of positions neighboring the block. No Diagonal.
**/
public function neighbors():Array<BlockPos> {
return [
new BlockPos(this.x + 1, this.y + 0, this.z + 0), // Front
new BlockPos(this.x - 1, this.y + 0, this.z + 0), // Back
new BlockPos(this.x + 0, this.y + 0, this.z - 1), // Left
new BlockPos(this.x + 0, this.y + 0, this.z + 1), // Right
new BlockPos(this.x + 0, this.y - 1, this.z + 0), // Bot
new BlockPos(this.x + 0, this.y + 1, this.z + 0), // Top
];
}
}

View File

@@ -3,7 +3,8 @@ package lib;
/** /**
Represents an item in the game. Represents an item in the game.
**/ **/
enum abstract Item(String) to String { @:forward
enum abstract Item(PaN) to String {
public inline function new(name:String) { public inline function new(name:String) {
// Check if the name is valid. in the format `mod:item_name` e.g. `minecraft:apple` // Check if the name is valid. in the format `mod:item_name` e.g. `minecraft:apple`
// TODO: implement // TODO: implement
@@ -16,10 +17,6 @@ enum abstract Item(String) to String {
return new Item(s); return new Item(s);
} }
function getBase():String {
return this.split(":")[0];
}
var Coal = "minecraft:coal"; var Coal = "minecraft:coal";
var Charcoal = "minecraft:charcoal"; var Charcoal = "minecraft:charcoal";
} }

18
src/lib/PaN.hx Normal file
View File

@@ -0,0 +1,18 @@
package lib;
/**
Provider and name. e.g. "minecraft:dirt", "mod:item", "minecraft:overworld"
**/
abstract PaN(String) to String from String {
public inline function new(pan:String) {
this = pan;
}
public function getProvider():String {
return this.split(":")[0];
}
public function getName():String {
return this.split(":")[1];
}
}

View File

@@ -1,10 +1,12 @@
package lib; package lib;
class Rect { import kernel.peripherals.Screen;
private final tl:Pos;
private final br:Pos;
public function new(p1:Pos, p2:Pos) { class Rect {
public final tl:ScreenPos;
public final br:ScreenPos;
public function new(p1:ScreenPos, p2:ScreenPos) {
this.tl = { this.tl = {
x: MathI.min(p1.x, p2.x), x: MathI.min(p1.x, p2.x),
y: MathI.min(p1.y, p2.y) y: MathI.min(p1.y, p2.y)
@@ -20,11 +22,11 @@ class Rect {
return getWidth() * getHight(); return getWidth() * getHight();
} }
public function isInside(p:Pos):Bool { public function isInside(p:ScreenPos):Bool {
return (p.x >= tl.x && p.x <= br.x) && (p.y >= tl.y && p.y <= br.y); return (p.x >= tl.x && p.x <= br.x) && (p.y >= tl.y && p.y <= br.y);
} }
public function isOutside(p:Pos):Bool { public function isOutside(p:ScreenPos):Bool {
return !this.isInside(p); return !this.isInside(p);
} }
@@ -36,7 +38,7 @@ class Rect {
return br.x - tl.x; return br.x - tl.x;
} }
public function offset(pos:Pos) { public function offset(pos:ScreenPos) {
tl.x += pos.x; tl.x += pos.x;
tl.y += pos.y; tl.y += pos.y;
br.x += pos.x; br.x += pos.x;

View File

@@ -7,38 +7,38 @@ import lib.Vec.Vec2;
Basicly a wrapper for Vec2<Int> with some extra functions. Basicly a wrapper for Vec2<Int> with some extra functions.
**/ **/
@:forward(x, y) @:forward(x, y)
abstract Pos(Vec2<Int>) from Vec2<Int> to Vec2<Int> { abstract ScreenPos(Vec2<Int>) from Vec2<Int> to Vec2<Int> {
inline public function new(i:Vec2<Int>) { inline public function new(i:Vec2<Int>) {
this = i; this = i;
} }
@:op(A + B) @:op(A + B)
public function add(rhs:Vec2<Int>):Pos { public function add(rhs:Vec2<Int>):ScreenPos {
return new Pos({ return new ScreenPos({
y: this.y + rhs.y, y: this.y + rhs.y,
x: this.x + rhs.x, x: this.x + rhs.x,
}); });
} }
@:op(A - B) @:op(A - B)
public function sub(rhs:Vec2<Int>):Pos { public function sub(rhs:Vec2<Int>):ScreenPos {
return new Pos({ return new ScreenPos({
y: this.y - rhs.y, y: this.y - rhs.y,
x: this.x - rhs.x, x: this.x - rhs.x,
}); });
} }
@:op(A * B) @:op(A * B)
public function multiply(rhs:Vec2<Int>):Pos { public function multiply(rhs:Vec2<Int>):ScreenPos {
return new Pos({ return new ScreenPos({
y: this.y * rhs.y, y: this.y * rhs.y,
x: this.x * rhs.x, x: this.x * rhs.x,
}); });
} }
@:op(-A) @:op(-A)
public function negate():Pos { public function negate():ScreenPos {
return new Pos({ return new ScreenPos({
y: -this.y, y: -this.y,
x: -this.x, x: -this.x,
}); });

View File

@@ -1,6 +1,6 @@
package lib; package lib;
@:structInit class Vec2<T> { @:structInit class Vec2<T:Float> {
public var x:T; public var x:T;
public var y:T; public var y:T;
@@ -10,7 +10,7 @@ package lib;
} }
} }
@:structInit class Vec3<T> { @:structInit class Vec3<T:Float> {
public var x:T; public var x:T;
public var y:T; public var y:T;
public var z:T; public var z:T;

View File

@@ -8,14 +8,14 @@ import lib.Vec.Vec3;
`Y` represents the height of the point. `Y` represents the height of the point.
**/ **/
@:forward(x, y, z) @:forward(x, y, z)
abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> { abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
inline public function new(i:Vec3<Float>) { inline public function new(i:Vec3<Float>) {
this = i; this = i;
} }
@:op(A + B) @:op(A + B)
public function add(rhs:Vec3<Float>):Pos3 { public function add(rhs:Vec3<Float>):WorldPos {
return new Pos3({ return new WorldPos({
y: this.y + rhs.y, y: this.y + rhs.y,
x: this.x + rhs.x, x: this.x + rhs.x,
z: this.z + rhs.z z: this.z + rhs.z
@@ -23,8 +23,8 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
} }
@:op(A - B) @:op(A - B)
public function sub(rhs:Vec3<Float>):Pos3 { public function sub(rhs:Vec3<Float>):WorldPos {
return new Pos3({ return new WorldPos({
y: this.y - rhs.y, y: this.y - rhs.y,
x: this.x - rhs.x, x: this.x - rhs.x,
z: this.z - rhs.z z: this.z - rhs.z
@@ -32,8 +32,8 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
} }
@:op(A * B) @:op(A * B)
public function multiplyScalar(rhs:Float):Pos3 { public function multiplyScalar(rhs:Float):WorldPos {
return new Pos3({ return new WorldPos({
y: this.y * rhs, y: this.y * rhs,
x: this.x * rhs, x: this.x * rhs,
z: this.z * rhs z: this.z * rhs
@@ -41,8 +41,8 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
} }
@:op(A / B) @:op(A / B)
public function divideScalar(rhs:Float):Pos3 { public function divideScalar(rhs:Float):WorldPos {
return new Pos3({ return new WorldPos({
y: this.y / rhs, y: this.y / rhs,
x: this.x / rhs, x: this.x / rhs,
z: this.z / rhs z: this.z / rhs
@@ -50,8 +50,8 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
} }
@:op(-A) @:op(-A)
public function negate():Pos3 { public function negate():WorldPos {
return new Pos3({ return new WorldPos({
y: -this.y, y: -this.y,
x: -this.x, x: -this.x,
z: -this.z z: -this.z
@@ -62,15 +62,15 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z; return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
} }
public function cross(rhs:Vec3<Float>):Pos3 { public function cross(rhs:Vec3<Float>):WorldPos {
return new Pos3({ return new WorldPos({
x: this.y * rhs.z - this.z * rhs.y, x: this.y * rhs.z - this.z * rhs.y,
y: this.z * rhs.x - this.x * rhs.z, y: this.z * rhs.x - this.x * rhs.z,
z: this.x * rhs.y - this.y * rhs.x z: this.x * rhs.y - this.y * rhs.x
}); });
} }
public function normalize():Pos3 { public function normalize():WorldPos {
var l = length(); var l = length();
return multiplyScalar(1 / l); return multiplyScalar(1 / l);
} }
@@ -80,32 +80,32 @@ abstract Pos3(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
} }
@:op(A == B) @:op(A == B)
public function equals(rhs:Pos3):Bool { public function equals(rhs:WorldPos):Bool {
return close(rhs); return close(rhs);
} }
@:op(A != B) @:op(A != B)
public function notEquals(rhs:Pos3):Bool { public function notEquals(rhs:WorldPos):Bool {
return !close(rhs); return !close(rhs);
} }
public function close(rhs:Pos3, epsilon:Float = 0.001):Bool { public function close(rhs:WorldPos, epsilon:Float = 0.001):Bool {
return Math.abs(this.x - rhs.x) < epsilon && Math.abs(this.y - rhs.y) < epsilon && Math.abs(this.z - rhs.z) < epsilon; return Math.abs(this.x - rhs.x) < epsilon && Math.abs(this.y - rhs.y) < epsilon && Math.abs(this.z - rhs.z) < epsilon;
} }
public function toString():String { public function toString():String {
return 'Pos3(${this.x}, ${this.y}, ${this.z})'; return 'WorldPos(${this.x}, ${this.y}, ${this.z})';
} }
public function round():Pos3 { public function round():WorldPos {
return new Pos3({ return new WorldPos({
x: Math.fround(this.x), x: Math.fround(this.x),
y: Math.fround(this.y), y: Math.fround(this.y),
z: Math.fround(this.z) z: Math.fround(this.z)
}); });
} }
public function distance(rhs:Pos3):Float { public function distance(rhs:WorldPos):Float {
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2)); return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
} }
} }

View File

@@ -1,7 +1,7 @@
package lib.ui; package lib.ui;
import kernel.ui.WindowContext; import kernel.ui.WindowContext;
import lib.Pos; import lib.ScreenPos;
import lib.Rect; import lib.Rect;
import kernel.ui.Pixel; import kernel.ui.Pixel;
@@ -10,7 +10,7 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
this = [[]]; this = [[]];
} }
public inline function set(i:Pos, pixel:Pixel) { public inline function set(i:ScreenPos, pixel:Pixel) {
if (this[i.y] == null) { if (this[i.y] == null) {
this[i.y] = []; this[i.y] = [];
} }
@@ -18,7 +18,7 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
this[i.y][i.x] = pixel; this[i.y][i.x] = pixel;
} }
public inline function get(i:Pos):Null<Pixel> { public inline function get(i:ScreenPos):Null<Pixel> {
if (this[i.y] == null) { if (this[i.y] == null) {
return null; return null;
} }
@@ -26,11 +26,11 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
return this[i.y][i.x]; return this[i.y][i.x];
} }
public function keyValueIterator():KeyValueIterator<Pos, Pixel> { public function keyValueIterator():KeyValueIterator<ScreenPos, Pixel> {
return new CanvasKeyValueIterator(this); return new CanvasKeyValueIterator(this);
} }
public function combine(other:Canvas, offset:Pos) { public function combine(other:Canvas, offset:ScreenPos) {
for (key => value in other) { for (key => value in other) {
if (value == null) { if (value == null) {
continue; continue;
@@ -127,8 +127,8 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
class CanvasKeyValueIterator { class CanvasKeyValueIterator {
private final canvas:Array<Array<Pixel>>; private final canvas:Array<Array<Pixel>>;
private var index:Null<Pos> = {x: 0, y: 0}; private var index:Null<ScreenPos> = {x: 0, y: 0};
private var nextIndex:Null<Pos> = null; private var nextIndex:Null<ScreenPos> = null;
@:allow(lib.ui.Canvas) @:allow(lib.ui.Canvas)
private function new(canvas:Array<Array<Pixel>>) { private function new(canvas:Array<Array<Pixel>>) {
@@ -141,7 +141,7 @@ class CanvasKeyValueIterator {
this.nextIndex = nextValidPixel(); this.nextIndex = nextValidPixel();
} }
private function isValidPos(pos:Pos):Bool { private function isValidPos(pos:ScreenPos):Bool {
if (this.canvas[pos.y] == null) { if (this.canvas[pos.y] == null) {
return false; return false;
} }
@@ -157,7 +157,7 @@ class CanvasKeyValueIterator {
return this.index != null; return this.index != null;
} }
private function nextValidPixel():Null<Pos> { private function nextValidPixel():Null<ScreenPos> {
if (this.index == null) { if (this.index == null) {
return null; return null;
} }
@@ -182,7 +182,7 @@ class CanvasKeyValueIterator {
return null; return null;
} }
public function next():{key:Pos, value:Pixel} { public function next():{key:ScreenPos, value:Pixel} {
var rtn = { var rtn = {
key: this.index, key: this.index,
value: this.canvas[this.index.y][this.index.x] value: this.canvas[this.index.y][this.index.x]

View File

@@ -1,17 +1,17 @@
package lib.ui; package lib.ui;
import lib.Pos; import lib.ScreenPos;
import kernel.ButtonType; import kernel.ButtonType;
using tink.CoreApi; using tink.CoreApi;
typedef UIEvents = { typedef UIEvents = {
public var ?onClick:Callback<{button:ButtonType, pos:Pos}>; public var ?onClick:Callback<{button:ButtonType, pos:ScreenPos}>;
public var ?onKey:Callback<{keyCode:Int, isHeld:Bool}>; public var ?onKey:Callback<{keyCode:Int, isHeld:Bool}>;
public var ?onKeyUp:Callback<Int>; public var ?onKeyUp:Callback<Int>;
public var ?onMouseDrag:Callback<{button:ButtonType, pos:Pos}>; public var ?onMouseDrag:Callback<{button:ButtonType, pos:ScreenPos}>;
public var ?onMouseScroll:Callback<{dir:Int, pos:Pos}>; public var ?onMouseScroll:Callback<{dir:Int, pos:ScreenPos}>;
public var ?onMouseUp:Callback<{button:ButtonType, pos:Pos}>; public var ?onMouseUp:Callback<{button:ButtonType, pos:ScreenPos}>;
public var ?onPaste:Callback<String>; public var ?onPaste:Callback<String>;
public var ?onChar:Callback<String>; public var ?onChar:Callback<String>;
} }

View File

@@ -15,7 +15,7 @@ abstract EventMap(Array<{bound:Rect, delegate:IUIEventDelegate}>) {
} }
} }
public function findResponseableDelegate(pos:Pos):IUIEventDelegate { public function findResponseableDelegate(pos:ScreenPos):IUIEventDelegate {
for (i in 0...this.length) { for (i in 0...this.length) {
var newi = (this.length - 1) - i; var newi = (this.length - 1) - i;
var element = this[newi]; var element = this[newi];

View File

@@ -3,5 +3,5 @@ package lib.ui.elements;
import lib.ui.rendere.IUIEventDelegate; import lib.ui.rendere.IUIEventDelegate;
interface IUIElement extends IUIEventDelegate { interface IUIElement extends IUIEventDelegate {
public function render(bounds:Pos):Canvas; public function render(bounds:ScreenPos):Canvas;
} }

View File

@@ -21,15 +21,15 @@ class RootElement implements IUIElement {
this.children = children; this.children = children;
} }
public function render(bounds:Pos):Canvas { public function render(bounds:ScreenPos):Canvas {
var canvas = new Canvas(); var canvas = new Canvas();
var offset = new Pos({x: 0, y: 0}); var offset = new ScreenPos({x: 0, y: 0});
if (hasTitle()) { if (hasTitle()) {
var title = new TextElement(this.title); var title = new TextElement(this.title);
var halfWidth = Math.floor(bounds.x / 2) - Math.floor(this.title.length / 2); var halfWidth = Math.floor(bounds.x / 2) - Math.floor(this.title.length / 2);
canvas.combine(title.render(bounds), {x: halfWidth, y: offset.y}); canvas.combine(title.render(bounds), {x: halfWidth, y: offset.y});
offset = new Pos({x: 0, y: 1}); offset = new ScreenPos({x: 0, y: 1});
} }
this.eventManager.clearMap(); this.eventManager.clearMap();
@@ -42,7 +42,7 @@ class RootElement implements IUIElement {
this.eventManager.addMapElement(child, bounds); this.eventManager.addMapElement(child, bounds);
canvas.combine(childCanvas, offset); canvas.combine(childCanvas, offset);
offset = new Pos({x: 0, y: offset.y + bounds.getHight() + 1}); offset = new ScreenPos({x: 0, y: offset.y + bounds.getHight() + 1});
} }
return canvas; return canvas;

View File

@@ -26,7 +26,7 @@ class TextElement implements IUIElement {
return uiEvents; return uiEvents;
} }
public function render(bounds:Pos):Canvas { public function render(bounds:ScreenPos):Canvas {
var canvas = new Canvas(); var canvas = new Canvas();
var x = 0; var x = 0;

View File

@@ -30,7 +30,7 @@ class UIEventManager implements IUIEventDelegate {
}; };
} }
private function handleClickEvent(params:{button:ButtonType, pos:Pos}) { private function handleClickEvent(params:{button:ButtonType, pos:ScreenPos}) {
var element = this.map.findResponseableDelegate(params.pos); var element = this.map.findResponseableDelegate(params.pos);
if (element == null) { if (element == null) {
return; return;
@@ -48,11 +48,11 @@ class UIEventManager implements IUIEventDelegate {
private function handlePasteEvent(text:String) {} private function handlePasteEvent(text:String) {}
private function handleMouseUpEvent(params:{button:ButtonType, pos:Pos}) {} private function handleMouseUpEvent(params:{button:ButtonType, pos:ScreenPos}) {}
private function handleMouseScrollEvent(params:{dir:Int, pos:Pos}) {} private function handleMouseScrollEvent(params:{dir:Int, pos:ScreenPos}) {}
private function handleMouseDragEvent(params:{button:ButtonType, pos:Pos}) {} private function handleMouseDragEvent(params:{button:ButtonType, pos:ScreenPos}) {}
private function handleKeyEvent(params:{keyCode:Int, isHeld:Bool}) {} private function handleKeyEvent(params:{keyCode:Int, isHeld:Bool}) {}