Skip to content

Commit 4a12d6a

Browse files
committed
Allow buttons to be used in a linked way
1 parent 96ebdcc commit 4a12d6a

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

src/input/multitouch_device.cpp

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ void MultitouchDevice::reset()
174174

175175
m_orientation = 0.0f;
176176
m_gyro_time = 0;
177+
m_affected_linked_buttons.clear();
177178
} // reset
178179

179180
// ----------------------------------------------------------------------------
@@ -249,11 +250,88 @@ void MultitouchDevice::updateDeviceState(unsigned int event_id)
249250
{
250251
assert(event_id < m_events.size());
251252

253+
auto inside_button = [](MultitouchButton* button,
254+
const MultitouchEvent& event)->bool
255+
{
256+
if (button == NULL)
257+
return false;
258+
return event.x >= button->x &&
259+
event.x <= button->x + button->width &&
260+
event.y >= button->y && event.y <= button->y + button->height;
261+
};
262+
auto is_linked_button = [](MultitouchButton* button)->bool
263+
{
264+
if (button == NULL)
265+
return false;
266+
return button->type >= BUTTON_FIRE &&
267+
button->type <= BUTTON_RESCUE;
268+
};
269+
270+
const MultitouchEvent& event = m_events[event_id];
271+
std::vector<MultitouchButton*> linked_buttons;
272+
for (MultitouchButton* button : m_buttons)
273+
{
274+
if (button == NULL || !is_linked_button(button))
275+
continue;
276+
linked_buttons.push_back(button);
277+
}
278+
279+
if (!event.touched)
280+
{
281+
m_affected_linked_buttons.erase(event.id);
282+
}
283+
else
284+
{
285+
for (MultitouchButton* button : linked_buttons)
286+
{
287+
if (inside_button(button, event))
288+
{
289+
auto& ret = m_affected_linked_buttons[event.id];
290+
if (!ret.empty() && ret.back() == button)
291+
continue;
292+
if (ret.size() >= 2)
293+
{
294+
if (ret[0] == button)
295+
ret.resize(1);
296+
else if (ret[ret.size() - 2] == button)
297+
ret.resize(ret.size() - 1);
298+
else
299+
ret.push_back(button);
300+
}
301+
else
302+
ret.push_back(button);
303+
break;
304+
}
305+
}
306+
}
307+
for (MultitouchButton* button : linked_buttons)
308+
{
309+
bool found = false;
310+
for (auto& p : m_affected_linked_buttons)
311+
{
312+
auto it = std::find(p.second.begin(), p.second.end(), button);
313+
if (it != p.second.end())
314+
{
315+
button->pressed = true;
316+
handleControls(button);
317+
found = true;
318+
break;
319+
}
320+
}
321+
if (!found)
322+
{
323+
button->pressed = false;
324+
handleControls(button);
325+
}
326+
}
327+
252328
MultitouchButton* pressed_button = NULL;
253329

254330
for (MultitouchButton* button : m_buttons)
255331
{
256-
if (button->pressed && button->event_id == event_id)
332+
if (is_linked_button(button))
333+
continue;
334+
if (button->pressed && button->event_id == event_id)
257335
{
258336
pressed_button = button;
259337
break;
@@ -262,16 +340,15 @@ void MultitouchDevice::updateDeviceState(unsigned int event_id)
262340

263341
for (MultitouchButton* button : m_buttons)
264342
{
343+
if (is_linked_button(button))
344+
continue;
265345
if (pressed_button != NULL && button != pressed_button)
266346
continue;
267-
347+
268348
bool update_controls = false;
269349
bool prev_button_state = button->pressed;
270-
MultitouchEvent event = m_events[event_id];
271350

272-
if (pressed_button != NULL ||
273-
(event.x >= button->x && event.x <= button->x + button->width &&
274-
event.y >= button->y && event.y <= button->y + button->height))
351+
if (pressed_button != NULL || inside_button(button, event))
275352
{
276353
button->pressed = event.touched;
277354
button->event_id = event_id;
@@ -295,7 +372,7 @@ void MultitouchDevice::updateDeviceState(unsigned int event_id)
295372
update_controls = true;
296373
}
297374
}
298-
375+
299376
if (button->type == MultitouchButtonType::BUTTON_STEERING ||
300377
button->type == MultitouchButtonType::BUTTON_UP_DOWN)
301378
{

src/input/multitouch_device.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define HEADER_MULTITOUCH_DEVICE_HPP
2121

2222
#include <array>
23+
#include <map>
2324
#include <vector>
2425

2526
#include "input/input_device.hpp"
@@ -99,6 +100,8 @@ class MultitouchDevice : public InputDevice
99100
void handleControls(MultitouchButton* button);
100101
bool isGameRunning();
101102

103+
std::map<unsigned, std::vector<MultitouchButton*> > m_affected_linked_buttons;
104+
102105
public:
103106
/** The array that contains data for all multitouch input events */
104107
std::array<MultitouchEvent, NUMBER_OF_MULTI_TOUCHES> m_events;

0 commit comments

Comments
 (0)