program GUI;

uses crt, gLib2D, sdl_addon, sdl, sdl_ttf, sysutils;

procedure blitText(x,y : integer ; image : gImage ; color : gColor);
// Afficher texte
begin
	gBeginRects(image);
		gSetAlpha(200);
		gSetCoordMode(g_center);
		gSetCoord(x,y);
		gSetColor(color);
		gAdd;
	gEnd;
end;

function getInput(key : integer) : char;
// Gestion de l'entrée clavier
begin
	getInput := chr(key);
end;

procedure sdl_key(var txt : string; key : integer; var empty, quit, caps : boolean);
begin
	case key of
		SDLK_backspace :
		if length(txt) = 1 then
		begin
			txt := '< Tapez votre message >';
			empty := true;
		end
		else if not empty then
			txt := copy(txt,1,length(txt)-1);
		SDLK_escape : quit := true;
		SDLK_rshift, SDLK_lshift : 
//		SDLK_return : send_text(txt);
	else if empty then
	begin
		empty := false;
		txt := getInput(key,caps);
	end
	else
		txt := txt+getInput(key);
		txt := getInput(key,caps);
	end;
end;

var
	font : PTTF_font;
	txt : string;
	imgTxt : gImage;
	empty, quit, caps : boolean;

begin
	gClear(black);
	empty := true;
	quit := false;
	txt := '< Tapez votre message >'+inttostr(sdlk_minus);
	font := TTF_OpenFont('font.ttf', 30);
	while true do
	begin
		gClear(black);
		imgTxt := gTextLoad(txt,font);
		blitText(200,200,imgTxt,white);
		gFlip;
		gTexFree(imgTxt);
		while sdl_update = 1 do
		begin
			if (sdl_get_keypressed > 0) then
				sdl_key(txt,sdl_get_keypressed,empty,quit,caps);
			if sdl_do_quit or quit then
				exit;
		end;
	end;
end.
