Increase savestate view usability
Jeffrey Pfau jeffrey@endrift.com
Wed, 15 Oct 2014 00:37:29 -0700
6 files changed,
107 insertions(+),
11 deletions(-)
M
src/platform/qt/CMakeLists.txt
→
src/platform/qt/CMakeLists.txt
@@ -29,6 +29,7 @@ Display.cpp
GameController.cpp LoadSaveState.cpp LogView.cpp + SavestateButton.cpp Window.cpp VFileDevice.cpp)
M
src/platform/qt/LoadSaveState.cpp
→
src/platform/qt/LoadSaveState.cpp
@@ -3,6 +3,8 @@
#include "GameController.h" #include "VFileDevice.h" +#include <QKeyEvent> + extern "C" { #include "gba-serialize.h" #include "gba-video.h"@@ -13,6 +15,7 @@
LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent) : QWidget(parent) , m_controller(controller) + , m_currentFocus(0) { m_ui.setupUi(this);@@ -32,6 +35,7 @@
int i; for (i = 0; i < NUM_SLOTS; ++i) { loadState(i); + m_slots[i]->installEventFilter(this); connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i); }); } }@@ -41,6 +45,35 @@ m_mode = mode;
QString text = mode == LoadSave::LOAD ? tr("Load State") : tr("SaveState"); setWindowTitle(text); m_ui.lsLabel->setText(text); +} + +bool LoadSaveState::eventFilter(QObject*, QEvent* event) { + if (event->type() == QEvent::KeyPress) { + int column = m_currentFocus % 3; + int row = m_currentFocus - column; + switch (static_cast<QKeyEvent*>(event)->key()) { + case Qt::Key_Up: + row += 6; + break; + case Qt::Key_Down: + row += 3; + break; + case Qt::Key_Left: + column += 2; + break; + case Qt::Key_Right: + column += 1; + break; + default: + return false; + } + column %= 3; + row %= 9; + m_currentFocus = column + row; + m_slots[m_currentFocus]->setFocus(); + return true; + } + return false; } void LoadSaveState::loadState(int slot) {
M
src/platform/qt/LoadSaveState.h
→
src/platform/qt/LoadSaveState.h
@@ -8,6 +8,7 @@
namespace QGBA { class GameController; +class SavestateButton; enum class LoadSave { LOAD,@@ -18,22 +19,25 @@ class LoadSaveState : public QWidget {
Q_OBJECT public: - const static int NUM_SLOTS = 9; LoadSaveState(GameController* controller, QWidget* parent = nullptr); void setMode(LoadSave mode); +protected: + virtual bool eventFilter(QObject*, QEvent*) override; + private: void loadState(int slot); void triggerState(int slot); Ui::LoadSaveState m_ui; GameController* m_controller; - QPushButton* m_slots[NUM_SLOTS]; + SavestateButton* m_slots[NUM_SLOTS]; LoadSave m_mode; + int m_currentFocus; QPixmap m_currentImage; };
M
src/platform/qt/LoadSaveState.ui
→
src/platform/qt/LoadSaveState.ui
@@ -30,7 +30,7 @@ <property name="spacing">
<number>2</number> </property> <item row="1" column="0"> - <widget class="QPushButton" name="state1"> + <widget class="QGBA::SavestateButton" name="state1"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -58,7 +58,7 @@ </property>
</widget> </item> <item row="1" column="1"> - <widget class="QPushButton" name="state2"> + <widget class="QGBA::SavestateButton" name="state2"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -108,7 +108,7 @@ </property>
</widget> </item> <item row="1" column="2"> - <widget class="QPushButton" name="state3"> + <widget class="QGBA::SavestateButton" name="state3"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -136,7 +136,7 @@ </property>
</widget> </item> <item row="2" column="0"> - <widget class="QPushButton" name="state4"> + <widget class="QGBA::SavestateButton" name="state4"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -164,7 +164,7 @@ </property>
</widget> </item> <item row="2" column="1"> - <widget class="QPushButton" name="state5"> + <widget class="QGBA::SavestateButton" name="state5"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -192,7 +192,7 @@ </property>
</widget> </item> <item row="2" column="2"> - <widget class="QPushButton" name="state6"> + <widget class="QGBA::SavestateButton" name="state6"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -220,7 +220,7 @@ </property>
</widget> </item> <item row="3" column="0"> - <widget class="QPushButton" name="state7"> + <widget class="QGBA::SavestateButton" name="state7"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -248,7 +248,7 @@ </property>
</widget> </item> <item row="3" column="1"> - <widget class="QPushButton" name="state8"> + <widget class="QGBA::SavestateButton" name="state8"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -276,7 +276,7 @@ </property>
</widget> </item> <item row="3" column="2"> - <widget class="QPushButton" name="state9"> + <widget class="QGBA::SavestateButton" name="state9"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <horstretch>0</horstretch>@@ -305,6 +305,13 @@ </widget>
</item> </layout> </widget> + <customwidgets> + <customwidget> + <class>QGBA::SavestateButton</class> + <extends>QPushButton</extends> + <header>SavestateButton.h</header> + </customwidget> + </customwidgets> <tabstops> <tabstop>state1</tabstop> <tabstop>state2</tabstop>
A
src/platform/qt/SavestateButton.cpp
@@ -0,0 +1,33 @@
+#include "SavestateButton.h" + +#include <QApplication> +#include <QPainter> + +using namespace QGBA; + +SavestateButton::SavestateButton(QWidget* parent) + : QAbstractButton(parent) +{ + // Nothing to do +} + +void SavestateButton::paintEvent(QPaintEvent*) { + QPainter painter(this); + QRect frame(0, 0, width(), height()); + QRect full(1, 1, width() - 2, height() - 2); + QPalette palette = QApplication::palette(this); + painter.setPen(Qt::black); + QLinearGradient grad(0, 0, 0, 1); + grad.setCoordinateMode(QGradient::ObjectBoundingMode); + grad.setColorAt(0, palette.color(QPalette::Shadow)); + grad.setColorAt(1, palette.color(QPalette::Dark)); + painter.setBrush(grad); + painter.drawRect(frame); + painter.setPen(Qt::NoPen); + painter.drawPixmap(full, icon().pixmap(full.size())); + if (hasFocus()) { + QColor highlight = palette.color(QPalette::Highlight); + highlight.setAlpha(128); + painter.fillRect(full, highlight); + } +}
A
src/platform/qt/SavestateButton.h
@@ -0,0 +1,18 @@
+#ifndef QGBA_SAVESTATE_BUTTON +#define QGBA_SAVESTATE_BUTTON + +#include <QAbstractButton> + +namespace QGBA { + +class SavestateButton : public QAbstractButton { +public: + SavestateButton(QWidget* parent = nullptr); + +protected: + virtual void paintEvent(QPaintEvent *e) override; +}; + +} + +#endif