{
id: "menu_selector_load_game",
ignore_collide: true,
zorder: 20,
is_human: true,

editor_info: {
	category: "playable",
	},
	
vars: {
	currently_selected: null,
	},
	
consts: {
	offset: [-18,0],
	},

properties: {
	item_chars: "filter(level.chars, value.consts.is_menu_item)",
	
	closest_item: "choose(item_chars, -distance(value, {midpoint_x: midpoint_x - consts.offset[0], midpoint_y: midpoint_y - consts.offset[1]}))",
	click_item: "def(x,y) choose(item_chars, -distance(value, {midpoint_x: x, midpoint_y: y}))",
	
	first_item: "closest_item.default_in_group",
	
	select: "def(item) if(item, [
		set(xy, list_add(item.xy, consts.offset)),
		set(currently_selected, item),
		fire_event(item, 'selected'),
		fire_event(currently_selected, 'deselected'),
		currently_selected.show_selector,
		item.hide_selector,
		fire_event(item, 'activate'),
		if(level.cycle > 10, sound('MenuCursorMove.ogg')),
		],[
		fire_event('failed_select'),
		])",
		
	// debug_fn([value.msg, decimal(x-value.x)/decimal(y-value.y)], true))
	next_item: "def(direction) //as on a number pad
		choose(
			filter(item_chars,
				switch(direction,
					2, value.y > y2 and abs(decimal(x2-value.x)/decimal(y2-value.y)) <= 1,
					4, value.x < x2 and abs(decimal(x2-value.x)/decimal(y2-value.y)) >= 1, // The below is a strange workaround for a bug involving infinite math. the abs() is returning the absolute of -inf8, which is one more than inf7, so it wraps back around to -inf8. However, -1/0 is inverting inf7, coming out with -inf7, which is one more than -inf8… as far as I can tell.
					6, value.x > x2 and abs(decimal(x2-value.x)/decimal(y2-value.y)) >= 1,
					8, value.y < y2 and abs(decimal(x2-value.x)/decimal(y2-value.y)) <= 1,
				//	Who do we appreciate?
					)),
			-distance(value, {midpoint_x: midpoint_x - consts.offset[0], midpoint_y: midpoint_y - consts.offset[1]}))
			where x2 = x - self.consts.offset[0]
			where y2 = y - self.consts.offset[1]",
			
	screen_scale_factor: "if(DOUBLE_SCALE, 1, 2)",
},

on_start_level: "[
	select(first_item),
	set(alpha,0),
	spawn(me.type+'.arrow',mid_x,mid_y,facing, [set(child.parent,me)])
	]",
	
on_ctrl_down: "select(next_item(2))",
on_ctrl_up:   "select(next_item(8))",
on_ctrl_left: "select(next_item(4).selector.targeting)",
on_ctrl_right:"select(next_item(6).selector.targeting)",

"on_mouse_down*": "if(mouse_button in [1,3], select(click_item(mouse_x*screen_scale_factor, (mouse_y+8)*screen_scale_factor )))",

on_ctrl_tongue: "fire_event('finish_menu')",
on_ctrl_attack: "fire_event('ctrl_tongue')",
on_ctrl_jump: "fire_event('ctrl_tongue')",

//on_finish_menu: "",  //TODO virtual, would be defined in level, except :  need to know how to assign event handlers in spawn(), because on the titlescreen we have to spawn our playable object, we can't put stuff in the instance of the playable object that's on the actual level, because we obliterate that when we transfer to the level.
			"on_enter_level": "[
				map(filter(level.chars, value.group=0), 'slot_obj', [
					if(level_data['no save data'], remove_object(slot_obj),
					map(keys(level_data), 'ld_key',
						spawn('text_controller', slot_obj.mid_x+20, slot_obj.mid_y+(index+1)*25, 1, child.set_text('${ld_key}: ${level_data[ld_key]}')),
					)),
					set(slot_obj.vars.save_slot_index, index),
				] where level_data = (if(sd.version, {
					~level~: translate(sd.title),
					~coins~: player.vars.coins,
					~visited levels~: size(player.vars.levels_visited),
					// can't do this'hp': '${player.hitpoints}/${player.max_hitpoints}',
					~hp~: str(player.hitpoints/2 or player_default_hitpoints)+'/'+str(player.max_hitpoints/2 or player_default_hitpoints),
					} where player_default_hitpoints = object(player.type or 'frogatto_playable').hitpoints/2
					where player = find(sd.character, 'character', character.is_human),
					{'no save data': true})
					where sd = get_save_document(index))
				),
			]",
			
			"on_finish_menu": "if(cycle > 30, [
				swallow_event(),
		 		set(control_lock, []),
		 		screen_flash([255,255,255,0], [0,0,0,5], 52),
		 		schedule(50,
		 		  [set(control_lock, null),
		 		  load_game('instant', currently_selected.vars.save_slot_index),]
		 		  )
		 		])",

on_process:"
	if(13 in ctrl_keys or ' ' in ctrl_keys,
		fire_event('finish_menu')
	)",

animation: {
	id: "normal",
	image: "characters/stand_in_selector.png",
	rect: [0,0,15,15],
},

object_type: [
	{
		id: "arrow",
		ignore_collide: true,
		zorder: 30,
		
		on_end_anim: "set(animation,animation)",
		on_process:  "[set(relative_x, -50 + 4*sin(cycle*10)),set(relative_y, 2 - img_h/2)]",
		on_create: "set(rotate,-90)",
		
		animation: {
			id: "normal",
			image: "gui/arrow-menu-cursor.png",
			rect: [0,0,23,23],
			frames: 8,
			frames_per_row: 4,
			duration: 3,
		},
	},
	{
		id: "arrow_selected",
		ignore_collide: true,
		zorder: 25,
		
		on_end_anim: "set(animation,animation)",
		on_create: "set(rotate,-90)",
		
		animation: {
			id: "normal",
			image: "gui/arrow-menu-selected.png",
			rect: [0,0,23,23],
			frames: 1,
			duration: 100,
		},
	}],

}
