Skip to content

Commit

Permalink
UI|Client|Input Settings: Added a Key Grabber utility
Browse files Browse the repository at this point in the history
Shows the key codes and symbolic name of all received key events
(except Esc, which clears keyboard focus).
  • Loading branch information
skyjake committed Oct 4, 2013
1 parent 8f46ca7 commit 085c6cc
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/client.pro
Expand Up @@ -425,6 +425,7 @@ DENG_HEADERS += \
include/ui/widgets/gameselectionwidget.h \
include/ui/widgets/gridpopupwidget.h \
include/ui/widgets/icvarwidget.h \
include/ui/widgets/keygrabberwidget.h \
include/ui/widgets/labelwidget.h \
include/ui/widgets/legacywidget.h \
include/ui/widgets/lineeditwidget.h \
Expand Down Expand Up @@ -765,6 +766,7 @@ SOURCES += \
src/ui/widgets/foldpanelwidget.cpp \
src/ui/widgets/gameselectionwidget.cpp \
src/ui/widgets/gridpopupwidget.cpp \
src/ui/widgets/keygrabberwidget.cpp \
src/ui/widgets/labelwidget.cpp \
src/ui/widgets/legacywidget.cpp \
src/ui/widgets/lineeditwidget.cpp \
Expand Down
1 change: 1 addition & 0 deletions doomsday/client/include/ui/dialogs/inputsettingsdialog.h
Expand Up @@ -37,6 +37,7 @@ public slots:
protected slots:
void mouseTogglesChanged();
void mouseSensitivityChanged(double value);
void showDeveloperPopup();

private:
DENG2_PRIVATE(d)
Expand Down
39 changes: 39 additions & 0 deletions doomsday/client/include/ui/widgets/keygrabberwidget.h
@@ -0,0 +1,39 @@
/** @file keygrabberwidget.h Grabs key events and shows them.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 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</small>
*/

#ifndef DENG_CLIENT_KEYGRABBERWIDGET_H
#define DENG_CLIENT_KEYGRABBERWIDGET_H

#include "labelwidget.h"

/**
* When focused, grabs key events and shows the key event data. However, Esc is
* never grabbed.
*/
class KeyGrabberWidget : public LabelWidget
{
public:
KeyGrabberWidget(de::String const &name = "");

bool handleEvent(de::Event const &event);

private:
DENG2_PRIVATE(d)
};

#endif // DENG_CLIENT_KEYGRABBERWIDGET_H
25 changes: 22 additions & 3 deletions doomsday/client/src/ui/dialogs/inputsettingsdialog.cpp
Expand Up @@ -20,6 +20,8 @@
#include "ui/widgets/cvarsliderwidget.h"
#include "ui/widgets/cvartogglewidget.h"
#include "ui/widgets/variabletogglewidget.h"
#include "ui/widgets/gridpopupwidget.h"
#include "ui/widgets/keygrabberwidget.h"

#include "clientapp.h"
#include "con_main.h"
Expand All @@ -38,6 +40,7 @@ DENG_GUI_PIMPL(InputSettingsDialog)
ToggleWidget *mouseInvertX;
ToggleWidget *mouseInvertY;
CVarToggleWidget *joyEnable;
GridPopupWidget *devPopup;

Instance(Public *i) : Base(i)
{
Expand All @@ -51,6 +54,12 @@ DENG_GUI_PIMPL(InputSettingsDialog)
area.add(mouseInvertX = new ToggleWidget);
area.add(mouseInvertY = new ToggleWidget);
area.add(joyEnable = new CVarToggleWidget("input-joy"));

// Developer options.
self.add(devPopup = new GridPopupWidget);
*devPopup << LabelWidget::newWithText(tr("Key Grabber:"))
<< new KeyGrabberWidget;
devPopup->commit();
}

void fetch()
Expand Down Expand Up @@ -138,9 +147,14 @@ InputSettingsDialog::InputSettingsDialog(String const &name)
area().setContentSize(layout.width(), layout.height());

buttons()
<< new DialogButtonItem(DialogWidget::Default | DialogWidget::Accept, tr("Close"))
<< new DialogButtonItem(DialogWidget::Action, tr("Reset to Defaults"),
new SignalAction(this, SLOT(resetToDefaults())));
<< new DialogButtonItem(Default | Accept, tr("Close"))
<< new DialogButtonItem(Action, tr("Reset to Defaults"),
new SignalAction(this, SLOT(resetToDefaults())))
<< new DialogButtonItem(Action | Id1,
style().images().image("gauge"),
new SignalAction(this, SLOT(showDeveloperPopup())));

d->devPopup->setAnchorAndOpeningDirection(buttonWidget(Id1)->rule(), ui::Up);

d->fetch();
}
Expand Down Expand Up @@ -174,3 +188,8 @@ void InputSettingsDialog::mouseSensitivityChanged(double value)
}
}
}

void InputSettingsDialog::showDeveloperPopup()
{
d->devPopup->open();
}
113 changes: 113 additions & 0 deletions doomsday/client/src/ui/widgets/keygrabberwidget.cpp
@@ -0,0 +1,113 @@
/** @file keygrabberwidget.cpp
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 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</small>
*/

#include "ui/widgets/keygrabberwidget.h"
#include "ui/dd_input.h"
#include "ui/b_util.h"

#include <de/KeyEvent>
#include <de/ddstring.h>

using namespace de;
using namespace ui;

DENG_GUI_PIMPL(KeyGrabberWidget)
{
Instance(Public *i) : Base(i)
{}

void focus()
{
root().setFocus(thisPublic);
self.set(Background(Background::GradientFrame, style().colors().colorf("accent"), 6));
self.setText(tr("Waiting for a key..."));
}

void unfocus()
{
root().setFocus(0);
self.set(Background());
self.setText(tr("Click to focus"));
}
};

KeyGrabberWidget::KeyGrabberWidget(String const &name)
: LabelWidget(name), d(new Instance(this))
{
setTextLineAlignment(AlignLeft);
setText(tr("Click to focus"));
}

bool KeyGrabberWidget::handleEvent(Event const &event)
{
if(!hasFocus())
{
switch(handleMouseClick(event))
{
case MouseClickFinished:
d->focus();
return true;

default:
return false;
}
}
else
{
if(KeyEvent const *key = event.maybeAs<KeyEvent>())
{
if(key->ddKey() == DDKEY_ESCAPE)
{
d->unfocus();
return true;
}

ddstring_t name;
Str_Init(&name);

ddevent_t ev;
DD_ConvertEvent(event, &ev);

B_AppendEventToString(&ev, &name);

String info;
info = String("DD:%1 Qt:0x%2 Native:0x%3\n" _E(m) "%4")
.arg(key->ddKey())
.arg(key->qtKey(), 0, 16)
.arg(key->nativeCode(), 0, 16)
.arg(Str_Text(&name));
setText(info);

Str_Free(&name);
return true;
}

if(MouseEvent const *mouse = event.maybeAs<MouseEvent>())
{
if(mouse->type() == Event::MouseButton &&
mouse->state() == MouseEvent::Released &&
!hitTest(event))
{
// Clicking outside clears focus.
d->unfocus();
return true;
}
}
}
return false;
}
5 changes: 5 additions & 0 deletions doomsday/client/src/ui/widgets/labelwidget.cpp
Expand Up @@ -138,6 +138,11 @@ public Font::RichFormat::IStyle
return style().richStyleFormat(contentStyle, sizeFactor, fontWeight, fontStyle, colorIndex);
}

Font const *richStyleFont(Font::RichFormat::Style fontStyle) const
{
return style().richStyleFont(fontStyle);
}

void glInit()
{
drawable.addBuffer(new VertexBuf);
Expand Down

0 comments on commit 085c6cc

Please sign in to comment.