Post Reply 
Entering the 'Edit Integer' window
01-10-2024, 05:09 PM (This post was last modified: 01-10-2024 08:10 PM by komame.)
Post: #23
RE: Entering the 'Edit Integer' window
As promised, I have prepared another solution. This version operates on values from 0 to FFFFFFFFFFFFFFF (which is significantly larger than before) and it also has cursor control, so you can edit individual digits in DEC and HEX values. The program has become quite complicated due to the implementation of the editor. It may still be possible to simplify it.

Code:
KEY KS_Minus()
BEGIN
  local wd, wh, white=16777215;
  local p=1, key, keys=MAKELIST(-1,X,0,50), value=#0:64d;
  keys:=REPLACE(keys,32,{7,8,9,-1,-1,4,5,6,-1,-1,1,2,3,-1,-1,0});
  keys:=REPLACE(keys,14,{10,11,12,13,14,99,15});
  keys[0]:=20;
  keys[1]:=21;
  keys[7]:=22;
  keys[8]:=23;
  keys[30]:=100;
  local s,dt,i,ht,txd="",txh="",he=#0:1b;
  RECT_P(18,80,246+38+18,146,0,white);
  BLIT_P(18,61,247+38+18,81,G0,0,0,1,20);
  TEXTOUT_P("Edit Integer",123,62,3,white);
  TEXTOUT_P("DEC:",24,86,2,0); TEXTOUT_P("HEX:",24,100,2,0);
  TEXTOUT_P("APPS => base, ENTER => OK",88,130,1);

  Bits:=32;
  REPEAT
    IF value=0 THEN
      ""▶txd▶txh▶dt▶ht
    ELSE
      local sz:=STRING(value,1);
      dt:=MID(sz,2,SIZE(sz)-5);
      sz:=STRING(SETBASE(value,4));
      ht:=MID(sz,2,SIZE(sz)-5);
      txd:=CHAR(ASC(MID(STRING(value,1)▶txd,2,SIZE(txd)-5))+65248);
      txh:=CHAR(ASC(MID(STRING(SETBASE(value,4))▶txh,2,SIZE(txh)-5))+65248);
    END;

    wd:=TEXTOUT_P(txd,51,86,2,0,251,white);
    RECT_P(wd,81,301,97); // clear the space after DEC
    wh:=TEXTOUT_P(txh,51,100,2,0,251,white);
    RECT_P(wh,100,301,111); // clear the space after HEX

    IF value = #0 THEN
      51▶wd▶wh;
    END;
    INVERT_P(42+p*12,IFTE(he,100,86),48+p*12,IFTE(he,111,97));
    key:=keys[WAIT(0)];
    IF key<>-1 THEN
      key:=SETBASE(key,3);
    END;
    CASE
      IF key=100 THEN
        RETURN "#"+ht+"h";
      END;
      IF key=99 THEN
        IF p>1 THEN
          p:=p-1;        
          IF he THEN
            ht:=SUPPRESS(ht,p);
            value:=SETBASE(EXPR("#"+ht+":64h"),3);        
          ELSE
            dt:=SUPPRESS(dt,p);
            value:=EXPR("#"+dt+":64d");
          END;          
          IF(SIZE(STRING(value,1))<p) THEN
            p:=1;
          END;
        END;
      END;
      IF key >= 0 AND key < 10+he*9 THEN
        local ov=value, ot=IFTE(he,ht,dt);
        //value:= IFTE(key=99,value/(#10d+#6d*he),value*(#10d+#6d*he)+key);
        IF key<>99 THEN
          IF p=1 AND key=0 THEN
            CONTINUE;
          END;
          IF he THEN
            local ch:=CHAR(IFTE(key<10,key+48,key+55));
            IF SIZE(ht)<p THEN
              p:=p+1;
              ht:=ht+ch;
            ELSE
              ht[p]:=ch;
              p:=p+1;
            END;
            value:=SETBASE(EXPR("#"+ht+":64h"),3);
          ELSE
            IF SIZE(dt)<p THEN
              p:=p+1;
              dt:=dt+CHAR(key+48);
            ELSE
              dt[p]:=key+48;  
              p:=p+1;
            END;
            value:=EXPR("#"+dt+":64d");
          END;
          IF value > #FFFFFFFFFFFFFFF:64h THEN
            p:=p-1;
            value:=ov;
            IF he THEN
              ht:=ot;
            ELSE
              dt:=ot;
            END; 
          END;
        END;
      END;
      IF key=20 THEN // Apps changes the base
        he:=NOT he;
        IF he AND SIZE(ht)<p THEN
          p:=SIZE(ht)+1;
        END
      END;
      IF key=22 AND p>1 THEN // left
        p:=p-1;
      END;
      IF key=23 THEN // right
        IF he THEN
          IF p<=SIZE(ht) THEN
            p:=p+1;
          END;
        ELSE
          IF p<=SIZE(dt) THEN
            p:=p+1;
          END;
        END;
      END;
      IF key=21 THEN   // Symb Key sets value to 0
        value:=#0:64d;
      END;
    END;
  UNTIL ISKEYDOWN(4);  //ESC will exit

  RETURN 46;  //one way to exit program with no apparent consequences
END;

Piotr
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Entering the 'Edit Integer' window - komame - 01-10-2024 05:09 PM



User(s) browsing this thread: 6 Guest(s)