Skip to content

Commit

Permalink
[Qt] XSideBar now handles controller navigation
Browse files Browse the repository at this point in the history
(Dpad only for now)
  • Loading branch information
Razzile committed Jan 2, 2024
1 parent e646ac6 commit 2d4a7db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/xenia/ui/qt/tabs/split_tab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,13 @@ void SplitTab::Build() {

sidebar_->addSpacing(20);

QButtonGroup* bg = new QButtonGroup();

// loop over sidebar button items and connect them to slots
int counter = 0;
for (auto it = sidebar_items_.begin(); it != sidebar_items_.end();
++it, ++counter) {
SidebarItem& item = *it;
auto btn = sidebar_->addAction(item.glyph(), item.title());
btn->setCheckable(true);
bg->addButton(btn);

// set the first item to checked
if (counter == 0) {
Expand Down
49 changes: 45 additions & 4 deletions src/xenia/ui/qt/widgets/sidebar.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include "xenia/ui/qt/widgets/sidebar.h"

#include <QButtonGroup>

#include "xenia/ui/qt/events/hid_event.h"

namespace xe {
namespace ui {
namespace qt {

XSideBar::XSideBar() : Themeable<QToolBar>("XSideBar") {}
XSideBar::XSideBar()
: Themeable<QToolBar>("XSideBar"), buttons_(new QButtonGroup(this)) {}

XSideBarButton* XSideBar::addAction(const QString& text) {
auto button = new XSideBarButton(text);

buttons_.append(button);
buttons_->addButton(button);
QToolBar::addWidget(button);

return button;
Expand All @@ -18,7 +23,7 @@ XSideBarButton* XSideBar::addAction(const QString& text) {
XSideBarButton* XSideBar::addAction(QChar glyph, const QString& text) {
auto button = new XSideBarButton(glyph, text);

buttons_.append(button);
buttons_->addButton(button);
QToolBar::addWidget(button);

return button;
Expand All @@ -31,6 +36,42 @@ QWidget* XSideBar::addSpacing(int size) {
return spacer;
}

bool XSideBar::event(QEvent* event) {
if (event->type() == HidEvent::ButtonPressType) {
auto button_press_event = static_cast<ButtonPressEvent*>(event);
// skip repeat presses
if (button_press_event->is_repeat()) {
return false;
}

int direction = 0;

if (button_press_event->buttons() & kInputDpadDown) {
direction = -1;
} else if (button_press_event->buttons() & kInputDpadUp) {
direction = 1;
}

if (direction != 0) {
for (const auto& btn : buttons_->buttons()) {
if (btn->hasFocus()) {
if (int id = buttons_->id(btn); id != -1) {
if (auto next = buttons_->button(id + direction)) {
next->setFocus();
break;
}
}
}
}

event->accept();
return true;
}
}

return Themeable<QToolBar>::event(event);
}

} // namespace qt
} // namespace ui
} // namespace xe
} // namespace xe
7 changes: 6 additions & 1 deletion src/xenia/ui/qt/widgets/sidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class XSideBar : public Themeable<QToolBar> {

QWidget* addSpacing(int size);

QButtonGroup* buttons() const { return buttons_; }

protected:
bool event(QEvent* event) override;

private:
QVector<XSideBarButton*> buttons_;
QButtonGroup* buttons_;
};

} // namespace qt
Expand Down

0 comments on commit 2d4a7db

Please sign in to comment.