GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2020-04-11 02:23:50

lostdarkwolf
Member
Registered: 2015-11-19
Posts: 31

custom coded multiline text entry form

The main reason I made the fixhash scripts, was to try and make good multi-line text fields. The UI for my text fields isn't very pretty, but it is discreet. The functionality is very nice as well, and it doesn't use surfaces.

Some of the required scripts are found here: https://www.gmlscripts.com/forums/viewtopic.php?id=2475
The external required scripts are string_length_line, string_width_fixhash, string_height_fixhash, and chr_width.

It works just like a normal textbox, except that there is no scroll bars. Instead, there is a scroll indicator that only works with the mouse wheel. Note that you may also scroll automatically with arrow key navigation.

Example of use for two text fields:
Create event:
text_field_init()
text_field_prime(0,'ABC')
text_field_prime(1,'123')

step event:
text_field_step(0,0,true,true,x,y,250,100,true);
text_field_step(1,0,true,true,x,y+120,250,100,true);

draw event:
text_field_draw(0,x,y,250,100,$DDDDDD,c_lime,$1D1D1D);
text_field_draw(1,x,y+120,250,100,$DDDDDD,c_lime,$1D1D1D);

get text field content like this:
your_variable=global.SCR_text_field_string[text_field_id]
EDIT: 4-11-20 added missed script, string_line_pos.

Expand///string_line_pos(string, pos, front)
//
// returns the line where pos is at.
// the hash symbol does not produce a newline.
//
// string        the string to get length from, string
// line_number   the line number starting from one, real (integer >0)
//
/// GMLscripts.com/license

var var_back_stop, var_front_stop;
var_back_stop=argument1;
var_front_stop=argument1;
while string_char_at(argument0,var_back_stop-1)!=chr(10)
and string_char_at(argument0,var_back_stop-1)!=chr(13) 
and var_back_stop>=1 {
 var_back_stop-=1;
}
if argument2 {
 while string_char_at(argument0,var_front_stop)!=chr(10)
 and string_char_at(argument0,var_front_stop)!=chr(13) 
 and var_front_stop<=string_length(argument0) {
  var_front_stop+=1;
 }
}
return string_copy(argument0,var_back_stop,var_front_stop-var_back_stop);
Expand///draw_text_fixhash_cropped(x,y,text,left,top,width,height)
//
// draws only the cropped area of the specified string
// the hash symbol does not produce a newline
// this script requires the script string_width_fixhash.
//
// x        x position of the string, real
// y        y position of the string, real
// text     string to draw, string
// halign   horizontal alignment, real (0 = left; 1 = center; 2 = right)
// valign   vertical alignment, real (0 = top; 1 = middle; 2 = bottem)
//
// justification is always left
//
/// GMLscripts.com/license

var var_main_string, var_width, var_letter, var_ii, var_draw_x, var_draw_y, var_line_height, var_space_size;
var_main_string=argument2;
var_main_string=string_replace_all(var_main_string,chr(13)+chr(10),chr(10))
var_ii=1;
var_line_height=string_height('A');
var_space_size=string_width(' ');
var_width=string_width_fixhash(var_main_string);
var_draw_x=-argument3;
var_draw_y=-argument4;
var_spacing=0;
var_line_number=1 // current line number

repeat string_length(var_main_string) {
 var_letter=ord(string_char_at(var_main_string,var_ii))
 switch var_letter {
  case 9: // tab
   var_draw_x+=var_space_size*4; // left
  break;
  case 10: // newline
   var_line_number+=1;
   var_draw_y+=var_line_height;
   var_draw_x=-argument3
  break;
  case 13: // newline
   var_line_number+=1;
   var_draw_y+=var_line_height;
   var_draw_x=-argument3
  break;
  case 32: // space
   var_draw_x+=var_space_size;
  break;
 }
 // if letter is drawable
 if var_letter>=33 and var_letter<=126 {
  // if letter = "#" 
   if var_letter=35 {
    if var_draw_x>=0 and var_draw_y>=0 
    and var_draw_x+string_width('\#')<argument5 and  var_draw_y+var_line_height<argument6 {
     draw_text(argument0+var_draw_x,argument1+var_draw_y,'\#')
    }
    var_draw_x+=string_width('\#')+var_spacing;
   }
   else {
    if var_draw_x>=0 and var_draw_y>=0 
    and var_draw_x+string_width(chr(var_letter))<=argument5 and  var_draw_y+var_line_height<=argument6 {
     draw_text(argument0+var_draw_x,argument1+var_draw_y,chr(var_letter))
    }
    var_draw_x+=string_width(chr(var_letter))+var_spacing;
   }
 }
 var_ii+=1;
}
Expand///draw_text_pos(x,y,text,halign,valign,pos,char)
//
// draws a position indicator for the specified string
// the hash symbol does not produce a newline
// this script requires the scripts string_length_line, string_width_fixhash, and string_height_fixhash.
//
// x        x position of the string, real
// y        y position of the string, real
// text     string to draw, string
// halign   horizontal alignment, real (0 = left; 1 = center; 2 = right)
// valign   vertical alignment, real (0 = top; 1 = middle; 2 = bottem)
//
/// GMLscripts.com/license

var_justify=argument3;
var_justify_vert=argument4;
var_pos=argument5;
var_char=argument6;

var var_main_string, var_width, var_letter, var_ii, var_draw_x, var_draw_y, var_line_height, var_space_size;
var_main_string=argument2;
var_ii=0;
var_line_height=string_height('A');
var_space_size=string_width(' ');
var_width=string_width_fixhash(var_main_string);
if var_justify=0 { // left
 var_draw_x=0;
}
if var_justify=1 { // center
 var_draw_x=-string_width_fixhash(string_copy(var_main_string,1,string_length_line(var_main_string,1)))/2;
}
if var_justify=2 { // right
 var_draw_x=-string_width_fixhash(string_copy(var_main_string,1,string_length_line(var_main_string,1)));
}
if var_justify_vert=0 { // top
 var_draw_y=0;
}
if var_justify_vert=1 { // middle
 var_draw_y=-(string_height_fixhash(var_main_string))/2;
}
if var_justify_vert=2 { // bottem
 var_draw_y=-(string_height_fixhash(var_main_string));
}
var_spacing=0;
var_line_number=1 // current line number

repeat string_length(var_main_string)+1 {
 var_letter=ord(string_char_at(var_main_string,var_ii+1))
 
 // if letter = "#" 
 if var_letter=35 {
  if var_ii=var_pos {
   draw_text(argument0+var_draw_x-(string_width(var_char)/2),argument1+var_draw_y,var_char) // draw char
   return var_ii;
  }
  if var_justify=0 var_draw_x+=string_width('\#')+var_spacing;
  if var_justify=1 var_draw_x+=string_width('\#')+var_spacing;
  if var_justify=2 var_draw_x+=string_width('\#')+var_spacing;
 }
 else {
  if var_ii=var_pos {
   draw_text(argument0+var_draw_x-(string_width(var_char)/2),argument1+var_draw_y,var_char) // draw char
   return var_ii;
  }
  if var_justify=0 var_draw_x+=string_width(chr(var_letter))+var_spacing;
  if var_justify=1 var_draw_x+=string_width(chr(var_letter))+var_spacing;
  if var_justify=2 var_draw_x+=string_width(chr(var_letter))+var_spacing;
 }
 
 if var_letter=9 {
  if var_justify=0 var_draw_x+=var_space_size*4; // left
  if var_justify=1 var_draw_x+=var_space_size*2; // center
  if var_justify=2 var_draw_x+=var_space_size*4; // right
 }
 if var_letter=10 { // newline
  var_line_number+=1;
  var_draw_y+=var_line_height;
  if var_justify=0 var_draw_x=0
  if var_justify=1 var_draw_x=-string_width_fixhash(string_copy(var_main_string, var_ii+1,string_length_line(var_main_string,var_line_number)))/2;
  if var_justify=2 var_draw_x=-string_width_fixhash(string_copy(var_main_string,var_ii+1,string_length_line(var_main_string,var_line_number)));
 }
 if var_letter=13 { // newline
  var_line_number+=1;
  var_draw_y+=var_line_height;
  if var_justify=0 var_draw_x=0
  if var_justify=1 var_draw_x=-string_width_fixhash(string_copy(var_main_string, var_ii+1,string_length_line(var_main_string,var_line_number)))/2;
  if var_justify=2 var_draw_x=-string_width_fixhash(string_copy(var_main_string,var_ii+1,string_length_line(var_main_string,var_line_number)));
 }
 var_ii+=1;
}
Expand///string_mouse_pos_drawcrop_fixhash(x,y,text,left,top,width,height)
//
// returns a position in the specified string where the mouse is located
// returns -1 if no such charactor is found
// the hash symbol does not produce a newline
// this script requires the script string_width_fixhash
//
// x        x position of the string, real
// y        y position of the string, real
// text     string to draw, string
// halign   horizontal alignment, real (0 = left; 1 = center; 2 = right)
// valign   vertical alignment, real (0 = top; 1 = middle; 2 = bottem)
//
// justification is always left
//
/// GMLscripts.com/license


var var_main_string, var_width, var_letter, var_ii, var_draw_x, var_draw_y, var_line_height, var_space_size;
var_main_string=argument2;
var_main_string=string_replace_all(var_main_string,chr(13)+chr(10),chr(10))
var_ii=1;
var_line_height=string_height('A');
var_space_size=string_width(' ');
var_width=string_width_fixhash(var_main_string);
var_draw_x=-argument3;
var_draw_y=-argument4;
var_spacing=0;
var_line_number=1 // current line number

repeat string_length(var_main_string) {
 var_letter=ord(string_char_at(var_main_string,var_ii))
 switch var_letter {
  case 9: // tab
   var_draw_x+=var_space_size*4; // left
  break;
  case 10: // newline
   var_line_number+=1;
   var_draw_y+=var_line_height;
   var_draw_x=-argument3
  break;
  case 13: // newline
   var_line_number+=1;
   var_draw_y+=var_line_height;
   var_draw_x=-argument3
  break;
  case 32: // space
   var_draw_x+=var_space_size;
  break;
 }
 // if letter is drawable
 if var_letter>=33 and var_letter<=126 {
  // if letter = "#" 
   if var_letter=35 {
    if var_draw_x>=0 and var_draw_y>=0 
    and var_draw_x+string_width('\#')<argument5 and  var_draw_y+var_line_height<argument6 {
     if mouse_x=median(var_draw_x,var_draw_x+string_width('\#'),mouse_x)
     and mouse_y=median(var_draw_y,var_draw_y+string_height('\#'),mouse_y) {
      return var_ii;
     }
    }
    var_draw_x+=string_width('\#')+var_spacing;
   }
   else {
    if var_draw_x>=0 and var_draw_y>=0 
    and var_draw_x+string_width(chr(var_letter))<argument5 and  var_draw_y+var_line_height<argument6 {
     if mouse_x=median(argument0+var_draw_x,argument0+var_draw_x+string_width(chr(var_letter)),mouse_x)
     and mouse_y=median(argument1+var_draw_y,argument1+var_draw_y+string_height(chr(var_letter)),mouse_y) {
      return var_ii;
     }
    }
    var_draw_x+=string_width(chr(var_letter))+var_spacing;
   }
 }
 var_ii+=1;
}
return -1;
Expand///chr_printable(keycode, input_format, use_backspace, use_space)
//
// returns true if the keycode produces a printable charactor.
//
// keycode         virtual key code, real
// input_format    format of charactors to consider printable, real
// use_backspace   true if backspace is considered printable, real
// use_space       true if space is considered printable, real
//
/// GMLscripts.com/license

// List of values for input_format:
// 0 = all charactors access (US keyboard)
// 1 = letters only access
// 2 = real number access
// 3 = natural number access

if argument1=0 { // access all (US keyboard)
 if argument2 and argument0=8 return true; // backspace
 if argument3 and argument0=32 return true; // space
 switch argument0 {
  case 106: // numberpad *
  case 107: // numberpad +
  case 109: // numberpad -
  case 110: // numberpad .
  case 111: // numberpad /
  return true;
 }
 if argument0=median(48,57,argument0) return true; // number line 0 to 9
 if argument0=median(65,90,argument0) return true; // letters A to Z
 if argument0=median(96,105,argument0) return true; // numberpad numbers 0 to 9
 if argument0=median(187,192,argument0) return true; // OEMs  =+  ,<  -_  .>  /|  `~
 if argument0=median(219,222,argument0) return true; // OEMs  [{  \|  ]}  '"
}
if argument1=1 { // letter access
 if argument2 and argument0=8 return true; // backspace
 if argument3 and argument0=32 return true; // space
 if argument0=median(65,90,argument0) return true; // letters A to Z
}
if argument1=2 { // real number access
 if argument2 and argument0=8 return true; // backspace
 if argument0=189 and keyboard_check(vk_shift) return true;// 189 + shift = minus (OEM)
 if argument0=190 and not keyboard_check(vk_shift) return true;// 190 + no shift = point (OEM)
 if argument2 and argument0=8 return true; // backspace
 switch argument0 {
  case 109: // numberpad -
  case 110: // numberpad .
  return true;
 }
 if argument0=median(48,57,argument0) return true; // number line 0 to 9
 if argument0=median(96,105,argument0) return true; // numberpad numbers 0 to 9
}
if argument1=2 { // natural number access
 if argument2 and argument0=8 return true; // backspace
 if argument0=median(48,57,argument0) return true; // number line 0 to 9
 if argument0=median(96,105,argument0) return true; // numberpad numbers 0 to 9
}
Expand///text_field_prime()
//
// This script simply initializes the text field system
//
/// GMLscripts.com/license

global.SCR_text_field_repeat_time=0.09;
global.SCR_text_field_repeat_wait=0.4;
global.SCR_text_field_repeat_timer=ceil(room_speed*global.SCR_text_field_repeat_wait);
global.SCR_text_field_pos=1;
global.SCR_text_field_selpos=1;
global.SCR_text_field_active=-1;
global.SCR_text_field_scroll_x=0;
global.SCR_text_field_scroll_y=0;
global.SCR_text_field_edit_switch=false;
Expand///text_field_prime(text_field_id, default_string)
//
// This script can create a text field, as well as reset it.
// Provide your own id, use caution.
//
// text_field_id    desired text field ID to set. real (integer)
// default_string   default string for the text field, string
//
//
//
/// GMLscripts.com/license

global.SCR_text_field_string[argument0]=argument1;
global.SCR_text_field_scroll_y[argument0]=0;
global.SCR_text_field_scroll_x[argument0]=-8;
Expand///text_field_step(text_field_id, text_field_type, allow_backspace, allow_space, x, y, width, height, allow_enter)
//
// Runs per-step functions for a single textbox
// the hash symbol does not produce a newline
// Required scripts: chr_printable, string_width_fixhash, string_height_fixhash, string_line_pos, string_mouse_pos_drawcrop_fixhash.
// 
// text_field_id     integer ID for a single text field, real (integer)
// text_field_type   text field input allowance type, real (integer, see list below)
// allow_backspace   true if use of backspace is allowed, real (boolean)
// allow_space       true if use of space is allowed, real (boolean)
// x                 x location for the left side of the text field, real
// y                 y location for the top of the text field, real
// width             width of the text field in pixels, real
// height            height of the text field in pixels, real
// allow_enter       true if use of space is allowed, real (boolean)
//
/// GMLscripts.com/license

// List of values for text_field_type:
// 0 = all charactors access (US keyboard)
// 1 = letters only access
// 2 = real number access
// 3 = natural number access

// -- manage active text field --
if mouse_check_button_pressed(mb_left) {
 if mouse_x=median(argument4,argument4+argument6,mouse_x)
 and mouse_y=median(argument5,argument5+argument7,mouse_y) {
  // set edit switch for showing the blinker
  global.SCR_text_field_edit_switch=true;
  if global.SCR_text_field_active!=argument0 global.SCR_text_field_active=argument0;
  // set pos to mouse pos on text click
  global.SCR_text_field_pos=string_mouse_pos_drawcrop_fixhash(argument4,argument5,global.SCR_text_field_string[argument0],global.SCR_text_field_scroll_x[argument0],global.SCR_text_field_scroll_y[argument0],argument6,argument7);
  // set pos to random pos on empty-space click
  if global.SCR_text_field_pos=-1 global.SCR_text_field_pos=1+irandom(string_length(global.SCR_text_field_string[argument0]))
 }
 if global.SCR_text_field_active=argument0 
 and mouse_x!=median(argument4,argument4+argument6,mouse_x)
 and mouse_y!=median(argument5,argument5+argument7,mouse_y) {
  global.SCR_text_field_active=-1;
 }
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}
if mouse_check_button(mb_left) { // selpos update
 if mouse_x=median(argument4,argument4+argument6,mouse_x)
 and mouse_y=median(argument5,argument5+argument7,mouse_y) {
  if global.SCR_text_field_active!=argument0 global.SCR_text_field_active=argument0;
  // set pos to random pos on click
  // global.SCR_text_field_pos=1+irandom(string_length(global.SCR_text_field_string[argument0]))
  // set pos to mouse pos on click
  global.SCR_text_field_selpos=string_mouse_pos_drawcrop_fixhash(argument4,argument5,global.SCR_text_field_string[argument0],global.SCR_text_field_scroll_x[argument0],global.SCR_text_field_scroll_y[argument0],argument6,argument7);
  if global.SCR_text_field_selpos=-1 global.SCR_text_field_selpos=global.SCR_text_field_pos
 }
}
if global.SCR_text_field_active!=argument0 return 0;

if keyboard_check(vk_anykey) {
 // set edit switch for showing the blinker
 global.SCR_text_field_edit_switch=true;
 // scroll ending type limit
 global.SCR_text_field_scroll_x[argument0]=max(global.SCR_text_field_scroll_x[argument0], -argument6+string_width_fixhash(string_line_pos(global.SCR_text_field_string[argument0],global.SCR_text_field_pos,false))+24)
 global.SCR_text_field_scroll_y[argument0]=max(global.SCR_text_field_scroll_y[argument0], -argument7+string_height_fixhash(string_copy(global.SCR_text_field_string[argument0],1,global.SCR_text_field_pos-1)))
 // scroll begining type limit
 global.SCR_text_field_scroll_x[argument0]=min(global.SCR_text_field_scroll_x[argument0], string_width_fixhash(string_line_pos(global.SCR_text_field_string[argument0],global.SCR_text_field_pos,false))-24)
 global.SCR_text_field_scroll_y[argument0]=min(global.SCR_text_field_scroll_y[argument0], string_height_fixhash(string_copy(global.SCR_text_field_string[argument0],1,global.SCR_text_field_pos-1))-string_height('A'))
}
// -- scroll zero limit --
global.SCR_text_field_scroll_x[argument0]=max(-8,global.SCR_text_field_scroll_x[argument0])
global.SCR_text_field_scroll_y[argument0]=max(0,global.SCR_text_field_scroll_y[argument0])

// -- extra navigation --
if keyboard_check(vk_end) {
 global.SCR_text_field_pos=string_length(global.SCR_text_field_string[argument0])+1
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}
if keyboard_check(vk_home) {
 global.SCR_text_field_pos=1;
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}

// -- cut, copy, and paste --
if keyboard_check(vk_control) {
 if keyboard_check_pressed(ord("X")) {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  clipboard_set_text(string_copy(global.SCR_text_field_string[argument0],var_min,max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min))
  
  // delete selection, if present
  if global.SCR_text_field_selpos!=global.SCR_text_field_pos {
   var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
   global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
   global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
   global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  }
 }
 if keyboard_check_pressed(ord("C")) {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  clipboard_set_text(string_copy(global.SCR_text_field_string[argument0],var_min,max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min))
 }
 if keyboard_check_pressed(ord("V")) {
  // delete selection, if present
  if global.SCR_text_field_selpos!=global.SCR_text_field_pos {
   var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
   global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
   global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
   global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  }

  var var_add_me; var_add_me=clipboard_get_text();
  if is_string(var_add_me) {
   global.SCR_text_field_string[argument0]=string_insert(var_add_me,global.SCR_text_field_string[argument0],global.SCR_text_field_pos)
  }
  global.SCR_text_field_pos+=string_length(var_add_me); // ?? NOT RIGHT
  global.SCR_text_field_selpos=global.SCR_text_field_pos
 }
}

// -- key repeat --
if keyboard_key=vk_nokey or keyboard_check_pressed(keyboard_key) global.SCR_text_field_repeat_timer=ceil(room_speed*global.SCR_text_field_repeat_wait);
if keyboard_key!=vk_nokey and not keyboard_check_pressed(keyboard_key) global.SCR_text_field_repeat_timer-=1;

// -- mouse scrolling --
if mouse_x=median(argument4,argument4+argument6,mouse_x)
and mouse_y=median(argument5,argument5+argument7,mouse_y) {
 // set edit switch for hiding the blinker
 if mouse_wheel_up() or mouse_wheel_down() global.SCR_text_field_edit_switch=false;
 if mouse_y-(argument5+argument7)<mouse_x-(argument4+argument6) {
  global.SCR_text_field_scroll_y[argument0]+=(-mouse_wheel_up()+mouse_wheel_down())*8;
 }
 else {
  global.SCR_text_field_scroll_x[argument0]+=(-mouse_wheel_up()+mouse_wheel_down())*8;
 }
}

// -- arrow key navigation --
if keyboard_check_pressed(vk_right) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_right)) {
 if keyboard_check(vk_control) { // jump to space
  while string_char_at(global.SCR_text_field_string[argument0],global.SCR_text_field_pos)!=' ' 
  and global.SCR_text_field_pos<=string_length(global.SCR_text_field_string[argument0]) {
   global.SCR_text_field_pos+=1;
  }
 }
 else {
  if global.SCR_text_field_pos<=string_length(global.SCR_text_field_string[argument0]) global.SCR_text_field_pos+=1;
 }
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}
if keyboard_check_pressed(vk_left) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_left)) {
 if keyboard_check(vk_control) { // jump to space
  while string_char_at(global.SCR_text_field_string[argument0],global.SCR_text_field_pos)!=' ' 
  and global.SCR_text_field_pos>1 {
   global.SCR_text_field_pos-=1;
  }
 }
 else {
  if global.SCR_text_field_pos>1 global.SCR_text_field_pos-=1;
 }
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}
if keyboard_check_pressed(vk_up) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_up)) {
 // control to scroll and go up
 if keyboard_check(vk_control){
  global.SCR_text_field_scroll_y[argument0]=max(0,global.SCR_text_field_scroll_y[argument0]-string_height('A'))
 }
 
 var var_newline_pos, var_new_pos_x;
 var_newline_pos=max(global.SCR_text_field_pos-1,1);
 while (string_char_at(global.SCR_text_field_string[argument0],var_newline_pos)!=chr(10)
 and    string_char_at(global.SCR_text_field_string[argument0],var_newline_pos)!=chr(13)) 
 and   var_newline_pos>1 {
  var_newline_pos-=1;
 }
 
 var_new_pos_x=string_width_fixhash(string_copy(global.SCR_text_field_string[argument0],var_newline_pos+1,max(global.SCR_text_field_pos-var_newline_pos-2,1)))-3
 
 global.SCR_text_field_pos=var_newline_pos;
 while string_width_fixhash(string_line_pos(global.SCR_text_field_string[argument0],global.SCR_text_field_pos-1,false))>var_new_pos_x
 and   global.SCR_text_field_pos>1 {
  global.SCR_text_field_pos-=1;
 }
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}
if keyboard_check_pressed(vk_down) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_down)) {
 // control to scroll and go down
 if keyboard_check(vk_control){
  global.SCR_text_field_scroll_y[argument0]=min(string_height_fixhash(global.SCR_text_field_string[argument0])-string_height('A'),global.SCR_text_field_scroll_y[argument0]+string_height('A'))
 }

 var var_newline_pos, var_newline_pos2, var_old_pos, var_new_pos_x, var_find_me;
 var_newline_pos=global.SCR_text_field_pos;
 while (string_char_at(global.SCR_text_field_string[argument0],var_newline_pos)!=chr(10)
 and    string_char_at(global.SCR_text_field_string[argument0],var_newline_pos)!=chr(13)) 
 and   var_newline_pos<=string_length(global.SCR_text_field_string[argument0]) {
  var_newline_pos+=1;
 }
 var_newline_pos2=global.SCR_text_field_pos-1;
 while (string_char_at(global.SCR_text_field_string[argument0],var_newline_pos2)!=chr(10)
 and    string_char_at(global.SCR_text_field_string[argument0],var_newline_pos2)!=chr(13)) 
 and   var_newline_pos2>0 {
  var_newline_pos2-=1;
 }
 
 var_new_pos_x=string_width(string_copy(global.SCR_text_field_string[argument0],var_newline_pos2+1,global.SCR_text_field_pos-var_newline_pos2-1))-3
 
 global.SCR_text_field_pos=min(var_newline_pos+1,1+string_length(global.SCR_text_field_string[argument0]));

 while string_width(string_line_pos(global.SCR_text_field_string[argument0],global.SCR_text_field_pos,false))<var_new_pos_x
 and   global.SCR_text_field_pos<=string_length(global.SCR_text_field_string[argument0]) 
 and   string_char_at(global.SCR_text_field_string[argument0],global.SCR_text_field_pos)!=chr(10)
 and   string_char_at(global.SCR_text_field_string[argument0],global.SCR_text_field_pos)!=chr(13) {
  
  global.SCR_text_field_pos+=1;
 }
 if not keyboard_check(vk_shift) global.SCR_text_field_selpos=global.SCR_text_field_pos;
}

// -- text entry --
if keyboard_check_pressed(keyboard_key) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(keyboard_key)) 
and chr_printable(keyboard_key,argument1,false,argument3) 
and not keyboard_check(vk_control) {
 // delete selection, if present
 if global.SCR_text_field_selpos!=global.SCR_text_field_pos {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
  global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
 }
 // add last typed charactor to text field at edit position
 global.SCR_text_field_string[argument0]=string_insert(keyboard_lastchar,global.SCR_text_field_string[argument0],global.SCR_text_field_pos);
 // advance pos and selpos
 global.SCR_text_field_pos+=1;
 global.SCR_text_field_selpos+=1;
}
if (keyboard_check_pressed(vk_backspace) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_backspace))) and argument2 {
 // if no selection, remove current charactor (if possible)
 if global.SCR_text_field_selpos=global.SCR_text_field_pos {
  if global.SCR_text_field_pos>1 {
   global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0],global.SCR_text_field_pos-1,1);
   // subtract from pos and selpos
   global.SCR_text_field_pos-=1;
   global.SCR_text_field_selpos-=1;
  }
 }
 // else, delete selection.
 else {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
  global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
 }
}

// -- delete key --
if keyboard_check_pressed(vk_delete) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_delete)) {
 // if no selection, remove back charactor (if possible)
 if global.SCR_text_field_selpos=global.SCR_text_field_pos {
  if global.SCR_text_field_pos<=string_length(global.SCR_text_field_string[argument0]) global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0],global.SCR_text_field_pos,1);
 }
 // else, delete selection.
 else {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
  global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
 }
}
// -- enter key --
if (keyboard_check_pressed(vk_enter) or (global.SCR_text_field_repeat_timer=0 and keyboard_check(vk_enter))) and argument8 {
 // delete selection, if present
 if global.SCR_text_field_selpos!=global.SCR_text_field_pos {
  var var_min; var_min=min(global.SCR_text_field_selpos,global.SCR_text_field_pos); 
  global.SCR_text_field_string[argument0]=string_delete(global.SCR_text_field_string[argument0], var_min, max(global.SCR_text_field_selpos,global.SCR_text_field_pos)-var_min);
  global.SCR_text_field_selpos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
  global.SCR_text_field_pos=min(global.SCR_text_field_selpos,global.SCR_text_field_pos);
 }
 // add newline charactor to text field at edit position
 global.SCR_text_field_string[argument0]=string_insert(chr(10),global.SCR_text_field_string[argument0],global.SCR_text_field_pos);
 // advance pos and selpos
 global.SCR_text_field_pos+=1;
 global.SCR_text_field_selpos+=1;
}

// -- reset the repeat timer (if needed) --
if global.SCR_text_field_repeat_timer=0 {
 global.SCR_text_field_repeat_timer=ceil(room_speed*global.SCR_text_field_repeat_time);
}
Expand///text_field_draw(id, x, y, width, height, col_front, col_highlight, col_back)
//
// draws a single text field.
// required scripts: draw_text_pos, draw_text_fixhash_cropped.
//
// id              integer ID for a single text field, real (integer)
// x               x location for the left side of the text field, real
// y               y location for the top of the text field, real
// width           width of the text field in pixels, real
// height          height of the text field in pixels, real
// col_front       color of outline, text, and blinker, real (color)
// col_highlight   color of highlights and mouse scroll indicator, real (color)
// col_back        color of the text field's background, real (color)
//
/// GMLscripts.com/license

color_1=argument5;
color_2=argument6;
color_3=argument7;

// outline
draw_set_color(color_1)
draw_rectangle(argument1,argument2,argument1+argument3,argument2+argument4,true);

// backdrop
draw_set_color(color_3)
draw_rectangle(argument1,argument2,argument1+argument3,argument2+argument4,false);

// mouse scroll indicator
if mouse_x=median(argument1,argument1+argument3,mouse_x)
and mouse_y=median(argument2,argument2+argument4,mouse_y) 
and irandom(8) {
 draw_set_color(color_2)
 if mouse_y-(argument2+argument4)<mouse_x-(argument1+argument3) {
  draw_line(argument1+argument3-1,argument2,argument1+argument3-1,argument2+argument4)
 }
 else {
  draw_line(argument1,argument2+argument4-1,argument1+argument3,argument2+argument4-1)
 }
}

// text blinker cursor
if irandom(8) and global.SCR_text_field_edit_switch=true and global.SCR_text_field_active=argument0 {
 draw_set_color(color_1)
 draw_text_pos(argument1-global.SCR_text_field_scroll_x[argument0],argument2-global.SCR_text_field_scroll_y[argument0],global.SCR_text_field_string[argument0],0,0,global.SCR_text_field_pos-1,"|")
 if global.SCR_text_field_selpos!=global.SCR_text_field_pos {
  draw_set_color(color_2)
  draw_text_pos(argument1-global.SCR_text_field_scroll_x[argument0],argument2-global.SCR_text_field_scroll_y[argument0],global.SCR_text_field_string[argument0],0,0,global.SCR_text_field_selpos-1,"|")
 }
}

// text feild contents
draw_set_color(color_1)
draw_text_fixhash_cropped(argument1,argument2,global.SCR_text_field_string[argument0],global.SCR_text_field_scroll_x[argument0],global.SCR_text_field_scroll_y[argument0],argument3,argument4)

Last edited by lostdarkwolf (2020-04-11 12:37:40)

Offline

Board footer

Powered by FluxBB