Skip to content

Commit 13eb003

Browse files
committed
Redo FunkinText
1 parent 7b1c26c commit 13eb003

2 files changed

Lines changed: 99 additions & 157 deletions

File tree

source/funkin/backend/FunkinSprite.hx

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class FunkinSprite extends FlxAnimate implements IBeatReceiver implements IOffse
7878

7979
public var animateSettings:FlxAnimateSettings = {};
8080

81+
// originally used for zoom factor, now unused
8182
var _rect2:FlxRect;
8283

8384
public function new(?X:Float = 0, ?Y:Float = 0, ?SimpleGraphic:FlxGraphicAsset)
@@ -190,7 +191,6 @@ class FunkinSprite extends FlxAnimate implements IBeatReceiver implements IOffse
190191

191192
// ANIMATE ATLAS DRAWING
192193
#if REGION
193-
194194
public override function destroy()
195195
{
196196
if (animOffsets != null) {
@@ -208,40 +208,6 @@ class FunkinSprite extends FlxAnimate implements IBeatReceiver implements IOffse
208208
}
209209
#end
210210

211-
// ZOOM FACTOR
212-
private inline function __shouldDoZoomFactor()
213-
return zoomFactorEnabled && zoomFactor != 1;
214-
215-
private inline function __prepareZoomFactor(?rect:FlxRect, camera:FlxCamera):FlxRect {
216-
return (rect ?? FlxRect.get()).set(
217-
camera.width * 0.5,
218-
camera.height * 0.5,
219-
(camera.scaleX > 0 ? Math.max : Math.min)(0, (1.0 - zoomFactor) / camera.scaleX + zoomFactor),
220-
(camera.scaleY > 0 ? Math.max : Math.min)(0, (1.0 - zoomFactor) / camera.scaleY + zoomFactor)
221-
);
222-
}
223-
224-
override public function isOnScreen(?camera:FlxCamera):Bool
225-
{
226-
if (forceIsOnScreen)
227-
return true;
228-
229-
if (camera == null)
230-
camera = FlxG.camera;
231-
232-
var bounds = getScreenBounds(_rect, camera);
233-
if (bounds.width == 0 && bounds.height == 0)
234-
return false;
235-
return camera.containsRect(bounds);
236-
}
237-
238-
private inline function __shouldDoAngleFactor()
239-
return angleFactorEnabled && angleFactor != 1;
240-
241-
private inline function __prepareAngleFactor(camera:FlxCamera):Float {
242-
return FlxMath.lerp(-camera.angle, 0, angleFactor);
243-
}
244-
245211
// OFFSETTING
246212
#if REGION
247213
public var animOffsets:Map<String, FlxPoint> = new Map<String, FlxPoint>();
@@ -371,22 +337,23 @@ class FunkinSprite extends FlxAnimate implements IBeatReceiver implements IOffse
371337
override function prepareDrawMatrix(matrix:FlxMatrix, camera:FlxCamera):Void {
372338
super.prepareDrawMatrix(matrix, camera);
373339

374-
if (__shouldDoZoomFactor() || __shouldDoAngleFactor()) {
375-
__prepareZoomFactor(_rect2, camera);
340+
final ox = camera.width * 0.5, oy = camera.height * 0.5;
341+
final sx = (camera.scaleX > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleX + zoomFactor);
342+
final sy = (camera.scaleY > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleY + zoomFactor);
343+
344+
if (zoomFactorEnabled && zoomFactor != 1) {
345+
matrix.setTo(
346+
matrix.a * sx, matrix.b * sy,
347+
matrix.c * sx, matrix.d * sy,
348+
(matrix.tx - ox) * sx + ox,
349+
(matrix.ty - oy) * sy + oy
350+
);
351+
}
376352

377-
if (__shouldDoZoomFactor()) {
378-
matrix.setTo(
379-
matrix.a * _rect2.width, matrix.b * _rect2.height,
380-
matrix.c * _rect2.width, matrix.d * _rect2.height,
381-
(matrix.tx - _rect2.x) * _rect2.width + _rect2.x,
382-
(matrix.ty - _rect2.y) * _rect2.height + _rect2.y
383-
);
384-
}
385-
if (__shouldDoAngleFactor()) {
386-
matrix.translate(-_rect2.x, -_rect2.y);
387-
matrix.rotate(FlxAngle.asRadians(__prepareAngleFactor(camera)));
388-
matrix.translate(_rect2.x, _rect2.y);
389-
}
353+
if (angleFactorEnabled && angleFactor != 1) {
354+
matrix.translate(-ox, -oy);
355+
matrix.rotate(-camera.angle * FlxAngle.TO_RAD * (1.0 - angleFactor));
356+
matrix.translate(ox, oy);
390357
}
391358
}
392359

Lines changed: 82 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package funkin.backend;
22

3+
import animate.FlxAnimate;
34
import flixel.FlxCamera;
45
import flixel.FlxG;
5-
import flixel.math.FlxMath;
66
import flixel.math.FlxAngle;
77
import flixel.math.FlxMatrix;
88
import flixel.math.FlxPoint;
99
import flixel.math.FlxRect;
1010
import flixel.text.FlxText;
1111
import flixel.util.FlxColor;
1212
import flixel.graphics.frames.FlxFrame;
13+
1314
import funkin.backend.system.Flags;
1415

16+
@:access(animate.FlxAnimate)
1517
class FunkinText extends FlxText
1618
{
17-
public var zoomFactor:Float = 1;
18-
public var zoomFactorEnabled:Bool = true;
19-
public var angleFactor:Float = 1;
20-
public var angleFactorEnabled:Bool = true;
19+
public var zoomFactor:Float = 1;
20+
public var zoomFactorEnabled:Bool = true;
21+
22+
public var angleFactor:Float = 1;
23+
public var angleFactorEnabled:Bool = true;
2124

2225
/**
2326
* Change the skew of your sprite's graphic.
@@ -34,82 +37,45 @@ class FunkinText extends FlxText
3437
*/
3538
public var matrixExposed:Bool = false;
3639

37-
public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, ?Size:Int, Border:Bool = true)
38-
{
39-
if (Size == null)
40-
Size = Flags.DEFAULT_FONT_SIZE;
41-
42-
super(X, Y, FieldWidth, Text, Size);
40+
public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, ?Size:Int, Border:Bool = true)
41+
{
42+
if (Size == null) Size = Flags.DEFAULT_FONT_SIZE;
4343

44-
setFormat(Paths.font(Flags.DEFAULT_FONT), Size, FlxColor.WHITE);
44+
super(X, Y, FieldWidth, Text, Size);
4545

46-
if (Border)
47-
{
48-
borderStyle = OUTLINE;
49-
borderSize = 1;
50-
borderColor = 0xFF000000;
51-
}
52-
}
46+
setFormat(Paths.font(Flags.DEFAULT_FONT), Size, FlxColor.WHITE);
5347

54-
var _rect2:FlxRect;
48+
if (Border)
49+
{
50+
borderStyle = OUTLINE;
51+
borderSize = 1;
52+
borderColor = 0xFF000000;
53+
}
54+
}
5555

56-
override function initVars() {
56+
override function initVars()
57+
{
5758
super.initVars();
5859
skew = new FlxPoint();
5960
transformMatrix = new FlxMatrix();
60-
_rect2 = FlxRect.get();
6161
}
6262

63-
private inline function __shouldDoZoomFactor():Bool
64-
{
65-
return zoomFactorEnabled && zoomFactor != 1;
66-
}
67-
68-
private inline function __prepareZoomFactor(rect:FlxRect, camera:FlxCamera):FlxRect {
69-
return (rect ?? FlxRect.get()).set(
70-
camera.width * 0.5,
71-
camera.height * 0.5,
72-
(camera.scaleX > 0 ? Math.max : Math.min)(0, (1.0 - zoomFactor) / camera.scaleX + zoomFactor),
73-
(camera.scaleY > 0 ? Math.max : Math.min)(0, (1.0 - zoomFactor) / camera.scaleY + zoomFactor)
74-
);
63+
override function destroy()
64+
{
65+
super.destroy();
66+
skew = FlxDestroyUtil.put(skew);
67+
transformMatrix = null;
7568
}
7669

77-
private inline function __shouldDoAngleFactor():Bool
78-
{
79-
return angleFactorEnabled && angleFactor != 1;
80-
}
81-
82-
private inline function __prepareAngleFactor(camera:FlxCamera):Float
83-
{
84-
return FlxMath.lerp(-camera.angle, 0, angleFactor);
85-
}
86-
87-
// EVERYTHING AFTER THIS COMMENT IS STOLen from flixel animate ok sorry
88-
public override function drawComplex(camera:FlxCamera):Void
70+
override function drawComplex(camera:FlxCamera):Void
8971
{
90-
if (!__shouldDoZoomFactor() && !__shouldDoAngleFactor() && (skew.x == 0) && (skew.y == 0)) {
91-
return super.drawComplex(camera);
92-
}
93-
94-
final frame = this._frame;
95-
final matrix = this._matrix; // TODO: Just use local?
96-
97-
frame.prepareMatrix(matrix, FlxFrameAngle.ANGLE_0, checkFlipX(), checkFlipY());
98-
prepareDrawMatrix(matrix, camera);
99-
100-
if (layer != null)
101-
layer.drawPixels(this, camera, frame, framePixels, matrix, colorTransform, blend, antialiasing, shaderEnabled ? shader : null);
102-
else
103-
camera.drawPixels(frame, framePixels, matrix, colorTransform, blend, antialiasing, shaderEnabled ? shader : null);
104-
}
72+
_frame.prepareMatrix(_matrix, ANGLE_0, checkFlipX(), checkFlipY());
73+
_matrix.translate(-origin.x, -origin.y);
10574

106-
function prepareDrawMatrix(matrix:FlxMatrix, camera:FlxCamera):Void {
107-
matrix.translate(-origin.x, -origin.y);
10875
if (frameOffsetAngle != null && frameOffsetAngle != angle)
10976
{
11077
var angleOff = (frameOffsetAngle - angle) * FlxAngle.TO_RAD;
111-
var cos = Math.cos(angleOff);
112-
var sin = Math.sin(angleOff);
78+
var cos = Math.cos(angleOff), sin = Math.sin(angleOff);
11379
// cos doesnt need to be negated
11480
_matrix.rotateWithTrig(cos, -sin);
11581
_matrix.translate(-frameOffset.x, -frameOffset.y);
@@ -118,66 +84,75 @@ class FunkinText extends FlxText
11884
else
11985
_matrix.translate(-frameOffset.x, -frameOffset.y);
12086

121-
matrix.scale(scale.x, scale.y);
87+
_matrix.scale(scale.x, scale.y);
12288

123-
if (matrixExposed) matrix.concat(transformMatrix);
89+
if (matrixExposed) _matrix.concat(transformMatrix);
12490
else {
12591
if (angle != 0)
12692
{
12793
updateTrig();
128-
matrix.rotateWithTrig(_cosAngle, _sinAngle);
94+
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
12995
}
13096
if (skew.x != 0 || skew.y != 0)
13197
{
132-
updateSkew();
133-
matrix.concat(_skewMatrix);
98+
FlxAnimate._skewMatrix.setTo(1, Math.tan(skew.y * FlxAngle.TO_RAD), Math.tan(skew.x * FlxAngle.TO_RAD), 1, 0, 0);
99+
_matrix.concat(FlxAnimate._skewMatrix);
134100
}
135101
}
136102

137-
getScreenPosition(_point, camera).subtractPoint(offset).add(origin.x, origin.y);
138-
matrix.translate(_point.x, _point.y);
103+
getScreenPosition(_point, camera).subtractPoint(offset).addPoint(origin);
104+
_matrix.translate(_point.x, _point.y);
139105

140106
if (isPixelPerfectRender(camera))
141-
preparePixelPerfectMatrix(matrix);
142-
143-
if (__shouldDoZoomFactor() || __shouldDoAngleFactor()) {
144-
__prepareZoomFactor(_rect2, camera);
145-
146-
if (__shouldDoZoomFactor()) {
147-
matrix.setTo(
148-
matrix.a * _rect2.width, matrix.b * _rect2.height,
149-
matrix.c * _rect2.width, matrix.d * _rect2.height,
150-
(matrix.tx - _rect2.x) * _rect2.width + _rect2.x,
151-
(matrix.ty - _rect2.y) * _rect2.height + _rect2.y
152-
);
153-
}
154-
if (__shouldDoAngleFactor()) {
155-
matrix.translate(-_rect2.x, -_rect2.y);
156-
matrix.rotate(FlxAngle.asRadians(__prepareAngleFactor(camera)));
157-
matrix.translate(_rect2.x, _rect2.y);
158-
}
107+
{
108+
_matrix.tx = Math.floor(_matrix.tx);
109+
_matrix.ty = Math.floor(_matrix.ty);
159110
}
160-
}
161111

162-
function preparePixelPerfectMatrix(matrix:FlxMatrix):Void
163-
{
164-
matrix.tx = Math.floor(matrix.tx);
165-
matrix.ty = Math.floor(matrix.ty);
166-
}
167-
168-
// semi stolen from FlxSkewedSprite
169-
static var _skewMatrix:FlxMatrix = new FlxMatrix();
112+
// Copied from FunkinSprite
113+
final ox = camera.width * 0.5, oy = camera.height * 0.5;
114+
final sx = (camera.scaleX > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleX + zoomFactor);
115+
final sy = (camera.scaleY > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleY + zoomFactor);
116+
117+
if (zoomFactorEnabled && zoomFactor != 1) {
118+
_matrix.setTo(
119+
_matrix.a * sx, _matrix.b * sy,
120+
_matrix.c * sx, _matrix.d * sy,
121+
(_matrix.tx - ox) * sx + ox,
122+
(_matrix.ty - oy) * sy + oy
123+
);
124+
}
170125

171-
private inline function updateSkew():Void
172-
{
173-
_skewMatrix.setTo(1, Math.tan(skew.y * FlxAngle.TO_RAD), Math.tan(skew.x * FlxAngle.TO_RAD), 1, 0, 0);
126+
if (angleFactorEnabled && angleFactor != 1) {
127+
_matrix.translate(-ox, -oy);
128+
_matrix.rotate(-camera.angle * FlxAngle.TO_RAD * (1.0 - angleFactor));
129+
_matrix.translate(ox, oy);
130+
}
131+
132+
if (layer != null)
133+
layer.drawPixels(this, camera, _frame, framePixels, _matrix, colorTransform, blend, antialiasing, shaderEnabled ? shader : null);
134+
else
135+
camera.drawPixels(_frame, framePixels, _matrix, colorTransform, blend, antialiasing, shaderEnabled ? shader : null);
174136
}
175137

176-
public override function destroy()
138+
override function getScreenBounds(?rect:FlxRect, ?camera:FlxCamera):FlxRect
177139
{
178-
super.destroy();
179-
_rect2 = FlxDestroyUtil.put(_rect2);
180-
skew = FlxDestroyUtil.put(skew);
181-
transformMatrix = null;
140+
if (camera == null) camera = FlxG.camera;
141+
rect = super.getScreenBounds(rect, camera);
142+
143+
if (zoomFactorEnabled && zoomFactor != 1) {
144+
final ox = camera.width * 0.5, oy = camera.height * 0.5;
145+
final sx = (camera.scaleX > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleX + zoomFactor);
146+
final sy = (camera.scaleY > 0.0 ? Math.max : Math.min)(0.0, (1.0 - zoomFactor) / camera.scaleY + zoomFactor);
147+
148+
rect.set(
149+
(rect.x - ox) * sx + ox,
150+
(rect.y - oy) * sy + oy,
151+
rect.width * sx,
152+
rect.height * sy
153+
);
154+
}
155+
156+
return rect;
182157
}
183158
}

0 commit comments

Comments
 (0)