mirror of
https://github.com/celisej567/mcpe.git
synced 2026-09-11 20:11:18 +03:00
* Initial commit.
:)
This commit is contained in:
92
source/UserInput/Controller.cpp
Normal file
92
source/UserInput/Controller.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "Controller.hpp"
|
||||
|
||||
bool Controller::isTouchedValues[2];
|
||||
float Controller::stickValuesX[2];
|
||||
float Controller::stickValuesY[2];
|
||||
const float Controller_unk_1[3] = { 0.0f, 0.64f, -0.64f };
|
||||
|
||||
bool Controller::isValidStick(int stickNo)
|
||||
{
|
||||
// We have 2 'sticks' on the Xperia Play
|
||||
return stickNo >= 1 && stickNo <= 2;
|
||||
}
|
||||
|
||||
float Controller::linearTransform(float a1, float a2, float a3, bool b)
|
||||
{
|
||||
float x1 = fabsf(a2);
|
||||
float x2 = a1;
|
||||
float x3 = fabsf(x1);
|
||||
if (x3 >= x2)
|
||||
return 0.0f;
|
||||
|
||||
float x4 = (a1 - x1) * a3;
|
||||
if (b)
|
||||
{
|
||||
float x5 = fabsf(x4);
|
||||
|
||||
if (x5 > 1.0f)
|
||||
{
|
||||
x4 = -1.0;
|
||||
if (x4 > 0.0f)
|
||||
x4 = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
return x4;
|
||||
}
|
||||
|
||||
void Controller::feed(int stickNo, int touched, float x, float y)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return;
|
||||
|
||||
int index = (x >= 0.0f) ? 1 : 0;
|
||||
|
||||
// maybe the 2 'touch sticks' are actually internally 1 single surface??
|
||||
|
||||
isTouchedValues[index] = touched != 0;
|
||||
stickValuesX[index] = linearTransform(x + Controller_unk_1[index + 1], 0.0f, 2.78f, true);
|
||||
stickValuesY[index] = y;
|
||||
}
|
||||
|
||||
float Controller::getX(int stickNo)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return 0.0f;
|
||||
|
||||
return stickValuesX[stickNo - 1];
|
||||
}
|
||||
|
||||
float Controller::getY(int stickNo)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return 0.0f;
|
||||
|
||||
return stickValuesY[stickNo - 1];
|
||||
}
|
||||
|
||||
float Controller::getTransformedX(int stickNo, float a2, float a3, bool b)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return 0.0f;
|
||||
|
||||
// @BUG: subtracting by 1? What if we're trying to grab stick 0?
|
||||
return linearTransform(stickValuesX[stickNo - 1], a2, a3, b);
|
||||
}
|
||||
|
||||
float Controller::getTransformedY(int stickNo, float a2, float a3, bool b)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return 0.0f;
|
||||
|
||||
// @BUG: subtracting by 1? What if we're trying to grab stick 0?
|
||||
return linearTransform(stickValuesY[stickNo - 1], a2, a3, b);
|
||||
}
|
||||
|
||||
bool Controller::isTouched(int stickNo)
|
||||
{
|
||||
if (!isValidStick(stickNo))
|
||||
return false;
|
||||
|
||||
return isTouchedValues[stickNo - 1];
|
||||
}
|
||||
21
source/UserInput/Controller.hpp
Normal file
21
source/UserInput/Controller.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Controller
|
||||
{
|
||||
public:
|
||||
static bool isValidStick(int stickNo);
|
||||
static float linearTransform(float, float, float, bool);
|
||||
static void feed(int stickNo, int touched, float x, float y);
|
||||
static float getX(int stickNo);
|
||||
static float getY(int stickNo);
|
||||
static float getTransformedX(int stickNo, float a2, float a3, bool b);
|
||||
static float getTransformedY(int stickNo, float a2, float a3, bool b);
|
||||
static bool isTouched(int stickNo);
|
||||
|
||||
public:
|
||||
static bool isTouchedValues[2];
|
||||
static float stickValuesX[2], stickValuesY[2];
|
||||
};
|
||||
|
||||
114
source/UserInput/ControllerTurnInput.cpp
Normal file
114
source/UserInput/ControllerTurnInput.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "ControllerTurnInput.hpp"
|
||||
#include "Controller.hpp"
|
||||
|
||||
ITurnInput::Delta ControllerTurnInput::getTurnDelta()
|
||||
{
|
||||
bool isTouched = Controller::isTouched(m_stickNo);
|
||||
float deltaX, deltaY;
|
||||
|
||||
if (field_8 == 1)
|
||||
{
|
||||
float deltaTime = getDeltaTime(), dx, dy;
|
||||
if (isTouched)
|
||||
{
|
||||
dx = Controller::getX(m_stickNo);
|
||||
dy = Controller::getY(m_stickNo);
|
||||
}
|
||||
else
|
||||
{
|
||||
dx = field_10 * 0.7f;
|
||||
dy = field_14 * 0.7f;
|
||||
}
|
||||
|
||||
field_10 = dx;
|
||||
field_14 = dy;
|
||||
|
||||
float xs = dx >= 0.0f ? 0.1f : -0.1f, xt;
|
||||
|
||||
if (fabsf(dx) <= 0.1f)
|
||||
{
|
||||
xt = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
xs = dx - xs;
|
||||
xt = 250.0f;
|
||||
xt = xs * xt;
|
||||
}
|
||||
|
||||
float ys = dy >= 0.0f ? 0.1f : -0.1f, yt;
|
||||
|
||||
if (fabsf(dy) <= 0.1f)
|
||||
{
|
||||
yt = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
ys = dy - ys;
|
||||
yt = 200.0f;
|
||||
yt = ys * yt;
|
||||
}
|
||||
|
||||
deltaX = deltaTime * xt;
|
||||
deltaY = deltaTime * -yt;
|
||||
}
|
||||
else if (field_8 != 2 || !field_18 && !isTouched)
|
||||
{
|
||||
deltaX = 0.0f;
|
||||
deltaY = -0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
float sx = Controller::getX(m_stickNo);
|
||||
float sy = Controller::getY(m_stickNo);
|
||||
|
||||
getDeltaTime(); //@NOTE: call isn't useless since it updates m_prevTime
|
||||
|
||||
if (!field_18)
|
||||
{
|
||||
field_10 = sx;
|
||||
field_14 = sy;
|
||||
}
|
||||
|
||||
if (isTouched)
|
||||
{
|
||||
float x1 = sx - field_10;
|
||||
float x2 = 0.0f;
|
||||
float x3;
|
||||
field_10 = sx;
|
||||
|
||||
// @HUH: what the hell, decompiler?
|
||||
if (x1 >= 0.0f)
|
||||
x3 = 0.0f;
|
||||
else
|
||||
x3 = -0.0f;
|
||||
|
||||
float x4 = sy - field_14;
|
||||
field_14 = sy;
|
||||
|
||||
if (fabsf(x1) > 0.0f)
|
||||
x2 = x1 - x3;
|
||||
|
||||
float ndy = 0.0f, x5;
|
||||
if (x4 >= 0.0f)
|
||||
x5 = 0.0f;
|
||||
else
|
||||
x5 = -0.0f;
|
||||
|
||||
float x6 = 0.0f;
|
||||
if (fabsf(x4) > 0.0f)
|
||||
x6 = x4 - x5;
|
||||
|
||||
deltaX = x2 * 100.0f;
|
||||
deltaY = -x6 * 100.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
deltaX = 0.0f;
|
||||
deltaY = -0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
field_18 = isTouched;
|
||||
return Delta(deltaX, deltaY);
|
||||
}
|
||||
17
source/UserInput/ControllerTurnInput.hpp
Normal file
17
source/UserInput/ControllerTurnInput.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "ITurnInput.hpp"
|
||||
|
||||
class ControllerTurnInput : public ITurnInput
|
||||
{
|
||||
public:
|
||||
ITurnInput::Delta getTurnDelta();
|
||||
|
||||
private:
|
||||
int field_8 = 2;
|
||||
int m_stickNo = 2;
|
||||
float field_10 = 0.0f;
|
||||
float field_14 = 0.0f;
|
||||
bool field_18 = false;
|
||||
};
|
||||
|
||||
14
source/UserInput/ITurnInput.cpp
Normal file
14
source/UserInput/ITurnInput.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "ITurnInput.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
float ITurnInput::getDeltaTime()
|
||||
{
|
||||
if (m_prevTime == -1.0f)
|
||||
m_prevTime = getTimeS();
|
||||
|
||||
float newTime = getTimeS();
|
||||
float delta = newTime - m_prevTime;
|
||||
m_prevTime = newTime;
|
||||
|
||||
return delta;
|
||||
}
|
||||
21
source/UserInput/ITurnInput.hpp
Normal file
21
source/UserInput/ITurnInput.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
class ITurnInput
|
||||
{
|
||||
public:
|
||||
struct Delta
|
||||
{
|
||||
float x = 0.0f, y = 0.0f;
|
||||
Delta() {}
|
||||
Delta(float x, float y) : x(x), y(y) {}
|
||||
};
|
||||
|
||||
public:
|
||||
float getDeltaTime();
|
||||
virtual ~ITurnInput();
|
||||
virtual Delta getTurnDelta() = 0;
|
||||
|
||||
private:
|
||||
float m_prevTime = -1.0f;
|
||||
};
|
||||
|
||||
15
source/UserInput/Keyboard.cpp
Normal file
15
source/UserInput/Keyboard.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
std::vector<Keyboard::Input> Keyboard::_inputs;
|
||||
int Keyboard::_index = -1;
|
||||
int Keyboard::_states[256];
|
||||
|
||||
void Keyboard::feed(int down, int key)
|
||||
{
|
||||
Input i;
|
||||
i.field_0 = down;
|
||||
i.field_4 = uint8_t(key);
|
||||
_inputs.push_back(i);
|
||||
|
||||
_states[key] = down;
|
||||
}
|
||||
22
source/UserInput/Keyboard.hpp
Normal file
22
source/UserInput/Keyboard.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Keyboard
|
||||
{
|
||||
public:
|
||||
//@TODO: Rename this to KeyboardAction
|
||||
struct Input
|
||||
{
|
||||
int field_0;
|
||||
uint8_t field_4;
|
||||
};
|
||||
|
||||
static std::vector<Input> _inputs;
|
||||
static int _states[256];
|
||||
static int _index;
|
||||
|
||||
// likely inlined
|
||||
static void feed(int down, int key);
|
||||
};
|
||||
|
||||
52
source/UserInput/KeyboardInput.cpp
Normal file
52
source/UserInput/KeyboardInput.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "KeyboardInput.hpp"
|
||||
|
||||
KeyboardInput::KeyboardInput(Options* pOpts)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
m_keys[i] = false;
|
||||
|
||||
m_pOptions = pOpts;
|
||||
}
|
||||
|
||||
void KeyboardInput::releaseAllKeys()
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
m_keys[i] = false;
|
||||
}
|
||||
|
||||
void KeyboardInput::setKey(int keyCode, bool b)
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
if (m_pOptions->m_keyBinds[0].value == keyCode) index = 0;
|
||||
if (m_pOptions->m_keyBinds[2].value == keyCode) index = 1;
|
||||
if (m_pOptions->m_keyBinds[1].value == keyCode) index = 2;
|
||||
if (m_pOptions->m_keyBinds[3].value == keyCode) index = 3;
|
||||
if (m_pOptions->m_keyBinds[4].value == keyCode) index = 4;
|
||||
if (m_pOptions->m_keyBinds[9].value == keyCode) index = 5;
|
||||
|
||||
if (index == -1)
|
||||
return;
|
||||
|
||||
m_keys[index] = b;
|
||||
}
|
||||
|
||||
void KeyboardInput::tick(/* Player* */)
|
||||
{
|
||||
m_horzInput = 0.0f;
|
||||
m_vertInput = 0.0f;
|
||||
|
||||
if (m_keys[FORWARD]) m_vertInput += 1.0f;
|
||||
if (m_keys[BACKWARD]) m_vertInput -= 1.0f;
|
||||
if (m_keys[LEFT]) m_horzInput += 1.0f;
|
||||
if (m_keys[RIGHT]) m_horzInput -= 1.0f;
|
||||
|
||||
m_bJumpButton = m_keys[JUMP];
|
||||
m_bSneakButton = m_keys[SNEAK];
|
||||
|
||||
if (m_keys[SNEAK])
|
||||
{
|
||||
m_horzInput = m_horzInput * 0.3f;
|
||||
m_vertInput = m_vertInput * 0.3f;
|
||||
}
|
||||
}
|
||||
34
source/UserInput/KeyboardInput.hpp
Normal file
34
source/UserInput/KeyboardInput.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "Options.hpp"
|
||||
|
||||
class KeyboardInput
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
FORWARD,
|
||||
BACKWARD,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
JUMP,
|
||||
SNEAK,
|
||||
};
|
||||
|
||||
public:
|
||||
KeyboardInput(Options*);
|
||||
|
||||
virtual void releaseAllKeys();
|
||||
virtual void setKey(int index, bool b);
|
||||
virtual void tick(/* Player* */);
|
||||
|
||||
public:
|
||||
float m_horzInput = 0.0f;
|
||||
float m_vertInput = 0.0f;
|
||||
bool field_C = false;
|
||||
bool m_bJumpButton = false;
|
||||
bool m_bSneakButton = false;
|
||||
bool m_keys[10];
|
||||
Options* m_pOptions = nullptr;
|
||||
};
|
||||
|
||||
18
source/UserInput/Mouse.cpp
Normal file
18
source/UserInput/Mouse.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Mouse.hpp"
|
||||
|
||||
std::vector<MouseInput> 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));
|
||||
|
||||
if (x1)
|
||||
Mouse::_buttonStates[x1] = x2;
|
||||
|
||||
_xOld = _x;
|
||||
_yOld = _y;
|
||||
_x = x3;
|
||||
_y = x4;
|
||||
}
|
||||
36
source/UserInput/Mouse.hpp
Normal file
36
source/UserInput/Mouse.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct MouseInput
|
||||
{
|
||||
int field_0 = 0;
|
||||
int field_4 = 0;
|
||||
int field_8 = 0;
|
||||
int field_C = 0;
|
||||
|
||||
MouseInput() {}
|
||||
MouseInput(int x1, int x2, int x3, int x4)
|
||||
{
|
||||
field_0 = x1;
|
||||
field_4 = x2;
|
||||
field_8 = x3;
|
||||
field_C = x4;
|
||||
}
|
||||
};
|
||||
|
||||
class Mouse
|
||||
{
|
||||
public:
|
||||
static void feed(int, int, int, int);
|
||||
|
||||
// @TODO: There's plenty of inlined code here. Out-line it.
|
||||
|
||||
public:
|
||||
static std::vector<MouseInput> _inputs;
|
||||
static int _index;
|
||||
static int _x, _y;
|
||||
static int _xOld, _yOld;
|
||||
static int _buttonStates[3];
|
||||
};
|
||||
|
||||
26
source/UserInput/MouseTurnInput.cpp
Normal file
26
source/UserInput/MouseTurnInput.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "MouseTurnInput.hpp"
|
||||
#include "Mouse.hpp"
|
||||
|
||||
constexpr float C_SENSITIVITY = 0.9f;
|
||||
|
||||
MouseTurnInput::MouseTurnInput(Minecraft* pMC)
|
||||
{
|
||||
m_pMinecraft = pMC;
|
||||
}
|
||||
|
||||
ITurnInput::~ITurnInput()
|
||||
{
|
||||
}
|
||||
|
||||
ITurnInput::Delta MouseTurnInput::getTurnDelta()
|
||||
{
|
||||
int deltaX = 0, deltaY = 0;
|
||||
m_pMinecraft->platform()->getMouseDiff(deltaX, deltaY);
|
||||
m_pMinecraft->platform()->clearDiff();
|
||||
|
||||
Delta d;
|
||||
d.x = C_SENSITIVITY * deltaX;
|
||||
d.y = C_SENSITIVITY * deltaY;
|
||||
|
||||
return d;
|
||||
}
|
||||
19
source/UserInput/MouseTurnInput.hpp
Normal file
19
source/UserInput/MouseTurnInput.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "ITurnInput.hpp"
|
||||
#include "Minecraft.hpp"
|
||||
class Minecraft;
|
||||
|
||||
class MouseTurnInput : public ITurnInput
|
||||
{
|
||||
public:
|
||||
MouseTurnInput(Minecraft*);
|
||||
Delta getTurnDelta() override;
|
||||
|
||||
private:
|
||||
int m_lastX = -1;
|
||||
int m_lastY = -1;
|
||||
|
||||
Minecraft* m_pMinecraft;
|
||||
};
|
||||
|
||||
17
source/UserInput/User.hpp
Normal file
17
source/UserInput/User.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class User
|
||||
{
|
||||
public:
|
||||
User(const std::string& a, const std::string& b)
|
||||
{
|
||||
field_0 = a;
|
||||
field_18 = b;
|
||||
}
|
||||
|
||||
public:
|
||||
std::string field_0;
|
||||
std::string field_18;
|
||||
};
|
||||
Reference in New Issue
Block a user