From de35f341737949c92a16cc9badc176804fa9f64d Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 8 Mar 2022 13:33:40 +0100 Subject: [PATCH] use Pos instead of Vec2 --- src/kernel/KernelEvents.hx | 21 +++++++++++---------- src/kernel/MainTerm.hx | 5 +++-- src/kernel/peripherals/Screen.hx | 3 ++- src/kernel/ui/TermBuffer.hx | 7 ++++--- src/kernel/ui/TermWriteable.hx | 3 ++- src/kernel/ui/VirtualTermWriter.hx | 3 ++- src/kernel/ui/WindowContext.hx | 19 ++++++++++--------- src/lib/ui/Canvas.hx | 15 ++++++++------- src/lib/ui/UIEvents.hx | 9 +++++---- src/lib/ui/reactive/ListElement.hx | 2 +- src/lib/ui/reactive/ReactiveUI.hx | 3 ++- src/util/Pos.hx | 4 ++++ src/util/Rect.hx | 10 +++++----- 13 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index ec2d352..0dfd1bd 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -1,5 +1,6 @@ package kernel; +import util.Pos; import cc.HTTP.HTTPResponse; import lua.TableTools; import lua.Coroutine; @@ -35,11 +36,11 @@ class KernelEvents { distance:Int }>; public final onMonitorResize:Signal; - public final onMonitorTouch:Signal<{addr:String, pos:Vec2}>; - public final onMouseClick:Signal<{button:ButtonType, pos:Vec2}>; - public final onMouseDrag:Signal<{button:ButtonType, pos:Vec2}>; - public final onMouseScroll:Signal<{dir:Int, pos:Vec2}>; - public final onMouseUp:Signal<{button:ButtonType, pos:Vec2}>; + public final onMonitorTouch:Signal<{addr:String, pos:Pos}>; + public final onMouseClick:Signal<{button:ButtonType, pos:Pos}>; + public final onMouseDrag:Signal<{button:ButtonType, pos:Pos}>; + public final onMouseScroll:Signal<{dir:Int, pos:Pos}>; + public final onMouseUp:Signal<{button:ButtonType, pos:Pos}>; public final onPaste:Signal; public final onPeripheral:Signal; public final onPeripheralDetach:Signal; @@ -73,11 +74,11 @@ class KernelEvents { distance:Int }> = Signal.trigger(); private final onMonitorResizeTrigger:SignalTrigger = Signal.trigger(); - private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Vec2}> = Signal.trigger(); - private final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}> = Signal.trigger(); - private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}> = Signal.trigger(); - private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Vec2}> = Signal.trigger(); - private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}> = Signal.trigger(); + private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger(); + private final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); + private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); + private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}> = Signal.trigger(); + private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}> = Signal.trigger(); private final onPasteTrigger:SignalTrigger = Signal.trigger(); private final onPeripheralTrigger:SignalTrigger = Signal.trigger(); private final onPeripheralDetachTrigger:SignalTrigger = Signal.trigger(); diff --git a/src/kernel/MainTerm.hx b/src/kernel/MainTerm.hx index 5784faf..991a959 100644 --- a/src/kernel/MainTerm.hx +++ b/src/kernel/MainTerm.hx @@ -1,5 +1,6 @@ package kernel; +import util.Pos; using tink.CoreApi; import kernel.ui.TermWriteable; @@ -38,7 +39,7 @@ class MainTerm implements TermWriteable { Term.scroll(y); } - public function getCursorPos():Vec2 { + public function getCursorPos():Pos { var rtn = Term.getCursorPos(); return { x: rtn.x - 1, @@ -59,7 +60,7 @@ class MainTerm implements TermWriteable { Term.setCursorBlink(blink); } - public function getSize():Vec2 { + public function getSize():Pos { var rtn = Term.getSize(); return { x: rtn.width, diff --git a/src/kernel/peripherals/Screen.hx b/src/kernel/peripherals/Screen.hx index 89974e9..9526098 100644 --- a/src/kernel/peripherals/Screen.hx +++ b/src/kernel/peripherals/Screen.hx @@ -1,6 +1,7 @@ package kernel.peripherals; +import util.Pos; import cc.Term.TerminalSize; import kernel.ui.TermWriteable; import util.Vec.Vec2; @@ -52,7 +53,7 @@ class Screen implements TermWriteable implements IPeripheral { nativ.scroll(y); } - public function getCursorPos():Vec2 { + public function getCursorPos():Pos { var rtn = nativ.getCursorPos(); return { x: rtn.x - 1, diff --git a/src/kernel/ui/TermBuffer.hx b/src/kernel/ui/TermBuffer.hx index 80db7d5..5a28c71 100644 --- a/src/kernel/ui/TermBuffer.hx +++ b/src/kernel/ui/TermBuffer.hx @@ -1,6 +1,7 @@ package kernel.ui; +import util.Pos; import util.Vec.Vec2; import util.Color; import kernel.ui.TermWriteable; @@ -18,7 +19,7 @@ class TermBuffer implements TermWriteable { **/ private var screenBuffer:Array>; - private var cursorPos:Vec2 = {x: 0, y: 0}; + private var cursorPos:Pos = {x: 0, y: 0}; private var currentTextColor:Color = White; private var currentBgColor:Color = Black; private var cursorBlink: Bool = false; @@ -90,7 +91,7 @@ class TermBuffer implements TermWriteable { target.setBackgroundColor(currentBgColor); } - private function safeWriteScreenBuffer(pos:Vec2, char:String) { + private function safeWriteScreenBuffer(pos:Pos, char:String) { if (screenBuffer.length > pos.y && screenBuffer[pos.y].length > pos.x) { screenBuffer[pos.y][pos.x].char = char; screenBuffer[pos.y][pos.x].bg = currentBgColor; @@ -120,7 +121,7 @@ class TermBuffer implements TermWriteable { ]); } - public function getCursorPos():Vec2 { + public function getCursorPos():Pos { return cursorPos; } diff --git a/src/kernel/ui/TermWriteable.hx b/src/kernel/ui/TermWriteable.hx index 3906c1b..702594f 100644 --- a/src/kernel/ui/TermWriteable.hx +++ b/src/kernel/ui/TermWriteable.hx @@ -1,5 +1,6 @@ package kernel.ui; +import util.Pos; using tink.CoreApi; import util.Color; @@ -17,7 +18,7 @@ interface TermWriteable { /** Even though CC is 1 based we use a 0 based index. **/ - public function getCursorPos():Vec2; + public function getCursorPos():Pos; /** Even though CC is 1 based we use a 0 based index. diff --git a/src/kernel/ui/VirtualTermWriter.hx b/src/kernel/ui/VirtualTermWriter.hx index 9487794..ed90b01 100644 --- a/src/kernel/ui/VirtualTermWriter.hx +++ b/src/kernel/ui/VirtualTermWriter.hx @@ -1,5 +1,6 @@ package kernel.ui; +import util.Pos; using tink.CoreApi; import util.Vec.Vec2; @@ -85,7 +86,7 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { super.scroll(y); } - public override function getCursorPos():Vec2 { + public override function getCursorPos():Pos { if (isEnabled()) { return target.getCursorPos(); } else { diff --git a/src/kernel/ui/WindowContext.hx b/src/kernel/ui/WindowContext.hx index 2cb8832..36b5c6e 100644 --- a/src/kernel/ui/WindowContext.hx +++ b/src/kernel/ui/WindowContext.hx @@ -1,5 +1,6 @@ package kernel.ui; +import util.Pos; using tink.CoreApi; import util.Color; @@ -13,20 +14,20 @@ import kernel.ui.TermWriteable; class WindowContext implements TermWriteable { private final writer:VirtualTermWriter; - public var onClick(default, null):Signal<{button:ButtonType, pos:Vec2}>; + public var onClick(default, null):Signal<{button:ButtonType, pos:Pos}>; public var onKey(default, null):Signal<{keyCode:Int, isHeld:Bool}>; public var onKeyUp(default, null):Signal; - public var onMouseDrag(default, null):Signal<{button:ButtonType, pos:Vec2}>; - public var onMouseScroll(default, null):Signal<{dir:Int, pos:Vec2}>; - public var onMouseUp(default, null):Signal<{button:ButtonType, pos:Vec2}>; + public var onMouseDrag(default, null):Signal<{button:ButtonType, pos:Pos}>; + public var onMouseScroll(default, null):Signal<{dir:Int, pos:Pos}>; + public var onMouseUp(default, null):Signal<{button:ButtonType, pos:Pos}>; public var onPaste(default, null):Signal; - @:allow(kernel.ui.WindowManager) private final onClickTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}>; + @: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 onKeyUpTrigger:SignalTrigger; - @:allow(kernel.ui.WindowManager) private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}>; - @:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Vec2}>; - @:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Vec2}>; + @:allow(kernel.ui.WindowManager) private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>; + @:allow(kernel.ui.WindowManager) private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Pos}>; + @:allow(kernel.ui.WindowManager) private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Pos}>; @:allow(kernel.ui.WindowManager) private final onPasteTrigger:SignalTrigger; @:allow(kernel.ui.WindowManager) @@ -81,7 +82,7 @@ class WindowContext implements TermWriteable { writer.scroll(y); } - public function getCursorPos():Vec2 { + public function getCursorPos():Pos { return writer.getCursorPos(); } diff --git a/src/lib/ui/Canvas.hx b/src/lib/ui/Canvas.hx index a0247c2..81e5e00 100644 --- a/src/lib/ui/Canvas.hx +++ b/src/lib/ui/Canvas.hx @@ -1,5 +1,6 @@ package lib.ui; +import util.Pos; import util.Rect; import util.Vec.Vec2; import kernel.ui.Pixel; @@ -9,7 +10,7 @@ abstract Canvas(Array>) to Array> { this = [[]]; } - public inline function set(i:Vec2, pixel:Pixel) { + public inline function set(i:Pos, pixel:Pixel) { if (this[i.y] == null) { this[i.y] = []; } @@ -17,15 +18,15 @@ abstract Canvas(Array>) to Array> { this[i.y][i.x] = pixel; } - public inline function get(i:Vec2):Pixel { + public inline function get(i:Pos):Pixel { return this[i.y][i.x]; } - public function keyValueIterator():KeyValueIterator, Pixel> { + public function keyValueIterator():KeyValueIterator { return new CanvasKeyValueIterator(this); } - public function combine(other:Canvas, offset:Vec2) { + public function combine(other:Canvas, offset:Pos) { for (key => value in other) { if (value == null) { continue; @@ -67,7 +68,7 @@ abstract Canvas(Array>) to Array> { class CanvasKeyValueIterator { private final canvas:Array>; - private var index:Vec2 = {x: 0, y: 0}; + private var index:Pos = {x: 0, y: 0}; @:allow(lib.ui.Canvas) private function new(canvas:Array>) { @@ -78,8 +79,8 @@ class CanvasKeyValueIterator { return index.y < canvas.length && index.x <= canvas[index.y].length; } - public function next():{key:Vec2, value:Pixel} { - var oldIndex:Vec2 = this.index; + public function next():{key:Pos, value:Pixel} { + var oldIndex:Pos = this.index; if (index.x >= canvas[index.y].length) { // Goto next line diff --git a/src/lib/ui/UIEvents.hx b/src/lib/ui/UIEvents.hx index 44722d4..cf92592 100644 --- a/src/lib/ui/UIEvents.hx +++ b/src/lib/ui/UIEvents.hx @@ -1,15 +1,16 @@ package lib.ui; +import util.Pos; import util.Vec.Vec2; import kernel.ButtonType; using tink.CoreApi; typedef UIEvents = { - public var ?onClick:Callback<{button:ButtonType, pos:Vec2}>; + public var ?onClick:Callback<{button:ButtonType, pos:Pos}>; public var ?onKey:Callback<{keyCode:Int, isHeld:Bool}>; public var ?onKeyUp:Callback; - public var ?onMouseDrag:Callback<{button:ButtonType, pos:Vec2}>; - public var ?onMouseScroll:Callback<{dir:Int, pos:Vec2}>; - public var ?onMouseUp:Callback<{button:ButtonType, pos:Vec2}>; + public var ?onMouseDrag:Callback<{button:ButtonType, pos:Pos}>; + public var ?onMouseScroll:Callback<{dir:Int, pos:Pos}>; + public var ?onMouseUp:Callback<{button:ButtonType, pos:Pos}>; public var ?onPaste:Callback; } diff --git a/src/lib/ui/reactive/ListElement.hx b/src/lib/ui/reactive/ListElement.hx index 1aff4da..d5373ce 100644 --- a/src/lib/ui/reactive/ListElement.hx +++ b/src/lib/ui/reactive/ListElement.hx @@ -44,7 +44,7 @@ class ListElement extends UIElement { public function render(bounds:Vec2,offset: Pos):Canvas { var canvas: Canvas = new Canvas(); - var writePoint:Vec2 = {x: 0, y: 0}; + var writePoint:Pos = {x: 0, y: 0}; this.offset = offset; for(element in this.content.get()){ diff --git a/src/lib/ui/reactive/ReactiveUI.hx b/src/lib/ui/reactive/ReactiveUI.hx index a86e690..314538f 100644 --- a/src/lib/ui/reactive/ReactiveUI.hx +++ b/src/lib/ui/reactive/ReactiveUI.hx @@ -1,5 +1,6 @@ package lib.ui.reactive; +import util.Pos; import util.Rect; import util.Color; import util.Vec.Vec2; @@ -145,7 +146,7 @@ class ReactiveUI { var canvas:Canvas = new Canvas(); var elementMap: Map = new Map(); - var writePoint:Vec2 = {x: 0, y: 0}; + var writePoint:Pos = {x: 0, y: 0}; for (child in children) { if (bounds.y - writePoint.y <= 0) { diff --git a/src/util/Pos.hx b/src/util/Pos.hx index 5d145e9..e59a95f 100644 --- a/src/util/Pos.hx +++ b/src/util/Pos.hx @@ -2,6 +2,10 @@ package util; import util.Vec.Vec2; +/** + Reporesents a Point in a 2D `Int` System. + Basicly a wrapper for Vec2 with some extra functions. +**/ @:forward(x,y) abstract Pos(Vec2) from Vec2 to Vec2{ inline public function new(i:Vec2) { diff --git a/src/util/Rect.hx b/src/util/Rect.hx index d7a848d..20bb491 100644 --- a/src/util/Rect.hx +++ b/src/util/Rect.hx @@ -4,10 +4,10 @@ import util.Vec.Vec2; class Rect { - private final tr:Vec2; - private final bl:Vec2; + private final tr:Pos; + private final bl:Pos; - public function new(p1: Vec2,p2:Vec2) { + public function new(p1: Pos,p2:Pos) { this.tr = { x: MathI.max(p1.x,p2.x), y: MathI.max(p1.y,p2.y) @@ -23,11 +23,11 @@ class Rect { return (tr.x - bl.x) * (tr.y - bl.y); } - public function isInside(p: Vec2): Bool { + public function isInside(p: Pos): Bool { return (p.x >= bl.x && p.x <= tr.x) && (p.y >= bl.y && p.y <= tr.y); } - public function isOutside(p: Vec2): Bool { + public function isOutside(p: Pos): Bool { return !this.isInside(p); } }