all repos — mgba @ 091e717133a10f785f02d1d4e880b8271fea8066

mGBA Game Boy Advance Emulator

src/platform/qt/GBAKeyEditor.cpp (view raw)

  1#include "GBAKeyEditor.h"
  2
  3#include <QPaintEvent>
  4#include <QPainter>
  5#include <QPushButton>
  6#include <QVBoxLayout>
  7
  8#include "InputController.h"
  9#include "KeyEditor.h"
 10
 11extern "C" {
 12#include "gba-input.h"
 13}
 14
 15using namespace QGBA;
 16
 17const qreal GBAKeyEditor::DPAD_CENTER_X = 0.247;
 18const qreal GBAKeyEditor::DPAD_CENTER_Y = 0.431;
 19const qreal GBAKeyEditor::DPAD_WIDTH = 0.1;
 20const qreal GBAKeyEditor::DPAD_HEIGHT = 0.1;
 21
 22GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* parent)
 23	: QWidget(parent)
 24	, m_type(type)
 25	, m_controller(controller)
 26{
 27	setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
 28	setMinimumSize(300, 300);
 29
 30	const GBAInputMap* map = controller->map();
 31
 32	m_keyDU = new KeyEditor(this);
 33	m_keyDD = new KeyEditor(this);
 34	m_keyDL = new KeyEditor(this);
 35	m_keyDR = new KeyEditor(this);
 36	m_keySelect = new KeyEditor(this);
 37	m_keyStart = new KeyEditor(this);
 38	m_keyA = new KeyEditor(this);
 39	m_keyB = new KeyEditor(this);
 40	m_keyL = new KeyEditor(this);
 41	m_keyR = new KeyEditor(this);
 42	m_keyDU->setValue(GBAInputQueryBinding(map, type, GBA_KEY_UP));
 43	m_keyDD->setValue(GBAInputQueryBinding(map, type, GBA_KEY_DOWN));
 44	m_keyDL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_LEFT));
 45	m_keyDR->setValue(GBAInputQueryBinding(map, type, GBA_KEY_RIGHT));
 46	m_keySelect->setValue(GBAInputQueryBinding(map, type, GBA_KEY_SELECT));
 47	m_keyStart->setValue(GBAInputQueryBinding(map, type, GBA_KEY_START));
 48	m_keyA->setValue(GBAInputQueryBinding(map, type, GBA_KEY_A));
 49	m_keyB->setValue(GBAInputQueryBinding(map, type, GBA_KEY_B));
 50	m_keyL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_L));
 51	m_keyR->setValue(GBAInputQueryBinding(map, type, GBA_KEY_R));
 52
 53	connect(m_keyDU, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 54	connect(m_keyDD, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 55	connect(m_keyDL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 56	connect(m_keyDR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 57	connect(m_keySelect, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 58	connect(m_keyStart, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 59	connect(m_keyA, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 60	connect(m_keyB, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 61	connect(m_keyL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 62	connect(m_keyR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 63
 64	m_buttons = new QWidget(this);
 65	QVBoxLayout* layout = new QVBoxLayout;
 66	m_buttons->setLayout(layout);
 67
 68	QPushButton* setAll = new QPushButton(tr("Set all"));
 69	connect(setAll, SIGNAL(pressed()), this, SLOT(setAll()));
 70	layout->addWidget(setAll);
 71
 72	QPushButton* save = new QPushButton(tr("Save"));
 73	connect(save, SIGNAL(pressed()), this, SLOT(save()));
 74	layout->addWidget(save);
 75	layout->setSpacing(6);
 76
 77	m_keyOrder = QList<KeyEditor*>{
 78		m_keyDU,
 79		m_keyDR,
 80		m_keyDD,
 81		m_keyDL,
 82		m_keyA,
 83		m_keyB,
 84		m_keySelect,
 85		m_keyStart,
 86		m_keyL,
 87		m_keyR
 88	};
 89
 90	m_currentKey = m_keyOrder.end();
 91
 92	m_background.load(":/res/keymap.qpic");
 93
 94	setAll->setFocus();
 95}
 96
 97void GBAKeyEditor::setAll() {
 98	m_currentKey = m_keyOrder.begin();
 99	(*m_currentKey)->setFocus();
100}
101
102void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
103	setLocation(m_buttons, 0.5, 0.2);
104	setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
105	setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
106	setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
107	setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
108	setLocation(m_keySelect, 0.415, 0.93);
109	setLocation(m_keyStart, 0.585, 0.93);
110	setLocation(m_keyA, 0.826, 0.451);
111	setLocation(m_keyB, 0.667, 0.490);
112	setLocation(m_keyL, 0.1, 0.1);
113	setLocation(m_keyR, 0.9, 0.1);
114}
115
116void GBAKeyEditor::paintEvent(QPaintEvent* event) {
117	QPainter painter(this);
118	painter.scale(width() / 480.0, height() / 480.0);
119	painter.drawPicture(0, 0, m_background);
120}
121
122void GBAKeyEditor::setNext() {
123	if (m_currentKey == m_keyOrder.end()) {
124		return;
125	}
126
127	if (!(*m_currentKey)->hasFocus()) {
128		m_currentKey = m_keyOrder.end();
129	}
130
131	++m_currentKey;
132	if (m_currentKey != m_keyOrder.end()) {
133		(*m_currentKey)->setFocus();
134	} else {
135		(*(m_currentKey - 1))->clearFocus();
136	}
137}
138
139void GBAKeyEditor::save() {
140	m_controller->bindKey(m_type, m_keyDU->value(), GBA_KEY_UP);
141	m_controller->bindKey(m_type, m_keyDD->value(), GBA_KEY_DOWN);
142	m_controller->bindKey(m_type, m_keyDL->value(), GBA_KEY_LEFT);
143	m_controller->bindKey(m_type, m_keyDR->value(), GBA_KEY_RIGHT);
144	m_controller->bindKey(m_type, m_keySelect->value(), GBA_KEY_SELECT);
145	m_controller->bindKey(m_type, m_keyStart->value(), GBA_KEY_START);
146	m_controller->bindKey(m_type, m_keyA->value(), GBA_KEY_A);
147	m_controller->bindKey(m_type, m_keyB->value(), GBA_KEY_B);
148	m_controller->bindKey(m_type, m_keyL->value(), GBA_KEY_L);
149	m_controller->bindKey(m_type, m_keyR->value(), GBA_KEY_R);
150	m_controller->saveConfiguration(m_type);
151}
152
153void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
154	QSize s = size();
155	QSize hint = widget->sizeHint();
156	widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
157}