Legacy GM Simple Text Button

TheWaffle

Member
GM Version: GMS1.4
Target Platform: All
Download: N/A
Links: N/A

This is a wee bit more fancy than just a simple button object with text applied,
such as a typical "OK" button.

What this is, is a button that displays any text as multi-line and adjust itself and the background
to display it properly.

WHY?
In my game, I needed a simple way to ask the user complex questions and present the
user with options. The options are just simple "OK" buttons so I don't talk about them here.

example is in the image below:babybrother.PNG

I could have done custom buttons (easy) but would have required 100? objects. Too much work.
So this uses just one button with one sprite for the image (purple in this case, 32x32 solid color).
Collisions and other events are not required for this button because it only shows text.

First, the setup/ definition of this button called OBJ_msg:
msg = "Blank";
alarm[0]=5;
xoff=0;
yoff=0;
woff=100;
hoff=32;
fmark=0;
win_w=browser_width;
win_h=browser_height;
/// scale background
if (DEBUGMODE == true )
{
msg=msg+"#>"+room_get_name(room);
}
fmark=string_pos("#",msg);
hoff=10;
mstart=1;
if fmark==0 {mend=string_length(msg);}else{mend=fmark;}
woff=10;
msg3=msg;
do
{
msg2=string_copy(msg3,mstart,mend);
woff2= string_width(msg2)+10;
if woff2>woff {woff=woff2;}
hoff+=string_height("DUMMY");
mstart=mend+1;
msg3=string_copy(msg3,mstart,string_length(msg3));
mstart=1;
fmark=string_pos("#",msg3);
if fmark==0 {mend=string_length(msg3);}else{mend=fmark;}
}
until (string_length(msg3)==0);


x=320-woff/2;
y=480-(hoff)-32;
xoff=5;
yoff=5;
/// recenter
window_center();
/// check resize
if browser_width != win_w
{
alarm[1]=5;
}
if browser_height != win_h
{
alarm[1]=5;
}
win_w=browser_width;
win_h=browser_height;
draw_sprite_stretched(SPR_msg,0,x,y,woff,hoff);
draw_text(x+xoff,y+yoff,msg);

To use this button, I just drop it into any room that needs to show information
and right-click on it to edit creation code to add ....

msg= "Mommy has the flue and is very sick. #";
msg=msg+"Daddy went to work and will be home late.#"
msg=msg+"Your job is to take care of your baby brother.#"
msg=msg+"Do you accept the job?"


Things that will not work "out of the box" .....
DEBUGMODE is a macro I defined so that if a room does not look right,
I can read to see what room I am in so I can fix it ....

alarm1 and the step event are not required. I use them to see if the HTML browser has changed size
so I can re-center my game in the browser.

The real magik happens in alarm0.
A note on comments : My variables have descriptive names so I did not add extra comments.
__off indicates an offset from the default x/y position, so, w_off is a width offset and so on.

in the creation event I define msg="Blank" so that if this message is displayed,
I know I did not use the "Edit Creation Code" option in the room.

How this works ....
During creation, I set alarm0 to 5.
After the room is finished, the custom creation code is executed, followed by the code in alarm0;
Alarm0 scans the msg string (define in the creation event as an object variable)
looking for the "#" symbol to determine how many lines.
While scanning, it also needs to measure each line and find the longest line and set w_off to the maximum
and the x/y position shifted to center the text.

during the draw event, the background sprite is resized to the h_off/w_off values
and drawn at the new location with the text.

EDIT:
An alternative to using the sprite image, you could use the draw_box option in the draw event.
I like having the sprite image, even if not used, so the object is easier to see when dropped in the room.
 
Last edited:

TheWaffle

Member
Of course, if you require something more in depth,

is a tutorial by shawn, He always has cool stuff.
Mine is a bit more simplistic and simply works. His is more versitile, maybe I should have watched his tutorial first ;)
 
Top