Announcement

Collapse
No announcement yet.

Add a graphical code in a form.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add a graphical code in a form.

    Hi everyone,

    I'm trying to add a code into a specific form only, but I don't really understand how MagicCode/MagicEvent work.
    (I'm trying to put this code https://forum.4dsystems.com.au/node/45814 into my form14)

    I tried to create MagicCode and MagicEvent with this follow example:

    Code:
    func MagicEvent6(var newval)               in MagicCode file                    in MagicCode file
        if(CurrentForm == 14)                      if(CurrentForm == 14)              
            print("Example");               OR       print("Example")            OR      print("Example")
    endfunc
    All this example display nothing on the screen.
    I read https://www.4dsystems.com.au/appnote/4D-AN-00199/ and https://www.4dsystems.com.au/appnote/4D-AN-00182/ , but these docs don't explain a lot.

    If you have something better to explain or solution to do what I want !

    Best Regards,
    Toragan

    EDIT:

    I finally find a solution using a MagicCode as Const/Global/Data and another one as PostActivateForm.

    But, I'm still interested in informations about MagicCode and MagicEvent in general.

    Best regards,
    Toragan
    Last edited by Toragan; 8th May 2019, 12:59 AM.

  • #2
    Hello Toragan,

    The Magic Event and Magic Code description are described in the application note that you have mentioned:
    ViSi-Genie How to Add Magic Objects
    ViSi-Genie Magic Code Insertion Points

    Magic Event
    Events are usually triggered when input objects such as winbutton objects on the display are pressed.
    In the Genie environment of the standard Workshop IDE, the OnChanged event property of a winbutton, for example,
    can be configured to toggle the state of a LED object.

    In Workshop Pro, it is now possible for the user to create a custom event, through 4DGL coding, in
    the form of a Magic Event object. For instance, a winbutton, when pressed,
    can make a LED object blink ten times.

    Supporting Application Notes and Examples are also provided:
    1.) ViSi-Genie Magic Slide to Unlock
    2.) ViSi-Genie Magic Main Loop Counter
    3.) ViSi-Genie Magic Button Counters
    Magic Event is used when you want to invoke a response from an input 'widget' (e.g button). As explained above, you can make the
    LED blink ten times or you can also print something on the display. Either way, this function is active only when called.


    Magic Code
    A Magic Code object allows the user to insert custom 4DGL code into specific locations inside the Genie project.
    For example, a counter variable can be declared and initialized in a Magic Code object inserted to the location
    “Constant/Global/Data”. This variable can then be accessed and used by another Magic Code object inserted at the location “MainLoop”.

    Supporting Application Notes and Examples are also provided:
    1.) ViSi-Genie Magic Main Loop Counter
    2.) ViSi-Genie Magic Keyboard Edit
    Magic code has 6 insertion points. It would be best to look at this picture to further understand its structure.
    You can try to print something on each point to see where your code is running in the program.
    Click image for larger version

Name:	Magic Code Structure.PNG
Views:	58
Size:	54.4 KB
ID:	68165

    I have attached a simple program using Magic Events and Magic Code. I hope this helps.

    Best Regards,
    Kevin

    Comment


    • #3
      Hi Kevin,

      I better understanding now.

      But I still have a problem with something I can't understand.

      Fatal Error : Functions can't be nested (line 31 column 1 file:MagicCode3.inc)

      1 - I don't understand why she can't be nested. Maybe I did something wrong, I'll try to find an issue and EDIT the post if it's done.
      2 - When I'd like to insert more items (red lines), my project crash but the items size seems be enough.


      MagicCode3.inc (Constant/Global/Data)
      Code:
      // font to use for listbox
      //#constant font     -1           // select -1 to use usd font file "font.gci" and "font.dat"
      #constant font     FONT3       // otherwise use 'standard' font
      #constant fontMulH 2            // font height multiplier
      #constant fontMulW 1            // font width multiplier
      #constant largelist             // uncomment to define a large list to show 'slowdown'
      
      // constants for demo part (and implementation, depending if fixed size is used)
      var items[1000] ;           // string to contain entries, could be mem_alloc'd
      
      // constants for listbox
      #constant maxent    100     // maximum number of entries in list
      #constant seljitter 3       // amount of y pixels that touch can move before it is considered a move and not a touch
      #constant selbgcol  BLUE    // selected background colour
      #constant normbgcol WHITE   // un-selected background colour
      #constant selfgcol  WHITE   // selected foreground colour
      #constant normfgcol GRAY    // un-selected foreground colour
      #constant ygap      1       // space between entries, larger font is a better idea
      #constant boxcolour BLACK   // colour of the listbox
      #constant insidespace 5     // blank space inside listbox (+1)
      #constant boxwidth 2        // only 1 or 2 supported, see code
      
      const var itempos[maxent] ;       // string pointer to each entry
      var itemsel ;               // selected item, -1 means nothing selected
      var itemcnt ;               // number of items
      var itemmaxh ;              // height of each item
      var itemsw ;                // number of items in window
      var postop ;                // top position in window (pixels)
      var vptop, vpleft, vpright ; // writeable area within listboxfunc printitems()
          var i, j, bgc ;
          gfx_Clipping(ON) ;
          gfx_MoveTo(vpleft,vptop+postop) ;
          for(i:=0; i<itemcnt; i++)
              if (itemsel == i)
                  txt_FGcolour(selfgcol) ;
                  bgc := selbgcol ;
              else
                  txt_FGcolour(normfgcol);
                  bgc := normbgcol ;
              endif
              txt_BGcolour(bgc);
              j := itempos[i] ;
              str_Printf(&j, "%s\n") ;
              gfx_RectangleFilled(peekW(GFX_X2), peekW(GFX_Y1), vpright, peekW(GFX_Y1)+itemmaxh, bgc) ;       // fill out to width of listbox
          next
          gfx_RectangleFilled(vpleft, peekW(GFX_Y1)+itemmaxh, vpright, peekW(GFX_Y1)+itemmaxh*2, normbgcol) ; // fill out bottom of screen (only important at end or list)
          gfx_Clipping(OFF) ;
      endfunc
      
      func listbox(var ipitems, var font, var top, var left, var bottom, var right)
          var i, p, ogap, ts, tx, ty, topy, vpo, cit, cansel ;
          txt_FontID(font) ;
      #IF fontMulH != 1
          txt_Height(fontMulH);
      #ENDIF
      #IF fontMulH != 1
          txt_Width(fontMulW);
      #ENDIF
      
          itemcnt := 0 ;
          ogap := txt_Ygap(ygap);                 // set text gap, again to help with touchability
          p := str_Ptr(ipitems) ;
          while(str_GetByte(p) != 0)              // find each entry
              itempos[itemcnt++] := p ;           // save pointer intto the string
              p := str_Find(&p, "," );            // find end of required string
              str_PutByte(p++,'\0');              // change ',' to \0 to make individual strings
          wend
          itemsel := -1 ;
          itemmaxh := charheight(str_GetByte(itempos[0])) ;
          gfx_Rectangle(left, top, right, bottom, boxcolour) ;    // draw box to make listbox
      #IF boxwidth == 2
          left++ ;
          top++ ;
          bottom-- ;
          right-- ;
          gfx_Rectangle(left, top, right, bottom, boxcolour) ;    // draw box to make listbox
      #ENDIF
          vpleft  := left + insidespace ;
          vptop   := top + insidespace ;
          vpright := right - insidespace ;
          bottom -= insidespace ;
          itemsw := (bottom-vptop) / itemmaxh ;
          gfx_ClipWindow(vpleft, vptop, vpright, bottom);           // set the clipping area
          printitems() ;
      
          touch_Set(TOUCH_ENABLE);
          repeat
              ts := touch_Get(TOUCH_STATUS) ;
              tx := touch_Get(TOUCH_GETX) ;
              ty := touch_Get(TOUCH_GETY) ;
              if (ts == TOUCH_PRESSED)
                  if ((tx >= vpleft) && (tx <= vpright) && (ty >= vptop) &&  (ty <= bottom))
                      vpo := postop ;
                      topy := ty ;
                      cit := (ty - vptop - postop) / itemmaxh ;
                      cansel := 1 ;
                  else                                            // break loop if touched
                  break ;                                     // better just to ignore and use an 'ok' button
                  endif
              else if (ts == TOUCH_RELEASED)
                  if (cansel)                                     // select if touch and release within an entry and no moving outside set limits
                      itemsel := cit ;
                      printitems() ;
                  endif
              else if (ts == TOUCH_MOVING)
                  postop := vpo - topy + ty ;
                  if (postop > 0)                                 // ensure we can't go up beyond top
                      postop := 0 ;
                  else if (postop < -(itemcnt-itemsw)*itemmaxh)   // or down beyond bottom
                      postop := -(itemcnt-itemsw)*itemmaxh ;
                  endif
                  printitems() ;
                  if (ABS(topy-ty) > seljitter) cansel := 0 ;
              endif
          forever
      #IF fontMulH != 1
          txt_Height(1);  // reset
      #ENDIF
      #IF fontMulH != 1
          txt_Width(1);   // reset
      #ENDIF
          txt_Ygap(ogap);                         // reset text gap
          txt_FGcolour(normfgcol) ;
          txt_BGcolour(normbgcol) ;
          for(i:=1; i<itemcnt; i++)
              str_PutByte(itempos[i]-1,',');      // change \0 back to ','
          next
          return itemsel ;
      endfunc
      MagicCode4.inc (PostActiveForm)
      Code:
      #inherit "MagicCode3.inc"
      
      var check := 0;
      
      if (CurrentForm == 14)
          var count, hFont;
          to(items);
          print("[M12] : SweepMode,[M24] : Macro toto,[M1],[M2] : SweepMode,[M13],[M24] : Macro toto,[M24] : Macro toto,[M24] : SweepRegFreq RegAmpl Stop&Recov,[M24] : Macro toto,[M24] : Macro toto,[M24] : Macro toto,[M24] : Macro toto,[TEST] Verif de list");
      #IF EXISTS largelist
          for (count := 0; i < 50; count++)         // test of a huge list
              to(APPEND) ;
              print(",Item ", count+11) ;
          next
      #ENDIF
          while(check != 1)
              count := listbox(items, hFont, 70, 10, 230, 310) ;      // (non string) pointer to item list, font to use (could be uSD font), top, left, bottom, right or listbox
      
              gfx_MoveTo(0, 240) ;
              if (count == -1)
                  print("You didn't select anything") ;
              else
                  check := 1;
                  print("You Selected item ", count, "       ") ;
              endif
      //    print([STR] items) ;
          wend
      endif
      Best Regards,
      Toragan
      Last edited by Toragan; 9th May 2019, 08:22 PM.

      Comment


      • #4
        Hello Toragan,

        I have checked the code that you posted above and it seems that there are some items you replaced with the original code.

        For example this code:

        for(i:=0; i<itemcnt; i++)

        whereas, it should be

        for(i:=0; i<itemcnt; i++)

        Comparison Operators ('<' and '>') are replaced by some code for an unknown reason, can you check again at your end if that is the case?


        1 - I don't understand why she can't be nested. Maybe I did something wrong, I'll try to find an issue and EDIT the post if it's done.
        The error that you are receiving appears when you are inserting another function inside a function

        Example

        function 1()
        function 2()
        endfunc
        endfunc



        However, the error does not spawn in the code above still, you forgot to add the

        Code:
        func printitems()
        in line 31.

        2 - When I'd like to insert more items (red lines), my project crash but the items size seems be enough.
        Can I ask how are you adding more items? You can use the command:

        Code:
        to(APPEND) ;
        print(",Test 1,Test 2,Test 3");
        to insert more items on the array

        Attached is a modified version of your code, can you try and see if everything works correctly?
        Also, I added some comments please try to read them. Thanks

        I hope this helps.

        Best regards,
        Kevin
        Attached Files

        Comment


        • #5
          Hi Kevin,


          For example this code:

          for(i:=0; i&lt;itemcnt; i++)

          whereas, it should be

          for(i:=0; i<itemcnt; i++)

          Comparison Operators ('<' and '>') are replaced by some code for an unknown reason, can you check again at your end if that is the case?



          1 - I don't understand why she can't be nested. Maybe I did something wrong, I'll try to find an issue and EDIT the post if it's done.


          The error that you are receiving appears when you are inserting another function inside a function

          Example

          function 1()
          function 2()
          endfunc
          endfunc



          However, the error does not spawn in the code above still, you forgot to add the

          Code:

          func printitems()
          in line 31.
          I had some problems with the code and I copied a wrong part, sorry.

          2 - When I'd like to insert more items (red lines), my project crash but the items size seems be enough.


          Can I ask how are you adding more items? You can use the command:

          Code:

          to(APPEND) ; print(",Test 1,Test 2,Test 3");
          to insert more items on the array
          Yes it's what I did, I sure the problem comes from the counter in my loop.


          But now I have context error, following screenshot of errors.
          It's the reasons why I try to fix that, adding "#inherit "MagicCode3.inc"". But he don't know items, listbox.

          Click image for larger version  Name:	Capture.PNG Views:	0 Size:	12.3 KB ID:	68188

          And adding #inherit "MagicCode3.inc""

          Fatal Error : Functions can't be nested (line 31 column 1 file:MagicCode3.inc)
          Last edited by Toragan; 10th May 2019, 06:24 PM.

          Comment


          • #6
            Hello Toragan,

            Did you try to run the program that I have attached? Can you confirm that it is working as intended?

            The variable 'items' is declared on global, you should not have any problem accessing it.

            Can I ask what is the InsertPoint of your 'MagicCode3'? It should be on the Constant/Global/Data.


            Best Regards,
            Kevin

            Comment


            • #7
              Hi,

              Yes your program work perfectly, I copy and paste on my own but it's tell me Errors.
              I'm ok with you, "items" is a global and my file in Constant/Global/Data, it's the reason why i'm stuck.

              Best regards,
              Toragan

              Comment


              • John Kevin
                John Kevin commented
                Editing a comment
                Can you try to email me your project and I will take a look, john dot asuncion at 4dsystems dot com dot au.
                Last edited by John Kevin; 10th May 2019, 06:55 PM.

            • #8
              This is my project.

              The MagicCode3.inc is not implement in the 4DGenieS file, but finally my question is, why ?
              Why he's not implement by himself into 4DGenieS file ?

              When I'm trying to write #inherit "MagicCode3.inc" in the 4DGenieS file, he's automatically delete after compilation.
              Attached Files
              Last edited by Toragan; 10th May 2019, 10:45 PM.

              Comment


              • #9
                Hello Toragan,

                Sorry for the delayed response.

                Upon looking at your project, I have noticed that you already have a magic code for Constant/Global/Data (MagicCode1).
                You do not need to create another magic code for this, you can resolve the issue if you copy and paste your 'MagicCode3' in your 'MagicCode1'.
                Let me know if that helps.

                Best Regards,
                Kevin

                Comment


                • #10
                  Hi Kevin,

                  No problems !

                  It's working better, however when I sliding the listbox all words are duplicated like the listbox isn't never refresh.
                  I think about a loop ... It's working very well when I trying on the Form0, but when I'm writing if (CurrentForm == 14) it's happen that.
                  If I fix it I'll edit the post.

                  Ps: The problem of the crash of 4DWorkshop still doesn't seem fixed.
                  I tried to fix by different ways without success.


                  Best Regards
                  Thomas
                  Last edited by Toragan; 15th May 2019, 05:51 PM.

                  Comment


                  • #11
                    Hello,

                    I have tried to run your program and it really gets stuck on the sliding list. As of now, I am finding what is
                    causing the issue. I will get back to you as soon as I have found something relevant.

                    I noticed that when you run the program through setting the initial form to either '7' or '14' the sliding list works fine.

                    Best regards,
                    Kevin

                    Comment


                    • #12
                      Hi,

                      Thanks you.
                      I'm also trying to find something, I'll tell you if I have interesting things.

                      EDIT: I tried with a smaller project, I created 4 forms and put the sliding list in the form3. It works very well ...
                      I have tried many things to fix that but nothing works, I am really stuck in this problem.
                      Ps: It works when it's load on the first form, but if you press the screen outside the sliding list, the programme reboot and it's broken again.
                      Best regards,
                      Toragan
                      Last edited by Toragan; 16th May 2019, 12:41 AM.

                      Comment


                      • #13
                        Hello,

                        I think I have found what is causing the issue. Upon deleting the 'string' widget on Form8, the sliding list works just fine.

                        I have attached a modified version of your project for you to test.
                        I do not have any data inputs, so I commented the initialization for the Modbus to run the whole project.

                        Also, I added some lines of code on Form14 so you can test the program continuously.

                        Best Regards,
                        Kevin
                        Attached Files

                        Comment


                        • #14
                          Hello,

                          The attached file is exactly what you had run on your screen ?
                          I tried to delete the "string" widget on Form8 but I have no change.

                          Comment


                          • #15
                            Hello,

                            I tried to delete the "string" widget on Form8 but I have no change.
                            Did you reload the graphics file?

                            Can you try again this attached file and let me know. Thanks

                            Best Regards,
                            Kevin
                            Attached Files

                            Comment

                            Working...
                            X