# Script da assegnare a un'icona qualsiasi (poi lo script imposterà quella giusta), la quale andrà
# collegata al primo segnale permissivo o all'imperativo di blocco di una sezione di blocco.
# Un click su di essa chiude e spegne o accende e apre il segnale a cui è collegata, a seconda che
# esso sia acceso (indicatore bianco) o spento (indicatore nero); i segnali permissivi successivi
# faranno lo stesso (chiusura e apertura solo se impostati come "Intermediate").
# All'inizio della simulazione il segnale collegato verrà spento se è per il binario di destra.

# Script to be assigned to any icon (the script itself will set the correct one), which will be
# linked to the first permissive signal (or to the main block signal) of a block section. A click
# on it will unclear and switch off or switch on and clear the linked signal, depending on its
# being on (white indication) or off (black indication); the permissive signals that follow will
# do the same (clearing and unclearing only if set as "Intermediate"). At the beginning of the
# simulation the linked signal will be switched off if it commands the track to its left.

# P.Rosati  21/01/2021 (Versione 5.1)

OnInit:
	if .linked.script = "rfi_perm_1F.tds" or .linked.script = "rfi_perm_1F_nd.tds" or .linked.script > "rfi_perm_1P" and .linked.script < "rfi_perm_2N"
		if .linked.direction = 0
			.icon = "BL_b_W.xpm"
		end
		if .linked.direction = 1
			.icon = "BL_b_E.xpm"
		end
		if .linked.direction = 16
			.icon = "BL_b_S.xpm"
		end
		if .linked.direction = 17
			.icon = "BL_b_N.xpm"
		end
		.linked.aspect = off
		do update
		return
	end
	if .linked.script = "rfi_imp_b.tds" or .linked.script = "rfi_imp_bf.tds" or .linked.script > "rfi_perm_2P" and .linked.script < "rfi_perm_AVs"
		if .linked.direction = 0
			.icon = "BL_b_W.xpm"
		end
		if .linked.direction = 1
			.icon = "BL_b_E.xpm"
		end
		if .linked.direction = 16
			.icon = "BL_b_S.xpm"
		end
		if .linked.direction = 17
			.icon = "BL_b_N.xpm"
		end
		.linked.aspect = off
		do update
		return
	end
	if .linked.direction = 0
		.icon = "BL_w_W.xpm"
	end
	if .linked.direction = 1
		.icon = "BL_w_E.xpm"
	end
	if .linked.direction = 16
		.icon = "BL_w_S.xpm"
	end
	if .linked.direction = 17
		.icon = "BL_w_N.xpm"
	end
end

OnIconUpdate:
	if .linked.aspect = disabled or .linked.aspect = off or .linked.aspect = fault or .linked.aspect = nothing
		if .linked.direction = 0
			.icon = "BL_b_W.xpm"
		end
		if .linked.direction = 1
			.icon = "BL_b_E.xpm"
		end
		if .linked.direction = 16
			.icon = "BL_b_S.xpm"
		end
		if .linked.direction = 17
			.icon = "BL_b_N.xpm"
		end
	else
		if .linked.direction = 0
			.icon = "BL_w_W.xpm"
		end
		if .linked.direction = 1
			.icon = "BL_w_E.xpm"
		end
		if .linked.direction = 16
			.icon = "BL_w_S.xpm"
		end
		if .linked.direction = 17
			.icon = "BL_w_N.xpm"
		end
	end
end

OnClicked:
	if .linked.aspect = off
		.linked.aspect = red
		if .linked.color = black and .linked.locked = 0
			.linked.click = 1
		end
		if .linked.aspect = red
			do update
		end
		return
	end
	if .linked.aspect = disabled
		.linked.aspect = green
		do update
		return
	end
	if .linked.aspect = red
		.linked.aspect = off
		do update
		return
	end
	if .linked.aspect ! fault and .linked.aspect ! nothing
		.linked.aspect = disabled
		.linked.click = 1
		if .linked.aspect = disabled
			do update
		end
		return
	end
end