GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/touch/touchactions.cpp Lines: 3 40 7.5 %
Date: 2017-11-29 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "input/touch/touchactions.h"
22
23
#include "input/inputmanager.h"
24
#include "input/mouseinput.h"
25
26
#include "input/touch/touchmanager.h"
27
28
#include "debug.h"
29
30
bool padClicked(false);
31
int halfJoyPad = 50;
32
33
#define impHandler(name) void name(const MouseInput &mouseInput)
34
#define impHandler0(name) void name(const MouseInput &mouseInput A_UNUSED)
35
36
152
void setHalfJoyPad(const int s)
37
{
38
152
    halfJoyPad = s;
39
152
}
40
41
static void moveChar(int x, int y)
42
{
43
    static const int lim1 = 10;
44
    static const int diff = 20;
45
46
    // set center at (0,0)
47
    x -= halfJoyPad;
48
    y -= halfJoyPad;
49
50
    // some magic for checking at what sector was click
51
    if (abs(x) < lim1)
52
        x = 0;
53
54
    if (abs(y) < lim1)
55
        y = 0;
56
57
    const int x2 = abs(x);
58
    const int y2 = abs(y);
59
    if (x2 > y2)
60
    {
61
        if ((y2 != 0) && x2 * 10 / y2 > diff)
62
            y = 0;
63
    }
64
    else
65
    {
66
        if ((x2 != 0) && y2 * 10 / x2 > diff)
67
            x = 0;
68
    }
69
70
    // detecting direction
71
    if (x > 0)
72
    {
73
        touchManager.setActionActive(InputAction::MOVE_LEFT, false);
74
        touchManager.setActionActive(InputAction::MOVE_RIGHT, true);
75
    }
76
    else if (x < 0)
77
    {
78
        touchManager.setActionActive(InputAction::MOVE_LEFT, true);
79
        touchManager.setActionActive(InputAction::MOVE_RIGHT, false);
80
    }
81
    else
82
    {
83
        touchManager.setActionActive(InputAction::MOVE_LEFT, false);
84
        touchManager.setActionActive(InputAction::MOVE_RIGHT, false);
85
    }
86
    if (y > 0)
87
    {
88
        touchManager.setActionActive(InputAction::MOVE_DOWN, true);
89
        touchManager.setActionActive(InputAction::MOVE_UP, false);
90
    }
91
    else if (y < 0)
92
    {
93
        touchManager.setActionActive(InputAction::MOVE_DOWN, false);
94
        touchManager.setActionActive(InputAction::MOVE_UP, true);
95
    }
96
    else
97
    {
98
        touchManager.setActionActive(InputAction::MOVE_DOWN, false);
99
        touchManager.setActionActive(InputAction::MOVE_UP, false);
100
    }
101
    inputManager.updateConditionMask(true);
102
}
103
104
impHandler(padClick)
105
{
106
    moveChar(mouseInput.getX(), mouseInput.getY());
107
    padClicked = true;
108
}
109
110
impHandler(padEvents)
111
{
112
    if (mouseInput.getType() == MouseEventType::MOVED)
113
    {
114
        if (padClicked)
115
            moveChar(mouseInput.getX(), mouseInput.getY());
116
    }
117
}
118
119
impHandler0(padOut)
120
{
121
    padClicked = false;
122
    moveChar(halfJoyPad, halfJoyPad);
123
}
124
125
impHandler0(padUp)
126
{
127
    padClicked = false;
128
    moveChar(halfJoyPad, halfJoyPad);
129
}