11package funkin .backend ;
22
3+ import animate .FlxAnimate ;
34import flixel .FlxCamera ;
45import flixel .FlxG ;
56import flixel .math .FlxMath ;
@@ -10,14 +11,17 @@ import flixel.math.FlxRect;
1011import flixel .text .FlxText ;
1112import flixel .util .FlxColor ;
1213import flixel .graphics .frames .FlxFrame ;
14+
1315import funkin .backend .system .Flags ;
1416
17+ @:access (animate. FlxAnimate )
1518class FunkinText extends FlxText
1619{
17- public var zoomFactor : Float = 1 ;
18- public var zoomFactorEnabled : Bool = true ;
19- public var angleFactor : Float = 1 ;
20- public var angleFactorEnabled : Bool = true ;
20+ public var zoomFactor : Float = 1 ;
21+ public var zoomFactorEnabled : Bool = true ;
22+
23+ public var angleFactor : Float = 1 ;
24+ public var angleFactorEnabled : Bool = true ;
2125
2226 /**
2327 * Change the skew of your sprite's graphic.
@@ -34,91 +38,45 @@ class FunkinText extends FlxText
3438 */
3539 public var matrixExposed : Bool = false ;
3640
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 );
41+ public function new (X : Float = 0 , Y : Float = 0 , FieldWidth : Float = 0 , ? Text : String , ? Size : Int , Border : Bool = true )
42+ {
43+ if (Size == null ) Size = Flags .DEFAULT_FONT_SIZE ;
4344
44- setFormat ( Paths . font ( Flags . DEFAULT_FONT ), Size , FlxColor . WHITE );
45+ super ( X , Y , FieldWidth , Text , Size );
4546
46- if (Border )
47- {
48- borderStyle = OUTLINE ;
49- borderSize = 1 ;
50- borderColor = 0xFF000000 ;
51- }
52- }
47+ setFormat (Paths .font (Flags .DEFAULT_FONT ), Size , FlxColor .WHITE );
5348
54- var _rect2 : FlxRect ;
49+ if (Border )
50+ {
51+ borderStyle = OUTLINE ;
52+ borderSize = 1 ;
53+ borderColor = 0xFF000000 ;
54+ }
55+ }
5556
56- override function initVars () {
57+ override function initVars ()
58+ {
5759 super .initVars ();
5860 skew = new FlxPoint ();
5961 transformMatrix = new FlxMatrix ();
60- _rect2 = FlxRect .get ();
6162 }
6263
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- if (Flags .USE_LEGACY_ZOOM_FACTOR )
70- return rect .set (
71- camera .width * 0.5 ,
72- camera .height * 0.5 ,
73- (camera .scaleX > 0 ? Math .max : Math .min )(0 , FlxMath .lerp (1 / camera .scaleX , 1 , zoomFactor )),
74- (camera .scaleY > 0 ? Math .max : Math .min )(0 , FlxMath .lerp (1 / camera .scaleY , 1 , zoomFactor ))
75- );
76- else
77- return rect .set (
78- camera .width * 0.5 + camera .scroll .x * scrollFactor .x ,
79- camera .height * 0.5 + camera .scroll .y * scrollFactor .y ,
80- (camera .scaleX > 0 ? Math .max : Math .min )(0 , FlxMath .lerp (1 / camera .scaleX , 1 , zoomFactor )),
81- (camera .scaleY > 0 ? Math .max : Math .min )(0 , FlxMath .lerp (1 / camera .scaleY , 1 , zoomFactor ))
82- );
64+ override function destroy ()
65+ {
66+ super .destroy ();
67+ skew = FlxDestroyUtil .put (skew );
68+ transformMatrix = null ;
8369 }
8470
85- private inline function __shouldDoAngleFactor (): Bool
86- {
87- return angleFactorEnabled && angleFactor != 1 ;
88- }
89-
90- private inline function __prepareAngleFactor (camera : FlxCamera ): Float
91- {
92- return FlxMath .lerp (- camera .angle , 0 , angleFactor );
93- }
94-
95- // EVERYTHING AFTER THIS COMMENT IS STOLen from flixel animate ok sorry
96- public override function drawComplex (camera : FlxCamera ): Void
71+ override function drawComplex (camera : FlxCamera ): Void
9772 {
98- if (! __shouldDoZoomFactor () && ! __shouldDoAngleFactor () && (skew .x == 0 ) && (skew .y == 0 )) {
99- return super .drawComplex (camera );
100- }
101-
102- final frame = this ._frame ;
103- final matrix = this ._matrix ; // TODO: Just use local?
104-
105- frame .prepareMatrix (matrix , FlxFrameAngle .ANGLE_0 , checkFlipX (), checkFlipY ());
106- prepareDrawMatrix (matrix , camera );
73+ _frame .prepareMatrix (_matrix , ANGLE_0 , checkFlipX (), checkFlipY ());
74+ _matrix .translate (- origin .x - _graphicOffset .x , - origin .y - _graphicOffset .y );
10775
108- if (layer != null )
109- layer .drawPixels (this , camera , frame , framePixels , matrix , colorTransform , blend , antialiasing , shaderEnabled ? shader : null ,
110- wrapMode );
111- else
112- camera .drawPixels (frame , framePixels , matrix , colorTransform , blend , antialiasing , shaderEnabled ? shader : null , wrapMode );
113- }
114-
115- function prepareDrawMatrix (matrix : FlxMatrix , camera : FlxCamera ): Void {
116- matrix .translate (- origin .x , - origin .y );
11776 if (frameOffsetAngle != null && frameOffsetAngle != angle )
11877 {
11978 var angleOff = (frameOffsetAngle - angle ) * FlxAngle .TO_RAD ;
120- var cos = Math .cos (angleOff );
121- var sin = Math .sin (angleOff );
79+ var cos = Math .cos (angleOff ), sin = Math .sin (angleOff );
12280 // cos doesnt need to be negated
12381 _matrix .rotateWithTrig (cos , - sin );
12482 _matrix .translate (- frameOffset .x , - frameOffset .y );
@@ -127,66 +85,75 @@ class FunkinText extends FlxText
12785 else
12886 _matrix .translate (- frameOffset .x , - frameOffset .y );
12987
130- matrix .scale (scale .x , scale .y );
88+ _matrix .scale (scale .x , scale .y );
13189
132- if (matrixExposed ) matrix .concat (transformMatrix );
90+ if (matrixExposed ) _matrix .concat (transformMatrix );
13391 else {
13492 if (angle != 0 )
13593 {
13694 updateTrig ();
137- matrix .rotateWithTrig (_cosAngle , _sinAngle );
95+ _matrix .rotateWithTrig (_cosAngle , _sinAngle );
13896 }
13997 if (skew .x != 0 || skew .y != 0 )
14098 {
141- updateSkew ( );
142- matrix .concat (_skewMatrix );
99+ FlxAnimate . _skewMatrix . setTo ( 1 , Math . tan ( skew . y * FlxAngle . TO_RAD ), Math . tan ( skew . x * FlxAngle . TO_RAD ), 1 , 0 , 0 );
100+ _matrix .concat (FlxAnimate . _skewMatrix );
143101 }
144102 }
145103
146- getScreenPosition (_point , camera ).subtractPoint (offset ).add (origin . x , origin . y );
147- matrix .translate (_point .x , _point .y );
104+ getScreenPosition (_point , camera ).subtractPoint (offset ).addPoint (origin );
105+ _matrix .translate (_point .x , _point .y );
148106
149107 if (isPixelPerfectRender (camera ))
150- preparePixelPerfectMatrix (matrix );
151-
152- if (__shouldDoZoomFactor () || __shouldDoAngleFactor ()) {
153- __prepareZoomFactor (_rect2 , camera );
154-
155- if (__shouldDoZoomFactor ()) {
156- matrix .setTo (
157- matrix .a * _rect2 .width , matrix .b * _rect2 .height ,
158- matrix .c * _rect2 .width , matrix .d * _rect2 .height ,
159- (matrix .tx - _rect2 .x ) * _rect2 .width + _rect2 .x ,
160- (matrix .ty - _rect2 .y ) * _rect2 .height + _rect2 .y
161- );
162- }
163- if (__shouldDoAngleFactor ()) {
164- matrix .translate (- _rect2 .x , - _rect2 .y );
165- matrix .rotate (FlxAngle .asRadians (__prepareAngleFactor (camera )));
166- matrix .translate (_rect2 .x , _rect2 .y );
167- }
108+ {
109+ _matrix .tx = Math .floor (_matrix .tx );
110+ _matrix .ty = Math .floor (_matrix .ty );
168111 }
169- }
170112
171- function preparePixelPerfectMatrix (matrix : FlxMatrix ): Void
172- {
173- matrix .tx = Math .floor (matrix .tx );
174- matrix .ty = Math .floor (matrix .ty );
175- }
176-
177- // semi stolen from FlxSkewedSprite
178- static var _skewMatrix : FlxMatrix = new FlxMatrix ();
113+ // Copied from FunkinSprite
114+ final ox = camera .width * 0.5 , oy = camera .height * 0.5 ;
115+ final sx = (camera .scaleX > 0.0 ? Math .max : Math .min )(0.0 , (1.0 - zoomFactor ) / camera .scaleX + zoomFactor );
116+ final sy = (camera .scaleY > 0.0 ? Math .max : Math .min )(0.0 , (1.0 - zoomFactor ) / camera .scaleY + zoomFactor );
117+
118+ if (zoomFactorEnabled && zoomFactor != 1 ) {
119+ matrix .setTo (
120+ matrix .a * sx , matrix .b * sy ,
121+ matrix .c * sx , matrix .d * sy ,
122+ (matrix .tx - ox ) * sx + ox ,
123+ (matrix .ty - oy ) * sy + oy
124+ );
125+ }
179126
180- private inline function updateSkew (): Void
181- {
182- _skewMatrix .setTo (1 , Math .tan (skew .y * FlxAngle .TO_RAD ), Math .tan (skew .x * FlxAngle .TO_RAD ), 1 , 0 , 0 );
127+ if (angleFactorEnabled && angleFactor != 1 ) {
128+ matrix .translate (- ox , - oy );
129+ matrix .rotate (- camera .angle * FlxAngle .TO_RAD * (1.0 - angleFactor ));
130+ matrix .translate (ox , oy );
131+ }
132+
133+ if (layer != null )
134+ layer .drawPixels (this , camera , _frame , framePixels , _matrix , colorTransform , blend , antialiasing , shaderEnabled ? shader : null );
135+ else
136+ camera .drawPixels (_frame , framePixels , _matrix , colorTransform , blend , antialiasing , shaderEnabled ? shader : null );
183137 }
184138
185- public override function destroy ()
139+ override function getScreenBounds ( ? rect : FlxRect , ? camera : FlxCamera ) : FlxRect
186140 {
187- super .destroy ();
188- _rect2 = FlxDestroyUtil .put (_rect2 );
189- skew = FlxDestroyUtil .put (skew );
190- transformMatrix = null ;
141+ if (camera == null ) camera = getDefaultCamera ();
142+ rect = super .getScreenBounds (rect , camera );
143+
144+ if (zoomFactorEnabled && zoomFactor != 1 ) {
145+ final ox = camera .width * 0.5 , oy = camera .height * 0.5 ;
146+ final sx = (camera .scaleX > 0.0 ? Math .max : Math .min )(0.0 , (1.0 - zoomFactor ) / camera .scaleX + zoomFactor );
147+ final sy = (camera .scaleY > 0.0 ? Math .max : Math .min )(0.0 , (1.0 - zoomFactor ) / camera .scaleY + zoomFactor );
148+
149+ rect .set (
150+ (rect .x - ox ) * sx + ox ,
151+ (rect .y - oy ) * sy + oy ,
152+ rect .width * sx ,
153+ rect .height * sy
154+ );
155+ }
156+
157+ return rect ;
191158 }
192- }
159+ }
0 commit comments