Information about object: obj_textfield
Sprite: spr_textfield
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:
//define any of these variables in each instance's creation code
//which can be accesses by ctrl + right clicking the instance in the room
//if you do not define them, this code will define them with default values
label = "" //the label to the left of the textfield
label_font = -1 //font for the label, does not have to be monospaced
text = "" //at any time, you can simply retrieve the text variable
font = fnt_couriernew //works best with monospaced font
//if this is a password box, the font doesn't have to be monospaced
width = sprite_width - 4 //the width that text is allowed within
if(!variable_instance_exists(id, "password"))password = false
selected = false //whether this box starts out active or not
//this stuff probably shouldn't be modified
draw_set_font(font) //this is temporary, to determine character width
cw = string_width("*") //the width of a single character
click = false //whether the mouse button is being held down or not (selection)
pos = 1 //the cursor position
left = 1 //the display position (position of leftmost visible character)
sel = 1 //selection position (same as pos if no text selected)
cap = width div cw //maximum number of characters permitted in view
//reads the cursor blink rate from the registry
beam = "|" //the symbol used for the cursor - also in alarm event
blink = 700
if blink != -1 {
blink = blink/1200
blink = room_speed * blink
alarm = blink
}
Alarm Event for alarm 0:
execute code:
if beam != "|" {
beam = "|"
} else {
beam = ""
}
alarm = blink
Step Event:
execute code:
if !selected { exit }
if keyboard_string != "" {
if sel != pos {
text = string_delete(text,min(pos,sel),abs(pos-sel))
pos = min(pos,sel)
sel = pos
}
text = string_insert(keyboard_string,text,pos)
pos += string_length(keyboard_string)
sel = pos
if pos - left > cap {
left = pos - cap
} else if left + cap > string_length(text) {
left = max(1,string_length(text) - cap + 1)
}
if pos < left { left = pos }
keyboard_string = ""
alarm = blink
beam = "|"
}
Keyboard Event for <Backspace> Key:
execute code:
if !selected { exit }
keyboard_clear(vk_backspace)
alarm = blink
beam = "|"
if (pos = 1) && (sel = 1) { exit }
if pos = sel {
text = string_delete(text,pos - 1, 1)
pos -= 1
} else {
text = string_delete(text,min(pos,sel),abs(pos-sel))
pos = min(pos,sel)
}
sel = pos
if left + cap > string_length(text) {
left = max(1,string_length(text) - cap + 1)
}
if pos < left { left = pos }
Keyboard Event for <End> Key:
execute code:
if !selected { exit }
alarm = blink
beam = "|"
pos = string_length(text) + 1
left = max(0,string_length(text) - cap + 1)
if !keyboard_check(vk_shift) { sel = pos }
Keyboard Event for <Home> Key:
execute code:
if !selected { exit }
alarm = blink
beam = "|"
pos = 1
left = 1
if !keyboard_check(vk_shift) { sel = pos }
Keyboard Event for <Left> Key:
execute code:
if !selected { exit }
keyboard_clear(vk_left)
alarm = blink
beam = "|"
if !keyboard_check(vk_control) {
if pos > 1 { pos -= 1 }
if pos < left { left -= 1 }
} else if pos > 1 {
do {
pos -= 1
if pos < left { left -= 1 }
} until (pos = 1 || string_char_at(text,pos-1) = " ")
}
if !keyboard_check(vk_shift) { sel = pos }
Keyboard Event for <Right> Key:
execute code:
if !selected { exit }
keyboard_clear(vk_right)
alarm = blink
beam = "|"
if !keyboard_check(vk_control) {
if pos <= string_length(text) { pos += 1 }
if pos - left > cap { left += 1 }
} else if pos <= string_length(text) {
do {
pos += 1
if pos - left > cap { left += 1 }
} until (pos > string_length(text) || string_char_at(text,pos-1) = " ")
}
if !keyboard_check(vk_shift) { sel = pos }
Keyboard Event for <Delete> Key:
execute code:
if !selected { exit }
keyboard_clear(vk_delete)
alarm = blink
beam = "|"
if (pos > string_length(text)) && (sel > string_length(text)) { exit }
if sel = pos {
text = string_delete(text,pos,1)
} else {
text = string_delete(text,min(pos,sel),abs(pos-sel))
pos = min(pos,sel)
}
sel = pos
if left + cap > string_length(text) {
left = max(1,string_length(text) - cap + 1)
}
if pos < left { left = pos }
Mouse Event for Mouse Enter:
execute code: window_set_cursor(cr_beam)
Mouse Event for Mouse Leave:
execute code: window_set_cursor(cr_default)
Mouse Event for Glob Left Button:
execute code:
if !selected { exit }
if !click { exit }
sel = max(min((mouse_x - x) div cw, string_length(text)) + left,1)
Mouse Event for Glob Left Pressed:
execute code:
if position_meeting(mouse_x,mouse_y,self) {
alarm = blink
beam = "|"
keyboard_string = ""
selected = true
click = true
pos = min((mouse_x - x) div cw, string_length(text)) + left
if !keyboard_check(vk_shift) { sel = pos }
} else {
selected = false
}
Mouse Event for Glob Left Released:
execute code: click = false
Draw Event:
execute code:
var d2,m,p,pl,pr;
draw_set_color(c_black)
draw_set_font(font)
draw_sprite(sprite_index,0,x,y)
draw_set_font(label_font)
draw_text(x - string_width(label + " "), y, label)
draw_set_font(font)
if password {
d2 = ""
for (m = 0; m < string_length(text); m += 1) {
d2 += "*"
}
} else {
d2 = text
}
draw_text(x+4,y,string_copy(d2,left,cap))
if selected {
draw_text(x+min(pos-left,cap)*cw,y,beam)
if sel != pos {
p = min(max(sel,left),left+cap)
draw_rectangle(
x+4+(p-left)*cw,y,
x+4+min(pos-left,cap)*cw,y+sprite_height,false
)
draw_set_color(c_white)
pl = min(pos,p)
pr = max(pos,p)
draw_text(x+4+(pl-left)*cw,y,string_copy(d2,pl,pr-pl))
}
}
Key Press Event for A-key Key:
execute code:
if !selected || !keyboard_check(vk_control) { exit }
sel = 1
pos = string_length(text) + 1
left = max(0,string_length(text) - cap + 1)
Key Press Event for C-key Key:
execute code:
if !selected || !keyboard_check(vk_control) || sel = pos { exit }
var d2,m,pl,pr;
if password {
d2 = ""
for (m = 0; m < string_length(text); m += 1) {
d2 += "*"
}
} else {
d2 = text
}
pl = min(pos,sel)
pr = max(pos,sel)
clipboard_set_text(string_copy(d2,pl,pr-pl))
Key Press Event for V-key Key:
execute code:
if !selected || !keyboard_check(vk_control) { exit }
keyboard_string = clipboard_get_text()
Key Press Event for X-key Key:
execute code:
if !selected || !keyboard_check(vk_control) || sel = pos { exit }
var d2,m,pl,pr;
if password {
d2 = ""
for (m = 0; m < string_length(text); m += 1) {
d2 += "*"
}
} else {
d2 = text
}
pl = min(pos,sel)
pr = max(pos,sel)
clipboard_set_text(string_copy(d2,pl,pr-pl))
text = string_delete(text,min(pos,sel),abs(pos-sel))
pos = min(pos,sel)
sel = pos
if left + cap > string_length(text) {
left = max(1,string_length(text) - cap)
}
if pos < left { pos = left }
No comments:
Post a Comment