hedgewars/uCursor.pas
changeset 12793 575c0de98505
parent 12660 d3fb69e31165
child 12831 1fbc0d5a82d0
equal deleted inserted replaced
12792:8fa21750470f 12793:575c0de98505
     4 
     4 
     5 procedure init;
     5 procedure init;
     6 procedure resetPosition;
     6 procedure resetPosition;
     7 procedure updatePosition;
     7 procedure updatePosition;
     8 procedure handlePositionUpdate(x, y: LongInt);
     8 procedure handlePositionUpdate(x, y: LongInt);
       
     9 procedure setSystemCursor(enabled: boolean);
     9 
    10 
    10 implementation
    11 implementation
    11 
    12 
    12 uses SDLh, uVariables;
    13 uses SDLh, uVariables;
    13 
    14 
    16     resetPosition();
    17     resetPosition();
    17 end;
    18 end;
    18 
    19 
    19 procedure resetPosition;
    20 procedure resetPosition;
    20 begin
    21 begin
    21     // Move curser by 1px in case it's already centered.
       
    22     // The game camera in the Alpha for 0.9.23 screwed up if
       
    23     // the game started with the mouse already being centered.
       
    24     // This fixes it, but we might have overlooked a related
       
    25     // bug somewhere else.
       
    26     // No big deal since this function is (so far) only called once.
       
    27     SDL_WarpMouse((cScreenWidth div 2) + 1, cScreenHeight div 2);
       
    28     SDL_WarpMouse(cScreenWidth div 2, cScreenHeight div 2);
    22     SDL_WarpMouse(cScreenWidth div 2, cScreenHeight div 2);
    29 end;
    23 end;
    30 
    24 
    31 procedure updatePosition;
    25 procedure updatePosition;
    32 var x, y: LongInt;
    26 var x, y: LongInt;
    33 begin
    27 begin
    34     SDL_GetMouseState(@x, @y);
    28     SDL_GetRelativeMouseState(@x, @y);
    35 
    29 
    36     if(x <> cScreenWidth div 2) or (y <> cScreenHeight div 2) then
    30     if(x <> 0) or (y <> 0) then
    37     begin
    31         handlePositionUpdate(x, y);
    38         handlePositionUpdate(x - cScreenWidth div 2, y - cScreenHeight div 2);
       
    39 
       
    40         if cHasFocus then
       
    41             SDL_WarpMouse(cScreenWidth div 2, cScreenHeight div 2);
       
    42     end
       
    43 end;
    32 end;
    44 
    33 
    45 procedure handlePositionUpdate(x, y: LongInt);
    34 procedure handlePositionUpdate(x, y: LongInt);
    46 begin
    35 begin
    47     CursorPoint.X:= CursorPoint.X + x;
    36     CursorPoint.X:= CursorPoint.X + x;
    48     CursorPoint.Y:= CursorPoint.Y - y;
    37     CursorPoint.Y:= CursorPoint.Y - y;
    49 end;
    38 end;
    50 
    39 
       
    40 procedure setSystemCursor(enabled: boolean);
       
    41 begin
       
    42     if enabled then
       
    43         begin
       
    44         SDL_SetRelativeMouseMode(false);
       
    45         if cHasFocus then
       
    46             resetPosition();
       
    47         SDL_ShowCursor(1);
       
    48         end
       
    49     else
       
    50         begin
       
    51         SDL_ShowCursor(0);
       
    52         SDL_GetRelativeMouseState(nil, nil);
       
    53         SDL_SetRelativeMouseMode(true);
       
    54         end;
       
    55 end;
       
    56 
    51 end.
    57 end.