Skip to content

Commit

Permalink
Add buttons and file selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Jan 11, 2024
1 parent a5a6169 commit 2e938bf
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 120 deletions.
28 changes: 28 additions & 0 deletions src/Clickable.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import QtQuick 2.10
import QtQuick.Controls 2.4

Label {
id: root
padding: 5
verticalAlignment: Qt.AlignVCenter
horizontalAlignment: Qt.AlignHCenter
property int border: 0
property int radius: 0
property string borderColor: "black"
property string backgroundColor: "transparent"
signal pressed()
signal released()
signal clicked()
MouseArea {
anchors.fill: parent
onPressed: root.pressed()
onReleased: root.released()
onClicked: root.clicked()
}
background: Rectangle{
border.width: root.border
border.color: root.borderColor
radius: root.radius
color: root.backgroundColor
}
}
45 changes: 45 additions & 0 deletions src/FilePicker.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import QtQuick 2.10
import Qt.labs.folderlistmodel 2.15
import "."

Item {
id: root
property var nameFilters: ["*.gb"]
property string rootFolder: "file:///home/root"
property string folder: "file:///home/root/roms"
property int selectedIndex: -1
function selected(){ return folderModel.get(selectedIndex, "filePath") }

Rectangle {
anchors.fill: parent
color: "white"
}

FolderListModel {
id: folderModel
nameFilters: root.nameFilters
rootFolder: root.rootFolder
folder: root.folder
}

Component {
id: fileDelegate
Clickable {
text: fileName
width: parent.width
font.pointSize: 20
function index(){ return folderModel.indexOf(fileUrl) }
color: root.selectedIndex === index() ? "white" : "black"
backgroundColor: root.selectedIndex === index() ? "black" : "white"
onClicked: root.selectedIndex = root.selectedIndex === index() ? -1 : index()
}
}

ListView {
id: list
anchors.fill: parent
model: folderModel
delegate: fileDelegate
}
}

30 changes: 3 additions & 27 deletions src/gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ class Gameboy : public QQuickPaintedItem {
QRect rect(){ return QRect(0, 0, width(), height()); }
bool running(){ return thread->isRunning(); }
bool paused(){ return thread->isPaused(); }
Q_REVISION(1) Q_INVOKABLE void loadROM(QString path){
thread->stop();
thread->loadROM(path);
}
Q_REVISION(1) Q_INVOKABLE void loadROM(QString path){ thread->loadROM(path); }
Q_REVISION(1) Q_INVOKABLE void stop(){ thread->stop(); }
Q_REVISION(1) Q_INVOKABLE void toggle(){ thread->toggle(); }
Q_REVISION(1) Q_INVOKABLE void toggleSpeed(){ thread->toggleSpeed(); }
Q_REVISION(1) Q_INVOKABLE void keyDown(int keycode){ thread->keyDown(keycode); }
Q_REVISION(1) Q_INVOKABLE void keyUp(int keycode){ thread->keyUp(keycode); }

signals:
void runningChanged(bool);
Expand All @@ -61,27 +60,4 @@ class Gameboy : public QQuickPaintedItem {
private:
QImage* image;
GameboyThread* thread;

GBKeypadKey qtkeytogb(int keycode) {
switch (keycode) {
case Qt::Key_Return:
return GBKeypadKey_START;
case Qt::Key_Space:
return GBKeypadKey_SELECT;
case Qt::Key_Left:
return GBKeypadKey_LEFT;
case Qt::Key_Right:
return GBKeypadKey_RIGHT;
case Qt::Key_Down:
return GBKeypadKey_DOWN;
case Qt::Key_Up:
return GBKeypadKey_UP;
case Qt::Key_X:
return GBKeypadKey_B;
case Qt::Key_Z:
return GBKeypadKey_A;
default:
return GBKeypadKey_NONE;
}
}
};
42 changes: 41 additions & 1 deletion src/gameboythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class GameboyThread : public QThread{
delete qboy;
}
bool loadROM(QString path){
stop();
qDebug() << "Loading ROM:" << path;
QFile file(path);
if(!file.exists()){
qDebug() << "Path does not exist!";
return false;
}
qboy->reset();
qboy->loadgame(path.toStdString());
start();
return true;
Expand All @@ -46,6 +48,7 @@ class GameboyThread : public QThread{
if(pauseRequested){
pauseRequested = false;
emit resumed();
schedule(0);
}else{
pauseRequested = true;
emit paused();
Expand All @@ -61,6 +64,20 @@ class GameboyThread : public QThread{
}
QImage* getImage(){ return new QImage(qboy->getLCD(), 160, 144, QImage::Format_RGB32); }
bool isPaused(){ return isRunning() && pauseRequested; }
void keyDown(int keycode){
GBKeypadKey key = qtkeytogb(keycode);
if(key != GBKeypadKey_NONE){
qDebug() << "Down" << key;
qboy->keydown(key);
}
}
void keyUp(int keycode){
GBKeypadKey key = qtkeytogb(keycode);
if(key != GBKeypadKey_NONE){
qDebug() << "Up" << key;
qboy->keyup(key);
}
}

signals:
void updated();
Expand All @@ -84,7 +101,7 @@ class GameboyThread : public QThread{

private slots:
void cycle(){
while(true){
while(!pauseRequested){
qboy->cycle();
if(qboy->refresh_screen()){
auto now = std::chrono::steady_clock::now();
Expand All @@ -108,4 +125,27 @@ private slots:
bool pauseRequested;
int thirds;
std::chrono::time_point<std::chrono::steady_clock> lastFrame;

GBKeypadKey qtkeytogb(int keycode) {
switch (keycode) {
case Qt::Key_Return:
return GBKeypadKey_START;
case Qt::Key_Space:
return GBKeypadKey_SELECT;
case Qt::Key_Left:
return GBKeypadKey_LEFT;
case Qt::Key_Right:
return GBKeypadKey_RIGHT;
case Qt::Key_Down:
return GBKeypadKey_DOWN;
case Qt::Key_Up:
return GBKeypadKey_UP;
case Qt::Key_X:
return GBKeypadKey_B;
case Qt::Key_Z:
return GBKeypadKey_A;
default:
return GBKeypadKey_NONE;
}
}
};
Loading

0 comments on commit 2e938bf

Please sign in to comment.