Esempio n. 1
0
int
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
                    float x, float y, float pressure)
{
    SDL_Touch *touch;
    SDL_Finger *finger;
    int posted;
    float xrel, yrel, prel;

    touch = SDL_GetTouch(id);
    if (!touch) {
        return -1;
    }

    finger = SDL_GetFinger(touch,fingerid);
    if (!finger) {
        return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
    }

    xrel = x - finger->x;
    yrel = y - finger->y;
    prel = pressure - finger->pressure;

    /* Drop events that don't change state */
    if (!xrel && !yrel && !prel) {
#if 0
        printf("Touch event didn't change state - dropped!\n");
#endif
        return 0;
    }

    /* Update internal touch coordinates */
    finger->x = x;
    finger->y = y;
    finger->pressure = pressure;

    /* Post the event, if desired */
    posted = 0;
    if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
        SDL_Event event;
        event.tfinger.type = SDL_FINGERMOTION;
        event.tfinger.touchId = id;
        event.tfinger.fingerId = fingerid;
        event.tfinger.x = x;
        event.tfinger.y = y;
        event.tfinger.dx = xrel;
        event.tfinger.dy = yrel;
        event.tfinger.pressure = pressure;
        posted = (SDL_PushEvent(&event) > 0);
    }
    return posted;
}
Esempio n. 2
0
int
SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
{
    int index = SDL_GetFingerIndexId(touch,fingerid);
    SDL_Finger* finger = SDL_GetFinger(touch,fingerid);

    if (!finger) {
        return -1;
    }

    SDL_free(finger);
    touch->num_fingers--;
    touch->fingers[index] = touch->fingers[touch->num_fingers];
    return 0;
}
Esempio n. 3
0
int
SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
              SDL_bool down, float x, float y, float pressure)
{
    int posted;
    SDL_Finger *finger;

    SDL_Touch* touch = SDL_GetTouch(id);
    if (!touch) {
        return -1;
    }

    finger = SDL_GetFinger(touch, fingerid);
    if (down) {
        if (finger) {
            /* This finger is already down */
            return 0;
        }

        if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
            return 0;
        }

        posted = 0;
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
            SDL_Event event;
            event.tfinger.type = SDL_FINGERDOWN;
            event.tfinger.touchId = id;
            event.tfinger.fingerId = fingerid;
            event.tfinger.x = x;
            event.tfinger.y = y;
            event.tfinger.dx = 0;
            event.tfinger.dy = 0;
            event.tfinger.pressure = pressure;
            posted = (SDL_PushEvent(&event) > 0);
        }
    } else {
        if (!finger) {
            /* This finger is already up */
            return 0;
        }

        posted = 0;
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
            SDL_Event event;
            event.tfinger.type = SDL_FINGERUP;
            event.tfinger.touchId =  id;
            event.tfinger.fingerId = fingerid;
            /* I don't trust the coordinates passed on fingerUp */
            event.tfinger.x = finger->x;
            event.tfinger.y = finger->y;
            event.tfinger.dx = 0;
            event.tfinger.dy = 0;
            event.tfinger.pressure = pressure;
            posted = (SDL_PushEvent(&event) > 0);
        }

        SDL_DelFinger(touch, fingerid);
    }
    return posted;
}
Esempio n. 4
0
int
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, 
                    float xin, float yin, float pressurein)
{
    SDL_Touch *touch;
    SDL_Finger *finger;
    int posted;
    Sint16 xrel, yrel;
    Uint16 x;
    Uint16 y;
    Uint16 pressure;
    
    touch = SDL_GetTouch(id);
    if (!touch) {
      return SDL_TouchNotFoundError(id);
    }

    //scale to Integer coordinates
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
    pressure = (Uint16)((pressurein+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
    if(touch->flush_motion) {
        return 0;
    }
    
    finger = SDL_GetFinger(touch,fingerid);
    if(finger == NULL || !finger->down) {
        return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);        
    } else {
        /* the relative motion is calculated regarding the last position */
        if (relative) {
            xrel = x;
            yrel = y;
            x = (finger->last_x + x);
            y = (finger->last_y + y);
        } else {
            if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
            if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
            if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
            xrel = x - finger->last_x;
            yrel = y - finger->last_y;
            //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
        }
        
        /* Drop events that don't change state */
        if (!xrel && !yrel) {
#if 0
            printf("Touch event didn't change state - dropped!\n");
#endif
            return 0;
        }
        
        /* Update internal touch coordinates */
        
        finger->x = x;
        finger->y = y;
        
        /*Should scale to window? Normalize? Maintain Aspect?*/
        //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
        
        /* make sure that the pointers find themselves inside the windows */
        /* only check if touch->xmax is set ! */
        /*
          if (x_max && touch->x > x_max) {
          touch->x = x_max;
          } else if (touch->x < 0) {
          touch->x = 0;
          }
          
          if (y_max && touch->y > y_max) {
          touch->y = y_max;
          } else if (touch->y < 0) {
          touch->y = 0;
          }
        */
        finger->xdelta = xrel;
        finger->ydelta = yrel;
        finger->pressure = pressure;
        
        
        
        /* Post the event, if desired */
        posted = 0;
        if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
            SDL_Event event;
            event.tfinger.type = SDL_FINGERMOTION;
            event.tfinger.touchId = id;
            event.tfinger.fingerId = fingerid;
            event.tfinger.x = x;
            event.tfinger.y = y;
            event.tfinger.dx = xrel;
            event.tfinger.dy = yrel;            
                
            event.tfinger.pressure = pressure;
            event.tfinger.state = touch->buttonstate;
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
            posted = (SDL_PushEvent(&event) > 0);
        }
        finger->last_x = finger->x;
        finger->last_y = finger->y;
        finger->last_pressure = finger->pressure;
        return posted;
    }
}
Esempio n. 5
0
int
SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, 
                   float xin, float yin, float pressurein)
{
    int posted;
        Uint16 x;
        Uint16 y;
        Uint16 pressure;
        SDL_Finger *finger;

    SDL_Touch* touch = SDL_GetTouch(id);

    if(!touch) {
      return SDL_TouchNotFoundError(id);
    }

    
    //scale to Integer coordinates
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
    pressure = (Uint16)((pressurein+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
    
    finger = SDL_GetFinger(touch,fingerid);
    if(down) {
        if(finger == NULL) {
            SDL_Finger nf;
            nf.id = fingerid;
            nf.x = x;
            nf.y = y;
            nf.pressure = pressure;
            nf.xdelta = 0;
            nf.ydelta = 0;
            nf.last_x = x;
            nf.last_y = y;
            nf.last_pressure = pressure;
            nf.down = SDL_FALSE;
            if(SDL_AddFinger(touch,&nf) < 0) return 0;
            finger = SDL_GetFinger(touch,fingerid);
        }
        else if(finger->down) return 0;
        if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
        posted = 0;
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
            SDL_Event event;
            event.tfinger.type = SDL_FINGERDOWN;
            event.tfinger.touchId = id;
            event.tfinger.x = x;
            event.tfinger.y = y;
            event.tfinger.dx = 0;
            event.tfinger.dy = 0;
            event.tfinger.pressure = pressure;
            event.tfinger.state = touch->buttonstate;
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
            event.tfinger.fingerId = fingerid;
            posted = (SDL_PushEvent(&event) > 0);
        }
        if(posted) finger->down = SDL_TRUE;
        return posted;
    }
    else {
        if(finger == NULL) {
            SDL_SetError("Finger not found.");
            return 0;
        }      
        posted = 0;
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
            SDL_Event event;
            event.tfinger.type = SDL_FINGERUP;
            event.tfinger.touchId =  id;
            event.tfinger.state = touch->buttonstate;
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
            event.tfinger.fingerId = fingerid;
            //I don't trust the coordinates passed on fingerUp
            event.tfinger.x = finger->x; 
            event.tfinger.y = finger->y;
            event.tfinger.dx = 0;
            event.tfinger.dy = 0;
            event.tfinger.pressure = pressure;

            if(SDL_DelFinger(touch,fingerid) < 0) return 0;
            posted = (SDL_PushEvent(&event) > 0);
        }        
        return posted;
    }
}