summaryrefslogtreecommitdiff
path: root/src/util/input.cpp
blob: 4a786aad9ce56bef79941f03f9eaae6c69abb5e4 (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
#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;
    } 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;
  }
}


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;
}