Android Cannot Align Text Horizontally.

J

Jamonnin

Guest
I've tried setting coordinates using room width and height, as well as a few other methods. I've been googling for 2 hours and cannot find an efficient way to center drawn text onto the screen no matter the android device.
 
If you want it centered on the screen and not the room, use the draw gui event and find the center with the display_get_gui_width/height functions.
Toghether with the draw_set_halign/valign functions your text should be centered.
 

TsukaYuriko

☄️
Forum Staff
Moderator
I already tried this and it didn't work..
One of the most important rules of asking for help is to never say "doesn't work". Interpret an urge to say that as a clear sign that you need to provide more information, including but not limited to what you tried, what you expected to happen and what happened instead.
 
M

Maximus

Guest
Here's a script that does what @The Reverend suggested:

Code:
/// draw_text_gui_hcentre(offset, yy, h_align, v_align, str)
/* 
*   example use:
*   Draw GUI event: 
*   draw_text_gui_hcentre(0, 0, fa_center, fa_top, "This text is in the top centre of the GUI");
*/ 
{
    var offset, yy, h_align, v_align, str;
    offset      = argument0;
    yy          = argument1;
    h_align     = argument2;
    v_align     = argument3;
    str         = argument4;
    var centre_x, xx;
    centre_x    = 0.5*display_get_gui_width();
    xx = centre_x + offset;
    draw_set_halign(h_align);
    draw_set_valign(v_align);
     
    draw_text(xx, yy, str);
}
 
J

Jamonnin

Guest
One of the most important rules of asking for help is to never say "doesn't work". Interpret an urge to say that as a clear sign that you need to provide more information, including but not limited to what you tried, what you expected to happen and what happened instead.
Very good point. Usually I would, I was just frustrated because I spent 5-7 hours creating a menu system I would use for all my games, and ended up just scrapping it because of one tiny detail that was fixable. Very stupid thing to do. Thanks for the help.


Here's a script that does what @The Reverend suggested:

Code:
/// draw_text_gui_hcentre(offset, yy, h_align, v_align, str)
/*
*   example use:
*   Draw GUI event:
*   draw_text_gui_hcentre(0, 0, fa_center, fa_top, "This text is in the top centre of the GUI");
*/
{
    var offset, yy, h_align, v_align, str;
    offset      = argument0;
    yy          = argument1;
    h_align     = argument2;
    v_align     = argument3;
    str         = argument4;
    var centre_x, xx;
    centre_x    = 0.5*display_get_gui_width();
    xx = centre_x + offset;
    draw_set_halign(h_align);
    draw_set_valign(v_align);
    
    draw_text(xx, yy, str);
}
I'll try a few variations of this now, thank you!
 
J

Jamonnin

Guest
Still struggling. I've tried three different techniques and they all fall short.

Code:
draw_set_halign(fa_center);
draw_set_valign(fa_top);
var test1 = display_get_gui_width();
var test2 = view_wview[0];
var test3 = room_width;
//scr_draw_text_outline(display_get_gui_width() / 2, display_get_gui_height() / 2, 5, "test1");
//scr_draw_text_outline(view_wview[0] / 2, view_hview[0] / 3, 5, "test2");
//scr_draw_text_outline(room_width / 2, room_height / 2, 5, "test3");

draw_text(display_get_gui_width() / 2, 128, test1);
draw_text(view_wview[0] / 2, 360, test2);
draw_text(room_width / 2, 720, test3);



Ran a few more tests. It seems as though if my font is over 100 pts it stops being centered. 96 pts and below it centers perfectly.
 
Last edited by a moderator:
I couldn't reproduce that behaviour. Had this in Draw GUI event:
Code:
display_set_gui_maximise(); // would normally call only once i.e. in create event
xx = display_get_gui_width() / 2;

draw_set_halign(fa_center);
txt = string(display_get_gui_width());

draw_set_font(fnt_small);
draw_text(xx, 128, txt);
draw_set_font(fnt_large);
draw_text(xx, 256, txt);
draw_set_font(fnt_huge);
draw_text(xx, 512, txt);

draw_set_font(-1);
Where fnt_huge was a pt 192 font and was still centered.

Also tested with two fonts: arial and venus rising.
Tested on different window sizes: 768p & 1080p.
It was always working as expected.
 
J

Jamonnin

Guest
I couldn't reproduce that behaviour. Had this in Draw GUI event:
Code:
display_set_gui_maximise(); // would normally call only once i.e. in create event
xx = display_get_gui_width() / 2;

draw_set_halign(fa_center);
txt = string(display_get_gui_width());

draw_set_font(fnt_small);
draw_text(xx, 128, txt);
draw_set_font(fnt_large);
draw_text(xx, 256, txt);
draw_set_font(fnt_huge);
draw_text(xx, 512, txt);

draw_set_font(-1);
Where fnt_huge was a pt 192 font and was still centered.

Also tested with two fonts: arial and venus rising.
Tested on different window sizes: 768p & 1080p.
It was always working as expected.
That's strange. I honestly don't know what to say. I don't have to go above 96 pts so I'm just moving on for now. Thanks for the help :)
 
Top