2014-10-14 13:11:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
|
|
2012-09-18 22:04:21 +00:00
|
|
|
|
#include "ui/key.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
#include "common/global.h"
|
2013-12-02 23:11:26 +00:00
|
|
|
|
#include "common/stringutils.h"
|
2012-10-06 22:46:46 +00:00
|
|
|
|
|
|
|
|
|
#include <cstring>
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-08-17 21:03:52 +00:00
|
|
|
|
namespace Ui {
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
|
CKey::CKey() : CControl()
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-10-06 22:46:46 +00:00
|
|
|
|
m_catch = false;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2014-12-11 18:01:57 +00:00
|
|
|
|
m_input = CInput::GetInstancePointer();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CKey::~CKey()
|
|
|
|
|
{
|
2014-12-11 18:01:57 +00:00
|
|
|
|
m_input = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
|
bool CKey::Create(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-08-18 19:04:51 +00:00
|
|
|
|
if (eventMsg == EVENT_NULL)
|
|
|
|
|
eventMsg = GetUniqueEventType();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
CControl::Create(pos, dim, icon, eventMsg);
|
2012-10-06 22:46:46 +00:00
|
|
|
|
|
2013-12-02 23:11:26 +00:00
|
|
|
|
std::string name;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
GetResource(RES_EVENT, eventMsg, name);
|
2013-12-02 23:11:26 +00:00
|
|
|
|
SetName(name);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CKey::EventProcess(const Event &event)
|
|
|
|
|
{
|
2012-08-17 20:43:07 +00:00
|
|
|
|
if (m_state & STATE_DEAD)
|
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
CControl::EventProcess(event);
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (event.type == EVENT_MOUSE_BUTTON_DOWN)
|
|
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
|
if (event.mouseButton.button == MOUSE_BUTTON_LEFT) // left
|
2012-10-06 22:46:46 +00:00
|
|
|
|
m_catch = Detect(event.mousePos);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (event.type == EVENT_KEY_DOWN && m_catch)
|
|
|
|
|
{
|
|
|
|
|
m_catch = false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (TestKey(event.key.key)) // impossible ?
|
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
|
m_sound->Play(SOUND_TZOING);
|
2012-10-06 22:46:46 +00:00
|
|
|
|
}
|
|
|
|
|
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;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
m_sound->Play(SOUND_CLICK);
|
|
|
|
|
|
|
|
|
|
Event newEvent = event;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
newEvent.type = m_eventType;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
m_event->AddEvent(newEvent);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
bool CKey::TestKey(unsigned int key)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2014-12-11 18:48:06 +00:00
|
|
|
|
if (key == KEY(PAUSE) || key == KEY(PRINT)) return true; // blocked key
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
for (int i = 0; i < INPUT_SLOT_MAX; i++)
|
|
|
|
|
{
|
|
|
|
|
InputSlot slot = static_cast<InputSlot>(i);
|
2014-12-11 18:01:57 +00:00
|
|
|
|
InputBinding b = m_input->GetInputBinding(slot);
|
|
|
|
|
if (key == b.primary)
|
|
|
|
|
m_input->SetInputBinding(slot, InputBinding(b.secondary, KEY_INVALID)); // nothing!
|
|
|
|
|
if (key == b.secondary)
|
|
|
|
|
m_input->SetInputBinding(slot, InputBinding(b.primary, KEY_INVALID)); // nothing!
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (b.primary == KEY_INVALID) // first free option?
|
2014-12-11 18:01:57 +00:00
|
|
|
|
m_input->SetInputBinding(slot, InputBinding(b.secondary, b.primary)); // shift
|
2012-10-06 22:46:46 +00:00
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return false; // not used
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CKey::Draw()
|
|
|
|
|
{
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if ((m_state & STATE_VISIBLE) == 0)
|
2012-08-17 20:43:07 +00:00
|
|
|
|
return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
Math::Point iDim = m_dim;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
m_dim.x = 200.0f/640.0f;
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_SHADOW)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
DrawShadow(m_pos, m_dim);
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2014-07-24 20:17:49 +00:00
|
|
|
|
m_engine->SetTexture("textures/interface/button1.png");
|
2014-12-11 18:01:57 +00:00
|
|
|
|
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
float zoomExt = 1.00f;
|
|
|
|
|
float zoomInt = 0.95f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
int icon = 2;
|
|
|
|
|
if (m_binding.primary == KEY_INVALID && m_binding.secondary == KEY_INVALID) // no shortcut?
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 3;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_DEFAULT)
|
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
|
DrawPart(23, 1.3f, 0.0f);
|
|
|
|
|
|
|
|
|
|
zoomExt *= 1.15f;
|
|
|
|
|
zoomInt *= 1.15f;
|
|
|
|
|
}
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_HILIGHT)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 1;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_CHECK)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 0;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_PRESS)
|
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 3;
|
|
|
|
|
zoomInt *= 0.9f;
|
|
|
|
|
}
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if ((m_state & STATE_ENABLE) == 0)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 7;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_DEAD)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 17;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_catch)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
icon = 23;
|
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
|
DrawPart(icon, zoomExt, 8.0f / 256.0f); // draws the button
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
float h = m_engine->GetText()->GetHeight(m_fontType, m_fontSize) / 2.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2014-12-11 18:01:57 +00:00
|
|
|
|
std::string keyName = m_input->GetKeysString(m_binding);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
Math::Point pos;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
pos.x = m_pos.x + m_dim.x * 0.5f;
|
|
|
|
|
pos.y = m_pos.y + m_dim.y * 0.5f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
pos.y -= h;
|
2013-12-02 23:11:26 +00:00
|
|
|
|
m_engine->GetText()->DrawText(keyName, m_fontType, m_fontSize, pos, m_dim.x, Gfx::TEXT_ALIGN_CENTER, 0);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
m_dim = iDim;
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
if (m_state & STATE_DEAD)
|
2012-08-17 20:43:07 +00:00
|
|
|
|
return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
// Draws the name.
|
2012-08-17 20:43:07 +00:00
|
|
|
|
pos.x = m_pos.x + (214.0f / 640.0f);
|
|
|
|
|
pos.y = m_pos.y + m_dim.y * 0.5f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
pos.y -= h;
|
2013-12-02 23:11:26 +00:00
|
|
|
|
m_engine->GetText()->DrawText(m_name, m_fontType, m_fontSize, pos, m_dim.x, Gfx::TEXT_ALIGN_LEFT, 0);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
void CKey::SetBinding(InputBinding b)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-10-06 22:46:46 +00:00
|
|
|
|
m_binding = b;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
InputBinding CKey::GetBinding()
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-10-06 22:46:46 +00:00
|
|
|
|
return m_binding;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 22:46:46 +00:00
|
|
|
|
|
|
|
|
|
} // namespace Ui
|
2013-05-26 15:47:54 +00:00
|
|
|
|
|