source: trunk/src/input/input.cpp @ 1710

Last change on this file since 1710 was 1710, checked in by sam, 8 years ago

input: allow to easily track more than one input stick.

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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
17#if defined USE_SDL
18#   if defined HAVE_SDL_SDL_H
19#      include <SDL/SDL.h>
20#   else
21#      include <SDL.h>
22#   endif
23#endif
24
25#include "core.h"
26
27namespace lol
28{
29
30/*
31 * Input implementation class
32 */
33
34static class InputData
35{
36    friend class Input;
37
38public:
39    InputData()
40      : mouse(-1),
41        buttons(0),
42        nentities(0),
43        lastfocus(0)
44    { }
45
46private:
47    ivec2 mouse;
48    ivec3 buttons;
49
50    static int const MAX_ENTITIES = 100;
51    WorldEntity *entities[MAX_ENTITIES];
52    int nentities;
53    WorldEntity *lastfocus;
54
55    Array<Stick *> m_sticks;
56}
57inputdata;
58
59static InputData * const data = &inputdata;
60
61/*
62 * Public Input class
63 */
64
65#if 0
66vec2 Input::GetAxis(int axis)
67{
68    vec2 ret;
69
70#if defined USE_SDL
71    /* Simulate a joystick using the keyboard. This SDL call is free. */
72#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
73    Uint8 *keystate = SDL_GetKeyboardState(NULL);
74#else
75    Uint8 *keystate = SDL_GetKeyState(NULL);
76#endif
77    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
78    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
79    ret.x += left;
80    ret.y += up;
81    if (left && up)
82        ret = ret * sqrtf(0.5f);
83#else
84    ret = vec2(0, 0);
85#endif
86
87    return ret;
88}
89#endif
90
91ivec2 Input::GetMousePos()
92{
93    return data->mouse;
94}
95
96ivec3 Input::GetMouseButtons()
97{
98    return data->buttons;
99}
100
101//BH : Added this, is a v0.1 Alpha version.
102int Input::GetButtonState(int button)
103{
104#if defined USE_SDL
105#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
106    Uint8 *keystate = SDL_GetKeyboardState(NULL);
107#else
108    Uint8 *keystate = SDL_GetKeyState(NULL);
109#endif
110    return keystate[button];
111#else
112    return 0;
113#endif
114}
115
116void Input::TrackMouse(WorldEntity *e)
117{
118    if (data->nentities >= InputData::MAX_ENTITIES)
119        return;
120    data->entities[data->nentities] = e;
121    data->nentities++;
122}
123
124void Input::UntrackMouse(WorldEntity *e)
125{
126    for (int n = 0; n < data->nentities; n++)
127    {
128        if (data->entities[n] != e)
129            continue;
130
131        data->entities[n] = data->entities[data->nentities - 1];
132        data->nentities--;
133        n--;
134    }
135}
136
137void Input::SetMousePos(ivec2 coord)
138{
139    data->mouse = coord;
140
141    WorldEntity *top = NULL;
142
143    for (int n = 0; n < data->nentities; n++)
144    {
145        if (coord.x < data->entities[n]->m_bbox[0].x
146             || coord.x >= data->entities[n]->m_bbox[1].x
147             || coord.y < data->entities[n]->m_bbox[0].y
148             || coord.y >= data->entities[n]->m_bbox[1].y)
149            continue;
150
151        if (!top || top->m_bbox[1].z < data->entities[n]->m_bbox[1].z)
152            top = data->entities[n];
153    }
154
155    for (int n = 0; n < data->nentities; n++)
156    {
157        if (data->entities[n] == top)
158        {
159            data->entities[n]->m_mousepos = coord - (ivec2)top->m_bbox[0].xy;
160            if (top != data->lastfocus)
161                data->entities[n]->m_pressed = data->buttons;
162            else
163                data->entities[n]->m_clicked = ivec3(0);
164        }
165        else
166        {
167            data->entities[n]->m_mousepos = ivec2(-1);
168            /* FIXME */
169            data->entities[n]->m_released = ivec3(0);
170            data->entities[n]->m_pressed = ivec3(0);
171            data->entities[n]->m_clicked = ivec3(0);
172        }
173    }
174
175    data->lastfocus = top;
176}
177
178void Input::SetMouseButton(int index)
179{
180    data->buttons[index] = 1;
181
182    if (data->lastfocus)
183    {
184        if (!data->lastfocus->m_pressed[index])
185            data->lastfocus->m_clicked[index] = 1;
186        data->lastfocus->m_pressed[index] = 1;
187        data->lastfocus->m_released[index] = 0;
188    }
189}
190
191void Input::UnsetMouseButton(int index)
192{
193    data->buttons[index] = 0;
194
195    if (data->lastfocus)
196    {
197        if (data->lastfocus->m_pressed[index])
198            data->lastfocus->m_released[index] = 1;
199        data->lastfocus->m_pressed[index] = 0;
200        data->lastfocus->m_clicked[index] = 0;
201    }
202}
203
204Stick *Input::CreateStick()
205{
206    Stick *stick = new Stick();
207    Ticker::Ref(stick);
208    data->m_sticks.Push(stick);
209    return stick;
210}
211
212void Input::DestroyStick(Stick *stick)
213{
214    for (int i = 0; i < data->m_sticks.Count(); i++)
215        if (data->m_sticks[i] == stick)
216            data->m_sticks.Remove(i);
217    Ticker::Unref(stick);
218}
219
220Stick *Input::TrackStick(int desired)
221{
222    /* FIXME: add the possibility to choose amongst sticks */
223    if (desired >= data->m_sticks.Count())
224        return NULL;
225    Ticker::Ref(data->m_sticks[desired]);
226    return data->m_sticks[desired];
227}
228
229void Input::UntrackStick(Stick *stick)
230{
231    Ticker::Unref(stick);
232}
233
234} /* namespace lol */
235
Note: See TracBrowser for help on using the repository browser.