use Pos instead of Vec2<Int>

This commit is contained in:
Djeeberjr 2022-03-08 13:33:40 +01:00
parent edc3195192
commit de35f34173
13 changed files with 59 additions and 45 deletions

View File

@ -1,5 +1,6 @@
package kernel; package kernel;
import util.Pos;
import cc.HTTP.HTTPResponse; import cc.HTTP.HTTPResponse;
import lua.TableTools; import lua.TableTools;
import lua.Coroutine; import lua.Coroutine;
@ -35,11 +36,11 @@ class KernelEvents {
distance:Int distance:Int
}>; }>;
public final onMonitorResize:Signal<String>; public final onMonitorResize:Signal<String>;
public final onMonitorTouch:Signal<{addr:String, pos:Vec2<Int>}>; public final onMonitorTouch:Signal<{addr:String, pos:Pos}>;
public final onMouseClick:Signal<{button:ButtonType, pos:Vec2<Int>}>; public final onMouseClick:Signal<{button:ButtonType, pos:Pos}>;
public final onMouseDrag:Signal<{button:ButtonType, pos:Vec2<Int>}>; public final onMouseDrag:Signal<{button:ButtonType, pos:Pos}>;
public final onMouseScroll:Signal<{dir:Int, pos:Vec2<Int>}>; public final onMouseScroll:Signal<{dir:Int, pos:Pos}>;
public final onMouseUp:Signal<{button:ButtonType, pos:Vec2<Int>}>; public final onMouseUp:Signal<{button:ButtonType, pos:Pos}>;
public final onPaste:Signal<String>; public final onPaste:Signal<String>;
public final onPeripheral:Signal<String>; public final onPeripheral:Signal<String>;
public final onPeripheralDetach:Signal<String>; public final onPeripheralDetach:Signal<String>;
@ -73,11 +74,11 @@ class KernelEvents {
distance:Int distance:Int
}> = Signal.trigger(); }> = Signal.trigger();
private final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger(); private final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger();
private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Vec2<Int>}> = Signal.trigger(); private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger();
private final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger(); private final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger();
private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger(); private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger();
private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Vec2<Int>}> = Signal.trigger(); private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}> = Signal.trigger();
private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger(); private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger();
private final onPasteTrigger:SignalTrigger<String> = Signal.trigger(); private final onPasteTrigger:SignalTrigger<String> = Signal.trigger();
private final onPeripheralTrigger:SignalTrigger<String> = Signal.trigger(); private final onPeripheralTrigger:SignalTrigger<String> = Signal.trigger();
private final onPeripheralDetachTrigger:SignalTrigger<String> = Signal.trigger(); private final onPeripheralDetachTrigger:SignalTrigger<String> = Signal.trigger();

View File

@ -1,5 +1,6 @@
package kernel; package kernel;
import util.Pos;
using tink.CoreApi; using tink.CoreApi;
import kernel.ui.TermWriteable; import kernel.ui.TermWriteable;
@ -38,7 +39,7 @@ class MainTerm implements TermWriteable {
Term.scroll(y); Term.scroll(y);
} }
public function getCursorPos():Vec2<Int> { public function getCursorPos():Pos {
var rtn = Term.getCursorPos(); var rtn = Term.getCursorPos();
return { return {
x: rtn.x - 1, x: rtn.x - 1,
@ -59,7 +60,7 @@ class MainTerm implements TermWriteable {
Term.setCursorBlink(blink); Term.setCursorBlink(blink);
} }
public function getSize():Vec2<Int> { public function getSize():Pos {
var rtn = Term.getSize(); var rtn = Term.getSize();
return { return {
x: rtn.width, x: rtn.width,

View File

@ -1,6 +1,7 @@
package kernel.peripherals; package kernel.peripherals;
import util.Pos;
import cc.Term.TerminalSize; import cc.Term.TerminalSize;
import kernel.ui.TermWriteable; import kernel.ui.TermWriteable;
import util.Vec.Vec2; import util.Vec.Vec2;
@ -52,7 +53,7 @@ class Screen implements TermWriteable implements IPeripheral {
nativ.scroll(y); nativ.scroll(y);
} }
public function getCursorPos():Vec2<Int> { public function getCursorPos():Pos {
var rtn = nativ.getCursorPos(); var rtn = nativ.getCursorPos();
return { return {
x: rtn.x - 1, x: rtn.x - 1,

View File

@ -1,6 +1,7 @@
package kernel.ui; package kernel.ui;
import util.Pos;
import util.Vec.Vec2; import util.Vec.Vec2;
import util.Color; import util.Color;
import kernel.ui.TermWriteable; import kernel.ui.TermWriteable;
@ -18,7 +19,7 @@ class TermBuffer implements TermWriteable {
**/ **/
private var screenBuffer:Array<Array<Pixel>>; private var screenBuffer:Array<Array<Pixel>>;
private var cursorPos:Vec2<Int> = {x: 0, y: 0}; private var cursorPos:Pos = {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;
@ -90,7 +91,7 @@ class TermBuffer implements TermWriteable {
target.setBackgroundColor(currentBgColor); target.setBackgroundColor(currentBgColor);
} }
private function safeWriteScreenBuffer(pos:Vec2<Int>, char:String) { private function safeWriteScreenBuffer(pos:Pos, 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;
@ -120,7 +121,7 @@ class TermBuffer implements TermWriteable {
]); ]);
} }
public function getCursorPos():Vec2<Int> { public function getCursorPos():Pos {
return cursorPos; return cursorPos;
} }

View File

@ -1,5 +1,6 @@
package kernel.ui; package kernel.ui;
import util.Pos;
using tink.CoreApi; using tink.CoreApi;
import util.Color; import util.Color;
@ -17,7 +18,7 @@ interface TermWriteable {
/** /**
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():Vec2<Int>; public function getCursorPos():Pos;
/** /**
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,5 +1,6 @@
package kernel.ui; package kernel.ui;
import util.Pos;
using tink.CoreApi; using tink.CoreApi;
import util.Vec.Vec2; import util.Vec.Vec2;
@ -85,7 +86,7 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer {
super.scroll(y); super.scroll(y);
} }
public override function getCursorPos():Vec2<Int> { public override function getCursorPos():Pos {
if (isEnabled()) { if (isEnabled()) {
return target.getCursorPos(); return target.getCursorPos();
} else { } else {

View File

@ -1,5 +1,6 @@
package kernel.ui; package kernel.ui;
import util.Pos;
using tink.CoreApi; using tink.CoreApi;
import util.Color; import util.Color;
@ -13,20 +14,20 @@ import kernel.ui.TermWriteable;
class WindowContext implements TermWriteable { class WindowContext implements TermWriteable {
private final writer:VirtualTermWriter; private final writer:VirtualTermWriter;
public var onClick(default, null):Signal<{button:ButtonType, pos:Vec2<Int>}>; public var onClick(default, null):Signal<{button:ButtonType, pos:Pos}>;
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:Vec2<Int>}>; public var onMouseDrag(default, null):Signal<{button:ButtonType, pos:Pos}>;
public var onMouseScroll(default, null):Signal<{dir:Int, pos:Vec2<Int>}>; public var onMouseScroll(default, null):Signal<{dir:Int, pos:Pos}>;
public var onMouseUp(default, null):Signal<{button:ButtonType, pos:Vec2<Int>}>; public var onMouseUp(default, null):Signal<{button:ButtonType, pos:Pos}>;
public var onPaste(default, null):Signal<String>; public var onPaste(default, null):Signal<String>;
@:allow(kernel.ui.WindowManager) private final onClickTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}>; @:allow(kernel.ui.WindowManager) private final onClickTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>;
@: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:Vec2<Int>}>; @:allow(kernel.ui.WindowManager) private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>;
@:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Vec2<Int>}>; @:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}>;
@:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}>; @:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>;
@:allow(kernel.ui.WindowManager) private final onPasteTrigger:SignalTrigger<String>; @:allow(kernel.ui.WindowManager) private final onPasteTrigger:SignalTrigger<String>;
@:allow(kernel.ui.WindowManager) @:allow(kernel.ui.WindowManager)
@ -81,7 +82,7 @@ class WindowContext implements TermWriteable {
writer.scroll(y); writer.scroll(y);
} }
public function getCursorPos():Vec2<Int> { public function getCursorPos():Pos {
return writer.getCursorPos(); return writer.getCursorPos();
} }

View File

@ -1,5 +1,6 @@
package lib.ui; package lib.ui;
import util.Pos;
import util.Rect; import util.Rect;
import util.Vec.Vec2; import util.Vec.Vec2;
import kernel.ui.Pixel; import kernel.ui.Pixel;
@ -9,7 +10,7 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> {
this = [[]]; this = [[]];
} }
public inline function set(i:Vec2<Int>, pixel:Pixel) { public inline function set(i:Pos, pixel:Pixel) {
if (this[i.y] == null) { if (this[i.y] == null) {
this[i.y] = []; this[i.y] = [];
} }
@ -17,15 +18,15 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> {
this[i.y][i.x] = pixel; this[i.y][i.x] = pixel;
} }
public inline function get(i:Vec2<Int>):Pixel { public inline function get(i:Pos):Pixel {
return this[i.y][i.x]; return this[i.y][i.x];
} }
public function keyValueIterator():KeyValueIterator<Vec2<Int>, Pixel> { public function keyValueIterator():KeyValueIterator<Pos, Pixel> {
return new CanvasKeyValueIterator(this); return new CanvasKeyValueIterator(this);
} }
public function combine(other:Canvas, offset:Vec2<Int>) { public function combine(other:Canvas, offset:Pos) {
for (key => value in other) { for (key => value in other) {
if (value == null) { if (value == null) {
continue; continue;
@ -67,7 +68,7 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> {
class CanvasKeyValueIterator { class CanvasKeyValueIterator {
private final canvas:Array<Array<Pixel>>; private final canvas:Array<Array<Pixel>>;
private var index:Vec2<Int> = {x: 0, y: 0}; private var index:Pos = {x: 0, y: 0};
@:allow(lib.ui.Canvas) @:allow(lib.ui.Canvas)
private function new(canvas:Array<Array<Pixel>>) { private function new(canvas:Array<Array<Pixel>>) {
@ -78,8 +79,8 @@ class CanvasKeyValueIterator {
return index.y < canvas.length && index.x <= canvas[index.y].length; return index.y < canvas.length && index.x <= canvas[index.y].length;
} }
public function next():{key:Vec2<Int>, value:Pixel} { public function next():{key:Pos, value:Pixel} {
var oldIndex:Vec2<Int> = this.index; var oldIndex:Pos = this.index;
if (index.x >= canvas[index.y].length) { if (index.x >= canvas[index.y].length) {
// Goto next line // Goto next line

View File

@ -1,15 +1,16 @@
package lib.ui; package lib.ui;
import util.Pos;
import util.Vec.Vec2; import util.Vec.Vec2;
import kernel.ButtonType; import kernel.ButtonType;
using tink.CoreApi; using tink.CoreApi;
typedef UIEvents = { typedef UIEvents = {
public var ?onClick:Callback<{button:ButtonType, pos:Vec2<Int>}>; public var ?onClick:Callback<{button:ButtonType, pos:Pos}>;
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:Vec2<Int>}>; public var ?onMouseDrag:Callback<{button:ButtonType, pos:Pos}>;
public var ?onMouseScroll:Callback<{dir:Int, pos:Vec2<Int>}>; public var ?onMouseScroll:Callback<{dir:Int, pos:Pos}>;
public var ?onMouseUp:Callback<{button:ButtonType, pos:Vec2<Int>}>; public var ?onMouseUp:Callback<{button:ButtonType, pos:Pos}>;
public var ?onPaste:Callback<String>; public var ?onPaste:Callback<String>;
} }

View File

@ -44,7 +44,7 @@ class ListElement extends UIElement {
public function render(bounds:Vec2<Int>,offset: Pos):Canvas { public function render(bounds:Vec2<Int>,offset: Pos):Canvas {
var canvas: Canvas = new Canvas(); var canvas: Canvas = new Canvas();
var writePoint:Vec2<Int> = {x: 0, y: 0}; var writePoint:Pos = {x: 0, y: 0};
this.offset = offset; this.offset = offset;
for(element in this.content.get()){ for(element in this.content.get()){

View File

@ -1,5 +1,6 @@
package lib.ui.reactive; package lib.ui.reactive;
import util.Pos;
import util.Rect; import util.Rect;
import util.Color; import util.Color;
import util.Vec.Vec2; import util.Vec.Vec2;
@ -145,7 +146,7 @@ class ReactiveUI {
var canvas:Canvas = new Canvas(); var canvas:Canvas = new Canvas();
var elementMap: Map<Rect,UIElement> = new Map(); var elementMap: Map<Rect,UIElement> = new Map();
var writePoint:Vec2<Int> = {x: 0, y: 0}; var writePoint:Pos = {x: 0, y: 0};
for (child in children) { for (child in children) {
if (bounds.y - writePoint.y <= 0) { if (bounds.y - writePoint.y <= 0) {

View File

@ -2,6 +2,10 @@ package util;
import util.Vec.Vec2; import util.Vec.Vec2;
/**
Reporesents a Point in a 2D `Int` System.
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 Pos(Vec2<Int>) from Vec2<Int> to Vec2<Int>{
inline public function new(i:Vec2<Int>) { inline public function new(i:Vec2<Int>) {

View File

@ -4,10 +4,10 @@ import util.Vec.Vec2;
class Rect { class Rect {
private final tr:Vec2<Int>; private final tr:Pos;
private final bl:Vec2<Int>; private final bl:Pos;
public function new(p1: Vec2<Int>,p2:Vec2<Int>) { public function new(p1: Pos,p2:Pos) {
this.tr = { this.tr = {
x: MathI.max(p1.x,p2.x), x: MathI.max(p1.x,p2.x),
y: MathI.max(p1.y,p2.y) y: MathI.max(p1.y,p2.y)
@ -23,11 +23,11 @@ class Rect {
return (tr.x - bl.x) * (tr.y - bl.y); return (tr.x - bl.x) * (tr.y - bl.y);
} }
public function isInside(p: Vec2<Int>): Bool { public function isInside(p: Pos): Bool {
return (p.x >= bl.x && p.x <= tr.x) && (p.y >= bl.y && p.y <= tr.y); return (p.x >= bl.x && p.x <= tr.x) && (p.y >= bl.y && p.y <= tr.y);
} }
public function isOutside(p: Vec2<Int>): Bool { public function isOutside(p: Pos): Bool {
return !this.isInside(p); return !this.isInside(p);
} }
} }