openmohaa/code/uilib/uibind.cpp

201 lines
5 KiB
C++
Raw Permalink Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
2023-12-27 18:54:20 +01:00
Copyright (C) 2023 the OpenMoHAA team
2016-03-27 11:49:47 +02:00
This file is part of OpenMoHAA source code.
OpenMoHAA source code 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 2 of the License,
or (at your option) any later version.
OpenMoHAA source code 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 OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "ui_local.h"
2023-12-27 22:47:01 +01:00
#include "uibindlist.h"
#include "../qcommon/localization.h"
2016-03-27 11:49:47 +02:00
2023-12-28 01:12:19 +01:00
Event EV_UIBindButton_Command("bindcommand", EV_DEFAULT, "s", "cmd", "Command to bind");
2023-12-27 18:54:20 +01:00
CLASS_DECLARATION(UIButton, UIBindButton, NULL) {
2023-12-28 01:12:19 +01:00
{&W_AnyMouseDown, &UIBindButton::Pressed },
{&EV_UIBindButton_Command, &UIBindButton::SetCommand},
{NULL, NULL }
2016-03-27 11:49:47 +02:00
};
2023-12-27 22:47:01 +01:00
UIBindButton::UIBindButton()
: m_getkey(qfalse)
, m_bindindex(-1)
, m_alternate(qfalse)
, m_mat(NULL)
{}
2016-03-27 11:49:47 +02:00
2023-12-27 18:54:20 +01:00
UIBindButton::UIBindButton(str entersound, str activesound)
2023-12-27 22:47:01 +01:00
: m_getkey(qfalse)
, m_bindindex(-1)
, m_alternate(qfalse)
, m_mat(NULL)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
m_entersound = entersound;
m_activesound = activesound;
m_mat = uWinMan.RegisterShader("textures/bind/emptykey");
m_material = m_mat;
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::SetAlternate(qboolean a)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
m_alternate = a;
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::SetCommand(Event *ev)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
SetCommand(ev->GetString(1));
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::SetCommand(str s)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
m_bindcommand = s;
m_bindindex = uWinMan.AddBinding(m_bindcommand);
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::Pressed(void)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
m_getkey = qtrue;
uWinMan.SetBindActive(this);
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::Pressed(Event *ev)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
int i, j;
int buttons;
buttons = ev->GetInteger(3);
if (m_getkey) {
for (i = K_MOUSE1, j = 0; i <= K_MOUSE5; i++, j++) {
if (buttons & (1 << j)) {
KeyEvent(i, 0);
}
}
return;
}
if (!(buttons & 1)) {
// Left mouse button is not held
return;
}
if (m_parent->isSubclassOf(UIFakkBindList)) {
UIFakkBindList *fbl = static_cast<UIFakkBindList *>(m_parent);
if (!fbl->SetActiveRow(this)) {
return;
}
}
m_getkey = true;
uWinMan.SetBindActive(this);
uii.Snd_PlaySound(m_activesound);
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
qboolean UIBindButton::KeyEvent(int key, unsigned int time)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
if (!m_getkey) {
if (m_parent) {
return m_parent->KeyEvent(key, 0);
}
return qfalse;
}
if (key != K_ESCAPE && key != K_PAUSE && key != 0) {
2023-12-27 22:47:01 +01:00
uWinMan.BindKeyToCommand(m_bindcommand, key, m_bindindex, m_alternate);
}
m_getkey = false;
uWinMan.SetBindActive(NULL);
uii.Snd_PlaySound(m_entersound);
2023-12-27 22:47:01 +01:00
return qtrue;
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::DrawPressed(void)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
DrawUnpressed();
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::Clear(void)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
int key1 = -1, key2 = -1;
str s;
s = uWinMan.GetKeyStringForCommand(m_bindcommand, m_bindindex, m_alternate, &key1, &key2);
if (m_alternate) {
if (key2 > 0) {
uWinMan.BindKeyToCommand(str(), key2, m_bindindex, m_alternate);
}
} else {
if (key1 > 0) {
uWinMan.BindKeyToCommand(str(), key1, m_bindindex, m_alternate);
}
}
2016-03-27 11:49:47 +02:00
}
2023-12-27 18:54:20 +01:00
void UIBindButton::DrawUnpressed(void)
2016-03-27 11:49:47 +02:00
{
2023-12-27 22:47:01 +01:00
str keyname;
UIRect2D clientFrame;
clientFrame = getClientFrame();
if (uWinMan.BindActive() == this) {
DrawBoxWithSolidBorder(clientFrame, m_background_color, m_border_color.original, 2, 2, 1.f);
}
if (m_bindindex <= 0) {
return;
}
str s = uWinMan.GetKeyStringForCommand(m_bindcommand, m_bindindex, m_alternate, NULL, NULL);
m_font->setColor(m_background_color);
if (str::cmp(s, m_last_keyname)) {
m_mat = uWinMan.RegisterShader("textures/bind/" + s);
if (m_mat) {
if (m_mat->GetMaterial()) {
m_material = m_mat;
} else {
m_material = uWinMan.RegisterShader("textures/bind/emptykey");
}
} else {
m_material = uWinMan.RegisterShader("textures/bind/emptykey");
}
m_last_keyname = s;
}
if (m_mat) {
if (m_mat->GetMaterial()) {
return;
}
}
m_font->PrintJustified(
UIRect2D(clientFrame.pos.x + 1, clientFrame.pos.y + 1, clientFrame.size.width, clientFrame.size.height),
2023-12-27 22:47:01 +01:00
m_iFontAlignmentHorizontal,
m_iFontAlignmentVertical,
Sys_LV_CL_ConvertString(s),
m_bVirtual ? m_vVirtualScale : NULL
);
2016-03-27 11:49:47 +02:00
}