Skip to content

Commit

Permalink
Manipulate nodes through their transform directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Feb 7, 2024
1 parent 74c0a0c commit 6e60508
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ImageScript extends Script<Sprite> {

var width = imageFile.width
var height = imageFile.height
scaleXY(scene.window.renderResolution.calculateScaleToFit(width, height))
translate(-width / 2, -height / 2)
transform.scaleXY(scene.window.renderResolution.calculateScaleToFit(width, height))
transform.translate(-width / 2, -height / 2)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,15 +24,15 @@ import org.joml.primitives.Rectanglef

/**
* Extensions to JOMLs objects to work with Red Horizon.
*
*
* @author Emanuel Rabina
*/
class JomlExtensions {

/**
* Return an array of points {@code Vector2f}s, each representing a point of
* this rectangle.
*
*
* @param self
* @param clazz
* @return An array of 4 vectors, one for each x/y point around the rectangle.
Expand All @@ -53,7 +53,7 @@ class JomlExtensions {
/**
* Overload the {@code -} operator to perform vector subtraction. Note that
* this creates a new object to store the result and is returned.
*
*
* @param self
* @param v
* @return
Expand All @@ -66,7 +66,7 @@ class JomlExtensions {
/**
* Overload the {@code *} operator to perform matrix multiplication. Note
* that this creates a new object to store the result and is returned.
*
*
* @param self
* @param right
* @return
Expand All @@ -79,7 +79,7 @@ class JomlExtensions {
/**
* Overload the {@code +} operator to perform vector addition. Note that
* this creates a new object to store the result and is returned.
*
*
* @param self
* @param v
* @return
Expand All @@ -89,6 +89,18 @@ class JomlExtensions {
return self.add(v, new Vector3f())
}

/**
* Scale just the X and Y components of a matrix by the same value.
*
* @param self
* @param factor
* @return
*/
static Matrix4f scaleXY(Matrix4f self, float factor) {

return self.scaleXY(factor, factor)
}

/**
* Set a rectangle to represent the given values.
*
Expand All @@ -110,7 +122,7 @@ class JomlExtensions {

/**
* Test whether an XY plane represented by a rectangle is within this frustum.
*
*
* @param self
* @param plane
* @return
Expand All @@ -119,4 +131,17 @@ class JomlExtensions {

return self.testPlaneXY(plane.minX, plane.minY, plane.maxX, plane.maxY)
}

/**
* Translate this matrix by just an X and Y component.
*
* @param self
* @param x
* @param y
* @return
*/
static Matrix4f translate(Matrix4f self, float x, float y) {

return self.translate(x, y, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,23 @@ class Video extends Node<Video> implements AudioElement, GraphicsElement, Playab

}

@Override
Video scale(float x, float y, float z) {

[animation, soundTrack]*.scale(x, y, z)
return this
}
// @Override
// Video scale(float x, float y, float z) {
//
// [animation, soundTrack]*.scale(x, y, z)
// return this
// }

@Override
void stop() {

[animation, soundTrack]*.stop()
}

@Override
Video translate(float x, float y, float z) {

[animation, soundTrack]*.translate(x, y, z)
return this
}
// @Override
// Video translate(float x, float y, float z) {
//
// [animation, soundTrack]*.translate(x, y, z)
// return this
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package nz.net.ultraq.redhorizon.engine.scenegraph
import nz.net.ultraq.redhorizon.engine.scenegraph.scripting.Scriptable

import org.joml.Matrix4f
import org.joml.Vector2f
import org.joml.Vector3f
import org.joml.primitives.Rectanglef

Expand Down Expand Up @@ -81,63 +80,4 @@ class Node<T extends Node> implements SceneEvents, Scriptable<T>, Visitable {
child.onSceneAdded(scene)
}
}

/**
* Scale this element by the given values.
*/
T scale(float x, float y, float z) {

transform.scale(x, y, z)
bounds.scale(x, y, z)
return this
}

/**
* Scale the X and Y values of this element.
*
* @param factor
* @return
*/
T scaleXY(float factor) {

return scale(factor, factor, 1)
}

/**
* Translate the position of this element.
*
* @param offset
* @return
*/
T translate(Vector3f offset) {

return translate(offset.x, offset.y, offset.z)
}

/**
* Translate the position of this element.
*
* @param xyOffset
* @param z
* @return
*/
T translate(Vector2f xyOffset, float z = 0) {

return translate(xyOffset.x, xyOffset.y, z)
}

/**
* Translate the position of this element.
*
* @param x
* @param y
* @param z
* @return
*/
T translate(float x, float y, float z = 0) {

transform.translate(x, y, z)
bounds.translate(x, y)
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Sprite extends Node<Sprite> implements GraphicsElement {
.get()
)
region = new Rectanglef(0, 0, width, height)

super.onSceneAdded(scene)
}

@Override
Expand Down

0 comments on commit 6e60508

Please sign in to comment.