Changeset 102 for trunk/Game.pas


Ignore:
Timestamp:
Dec 9, 2024, 11:55:53 AM (13 days ago)
Author:
chronos
Message:
  • Modified: Lowered minimal swipe distance.
  • Modified: Show score and best score as boxes. Place them according window size.
  • Modified: Updated Common package.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Game.pas

    r100 r102  
    119119    procedure DoPaint;
    120120    procedure RenderTile(Canvas: TCanvas; Tile: TTile; TileRect: TRect; WithText: Boolean);
     121    procedure RenderControls(Canvas: TCanvas; Rect: TRect; Horizontal: Boolean);
     122    function RenderTextBox(Canvas: TCanvas; Pos: TPoint; Title, Value: string): TSize;
    121123    procedure GameOver;
    122124    procedure SetColorPalette(AValue: TColorPalette);
     
    185187resourcestring
    186188  SScore = 'Score';
    187   STopScore = 'Top score';
     189  STopScore = 'Best';
    188190  SSkinLinear = 'Linear';
    189191  SSkinPowerOfTwo = 'Power of two';
     
    643645  X, Y: Integer;
    644646  TileSize: TPoint;
    645   ValueStr: string;
    646647  Frame: TRect;
    647648  TileRect: TRect;
    648649  TopBarHeight: Integer;
     650  LeftBarWidth: Integer;
    649651  TileMargin: Integer;
    650652  TileCenter: TPoint;
     
    652654  MetaCanvas: TMetaCanvas;
    653655  BorderSize: Integer;
    654 begin
     656  ControlsRect: TRect;
     657  BoardRect: TRect;
     658  Horizontal: Boolean;
     659begin
     660  // Form.Canvas.Width and Form.Canvas.Height is not working correctly under Windows.
     661  // So dimensions are provided by CanvasSize parameter.
     662
    655663  MetaCanvas := TMetaCanvas.Create;
    656664  MetaCanvas.Size := Point(Canvas.Width, Canvas.Height);
    657665
    658   TopBarHeight := ScaleY(24, 96);
     666  // Clear background
    659667  MetaCanvas.Brush.Style := bsSolid;
    660668  MetaCanvas.Brush.Color := Core.Core.ThemeManager1.Theme.ColorControl;
    661669  MetaCanvas.FillRect(0, 0, MetaCanvas.Width, MetaCanvas.Height);
    662670
    663   ValueStr := SScore + ': ' + IntToStr(Score);
    664   MetaCanvas.Brush.Style := bsClear;
    665   MetaCanvas.Font.Color := Core.Core.ThemeManager1.Theme.ColorControlText;
    666   MetaCanvas.Font.Height := Trunc(TopBarHeight * 0.7);
    667   MetaCanvas.TextOut(ScaleY(16, 96), (TopBarHeight - MetaCanvas.TextHeight(ValueStr)) div 2, ValueStr);
    668 
    669   ValueStr := STopScore + ': ' + IntToStr(TopScore);
    670   MetaCanvas.Font.Color := Core.Core.ThemeManager1.Theme.ColorControlText;
    671   MetaCanvas.Font.Height := Trunc(TopBarHeight * 0.7);
    672   MetaCanvas.TextOut(ScaleY(136, 96), (TopBarHeight - MetaCanvas.TextHeight(ValueStr)) div 2, ValueStr);
    673 
    674   // Form.Canvas.Width and Form.Canvas.Height is not working correctly under Windows.
    675   // So dimensions are provided by CanvasSize parameter.
     671  TopBarHeight := ScaleY(55, 96);
     672  LeftBarWidth := ScaleY(90, 96);
     673  if CanvasSize.X - LeftBarWidth < Canvas.Height then begin
     674    ControlsRect := Rect(0, 0, CanvasSize.X, TopBarHeight);
     675    BoardRect := Rect(0, TopBarHeight, CanvasSize.X, CanvasSize.Y);
     676    Horizontal := True;
     677  end else begin
     678    ControlsRect := Rect(0, 0, LeftBarWidth, CanvasSize.Y);
     679    BoardRect := Rect(LeftBarWidth, 0, CanvasSize.X, CanvasSize.Y);
     680    Horizontal := False;
     681  end;
     682
     683  RenderControls(MetaCanvas, ControlsRect, Horizontal);
     684
    676685  BorderSize := ScaleY(2, 96);
    677   Frame := Rect(BorderSize, BorderSize + TopBarHeight, CanvasSize.X - BorderSize, CanvasSize.Y - BorderSize);
     686  Frame := Rect(BoardRect.Left + BorderSize, BoardRect.Top + BorderSize,
     687    BoardRect.Right - BorderSize, BoardRect.Bottom - BorderSize);
     688
    678689  TileSize := Point(Frame.Width div Board.Size.X, Frame.Height div Board.Size.Y);
    679690  if TileSize.X < TileSize.Y then TileSize.Y := TileSize.X;
     
    849860end;
    850861
     862procedure TGame.RenderControls(Canvas: TCanvas; Rect: TRect; Horizontal: Boolean);
     863var
     864  Pos: TPoint;
     865  Size: TSize;
     866begin
     867  if Horizontal then Pos := Point(ScaleY(16, 96), ScaleY(4, 96))
     868    else Pos := Point(ScaleY(4, 96), ScaleY(16, 96));
     869
     870  Size := RenderTextBox(Canvas, Pos, SScore, IntToStr(Score));
     871
     872  if Horizontal then Pos := Point(ScaleY(16 + 16, 96) + Size.Width, ScaleY(4, 96))
     873    else Pos := Point(ScaleY(4, 96), ScaleY(16 + 16, 96) + Size.Height);
     874
     875  Size := RenderTextBox(Canvas, Pos, STopScore, IntToStr(TopScore));
     876end;
     877
     878function TGame.RenderTextBox(Canvas: TCanvas; Pos: TPoint; Title, Value: string
     879  ): TSize;
     880var
     881  BoxSize: TSize;
     882begin
     883  with Canvas do begin
     884    Font.Color := Core.Core.ThemeManager1.Theme.ColorControlText;
     885    Font.Height := Trunc(24);
     886
     887    BoxSize := Size(TextWidth(Title), TextHeight(Title) + TextHeight(Value));
     888    if BoxSize.Width < TextWidth(Value) then BoxSize.Width := TextWidth(Value);
     889    BoxSize := Size(Round(BoxSize.Width * 1.2), Round(BoxSize.Height * 1));
     890
     891    Brush.Style := bsSolid;
     892    Brush.Color := Core.Core.ThemeManager1.Theme.ColorWindow;
     893    FillRect(Pos.X, Pos.Y, Pos.X + BoxSize.Width, Pos.Y + BoxSize.Height);
     894
     895    Brush.Style := bsClear;
     896    TextOut(Pos.X + (BoxSize.Width - TextWidth(Title)) div 2, Pos.Y, Title);
     897
     898    Brush.Style := bsClear;
     899    Font.Color := Core.Core.ThemeManager1.Theme.ColorControlText;
     900    Font.Height := Trunc(24);
     901    TextOut(Pos.X + (BoxSize.Width - TextWidth(Value)) div 2,
     902      Pos.Y + TextHeight(Title), Value);
     903  end;
     904
     905  Result := BoxSize;
     906end;
     907
    851908function TGame.CanUndo: Boolean;
    852909begin
Note: See TracChangeset for help on using the changeset viewer.