Created
February 20, 2016 20:04
-
-
Save canab/432d43bde100d20fcc0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace ActionLib.Display | |
| { | |
| internal class TextFitAutoScale | |
| { | |
| private readonly TextBase _field; | |
| private bool _defaultValuesSet = false; | |
| private float _defaultFontScale; | |
| private VAlign _defaultVAlign; | |
| public TextFitAutoScale(TextBase field) | |
| { | |
| _field = field; | |
| } | |
| public void apply() | |
| { | |
| restoreDefaultValues(); | |
| if (_field is TextLabel) | |
| adjustSingleLine(); | |
| else | |
| adjustMultiLine(); | |
| } | |
| void adjustSingleLine() | |
| { | |
| var owerflow = Math.Max( | |
| _field.textWidth / _field.width, | |
| _field.textHeight / _field.height); | |
| if (owerflow > 1) | |
| { | |
| saveDefaultValues(); | |
| _field.fontScale /= owerflow; | |
| _field.vAlignment = VAlign.MIDDLE; | |
| } | |
| } | |
| void adjustMultiLine() | |
| { | |
| var scaleStep = 0.05f * _field.fontScale; | |
| while (true) | |
| { | |
| if (_field.textHeight <= _field.height && _field.textWidth <= _field.width) | |
| break; | |
| if (_field.fontScale - scaleStep <= 0) | |
| break; | |
| if (!_defaultValuesSet) | |
| saveDefaultValues(); | |
| _field.fontScale -= scaleStep; | |
| } | |
| } | |
| public void reset() | |
| { | |
| restoreDefaultValues(); | |
| _defaultValuesSet = false; | |
| } | |
| void restoreDefaultValues() | |
| { | |
| if (_defaultValuesSet) | |
| { | |
| _field.fontScale = _defaultFontScale; | |
| _field.vAlignment = _defaultVAlign; | |
| } | |
| } | |
| void saveDefaultValues() | |
| { | |
| if (!_defaultValuesSet) | |
| { | |
| _defaultFontScale = _field.fontScale; | |
| _defaultVAlign = _field.vAlignment; | |
| _defaultValuesSet = true; | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment