renamed Pos and Pos3

This commit is contained in:
2024-10-14 21:40:26 +02:00
parent 08f41ccb0b
commit e0f8d274e7
25 changed files with 236 additions and 131 deletions

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

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

View File

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

View File

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

View File

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

View File

@@ -1,17 +1,17 @@
package lib.ui;
import lib.Pos;
import lib.ScreenPos;
import kernel.ButtonType;
using tink.CoreApi;
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 ?onKeyUp:Callback<Int>;
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 ?onMouseDrag:Callback<{button:ButtonType, pos:ScreenPos}>;
public var ?onMouseScroll:Callback<{dir:Int, pos:ScreenPos}>;
public var ?onMouseUp:Callback<{button:ButtonType, pos:ScreenPos}>;
public var ?onPaste: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) {
var newi = (this.length - 1) - i;
var element = this[newi];

View File

@@ -3,5 +3,5 @@ package lib.ui.elements;
import lib.ui.rendere.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;
}
public function render(bounds:Pos):Canvas {
public function render(bounds:ScreenPos):Canvas {
var canvas = new Canvas();
var offset = new Pos({x: 0, y: 0});
var offset = new ScreenPos({x: 0, y: 0});
if (hasTitle()) {
var title = new TextElement(this.title);
var halfWidth = Math.floor(bounds.x / 2) - Math.floor(this.title.length / 2);
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();
@@ -42,7 +42,7 @@ class RootElement implements IUIElement {
this.eventManager.addMapElement(child, bounds);
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;

View File

@@ -26,7 +26,7 @@ class TextElement implements IUIElement {
return uiEvents;
}
public function render(bounds:Pos):Canvas {
public function render(bounds:ScreenPos):Canvas {
var canvas = new Canvas();
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);
if (element == null) {
return;
@@ -48,11 +48,11 @@ class UIEventManager implements IUIEventDelegate {
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}) {}