Skip to content

Commit

Permalink
Untied right stick from focus scroll; Make right stick scroll without…
Browse files Browse the repository at this point in the history
… changing focus (works with text overlay);
  • Loading branch information
efosamark authored and IrneRacoonovich committed Sep 3, 2024
1 parent 681568b commit 1497ce1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
41 changes: 35 additions & 6 deletions lib/libtesla/include/tesla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ namespace tsl {
enum class Class {
Element,
ListItem,
TrackBar
TrackBar,
List
};

namespace cfg {
Expand Down Expand Up @@ -1894,6 +1895,28 @@ namespace tsl {
delete item;
}

virtual bool handleInput(u64 keysDown, u64 keysHeld, const HidTouchState &touchPos, HidAnalogStickState leftJoyStick, HidAnalogStickState rightJoyStick) override {
// process scrolling with right stick without changing focus
const int scroll_amount = 25;
if (((((keysHeld | keysDown) & (HidNpadButton_StickRUp)) != 0) | (((keysHeld | keysDown) & (HidNpadButton_StickRDown)) != 0))) {
if ((keysHeld | keysDown) & (HidNpadButton_StickRDown)) {
this->m_nextOffset += scroll_amount;
}
if ((keysHeld | keysDown) & (HidNpadButton_StickRUp)) {
this->m_nextOffset -= scroll_amount;
}

if (this->m_nextOffset < 0)
this->m_nextOffset = 0;

if (this->m_nextOffset > (this->m_listHeight - this->getHeight()) + 50)
this->m_nextOffset = (this->m_listHeight - this->getHeight() + 50);

return true;
}
return false;
}

virtual void draw(gfx::Renderer *renderer) override {
if (this->m_clearList) {
for (auto& item : this->m_items)
Expand Down Expand Up @@ -1966,6 +1989,10 @@ namespace tsl {

}

Class getClass() override {
return Class::List;
}

virtual void layout(u16 parentX, u16 parentY, u16 parentWidth, u16 parentHeight) override {
s32 y = this->getY() - this->m_offset;

Expand Down Expand Up @@ -3406,6 +3433,7 @@ namespace tsl {
if (topElement == nullptr)
return;
else if (currentGui != nullptr) {

if (!currentGui->initialFocusSet() || keysDown & (HidNpadButton_AnyUp | HidNpadButton_AnyDown | HidNpadButton_AnyLeft | HidNpadButton_AnyRight)) {
currentGui->requestFocus(topElement, FocusDirection::None);
currentGui->markInitialFocusSet();
Expand Down Expand Up @@ -3436,15 +3464,16 @@ namespace tsl {
if (!handled && currentFocus != nullptr) {
static bool shouldShake = true;

if ((((keysHeld & HidNpadButton_AnyUp) != 0) + ((keysHeld & HidNpadButton_AnyDown) != 0) + ((keysHeld & HidNpadButton_AnyLeft) != 0) + ((keysHeld & HidNpadButton_AnyRight) != 0)) == 1) {
if ((((keysHeld & (HidNpadButton_Up | HidNpadButton_StickLUp)) != 0) + ((keysHeld & (HidNpadButton_Down | HidNpadButton_StickLDown)) != 0) +
((keysHeld & (HidNpadButton_Left | HidNpadButton_StickLLeft)) != 0) + ((keysHeld & (HidNpadButton_Right | HidNpadButton_StickLRight)) != 0)) == 1) {
if ((repeatTick == 0 || repeatTick > 20) && (repeatTick % 4) == 0) {
if (keysHeld & HidNpadButton_AnyUp)
if (keysHeld & (HidNpadButton_Up | HidNpadButton_StickLUp))
currentGui->requestFocus(currentFocus->getParent(), FocusDirection::Up, shouldShake);
else if (keysHeld & HidNpadButton_AnyDown)
else if (keysHeld & (HidNpadButton_Down | HidNpadButton_StickLDown))
currentGui->requestFocus(currentFocus->getParent(), FocusDirection::Down, shouldShake);
else if (keysHeld & HidNpadButton_AnyLeft)
else if (keysHeld & (HidNpadButton_Left | HidNpadButton_StickLLeft))
currentGui->requestFocus(currentFocus->getParent(), FocusDirection::Left, shouldShake);
else if (keysHeld & HidNpadButton_AnyRight)
else if (keysHeld & (HidNpadButton_Right | HidNpadButton_StickLRight))
currentGui->requestFocus(currentFocus->getParent(), FocusDirection::Right, shouldShake);

shouldShake = currentGui->getFocusedElement() != currentFocus;
Expand Down
12 changes: 12 additions & 0 deletions source/HelpOverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class HelpOverlay : public tsl::Gui {
private:
std::string helpPath;
tsl::elm::List* p_list;

public:
HelpOverlay(const std::string& helpPath)
Expand All @@ -24,6 +25,7 @@ class HelpOverlay : public tsl::Gui {

auto rootFrame = new tsl::elm::OverlayFrame("Help", "Uberhand Package", "", false, "\uE0E1 Back ");
auto list = new tsl::elm::List();
p_list = list;

if (!isFileOrDirectory(helpPath)) {
list->addItem(new tsl::elm::CustomDrawer([lineHeight, fontSize](tsl::gfx::Renderer* renderer, s32 x, s32 y, s32 w, s32 h) {
Expand Down Expand Up @@ -53,6 +55,16 @@ class HelpOverlay : public tsl::Gui {
if (keysDown & KEY_B) {
tsl::goBack();
return true;
} else {
// process right stick scrolling
if ((keysHeld | keysDown) & (HidNpadButton_StickRDown)) {
this->p_list->handleInput(HidNpadButton_StickRDown, 0,{},{},{});
return true;
}
if ((keysHeld | keysDown) & (HidNpadButton_StickRUp)) {
this->p_list->handleInput(HidNpadButton_StickRUp, 0,{},{},{});
return true;
}
}
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions source/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
#define KEY_SR HidNpadButton_AnySR
#define KEY_LSTICK HidNpadButton_StickL
#define KEY_RSTICK HidNpadButton_StickR
#define KEY_UP (HidNpadButton_Up | HidNpadButton_StickLUp | HidNpadButton_StickRUp)
#define KEY_DOWN (HidNpadButton_Down | HidNpadButton_StickLDown | HidNpadButton_StickRDown)
#define KEY_LEFT (HidNpadButton_Left | HidNpadButton_StickLLeft | HidNpadButton_StickRLeft)
#define KEY_RIGHT (HidNpadButton_Right | HidNpadButton_StickLRight | HidNpadButton_StickRRight)
#define KEY_UP (HidNpadButton_Up | HidNpadButton_StickLUp)
#define KEY_DOWN (HidNpadButton_Down | HidNpadButton_StickLDown)
#define KEY_LEFT (HidNpadButton_Left | HidNpadButton_StickLLeft)
#define KEY_RIGHT (HidNpadButton_Right | HidNpadButton_StickLRight)
#define touchPosition const HidTouchState
#define touchInput &touchPos
#define JoystickPosition HidAnalogStickState
Expand Down

0 comments on commit 1497ce1

Please sign in to comment.