Increase savestate view usability

This commit is contained in:
Jeffrey Pfau 2014-10-15 00:37:29 -07:00
parent b73f6314ca
commit 7d8041cb58
6 changed files with 107 additions and 11 deletions

View File

@ -29,6 +29,7 @@ set(SOURCE_FILES
GameController.cpp GameController.cpp
LoadSaveState.cpp LoadSaveState.cpp
LogView.cpp LogView.cpp
SavestateButton.cpp
Window.cpp Window.cpp
VFileDevice.cpp) VFileDevice.cpp)

View File

@ -3,6 +3,8 @@
#include "GameController.h" #include "GameController.h"
#include "VFileDevice.h" #include "VFileDevice.h"
#include <QKeyEvent>
extern "C" { extern "C" {
#include "gba-serialize.h" #include "gba-serialize.h"
#include "gba-video.h" #include "gba-video.h"
@ -13,6 +15,7 @@ using namespace QGBA;
LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent) LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_controller(controller) , m_controller(controller)
, m_currentFocus(0)
{ {
m_ui.setupUi(this); m_ui.setupUi(this);
@ -32,6 +35,7 @@ LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent)
int i; int i;
for (i = 0; i < NUM_SLOTS; ++i) { for (i = 0; i < NUM_SLOTS; ++i) {
loadState(i); loadState(i);
m_slots[i]->installEventFilter(this);
connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i); }); connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i); });
} }
} }
@ -43,6 +47,35 @@ void LoadSaveState::setMode(LoadSave mode) {
m_ui.lsLabel->setText(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) { void LoadSaveState::loadState(int slot) {
GBAThread* thread = m_controller->thread(); GBAThread* thread = m_controller->thread();
VFile* vf = GBAGetState(thread->gba, thread->stateDir, slot, false); VFile* vf = GBAGetState(thread->gba, thread->stateDir, slot, false);

View File

@ -8,6 +8,7 @@
namespace QGBA { namespace QGBA {
class GameController; class GameController;
class SavestateButton;
enum class LoadSave { enum class LoadSave {
LOAD, LOAD,
@ -18,22 +19,25 @@ class LoadSaveState : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
const static int NUM_SLOTS = 9; const static int NUM_SLOTS = 9;
LoadSaveState(GameController* controller, QWidget* parent = nullptr); LoadSaveState(GameController* controller, QWidget* parent = nullptr);
void setMode(LoadSave mode); void setMode(LoadSave mode);
protected:
virtual bool eventFilter(QObject*, QEvent*) override;
private: private:
void loadState(int slot); void loadState(int slot);
void triggerState(int slot); void triggerState(int slot);
Ui::LoadSaveState m_ui; Ui::LoadSaveState m_ui;
GameController* m_controller; GameController* m_controller;
QPushButton* m_slots[NUM_SLOTS]; SavestateButton* m_slots[NUM_SLOTS];
LoadSave m_mode; LoadSave m_mode;
int m_currentFocus;
QPixmap m_currentImage; QPixmap m_currentImage;
}; };

View File

@ -30,7 +30,7 @@
<number>2</number> <number>2</number>
</property> </property>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QPushButton" name="state1"> <widget class="QGBA::SavestateButton" name="state1">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -58,7 +58,7 @@
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QPushButton" name="state2"> <widget class="QGBA::SavestateButton" name="state2">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -108,7 +108,7 @@
</widget> </widget>
</item> </item>
<item row="1" column="2"> <item row="1" column="2">
<widget class="QPushButton" name="state3"> <widget class="QGBA::SavestateButton" name="state3">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -136,7 +136,7 @@
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QPushButton" name="state4"> <widget class="QGBA::SavestateButton" name="state4">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -164,7 +164,7 @@
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QPushButton" name="state5"> <widget class="QGBA::SavestateButton" name="state5">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -192,7 +192,7 @@
</widget> </widget>
</item> </item>
<item row="2" column="2"> <item row="2" column="2">
<widget class="QPushButton" name="state6"> <widget class="QGBA::SavestateButton" name="state6">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -220,7 +220,7 @@
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QPushButton" name="state7"> <widget class="QGBA::SavestateButton" name="state7">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -248,7 +248,7 @@
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="QPushButton" name="state8"> <widget class="QGBA::SavestateButton" name="state8">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -276,7 +276,7 @@
</widget> </widget>
</item> </item>
<item row="3" column="2"> <item row="3" column="2">
<widget class="QPushButton" name="state9"> <widget class="QGBA::SavestateButton" name="state9">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -305,6 +305,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>QGBA::SavestateButton</class>
<extends>QPushButton</extends>
<header>SavestateButton.h</header>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>state1</tabstop> <tabstop>state1</tabstop>
<tabstop>state2</tabstop> <tabstop>state2</tabstop>

View File

@ -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);
}
}

View File

@ -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