File: KeyMap.cpp

package info (click to toggle)
hedgewars 1.0.2-13
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,164 kB
  • sloc: pascal: 54,829; cpp: 27,221; ansic: 22,809; java: 8,210; haskell: 6,797; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 32
file content (103 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Hedgewars, a free turn based strategy game
 * Copyright (c) 2004-2019 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include <QStringList>
#include "KeyMap.h"
#include "SDL.h"

KeyMap & KeyMap::instance()
{
    static KeyMap instance;
    instance.getKeyMap();
    return instance;
}

bool KeyMap::getKeyMap()
{
    if (keyMapGenerated)
        return true;
    QFile keyFile(":keys.csv");
    if (!keyFile.open(QIODevice::ReadOnly))
    {
        qWarning("ERROR: keys.csv could not be opened!");
        return false;
    }
    QString keyString = keyFile.readAll();
    QStringList cells = QStringList() << QString("") << QString("");
    QChar currChar;
    bool isInQuote = false;
    int cell = 0;
    int charInCell = 0;
    QString scancode = "";
    QString keyname = "";
    for(long long int i = 0; i < keyString.length(); i++)
    {
        currChar = keyString.at(i);
        if (currChar == '\"') {
            isInQuote = !isInQuote;
        }
        if (currChar == ',' && !isInQuote) {
            cell++;
            continue;
        }
        if (currChar == '\n') {
            mapOfKeynames[(SDL_Scancode) scancode.toInt()] = keyname;
            mapOfScancodes[keyname] = (SDL_Scancode) scancode.toInt();
            if ((SDL_Scancode) scancode.toInt() == SDL_SCANCODE_UNKNOWN)
                continue;
            cell = 0;
            scancode = "";
            keyname = "";
            continue;
        }
        if (cell == 0 && currChar != '\"') {
            scancode += currChar;
        } else if (cell == 1 && currChar != '\"') {
            keyname += currChar;
        }
        charInCell++;
    }
    keyMapGenerated = true;
    keyFile.close();
    return true;
}

SDL_Scancode KeyMap::getScancodeFromKeyname(QString keyname)
{
    if (mapOfScancodes.contains(keyname))
        return mapOfScancodes[keyname];
    else
        return SDL_SCANCODE_UNKNOWN;
}

QString KeyMap::getKeynameFromScancode(int scancode)
{
    if (mapOfKeynames.contains((SDL_Scancode) scancode))
        if ((SDL_Scancode) scancode == SDL_SCANCODE_UNKNOWN)
            return QString("none");
        else
            return mapOfKeynames[(SDL_Scancode) scancode];
    else
        return QString("");
}

QString KeyMap::getKeynameFromScancodeConverted(int scancode)
{
    SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode);
    return SDL_GetKeyName(keycode);
}