Skip to content

Commit

Permalink
Clean up UI on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Jan 12, 2024
1 parent 3a36bb6 commit afc0829
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Gameboy : public QQuickPaintedItem {
Q_PROPERTY(bool slowedDown READ slowedDown NOTIFY slowedDownChanged REVISION 1)
Q_PROPERTY(QString homeFolder READ homeFolder CONSTANT REVISION 1)
Q_PROPERTY(QString romsFolder READ romsFolder CONSTANT REVISION 1)
Q_PROPERTY(QString romName READ romName NOTIFY romNameChanged REVISION 1)
QML_NAMED_ELEMENT(Gameboy)

public:
Expand All @@ -37,6 +38,7 @@ class Gameboy : public QQuickPaintedItem {
connect(thread, &GameboyThread::paused, this, [this]{ emit pausedChanged(true); }, Qt::QueuedConnection);
connect(thread, &GameboyThread::resumed, this, [this]{ emit pausedChanged(false); }, Qt::QueuedConnection);
connect(thread, &GameboyThread::slowedDownChanged, this, &Gameboy::slowedDownChanged, Qt::QueuedConnection);
connect(thread, &GameboyThread::romNameChanged, this, &Gameboy::romNameChanged, Qt::QueuedConnection);
}
~Gameboy(){
delete thread;
Expand All @@ -60,11 +62,13 @@ class Gameboy : public QQuickPaintedItem {
return QUrl::fromLocalFile(home.absoluteFilePath("roms")).toString();
}
bool slowedDown(){ return thread->slowedDown(); }
QString romName(){ return thread->romName(); }

signals:
void runningChanged(bool);
void pausedChanged(bool);
void slowedDownChanged(bool);
void romNameChanged(QString);

protected:
void paint(QPainter* painter){
Expand Down
11 changes: 11 additions & 0 deletions src/gameboythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QAbstractEventDispatcher>
#include <QWaitCondition>
#include <QMutex>
#include <QFileInfo>

#include <chrono>
#include <libqboy.h>
Expand All @@ -17,6 +18,7 @@ using ms_t = std::chrono::milliseconds;
class GameboyThread : public QThread{
Q_OBJECT
Q_PROPERTY(bool slowedDown READ slowedDown NOTIFY slowedDownChanged REVISION 1)
Q_PROPERTY(QString romName READ romName NOTIFY romNameChanged REVISION 1)

public:
explicit GameboyThread(QObject* parent = 0)
Expand All @@ -41,6 +43,7 @@ class GameboyThread : public QThread{
qboy->reset();
qboy->loadgame(path.toStdString());
currentROM = path;
emit romNameChanged(romName());
start();
return true;
}
Expand Down Expand Up @@ -72,6 +75,7 @@ class GameboyThread : public QThread{
requestInterruption();
quit();
while(isRunning()){}
emit updated();
}
QImage* getImage(){ return new QImage(qboy->getLCD(), 160, 144, QImage::Format_RGB32); }
bool isPaused(){ return isRunning() && pauseRequested; }
Expand All @@ -90,12 +94,19 @@ class GameboyThread : public QThread{
}
}
bool slowedDown(){ return slowdown; }
QString romName(){
if(currentROM.isEmpty()){
return "";
}
return QFileInfo(currentROM).completeBaseName();
}

signals:
void updated();
void paused();
void resumed();
void slowedDownChanged(bool);
void romNameChanged(QString);

protected:
void run(){
Expand Down
73 changes: 66 additions & 7 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ ApplicationWindow {
visible: stateController.state !== "loading"
width: Screen.width
height: Screen.height
title: Qt.application.displayName
minimumWidth: gameboyContainer.width + speedButtons.width + stateButtons.width + 50
minimumHeight: gameboyContainer.height + buttonSelect.height + 50
title: gameboy.running ? gameboy.romName : Qt.application.displayName
Component.onCompleted: stateController.state = "loaded"
header: Rectangle {
color: "black"
Expand Down Expand Up @@ -109,10 +111,23 @@ ApplicationWindow {
},
Item {
anchors.fill: parent
enabled: gameboy.running && stateController.state === "loaded"
enabled: stateController.state === "loaded"

ColumnLayout {
anchors.bottom: gameboyContainer.bottom
id: speedButtons
enabled: gameboy.running
anchors.bottom: {
if(gameboyContainer.bottom < buttonUp.top){
return gameboyContainer.bottom
}
return buttonUp.top
}
anchors.bottomMargin: {
if(gameboyContainer.bottom < buttonUp.top){
return 0
}
return 20
}
anchors.right: gameboyContainer.left
anchors.rightMargin: 20
width: 150
Expand All @@ -121,30 +136,35 @@ ApplicationWindow {
onClicked: gameboyContainer.scale = 1
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
text: "2x"
onClicked: gameboyContainer.scale = 2
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
text: "3x"
onClicked: gameboyContainer.scale = 3
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
text: "4x"
onClicked: gameboyContainer.scale = 4
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
text: "5x"
onClicked: gameboyContainer.scale = 5
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
}

Expand All @@ -168,40 +188,64 @@ ApplicationWindow {
}

ColumnLayout {
anchors.bottom: gameboyContainer.bottom
id: stateButtons
anchors.bottom: {
if(gameboyContainer.bottom < buttonA.top){
return gameboyContainer.bottom
}
return buttonA.top
}
anchors.bottomMargin: {
if(gameboyContainer.bottom < buttonA.top){
return 0
}
return 20
}
anchors.left: gameboyContainer.right
anchors.leftMargin: 20
width: 150

Clickable {
id: stopButton
text: "Stop"
enabled: gameboy.running
onClicked: gameboy.stop()
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
id: resetButton
text: "Reset"
enabled: gameboy.romName !== ""
onClicked: gameboy.reset()
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Item { Layout.fillWidth: true; Layout.preferredHeight: stopButton.height }
Clickable {
id: toggleButton
enabled: gameboy.running
text: gameboy.paused ? "Resume" : "Pause"
onClicked: gameboy.toggle()
border: 1
Layout.fillWidth: true
backgroundColor: "white"
}
Clickable {
id: toggleSpeedButton
text: ""
enabled: gameboy.romName !== ""
color: gameboy.slowedDown ? "black" : "white"
backgroundColor: gameboy.slowedDown ? "white" : "black"
enabled: gameboy.running
onClicked: gameboy.toggleSpeed()
border: 1
Layout.fillWidth: true
}
}

Clickable {
id: buttonLeft
enabled: gameboy.running
text: ""
font.pointSize: 20
width: height
Expand All @@ -210,23 +254,27 @@ ApplicationWindow {
radius: width / 2
anchors.bottom: buttonDown.top
anchors.right: buttonDown.left
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Left)
onReleased: gameboy.keyUp(Qt.Key_Left)
}
Clickable {
id: buttonUp
enabled: gameboy.running
text: ""
font.pointSize: 20
width: height
border: 1
radius: width / 2
anchors.bottom: buttonLeft.top
anchors.left: buttonDown.left
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Up)
onReleased: gameboy.keyUp(Qt.Key_Up)
}
Clickable {
id: buttonRight
enabled: gameboy.running
text: ""
font.pointSize: 20
width: height
Expand All @@ -235,11 +283,13 @@ ApplicationWindow {
radius: width / 2
anchors.bottom: buttonDown.top
anchors.left: buttonDown.right
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Right)
onReleased: gameboy.keyUp(Qt.Key_Right)
}
Clickable {
id: buttonDown
enabled: gameboy.running
text: ""
font.pointSize: 20
width: height
Expand All @@ -248,25 +298,29 @@ ApplicationWindow {
anchors.bottom: parent.bottom
anchors.bottomMargin: 100
anchors.left: parent.left
backgroundColor: "white"
anchors.leftMargin: 100 + buttonLeft.width
onPressed: gameboy.keyDown(Qt.Key_Down)
onReleased: gameboy.keyUp(Qt.Key_Down)
}

Clickable {
id: buttonStart
enabled: gameboy.running
text: "start"
font.pointSize: 15
border: 1
radius: 10
anchors.bottom: buttonSelect.bottom
anchors.left: buttonSelect.right
anchors.leftMargin: 20
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Return)
onReleased: gameboy.keyUp(Qt.Key_Return)
}
Clickable {
id: buttonSelect
enabled: gameboy.running
text: "select"
font.pointSize: 15
border: 1
Expand All @@ -275,12 +329,14 @@ ApplicationWindow {
anchors.bottomMargin: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.horizontalCenterOffset: -(buttonSelect.width / 2) - (buttonStart.anchors.leftMargin / 2)
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Space)
onReleased: gameboy.keyUp(Qt.Key_Space)
}

Clickable {
id: buttonB
enabled: gameboy.running
text: "B"
font.pointSize: 20
border: 1
Expand All @@ -290,11 +346,13 @@ ApplicationWindow {
anchors.bottomMargin: 100
anchors.right: buttonA.left
anchors.rightMargin: 20
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_X)
onReleased: gameboy.keyUp(Qt.Key_X)
}
Clickable {
id: buttonA
enabled: gameboy.running
text: "A"
font.pointSize: 20
border: 1
Expand All @@ -303,6 +361,7 @@ ApplicationWindow {
anchors.right: parent.right
anchors.rightMargin: 100
anchors.bottom: buttonB.top
backgroundColor: "white"
onPressed: gameboy.keyDown(Qt.Key_Z)
onReleased: gameboy.keyUp(Qt.Key_Z)
}
Expand Down

0 comments on commit afc0829

Please sign in to comment.