Simon Donkers
Game Maker
Home > GameMaker > Scripts > Textfield script

Textfield script

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 andmouse_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 andmouse_x<argument0 + argument2 and mouse_y>argument1 and mouse_y<argument1 + string_height('ABC') + 5) then
{
        text := keyboard_string;
        selected := false;
}
Loading...