DEADSOFTWARE

Holmes UI window title fix
[d2df-sdl.git] / src / game / g_holmes_ui.inc
index f5472e8c2ca62ede2eefe254fc8f991fb332fe24..a6daf4f491b4d6da83f65e0ffbcf53c5de212cec 100644 (file)
@@ -16,6 +16,9 @@
 // ////////////////////////////////////////////////////////////////////////// //
 type
   THControl = class
+  public
+    type TActionCB = procedure (me: THControl; uinfo: Integer);
+
   private
     mParent: THControl;
     mX, mY: Integer;
@@ -66,6 +69,9 @@ type
     // modifies rect0
     class function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
 
+  public
+    actionCB: TActionCB;
+
   public
     constructor Create (ax, ay, aw, ah: Integer; aparent: THControl=nil);
     destructor Destroy (); override;
@@ -115,13 +121,20 @@ type
     mTitle: AnsiString;
     mDragging: Boolean;
     mDragStartX, mDragStartY: Integer;
+    mWaitingClose: Boolean;
+    mInClose: Boolean;
 
   protected
     procedure blurred (); override;
 
+  public
+    closeCB: TActionCB; // called after window was removed from ui window list
+
   public
     constructor Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
 
+    procedure centerInScreen ();
+
     // `sx` and `sy` are screen coordinates
     procedure drawControl (sx, sy: Integer); override;
     procedure drawControlPost (sx, sy: Integer); override;
@@ -131,17 +144,49 @@ type
   end;
 
 
+  THCtlSimpleText = class(THControl)
+  private
+    type
+      PItem = ^TItem;
+      TItem = record
+        title: AnsiString;
+        centered: Boolean;
+        hline: Boolean;
+      end;
+  private
+    mItems: array of TItem;
+
+  public
+    constructor Create (ax, ay: Integer; aparent: THControl=nil);
+    destructor Destroy (); override;
+
+    procedure appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
+
+    procedure drawControl (sx, sy: Integer); override;
+
+    function mouseEvent (var ev: THMouseEvent): Boolean; override;
+    function keyEvent (var ev: THKeyEvent): Boolean; override;
+  end;
+
+
   THCtlCBListBox = class(THControl)
   private
-    mItems: array of AnsiString;
-    mChecks: array of PBoolean;
+    type
+      PItem = ^TItem;
+      TItem = record
+        title: AnsiString;
+        varp: PBoolean;
+        actionCB: TActionCB;
+      end;
+  private
+    mItems: array of TItem;
     mCurIndex: Integer;
 
   public
     constructor Create (ax, ay: Integer; aparent: THControl=nil);
     destructor Destroy (); override;
 
-    procedure appendItem (const atext: AnsiString; bv: PBoolean);
+    procedure appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
 
     procedure drawControl (sx, sy: Integer); override;
 
@@ -149,6 +194,7 @@ type
     function keyEvent (var ev: THKeyEvent): Boolean; override;
   end;
 
+
 // ////////////////////////////////////////////////////////////////////////// //
 var
   uiTopList: array of THControl = nil;
@@ -249,6 +295,10 @@ begin
       ctl.blurred();
       for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
       SetLength(uiTopList, Length(uiTopList)-1);
+      if (ctl is THTopWindow) then
+      begin
+        if assigned(THTopWindow(ctl).closeCB) then THTopWindow(ctl).closeCB(ctl, 0);
+      end;
       exit;
     end;
   end;
@@ -288,6 +338,7 @@ begin
   mEatKeys := false;
   scallowed := false;
   mDrawShadow := false;
+  actionCB := nil;
 end;
 
 
@@ -717,6 +768,10 @@ begin
   begin
     if (ctl <> topLevel.mFocused) then ctl.setFocused(true);
     result := ctl.mouseEvent(ev);
+  end
+  else if (ctl = self) and assigned(actionCB) then
+  begin
+    actionCB(self, 0);
   end;
 end;
 
@@ -730,7 +785,7 @@ begin
   if (topLevel.mFocused <> self) and isMyChild(topLevel.mFocused) and topLevel.mFocused.mEnabled then result := topLevel.mFocused.keyEvent(ev);
   if (mParent = nil) then
   begin
-    if (ev.kstate = THKeyEvent.ModShift) and (ev.scan = SDL_SCANCODE_TAB) then
+    if (ev.kind = ev.Press) and (ev = 'S-Tab') then
     begin
       result := true;
       if (ev.kind = ev.Press) then
@@ -744,7 +799,7 @@ begin
       end;
       exit;
     end;
-    if (ev.kstate = 0) and (ev.scan = SDL_SCANCODE_TAB) then
+    if (ev.kind = ev.Press) and (ev = 'Tab') then
     begin
       result := true;
       if (ev.kind = ev.Press) then
@@ -758,7 +813,7 @@ begin
       end;
       exit;
     end;
-    if mEscClose and (ev.kind = ev.Press) and (ev.kstate = 0) and (ev.scan = SDL_SCANCODE_ESCAPE) then
+    if mEscClose and (ev.kind = ev.Press) and (ev = 'Escape') then
     begin
       result := true;
       uiRemoveWindow(self);
@@ -776,14 +831,27 @@ begin
   mFrameWidth := 8;
   mFrameHeight := 8;
   mTitle := atitle;
-  if (mWidth < mFrameWidth*2) then mWidth := mFrameWidth*2;
+  if (mWidth < mFrameWidth*2+3*8) then mWidth := mFrameWidth*2+3*8;
   if (mHeight < mFrameHeight*2) then mHeight := mFrameHeight*2;
   if (Length(mTitle) > 0) then
   begin
-    if (mWidth < Length(mTitle)*8+mFrameWidth*2) then mWidth := Length(mTitle)*8+mFrameWidth*2;
+    if (mWidth < Length(mTitle)*8+mFrameWidth*2+3*8) then mWidth := Length(mTitle)*8+mFrameWidth*2+3*8;
   end;
   mDragging := false;
   mDrawShadow := true;
+  mWaitingClose := false;
+  mInClose := false;
+  closeCB := nil;
+end;
+
+
+procedure THTopWindow.centerInScreen ();
+begin
+  if (mWidth > 0) and (mHeight > 0) then
+  begin
+    mX := (gWinSizeX-mWidth) div 2;
+    mY := (gWinSizeY-mHeight) div 2;
+  end;
 end;
 
 
@@ -808,13 +876,18 @@ begin
   begin
     drawRect(mX+3, mY+3, mWidth-6, mHeight-6, r, g, b);
     drawRect(mX+5, mY+5, mWidth-10, mHeight-10, r, g, b);
+    setScissor(mFrameWidth, 0, 3*8, 8);
+    fillRect(mX+mFrameWidth, mY, 3*8, 8, 0, 0, 128);
+    drawText8(mX+mFrameWidth, mY, '[ ]', r, g, b);
+    if mInClose then drawText8(mX+mFrameWidth+7, mY, '#', 0, 255, 0)
+    else drawText8(mX+mFrameWidth+7, mY, '*', 0, 255, 0);
   end;
   if (Length(mTitle) > 0) then
   begin
-    setScissor(mFrameWidth, 0, mWidth-mFrameWidth*2, 8);
-    tx := (mWidth-Length(mTitle)*8) div 2;
-    fillRect(mX+tx-3, mY, Length(mTitle)*8+3+2, 8, 0, 0, 128);
-    drawText8(mX+tx, mY, mTitle, r, g, b);
+    setScissor(mFrameWidth+3*8, 0, mWidth-mFrameWidth*2-3*8, 8);
+    tx := (mX+3*8)+((mWidth-3*8)-Length(mTitle)*8) div 2;
+    fillRect(tx-3, mY, Length(mTitle)*8+3+2, 8, 0, 0, 128);
+    drawText8(tx, mY, mTitle, r, g, b);
   end;
   inherited drawControlPost(sx, sy);
 end;
@@ -823,6 +896,8 @@ end;
 procedure THTopWindow.blurred ();
 begin
   mDragging := false;
+  mWaitingClose := false;
+  mInClose := false;
   inherited;
 end;
 
@@ -831,7 +906,7 @@ function THTopWindow.keyEvent (var ev: THKeyEvent): Boolean;
 begin
   result := inherited keyEvent(ev);
   if not getFocused then exit;
-  if (ev.kstate = ev.ModAlt) and (ev.kind = ev.Press) and (ev.scan = SDL_SCANCODE_F3) then
+  if (ev.kind = ev.Press) and (ev = 'M-F3') then
   begin
     uiRemoveWindow(self);
     result := true;
@@ -859,13 +934,30 @@ begin
     exit;
   end;
 
-  if (ev.kind = ev.Press) and (ev.but = ev.Left) then
+  lx := ev.x;
+  ly := ev.y;
+  if toLocal(lx, ly) then
   begin
-    lx := ev.x;
-    ly := ev.y;
-    if toLocal(lx, ly) then
+    if (ev.kind = ev.Press) then
     begin
       if (ly < 8) then
+      begin
+        if (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
+        begin
+          //uiRemoveWindow(self);
+          mWaitingClose := true;
+          mInClose := true;
+        end
+        else
+        begin
+          mDragging := true;
+          mDragStartX := ev.x;
+          mDragStartY := ev.y;
+        end;
+        result := true;
+        exit;
+      end;
+      if (lx < mFrameWidth) or (lx >= mWidth-mFrameWidth) or (ly >= mHeight-mFrameHeight) then
       begin
         mDragging := true;
         mDragStartX := ev.x;
@@ -874,24 +966,118 @@ begin
         exit;
       end;
     end;
-  end;
 
-  if (ev.kind = ev.Press) and (ev.but = ev.Right) then
-  begin
-    lx := ev.x;
-    ly := ev.y;
-    if toLocal(lx, ly) then
+    if (ev.kind = ev.Release) then
     begin
-      if (ly < 8) then
+      if mWaitingClose and (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
       begin
         uiRemoveWindow(self);
         result := true;
         exit;
       end;
+      mWaitingClose := false;
+      mInClose := false;
+    end;
+
+    if (ev.kind = ev.Motion) then
+    begin
+      if mWaitingClose then
+      begin
+        mInClose := (lx >= mFrameWidth) and (lx < mFrameWidth+3*8);
+        result := true;
+        exit;
+      end;
+    end;
+  end
+  else
+  begin
+    mInClose := false;
+    if (ev.kind <> ev.Motion) then mWaitingClose := false;
+  end;
+
+  result := inherited mouseEvent(ev);
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+constructor THCtlSimpleText.Create (ax, ay: Integer; aparent: THControl=nil);
+begin
+  mItems := nil;
+  inherited Create(ax, ay, 4, 4);
+end;
+
+
+destructor THCtlSimpleText.Destroy ();
+begin
+  mItems := nil;
+  inherited;
+end;
+
+
+procedure THCtlSimpleText.appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
+var
+  it: PItem;
+begin
+  if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
+  SetLength(mItems, Length(mItems)+1);
+  it := @mItems[High(mItems)];
+  it.title := atext;
+  it.centered := acentered;
+  it.hline := ahline;
+  if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
+end;
+
+
+procedure THCtlSimpleText.drawControl (sx, sy: Integer);
+var
+  f, tx: Integer;
+  it: PItem;
+  r, g, b: Integer;
+begin
+  for f := 0 to High(mItems) do
+  begin
+    it := @mItems[f];
+    tx := sx;
+    r := 255;
+    g := 255;
+    b := 0;
+    if it.centered then begin b := 255; tx := sx+(mWidth-Length(it.title)*8) div 2; end;
+    if it.hline then
+    begin
+      b := 255;
+      if (Length(it.title) = 0) then
+      begin
+        drawLine(sx+4, sy+3, sx+mWidth-8, sy+3, r, g, b);
+      end
+      else if (tx-3 > sx+4) then
+      begin
+        drawLine(sx+4, sy+3, tx-3, sy+3, r, g, b);
+        drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, r, g, b);
+      end;
     end;
+    drawText8(tx, sy, it.title, r, g, b);
+    Inc(sy, 8);
   end;
+end;
+
 
+function THCtlSimpleText.mouseEvent (var ev: THMouseEvent): Boolean;
+var
+  lx, ly: Integer;
+begin
   result := inherited mouseEvent(ev);
+  lx := ev.x;
+  ly := ev.y;
+  if not result and toLocal(lx, ly) then
+  begin
+    result := true;
+  end;
+end;
+
+
+function THCtlSimpleText.keyEvent (var ev: THKeyEvent): Boolean;
+begin
+  result := inherited keyEvent(ev);
 end;
 
 
@@ -899,7 +1085,6 @@ end;
 constructor THCtlCBListBox.Create (ax, ay: Integer; aparent: THControl=nil);
 begin
   mItems := nil;
-  mChecks := nil;
   mCurIndex := -1;
   inherited Create(ax, ay, 4, 4);
 end;
@@ -908,19 +1093,21 @@ end;
 destructor THCtlCBListBox.Destroy ();
 begin
   mItems := nil;
-  mChecks := nil;
   inherited;
 end;
 
 
-procedure THCtlCBListBox.appendItem (const atext: AnsiString; bv: PBoolean);
+procedure THCtlCBListBox.appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
+var
+  it: PItem;
 begin
-  if (Length(atext)*8+4+3*8+2 > mWidth) then mWidth := Length(atext)*8+4+3*8+2;
+  if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
   SetLength(mItems, Length(mItems)+1);
-  mItems[High(mItems)] := atext;
-  SetLength(mChecks, Length(mChecks)+1);
-  mChecks[High(mChecks)] := bv;
-  if (Length(mItems)*8+4 > mHeight) then mHeight := Length(mItems)*8+4;
+  it := @mItems[High(mItems)];
+  it.title := atext;
+  it.varp := bv;
+  it.actionCB := aaction;
+  if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
   if (mCurIndex < 0) then mCurIndex := 0;
 end;
 
@@ -928,29 +1115,26 @@ end;
 procedure THCtlCBListBox.drawControl (sx, sy: Integer);
 var
   f, tx: Integer;
+  it: PItem;
 begin
-  //fillRect(sx, sy, mWidth, mHeight, 0, 128, 0);
-  Inc(sx, 2);
-  Inc(sy, 2);
   for f := 0 to High(mItems) do
   begin
-    if (mCurIndex = f) then fillRect(sx-2, sy, mWidth, 8, 0, 128, 0);
-    if (mChecks[f] <> nil) then
+    it := @mItems[f];
+    if (mCurIndex = f) then fillRect(sx, sy, mWidth, 8, 0, 128, 0);
+    if (it.varp <> nil) then
     begin
-      //drawText8(sx, sy, '[ ]', 255, 255, 255);
-      //if mChecks[f]^ then drawText8(sx+6, sy, 'x', 255, 255, 255);
-      if mChecks[f]^ then drawText8(sx, sy, '[x]', 255, 255, 255) else drawText8(sx, sy, '[ ]', 255, 255, 255);
-      drawText8(sx+3*8+2, sy, mItems[f], 255, 255, 0);
+      if it.varp^ then drawText8(sx, sy, '[x]', 255, 255, 255) else drawText8(sx, sy, '[ ]', 255, 255, 255);
+      drawText8(sx+3*8+2, sy, it.title, 255, 255, 0);
     end
-    else if (Length(mItems[f]) > 0) then
+    else if (Length(it.title) > 0) then
     begin
-      tx := sx+(mWidth-Length(mItems[f])*8) div 2;
+      tx := sx+(mWidth-Length(it.title)*8) div 2;
       if (tx-3 > sx+4) then
       begin
         drawLine(sx+4, sy+3, tx-3, sy+3, 255, 255, 255);
-        drawLine(tx+Length(mItems[f])*8, sy+3, sx+mWidth-4, sy+3, 255, 255, 255);
+        drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, 255, 255, 255);
       end;
-      drawText8(sx+(mWidth-Length(mItems[f])*8) div 2, sy, mItems[f], 255, 255, 255);
+      drawText8(tx, sy, it.title, 255, 255, 255);
     end
     else
     begin
@@ -964,21 +1148,27 @@ end;
 function THCtlCBListBox.mouseEvent (var ev: THMouseEvent): Boolean;
 var
   lx, ly: Integer;
+  it: PItem;
 begin
   result := inherited mouseEvent(ev);
-  if not result and (Length(mItems) > 0) and (ev.kind = ev.Press) then
+  lx := ev.x;
+  ly := ev.y;
+  if not result and toLocal(lx, ly) then
   begin
-    lx := ev.x;
-    ly := ev.y;
-    if toLocal(lx, ly) then
+    result := true;
+    if (ev.kind = ev.Press) and (ev = 'lmb') then
     begin
-      if (ly < 2) then ly := 2;
       ly := ly div 8;
-      if (ly < 0) then ly := 0 else if (ly > High(mItems)) then ly := High(mItems);
-      if (mChecks[ly] <> nil) then
+      if (ly >= 0) and (ly < Length(mItems)) then
       begin
-        mCurIndex := ly;
-        if (mChecks[ly] <> nil) then mChecks[ly]^ := not mChecks[ly]^;
+        it := @mItems[ly];
+        if (it.varp <> nil) then
+        begin
+          mCurIndex := ly;
+          it.varp^ := not it.varp^;
+          if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
+          if assigned(actionCB) then actionCB(self, ly);
+        end;
       end;
     end;
   end;
@@ -986,65 +1176,68 @@ end;
 
 
 function THCtlCBListBox.keyEvent (var ev: THKeyEvent): Boolean;
+var
+  it: PItem;
 begin
   result := inherited keyEvent(ev);
   if not getFocused then exit;
   //result := true;
-  if (ev.kstate = 0) and (ev.kind = ev.Press) then
+  if (ev.kind = ev.Press) then
   begin
-    case ev.scan of
-      SDL_SCANCODE_HOME,
-      SDL_SCANCODE_PAGEUP:
-        begin
-          result := true;
-          mCurIndex := 0;
-        end;
-      SDL_SCANCODE_END,
-      SDL_SCANCODE_PAGEDOWN:
-        begin
-          result := true;
-          mCurIndex := High(mItems);
-        end;
-      SDL_SCANCODE_UP:
-        begin
-          result := true;
-          if (Length(mItems) > 0) then
-          begin
-            if (mCurIndex < 0) then mCurIndex := Length(mItems);
-            while (mCurIndex > 0) do
-            begin
-              Dec(mCurIndex);
-              if (mChecks[mCurIndex] <> nil) then break;
-            end;
-          end
-          else
-          begin
-            mCurIndex := -1;
-          end;
-        end;
-      SDL_SCANCODE_DOWN:
+    if (ev = 'Home') or (ev = 'PageUp') then
+    begin
+      result := true;
+      mCurIndex := 0;
+    end;
+    if (ev = 'End') or (ev = 'PageDown') then
+    begin
+      result := true;
+      mCurIndex := High(mItems);
+    end;
+    if (ev = 'Up') then
+    begin
+      result := true;
+      if (Length(mItems) > 0) then
+      begin
+        if (mCurIndex < 0) then mCurIndex := Length(mItems);
+        while (mCurIndex > 0) do
         begin
-          result := true;
-          if (Length(mItems) > 0) then
-          begin
-            if (mCurIndex < 0) then mCurIndex := -1;
-            while (mCurIndex < High(mItems)) do
-            begin
-              Inc(mCurIndex);
-              if (mChecks[mCurIndex] <> nil) then break;
-            end;
-          end
-          else
-          begin
-            mCurIndex := -1;
-          end;
+          Dec(mCurIndex);
+          if (mItems[mCurIndex].varp <> nil) then break;
         end;
-      SDL_SCANCODE_SPACE,
-      SDL_SCANCODE_RETURN:
+      end
+      else
+      begin
+        mCurIndex := -1;
+      end;
+    end;
+    if (ev = 'Down') then
+    begin
+      result := true;
+      if (Length(mItems) > 0) then
+      begin
+        if (mCurIndex < 0) then mCurIndex := -1;
+        while (mCurIndex < High(mItems)) do
         begin
-          result := true;
-          if (mCurIndex >= 0) and (mCurIndex < Length(mChecks)) and (mChecks[mCurIndex] <> nil) then mChecks[mCurIndex]^ := not mChecks[mCurIndex]^;
+          Inc(mCurIndex);
+          if (mItems[mCurIndex].varp <> nil) then break;
         end;
+      end
+      else
+      begin
+        mCurIndex := -1;
+      end;
+    end;
+    if (ev = 'Space') or (ev = 'Return') then
+    begin
+      result := true;
+      if (mCurIndex >= 0) and (mCurIndex < Length(mItems)) and (mItems[mCurIndex].varp <> nil) then
+      begin
+        it := @mItems[mCurIndex];
+        it.varp^ := not it.varp^;
+        if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
+        if assigned(actionCB) then actionCB(self, mCurIndex);
+      end;
     end;
   end;
 end;