11package funkin .backend ;
22
3+ import animate .FlxAnimate ;
34import flixel .FlxCamera ;
45import flixel .FlxG ;
5- import flixel .math .FlxMath ;
66import flixel .math .FlxAngle ;
77import flixel .math .FlxMatrix ;
88import flixel .math .FlxPoint ;
99import flixel .math .FlxRect ;
1010import flixel .text .FlxText ;
1111import flixel .util .FlxColor ;
1212import flixel .graphics .frames .FlxFrame ;
13+
1314import funkin .backend .system .Flags ;
1415
16+ @:access (animate. FlxAnimate )
1517class 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