GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/touch/touchactions.cpp Lines: 3 41 7.3 %
Date: 2021-03-17 Branches: 0 26 0.0 %

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