@@ -8,19 +8,11 @@ std::shared_ptr<Menu> pMenu;
88
99// Initialise the menu.
1010
11- Menu::Menu (int widgetCount )
11+ Menu::Menu ()
1212 : hoveredWidget(-1 ), sprites(), hoverSound(), pressSound()
1313{
1414 pMenu.reset (this );
1515
16- widgets.reserve (widgetCount);
17-
18- // Register the widget types.
19-
20- widgetTypes[0 ] = {vector2f (4 .0f , 1 .0f ), 0 }; // Small button.
21- widgetTypes[1 ] = {vector2f (4 .0f , 2 .0f ), 2 }; // Big button.
22- widgetTypes[2 ] = {vector2f (0 .0f , 1 .0f ), -1 }; // String.
23-
2416 // Load the necessary resources.
2517
2618 SpriteSheet sheet = pRenderer->GetSheet (" assets/sprites/widget/widget.bmp" );
@@ -69,6 +61,23 @@ void Menu::Update(float delta)
6961 pSoundMixer->PlaySound (pressSound);
7062 pOnEscape ();
7163 }
64+
65+ // Scroll the string widgets.
66+
67+ for (Widget& widget : widgets)
68+ {
69+ int stringLength = (int ) widget.string .length ();
70+
71+ if (widget.type == STRING && widget.maxLength != 0 && stringLength > widget.maxLength )
72+ {
73+ widget.scrollTime += delta * 2 .0f ;
74+
75+ if (widget.scrollTime >= (float ) (stringLength - widget.maxLength + 1 ))
76+ {
77+ widget.scrollTime = 0 .0f ;
78+ }
79+ }
80+ }
7281}
7382
7483// Render the menu.
@@ -85,7 +94,7 @@ void Menu::Render() const
8594
8695 // Draw the widget's sprite if applicable.
8796
88- if (widget.spriteIndex >= 0 )
97+ if (widget.type == BUTTON )
8998 {
9099 const Sprite& sprite = sprites[i == hoveredWidget ? widget.spriteIndex + 1 : widget.spriteIndex ];
91100
@@ -94,24 +103,64 @@ void Menu::Render() const
94103
95104 // Draw the widget's string.
96105
97- float x = widget.position .x + Clamp (widget.bounds .x * widget.alignment , 0 .25f , widget.bounds .x - 0 .25f );
98- float y = widget.position .y + widget.bounds .y - 0 .25f ;
106+ else
107+ {
108+ std::string string = widget.maxLength == 0 ? widget.string : widget.string .substr ((int ) widget.scrollTime , widget.maxLength );
109+ float x = widget.position .x + Clamp (widget.bounds .x * widget.alignment , 0 .25f , widget.bounds .x - 0 .25f );
110+ float y = widget.position .y + widget.bounds .y ;
99111
100- pRenderer->DrawString (widget.text , x, y, 4 .0f , widget.alignment );
112+ pRenderer->DrawString (string, x, y, 4 .0f , widget.alignment );
113+ }
101114 }
102115}
103116
104- // Add a widget to the menu;
105- // Alignment: 0.0 = left, 0.5 = centred, 1.0 = right.
117+ // Add a small button with a one-line string.
106118
107- void Menu::AddWidget (float x, float y, int type, std::function<void (int )> pOnPress, std::string text , float alignment)
119+ void Menu::AddSmallButton (float x, float y, std::function<void (int )> pOnPress, std::string string , float alignment)
108120{
109- vector2f bounds = widgetTypes[type].bounds ;
110121 vector2f position (x, y);
122+ vector2f bounds (4 .0f , 1 .0f );
123+
124+ widgets.reserve (2 );
111125
112126 position -= bounds * 0 .5f ;
127+ widgets.push_back ({BUTTON, position, bounds, std::move (pOnPress), 0 , " " , 0 .0f , 0 .0f , 0 });
128+
129+ // One line of text.
130+
131+ position.y -= 0 .25f ;
132+ widgets.push_back ({STRING, position, bounds, nullptr , 0 , std::move (string), alignment, 0 .0f , 9 });
133+ }
134+
135+ // Add a large button with a two-line string.
136+
137+ void Menu::AddLargeButton (float x, float y, std::function<void (int )> pOnPress, std::string string[2], float alignment)
138+ {
139+ vector2f position (x, y);
140+ vector2f bounds (4 .0f , 2 .0f );
141+
142+ widgets.reserve (3 );
143+
144+ position -= bounds * 0 .5f ;
145+ widgets.push_back ({BUTTON, position, bounds, std::move (pOnPress), 2 , " " , 0 .0f , 0 .0f , 0 });
146+
147+ // Two lines of text.
148+
149+ position.y -= 0 .375f ;
150+ widgets.push_back ({STRING, position, bounds, nullptr , 0 , std::move (string[0 ]), alignment, 0 .0f , 9 });
151+
152+ position.y -= 0 .75f ;
153+ widgets.push_back ({STRING, position, bounds, nullptr , 0 , std::move (string[1 ]), alignment, 0 .0f , 9 });
154+ }
155+
156+ // Add a string widget.
157+
158+ void Menu::AddString (float x, float y, std::string string, float alignment)
159+ {
160+ vector2f position (x, y - 0 .25f );
161+ vector2f bounds (0 .0f , 1 .0f );
113162
114- widgets.push_back ({position, bounds, widgetTypes[type]. spriteIndex , std::move (text ), alignment, std::move (pOnPress) });
163+ widgets.push_back ({STRING, position, bounds, nullptr , 0 , std::move (string ), alignment, 0 . 0f , 0 });
115164}
116165
117166// Clear the menu widgets.
0 commit comments