Jump to content


Photo

Text box


  • Please log in to reply
4 replies to this topic

#1 1bodger

1bodger

    GMC Member

  • GMC Member
  • 367 posts
  • Version:GM8

Posted 12 August 2012 - 01:16 PM

How do you make a text box that you can writing when playing the game (like notepad).
Using Game Maker 8 lite.

Edited by 1bodger, 12 August 2012 - 01:18 PM.

  • 0

#2 Madales

Madales

    GMC Member

  • GMC Member
  • 62 posts
  • Version:GM8

Posted 12 August 2012 - 01:32 PM

  • Open notepad.exe
  • Paste into it this code:
    #define txt_create/*  txt_init(def,x1,y1,x2,y2,highlight,nocopy,nopaste,no#,nomove)****def = default text**x1,y1,x2,y2 = the bounding box of the text (NOTE: The text allways has a 1 char indent.)**highlight = the color text is highlighted (omiting this will give a black highlight, usualy not wanted)**nocopy = disallow ctrl c and ctrl x**nopaste = disallow ctrl v**no# = prevent the player entering the # symbol (NOTE: The # symbol replaces the ¬ symbol... So you cant have both)**nomove = lock the text in place (no scrolling)*********NOTE: For the font: Chose all chars!*******and note that the font used in gm scripts is Courier New size 10.****Returns: Nothing*///set up varstxt_str=argument0//the texttxt_pos_x=0//the bar posision on the screentxt_pos=0//the bar posisiontxt_line=0//the current linetxt_pos2=0//the other endtxt_pos_x2=0txt_line2=0txt_selected=""//what is selectedtxt_holdtime=0//how long keys have been heldtxt_action=0//what action is being remotly takentxt_fake_pos=-100//when using up&down - used for holding bar posision.txt_fake_pos2=-100txt_replace="¬"//the symbol that is disabled so that the # symbol is enabled.txt_highlight=argument5if argument8txt_replace=""//turn off the # symboltxt_copy=!argument6//allow copy/cuttxt_paste=!argument7//allow pastetxt_move=!argument9//allow the text to movetxt_x=argument1//the position of the text in the boxtxt_y=argument2txt_x1=argument1//the text boxtxt_y1=argument2txt_x2=argument3txt_y2=argument4#define txt_step/*  txt_step()******Use in the step event of the text object****WARNING: Changes the Font and disables keyboard_string.****Returns Nothing***/    var height,width;    width=string_width("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")/61    height=string_height("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")draw_set_font(txt_font)//txt_x+=width*(1.5-txt_move)txt_select();//txt_x-=width*(1.5-txt_move)txt_action=0;txt_type();if keyboard_check(vk_anykey) && keyboard_key!=vk_up && keyboard_key!=vk_down{    txt_fake_pos=-100    txt_fake_pos2=-100}if txt_fake_pos !=-100{    txt_pos=txt_fake_pos    txt_pos2=txt_fake_pos2}txt_pos=min(txt_pos,string_length(string_extract(txt_str,"#",txt_line)))txt_pos2=min(txt_pos2,string_length(string_extract(txt_str,"#",txt_line2)))txt_pos_x=string_width(string_copy(string_extract(txt_str,"#",txt_line),0,txt_pos))txt_pos_x2=string_width(string_copy(string_extract(txt_str,"#",txt_line2),0,txt_pos2))if txt_move//move the text{    while txt_pos_x2<txt_x1+width -txt_x    txt_x+=1    while txt_pos_x2>txt_x2-width -txt_x    txt_x-=1        while txt_line2*height<txt_y1 -txt_y    txt_y+=height        while txt_line2*height>txt_y2-height -txt_y    txt_y-=height}#define txt_draw/*  txt_draw()****draws the text at the current x,y pos.****Returns Nothing***/txt_font=argument0draw_set_blend_mode(argument1)d3d_transform_set_identity()var col,width;width=string_width("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")/61col=draw_get_color()draw_set_color(c_white)draw_rectangle(txt_x1,txt_y1,txt_x2,txt_y2,0)draw_set_font(txt_font)draw_set_color(c_black);if txt_line!=txt_line2 || txt_pos!=txt_pos2draw_set_color(txt_highlight);//draw selectionvar height;height=string_height("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")if !txt_movetxt_x+=widthif txt_line =txt_line2draw_rectangle(max(txt_pos_x+txt_x-1*(txt_pos=txt_pos2),txt_x1),height*txt_line+txt_y,min(txt_pos_x2+txt_x+1*(txt_pos=txt_pos2),txt_x2),height*(txt_line+1)+txt_y,0)//If the box is on one lineelse{    if txt_line<txt_line2//multi line select    {        draw_rectangle(max(txt_pos_x+txt_x,txt_x1),height*txt_line+txt_y,txt_x2,height*(txt_line+1)+txt_y,0)        draw_rectangle(max(txt_x,txt_x1),height*(txt_line+1)+txt_y,txt_x2,height*txt_line2+txt_y,0)        draw_rectangle(max(txt_x,txt_x1),height*txt_line2+txt_y,min(txt_pos_x2+txt_x,txt_x2),height*(txt_line2+1)+txt_y,0)    }    else    {        draw_rectangle(median(txt_pos_x+txt_x,txt_x1,txt_x2),height*txt_line+txt_y,min(txt_x,txt_x2),height*(txt_line+1)+txt_y,0)        draw_rectangle(max(txt_x,txt_x1),height*(txt_line)+txt_y,txt_x2,height*(txt_line2+1)+txt_y,0)        draw_rectangle(txt_x2,height*txt_line2+txt_y,median(txt_pos_x2+txt_x,txt_x1,txt_x2),height*(txt_line2+1)+txt_y,0)    }}draw_set_color(c_black);if txt_replace=""txt_draw_text(txt_x,txt_y,txt_str)elsetxt_draw_text(txt_x,txt_y,string_replace_all(txt_str,txt_replace,"\#"))if !txt_movetxt_x-=width//reset colordraw_set_color(col);#define txt_draw_text/*SUB SCRIPT****Used to find what chars should be shown - and draw them, with color coding****txt_draw_text(x,y,str)***/var i,s,l,height,c,width,str;c=draw_get_color()str=""width=string_width("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")/61height=string_height("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")l=-1for (i=(txt_y1-txt_y)/height;i<min(string_count("#",argument2)+1,(txt_y2-txt_y)/height);i+=1){    draw_set_color(c_black)    s=string_extract(argument2,"#",i)        for (l=l;l<string_length(s);l+=1)    {        if string_width(string_copy(s,0,l+1))+argument0<txt_x2-width           and string_width(string_copy(s,0,l+1))+argument0>txt_x1+width         str=string_insert(string_char_at(s,l+1),str,string_length(str))    }    str=string_insert("#",str,string_length(str))    l=0}str=string_copy(str,0,string_length(str)-2)draw_text(txt_x1+width,txt_y1,str)draw_set_color(c)#define txt_get_pos/*  txt_get_pos(line,pos)****line=the line of the text**pos=the position on the line****Returns: the total pos.*/var i,s,l;s=""for (i=0;i<argument0;i+=1)s+=string_extract(txt_str,"#",i)s+=string_copy(string_extract(txt_str,"#",argument0),0,argument1)l=string_length(s)return l#define txt_type/*SUB SCRIPT****used to allow the player to type.***/var str,clip,fakepos;str=""if txt_keyboard_check(vk_anykey){    str=keyboard_string            if keyboard_key=vk_backspace    {        str="BACKSPACE"    }        if keyboard_key=vk_enter    {        str="#"    }        if keyboard_key=222    {        //symbolises a # symbol - as the # symbol is normaly forbidden.        str=txt_replace        //so now the txt_replace symbol is disabled    }    if keyboard_check(vk_control) && keyboard_check_pressed(ord("A"))    {        txt_pos2=string_length(string_extract(txt_str,"#",string_count("#",txt_str)))        txt_line2=string_count("#",txt_str)        txt_pos_x2=string_width(string_extract(txt_str,"#",string_count("#",txt_str)))        txt_pos=0        txt_line=0        txt_pos_x=0        return 0    }        if txt_copy && keyboard_check(vk_control) && (txt_keyboard_check(ord("C")) || txt_keyboard_check(ord("X"))) && (txt_pos2!=txt_pos || txt_line2!=txt_line)    {        clipboard_set_text(string_replace_all(txt_selected,"#",chr(13)))        if txt_keyboard_check(ord("X"))        {            txt_str=string_delete(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line+1,-txt_get_pos(txt_line,txt_pos)-txt_line+(txt_get_pos(txt_line2,txt_pos2)+txt_line2))            txt_pos2=txt_pos            txt_pos_x2=txt_pos_x            txt_line2=txt_line        }        return 0    }        if txt_paste && keyboard_check(vk_control) && keyboard_check_pressed(ord("V")) && clipboard_has_text()    {    if txt_pos2!=txt_pos || txt_line2!=txt_line        {            txt_str=string_delete(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line+1,-txt_get_pos(txt_line,txt_pos)-txt_line+(txt_get_pos(txt_line2,txt_pos2)+txt_line2))            txt_pos2=txt_pos            txt_pos_x2=txt_pos_x            txt_line2=txt_line        }    clip=string_replace_all(clipboard_get_text(),chr(13),"#")    txt_str=string_insert(clip,txt_str,txt_get_pos(txt_line,txt_pos)+1+txt_line)    txt_line+=string_count("#",clip)    txt_pos+=string_length(string_extract(clip,"#",string_count("#",clip)))    txt_pos_x+=string_width(string_extract(clip,"#",string_count("#",clip)))        txt_pos2=txt_pos    txt_pos_x2=txt_pos_x    txt_line2=txt_line    return 0    }    keyboard_string=""        if str = "BACKSPACE"    {        if txt_pos=txt_pos2 && txt_line2=txt_line        {                if txt_pos=0                {                fakepos=string_length(string_extract(txt_str,"#",txt_line-1))                }                                txt_action=1 *!(txt_pos=string_length(string_extract(txt_str,"#",txt_line))+1) *!(txt_pos=0)                txt_str=string_delete(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line,1)                                if txt_pos=0                {                    txt_line-=1                    txt_line2-=1                    txt_pos=fakepos                    txt_pos2=fakepos                }        }        else        {        txt_str=string_delete(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line+1,-txt_get_pos(txt_line,txt_pos)-txt_line+(txt_get_pos(txt_line2,txt_pos2)+txt_line2))        txt_pos2=txt_pos        txt_pos_x2=txt_pos_x        txt_line2=txt_line        }                if txt_str=txt_selected        txt_str=""    }    else if str != ""    {        if txt_pos2!=txt_pos ||  txt_line2!=txt_line        {            txt_str=string_delete(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line+1,-txt_get_pos(txt_line,txt_pos)-txt_line+(txt_get_pos(txt_line2,txt_pos2)+txt_line2))            txt_pos2=txt_pos            txt_pos_x2=txt_pos_x            txt_line2=txt_line        }        txt_str=string_insert(string_char_at(str,0),txt_str,txt_get_pos(txt_line,txt_pos)+1+txt_line)        txt_action=2    } }#define txt_select/*SUB SCRIPT****Used to select text.*/var tx,tp,tl,same;//moving the bar up/downif txt_keyboard_check(vk_up){    if !keyboard_check(vk_shift)    {        if txt_fake_pos =-100        txt_fake_pos=txt_pos        txt_line=median(txt_line-1,0,string_count("#",txt_str))    }    if txt_fake_pos2 =-100    txt_fake_pos2=txt_pos2    txt_line2=median(txt_line2-1,0,string_count("#",txt_str))}if txt_keyboard_check(vk_down){    if !keyboard_check(vk_shift)    {        if txt_fake_pos =-100        txt_fake_pos=txt_pos        txt_line=median(txt_line+1,0,string_count("#",txt_str))    }    if txt_fake_pos2 =-100    txt_fake_pos2=txt_pos2    txt_line2=median(txt_line2+1,0,string_count("#",txt_str))}//use arrows to navigateif txt_keyboard_check(vk_right) || txt_action=2  || txt_action=3repeat (1+(txt_action=3))// +(string_copy(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line+1,2)=txt_replace){    if !keyboard_check(vk_shift) || txt_action=2  || txt_action=3    {        txt_pos+=1        txt_pos_x+=string_width(string_char_at(string_extract(txt_str,"#",txt_line),txt_pos))    }    else    {        txt_pos2+=1        txt_pos_x2+=string_width(string_char_at(string_extract(txt_str,"#",txt_line2),txt_pos2))    }        if txt_pos>string_length(string_extract(txt_str,"#",txt_line))    {        txt_pos_x=0        txt_pos=0        txt_line+=1    }        if txt_pos2>string_length(string_extract(txt_str,"#",txt_line2))    {        txt_pos_x2=0        txt_pos2=0        txt_line2+=1    }    if !keyboard_check(vk_shift) || txt_action=2  || txt_action=3    {        txt_pos2=txt_pos        txt_pos_x2=txt_pos_x        txt_line2=txt_line    }}if txt_keyboard_check(vk_left) || txt_action=1 || txt_action=4repeat (1+(txt_action=4))// +(string_copy(txt_str,txt_get_pos(txt_line,txt_pos)+txt_line-1,2)=txt_replace){    if !keyboard_check(vk_shift) || txt_action=1 || txt_action=4    {        txt_pos-=1        txt_pos_x-=string_width(string_char_at(string_extract(txt_str,"#",txt_line),txt_pos+1))    }    else    {        txt_pos2-=1        txt_pos_x2-=string_width(string_char_at(string_extract(txt_str,"#",txt_line2),txt_pos2+1))    }        if txt_pos<0    {        txt_pos_x=string_width(string_extract(txt_str,"#",txt_line-1))        txt_pos=string_length(string_extract(txt_str,"#",txt_line-1))        txt_line-=1    }        if txt_pos2<0    {        txt_pos_x2=string_width(string_extract(txt_str,"#",txt_line2-1))        txt_pos2=string_length(string_extract(txt_str,"#",txt_line2-1))        txt_line2-=1    }    if !keyboard_check(vk_shift) || txt_action=1 || txt_action=4    {        txt_pos2=txt_pos        txt_pos_x2=txt_pos_x        txt_line2=txt_line    }}//select textif mouse_check_button_pressed(mb_left){    txt_pos=txt_mouse(0)    txt_pos_x=txt_mouse(2)    txt_line=txt_mouse(1)}if mouse_check_button(mb_left){    txt_pos2=txt_mouse(0)    txt_pos_x2=txt_mouse(2)    txt_line2=txt_mouse(1)}//swap the positions of the selection if you dragged backwards.if !mouse_check_button(mb_left) && !keyboard_check(vk_shift){if txt_line>txt_line2 || (txt_line=txt_line2 && txt_pos>txt_pos2)    {        tp=txt_pos2        tx=txt_pos_x2        tl=txt_line2                txt_pos2=txt_pos        txt_pos_x2=txt_pos_x        txt_line2=txt_line                txt_pos=tp        txt_pos_x=tx        txt_line=tl    }}//limit the bar to the ends of the textif txt_line>string_count("#",txt_str){    txt_line=string_count("#",txt_str)    txt_pos_x=string_width(string_extract(txt_str,"#",txt_line))    txt_pos=string_length(string_extract(txt_str,"#",txt_line))}if txt_line2>string_count("#",txt_str){    txt_line2=string_count("#",txt_str)    txt_pos_x2=string_width(string_extract(txt_str,"#",txt_line2))    txt_pos2=string_length(string_extract(txt_str,"#",txt_line2))}if txt_line<0 {txt_line=0;txt_pos_x=0;txt_pos=0;}if txt_line2<0 {txt_line2=0;txt_pos_x2=0;txt_pos2=0;}//chose what text is selectedvar str,line,pos;str=""if txt_line=txt_line2{    str=string_copy(string_extract(txt_str,"#",txt_line),txt_pos+1,txt_pos2-txt_pos)}else{    str=str+string_copy(string_extract(txt_str,"#",txt_line),txt_pos+1,room_width)+"#"    for (line=txt_line+1;line<txt_line2;line+=1)    {        str=str+string_extract(txt_str,"#",line)+"#"    }    str=str+string_copy(string_extract(txt_str,"#",txt_line2),0,txt_pos2)}if !mouse_check_button(mb_left) && !keyboard_check(vk_shift)txt_selected=str#define txt_mouse/*SUB SCRIPT****Used to find the mouse pos****txt_mouse(type)**0=pos**1=line**2=pos_x*/var pp,str,height,mx;pp=0height=string_height("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")//mx=min(string_width(string_extract(txt_str,"#",floor((mouse_y-y)/height))),mouse_x)mx=mouse_xif argument0=1{    return floor((mouse_y-txt_y)/height)}str=string_extract(txt_str,"#",floor((mouse_y-txt_y)/height))for (pp=0;pp<string_width(str);pp+=1){    if string_width(string_copy(str,0,pp))>=mx-txt_x    break;}if argument0=2{    if mx>string_width(string_copy(str,0,pp))-string_width(string_char_at(str,pp))/2    return string_width(string_copy(str,0,pp))    else    return string_width(string_copy(str,0,pp-1))}if mx>string_width(string_copy(str,0,pp))-string_width(string_char_at(str,pp-1))/2return pp;elsereturn pp-1;#define txt_keyboard_check/*SUB SCRIPT****used for having once... delay... repeat... keyboard events.*/if keyboard_check_pressed(argument0){    return 1}if keyboard_check(argument0){    txt_holdtime+=1    if txt_holdtime>=20    return 1}if !keyboard_check(vk_anykey) || keyboard_key=vk_shift //shift dosnt counttxt_holdtime=0return 0#define string_extract/***  Usage:**      string_extract(str,sep,index)****  Arguments:**      str         string of elements**      sep         character or string seperating the elements**      index       element to return, {0..n-1}****  Returns:**      the element at the given index within a given string of elements****  Example:**      string_extract("cat,dog,mouse",",",1) == "dog"****  GMLscripts.com*/{    var str,sep,ind,len;    str = argument0;    sep = argument1;    ind = argument2;    len = string_length(sep)-1;    repeat (ind) str = string_delete(str,1,string_pos(sep,str)+len);    str = string_delete(str,string_pos(sep,str),string_length(str));    return str;}
  • Save the file as *.gml
  • Insert it into Game Maker
  • Create a new object
  • Insert txt_create in the create event with every arguments
  • Insert txt_step in the step event
  • Insert txt_draw in the draw event, adding the font and the blend mode to use (watch txt_draw script)
  • Done!

Script from : GMLscripts.com

- Madales
  • 0

#3 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 12 August 2012 - 01:48 PM

And I was about to say get_string() :P
  • 0

#4 Madales

Madales

    GMC Member

  • GMC Member
  • 62 posts
  • Version:GM8

Posted 12 August 2012 - 01:57 PM

How do you make a text box that you can writing when playing the game (like notepad).
Using Game Maker 8 lite.

As notepad, this is a multi-line text editor, get_string() function has only a line!

PS: get_string() can be very useful for small strings! :happy:
  • 0

#5 commanderej

commanderej

    GMC Member

  • GMC Member
  • 47 posts
  • Version:GM8

Posted 13 August 2012 - 01:34 AM

Actually Im pretty sure you can use a sharp sign to make a get_string multiline...
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users