2012-06-26 20:23:05 +00:00
|
|
|
// * This file is part of the COLOBOT source code
|
|
|
|
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
|
2012-08-17 20:43:07 +00:00
|
|
|
// * Copyright (C) 2012 Polish Portal of Colobot (PPC)
|
2012-06-26 20:23:05 +00:00
|
|
|
// *
|
|
|
|
// * 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://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
// key.cpp
|
|
|
|
|
|
|
|
|
2012-09-18 22:04:21 +00:00
|
|
|
#include "ui/key.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-15 16:50:51 +00:00
|
|
|
#include <string.h>
|
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
|
|
|
|
|
|
|
void GetKeyName(char *name, int key)
|
|
|
|
{
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( !GetResource(RES_KEY, key, name) ) {
|
|
|
|
if (isalnum(key)) {
|
2012-06-26 20:23:05 +00:00
|
|
|
name[0] = key;
|
|
|
|
name[1] = 0;
|
|
|
|
}
|
2012-08-17 20:43:07 +00:00
|
|
|
else {
|
2012-06-26 20:23:05 +00:00
|
|
|
sprintf(name, "Code %d", key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Object's constructor.
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
CKey::CKey() : CControl()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
m_key[0] = 0;
|
|
|
|
m_key[1] = 0;
|
|
|
|
m_bCatch = false;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
|
|
|
m_app = CApplication::GetInstancePointer();
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object's destructor.
|
|
|
|
|
|
|
|
CKey::~CKey()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a new button.
|
|
|
|
|
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-17 20:43:07 +00:00
|
|
|
char name[100];
|
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);
|
|
|
|
GetResource(RES_EVENT, eventMsg, name);
|
2012-08-31 20:28:07 +00:00
|
|
|
SetName(std::string(name));
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management of an event.
|
|
|
|
|
|
|
|
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-08-17 20:43:07 +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
|
|
|
|
m_bCatch = Detect(event.mousePos);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:17:28 +00:00
|
|
|
if (event.type == EVENT_KEY_DOWN && m_bCatch) {
|
2012-06-26 20:23:05 +00:00
|
|
|
m_bCatch = false;
|
|
|
|
|
2012-09-19 20:17:28 +00:00
|
|
|
if ( TestKey(event.key.key) ) { // impossible ?
|
2012-06-26 20:23:05 +00:00
|
|
|
m_sound->Play(SOUND_TZOING);
|
2012-08-17 21:03:52 +00:00
|
|
|
} else {
|
2012-09-21 22:38:17 +00:00
|
|
|
// TODO: test for virtual, joystick, etc.
|
2012-09-19 20:17:28 +00:00
|
|
|
if ( event.key.key == m_key[0] || event.key.key == m_key[1] ) {
|
|
|
|
m_key[0] = event.key.key;
|
2012-06-26 20:23:05 +00:00
|
|
|
m_key[1] = 0;
|
2012-08-17 20:43:07 +00:00
|
|
|
} else {
|
2012-06-26 20:23:05 +00:00
|
|
|
m_key[1] = m_key[0];
|
2012-09-21 22:38:17 +00:00
|
|
|
m_key[0] = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks when a key is already used.
|
|
|
|
|
|
|
|
bool CKey::TestKey(int key)
|
|
|
|
{
|
2012-08-17 21:03:52 +00:00
|
|
|
if ( key == KEY(PAUSE) || key == KEY(PRINT) ) return true; // blocked key
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-19 20:17:28 +00:00
|
|
|
/* TODO: input bindings
|
2012-08-17 20:43:07 +00:00
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
if (key == m_app->GetKey(i, j) ) // key used?
|
|
|
|
m_app->SetKey(i, j, 0); // nothing!
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( m_app->GetKey(i, 0) == 0 ) { // first free option?
|
|
|
|
m_app->SetKey(i, 0, m_app->GetKey(i, 1)); // shift
|
|
|
|
m_app->SetKey(i, 1, 0);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-09-19 20:17:28 +00:00
|
|
|
} */
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
return false; // not used
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draws button.
|
|
|
|
|
|
|
|
void CKey::Draw()
|
|
|
|
{
|
2012-08-17 20:43:07 +00:00
|
|
|
Math::Point iDim, pos;
|
|
|
|
float zoomExt, zoomInt, h;
|
|
|
|
int icon;
|
|
|
|
char text[100];
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( (m_state & STATE_VISIBLE) == 0 )
|
|
|
|
return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
iDim = m_dim;
|
|
|
|
m_dim.x = 200.0f/640.0f;
|
|
|
|
|
|
|
|
if ( m_state & STATE_SHADOW )
|
|
|
|
DrawShadow(m_pos, m_dim);
|
2012-08-17 20:43:07 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-16 08:38:08 +00:00
|
|
|
m_engine->SetTexture("button1.png");
|
2012-08-17 20:43:07 +00:00
|
|
|
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL); // was D3DSTATENORMAL
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
zoomExt = 1.00f;
|
|
|
|
zoomInt = 0.95f;
|
|
|
|
|
|
|
|
icon = 2;
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( m_key[0] == 0 && m_key[1] == 0 ) // no shortcut?
|
2012-06-26 20:23:05 +00:00
|
|
|
icon = 3;
|
2012-08-17 20:43:07 +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-06-26 20:23:05 +00:00
|
|
|
if ( m_state & STATE_HILIGHT )
|
|
|
|
icon = 1;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( m_state & STATE_CHECK )
|
|
|
|
icon = 0;
|
2012-08-17 20:43:07 +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-06-26 20:23:05 +00:00
|
|
|
if ( (m_state & STATE_ENABLE) == 0 )
|
|
|
|
icon = 7;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( m_state & STATE_DEAD )
|
|
|
|
icon = 17;
|
2012-08-17 20:43:07 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( m_bCatch )
|
|
|
|
icon = 23;
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
DrawPart(icon, zoomExt, 8.0f / 256.0f); // draws the button
|
|
|
|
|
|
|
|
h = m_engine->GetText()->GetHeight(m_fontType, m_fontSize) / 2.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
GetKeyName(text, m_key[0]);
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( m_key[1] != 0 ) {
|
2012-06-26 20:23:05 +00:00
|
|
|
GetResource(RES_TEXT, RT_KEY_OR, text+strlen(text));
|
|
|
|
GetKeyName(text+strlen(text), m_key[1]);
|
|
|
|
}
|
|
|
|
|
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;
|
2012-08-17 20:43:07 +00:00
|
|
|
m_engine->GetText()->DrawText(std::string(text), 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-08-17 20:43:07 +00:00
|
|
|
if ( m_state & STATE_DEAD )
|
|
|
|
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;
|
2012-09-18 15:52:36 +00:00
|
|
|
m_engine->GetText()->DrawText(std::string(m_name), m_fontType, m_fontSize, pos, m_dim.x, Gfx::TEXT_ALIGN_LEFT, 0);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CKey::SetKey(int option, int key)
|
|
|
|
{
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( option < 0 || option > 1 ) return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
m_key[option] = key;
|
|
|
|
}
|
|
|
|
|
2012-08-17 20:43:07 +00:00
|
|
|
int CKey::GetKey(int option)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-08-17 20:43:07 +00:00
|
|
|
if ( option < 0 || option > 1 ) return 0;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
return m_key[option];
|
|
|
|
}
|
|
|
|
|
2012-08-17 21:03:52 +00:00
|
|
|
}
|