Fix releasing alt after mouse wheel scroll, resetting hotbar index to hotbar 0 or 1. (Thanks AndrewPH)

This commit is contained in:
UnknownShadow200 2017-10-18 23:51:43 +11:00
parent a893130bd4
commit 14b7cadaa0
8 changed files with 69 additions and 24 deletions

View File

@ -25,7 +25,7 @@ namespace ClassicalSharp.Gui.Widgets {
IsometricBlockDrawer drawer = new IsometricBlockDrawer();
public override void Init() { Reposition(); }
public override void Dispose() { }
public override void Dispose() { }
public override void Render(double delta) {
RenderHotbarOutline();
RenderHotbarBlocks();
@ -141,5 +141,27 @@ namespace ClassicalSharp.Gui.Widgets {
}
return false;
}
float deltaAcc;
public override bool HandlesMouseScroll(float delta) {
if (game.Input.AltDown) {
int index = game.Inventory.Offset / Inventory.BlocksPerRow;
game.Inventory.Offset = ScrolledIndex(delta, index, 1) * Inventory.BlocksPerRow;
altHandled = true;
} else {
game.Inventory.SelectedIndex = ScrolledIndex(delta, game.Inventory.SelectedIndex, -1);
}
return true;
}
int ScrolledIndex(float delta, int index, int dir) {
int steps = Utils.AccumulateWheelDelta(ref deltaAcc, delta);
const int blocksPerRow = Inventory.BlocksPerRow;
index += (dir * steps) % blocksPerRow;
if (index < 0) index += blocksPerRow;
if (index >= blocksPerRow) index -= blocksPerRow;
return index;
}
}
}

View File

@ -131,7 +131,6 @@ namespace ClassicalSharp {
}
}
float deltaAcc;
void MouseWheelChanged(object sender, MouseWheelEventArgs e) {
if (game.Gui.ActiveScreen.HandlesMouseScroll(e.Delta)) return;
@ -139,29 +138,8 @@ namespace ClassicalSharp {
bool hotbar = AltDown || ControlDown || ShiftDown;
if ((!hotbar && game.Camera.Zoom(e.Delta)) || DoFovZoom(e.Delta) || !inv.CanChangeHeldBlock)
return;
ScrollHotbar(e.Delta);
}
void ScrollHotbar(float delta) {
Inventory inv = game.Inventory;
if (AltDown) {
int index = inv.Offset / Inventory.BlocksPerRow;
inv.Offset = ScrolledIndex(delta, index) * Inventory.BlocksPerRow;
} else {
inv.SelectedIndex = ScrolledIndex(delta, inv.SelectedIndex);
}
}
int ScrolledIndex(float delta, int currentIndex) {
int steps = Utils.AccumulateWheelDelta(ref deltaAcc, delta);
const int blocksPerRow = Inventory.BlocksPerRow;
int diff = -steps % blocksPerRow;
int index = currentIndex + diff;
if (index < 0) index += blocksPerRow;
if (index >= blocksPerRow) index -= blocksPerRow;
return index;
game.Gui.hudScreen.hotbar.HandlesMouseScroll(e.Delta);
}
void KeyPressHandler(object sender, KeyPressEventArgs e) {

View File

@ -300,6 +300,7 @@
<ClCompile Include="Picking.c" />
<ClCompile Include="Program.c" />
<ClCompile Include="Random.c" />
<ClCompile Include="Screens.c" />
<ClCompile Include="SelectionBox.c" />
<ClCompile Include="SkyboxRenderer.c" />
<ClCompile Include="Stream.c" />

View File

@ -572,5 +572,8 @@
<ClCompile Include="Utils.c">
<Filter>Source Files\Utils</Filter>
</ClCompile>
<ClCompile Include="Screens.c">
<Filter>Source Files\2D</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -80,6 +80,10 @@ bool Key_GetPressed(Key key);
/* Sets whether the given key is currently being pressed. */
void Key_SetPressed(Key key, bool pressed);
#define Key_IsAltPressed() (Key_GetPressed(Key_AltLeft) || Key_GetPressed(Key_AltRight))
#define Key_IsControlPressed() (Key_GetPressed(Key_ControlLeft) || Key_GetPressed(Key_ControlRight))
#define Key_IsShiftPressed() (Key_GetPressed(Key_ShiftLeft) || Key_GetPressed(Key_ShiftRight))
/* Gets whether key repeating is on or not. If on (desirable for text input), multiple KeyDowns (varies by OS)
are generated for the same key when it is held down for a period of time. Should be off for game input. */
bool Key_KeyRepeat;

1
src/Client/Screens.c Normal file
View File

@ -0,0 +1 @@
#include "Screen.h"

View File

@ -353,6 +353,17 @@ void HotbarWidget_RepositionSelectionTexture(HotbarWidget* widget) {
widget->SelTex = Texture_FromRec(0, 0, y, hSize, vSize, rec);
}
Int32 HotbarWidget_ScrolledIndex(HotbarWidget* widget, Real32 delta, Int32 index, Int32 dir) {
Int32 steps = Utils_AccumulateWheelDelta(&widget->ScrollAcc, delta);
index += (dir * steps) % INVENTORY_BLOCKS_PER_HOTBAR;
if (index < 0) index += INVENTORY_BLOCKS_PER_HOTBAR;
if (index >= INVENTORY_BLOCKS_PER_HOTBAR) {
index -= INVENTORY_BLOCKS_PER_HOTBAR;
}
return index;
}
void HotbarWidget_Reposition(Widget* elem) {
HotbarWidget* widget = (HotbarWidget*)elem;
Real32 scale = Game_GetHotbarScale();
@ -438,10 +449,34 @@ bool HotbarWidget_HandlesMouseDown(GuiElement* elem, Int32 x, Int32 y, MouseButt
return false;
}
bool HotbarWidget_HandlesMouseScroll(GuiElement* elem, Real32 delta) {
HotbarWidget* widget = (HotbarWidget*)elem;
if (Key_IsAltPressed()) {
Int32 index = Inventory_Offset / INVENTORY_BLOCKS_PER_HOTBAR;
index = HotbarWidget_ScrolledIndex(widget, delta, index, 1);
Inventory_SetOffset(index * INVENTORY_BLOCKS_PER_HOTBAR);
widget->AltHandled = true;
} else {
Int32 index = HotbarWidget_ScrolledIndex(widget, delta, Inventory_SelectedIndex, -1);
Inventory_SetSelectedIndex(index);
}
return true;
}
void HotbarWidget_Create(HotbarWidget* widget) {
Widget_Init(&widget->Base);
widget->Base.HorAnchor = ANCHOR_CENTRE;
widget->Base.VerAnchor = ANCHOR_BOTTOM_OR_RIGHT;
widget->Base.Base.Init = HotbarWidget_Init;
widget->Base.Base.Render = HotbarWidget_Render;
widget->Base.Base.Free = HotbarWidget_Free;
widget->Base.Reposition = HotbarWidget_Reposition;
widget->Base.Base.HandlesKeyDown = HotbarWidget_HandlesKeyDown;
widget->Base.Base.HandlesKeyUp = HotbarWidget_HandlesKeyUp;
widget->Base.Base.HandlesMouseDown = HotbarWidget_HandlesMouseDown;
widget->Base.Base.HandlesMouseScroll = HotbarWidget_HandlesMouseScroll;
}

View File

@ -62,6 +62,7 @@ typedef struct HotbarWidget_ {
Real32 BarHeight, SelBlockSize, ElemSize;
Real32 BarXOffset, BorderSize;
bool AltHandled;
Real32 ScrollAcc;
} HotbarWidget;
void HotbarWidget_Create(HotbarWidget* widget);