Skip to content

Commit d65d14f

Browse files
committed
Enable mouse wheel scrolling for Ufopaedia novels
1 parent 3406ca2 commit d65d14f

12 files changed

Lines changed: 81 additions & 21 deletions

‎src/Interface/Text.cpp‎

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
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

2829
namespace 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()
5354
void Text::setBig()
5455
{
5556
_font = _big;
57+
_fontOrig = _big;
5658
processText();
5759
}
5860

@@ -62,6 +64,7 @@ void Text::setBig()
6264
void 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)
99101
void 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
}

‎src/Interface/Text.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ enum TextVAlign { ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM };
4040
class Text : public InteractiveSurface
4141
{
4242
private:
43-
Font *_big, *_small, *_font;
43+
Font *_big, *_small, *_font, *_fontOrig;
4444
Language *_lang;
4545
std::string _text;
4646
UString _processedText;
4747
std::vector<int> _lineWidth, _lineHeight;
48-
bool _wrap, _invert, _contrast, _indent;
48+
bool _wrap, _invert, _contrast, _indent, _scroll;
4949
TextHAlign _align;
5050
TextVAlign _valign;
5151
Uint8 _color, _color2;
52+
int _scrollY;
5253

5354
/// Processes the contained text.
5455
void processText();
@@ -101,6 +102,10 @@ class Text : public InteractiveSurface
101102
int getTextHeight(int line = -1) const;
102103
/// Draws the text.
103104
void draw();
105+
/// Sets the text's scrollable setting.
106+
void setScrollable(bool scroll);
107+
/// Special handling for mouse presses.
108+
void mousePress(Action* action, State* state);
104109
};
105110

106111
}

‎src/Ufopaedia/ArticleStateArmor.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace OpenXcom
8585

8686
_txtInfo->setColor(Palette::blockOffset(14)+15);
8787
_txtInfo->setWordWrap(true);
88+
_txtInfo->setScrollable(true);
8889
_txtInfo->setText(tr(defs->text));
8990

9091
// Add armor values

‎src/Ufopaedia/ArticleStateBaseFacility.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ namespace OpenXcom
111111

112112
_txtInfo->setColor(Palette::blockOffset(13)+10);
113113
_txtInfo->setWordWrap(true);
114+
_txtInfo->setScrollable(true);
114115
_txtInfo->setText(tr(defs->text));
115116

116117
_lstInfo = new TextList(200, 42, 10, 42);

‎src/Ufopaedia/ArticleStateCraft.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace OpenXcom
6464

6565
_txtInfo->setColor(Palette::blockOffset(14)+15);
6666
_txtInfo->setWordWrap(true);
67+
_txtInfo->setScrollable(true);
6768
_txtInfo->setText(tr(defs->text));
6869

6970
_txtStats = new Text(defs->rect_stats.width, defs->rect_stats.height, defs->rect_stats.x, defs->rect_stats.y);

‎src/Ufopaedia/ArticleStateCraftWeapon.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace OpenXcom
6565

6666
_txtInfo->setColor(Palette::blockOffset(14)+15);
6767
_txtInfo->setWordWrap(true);
68+
_txtInfo->setScrollable(true);
6869
_txtInfo->setText(tr(defs->text));
6970

7071
_lstInfo = new TextList(250, 111, 5, 80);

‎src/Ufopaedia/ArticleStateItem.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ namespace OpenXcom
157157

158158
_txtInfo->setColor(Palette::blockOffset(14)+15);
159159
_txtInfo->setWordWrap(true);
160+
_txtInfo->setScrollable(true);
160161
_txtInfo->setText(tr(defs->text));
161162

162163

‎src/Ufopaedia/ArticleStateTFTD.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace OpenXcom
7070

7171
_txtInfo->setColor(Palette::blockOffset(0)+2);
7272
_txtInfo->setWordWrap(true);
73+
_txtInfo->setScrollable(true);
7374
_txtInfo->setText(tr(defs->text));
7475

7576
// all of the above are common to the TFTD articles.

‎src/Ufopaedia/ArticleStateText.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace OpenXcom
5959

6060
_txtInfo->setColor(Palette::blockOffset(15)-1);
6161
_txtInfo->setWordWrap(true);
62+
_txtInfo->setScrollable(true);
6263
_txtInfo->setText(tr(defs->text));
6364
}
6465

‎src/Ufopaedia/ArticleStateTextImage.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace OpenXcom
6161

6262
_txtInfo->setColor(Palette::blockOffset(15)-1);
6363
_txtInfo->setWordWrap(true);
64+
_txtInfo->setScrollable(true);
6465
_txtInfo->setText(tr(defs->text));
6566

6667
centerAllSurfaces();

0 commit comments

Comments
 (0)