1717 * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919#include " Text.h"
20- #include < cmath >
20+ #include " ../fmath.h "
2121#include " ../Engine/Font.h"
2222#include " ../Engine/Options.h"
2323#include " ../Engine/Language.h"
2424#include " ../Engine/Unicode.h"
2525#include " ../Engine/ShaderDraw.h"
2626#include " ../Engine/ShaderMove.h"
27+ #include " ../Engine/Action.h"
2728
2829namespace OpenXcom
2930{
@@ -35,7 +36,7 @@ namespace OpenXcom
3536 * @param x X position in pixels.
3637 * @param y Y position in pixels.
3738 */
38- Text::Text (int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _big(0 ), _small(0 ), _font(0 ), _lang(0 ), _wrap(false ), _invert(false ), _contrast(false ), _indent(false ), _align(ALIGN_LEFT ), _valign(ALIGN_TOP ), _color(0 ), _color2(0 )
39+ Text::Text (int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _big(0 ), _small(0 ), _font(0 ), _fontOrig( 0 ), _lang(0 ), _wrap(false ), _invert(false ), _contrast(false ), _indent(false ), _scroll( false ), _align(ALIGN_LEFT ), _valign(ALIGN_TOP ), _color(0 ), _color2( 0 ), _scrollY (0 )
3940{
4041}
4142
@@ -53,6 +54,7 @@ Text::~Text()
5354void Text::setBig ()
5455{
5556 _font = _big;
57+ _fontOrig = _big;
5658 processText ();
5759}
5860
@@ -62,6 +64,7 @@ void Text::setBig()
6264void Text::setSmall ()
6365{
6466 _font = _small;
67+ _fontOrig = _small;
6568 processText ();
6669}
6770
@@ -88,8 +91,7 @@ void Text::initText(Font *big, Font *small, Language *lang)
8891 _big = big;
8992 _small = small;
9093 _lang = lang;
91- _font = _small;
92- processText ();
94+ setSmall ();
9395}
9496
9597/* *
@@ -99,11 +101,16 @@ void Text::initText(Font *big, Font *small, Language *lang)
99101void Text::setText (const std::string &text)
100102{
101103 _text = text;
104+ _font = _fontOrig;
102105 processText ();
103106 // If big text won't fit the space, try small text
104- if (_font == _big && ( getTextWidth () > getWidth () || getTextHeight () > getHeight ()) && _text[_text. size ()- 1 ] != ' . ' )
107+ if (! _text. empty () )
105108 {
106- setSmall ();
109+ if (_font == _big && (getTextWidth () > getWidth () || getTextHeight () > getHeight ()) && _text[_text.size () - 1 ] != ' .' )
110+ {
111+ _font = _small;
112+ processText ();
113+ }
107114 }
108115}
109116
@@ -307,6 +314,7 @@ void Text::processText()
307314 _processedText = Unicode::convUtf8ToUtf32 (_text);
308315 _lineWidth.clear ();
309316 _lineHeight.clear ();
317+ _scrollY = 0 ;
310318
311319 int width = 0 , word = 0 ;
312320 size_t space = 0 , textIndentation = 0 ;
@@ -502,22 +510,26 @@ void Text::draw()
502510 int color = _color;
503511 const UString &s = _processedText;
504512
505- for (std::vector<int >::iterator i = _lineHeight.begin (); i != _lineHeight.end (); ++i)
513+ height = getTextHeight ();
514+
515+ if (_scroll)
506516 {
507- height += *i ;
517+ y = _scrollY ;
508518 }
509-
510- switch (_valign)
519+ else
511520 {
512- case ALIGN_TOP :
513- y = 0 ;
514- break ;
515- case ALIGN_MIDDLE :
516- y = (int )ceil ((getHeight () - height) / 2.0 );
517- break ;
518- case ALIGN_BOTTOM :
519- y = getHeight () - height;
520- break ;
521+ switch (_valign)
522+ {
523+ case ALIGN_TOP :
524+ y = 0 ;
525+ break ;
526+ case ALIGN_MIDDLE :
527+ y = (int )ceil ((getHeight () - height) / 2.0 );
528+ break ;
529+ case ALIGN_BOTTOM :
530+ y = getHeight () - height;
531+ break ;
532+ }
521533 }
522534
523535 x = getLineX (line);
@@ -574,4 +586,37 @@ void Text::draw()
574586 }
575587}
576588
589+ /* *
590+ * Allows the text to be scrollable via mouse wheel.
591+ */
592+ void Text::setScrollable (bool scroll)
593+ {
594+ _scroll = scroll;
595+ }
596+
597+ /* *
598+ * Handles scrolling.
599+ * @param action Pointer to an action.
600+ * @param state State that the action handlers belong to.
601+ */
602+ void Text::mousePress (Action* action, State* state)
603+ {
604+ InteractiveSurface::mousePress (action, state);
605+ if (_scroll &&
606+ (action->getDetails ()->button .button == SDL_BUTTON_WHEELUP ||
607+ action->getDetails ()->button .button == SDL_BUTTON_WHEELDOWN ))
608+ {
609+ int scrollArea = getHeight () - getTextHeight ();
610+ if (scrollArea < 0 )
611+ {
612+ int scrollAmount = _font->getHeight () + _font->getSpacing ();
613+ if (action->getDetails ()->button .button == SDL_BUTTON_WHEELDOWN )
614+ scrollAmount = -scrollAmount;
615+
616+ _scrollY = Clamp (_scrollY + scrollAmount, scrollArea, 0 );
617+ _redraw = true ;
618+ }
619+ }
620+ }
621+
577622}
0 commit comments