* Outline all the Mouse and Keyboard code.

This commit is contained in:
iProgramInCpp
2023-08-11 11:19:26 +03:00
parent cf304095ed
commit 34fc30f08a
14 changed files with 191 additions and 83 deletions

View File

@@ -66,8 +66,8 @@ int g_MousePosX, g_MousePosY;
void UpdateMouse()
{
Mouse::_x = g_MousePosX;
Mouse::_y = g_MousePosY;
Mouse::setX(g_MousePosX);
Mouse::setY(g_MousePosY);
}
extern bool g_bIsMenuBackgroundAvailable;

View File

@@ -379,34 +379,30 @@ void Minecraft::tickInput()
if (!m_pLocalPlayer)
return;
bool bIsInGUI = m_gui.isInside(Mouse::_x, Mouse::_y);
bool bIsInGUI = m_gui.isInside(Mouse::getX(), Mouse::getY());
while (Mouse::_index + 1 < Mouse::_inputs.size())
while (Mouse::next())
{
Mouse::_index++;
if (getTimeMs() - field_2B4 > 200)
continue;
if (Mouse::_buttonStates[1])
m_gui.handleClick(1, Mouse::_x, Mouse::_y);
if (Mouse::isButtonDown(1))
m_gui.handleClick(1, Mouse::getX(), Mouse::getY());
if (!bIsInGUI && m_options.field_19)
{
MouseInput& input = Mouse::_inputs[Mouse::_index];
if (input.field_0 == 1 && input.field_4 == 1)
if (Mouse::getEventButton() == 1 && Mouse::getEventButtonState() == 1)
{
handleMouseClick(1);
field_DAC = field_DA8;
}
if (input.field_0 == 2 && input.field_4 == 1)
if (Mouse::getEventButton() == 2 && Mouse::getEventButtonState() == 1)
{
handleMouseClick(2);
field_DAC = field_DA8;
}
#ifdef ENH_ALLOW_SCROLL_WHEEL
if (input.field_0 == 3)
if (Mouse::getEventButton() == 3)
{
int slot = m_pLocalPlayer->m_pInventory->m_SelectedHotbarSlot;
@@ -416,7 +412,7 @@ void Minecraft::tickInput()
#define MAX_ITEMS (C_MAX_HOTBAR_ITEMS - 2)
#endif
if (input.field_4 > 0) // @NOTE: Scroll up
if (Mouse::getEventButtonState() > 0) // @NOTE: Scroll up
{
if (slot-- == 0)
{
@@ -435,16 +431,12 @@ void Minecraft::tickInput()
}
#endif
}
}
while (Keyboard::_index + 1 < Keyboard::_inputs.size())
while (Keyboard::next())
{
Keyboard::_index++;
Keyboard::Input& input = Keyboard::_inputs[Keyboard::_index];
int keyCode = input.field_4;
bool bPressed = input.field_0 == 1;
int keyCode = Keyboard::getEventKey();
bool bPressed = Keyboard::getEventKeyState() == 1;
m_pLocalPlayer->m_pKeyboardInput->setKey(keyCode, bPressed);
@@ -502,10 +494,10 @@ void Minecraft::tickInput()
if (m_options.field_19)
{
if (!Mouse::_buttonStates[1] || bIsInGUI)
if (!Mouse::isButtonDown(1) || bIsInGUI)
goto label_12;
}
else if (Keyboard::_states[m_options.m_keyBinds[Options::DESTROY].value] != 1)
else if (Keyboard::isKeyDown(m_options.m_keyBinds[Options::DESTROY].value))
{
goto label_12;
}
@@ -530,10 +522,8 @@ void Minecraft::tickInput()
field_2B4 = getTimeMs();
Keyboard::_inputs.clear();
Keyboard::_index = -1;
Mouse::_inputs.clear();
Mouse::_index = -1;
Keyboard::reset();
Mouse::reset();
}
void Minecraft::tickMouse()

View File

@@ -104,8 +104,7 @@ void NinecraftApp::update()
eglSwapBuffers(field_8, field_10);
#endif
Mouse::_xOld = Mouse::_x;
Mouse::_yOld = Mouse::_y;
Mouse::reset2();
updateStats();
}

View File

@@ -293,42 +293,28 @@ void Screen::updateEvents()
{
if (field_10) return;
for (int i = Mouse::_index + 1; i<int(Mouse::_inputs.size()); i++)
{
Mouse::_index = i;
while (Mouse::next())
mouseEvent();
}
for (int i = Keyboard::_index + 1; i<int(Keyboard::_inputs.size()); i++)
{
Keyboard::_index = i;
while (Keyboard::next())
keyboardEvent();
}
}
void Screen::keyboardEvent()
{
// @UB: This probably behaves in an unexpected way if _inputs is empty
#ifndef ORIGINAL_CODE
if (Keyboard::_inputs.empty() || Keyboard::_index < 0)
return;
#endif
if (Keyboard::_inputs[Keyboard::_index].field_0)
keyPressed(Keyboard::_inputs[Keyboard::_index].field_4);
if (Keyboard::getEventKeyState())
keyPressed(Keyboard::getEventKey());
}
void Screen::mouseEvent()
{
MouseInput& inp = Mouse::_inputs[Mouse::_index];
if (1 <= inp.field_0 && inp.field_0 <= 2)
MouseAction* pAction = Mouse::getEvent();
if (pAction->isButton())
{
if (inp.field_4 == 1)
mouseClicked(m_width * Mouse::_x / Minecraft::width, m_height * Mouse::_y / Minecraft::height - 1, inp.field_0);
if (Mouse::getEventButtonState())
mouseClicked (m_width * pAction->field_8 / Minecraft::width, m_height * pAction->field_C / Minecraft::height - 1, Mouse::getEventButton());
else
mouseReleased(m_width * Mouse::_x / Minecraft::width, m_height * Mouse::_y / Minecraft::height - 1, inp.field_0);
mouseReleased(m_width * pAction->field_8 / Minecraft::width, m_height * pAction->field_C / Minecraft::height - 1, Mouse::getEventButton());
}
}

View File

@@ -87,7 +87,7 @@ void RolledSelectionList::render(int mouseX, int mouseY, float f)
int nItems = getNumberOfItems();
// @TODO: fix gotos.
if (!Mouse::_buttonStates[1])
if (!Mouse::isButtonDown(1))
{
if (field_28 < 0)
{

View File

@@ -71,7 +71,7 @@ void ScrolledSelectionList::render(int mouseX, int mouseY, float f)
renderBackground();
int nItems = getNumberOfItems();
if (Mouse::_buttonStates[1])
if (Mouse::isButtonDown(1))
{
if (float(mouseY) >= field_C && float(mouseY) <= field_10 && mouseY != field_28)
{

View File

@@ -42,7 +42,7 @@ void WorldSelectionList::tick()
{
RolledSelectionList::tick();
field_D0++;
if (Mouse::_buttonStates[1] || !field_28)
if (Mouse::isButtonDown(1) || !field_28)
return;
m_selectedIndex = -1;

View File

@@ -40,7 +40,7 @@ void SelectWorldScreen::init()
m_buttons.push_back(&m_btnBack);
m_buttons.push_back(&m_btnDelete);
field_12C = Mouse::_buttonStates[1] == 0;
field_12C = Mouse::getButtonState(1) == 0;
m_buttonTabList.push_back(&m_btnUnknown);
m_buttonTabList.push_back(&m_btnDelete);
@@ -168,7 +168,7 @@ void SelectWorldScreen::render(int mouseX, int mouseY, float f)
else
{
m_pWorldSelectionList->render(0, 0, f);
field_12C = Mouse::_buttonStates[1] == 0;
field_12C = Mouse::getButtonState(1) == 0;
}
Screen::render(mouseX, mouseY, f);

View File

@@ -10,7 +10,7 @@
#include "GameMods.hpp"
std::vector<Keyboard::Input> Keyboard::_inputs;
std::vector<KeyboardAction> Keyboard::_inputs;
int Keyboard::_index = -1;
int Keyboard::_states[KEYBOARD_STATES_SIZE];
@@ -23,10 +23,40 @@ void Keyboard::feed(int down, int key)
}
#endif
Input i;
i.field_0 = down;
i.field_4 = uint8_t(key);
_inputs.push_back(i);
_inputs.push_back(KeyboardAction(key, down));
_states[key] = down;
}
bool Keyboard::next()
{
if (_index + 1 >= _inputs.size())
return false;
_index++;
return true;
}
int Keyboard::getEventKey()
{
return _inputs[_index].field_4;
}
int Keyboard::getEventKeyState()
{
return _inputs[_index].field_0;
}
bool Keyboard::isKeyDown(int keyCode)
{
if (keyCode < 0 || keyCode >= KEYBOARD_STATES_SIZE)
return false;
return _states[keyCode] == 1;
}
void Keyboard::reset()
{
_inputs.clear();
_index = -1;
}

View File

@@ -16,21 +16,31 @@
#define KEYBOARD_STATES_SIZE 256
struct KeyboardAction
{
int field_0;
uint8_t field_4;
KeyboardAction(uint8_t key, int state)
{
field_0 = state;
field_4 = key;
}
};
class Keyboard
{
public:
//@TODO: Rename this to KeyboardAction
struct Input
{
int field_0;
uint8_t field_4;
};
static void feed(int down, int key);
static bool next();
static int getEventKey();
static int getEventKeyState();
static bool isKeyDown(int keyCode);
static void reset();
static std::vector<Input> _inputs;
private:
static std::vector<KeyboardAction> _inputs;
static int _states[KEYBOARD_STATES_SIZE];
static int _index;
// likely inlined
static void feed(int down, int key);
};

View File

@@ -8,13 +8,13 @@
#include "Mouse.hpp"
std::vector<MouseInput> Mouse::_inputs;
std::vector<MouseAction> Mouse::_inputs;
int Mouse::_index, Mouse::_x, Mouse::_y;
int Mouse::_xOld, Mouse::_yOld, Mouse::_buttonStates[3];
void Mouse::feed(int x1, int x2, int x3, int x4)
{
_inputs.push_back(MouseInput(x1, x2, x3, x4));
_inputs.push_back(MouseAction(x1, x2, x3, x4));
if (x1 >= 3)
return;
@@ -27,3 +27,72 @@ void Mouse::feed(int x1, int x2, int x3, int x4)
_x = x3;
_y = x4;
}
short Mouse::getX()
{
return short(_x);
}
short Mouse::getY()
{
return short(_y);
}
bool Mouse::next()
{
if (_index + 1 >= int(_inputs.size()))
return false;
_index++;
return true;
}
int Mouse::getEventButton()
{
return _inputs[_index].field_0;
}
bool Mouse::isButtonDown(int btn)
{
return _buttonStates[btn];
}
void Mouse::reset()
{
_inputs.clear();
_index = -1;
}
MouseAction* Mouse::getEvent()
{
return &_inputs[_index];
}
int Mouse::getButtonState(int btn)
{
if (btn <= 0 || btn >= 3)
return 0;
return _buttonStates[btn];
}
void Mouse::setX(int x)
{
_x = x;
}
void Mouse::setY(int y)
{
_y = y;
}
void Mouse::reset2()
{
_xOld = _x;
_yOld = _y;
}
int Mouse::getEventButtonState()
{
return _inputs[_index].field_4;
}

View File

@@ -10,27 +10,32 @@
#include <vector>
struct MouseInput
struct MouseAction
{
int field_0;
int field_4;
int field_8;
int field_C;
MouseInput()
MouseAction()
{
field_0 = 0;
field_4 = 0;
field_8 = 0;
field_C = 0;
}
MouseInput(int x1, int x2, int x3, int x4)
MouseAction(int x1, int x2, int x3, int x4)
{
field_0 = x1;
field_4 = x2;
field_8 = x3;
field_C = x4;
}
bool isButton()
{
return field_0 == 1 || field_0 == 2;
}
};
class Mouse
@@ -38,10 +43,23 @@ class Mouse
public:
static void feed(int, int, int, int);
static short getX();
static short getY();
static bool next();
static bool isButtonDown(int btn);
static int getButtonState(int btn);
static int getEventButton();
static int getEventButtonState();
static MouseAction* getEvent();
static void setX(int x);
static void setY(int y);
static void reset();
static void reset2();
// @TODO: There's plenty of inlined code here. Out-line it.
public:
static std::vector<MouseInput> _inputs;
private:
static std::vector<MouseAction> _inputs;
static int _index;
static int _x, _y;
static int _xOld, _yOld;

View File

@@ -579,6 +579,11 @@ void GameRenderer::render(float f)
pMC->m_pLocalPlayer->turn(diff_field_84 * field_7C, diff_field_84 * multPitch * field_80);
}
int mouseX = int(Mouse::getX() * Gui::InvGuiScale);
int mouseY = int(Mouse::getY() * Gui::InvGuiScale);
// note: Multitouch code here
if (m_pMinecraft->isLevelGenerated())
{
if (t_keepPic < 0)
@@ -590,7 +595,7 @@ void GameRenderer::render(float f)
return;
}
m_pMinecraft->m_gui.render(f, m_pMinecraft->m_pScreen != nullptr, int(Mouse::_x * Gui::InvGuiScale), int(Mouse::_y * Gui::InvGuiScale));
m_pMinecraft->m_gui.render(f, m_pMinecraft->m_pScreen != nullptr, mouseX, mouseY);
}
}
else
@@ -606,11 +611,12 @@ void GameRenderer::render(float f)
if (m_pMinecraft->m_pScreen)
{
glClear(GL_ACCUM);
m_pMinecraft->m_pScreen->render(int(Mouse::_x * Gui::InvGuiScale), int(Mouse::_y * Gui::InvGuiScale), f);
m_pMinecraft->m_pScreen->render(mouseX, mouseY, f);
if (m_pMinecraft->m_pScreen && !m_pMinecraft->m_pScreen->isInGameScreen())
{
#ifdef ORIGINAL_CODE
// force some lag for some reason. I guess it's to make it spend more time actually generating the world?
sleepMs(15);
#endif
}

View File

@@ -73,9 +73,9 @@ void LocalPlayer::calculateFlight(float x, float y, float z)
float z1 = f1 * z;
float y1 = 0.0f;
if (Keyboard::_states[AKEYCODE_BUTTON_R1] == 1)
if (Keyboard::isKeyDown(AKEYCODE_BUTTON_R1))
y1 = f1 * 0.2f;
if (Keyboard::_states[AKEYCODE_BUTTON_L1] == 1)
if (Keyboard::isKeyDown(AKEYCODE_BUTTON_L1))
y1 = f1 * -0.2f;
field_BFC += x1;