summaryrefslogtreecommitdiff
path: root/src/util/input.cpp
blob: 284ebfe29b89b382eb45e26ba2df3d09218227fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "input.h"
#include "SDL_events.h"

INPUT_DATA input;

void input_on_event( SDL_Event* e ) {
  input.on_input.each( fn( ON_INPUT_FN* func ) { (*func)( e ); } );

  switch( e->type ) {
    case SDL_MOUSEBUTTONDOWN: {
      switch( e->button.button ) {
        case 1: input.mouse.left = 1; break;
        case 2: input.mouse.middle = 1; break;
        case 3: input.mouse.right = 1; break;
      }
    } break;
    case SDL_MOUSEBUTTONUP: {
      switch( e->button.button ) {
        case 1: input.mouse.left = 0; break;
        case 2: input.mouse.middle = 0; break;
        case 3: input.mouse.right = 0; break;
      }
    } break;
    case SDL_MOUSEWHEEL: {
      input.mouse.wheel = e->wheel.y;
    } break;
    case SDL_MOUSEMOTION: {
      input.mouse.pos.x = (F32)e->motion.x;
      input.mouse.pos.y = (F32)e->motion.y;
      input.mouse.pos_delta.x += (F32)e->motion.xrel * input.mpitch;
      input.mouse.pos_delta.y += (F32)e->motion.yrel * input.myaw;
    } break;
    case SDL_KEYDOWN: {
      input.keys[e->key.keysym.sym & 0xff] = 1;
    } break;
    case SDL_KEYUP: {
      input.keys[e->key.keysym.sym & 0xff] = 0;
    } break;
  }
}

extern void input_reset_mouse_accumulator() {
  input.mouse.pos_delta.x = 0;
  input.mouse.pos_delta.y = 0;
}

void input_on_mouse( I32 type, I32 x, I32 y ) {
  if( type == MOUSEEV_MOVE ) {
    input.mouse.pos.x = (F32)x;
    input.mouse.pos.y = (F32)y;
  }

  if( type == MOUSEEV_DOWN ) {
    I32 button = x;
    if( button == MOUSE_LEFT )
      input.mouse.left = 1;
    if( button == MOUSE_RIGHT )
      input.mouse.right = 1;
    if( button == MOUSE_MIDDLE )
      input.mouse.middle = 1;
    if( button == MOUSE_WHEEL )
      input.mouse.wheel = -1;
  }

  if( type == MOUSEEV_UP ) {
    I32 button = x;
    if( button == MOUSE_LEFT )
      input.mouse.left = 0;
    if( button == MOUSE_RIGHT )
      input.mouse.right = 0;
    if( button == MOUSE_MIDDLE )
      input.mouse.middle = 0;
    if( button == MOUSE_WHEEL )
      input.mouse.wheel = 1;
  }
}

void input_frame_end() {
  input.mouse.wheel = 0;
}

U8 input_is_key_down( U32 key ) {
  return input.keys[key];
}

void input_capture_mouse( bool capture ) {
  input_reset_mouse_accumulator();
  input.mouselock = capture;
  SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE );
}