source: trunk/src/input.cpp @ 866

Last change on this file since 866 was 866, checked in by sam, 12 years ago

core: more vec?i -> ?veci renames.

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cmath>
17
18#if defined USE_SDL
19#   include <SDL.h>
20#endif
21
22#include "core.h"
23
24namespace lol
25{
26
27/*
28 * Input implementation class
29 */
30
31static class InputData
32{
33    friend class Input;
34
35public:
36    InputData()
37      : mouse(-1),
38        buttons(0),
39        nentities(0),
40        lastfocus(0)
41    { }
42
43private:
44    ivec2 mouse;
45    ivec3 buttons;
46
47    static int const MAX_ENTITIES = 100;
48    WorldEntity *entities[MAX_ENTITIES];
49    int nentities;
50    WorldEntity *lastfocus;
51}
52inputdata;
53
54static InputData * const data = &inputdata;
55
56/*
57 * Public Input class
58 */
59
60vec2 Input::GetAxis(int axis)
61{
62    vec2 ret;
63
64#if defined USE_SDL
65    /* Simulate a joystick using the keyboard. This SDL call is free. */
66    Uint8 *keystate = SDL_GetKeyState(NULL);
67    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
68    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
69    ret.x += left;
70    ret.y += up;
71    if (left && up)
72        ret = ret * sqrtf(0.5f);
73#else
74    ret = 0;
75#endif
76
77    return ret;
78}
79
80ivec2 Input::GetMousePos()
81{
82    return data->mouse;
83}
84
85ivec3 Input::GetMouseButtons()
86{
87    return data->buttons;
88}
89
90void Input::TrackMouse(WorldEntity *e)
91{
92    if (data->nentities >= InputData::MAX_ENTITIES)
93        return;
94    data->entities[data->nentities] = e;
95    data->nentities++;
96}
97
98void Input::UntrackMouse(WorldEntity *e)
99{
100    for (int n = 0; n < data->nentities; n++)
101    {
102        if (data->entities[n] != e)
103            continue;
104
105        data->entities[n] = data->entities[data->nentities - 1];
106        data->nentities--;
107        n--;
108    }
109}
110
111void Input::SetMousePos(ivec2 coord)
112{
113    data->mouse = coord;
114
115    WorldEntity *top = NULL;
116
117    for (int n = 0; n < data->nentities; n++)
118    {
119        if (coord.x < data->entities[n]->bbox[0].x
120             || coord.x >= data->entities[n]->bbox[1].x
121             || coord.y < data->entities[n]->bbox[0].y
122             || coord.y >= data->entities[n]->bbox[1].y)
123            continue;
124
125        if (!top || top->bbox[1].z < data->entities[n]->bbox[1].z)
126            top = data->entities[n];
127    }
128
129    for (int n = 0; n < data->nentities; n++)
130    {
131        if (data->entities[n] == top)
132        {
133            data->entities[n]->mousepos = (ivec2)((ivec3)coord - top->bbox[0]);
134            if (top != data->lastfocus)
135                data->entities[n]->pressed = data->buttons;
136            else
137                data->entities[n]->clicked = 0;
138        }
139        else
140        {
141            data->entities[n]->mousepos = ivec2(-1);
142            /* FIXME */
143            data->entities[n]->released = 0;
144            data->entities[n]->pressed = 0;
145            data->entities[n]->clicked = 0;
146        }
147    }
148
149    data->lastfocus = top;
150}
151
152void Input::SetMouseButton(int index)
153{
154    data->buttons[index] = 1;
155
156    if (data->lastfocus)
157    {
158        if (!data->lastfocus->pressed[index])
159            data->lastfocus->clicked[index] = 1;
160        data->lastfocus->pressed[index] = 1;
161        data->lastfocus->released[index] = 0;
162    }
163}
164
165void Input::UnsetMouseButton(int index)
166{
167    data->buttons[index] = 0;
168
169    if (data->lastfocus)
170    {
171        if (data->lastfocus->pressed[index])
172            data->lastfocus->released[index] = 1;
173        data->lastfocus->pressed[index] = 0;
174        data->lastfocus->clicked[index] = 0;
175    }
176}
177
178} /* namespace lol */
179
Note: See TracBrowser for help on using the repository browser.