44#include < string>
55#include < vector>
66
7+ #include " Constants.h"
78#include " Font.h"
89#include " Graphics.h"
910#include " KeyPoll.h"
1213#include " UtilityClass.h"
1314#include " Vlogging.h"
1415
16+ static inline int adjust_center (std::vector<std::string>* text, uint32_t flags, int line)
17+ {
18+ if (flags & PR_CEN)
19+ {
20+ return font::len (flags, text->at (line).c_str ()) / 2 ;
21+ }
22+ return 0 ;
23+ }
24+
1525namespace TextInput
1626{
1727 bool taking_input;
@@ -210,7 +220,7 @@ namespace TextInput
210220 return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
211221 }
212222
213- char * select_part_first_line = UTF8_substr (get_line (rect.y ), rect.x , UTF8_total_codepoints (get_line (rect.y )) - rect. x );
223+ char * select_part_first_line = UTF8_substr (get_line (rect.y ), rect.x , UTF8_total_codepoints (get_line (rect.y )));
214224 char * select_part_last_line = UTF8_substr (get_line (rect.y2 ), 0 , rect.x2 );
215225
216226 // Loop through the lines in between
@@ -220,7 +230,7 @@ namespace TextInput
220230 total_length += SDL_strlen (get_line (i)) + 1 ;
221231 }
222232
223- char * select_part = (char *)SDL_malloc (total_length);
233+ char * select_part = (char *)SDL_malloc (total_length + 1 );
224234 strcpy (select_part, select_part_first_line);
225235 strcat (select_part, " \n " );
226236 for (int i = rect.y + 1 ; i < rect.y2 ; i++)
@@ -229,6 +239,7 @@ namespace TextInput
229239 strcat (select_part, " \n " );
230240 }
231241 strcat (select_part, select_part_last_line);
242+ strcat (select_part, " \0 " );
232243
233244 SDL_free (select_part_first_line);
234245 SDL_free (select_part_last_line);
@@ -776,6 +787,11 @@ namespace TextInput
776787 const int visible_lines = info.visible_lines ;
777788 const int visible_padding = info.visible_padding ;
778789
790+ if ((flags & PR_CEN) && text_x == -1 )
791+ {
792+ text_x = SCREEN_WIDTH_PIXELS / 2 ;
793+ }
794+
779795 if (cursor_pos.y < display_offset + visible_padding)
780796 {
781797 display_offset = SDL_max (0 , TextInput::cursor_pos.y - visible_padding);
@@ -792,7 +808,7 @@ namespace TextInput
792808 {
793809 if (i + display_offset < (int ) text->size ())
794810 {
795- font::print (flags, text_x, text_y + (i * font_height), text->at (i + display_offset), text_color.r , text_color.g , text_color.b );
811+ font::print (flags & ~PR_CEN , text_x - adjust_center (text, flags, i + display_offset) , text_y + (i * font_height), text->at (i + display_offset), text_color.r , text_color.g , text_color.b );
796812 }
797813 }
798814
@@ -813,9 +829,10 @@ namespace TextInput
813829
814830 char * offset_x = UTF8_substr (text->at (rect.y ).c_str (), 0 , rect.x );
815831 char * cut_string = UTF8_substr (text->at (rect.y ).c_str (), rect.x , rect.x2 );
832+ int align_offset = adjust_center (text, flags, rect.y );
816833
817- graphics.fill_rect (text_x + font::len (flags, offset_x), text_y + (y - display_offset) * font_height, font::len (flags, cut_string), font_height, text_color);
818- font::print (flags, text_x + font::len (flags, offset_x), text_y + (rect.y2 - display_offset) * font_height, cut_string, selected_color.r , selected_color.g , selected_color.b );
834+ graphics.fill_rect (text_x + font::len (flags, offset_x) - align_offset , text_y + (y - display_offset) * font_height, font::len (flags, cut_string), font_height, text_color);
835+ font::print (flags & ~PR_CEN , text_x + font::len (flags, offset_x) - align_offset , text_y + (rect.y2 - display_offset) * font_height, cut_string, selected_color.r , selected_color.g , selected_color.b );
819836
820837 SDL_free (offset_x);
821838 SDL_free (cut_string);
@@ -829,9 +846,10 @@ namespace TextInput
829846 const char * line = text->at (rect.y ).c_str ();
830847 char * offset_x = UTF8_substr (line, 0 , rect.x );
831848 char * selection_w = UTF8_substr (line, rect.x , UTF8_total_codepoints (line));
849+ int align_offset = adjust_center (text, flags, rect.y );
832850
833- graphics.fill_rect (text_x + font::len (flags, offset_x), text_y + (rect.y - display_offset) * font_height, SDL_max (font::len (PR_FONT_LEVEL, selection_w), 1 ), font_height, text_color);
834- font::print (flags, text_x + font::len (flags, offset_x), text_y + (rect.y - display_offset) * font_height, selection_w, selected_color.r , selected_color.g , selected_color.b );
851+ graphics.fill_rect (text_x + font::len (flags, offset_x) - align_offset , text_y + (rect.y - display_offset) * font_height, SDL_max (font::len (PR_FONT_LEVEL, selection_w), 1 ), font_height, text_color);
852+ font::print (flags & ~PR_CEN , text_x + font::len (flags, offset_x) - align_offset , text_y + (rect.y - display_offset) * font_height, selection_w, selected_color.r , selected_color.g , selected_color.b );
835853
836854 SDL_free (offset_x);
837855 SDL_free (selection_w);
@@ -843,9 +861,10 @@ namespace TextInput
843861 if (local_y >= 0 && local_y < visible_lines)
844862 {
845863 const int line_width = SDL_max (font::len (flags, text->at (rect.y + i).c_str ()), 1 );
864+ int align_offset = adjust_center (text, flags, rect.y + i);
846865
847- graphics.fill_rect (text_x, text_y + local_y * font_height, line_width, font_height, text_color);
848- font::print (flags, text_x, text_y + local_y * font_height, text->at (rect.y + i).c_str (), selected_color.r , selected_color.g , selected_color.b );
866+ graphics.fill_rect (text_x - align_offset , text_y + local_y * font_height, line_width, font_height, text_color);
867+ font::print (flags & ~PR_CEN , text_x - align_offset , text_y + local_y * font_height, text->at (rect.y + i).c_str (), selected_color.r , selected_color.g , selected_color.b );
849868 }
850869 }
851870
@@ -854,9 +873,10 @@ namespace TextInput
854873 const char * line_2 = text->at (rect.y2 ).c_str ();
855874 char * selection_w = UTF8_substr (line_2, 0 , rect.x2 );
856875 const int line_width = SDL_max (font::len (flags, selection_w), 1 );
876+ int align_offset = adjust_center (text, flags, rect.y2 );
857877
858- graphics.fill_rect (text_x, text_y + (rect.y2 - display_offset) * font_height, line_width, font_height, text_color);
859- font::print (flags, text_x, text_y + (rect.y2 - display_offset) * font_height, selection_w, selected_color.r , selected_color.g , selected_color.b );
878+ graphics.fill_rect (text_x - align_offset , text_y + (rect.y2 - display_offset) * font_height, line_width, font_height, text_color);
879+ font::print (flags & ~PR_CEN , text_x - align_offset , text_y + (rect.y2 - display_offset) * font_height, selection_w, selected_color.r , selected_color.g , selected_color.b );
860880
861881 SDL_free (selection_w);
862882 }
@@ -867,17 +887,18 @@ namespace TextInput
867887 if (TextInput::flash_timer < 15 )
868888 {
869889 char * substr = UTF8_substr (text->at (TextInput::cursor_pos.y ).c_str (), 0 , TextInput::cursor_pos.x );
890+ int align_offset = adjust_center (text, flags, TextInput::cursor_pos.y );
870891
871892 if (TextInput::cursor_pos.x < (int ) text->at (TextInput::cursor_pos.y ).size () || TextInput::selecting)
872893 {
873894 graphics.set_color (text_color);
874- int x = text_x + font::len (flags, substr);
895+ int x = text_x + font::len (flags, substr) - align_offset ;
875896 int y = text_y + ((TextInput::cursor_pos.y - display_offset) * font_height);
876897 SDL_RenderDrawLine (gameScreen.m_renderer , x, y, x, y + font_height - 1 );
877898 }
878899 else
879900 {
880- font::print (flags, text_x + font::len (flags, substr), text_y + ((TextInput::cursor_pos.y - display_offset) * font_height), " _" , text_color.r , text_color.g , text_color.b );
901+ font::print (flags & ~PR_CEN , text_x + font::len (flags, substr) - align_offset , text_y + ((TextInput::cursor_pos.y - display_offset) * font_height), " _" , text_color.r , text_color.g , text_color.b );
881902 }
882903 SDL_free (substr);
883904 }
0 commit comments