Browse Source

Gui Button down state

Arthur Yefimov 3 years ago
parent
commit
98a6761ce8
2 changed files with 58 additions and 16 deletions
  1. 35 9
      Programs/Buttons.Mod
  2. 23 7
      Programs/Gui.Mod

+ 35 - 9
Programs/Buttons.Mod

@@ -6,26 +6,52 @@ TYPE
   ButtonDesc* = RECORD(Gui.WidgetDesc)
   END;
 
-PROCEDURE DrawButton*(W: Gui.Widget; x, y: INTEGER);
+PROCEDURE DrawNormalButton*(W: Gui.Widget; x, y: INTEGER);
 VAR c: G.Color;
-  f: G.Font;
-  fw, fh: INTEGER;
 BEGIN
-  G.MakeCol(c, 210, 205, 200);
+  G.MakeCol(c, 212, 208, 200);
   G.FillRect(x, y, x + W.w - 1, y + W.h - 1, c);
   G.MakeCol(c, 255, 255, 255);
   G.Rect(x, y, x + W.w - 1, y + W.h - 1, c);
-  G.MakeCol(c, 0, 0, 0);
+  G.MakeCol(c, 64, 64, 64);
   G.HLine(x, y + W.h - 1, x + W.w - 1, c);
   G.VLine(x + W.w - 1, y, y + W.h - 1, c);
-  G.MakeCol(c, 80, 80, 80);
+  G.MakeCol(c, 128, 128, 128);
   G.HLine(x + 1, y + W.h - 2, x + W.w - 2, c);
-  G.VLine(x + W.w - 2, y + 1, y + W.h - 2, c);
+  G.VLine(x + W.w - 2, y + 1, y + W.h - 2, c)
+END DrawNormalButton;
+
+PROCEDURE DrawDownButton*(W: Gui.Widget; x, y: INTEGER);
+VAR c: G.Color;
+BEGIN
+  G.MakeCol(c, 212, 208, 200);
+  G.FillRect(x, y, x + W.w - 1, y + W.h - 1, c);
+  G.MakeCol(c, 64, 64, 64);
+  G.Rect(x, y, x + W.w - 1, y + W.h - 1, c);
+  G.MakeCol(c, 255, 255, 255);
+  G.HLine(x + 1, y + W.h - 1, x + W.w - 1, c);
+  G.VLine(x + W.w - 1, y + 1, y + W.h - 1, c);
+  G.MakeCol(c, 128, 128, 128);
+  G.HLine(x + 1, y + 1, x + W.w - 2, c);
+  G.VLine(x + 1, y + 1, y + W.h - 2, c)
+END DrawDownButton;
+
+PROCEDURE DrawButton*(W: Gui.Widget; x, y: INTEGER);
+VAR c: G.Color;
+  f: G.Font;
+  fw, fh: INTEGER;
+BEGIN
+  IF Gui.down IN W.state THEN DrawDownButton(W, x, y)
+  ELSE DrawNormalButton(W, x, y)
+  END;
   f := Gui.GetFont(W);
   G.GetMonoFontSize(f, fw, fh);
   G.MakeCol(c, 0, 0, 0);
-  G.DrawString(W.text.s(*!FIXME*), x + 4,
-    y + (W.h - fh) DIV 2, f, c)
+
+  INC(x, 4); INC(y, (W.h - fh) DIV 2);
+  IF Gui.down IN W.state THEN INC(x); INC(y) END;
+
+  G.DrawString(W.text.s(*!FIXME*), x, y, f, c)
 END DrawButton;
 
 PROCEDURE InitButton*(b: Button; w, h: INTEGER; text: ARRAY OF CHAR);

+ 23 - 7
Programs/Gui.Mod

@@ -1,6 +1,12 @@
 MODULE Gui;
 IMPORT G := Graph, Strings, Out;
 
+CONST
+  (* Widget.state set members *)
+  hover* = 0;
+  down*  = 1;
+  focus* = 2;
+
 TYPE
   Caption* = POINTER TO CaptionDesc;
   CaptionDesc* = RECORD
@@ -20,6 +26,9 @@ TYPE
   WidgetDesc* = RECORD
     x*, y*, w*, h*: INTEGER;
     tag*: INTEGER;
+    state*: SET;
+    visible: BOOLEAN;
+    enabled: BOOLEAN;
     body*: Widget;
     text*: Caption;
     bmp*: G.Bitmap;
@@ -71,6 +80,9 @@ PROCEDURE InitWidget*(w: Widget);
 BEGIN
   w.x := 0; w.y := 0; w.w := 24; w.h := 24;
   w.tag := 0;
+  w.state := {};
+  w.visible := TRUE;
+  w.enabled := TRUE;
   w.draw := NIL;
   w.onMouseDown := NIL;
   w.onMouseUp := NIL;
@@ -224,9 +236,10 @@ BEGIN
   x := e.x; y := e.y;
   W := FindWidgetUnderMouse(globalWin, x, y);
   IF W # NIL THEN
-    globalWin.curMouseDownWidget := W (* Save for future mouse up event *)
-  END;
-  TriggerOnMouseDown(W, x, y, e.button)
+    INCL(W.state, down); (* Mark button as being mouse-downed *)
+    globalWin.curMouseDownWidget := W; (* Save for future mouse up event *)
+    TriggerOnMouseDown(W, x, y, e.button)
+  END
 END HandleMouseDownEvent;
 
 PROCEDURE HandleMouseUpEvent*(e: G.Event);
@@ -235,10 +248,13 @@ VAR W: Widget;
 BEGIN
   x := e.x; y := e.y;
   W := globalWin.curMouseDownWidget;
-  WindowToWidgetXY(W, x, y);
-  TriggerOnMouseUp(W, x, y, e.button);
-  IF (x >= 0) & (y >= 0) & (W.w > x) & (W.h > y) THEN
-    TriggerOnClick(W)
+  IF W # NIL THEN
+    WindowToWidgetXY(W, x, y);
+    EXCL(W.state, down);
+    TriggerOnMouseUp(W, x, y, e.button);
+    IF (x >= 0) & (y >= 0) & (W.w > x) & (W.h > y) & (e.button = 1) THEN
+      TriggerOnClick(W)
+    END
   END
 END HandleMouseUpEvent;