summaryrefslogtreecommitdiffstats
path: root/src/ui/key.cpp
blob: fd9a77fb76e9baf37cef4c4b8dbae84d13e150a2 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see http://gnu.org/licenses
 */


#include "ui/key.h"

#include "common/global.h"
#include "common/stringutils.h"

#include <cstring>

namespace Ui {


static void GetKeyName(std::string& name, unsigned int key)
{
    if (!GetResource(RES_KEY, key, name))
    {
        name = StrUtils::Format("Code %d", key);
    }
}


CKey::CKey() : CControl()
{
    m_catch = false;

    m_robotMain = CRobotMain::GetInstancePointer();
}

CKey::~CKey()
{
    m_robotMain = nullptr;
}

bool CKey::Create(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
{
    if (eventMsg == EVENT_NULL)
        eventMsg = GetUniqueEventType();

    CControl::Create(pos, dim, icon, eventMsg);

    std::string name;
    GetResource(RES_EVENT, eventMsg, name);
    SetName(name);

    return true;
}

bool CKey::EventProcess(const Event &event)
{
    if (m_state & STATE_DEAD)
        return true;

    CControl::EventProcess(event);

    if (event.type == EVENT_MOUSE_BUTTON_DOWN)
    {
        if (event.mouseButton.button == MOUSE_BUTTON_LEFT) // left
            m_catch = Detect(event.mousePos);
    }

    if (event.type == EVENT_KEY_DOWN && m_catch)
    {
        m_catch = false;

        if (TestKey(event.key.key)) // impossible ?
        {
            m_sound->Play(SOUND_TZOING);
        }
        else
        {
            if (event.key.key == m_binding.primary || event.key.key == m_binding.secondary)
            {
                m_binding.secondary = KEY_INVALID;
                m_binding.primary = event.key.key;
            }
            else
            {
                m_binding.secondary = m_binding.primary;
                m_binding.primary = event.key.key;
            }
            m_sound->Play(SOUND_CLICK);

            Event newEvent = event;
            newEvent.type = m_eventType;
            m_event->AddEvent(newEvent);
        }
        return false;
    }

    return true;
}

bool CKey::TestKey(unsigned int key)
{
    if (key == KEY(PAUSE) || key == KEY(PRINT)) return true;  // blocked key

    for (int i = 0; i < INPUT_SLOT_MAX; i++)
    {
        InputSlot slot = static_cast<InputSlot>(i);
        InputBinding b = m_robotMain->GetInputBinding(slot);
        if (key == b.primary || key == b.secondary)
            m_robotMain->SetInputBinding(slot, InputBinding());  // nothing!

        if (b.primary == KEY_INVALID) // first free option?
            m_robotMain->SetInputBinding(slot, InputBinding(b.secondary, b.primary));  // shift
    }

    return false;  // not used
}

void CKey::Draw()
{
    if ((m_state & STATE_VISIBLE) == 0)
        return;

    Math::Point iDim = m_dim;
    m_dim.x = 200.0f/640.0f;

    if (m_state & STATE_SHADOW)
        DrawShadow(m_pos, m_dim);


    m_engine->SetTexture("textures/interface/button1.png");
    m_engine->SetState(Gfx::ENG_RSTATE_NORMAL); // was D3DSTATENORMAL

    float zoomExt = 1.00f;
    float zoomInt = 0.95f;

    int icon = 2;
    if (m_binding.primary == KEY_INVALID && m_binding.secondary == KEY_INVALID)  // no shortcut?
        icon = 3;

    if (m_state & STATE_DEFAULT)
    {
        DrawPart(23, 1.3f, 0.0f);

        zoomExt *= 1.15f;
        zoomInt *= 1.15f;
    }

    if (m_state & STATE_HILIGHT)
        icon = 1;

    if (m_state & STATE_CHECK)
        icon = 0;

    if (m_state & STATE_PRESS)
    {
        icon = 3;
        zoomInt *= 0.9f;
    }

    if ((m_state & STATE_ENABLE) == 0)
        icon = 7;

    if (m_state & STATE_DEAD)
        icon = 17;

    if (m_catch)
        icon = 23;

    DrawPart(icon, zoomExt, 8.0f / 256.0f);  // draws the button

    float h = m_engine->GetText()->GetHeight(m_fontType, m_fontSize) / 2.0f;

    std::string keyName;
    GetKeyName(keyName, m_binding.primary);
    if (m_binding.secondary != KEY_INVALID)
    {
        std::string orText;
        GetResource(RES_TEXT, RT_KEY_OR, orText);
        keyName.append(orText);

        std::string secondaryKeyName;
        GetKeyName(secondaryKeyName, m_binding.secondary);
        keyName.append(secondaryKeyName);
    }

    Math::Point pos;
    pos.x = m_pos.x + m_dim.x * 0.5f;
    pos.y = m_pos.y + m_dim.y * 0.5f;
    pos.y -= h;
    m_engine->GetText()->DrawText(keyName, m_fontType, m_fontSize, pos, m_dim.x, Gfx::TEXT_ALIGN_CENTER, 0);

    m_dim = iDim;

    if (m_state & STATE_DEAD)
        return;

    // Draws the name.
    pos.x = m_pos.x + (214.0f / 640.0f);
    pos.y = m_pos.y + m_dim.y * 0.5f;
    pos.y -= h;
    m_engine->GetText()->DrawText(m_name, m_fontType, m_fontSize, pos, m_dim.x, Gfx::TEXT_ALIGN_LEFT, 0);
}

void CKey::SetBinding(InputBinding b)
{
    m_binding = b;
}

InputBinding CKey::GetBinding()
{
    return m_binding;
}


} // namespace Ui