Skip to content

Commit cb4ecc5

Browse files
authored
ZoomFactor in FunkinText, Early Implementation (#1011)
1 parent 0e269dd commit cb4ecc5

1 file changed

Lines changed: 83 additions & 4 deletions

File tree

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,98 @@
11
package funkin.backend;
22

3+
import flixel.FlxCamera;
4+
import flixel.FlxG;
5+
import flixel.math.FlxMath;
36
import flixel.text.FlxText;
47
import flixel.util.FlxColor;
58
import funkin.backend.system.Flags;
69

7-
class FunkinText extends FlxText {
8-
public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, ?Size:Int, Border:Bool = true) {
9-
if (Size == null) Size = Flags.DEFAULT_FONT_SIZE;
10+
class FunkinText extends FlxText
11+
{
12+
public var zoomFactor:Float = 1;
13+
public var zoomFactorEnabled:Bool = true;
14+
15+
public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, ?Size:Int, Border:Bool = true)
16+
{
17+
if (Size == null)
18+
Size = Flags.DEFAULT_FONT_SIZE;
1019

1120
super(X, Y, FieldWidth, Text, Size);
21+
1222
setFormat(Paths.font(Flags.DEFAULT_FONT), Size, FlxColor.WHITE);
13-
if (Border) {
23+
24+
if (Border)
25+
{
1426
borderStyle = OUTLINE;
1527
borderSize = 1;
1628
borderColor = 0xFF000000;
1729
}
1830
}
31+
32+
private inline function __shouldDoZoomFactor():Bool
33+
{
34+
return zoomFactorEnabled && zoomFactor != 1;
35+
}
36+
37+
private inline function __getZoomScaleX(camera:FlxCamera):Float
38+
{
39+
return (camera.scaleX > 0 ? Math.max : Math.min)(0, FlxMath.lerp(1 / camera.scaleX, 1, zoomFactor));
40+
}
41+
42+
private inline function __getZoomScaleY(camera:FlxCamera):Float
43+
{
44+
return (camera.scaleY > 0 ? Math.max : Math.min)(0, FlxMath.lerp(1 / camera.scaleY, 1, zoomFactor));
45+
}
46+
47+
private inline function __getZoomAnchorX(camera:FlxCamera):Float
48+
{
49+
if (Flags.USE_LEGACY_ZOOM_FACTOR)
50+
return camera.width * 0.5;
51+
52+
return camera.width * 0.5 + camera.scroll.x * scrollFactor.x;
53+
}
54+
55+
private inline function __getZoomAnchorY(camera:FlxCamera):Float
56+
{
57+
if (Flags.USE_LEGACY_ZOOM_FACTOR)
58+
return camera.height * 0.5;
59+
60+
return camera.height * 0.5 + camera.scroll.y * scrollFactor.y;
61+
}
62+
63+
override public function draw():Void
64+
{
65+
if (!__shouldDoZoomFactor())
66+
{
67+
super.draw();
68+
return;
69+
}
70+
71+
var camera:FlxCamera = this.camera;
72+
73+
if (camera == null)
74+
camera = FlxG.camera;
75+
76+
var oldX:Float = x;
77+
var oldY:Float = y;
78+
var oldScaleX:Float = scale.x;
79+
var oldScaleY:Float = scale.y;
80+
81+
var zoomScaleX:Float = __getZoomScaleX(camera);
82+
var zoomScaleY:Float = __getZoomScaleY(camera);
83+
84+
var anchorX:Float = __getZoomAnchorX(camera);
85+
var anchorY:Float = __getZoomAnchorY(camera);
86+
87+
x = (x - anchorX) * zoomScaleX + anchorX;
88+
y = (y - anchorY) * zoomScaleY + anchorY;
89+
90+
scale.set(scale.x * zoomScaleX, scale.y * zoomScaleY);
91+
92+
super.draw();
93+
94+
x = oldX;
95+
y = oldY;
96+
scale.set(oldScaleX, oldScaleY);
97+
}
1998
}

0 commit comments

Comments
 (0)