{$APPTYPE CONSOLE}

uses
  windows, // TODO remove later
  dansiview,
  dstrnum,
  dvector2i,
  dvector4f,
  drect2i,
  dstream,
  dstreamfile,
  DEngBase,
  deng,
  DWinApi,
  dbit,
  dgui3,
  dgui_simple,
  dgui_console,
  dgui_sliders,
  dgui_place,
  dgui_dump,
  dgui_skinatlas,
  dpng;

var
  Simple: TGuiThemeSkinAtlas;
  Gui: TGui;
  L: TLabel;
  B: TButton;
  R: TGuiRect;
  MusicIcon: TGuiRect;
  MusicSlider: THorSlider;
  SoundIcon: TGuiRect;
  SoundSlider: THorSlider;
  Console: TConsole;
  Width: LongInt;
  Height: LongInt;

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

procedure Init(Eng: TEngine);
begin
  Log('Initializing gui theme');
  Log('Assigning font');
  Simple.Init('c256.bit', 'core_black_blue.png');
  Gui.Init(@Simple);
  R.Init(@Gui, Vec2i(50, 500), Vec2i(100, 100));
  L.Init(@Gui, Vec2i(50, 400), Vec2i(250, 50), 'Hello world!');
  B.Init(@Gui, Vec2i(50, 300), Vec2i(250, 24), 'Hello world!');
  MusicIcon.Init(@Gui, Vec2i(5, 5), Vec2i(24, 24));
  MusicSlider.Init(@Gui);
  MusicSlider.SetLocalRect(5 + 24 + 5, 5, 24 * 5, 24);
  SoundIcon.Init(@Gui, Vec2i(5, 5 + 24 + 5), Vec2i(24, 24));
  SoundSlider.Init(@Gui);
  SoundSlider.SetLocalRect(5 + 24 + 5, 5 + 24 + 5, 24 * 5, 24);
  MusicIcon.AlignTopLeft;
  MusicSlider.AlignTopLeft;
  SoundIcon.AlignTopLeft;
  SoundSlider.AlignTopLeft;
  B.Callback.SetProc(@OnMouseDown, nil);
  Console.Init(@Gui);
  GuiDump(@Gui);
  Width := Eng.Width;
  Height := Eng.Height;
end;

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

procedure CheckResize;
begin
  if (Width <> Eng.Width) or (Height <> Eng.Height) then begin
    Gui.OnResize(Eng.Width, Eng.Height);
    Width := Eng.Width;
    Height := Eng.Height;
  end;
end;

procedure Render(Eng: TEngine);
begin
  CheckResize;
  Eng.Draw.BeginRender;
  //Eng.Draw.ClearScreen(Vec4f(0, 0, 0, 1));
  Eng.Draw.ClearScreen(Vec4f(0.1, 0.1, 0.1, 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;
var
  Buf: array[0 .. 1024 - 1] of AnsiChar;
  BufLen: SizeInt;
begin
  if Event.IsKeyDown then begin
    BufLen := GetKeyNameTextA(Event.GetLParam, @Buf[0], Length(Buf));
    Log('PRESSED (' + IntToStr(BufLen) + ') ' + AnsiView(@Buf[0], BufLen).ToString);
  end;
  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
  Log('SizeOf(TGUIPlace) = ' + IntToStr(SizeOf(TGUIPlace)));
  Log('SizeOf(TGui) = ' + IntToStr(SizeOf(TGui)));
  Log('SizeOf(TButton) = ' + IntToStr(SizeOf(TButton)));
  WINAPI_SETTINGS.CloseButton := True;
  WINAPI_SETTINGS.AllowResize := True;
  Eng.InitDisplayMode(800, 600, 32, DENG_GL);
  Eng.InitTitle('dgui: test for atlased skin');
  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.
