DEADSOFTWARE

"hlm_ui_scale" convar; "--holmes_ui_scale" cli arg; (fgsfds request)
[d2df-sdl.git] / src / game / g_holmes_ui.inc
index 79a21e77a0763b1876bb008b7b7ef614e39763c0..d9909a2586a3540147eb716f3128443d75674226 100644 (file)
@@ -133,6 +133,8 @@ type
   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;
@@ -142,6 +144,31 @@ 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
     type
@@ -167,6 +194,7 @@ type
     function keyEvent (var ev: THKeyEvent): Boolean; override;
   end;
 
+
 // ////////////////////////////////////////////////////////////////////////// //
 var
   uiTopList: array of THControl = nil;
@@ -636,6 +664,10 @@ end;
 procedure THControl.setScissorGLInternal (x, y, w, h: Integer);
 begin
   if not scallowed then exit;
+  x := trunc(x*g_holmes_ui_scale);
+  y := trunc(y*g_holmes_ui_scale);
+  w := trunc(w*g_holmes_ui_scale);
+  h := trunc(h*g_holmes_ui_scale);
   y := gWinSizeY-(y+h);
   if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then glScissor(0, 0, 0, 0) else glScissor(x, y, w, h);
 end;
@@ -757,7 +789,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
@@ -771,7 +803,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
@@ -785,7 +817,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);
@@ -817,6 +849,16 @@ begin
 end;
 
 
+procedure THTopWindow.centerInScreen ();
+begin
+  if (mWidth > 0) and (mHeight > 0) then
+  begin
+    mX := trunc((gWinSizeX/g_holmes_ui_scale-mWidth)/2);
+    mY := trunc((gWinSizeY/g_holmes_ui_scale-mHeight)/2);
+  end;
+end;
+
+
 procedure THTopWindow.drawControl (sx, sy: Integer);
 begin
   fillRect(sx, sy, mWidth, mHeight, 0, 0, 128);
@@ -847,7 +889,7 @@ begin
   if (Length(mTitle) > 0) then
   begin
     setScissor(mFrameWidth+3*8, 0, mWidth-mFrameWidth*2-3*8, 8);
-    tx := mX+(mWidth-Length(mTitle)*8) div 2;
+    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;
@@ -868,7 +910,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;
@@ -961,6 +1003,88 @@ begin
 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;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 constructor THCtlCBListBox.Create (ax, ay: Integer; aparent: THControl=nil);
 begin
@@ -1014,7 +1138,7 @@ begin
         drawLine(sx+4, sy+3, tx-3, 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(it.title)*8) div 2, sy, it.title, 255, 255, 255);
+      drawText8(tx, sy, it.title, 255, 255, 255);
     end
     else
     begin
@@ -1036,7 +1160,7 @@ begin
   if not result and toLocal(lx, ly) then
   begin
     result := true;
-    if (ev.kind = ev.Press) then
+    if (ev.kind = ev.Press) and (ev = 'lmb') then
     begin
       ly := ly div 8;
       if (ly >= 0) and (ly < Length(mItems)) then
@@ -1062,67 +1186,62 @@ 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 (mItems[mCurIndex].varp <> 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 (mItems[mCurIndex].varp <> 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(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;
+          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;