Post Reply 
Graphic cursor problem
01-09-2021, 09:20 PM
Post: #1
Graphic cursor problem
Hi all,

[/code]Currently I am making a graphics program (app) where a cursor in the shape of a black square is projected on one of the squares on the screen, such that the character in the square is visible in reverse video. With one touch on the screen, the cursor can be moved to another square of your choice. When the program starts, the cursor is placed on the first square. So the idea is that you can move the cursor to any other square on the screen, with a touch on the screen. Unfortunately it goes wrong there, if the screen is touched, the cursor is not moved, but you will see the screen of the app from which the program is derived.

Some explanation about the program is in order: All variables that appear in this program are all declared at the beginning of this program, but that declaration is not visible in the code presented here. The variables cx and cy contain the coordinates where the screen was touched. The content of variable 'cursor' determines whether the cursor is visible or not, and is initially 1. List L9 contains characters, and list L0 contains numbers and sometimes characters. G2 contains a .PNG file that is copied to G0, and on which some characters are projected on places predetermined by coordinates. The variable 'tcrd' contains coordinates for letters, and the variable 'mcrd' for numbers and sometimes letters. If possible, the touches on the screen are converted to an index, which then takes the correct coordinates from list 'mcrd' and has to place the cursor in the right place on the screen. Since the characters are placed in the center of a square as much as possible, the cursor needs some math to get it in the right place. Based on the code, what could possibly be wrong or what needs to be inserted in order for the program to work in the end? Sincerely, Karel.

Code:

    . . .
  tcrd:={{119,98},{81,137}};
  mcrd:={{102,118},{119,118},{102,135},{119,135}};
  RECT();
  BLIT_P(G0,0,0,320,240,G2,0,0,320,240,#FFFFFF,255);
    FOR I FROM 1 TO 2 DO
      TEXTOUT_P(LEFT(L9(I),1),G0,tcrd(I,1),tcrd(I,2),3,#0,13); 
    END;
    FOR I FROM 1 TO 4 DO
      TEXTOUT_P(L0(I),G0,mcrd(I,1),mcrd(I,2),4,#0);
    END;
    CASE
      IF (cx≥98) AND (cx≤113) THEN
        CASE
          IF (cy≥120) AND (cy≤135) THEN
          index:=1;
          END;
          IF (cy≥137) AND (cy≤152) THEN
          index:=3;
          END;
        END;
      END;
      IF (cx≥115) AND (cx≤130) THEN
        CASE
          IF (cy≥120) AND (cy≤135) THEN
          index:=2;
          END;
          IF (cy≥137) AND (cy≤152) THEN
          index:=4;
          END;
        END;
      END;
    END;
    IF cursor:=1 THEN
      dx:=mcrd(index,1); dy:=mcrd(index,2); 
      INVERT_P(G0,dx-4,dy+2,dx+11,dy+17);
    END;
    . . .

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
01-10-2021, 09:45 PM (This post was last modified: 01-10-2021 09:46 PM by Han.)
Post: #2
RE: Graphic cursor problem
Insert a DEBUG command right before the last IF-THEN-END block. This will allow you to enter your program in debug mode and examine the contents of the variables. This is the easiest way to track down bugs in a program.

Code:
DEBUG; // <--- insert this command here
    IF cursor:=1 THEN
      dx:=mcrd(index,1); dy:=mcrd(index,2); 
      INVERT_P(G0,dx-4,dy+2,dx+11,dy+17);
    END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
01-12-2021, 05:39 AM
Post: #3
RE: Graphic cursor problem
Dear Mr. Han,
During debugging, the program works properly. So it must be something else that the original image of the underlying app appears. The question now is what needs to be changed to ensure that this does not happen again. I could use some help with this, because there are a lot more people in this community who have a lot more experience programming the Prime, including you. Therefore, I would like to see suggestions on how to address this problem. Thanks in advance! Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
01-12-2021, 12:13 PM (This post was last modified: 01-12-2021 12:17 PM by C.Ret.)
Post: #4
RE: Graphic cursor problem
(01-09-2021 09:20 PM)cahlucas Wrote:  [...] Based on the code, what could possibly be wrong or what needs to be inserted in order for the program to work in the end? Sincerely, Karel.

I am not sure, but I see a potential problem in the way index is initialized. To ease my explanation, I copy your code simplifying the tests which appear to be correct.

Code:

 ...
   index:= 0   ; //    <---------------------   There to set the  value of index to indicate a "user touch out of any expected zone "
    CASE
      IF   "cx in touchzone_x1" THEN
        CASE
          IF "cy in touchzone_y1" THEN index:=1; END;
          IF "cy in Touchzone_y2" THEN index:=3; END;
        END;
      END;
      IF "cx in Touchzone_x2" THEN
        CASE
          IF "cy in Touchzone_y1" THEN index:=2; END;
          IF "cy in Touchzone_y2" THEN index:=4; END;
        END;
      END;
    END;
 ...

The variable index is correctly set only when the user touch’s in one of the four Touch-Zone as expected, respectively 1 2 3 or 4 depending of the zone.

But, what happen to index when the user miss any of the four zones ? I can not see where the variable index is set to a specific value (I may propose set to 0) to indicate a wrong touch !

And, more important, in the last portion of the code, no test is made to verify that index is correctly set and one of the four Touch Zone was actually touched :

Code:

    IF cursor:=1 AND  index<>0 THEN    // <--------------- "index don't indicate any missed touch zone"
      dx:=mcrd(index,1); dy:=mcrd(index,2); 
      INVERT_P(G0,dx-4,dy+2,dx+11,dy+17);
    END;
    . . .

I hope this observation help a bit...
Debugging on the HP Prime is sometime really hard, a source of stress and time consuming... (I just spend two hours realizing that the incorrect behavior of my CAS function was due to a capitalized local variable name, the calculator was using the global variable same value each time what ever were the specific samples I introduce ... How fool are we on ours doggy horses ?? )

Best Regards.
C.Ret
Find all posts by this user
Quote this message in a reply
01-13-2021, 02:27 AM
Post: #5
RE: Graphic cursor problem
(01-12-2021 05:39 AM)cahlucas Wrote:  Dear Mr. Han,
During debugging, the program works properly. So it must be something else that the original image of the underlying app appears. The question now is what needs to be changed to ensure that this does not happen again. I could use some help with this, because there are a lot more people in this community who have a lot more experience programming the Prime, including you. Therefore, I would like to see suggestions on how to address this problem. Thanks in advance! Sincerely, Karel.

A likely reason why it seems to work during debugging is that (perhaps) you are not accounting for "mouse-up" events. So when a "mouse-down" even occurs (i.e. your finger touches the screen) and the code follows into DEBUG mode, everything looks fine. But the DEBUG mode probably gets in the way of the "mouse-up" event (when you lift your finger from the screen). So the cursor is indeed being drawn correctly at the precise moment you touch the screen, but perhaps your code is not accounting for what happens when your finger lifts up from the screen. But without the code in its entirety (at least without the mouse/keyboard handling portion), it's hard to say precisely what the cause is. A generic mouse/keyboard handler would look something like:

Code:
EXPORT KMINPUT()
BEGIN
  LOCAL run:=1, key;

  // change run to 0 to exit the while loop
  key:=WAIT(-1);
  CASE
    IF TYPE(key)==6 THEN
      // we have touchscreen events
      CASE
        IF key(1)==0 THEN kmMouseDown(); END;
        IF key(1)==1 THEN kmMouseUp(); END;
        // ... more mouse events
        IF key(1)==7 THEN kmMouseLongClick(); END;

        kmMouseEvent(); // default handler
      END;
    END; // end of touch events

    // at this point, all touch events were handled due to the
    // TYPE(key)==6 check; so only key-presses are left
    // and if a touch event occurred, these tests below are
    // never reached

    IF key==0 THEN kmDoAppsKey(); END;
    // ... more key definitions here
    IF key==50 THEN kmDoPlusKey(); END;

    kmOtherEvents(); // handle all remaining undefined keys
  END;

END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
01-13-2021, 05:52 AM
Post: #6
RE: Graphic cursor problem
Dear Mr. Han,
I hereby add the part of the mouse code as it is at the moment, so that you can see what possibly needs to be corrected. All variables used are declared at the beginning of the program, but this part is not included in this code snippet. I hope this helps you to solve this problem. Sincerely, Karel.

Code:

  . . .
  input:=WAIT(−1);
  CASE
    IF TYPE(input)==6 THEN
      CASE
        IF input(1)==#0h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#1h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#2h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#3h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#4h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#5h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#6h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
        IF input(1)==#7h THEN
        cx:=B→R(input(2));
        cy:=B→R(input(3));
        END;
      END;
    END;
  END;
  . . .
[/code]

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
Post Reply 




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