【GAMEMAKER】save and load text file

 Information about object: obj_textbox

Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

w = 50
h = 20
draw_set_font(fnt_courier)
tw = 8
th = 12
ins = 1

lines = 1
top = 0
left = 0
px = 0
py = 0
line[0] = ''
file = ""
saved = true
Step Event:
execute code:

if py < top { top = py }
if py > top + h - 1 { top = py - h + 1 }
if px < left { left = px }
if px > left + w - 1 { left = px - w + 1 }

if keyboard_string != '' {
 line[py] = string_copy(line[py],1,px) + keyboard_string + string_copy(line[py],px+ins,string_length(line[py])-px)
 px += string_length(keyboard_string)
 keyboard_string = ''
 saved = false
}

if line[0] = "" && lines = 1 && file = "" { saved = true }

room_caption = "Gamepad - "
if file = "" { room_caption += "Untitled Document" }
else { room_caption += filename_name(file) }
if !saved { room_caption += "*" }
room_caption += " ("+string(obj_right.size)+"b)"

if mouse_x > x && mouse_y > y && mouse_x < x + w*tw && mouse_y < y + h*th {
 mx = (mouse_x - x) / tw
 my = (mouse_y - y) / th
 window_set_cursor(cr_beam)
} else { window_set_cursor(cr_default) }

if top + h > max(h,lines) { top = lines - h }
Keyboard Event for <Backspace> Key:
execute code:

keyboard_clear(vk_backspace)
saved = false
if px = 0 {
 if py = 0 { exit }
 px = string_length(line[py-1])
 line[py-1] += line[py]
 for (m = py; m < lines-1; m += 1) { line[m] = line[m+1] }
 lines -= 1
 py -= 1
 exit
}
line[py] = string_delete(line[py],px,1)
px -= 1
Keyboard Event for <Enter> Key:
execute code:

keyboard_clear(vk_enter)
saved = false
for (m = lines-1; m > py; m -= 1) {
 line[m+1] = line[m]
}
line[py+1] = string_copy(line[py],px+ins,string_length(line[py])-px)
line[py] = string_copy(line[py],1,px)
py += 1
px = 0
lines += 1
Keyboard Event for <Page Up> Key:
execute code:

keyboard_clear(vk_pageup)
top = max(top - h,0)
py = max(py - h,0)
px = min(px,string_length(line[py]))
Keyboard Event for <Page Down> Key:
execute code:

keyboard_clear(vk_pagedown)
top = max(0,min(top + h,lines-h))
py = min(py + h,lines-1)
px = min(px,string_length(line[py]))
Keyboard Event for <Left> Key:
execute code:

keyboard_clear(vk_left)
if px = 0 && py = 0 { exit }
px -= 1
if px = -1 {
 py -= 1
 px = string_length(line[py])
}
Keyboard Event for <Up> Key:
execute code:

keyboard_clear(vk_up)
if py = 0 { exit }
py -= 1
px = min(px,string_length(line[py]))
Keyboard Event for <Right> Key:
execute code:

keyboard_clear(vk_right)
if px = string_length(line[py]) && py = lines-1 { exit }
px += 1
if px > string_length(line[py]) {
 py += 1
 px = 0
}
Keyboard Event for <Down> Key:
execute code:

keyboard_clear(vk_down)
if py = lines - 1 { exit }
py += 1
px = min(px,string_length(line[py]))
Keyboard Event for <Delete> Key:
execute code:

keyboard_clear(vk_delete)
saved = false
if px = string_length(line[py]) {
 if py = lines - 1 { exit }
 line[py] += line[py+1]
 for (m = py+1; m < lines-1; m += 1) { line[m] = line[m+1] }
 lines -= 1
 exit
}
line[py] = string_delete(line[py],px+1,1)
Mouse Event for Glob Left Button:
execute code:

if window_get_cursor() = cr_beam {
 py = min(floor(my+top),lines-1)
 px = min(round(mx+left),string_length(lines[py]))

 if py < top { top = py }
 if py > top + h - 1 { top = py - h + 1 }
 if px < left { left = px }
 if px > left + w - 1 { left = px - w + 1 } 
}
Draw Event:
for other object: execute code:

draw_set_color(c_white)
draw_rectangle(x-2,y+1,x+w*tw,y+h*th+2,false)
draw_set_color(c_black)
draw_line(x-2,y+1,x+w*tw,y+1)
draw_line(x-2,y+1,x-2,y+h*th+2)
draw_line(x+w*tw+15,y+1,x+w*tw+15,y+h*th+2)
draw_line(x-2,y+h*th+17,x+w*tw,y+h*th+17)
draw_set_halign(fa_right)
draw_text(x+w*tw/2,y+h*th,string(px)+', '+string(py+1))
draw_set_halign(fa_left)
draw_text(x+w*tw/2,y+h*th,'/'+string(lines))
draw_text(0,0,"Made by IsmAvatar")
for (m = top; m < min(top + h,lines); m += 1) {
 if m = py {
  draw_set_color(c_red)
  if ins = 1
   draw_text(x+(px-left)*tw-tw/3,y+(m-top)*12,'|')
  else
   draw_text(x+(px-left)*tw-tw/3,y+(m-top)*12,'/')
 }
 draw_set_color(c_black)
 draw_text(x,y+(m-top)*12,string_copy(string_replace_all(line[m],'#','\#'),left+1,w))
}
Key Press Event for <End> Key:
execute code:

if keyboard_check(vk_control) { py = lines - 1 }
px = string_length(line[py])
Key Press Event for <Home> Key:
execute code:

px = 0
if keyboard_check(vk_control) { py = 0 }
Key Press Event for <Insert> Key:
execute code:

ins = 3 - ins
Key Press Event for F2 Key:
execute code:

if !saved {
if(show_question(("File changed. Save changes?"))){
 
  keyboard_key_press(vk_f5)
  keyboard_key_release(vk_f5)
 }else{
  exit 
  }
  }


lines = 1
top = 0
left = 0
px = 0
py = 0
line[0] = ''
file = ""
saved = true
Key Press Event for F4 Key:
execute code:

lastfile = file
file = get_save_filename("Text files (*.txt)|*.txt","")
if file = "" {
 file = lastfile
 exit
}
if file_exists(file) {
 if !show_question("File exists. Overwrite?") {
  file = lastfile
  exit
 }
}
f = file_text_open_write(file)
file_text_close(f)
if !file_exists(file) {
 show_message("Could not save file")
 file = lastfile
 exit
}
f = file_text_open_write(file)
for (m = 0; m < lines; m += 1) {
 file_text_write_string(f,line[m])
 if m < lines - 1 { file_text_writeln(f) }
}
file_text_close(f)
saved = true
Key Press Event for F5 Key:
execute code:

if file = "" {
 keyboard_key_press(vk_f4)
 keyboard_key_release(vk_f4)
 exit
}
f = file_text_open_write(file)
for (m = 0; m < lines; m += 1) {
 file_text_write_string(f,line[m])
 if m < lines - 1 { file_text_writeln(f) }
}
file_text_close(f)
saved = true
Key Press Event for F9 Key:
execute code:

if !saved {
if(show_question(("File changed. Save changes?"))){
 
  keyboard_key_press(vk_f5)
  keyboard_key_release(vk_f5)
 }else{
  exit 
  }
  }


lastfile = file
file = get_open_filename("Text files (*.txt)|*.txt","")
if file = "" {
 file = lastfile
 exit
}
if !file_exists(file) {
 file = filename_change_ext(file,".txt")
 if !file_exists(file) {
  show_message(file+"#"+"File not found")
  file = lastfile
  exit
 }
}
f = file_text_open_read(file)
for (lines = 0; !file_text_eof(f); lines += 1) {
 line[lines] = file_text_read_string(f)
 file_text_readln(f)
}
file_text_close(f)
top = 0
left = 0
px = 0
py = 0
saved = true

Information about object: obj_new
Sprite: spr_new
Solid: false
Visible: true
Depth: -1
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Pressed:
execute code:

keyboard_key_press(vk_f2)
keyboard_key_release(vk_f2)
Draw Event:
execute code:

draw_this()

Information about object: obj_saveas
Sprite: spr_saveas
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Pressed:
execute code:

keyboard_key_press(vk_f4)
keyboard_key_release(vk_f4)
Draw Event:
execute code:

draw_this()

Information about object: obj_save
Sprite: spr_save
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Pressed:
execute code:

keyboard_key_press(vk_f5)
keyboard_key_release(vk_f5)
Draw Event:
execute code:

draw_this()

Information about object: obj_load
Sprite: spr_load
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Pressed:
execute code:

keyboard_key_press(vk_f9)
keyboard_key_release(vk_f9)
Draw Event:
execute code:

draw_this()

draw_sprite(sprite_index,0,x,y)
if position_meeting(mouse_x,mouse_y,self) { draw_sprite(spr_box,0,x,y) }

Information about object: obj_up
Sprite: spr_up
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

x = obj_textbox.x + obj_textbox.w * obj_textbox.tw
y = obj_textbox.y + 1
Step Event:
execute code:

image_single = (obj_textbox.top > 0)
Mouse Event for Left Button:
execute code:

if image_index {
 obj_textbox.top -= 1
 obj_textbox.py -= 1
 obj_textbox.px = min(string_length(obj_textbox.line[obj_textbox.py]),obj_textbox.px)
}

Information about object: obj_down
Sprite: spr_down
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

x = obj_textbox.x + obj_textbox.w * obj_textbox.tw
y = obj_textbox.y + obj_textbox.h * obj_textbox.th + 2
Step Event:
execute code:

image_single = (obj_textbox.lines > obj_textbox.top + obj_textbox.h)
Mouse Event for Left Button:
execute code:

if image_index {
 obj_textbox.top += 1
 obj_textbox.py += 1
 obj_textbox.px = min(string_length(obj_textbox.line[obj_textbox.py]),obj_textbox.px)
}


Information about object: obj_left
Sprite: spr_left
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

x = obj_textbox.x - 2
y = obj_textbox.y + obj_textbox.h * obj_textbox.th + 2
Step Event:
execute code:

image_single = (obj_textbox.left > 0)
Mouse Event for Left Button:
execute code:

if image_index {
 obj_textbox.left -= 1
 obj_textbox.px -= 1
}

Information about object: obj_right
Sprite: spr_right
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

x = obj_textbox.x + obj_textbox.w * obj_textbox.tw
y = obj_textbox.y + obj_textbox.h * obj_textbox.th + 2
ml = 0
size = 0
alarm = room_speed
Alarm Event for alarm 0:
execute code:

ml = 0
size = 0
for (m = 0; m < obj_textbox.lines; m += 1) {
 size += string_length(obj_textbox.line[m]) + 2
 ml = max(string_length(obj_textbox.line[m]),ml)
}
size -= 2
alarm = room_speed
Step Event:
execute code:

image_single = (obj_textbox.left + obj_textbox.w <= ml)
Mouse Event for Left Button:
execute code:

if image_index {
 obj_textbox.left += 1
 obj_textbox.px += 1
}

Information about object: obj_vbar
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

x = obj_up.x
Y = obj_up.y + obj_up.sprite_height
y = Y
th = obj_down.y - obj_up.y - obj_down.sprite_height - obj_up.sprite_height
bh = max(min(obj_textbox.h / obj_textbox.lines,1) * th,4)
Step Event:
execute code:

th = obj_down.y - obj_up.y - obj_down.sprite_height - obj_up.sprite_height
bh = max(min(obj_textbox.h / obj_textbox.lines,1) * th,4)
y = Y + (obj_textbox.top / max(obj_textbox.lines,obj_textbox.h)) * th
Draw Event:
execute code:

draw_set_color(make_color_rgb(224,224,224))
draw_rectangle(x,Y,x+16,Y+th,false)
draw_set_color(c_ltgray)
draw_rectangle(x+2,y+2,x+14,y+bh-1,false)
draw_set_color(c_black)
draw_line(x+15,y,x+15,y+bh)
draw_line(x,y+bh,x+16,y+bh)
draw_set_color(c_gray)
draw_line(x+1,y+bh-1,x+15,y+bh-1)
draw_line(x+14,y+1,x+14,y+bh-1)
draw_set_color(make_color_rgb(222,222,222))
draw_line(x,y,x+15,y)
draw_line(x,y,x,y+bh)
draw_set_color(c_white)
draw_line(x+1,y,x+1,y+bh-1)
draw_line(x,y+1,x+14,y+1)
draw_set_color(c_black)

Information about object: obj_hbar
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

y = obj_left.y
X = obj_left.x + obj_left.sprite_width
x = X
w = obj_textbox.w - 1
tw = obj_right.x - obj_left.x - obj_right.sprite_width - obj_left.sprite_width
bw = max(min(w / max(obj_right.ml,obj_textbox.left+w-1),1) * tw,4)
Step Event:
execute code:

tw = obj_right.x - obj_left.x - obj_right.sprite_width - obj_left.sprite_width
bw = max(min(w / max(obj_right.ml,obj_textbox.left+w),1) * tw,4)
x = X + (obj_textbox.left / max(max(obj_right.ml,obj_textbox.left+w),w)) * tw
Draw Event:
execute code:

draw_set_color(make_color_rgb(224,224,224))
draw_rectangle(X,y,X+tw,y+16,false)
draw_set_color(c_ltgray)
draw_rectangle(x+2,y+2,x+bw-1,y+14,false)
draw_set_color(c_black)
draw_line(x,y+15,x+bw,y+15)
draw_line(x+bw,y,x+bw,y+16)
draw_set_color(c_gray)
draw_line(x+bw-1,y+1,x+bw-1,y+15)
draw_line(x+1,y+14,x+bw-1,y+14)
draw_set_color(make_color_rgb(222,222,222))
draw_line(x,y,x,y+15)
draw_line(x,y,x+bw,y)
draw_set_color(c_white)
draw_line(x,y+1,x+bw-1,y+1)
draw_line(x+1,y,x+1,y+14)
draw_set_color(c_black)


No comments:

Power Is Shifting Rapidly to Indie Creators

The exodus from Twitter since the election is a sign of things to come ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ...

Contact Form

Name

Email *

Message *