Changeset 64 for trunk/UMenu.pas


Ignore:
Timestamp:
Nov 26, 2020, 4:58:02 PM (3 years ago)
Author:
chronos
Message:
  • Added: Support for dark mode.
  • Added: Top left back button to exit from game to game menu. Allow to restart game and continue with playing.
  • Added: Support for Escape key for exiting game and menu.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMenu.pas

    r63 r64  
    1414
    1515  TMenuItem = class
     16    BackgroundColor: TColor;
     17    BackgroundSelectedColor: TColor;
    1618    Kind: TMenuItemKind;
    1719    Text: string;
    1820    Bounds: TRect;
    1921    Selected: Boolean;
     22    FontSize: Integer;
     23    FontColor: Integer;
     24    Enabled: Boolean;
    2025    procedure Paint(Canvas: TCanvas; P: TPoint); virtual;
     26    function GetOutputText: string; virtual;
     27    constructor Create;
    2128  end;
    2229
     
    4350    destructor Destroy; override;
    4451    procedure Paint(Canvas: TCanvas; P: TPoint); override;
     52    function GetOutputText: string; override;
    4553    property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
    4654  end;
     
    6674
    6775  TMenu = class
     76  private
     77    FOnExit: TNotifyEvent;
     78  public
    6879    Items: TMenuItems;
    6980    Parent: TMenu;
     
    7384    constructor Create;
    7485    destructor Destroy; override;
     86    property OnExit: TNotifyEvent read FOnExit write FOnExit;
    7587  end;
    7688
     
    8799  SAutomatic = 'Automatic';
    88100  SFullScreen = 'Full screen';
     101  SContinue = 'Continue';
     102  SRestart = 'Try again';
    89103
    90104
     
    95109procedure TMenuItemButton.Paint(Canvas: TCanvas; P: TPoint);
    96110begin
    97   if Selected then Canvas.Brush.Color := $e78C31
    98     else Canvas.Brush.Color := $f7bC61;
     111  if Selected then Canvas.Brush.Color := BackgroundSelectedColor
     112    else Canvas.Brush.Color := BackgroundColor;
     113  if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
     114    else Canvas.Brush.Style := bsSolid;
    99115  Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(Text), Canvas.TextHeight(Text));
    100116  Canvas.TextOut(P.X, P.Y, Text);
     
    109125  OutputText := Text;
    110126  if Checked then OutputText := '✓' + OutputText;
    111   if Selected then Canvas.Brush.Color := $e78C31
    112     else Canvas.Brush.Color := $f7bC61;
     127  if Selected then Canvas.Brush.Color := BackgroundSelectedColor
     128    else Canvas.Brush.Color := BackgroundColor;
     129  if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
     130    else Canvas.Brush.Style := bsSolid;
    113131  Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(OutputText), Canvas.TextHeight(OutputText));
    114132  Canvas.TextOut(P.X, P.Y, OutputText);
     
    121139end;
    122140
     141function TMenuItem.GetOutputText: string;
     142begin
     143  Result := Text;
     144end;
     145
     146constructor TMenuItem.Create;
     147begin
     148  Enabled := True;
     149  FontSize := 40;
     150  FontColor := clWhite;
     151end;
     152
    123153{ TMenuItemComboBox }
    124154
     
    135165
    136166procedure TMenuItemComboBox.Paint(Canvas: TCanvas; P: TPoint);
    137 var
    138   OutputText: string;
    139 begin
    140   if Selected then Canvas.Brush.Color := $e78C31
    141     else Canvas.Brush.Color := $f7bC61;
    142   OutputText := Text + ': ' + States[Index];
    143   Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(OutputText), Canvas.TextHeight(OutputText));
    144   Canvas.TextOut(P.X, P.Y, OutputText);
     167begin
     168  if Selected then Canvas.Brush.Color := BackgroundSelectedColor
     169    else Canvas.Brush.Color := BackgroundColor;
     170  if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
     171    else Canvas.Brush.Style := bsSolid;
     172  Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(GetOutputText), Canvas.TextHeight(GetOutputText));
     173  Canvas.TextOut(P.X, P.Y, GetOutputText);
     174end;
     175
     176function TMenuItemComboBox.GetOutputText: string;
     177begin
     178  Result := Text + ': ' + States[Index];
    145179end;
    146180
     
    152186begin
    153187  for I := 0 to Items.Count - 1 do begin
    154     Items[I].Selected := Items[I].Bounds.Contains(Position);
     188    if Items[I].Enabled then
     189      Items[I].Selected := Items[I].Bounds.Contains(Position);
    155190  end;
    156191end;
     
    187222  Y: Integer;
    188223  LineHeight: Integer;
     224  TotalWidth: Integer;
     225  TotalHeight: Integer;
    189226begin
    190227  with Canvas do begin
    191     X := Width div 3;
    192     Y := Height div 5;
    193 
    194     // Title
    195     Font.Size := 60;
    196     Font.Color := clBlack;
    197     Font.Style := [fsBold];
    198     Brush.Style := bsClear;
    199     TextOut(X, Y, SBigMetro);
    200     LineHeight := Round(TextHeight('I') * 1.1);
    201     Inc(Y, LineHeight);
    202 
    203     // Menu items
    204     Font.Size := 40;
    205     Font.Color := clWhite;
     228    TotalWidth := 0;
     229    TotalHeight := 0;
     230
     231    // Calculate total dimensions for centering
    206232    Font.Style := [fsBold];
    207233    Brush.Style := bsSolid;
    208     LineHeight := Round(TextHeight('I') * 1.1);
    209234    for I := 0 to Items.Count - 1 do begin
     235      Font.Size := Items[I].FontSize;
     236      if TotalWidth < TextWidth(Items[I].GetOutputText) then
     237        TotalWidth := TextWidth(Items[I].GetOutputText);
     238      LineHeight := Round(TextHeight('I') * 1.1);
     239      TotalHeight := TotalHeight + LineHeight;
     240    end;
     241
     242    X := (Width - TotalWidth) div 2;
     243    Y := (Height - TotalHeight) div 2;
     244
     245    // Menu items
     246    Font.Style := [fsBold];
     247    Brush.Style := bsSolid;
     248    for I := 0 to Items.Count - 1 do begin
     249      Font.Size := Items[I].FontSize;
     250      Font.Color := Items[I].FontColor;
    210251      Items[I].Paint(Canvas, Point(X, Y));
     252      LineHeight := Round(TextHeight('I') * 1.1);
    211253      Inc(Y, LineHeight);
    212254    end;
Note: See TracChangeset for help on using the changeset viewer.