Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI: Add scrolling #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions source/client/app/Minecraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ void Minecraft::tickInput()
if (Mouse::isButtonDown(BUTTON_LEFT))
m_gui.handleClick(1, Mouse::getX(), Mouse::getY());

#ifdef ENH_ALLOW_SCROLL_WHEEL
if (Mouse::getEventButton() == BUTTON_SCROLLWHEEL)
m_gui.handleScroll(Mouse::getEventButtonState() == 1);
#endif

if (!bIsInGUI && getOptions()->field_19)
{
MouseButtonType buttonType = Mouse::getEventButton();
Expand All @@ -428,32 +433,6 @@ void Minecraft::tickInput()
handleMouseClick(buttonType);
field_DAC = field_DA8;
}

#ifdef ENH_ALLOW_SCROLL_WHEEL
if (Mouse::getEventButton() == BUTTON_SCROLLWHEEL)
{
int slot = m_pLocalPlayer->m_pInventory->m_selectedHotbarSlot;

int maxItems = m_gui.getNumUsableSlots() - 1;

if (Mouse::getEventButtonState() == 0) // @NOTE: Scroll up
{
if (slot-- == 0)
{
slot = maxItems;
}
}
else
{
if (slot++ == maxItems) // @NOTE: Scroll down
{
slot = 0;
}
}

m_pLocalPlayer->m_pInventory->selectSlot(slot);
}
#endif
}
}

Expand Down Expand Up @@ -1244,4 +1223,4 @@ void Minecraft::locateMultiplayer()
m_pRakNetInstance->pingForHosts(C_DEFAULT_PORT);
m_pNetEventCallback = new ClientSideNetworkHandler(this, m_pRakNetInstance);
#endif
}
}
22 changes: 21 additions & 1 deletion source/client/gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,26 @@ void Gui::handleClick(int clickID, int mouseX, int mouseY)
m_pMinecraft->m_pLocalPlayer->m_pInventory->selectSlot(slot);
}

void Gui::handleScroll(bool down)
{
int slot = m_pMinecraft->m_pLocalPlayer->m_pInventory->m_selectedHotbarSlot;

int maxItems = getNumUsableSlots() - 1;

if (down)
{
if (slot++ == maxItems)
slot = 0;
}
else
{
if (slot-- == 0)
slot = maxItems;
}

m_pMinecraft->m_pLocalPlayer->m_pInventory->selectSlot(slot);
}

void Gui::handleKeyPressed(int keyCode)
{
if (m_pMinecraft->getOptions()->isKey(KM_INVENTORY, keyCode))
Expand Down Expand Up @@ -553,4 +573,4 @@ RectangleArea Gui::getRectangleArea(bool b)
Minecraft::height - 24.0f / InvGuiScale,
centerX + hotbarWidthHalf,
Minecraft::height);
}
}
1 change: 1 addition & 0 deletions source/client/gui/Gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Gui : public GuiComponent
int getSlotIdAt(int mx, int my);
bool isInside(int mx, int my);
void handleClick(int id, int mx, int my);
void handleScroll(bool down);
void handleKeyPressed(int keyCode);
void renderMessages(bool bShowAll);
int getNumSlots(); // Gets the number of slots in the inventory. Includes the '...' if in touch mode.
Expand Down
6 changes: 6 additions & 0 deletions source/client/gui/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ void Screen::charInput(char chr)
}
}

void Screen::handleScroll(bool down)
{
}

static const char* g_panoramaList[] =
{
"gui/background/panorama_0.png",
Expand Down Expand Up @@ -430,6 +434,8 @@ void Screen::mouseEvent()
else
mouseReleased(m_width * pAction->_posX / Minecraft::width, m_height * pAction->_posY / Minecraft::height - 1 + getYOffset(), Mouse::getEventButton());
}
if (pAction->_buttonType == BUTTON_SCROLLWHEEL)
handleScroll(Mouse::getEventButtonState());
}

void Screen::renderBackground(int unk)
Expand Down
1 change: 1 addition & 0 deletions source/client/gui/Screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Screen : public GuiComponent
virtual void mouseReleased(int, int, int);
virtual void keyPressed(int);
virtual void charInput(char);
virtual void handleScroll(bool down);

// ported from 0.8
virtual void renderMenuBackground(float f);
Expand Down
7 changes: 7 additions & 0 deletions source/client/gui/components/RolledSelectionList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,10 @@ void RolledSelectionList::clickedHeader(int x, int y)
{

}

void RolledSelectionList::handleScroll(bool down)
{
float diff = 5.0f * (down ? -1.0f : 1.0f);
field_34 = field_30 = field_30 + diff;
field_28 = 0;
Copy link
Contributor

@uniformization uniformization Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably rename the unnamed fields that you use, only a suggestion though

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with this but I think these classes should be rewritten instead.

Copy link
Member

@iProgramMC iProgramMC Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with this but I think these classes should be rewritten instead.

Disagree. These classes implement the base of scrollable list views. I don't see how they need to be rewritten.

}
1 change: 1 addition & 0 deletions source/client/gui/components/RolledSelectionList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RolledSelectionList : public GuiComponent
virtual void renderBackground() = 0;
virtual void renderDecorations(int x, int y);
virtual void clickedHeader(int, int);
virtual void handleScroll(bool down);

int getItemAtXPositionRaw(int x);

Expand Down
8 changes: 7 additions & 1 deletion source/client/gui/components/ScrolledSelectionList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ void ScrolledSelectionList::renderScrollBackground()

void ScrolledSelectionList::checkInput(int mouseX, int mouseY)
{
int nItems = getNumberOfItems();
if (Mouse::isButtonDown(BUTTON_LEFT))
{
if (float(mouseY) >= field_C && float(mouseY) <= field_10 && abs(mouseY - field_28) > 5)
Expand Down Expand Up @@ -276,3 +275,10 @@ void ScrolledSelectionList::setRenderHeader(bool b, int i)
i = 0;
field_48 = i;
}

void ScrolledSelectionList::handleScroll(bool down)
{
float diff = 5.0f * (down ? -1.0f : 1.0f);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels weird. Like, when you scroll up, and then down, it seems to "continue" scrolling in the direction I was first scrolling in. Maybe it should just instantly set the velocity.

Also I think you should add a timeout of a few ms where the world select is just loose (as if you were holding a finger on the scroll area)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is for RolledSelectionList? Problem doesn't appear with ScrolledSelectionList.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, oops... Comment is related to RolledSelectionList.

field_34 -= diff;
field_38 += diff;
}
1 change: 1 addition & 0 deletions source/client/gui/components/ScrolledSelectionList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ScrolledSelectionList : public GuiComponent
virtual void checkInput(int mouseX, int mouseY);
virtual void onClickItem(int index, int mouseX, int mouseY);
virtual void renderScrollBackground();
virtual void handleScroll(bool down);

void setRenderHeader(bool, int);

Expand Down
4 changes: 4 additions & 0 deletions source/client/gui/screens/OptionsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ void OptionsScreen::buttonClicked(Button* pButton)
}
}

void OptionsScreen::handleScroll(bool down)
{
m_pList->handleScroll(down);
}

#else

Expand Down
1 change: 1 addition & 0 deletions source/client/gui/screens/OptionsScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class OptionsScreen : public Screen
void render(int, int, float) override;
void removed() override;
void buttonClicked(Button* pButton) override;
void handleScroll(bool down) override;

private:
OptionList* m_pList;
Expand Down
5 changes: 5 additions & 0 deletions source/client/gui/screens/SelectWorldScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ void SelectWorldScreen::buttonClicked(Button* pButton)
}
}

void SelectWorldScreen::handleScroll(bool down)
{
m_pWorldSelectionList->handleScroll(down);
}

bool SelectWorldScreen::isIndexValid(int idx)
{
if (!m_pWorldSelectionList)
Expand Down
1 change: 1 addition & 0 deletions source/client/gui/screens/SelectWorldScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SelectWorldScreen : public Screen
void render(int mouseX, int mouseY, float f) override;
bool handleBackEvent(bool b) override;
void buttonClicked(Button* pButton) override;
void handleScroll(bool down) override;

bool isIndexValid(int);
std::string getUniqueLevelName(const std::string& in);
Expand Down