Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
This resource is created for Game Maker 5 and isn't compatible with the latest version of Game Maker.
My apologies for the inconvinience.
Version: GM 5 Views: 9344 Script, Game Maker 5 compatible
Filesize: 2383 byte Creative Commons License

Textfield script

1/52/53/54/55/5 (13 votes)

A script which generates a basic windows text box.


 Textfield script
//////////////// Textfield script ////////////////////
//
// Copyright Simon Donkers 27-1-2005
// www.simondonkers.com - gmmentor@simondonkers.com
//
// This script generates a windows text box on the x and y with preset length.
// All characters typed after the length is reached will be ignored
// max. 80 characters can be typed in this text box!!!
// set 'treat uninitialised variables as value 0' to true
// the local variable text contains the typed text
// place in the draw event
// argument0 = x
// argument1 = y
// argument2 = width
//
/////////////////////////////////////////////////////
//draw box
brush_color := c_white;
pen_color := c_black;
draw_rectangle(argument0,argument1,argument0 + argument2,argument1 + string_height('ABC') + 5);
//if string doesn't fit in box shorten it
if selected then
{
        while string_width(keyboard_string)>argument2-5
        {
                keyboard_string := string_copy(keyboard_string,0,string_length(keyboard_string)-1)
        }
}
//if text isn't string make it one
if string(text) = '0' then
{
        text := '';
}
//draw text
if selected then
{
        draw_text(argument0 + 3,argument1 + 3,keyboard_string);
}
else
{
        draw_text(argument0 + 3,argument1 + 3,text);
}
//if pressed than select it
if mouse_x>argument0 and mouse_x<argument0 + argument2 and mouse_y>argument1 and mouse_y<argument1 + string_height('ABC') + 5 then
{
        set_cursor(cr_beam);
        if mouse_button = mb_left and selected = false then
        {
                keyboard_string := text;
                selected := true;
        }
}
else
{
        set_cursor(cr_arrrow);
}
//if pressed somewhere else than deselect it
if selected = true and mouse_button = mb_left and (not mouse_x>argument0 and mouse_x<argument0 + argument2 and mouse_y>argument1 and mouse_y<argument1 + string_height('ABC') + 5) then
{
        text := keyboard_string;
        selected := false;
}

Tip: Have a look at the tutorial How to execute a script

User comments

 joe posted at 2005-02-19 05:26:15Quote
It aint working buddy?it says unknown variable selected?
 Yibbon posted at 2005-02-19 07:04:35Quote
// set 'treat uninitialised variables as value 0' to true

-Look more closely....
 Ickie posted at 2005-05-14 05:37:41Quote
Nice script, now only gotta figure out how to put two in one room. o_O
 Javano posted at 2008-02-03 03:15:41Quote
i dont see 'treat uninitialised variables as value 0' anywhere in there
 Timber posted at 2009-10-06 10:39:22Quote
set 'treat uninitialised variables as value 0' to true

Where in script are ya supposed to do this? too bad, seemed like a cool script othervise, if it would have worked.
 RyShark posted at 2009-11-28 15:17:21Quote
Go to the global game settings on the error tab and tick the box next to 'treat uninitialised variables as value 0'
 RyShark posted at 2009-11-28 15:28:09Quote
Use this code to make it work on GM6/7


draw_set_color(c_black);
draw_rectangle(argument0,argument1,argument0 + argument2,argument1 + string_height('ABC') + 5,true);
draw_set_color(c_white);
draw_rectangle(argument0 + 1,argument1 + 1,argument0 + argument2 -1,argument1 + string_height('ABC') + 4,false);
draw_set_color(c_black);
//if string doesn't fit in box shorten it
if selected then
{
while string_width(keyboard_string)>argument2-5
{
keyboard_string := string_copy(keyboard_string,0,string_length(keyboard_string)-1)
}
}
//if text isn't string make it one
if string(text) = '0' then
{
text := '';
}
//draw text
if selected then
{
draw_text(argument0 + 3,argument1 + 3,keyboard_string);
}
else
{
draw_text(argument0 + 3,argument1 + 3,text);
}
//if pressed than select it
if mouse_x>argument0 and mouse_x<argument0 + argument2 and mouse_y>argument1 and mouse_y<argument1 + string_height('ABC') + 5 then
{
window_set_cursor(cr_beam);
if mouse_button = mb_left and selected = false then
{
keyboard_string := text;
selected := true;
}
}
else
{
window_set_cursor(cr_arrrow);
}
//if pressed somewhere else than deselect it
if selected = true and mouse_button = mb_left and (not mouse_x>argument0 and mouse_x<argument0 + argument2 and mouse_y>argument1 and mouse_y<argument1 + string_height('ABC') + 5) then
{
text := keyboard_string;
selected := false;
}

Reply

Name:
Site: