/*	kd:
	
	The shop has some buttons here and there. Some of them are invisible, even.
	Some of them are placed based on your resolution, others should always
	appear in the same place (and for all smartasses reading, I don't
	necessarily	mean in the centre).
	
	The troublesome part would be "I have to draw in the main control, but all
	these buttons are in who knows where".
	
	*/

struct Gc_UiButtons {
	
	// kd: Definitions. Coordinates may be in different spaces according to what
	// we need. It's clear from the start what the resolution is. After that, it
	// is a matter of skipping scaling. This way, we can make buttons that are
	// next to other sprites.
	private Array <double>				def_pos_x;
	private Array <double>				def_pos_y;
	private Array <double>				def_size_x;
	private Array <double>				def_size_y;
	private Array <bool>				def_is_normal_coord;
	
	// kd: Actually used:
	private Array <double>				pos_x;
	private Array <double>				pos_y;
	private Array <double>				size_x;
	private Array <double>				size_y;
	private uint						count;
	
	// kd: This allows assigning binds to a button.
	private Array <uint>				bt_shortcut;
	
	void ClearAndResize (uint size) {
		def_pos_x.				clear();
		def_pos_y.				clear();
		def_size_x.				clear();
		def_size_y.				clear();
		def_is_normal_coord.	clear();
		pos_x.					clear();
		pos_y.					clear();
		size_x.					clear();
		size_y.					clear();
		bt_shortcut.			clear();
		
		def_pos_x.				resize(size);
		def_pos_y.				resize(size);
		def_size_x.				resize(size);
		def_size_y.				resize(size);
		def_is_normal_coord.	resize(size);
		pos_x.					resize(size);
		pos_y.					resize(size);
		size_x.					resize(size);
		size_y.					resize(size);
		bt_shortcut.			resize(size);
		count = size;
		
		for(let i = 0; i < size; i++) {
			pos_x		[i] = 0;
			pos_y		[i] = 0;
			size_y		[i] = 0;
			size_y		[i] = 0;
			bt_shortcut	[i] = 0;
		}
	}
	
	// kd: Now we're getting ready for fun.
	void SetShortcut (int index, uint bt_press) {
		bt_shortcut [index] = bt_press;
	}
	
	// kd: As a fact: You must keep the cursor's resolution and the buttons'
	// resolution the same, and preferably based on your aspect ratio, so you
	// can move on the entire screen.
	
	void Init (
	vector2 resolution			= (1, 1),
	vector2 sprite_resolution	= (1, 1)) {
		pos_x.copy(def_pos_x);
		pos_y.copy(def_pos_y);
		size_x.copy(def_size_x);
		size_y.copy(def_size_y);
		
		// kd: Again, certain buttons are subject to being placed by pixels,
		// not a normal screen coordinate.
		for(let i = 0; i < count; i++)
		if(def_is_normal_coord [i]) {
			pos_x	[i] *= resolution.x;
			pos_y	[i] *= resolution.y;
			size_x	[i] *= resolution.x;
			size_y	[i] *= resolution.y;
		}
	}
	
	// kd: Button positions should be the same as the sprites' end positions.
	// Since sprite movement can be something really weird you don't wanna do
	// twice, we just copy it and rescale till we like it.
	
	// This specifically copies the first count sprites, btw. If you need more
	// sprites that aren't button, append them or make another sprites struct.
	
	void InitFromUiAnim (
	Gc_UiAnim		ui_anim,
	Gc_UiSprites	sprites,
	vector2			resolution			= (1, 1),
	vector2			sprite_resolution	= (1, 1)) {
		let scale = (
			resolution.x / sprite_resolution.x,
			resolution.y / sprite_resolution.y);
		
		for(let i = 0; i < count; i++) {
			let pos		= ui_anim.FinalPos(i);
			let size	= sprites.Size(i);
			pos_x [i]	= scale.x * pos.x;
			pos_y [i]	= scale.y * pos.y;
			size_x [i]	= size.x;
			size_y [i]	= size.y;
		}
	}
	
	// kd: Some cursor stuff.
	
	private bool						is_inside_button;
	private int							found_button;
	private vector2						relative_pos;
	
	void PrepareCursorCheck (vector2 cursor_pos) {
		is_inside_button	= false;
		found_button		= -1;
		
		// kd: Buttons are rectangular and their origin is at the centre.
		for(let i = 0; i < count; i++)
		if(	abs(cursor_pos.x - pos_x [i]) <= size_x [i] &&
			abs(cursor_pos.y - pos_y [i]) <= size_y [i]) {
			
			relative_pos = (
				cursor_pos.x - pos_x [i],
				cursor_pos.y - pos_y [i]);
			is_inside_button	= true;
			found_button		= i;
			return;
		}
	}
	
	bool IsHovered () const {
		return is_inside_button;
	}
	
	int FoundButton () const {
		return found_button;
	}
	
	vector2 RelativeNormalPos () const {
		let size = (size_x [found_button], size_y [found_button]);
		
		return (
			(relative_pos.x + size.x) / (2 * size.x),
			(relative_pos.y + size.y) / (2 * size.y));
	}
	
	// kd: Some keyboard stuff.
	private bool						has_acted;
	private int							found_action;
	
	void PrepareActionCheck (uint bt_press) {
		has_acted		= false;
		found_action	= -1;
		
		// kd: If you press multiple buttons, we only care for the one with the
		// highest priority, the one added earlier.
		for(let i = 0; i < count; i++)
		if(bt_press & bt_shortcut [i]) {
			has_acted		= true;
			found_action	= i;
			return;
		}
	}
	
	bool HasActed () const {
		return has_acted;
	}
	
	int FoundAction () const {
		return found_action;
	}
}
