uses
  dstrnum,
  dvector2i,
  dvector4f,
  drect2i,
  dstream,
  dstreamfile,
  DEngBase,
  deng,
  dbit,
  dgui3,
  dgui_dump,
  dgui_simple;

var
  Theme: TGuiThemePlain;
  Simple: TGuiThemeSimple;
  Gui: TGui;
  L: TLabel;
  B: TButton;
  Console: TConsole;

procedure OnMouseDown(Userdata: Pointer);
begin
  Log('BUTTON PRESSED');
  Console.AddLine('Button pressed :)');
end;

procedure Init(Eng: TEngine);
begin
  Log('Initializing gui theme');
  Theme.Init;
  Log('Assigning font');
  Simple.Init('data/font/c256.bit');
  Gui.Init(@Simple);
  L.Init(@Gui, Vec2i(50, 400), Vec2i(250, 50), 'Hello world!');
  B.Init(@Gui, Vec2i(50, 300), Vec2i(250, 50), 'Hello world!');
  B.Callback.SetProc(@OnMouseDown, nil);
  Console.Init(@Gui);
  GuiDump(@Gui);
end;

procedure Done(Eng: TEngine);
begin
  Gui.Done;
  Simple.Done;
  Theme.Done;
end;

procedure Render(Eng: TEngine);
begin
  Eng.Draw.BeginRender;
  Eng.Draw.ClearScreen(Vec4f(0, 0, 0, 1));
  Eng.Draw.Start2D;
  Eng.Draw.BlendMode(BM_ALPHA);
  Gui.Render;
  Eng.Draw.EndRender;
end;

procedure Update(Eng: TEngine);
begin
  Gui.Update;
  if Eng.Control.Pressed(KEY_ESC) then
      Eng.StopLoop;
end;

function OnWindowEvent(Event: PWindowEvent): Boolean;
begin
  if Event.IsLeftClickDown then begin
    Console.AddLine('Eng.Mouse: ' + IntToStr(Eng.Mouse.X) + ' ' + IntToStr(Eng.Mouse.Y));
    Console.AddLine('Ev.Cursor: ' + IntToStr(Round(Event.GetCursor.X)) + ' ' + IntToStr(Round(Event.GetCursor.Y)));
  end;
  Result := Gui.OnWindowEvent(Event);
end;

begin
  Eng.InitDisplayMode(800, 600, 32, DENG_GL);
  Eng.InitTitle('dgui: test for common');
  Eng.InitInitFunc(Init);
  Eng.InitDoneFunc(Done);
  Eng.InitRenderFunc(Render);
  Eng.InitUpdateFunc(Update, 30);
  Eng.InitEventFunc(OnWindowEvent);
  Log('Running app...');
  Eng.Run;
  Log('Shutting app down...');
end.
