the whole game

This commit is contained in:
Kolyah35
2026-03-02 22:04:18 +03:00
parent 816e9060b4
commit f0617a5d22
2069 changed files with 581500 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_ControllerTurnInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_ControllerTurnInput_H__
#include "ITurnInput.h"
#include "../../../platform/input/Controller.h"
/** A Controller Turn input */
class ControllerTurnInput : public ITurnInput {
public:
static const int MODE_OFFSET = 1;
static const int MODE_DELTA = 2;
ControllerTurnInput(int controllerId, int mode_)
: cid(controllerId),
mode(mode_),
cxO(0), cyO(0),
wasActive(false)
{}
bool isTouched() { return Controller::isTouched(cid); }
TurnDelta getTurnDelta() {
float dx = 0, dy = 0;
bool isActive = Controller::isTouched(cid);
if (MODE_OFFSET == mode) {
float dt = getDeltaTime();
const float MaxTurnX = 250.0f;
const float MaxTurnY = 200.0f;
float cx = isActive? Controller::getX(cid) : cxO * 0.7f;
float cy = isActive? Controller::getY(cid) : cyO * 0.7f;
dx = linearTransform( cx, 0.1f, MaxTurnX ) * dt;
dy = linearTransform( cy, 0.1f, MaxTurnY ) * dt;
cxO = cx;
cyO = cy;
} else
if (MODE_DELTA == mode && (wasActive || isActive)) {
float cx = Controller::getX(cid);
float cy = Controller::getY(cid);
// const float dt = getDeltaTime();
const float MaxTurnX = 100.0f;
const float MaxTurnY = 100.0f;
const float DeadZone = 0;//0.25f * dt;//0.02f;
if (!wasActive) {
cxO = cx;
cyO = cy;
}
if (isActive) {
dx = linearTransform(cx - cxO, DeadZone) * MaxTurnX;
dy = linearTransform(cy - cyO, DeadZone) * MaxTurnY;
cxO = cx;
cyO = cy;
}
}
wasActive = isActive;
return TurnDelta(dx, -dy);
}
int mode;
private:
int cid;
float cxO, cyO;
bool wasActive;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_ControllerTurnInput_H__*/

View File

@@ -0,0 +1,45 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT__IBuildInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT__IBuildInput_H__
#include "../../IConfigListener.h"
class Player;
class BuildActionIntention {
public:
BuildActionIntention()
: action(0)
{}
BuildActionIntention(int action)
: action(action)
{}
bool isFirstRemove() { return (action & BAI_FIRSTREMOVE) != 0; }
bool isRemoveContinue() { return (action & BAI_REMOVE) != 0; }
bool isBuild() { return (action & BAI_BUILD) != 0; }
bool isRemove() { return isFirstRemove() || isRemoveContinue(); }
bool isAttack() { return (action & BAI_ATTACK) != 0; }
bool isInteract() { return (action & BAI_INTERACT) != 0; }
int action;
static const int BAI_BUILD = 1;
static const int BAI_REMOVE = 2;
static const int BAI_FIRSTREMOVE = 4;
static const int BAI_ATTACK = 8;
static const int BAI_INTERACT = 16;
};
class IBuildInput: public IConfigListener {
public:
virtual ~IBuildInput(){}
virtual void onConfigChanged(const Config& c) {}
// @return true if user wants to do an action, false if not
virtual bool tickBuild(Player*, BuildActionIntention* bai) { return false; }
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT__IBuildInput_H__*/

View File

@@ -0,0 +1,75 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__IInputHolder_H__
#define NET_MINECRAFT_CLIENT_PLAYER__IInputHolder_H__
#include "IMoveInput.h"
#include "ITurnInput.h"
#include "IBuildInput.h"
#include "../../../platform/input/Mouse.h"
class Player;
class IInputHolder: public IConfigListener
{
public:
IInputHolder()
: mousex(0),
mousey(0),
alpha(0)
{}
virtual ~IInputHolder() {}
virtual void render(float alpha) {}
virtual bool allowPicking() {
mousex = Mouse::getX();
mousey = Mouse::getY();
return Mouse::getButtonState(MouseAction::ACTION_LEFT) == MouseAction::DATA_DOWN;
}
void onConfigChanged(const Config& c) {
getMoveInput()->onConfigChanged(c);
getTurnInput()->onConfigChanged(c);
getBuildInput()->onConfigChanged(c);
}
virtual IMoveInput* getMoveInput() = 0;
virtual ITurnInput* getTurnInput() = 0;
virtual IBuildInput* getBuildInput() = 0;
float mousex, mousey;
float alpha;
};
class CustomInputHolder: public IInputHolder
{
public:
CustomInputHolder(IMoveInput* move, ITurnInput* turn, IBuildInput* build)
{
setInputs(move, turn, build);
}
~CustomInputHolder() {
delete _move;
delete _turn;
delete _build;
}
void setInputs(IMoveInput* move, ITurnInput* turn, IBuildInput* build) {
_move = move;
_turn = turn;
_build = build;
}
virtual IMoveInput* getMoveInput() { return _move; }
virtual ITurnInput* getTurnInput() { return _turn; }
virtual IBuildInput* getBuildInput() { return _build; }
private:
IMoveInput* _move;
ITurnInput* _turn;
IBuildInput* _build;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__IInputHolder_H__*/

View File

@@ -0,0 +1,47 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__IMoveInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER__IMoveInput_H__
//package net.minecraft.client.player;
#include "../../IConfigListener.h"
class Player;
class Minecraft;
class IMoveInput
{
protected:
IMoveInput()
: xa(0),
ya(0),
wasJumping(false),
jumping(false),
sneaking(false),
wantUp(false),
wantDown(false),
isChangingFlightHeight(false)
{}
public:
virtual ~IMoveInput() {}
virtual void tick(Player* player) {}
virtual void render(float a) {}
virtual void setKey(int eventKey, bool eventKeyState) {}
virtual void releaseAllKeys() {}
virtual void onConfigChanged(const Config& c) {}
public:
float xa;
float ya;
bool wasJumping;
bool jumping;
bool sneaking;
bool wantUp;
bool wantDown;
bool isChangingFlightHeight;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__IMoveInput_H__*/

View File

@@ -0,0 +1,52 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__ITurnInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER__ITurnInput_H__
#include "../../../platform/time.h"
#include "../../IConfigListener.h"
#include <cmath>
/** Representing a Turn delta value */
class TurnDelta {
public:
TurnDelta(float dx, float dy)
: x(dx),
y(dy)
{}
float x, y;
};
/** Base class for classes providing TurnDeltas */
class ITurnInput: public IConfigListener {
public:
virtual ~ITurnInput() {}
virtual void onConfigChanged(const Config& c) {}
virtual TurnDelta getTurnDelta() = 0;
protected:
ITurnInput()
: _lastTime(-1.0f)
{}
float getDeltaTime(){
if (_lastTime == -1.0f) {
_lastTime = getTimeS();
}
float now = getTimeS();
float dt = now - _lastTime;
_lastTime = now;
return dt;
}
static float linearTransform(float value, float deadZone, float scale=1.0f, bool limit1=false) {
float deadSigned = value >= 0? deadZone : -deadZone;
if (std::abs(deadSigned) >= std::abs(value)) return 0;
float ret = (value - deadSigned) * scale;
if (limit1 && std::abs(ret) > 1) ret /= std::abs(ret);
return ret;
}
private:
float _lastTime;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__ITurnInput_H__*/

View File

@@ -0,0 +1,65 @@
#include "KeyboardInput.h"
#include "../../Options.h"
#include "../../../world/entity/player/Player.h"
KeyboardInput::KeyboardInput( Options* options )
{
for (int i = 0; i < NumKeys; ++i)
keys[i] = false;
this->options = options;
}
void KeyboardInput::setKey( int key, bool state )
{
int id = -1;
if (key == options->keyUp.key) id = KEY_UP;
if (key == options->keyDown.key) id = KEY_DOWN;
if (key == options->keyLeft.key) id = KEY_LEFT;
if (key == options->keyRight.key) id = KEY_RIGHT;
if (key == options->keyJump.key) id = KEY_JUMP;
if (key == options->keySneak.key) id = KEY_SNEAK;
if (key == options->keyCraft.key) id = KEY_CRAFT;
//printf("key: %d\n", id);
if (id >= 0) {
keys[id] = state;
}
}
void KeyboardInput::releaseAllKeys()
{
xa = 0;
ya = 0;
for (int i = 0; i < NumKeys; i++) {
keys[i] = false;
}
wantUp = wantDown = false;
}
void KeyboardInput::tick( Player* player )
{
xa = 0;
ya = 0;
if (keys[KEY_UP]) ya++;
if (keys[KEY_DOWN]) ya--;
if (keys[KEY_LEFT]) xa++;
if (keys[KEY_RIGHT]) xa--;
jumping = keys[KEY_JUMP];
sneaking = keys[KEY_SNEAK];
if (sneaking) {
xa *= 0.3f;
ya *= 0.3f;
}
#if defined(RPI) || defined(PLATFORM_GLFW)
wantUp = jumping;
wantDown = sneaking;
#endif
if (keys[KEY_CRAFT])
player->startCrafting((int)player->x, (int)player->y, (int)player->z, Recipe::SIZE_2X2);
//printf("\n>- %f %f\n", xa, ya);
}

View File

@@ -0,0 +1,36 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__KeyboardInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER__KeyboardInput_H__
//package net.minecraft.client.player;
#include "IMoveInput.h"
class Options;
class Player;
// @todo: extract a separate MoveInput (-> merge XperiaPlayInput)
class KeyboardInput: public IMoveInput
{
static const int NumKeys = 10;
public:
static const int KEY_UP = 0;
static const int KEY_DOWN = 1;
static const int KEY_LEFT = 2;
static const int KEY_RIGHT = 3;
static const int KEY_JUMP = 4;
static const int KEY_SNEAK = 5;
static const int KEY_CRAFT = 6;
KeyboardInput(Options* options);
void tick(Player* player);
void setKey(int key, bool state);
void releaseAllKeys();
protected:
bool keys[NumKeys];
Options* options;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__KeyboardInput_H__*/

View File

@@ -0,0 +1,36 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseBuildInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseBuildInput_H__
#include "IBuildInput.h"
#include "../../../platform/input/Mouse.h"
/** A Mouse Build input */
class MouseBuildInput : public IBuildInput {
public:
MouseBuildInput()
: buildDelayTicks(10),
buildHoldTicks(0)
{}
virtual bool tickBuild(Player* p, BuildActionIntention* bai) {
if (Mouse::getButtonState(MouseAction::ACTION_LEFT) != 0) {
*bai = BuildActionIntention(BuildActionIntention::BAI_REMOVE | BuildActionIntention::BAI_ATTACK);
return true;
}
if (Mouse::getButtonState(MouseAction::ACTION_RIGHT) != 0) {
if (buildHoldTicks >= buildDelayTicks) buildHoldTicks = 0;
if (++buildHoldTicks == 1) {
*bai = BuildActionIntention(BuildActionIntention::BAI_BUILD | BuildActionIntention::BAI_INTERACT);
return true;
}
} else {
buildHoldTicks = 0;
}
return false;
}
private:
int buildHoldTicks;
int buildDelayTicks;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseBuildInput_H__*/

View File

@@ -0,0 +1,43 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseTurnInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseTurnInput_H__
#include "ITurnInput.h"
#include "../../../platform/input/Mouse.h"
/** A Mouse Turn input */
class MouseTurnInput : public ITurnInput {
public:
static const int MODE_OFFSET = 1;
static const int MODE_DELTA = 2;
MouseTurnInput(int mode_, int centerX, int centerY)
: mode(mode_),
_centerX((float)centerX),
_centerY((float)centerY)
{}
void onConfigChanged(const Config& config) {
_centerX = ((float) config.width ) * 0.5f;
_centerY = ((float) config.height) * 0.5f;
}
TurnDelta getTurnDelta() {
float dx = 0, dy = 0;
if ( MODE_DELTA == mode ) {
dx = (float)Mouse::getDX();
dy = (float)Mouse::getDY();
} else if (MODE_OFFSET == mode) {
float dt = getDeltaTime();
dx = linearTransform( (float)Mouse::getX() - _centerX, 40, 0.4f ) * dt;
dy = linearTransform( (float)Mouse::getY() - _centerY, 30, 0.6f ) * dt;
}
return TurnDelta(dx, dy);
}
int mode;
private:
float _centerX;
float _centerY;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_MouseTurnInput_H__*/

View File

@@ -0,0 +1,49 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__XperiaPlayInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER__XperiaPlayInput_H__
//package net.minecraft.client.player;
#include "KeyboardInput.h"
// @note: This is just copy-pasted from KeyboardInput right now.
class XperiaPlayInput: public KeyboardInput
{
typedef KeyboardInput super;
public:
XperiaPlayInput(Options* options)
: super(options),
wasSneakTouched(false)
{}
void tick(Player* player) {
bool _sneaking = sneaking;
super::tick(player);
sneaking = _sneaking;
wantUp = (jumping && keys[KEY_UP]);
wantDown = (jumping && keys[KEY_DOWN]);
if ((wantUp | wantDown) && (player && player->abilities.flying)) ya = 0;
bool isSneakTouched = Controller::isTouched(1);
if (isSneakTouched && !wasSneakTouched) {
sneaking = !sneaking;
}
wasSneakTouched = isSneakTouched;
if (sneaking) {
xa *= 0.3f;
ya *= 0.3f;
}
}
/*
if (Controller::isTouched(moveStick)) {
xa = -Controller::getTransformedX(moveStick, 0.1f, 1.25f, true);
ya = +Controller::getTransformedY(moveStick, 0.1f, 1.25f, true);
}
*/
private:
bool wasSneakTouched;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__XperiaPlayInput_H__*/

View File

@@ -0,0 +1,14 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__ITouchScreenModel_H__
#define NET_MINECRAFT_CLIENT_PLAYER__ITouchScreenModel_H__
#include "../../../../platform/input/Mouse.h"
class ITouchScreenModel
{
public:
virtual ~ITouchScreenModel() {}
virtual int getPointerId(const MouseAction& m) { return m.pointerId; }
virtual int getPointerId(int x, int y, int pid) { return pid; }
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__ITouchScreenModel_H__*/

View File

@@ -0,0 +1,339 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER__TouchAreaModel_H__
#define NET_MINECRAFT_CLIENT_PLAYER__TouchAreaModel_H__
#include "ITouchScreenModel.h"
#include <vector>
#include "../../../../platform/time.h"
/// Interface for an area that can be point tested
class IArea{
public:
IArea()
: deleteMe(true)
{}
virtual ~IArea() {};
virtual bool isInside(float x, float y) = 0;
//virtual void expandRectToInclude(float& l, float& t, float& r, float& b) {}
bool deleteMe;
};
/// Holder for multiple <IArea, int areaId> pairs
class TouchAreaModel: public ITouchScreenModel
{
public:
TouchAreaModel() {}
~TouchAreaModel() {
clear();
}
void clear() {
for (unsigned int i = 0; i < _areas.size(); ++i) {
Area* a = _areas[i];
if (a->area && a->area->deleteMe)
delete a->area;
delete a;
}
_areas.clear();
}
int getPointerId(const MouseAction& m) {
return getPointerId(m.x, m.y, m.pointerId);
}
int getPointerId(int x, int y, int pid) {
//static Stopwatch w;
//w.printEvery(200, "IArea");
//w.start();
float fx = (float)x, fy = (float) y;
for (unsigned int i = 0; i < _areas.size(); ++i) {
const Area* a = _areas[i];
if (a->area->isInside(fx, fy)) {
//w.stop();
return a->areaId;
}
}
//w.stop();
return pid;
}
void addArea(int areaId, IArea* area) {
Area* a = new Area();
a->area = area;
a->areaId = areaId;
_areas.push_back( a );
}
private:
struct Area {
IArea* area;
int areaId;
};
std::vector<Area*> _areas;
};
/// Area represented by a polygon
class PolygonArea: public IArea
{
public:
PolygonArea(int numPoints, const float* xp, const float* yp)
: _numPoints(numPoints)
{
_x = new float[numPoints];
_y = new float[numPoints];
for (int i = 0; i < numPoints; ++i) {
_x[i] = xp[i];
_y[i] = yp[i];
}
}
~PolygonArea() {
delete[] _x;
delete[] _y;
}
virtual bool isInside(float x, float y)
{
bool c = false;
for (int i = 0, j = _numPoints-1; i < _numPoints; j = i++) {
if ((((_y[i] <= y) && (y < _y[j])) ||
((_y[j] <= y) && (y < _y[i]))) &&
(x < (_x[j] - _x[i]) * (y - _y[i]) / (_y[j] - _y[i]) + _x[i]))
c = !c;
}
return c == 1;
}
//private:
float* _x;
float* _y;
int _numPoints;
};
/// Area represented by a rectangle
class RectangleArea: public IArea
{
public:
RectangleArea(float x0, float y0, float x1, float y1)
: _x0(x0),
_x1(x1),
_y0(y0),
_y1(y1)
{}
virtual bool isInside(float x, float y) {
return x >= _x0 && x <= _x1
&& y >= _y0 && y <= _y1;
}
virtual float centerX()
{
return _x0 + (_x1 - _x0) * .5f;
}
virtual float centerY()
{
return _y0 + (_y1 - _y0) * .5f;
}
//private:
float _x0, _x1;
float _y0, _y1;
};
/// An area represented by a circle
class CircleArea: public IArea
{
public:
CircleArea(float x, float y, float r)
: _x(x), _y(y), _rr(r*r)
{}
virtual bool isInside(float x, float y) {
const float dx = x - _x;
const float dy = y - _y;
return (dx*dx + dy*dy) <= _rr;
}
private:
float _x, _y;
float _rr;
};
/// Inside any area in include list but none of exclude list
class IncludeExcludeArea: public IArea
{
public:
IncludeExcludeArea() {}
~IncludeExcludeArea() {
clear();
}
void clear() {
if (deleteMe) {
for (unsigned int i = 0; i < _includes.size(); ++i) {
IArea* area = _includes[i];
if (area->deleteMe) delete area;
}
for (unsigned int i = 0; i < _excludes.size(); ++i) {
IArea* area = _excludes[i];
if (area->deleteMe) delete area;
}
}
_includes.clear();
_excludes.clear();
}
void include(IArea* area) { _includes.push_back(area); }
void exclude(IArea* area) { _excludes.push_back(area); }
virtual bool isInside(float x, float y) {
for (unsigned int i = 0; i < _includes.size(); ++i)
if (_includes[i]->isInside(x, y)) {
bool good = true;
//@todo: cache _exclude-test results
for (unsigned int j = 0; j < _excludes.size(); ++j)
if (_excludes[j]->isInside(x, y)) {
good = false;
break;
}
if (good) {
return true;
}
}
return false;
}
private:
std::vector<IArea*> _includes;
std::vector<IArea*> _excludes;
};
/// At least one of the areas
class OrArea: public IArea
{
public:
OrArea(IArea* a, IArea* b)
: _a(a),
_b(b)
{}
~OrArea() {
if (deleteMe) {
if (_a->deleteMe) delete _a;
if (_b->deleteMe) delete _b;
}
}
virtual bool isInside(float x, float y) {
return _a->isInside(x, y) || _b->isInside(x, y);
}
private:
IArea* _a;
IArea* _b;
};
/// In both areas
class AndArea: public IArea
{
public:
AndArea(IArea* a, IArea* b)
: _a(a),
_b(b)
{}
~AndArea() {
if (deleteMe) {
if (_a->deleteMe) delete _a;
if (_b->deleteMe) delete _b;
}
}
virtual bool isInside(float x, float y) {
return _a->isInside(x, y) && _b->isInside(x, y);
}
private:
IArea* _a;
IArea* _b;
};
/// Exactly one of the areas
class XorArea: public IArea
{
public:
XorArea(IArea* a, IArea* b)
: _a(a),
_b(b)
{}
~XorArea() {
if (deleteMe) {
if (_a->deleteMe) delete _a;
if (_b->deleteMe) delete _b;
}
}
virtual bool isInside(float x, float y) {
return _a->isInside(x, y) ^ _b->isInside(x, y);
}
private:
IArea* _a;
IArea* _b;
};
/// Everything except this area
class NotArea: public IArea
{
public:
NotArea(IArea* a)
: _a(a)
{}
~NotArea() {
if (deleteMe && _a->deleteMe) delete _a;
}
virtual bool isInside(float x, float y) {
return !_a->isInside(x, y);
}
private:
IArea* _a;
};
/// First area, but not second
/// This is semantically the same as AndArea( a, new NotArea(b) )
class DifferenceArea: public IArea
{
public:
DifferenceArea(IArea* a, IArea* b)
: _a(a),
_b(b)
{}
~DifferenceArea() {
if (deleteMe) {
if (_a->deleteMe) delete _a;
if (_b->deleteMe) delete _b;
}
}
virtual bool isInside(float x, float y) {
return _a->isInside(x, y) && !_b->isInside(x, y);
}
private:
IArea* _a;
IArea* _b;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER__TouchAreaModel_H__*/

View File

@@ -0,0 +1,473 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchInputHolder_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchInputHolder_H__
#include "../IInputHolder.h"
#include "TouchscreenInput.h"
#include "../ITurnInput.h"
#include "TouchAreaModel.h"
#include "../../../../platform/input/Multitouch.h"
#include "../../../../platform/time.h"
#include "../../../../util/SmoothFloat.h"
#include "../../../../world/entity/player/Player.h"
#include "../../../../world/entity/player/Inventory.h"
template <class T>
class ModifyNotify {
public:
ModifyNotify()
: _changed(false)
{}
T& getOld() { return _old; }
T& getNew() { return _new; }
bool update(T& val) {
_changed = !equals(val);
if (_changed) {
_old = _new;
_new = val;
}
return _changed;
}
bool hasChanged() const { return _changed; }
virtual bool equals(const T& newVal) {
return _old != newVal;
}
protected:
T _old;
private:
T _new;
bool _changed;
};
//
// Implementation for unified [Turn & Build Input]
//
class UnifiedTurnBuild: public GuiComponent,
public ITurnInput,
public IBuildInput
{
public:
static const int MODE_OFFSET = 1;
static const int MODE_DELTA = 2;
UnifiedTurnBuild(int turnMode, int width, int height, float maxMovementDelta, float sensitivity, IInputHolder* holder, Minecraft* minecraft)
: mode(turnMode),
_holder(holder),
_options(&minecraft->options),
cxO(0), cyO(0),
wasActive(false),
_totalMoveDelta(0),
_maxMovement(maxMovementDelta),
_lastPlayer(0),
screenArea(-1, -1, 0, 0),
allowPicking(false),
state(State_None),
moveArea(-1,-1,0,0),
joyTouchArea(-1, -1, 0, 0),
inventoryArea(-1,-1, 0, 0),
pauseArea(-1, -1, 0, 0),
_buildMovement(0),
_sentFirstRemove(false),
_canDestroy(false),
_forceCanUse(false)
{
_area.deleteMe = false;
setSensitivity(sensitivity);
//((ITurnInput*)this)->onConfigChanged(createConfig(minecraft));
onConfigChanged(createConfig(minecraft));
_lastBuildDownTime = _startTurnTime = getTimeS();
}
void setSensitivity(float sensitivity) {
_sensitivity = sensitivity;
}
virtual void onConfigChanged(const Config& c) {
if (false && _options->isJoyTouchArea) {
int touchWidth = c.width - (int)inventoryArea._x1;
if (touchWidth > (int)c.minecraft->pixelCalc.millimetersToPixels(60))
touchWidth = (int)c.minecraft->pixelCalc.millimetersToPixels(60);
int touchHeight = (int)(c.height * 0.4f);
if (touchHeight > (int)c.minecraft->pixelCalc.millimetersToPixels(40))
touchHeight = (int)c.minecraft->pixelCalc.millimetersToPixels(40);
joyTouchArea._x0 = (float)(c.width - touchWidth);
joyTouchArea._y0 = (float)(c.height - touchHeight);
joyTouchArea._x1 = (float)c.width;
joyTouchArea._y1 = (float)c.height;
_area.clear();
_area.include (&joyTouchArea);
_model.clear();
_model.addArea(AREA_TURN, &_area);
} else {
screenArea = RectangleArea(0, 0, (float)c.width, (float)c.height);
// Expand the move area a bit
const float border = 10;
const float widen = (moveArea._x1-moveArea._x0) * 0.05f + border; // ~5% wider
moveArea._x0 -= widen;
moveArea._x1 += widen;
const float heighten = (moveArea._y1-moveArea._y0) * 0.05f + border; // ~5% taller
moveArea._y0 -= heighten;
moveArea._y1 += heighten;
pauseArea._x0 -= border;
pauseArea._x1 += border;
pauseArea._y0 -= border;
pauseArea._y1 += border;
//LOGI("move: %f, %f, %f, %f\n", moveArea._x0, moveArea._y0, moveArea._x1, moveArea._y1);
_area.clear();
_area.include(&screenArea);
_area.exclude(&moveArea);
_area.exclude(&inventoryArea);
#ifdef __APPLE__
_area.exclude(&pauseArea);
#endif /*__APPLE__*/
//LOGI("Movearea: %f %f %f% f\n", moveArea._x0, moveArea._x1, moveArea._y0, moveArea._y1);
_model.clear();
_model.addArea(AREA_TURN, &_area);
}
}
float calcNewAlpha(float current, float wanted) {
if (wanted > current)
return Mth::clamp(current + 0.02f, 0.0f, wanted);
if (wanted < current)
return Mth::clamp(current - 0.04f, wanted, 1.0f);
return current;
}
//
// Implementation for the ITurnInput part
//
TurnDelta getTurnDelta() {
float dx = 0, dy = 0;
const float now = getTimeS();
float cx = 0;
float cy = 0;
bool isActive = false;
const int* pointerIds;
bool wasFirstMovement = false;
int pointerCount = Multitouch::getActivePointerIds(&pointerIds);
for (int i = 0; i < pointerCount; ++i) {
int p = pointerIds[i];
int x = Multitouch::getX(p);
int y = Multitouch::getY(p);
int areaId = _model.getPointerId(x, y, p);
if (areaId == AREA_TURN) {
isActive = true;
cx = (float)x * 0.5f;
cy = (float)y * -0.5f;
wasFirstMovement = Multitouch::wasFirstMovement(p);
break;
}
}
if (isActive && !wasActive) {
_startTurnTime = now;
_totalMoveDelta = 0;
bool isInMovement = _lastPlayer? getSpeedSquared(_lastPlayer) > 0.01f/*.25f*/ : false;
//state = isInMovement? State_Turn : State_Deciding;
state = State_Deciding;
_canDestroy = !isInMovement;
_forceCanUse = false;
if (!_canDestroy && (_lastPlayer && _lastPlayer->canUseCarriedItemWhileMoving())) {
_forceCanUse = true;
_canDestroy = true;
}
_sentFirstRemove = false;
} else if (wasActive && !isActive) {
_sentFirstRemove = false;
state = State_None;
//_inBuild = false;
}
if (MODE_DELTA == mode && (wasActive || isActive)) {
// const float dt = getDeltaTime();
// const float MaxTurnX = 100.0f;
// const float MaxTurnY = 100.0f;
const float DeadZone = 0;//0.25f * dt;//0.02f;
if (!wasActive) {
cxO = cx;
cyO = cy;
}
if (isActive) {
dx = _sensitivity * linearTransform(cx - cxO, DeadZone);// * MaxTurnX;
dy = _sensitivity * linearTransform(cy - cyO, DeadZone);// * MaxTurnY;
float moveDelta = ( Mth::abs(dx) + Mth::abs(dy) );
//LOGI("moveDelta is : %f\n", moveDelta);
if (moveDelta > _maxMovement) {
dx = 0;
dy = 0;
moveDelta = 0;
}
_totalMoveDelta += moveDelta;
if (state == State_Deciding && _totalMoveDelta >= MaxBuildMovement/* && !_forceCanUse*/)
state = State_Turn;
// If a certain time has passed since we pressed button; check if
// we've moved enough to prevent removal state to become activated
const float since = now - _startTurnTime;
if (state == State_Deciding && (since >= (0.001f*RemovalMilliseconds))) {
//LOGI("DECIDED!: %f\n", _totalMoveDelta);
bool isInMovement = _lastPlayer? getSpeedSquared(_lastPlayer) > 0.01f/*.25f*/ : false;
if (!_forceCanUse && (_totalMoveDelta > 20.0f || isInMovement)) {
state = State_Turn;
} else {
state = State_Destroy;
_canDestroy = true;
}
}
// Removing the pointer ketchup-effect that's built into some devices
if (wasFirstMovement) {
dx = dy = 0;
}
cxO = cx;
cyO = cy;
//LOGI("move delta: %f (%f, %f), %d\n", moveDelta, dx, dy, state);
}
} else {
// Wasn't active, and isn't active!
state = State_None;
}
updateFeedbackProgressAlpha(now);
wasActive = isActive;
return TurnDelta(dx, -dy);
}
void updateFeedbackProgressAlpha(float now) {
const float MinAlphaValue = -0.05f;
if (_canDestroy) {
// Hack to test out fading in feedback circle
const float since = now - _startTurnTime;
if (state == State_Deciding) {
const float wantedAlpha = since / (0.001f*RemovalMilliseconds);
_holder->alpha = wantedAlpha * wantedAlpha;
} else {
if (state == State_Destroy) {
_holder->alpha = calcNewAlpha(_holder->alpha, 1);
} else if (state == State_Turn || state == State_None) {
_holder->alpha = calcNewAlpha(_holder->alpha, 0);
}
}
} else {
_holder->alpha = MinAlphaValue;
}
//LOGI("state: %d, canDestroy: %d %d\n", state, _canDestroy, _forceCanUse);
//LOGI("alpha: %f\n", _holder->alpha);
}
bool isInsideArea(float x, float y) {
return _area.isInside(x, y);
}
int mode;
static float getSpeedSquared(Entity* m) {
const float xd = m->x - m->xo;
const float yd = m->y - m->yo;
const float zd = m->z - m->zo;
const float speedSquared = xd*xd + yd*yd + zd*zd;
return speedSquared;
}
void render(float alpha) {
if (_options->isJoyTouchArea) {
fill( (int) (Gui::InvGuiScale * joyTouchArea._x0),
(int) (Gui::InvGuiScale * joyTouchArea._y0),
(int) (Gui::InvGuiScale * joyTouchArea._x1),
(int) (Gui::InvGuiScale * joyTouchArea._y1), 0x40000000);//0x20ffffff);
}
}
//
// Implementation for the IBuildInput part
//
virtual bool tickBuild(Player* player, BuildActionIntention* bai) {
_lastPlayer = player;
if (state == State_Destroy) {
if (!_sentFirstRemove) {
*bai = BuildActionIntention((_forceCanUse?0:BuildActionIntention::BAI_FIRSTREMOVE) | BuildActionIntention::BAI_INTERACT);
_sentFirstRemove = true;
} else {
*bai = BuildActionIntention((_forceCanUse?0:BuildActionIntention::BAI_REMOVE) | BuildActionIntention::BAI_INTERACT);
}
return true;
}
// Rewind the queue, in case someone has used it up before
Multitouch::rewind();
const float now = getTimeS();
allowPicking = false;
bool handled = false;
while (Multitouch::next()) {
MouseAction& m = Multitouch::getEvent();
if (m.action == MouseAction::ACTION_MOVE) continue;
int areaId = _model.getPointerId(m.x, m.y, m.pointerId);
if (areaId != AREA_TURN) continue;
allowPicking = true;
//LOGI("down? %d, up? %d (%d, %d)\n", z && (m.data == MouseAction::DATA_DOWN), z && (m.data == MouseAction::DATA_UP), m.x, m.y);
if (_totalMoveDelta <= MaxBuildMovement && (m.data == MouseAction::DATA_UP && !handled)) {
const float since = now - _lastBuildDownTime;
//LOGI("move: (%d) %.2f - %f\n", state, _totalMoveDelta, since);
if (state <= State_Deciding) {
if (since >= 0.0f && since < 0.25f) {
// We've pressed and released in haste; Let's build!
*bai = BuildActionIntention(BuildActionIntention::BAI_BUILD | BuildActionIntention::BAI_ATTACK);
handled = true;
}
}
state = State_None;
} else if (m.data == MouseAction::DATA_DOWN) {
_lastBuildDownTime = now;
_buildMovement = 0;
state = State_Deciding;
}
}
return handled;
}
bool allowPicking;
float alpha;
SmoothFloat smoothAlpha;
RectangleArea screenArea;
RectangleArea moveArea;
RectangleArea joyTouchArea;
RectangleArea inventoryArea;
RectangleArea pauseArea;
private:
IInputHolder* _holder;
// Turn
int cid;
float cxO, cyO;
bool wasActive;
TouchAreaModel _model;
IncludeExcludeArea _area;
bool _decidedTurnMode;
float _startTurnTime;
float _totalMoveDelta;
float _maxMovement;
float _sensitivity;
Player* _lastPlayer;
// Build
float _lastBuildDownTime;
float _buildMovement;
bool _sentFirstRemove;
bool _canDestroy;
bool _forceCanUse;
int state;
static const int State_None = 0;
static const int State_Deciding = 1;
static const int State_Turn = 2;
static const int State_Destroy = 3;
static const int State_Build = 4; // Will never happen
static const int MaxBuildMovement = 20;
static const int RemovalMilliseconds = 400;
static const int AREA_TURN = 100;
Options* _options;
};
class Minecraft;
#if defined(_MSC_VER)
#pragma warning( disable : 4355 ) // 'this' pointer in initialization list which is perfectly legal
#endif
class TouchInputHolder: public IInputHolder
{
public:
TouchInputHolder(Minecraft* mc, Options* options)
: _mc(mc),
_move(mc, options),
_turnBuild(UnifiedTurnBuild::MODE_DELTA, mc->width, mc->height, (float)MovementLimit, 1, this, mc)
{
onConfigChanged(createConfig(mc));
}
~TouchInputHolder() {
}
virtual void onConfigChanged(const Config& c) {
_move.onConfigChanged(c);
_turnBuild.moveArea = _move.getRectangleArea();
#ifdef __APPLE__
_turnBuild.pauseArea = _move.getPauseRectangleArea();
#endif
_turnBuild.inventoryArea = _mc->gui.getRectangleArea( _mc->options.isLeftHanded? 1 : -1 );
_turnBuild.setSensitivity(c.options->isJoyTouchArea? 1.8f : 1.0f);
((ITurnInput*)&_turnBuild)->onConfigChanged(c);
}
virtual bool allowPicking() {
const int* pointerIds;
int pointerCount = Multitouch::getActivePointerIds(&pointerIds);
for (int i = 0; i < pointerCount; ++i) {
int p = pointerIds[i];
const float x = Multitouch::getX(p);
const float y = Multitouch::getY(p);
if (_turnBuild.isInsideArea(x, y)) {
mousex = x;
mousey = y;
return true;
}
}
return false;
// return _turnBuild.allowPicking;
}
virtual void render(float alpha) {
_turnBuild.render(alpha);
}
virtual IMoveInput* getMoveInput() { return &_move; }
virtual ITurnInput* getTurnInput() { return &_turnBuild; }
virtual IBuildInput* getBuildInput() { return &_turnBuild; }
private:
TouchscreenInput_TestFps _move;
UnifiedTurnBuild _turnBuild;
Minecraft* _mc;
static const int MovementLimit = 200; // per update
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchInputHolder_H__*/

View File

@@ -0,0 +1,88 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchTurnInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchTurnInput_H__
#include "../ITurnInput.h"
#include "TouchAreaModel.h"
#include "../../../../platform/input/Multitouch.h"
class TouchTurnInput: public ITurnInput
{
public:
static const int MODE_OFFSET = 1;
static const int MODE_DELTA = 2;
TouchTurnInput(int mode_)
: mode(mode_),
cxO(0), cyO(0),
wasActive(false)
{
}
TurnDelta getTurnDelta() {
float dx = 0, dy = 0;
float cx = 0;
float cy = 0;
bool isActive = false;
const int* pointerIds;
int pointerCount = Multitouch::getActivePointerIds(&pointerIds);
for (int i = 0; i < pointerCount; ++i) {
int p = pointerIds[i];
int x = Multitouch::getX(p);
int y = Multitouch::getY(p);
int areaId = _model.getPointerId(x, y, p);
if (areaId == AREA_TURN) {
isActive = true;
cx = (float)x * 0.5f;
cy = (float)y * -0.5f;
break;
}
}
if (MODE_OFFSET == mode) {
//float dt = getDeltaTime();
//const float MaxTurnX = 250.0f;
//const float MaxTurnY = 200.0f;
//float cx = isActive? Controller::getX(cid) : cxO * 0.7f;
//float cy = isActive? Controller::getY(cid) : cyO * 0.7f;
//dx = linearTransform( cx, 0.1f, MaxTurnX ) * dt;
//dy = linearTransform( cy, 0.1f, MaxTurnY ) * dt;
//cxO = cx;
//cyO = cy;
} else
if (MODE_DELTA == mode && (wasActive || isActive)) {
// const float dt = getDeltaTime();
// const float MaxTurnX = 100.0f;
// const float MaxTurnY = 100.0f;
const float DeadZone = 0;//0.25f * dt;//0.02f;
if (!wasActive) {
cxO = cx;
cyO = cy;
}
if (isActive) {
dx = linearTransform(cx - cxO, DeadZone);// * MaxTurnX;
dy = linearTransform(cy - cyO, DeadZone);// * MaxTurnY;
cxO = cx;
cyO = cy;
}
}
wasActive = isActive;
return TurnDelta(dx, -dy);
}
int mode;
private:
int cid;
float cxO, cyO;
bool wasActive;
TouchAreaModel _model;
static const int AREA_TURN = 100;
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchTurnInput_H__*/

View File

@@ -0,0 +1,509 @@
#include "TouchscreenInput.h"
#include "../../../Options.h"
#include "../../../../platform/input/Multitouch.h"
#include "../../../gui/Gui.h"
#include "../../../renderer/Tesselator.h"
#include "../../../../world/entity/player/Player.h"
#include "../../../Minecraft.h"
#include "../../../../platform/log.h"
#include "../../../renderer/Textures.h"
#include "../../../sound/SoundEngine.h"
static const int AREA_DPAD_FIRST = 100;
static const int AREA_DPAD_N = 100;
static const int AREA_DPAD_S = 101;
static const int AREA_DPAD_W = 102;
static const int AREA_DPAD_E = 103;
static const int AREA_DPAD_C = 104;
static const int AREA_PAUSE = 105;
static int cPressed = 0;
static int cReleased = 0;
static int cDiscreet = 0;
static int cPressedPause = 0;
static int cReleasedPause = 0;
//static const int AREA_DPAD_N_JUMP = 105;
//
// TouchscreenInput_TestFps
//
static void Copy(int n, float* x, float* y, float* dx, float* dy) {
for (int i = 0; i < n; ++i) {
dx[i] = x[i];
dy[i] = y[i];
}
}
static void Translate(int n, float* x, float* y, float xt, float yt) {
for (int i = 0; i < n; ++i) {
x[i] += xt;
y[i] += yt;
}
}
static void Scale(int n, float* x, float* y, float xt, float yt) {
for (int i = 0; i < n; ++i) {
x[i] *= xt;
y[i] *= yt;
}
}
static void Transformed(int n, float* x, float* y, float* dx, float* dy, float xt, float yt, float sx=1.0f, float sy=1.0f) {
Copy(n, x, y, dx, dy);
Scale(n, dx, dy, sx, sy);
Translate(n, dx, dy, xt, yt);
//for (int i = 0; i < n; ++i) {
// LOGI("%d. (%f, %f)\n", i, dx[i], dy[i]);
//}
}
TouchscreenInput_TestFps::TouchscreenInput_TestFps( Minecraft* mc, Options* options )
: _minecraft(mc),
_options(options),
_northJump(false),
_forward(false),
_boundingRectangle(0, 0, 1, 1),
_pressedJump(false),
_pauseIsDown(false),
_sneakTapTime(-999),
aLeft(0),
aRight(0),
aUp(0),
aDown(0),
aJump(0),
aUpLeft(0),
aUpRight(0),
_allowHeightChange(false)
{
releaseAllKeys();
onConfigChanged( createConfig(mc) );
Tesselator& t = Tesselator::instance;
const int alpha = 128;
t.color( 0xc0c0c0, alpha); cPressed = t.getColor();
t.color( 0xffffff, alpha); cReleased = t.getColor();
t.color( 0xffffff, alpha / 4); cDiscreet = t.getColor();
t.color( 0xc0c0c0, 80); cPressedPause=t.getColor();
t.color( 0xffffff, 80); cReleasedPause=t.getColor();
}
TouchscreenInput_TestFps::~TouchscreenInput_TestFps() {
clear();
}
void TouchscreenInput_TestFps::clear() {
_model.clear();
delete aUpLeft; aUpLeft = NULL; // @todo: SAFEDEL
delete aUpRight; aUpRight = NULL;
}
bool TouchscreenInput_TestFps::isButtonDown(int areaId) {
return _buttons[areaId - AREA_DPAD_FIRST];
}
void TouchscreenInput_TestFps::onConfigChanged(const Config& c) {
clear();
const float w = (float)c.width;
const float h = (float)c.height;
/*
// Code for "Move when touching left side of the screen"
float x0[] = { 0, w * 0.3f, w * 0.3f, 0 };
float y0[] = { 0, 0, h-32, h-32 };
_model.addArea(AREA_MOVE, new RectangleArea(0, 0, w*0.3f, h-32));
*/
// Code for "D-pad with jump in center"
float Bw = w * 0.11f;//0.08f;
float Bh = Bw;//0.15f;
// If too large (like playing on Tablet)
PixelCalc& pc = _minecraft->pixelCalc;
if (pc.pixelsToMillimeters(Bw) > 14) {
Bw = Bh = pc.millimetersToPixels(14);
}
// temp data
float xx;
float yy;
const float BaseY = -8 + h - 3.0f * Bh;
const float BaseX = _options->isLeftHanded? -8 + w - 3 * Bw
: 8 + 0;
// Setup the bounding rectangle
_boundingRectangle = RectangleArea(BaseX, BaseY, BaseX + 3 * Bw, BaseY + 3 * Bh);
xx = BaseX + Bw; yy = BaseY;
_model.addArea(AREA_DPAD_N, aUp = new RectangleArea(xx, yy, xx+Bw, yy+Bh));
xx = BaseX;
aUpLeft = new RectangleArea(xx, yy, xx+Bw, yy+Bh);
xx = BaseX + 2 * Bw;
aUpRight = new RectangleArea(xx, yy, xx+Bw, yy+Bh);
xx = BaseX + Bw; yy = BaseY + Bh;
_model.addArea(AREA_DPAD_C, aJump = new RectangleArea(xx, yy, xx+Bw, yy+Bh));
xx = BaseX + Bw; yy = BaseY + 2 * Bh;
_model.addArea(AREA_DPAD_S, aDown = new RectangleArea(xx, yy, xx+Bw, yy+Bh));
xx = BaseX; yy = BaseY + Bh;
_model.addArea(AREA_DPAD_W, aLeft = new RectangleArea(xx, yy, xx+Bw, yy+Bh));
xx = BaseX + 2 * Bw; yy = BaseY + Bh;
_model.addArea(AREA_DPAD_E, aRight = new RectangleArea(xx, yy, xx+Bw, yy+Bh));
#ifdef __APPLE__
float maxPixels = _minecraft->pixelCalc.millimetersToPixels(10);
float btnSize = Mth::Min(18 * Gui::GuiScale, maxPixels);
_model.addArea(AREA_PAUSE, aPause = new RectangleArea(w - 4 - btnSize,
4,
w - 4,
4 + btnSize));
#endif /* __APPLE__ */
//rebuild();
}
void TouchscreenInput_TestFps::setKey( int key, bool state )
{
#ifdef WIN32
//LOGI("key: %d, %d\n", key, state);
int id = -1;
if (key == _options->keyUp.key) id = KEY_UP;
if (key == _options->keyDown.key) id = KEY_DOWN;
if (key == _options->keyLeft.key) id = KEY_LEFT;
if (key == _options->keyRight.key) id = KEY_RIGHT;
if (key == _options->keyJump.key) id = KEY_JUMP;
if (key == _options->keySneak.key) id = KEY_SNEAK;
if (key == _options->keyCraft.key) id = KEY_CRAFT;
if (id >= 0) {
_keys[id] = state;
}
#endif
}
void TouchscreenInput_TestFps::releaseAllKeys()
{
xa = 0;
ya = 0;
for (int i = 0; i<8; ++i)
_buttons[i] = false;
#ifdef WIN32
for (int i = 0; i<NumKeys; ++i)
_keys[i] = false;
#endif
_pressedJump = false;
_allowHeightChange = false;
}
void TouchscreenInput_TestFps::tick( Player* player )
{
xa = 0;
ya = 0;
jumping = false;
//bool gotEvent = false;
bool heldJump = false;
bool tmpForward = false;
bool tmpNorthJump = false;
for (int i = 0; i < 6; ++i)
_buttons[i] = false;
const int* pointerIds;
int pointerCount = Multitouch::getActivePointerIdsThisUpdate(&pointerIds);
for (int i = 0; i < pointerCount; ++i) {
int p = pointerIds[i];
int x = Multitouch::getX(p);
int y = Multitouch::getY(p);
if (_boundingRectangle.isInside((float)x, (float)y) && _forward && !isChangingFlightHeight)
{
float angle = Mth::PI + Mth::atan2(y - _boundingRectangle.centerY(), x - _boundingRectangle.centerX());
ya = Mth::sin(angle);
xa = Mth::cos(angle);
tmpForward = true;
}
int areaId = _model.getPointerId(x, y, p);
if (areaId < AREA_DPAD_FIRST)
{
continue;
}
bool setButton = false;
if (Multitouch::isPressed(p))
_allowHeightChange = (areaId == AREA_DPAD_C);
if (areaId == AREA_DPAD_C)
{
setButton = true;
heldJump = true;
// If we're in water or pressed down on the button: jump
if (player->isInWater()) {
jumping = true;
}
else if (Multitouch::isPressed(p)) {
jumping = true;
} // Or if we are walking forward, jump while going forward!
else if (_forward && !player->abilities.flying) {
areaId = AREA_DPAD_N;
tmpNorthJump = true;
//jumping = true;
ya += 1;
}
}
if (areaId == AREA_DPAD_N)
{
setButton = true;
if (player->isInWater())
jumping = true;
else if (!isChangingFlightHeight)
tmpForward = true;
ya += 1;
}
else if (areaId == AREA_DPAD_S && !_forward)
{
setButton = true;
ya -= 1;
/*
if (Multitouch::isReleased(p)) {
float now = getTimeS();
if (now - _sneakTapTime < 0.4f) {
ya += 1;
sneaking = !sneaking;
player->setSneaking(sneaking);
_sneakTapTime = -1;
} else {
_sneakTapTime = now;
}
}
*/
}
else if (areaId == AREA_DPAD_W && !_forward)
{
setButton = true;
xa += 1;
}
else if (areaId == AREA_DPAD_E && !_forward)
{
setButton = true;
xa -= 1;
}
#ifdef __APPLE__
else if (areaId == AREA_PAUSE) {
if (Multitouch::isReleased(p)) {
_minecraft->soundEngine->playUI("random.click", 1, 1);
_minecraft->screenChooser.setScreen(SCREEN_PAUSE);
}
}
#endif /*__APPLE__*/
_buttons[areaId - AREA_DPAD_FIRST] = setButton;
}
_forward = tmpForward;
// Only jump once at a time
if (tmpNorthJump) {
if (!_northJump)
jumping = true;
_northJump = true;
}
else _northJump = false;
isChangingFlightHeight = false;
wantUp = isButtonDown(AREA_DPAD_N) && (_allowHeightChange & (_pressedJump | wantUp));
wantDown = isButtonDown(AREA_DPAD_S) && (_allowHeightChange & (_pressedJump | wantDown));
if (player->abilities.flying && (wantUp || wantDown || (heldJump && !_forward)))
{
isChangingFlightHeight = true;
ya = 0;
}
_renderFlightImage = player->abilities.flying;
#ifdef WIN32
if (_keys[KEY_UP]) ya++;
if (_keys[KEY_DOWN]) ya--;
if (_keys[KEY_LEFT]) xa++;
if (_keys[KEY_RIGHT]) xa--;
if (_keys[KEY_JUMP]) jumping = true;
//sneaking = _keys[KEY_SNEAK];
if (_keys[KEY_CRAFT])
player->startCrafting((int)player->x, (int)player->y, (int)player->z, Recipe::SIZE_2X2);
#endif
if (sneaking) {
xa *= 0.3f;
ya *= 0.3f;
}
//printf("\n>- %f %f\n", xa, ya);
_pressedJump = heldJump;
}
static void drawRectangleArea(Tesselator& t, RectangleArea* a, int ux, int vy, float ssz = 64.0f) {
const float pm = 1.0f / 256.0f;
const float sz = ssz * pm;
const float uu = (float)(ux) * pm;
const float vv = (float)(vy) * pm;
const float x0 = a->_x0 * Gui::InvGuiScale;
const float x1 = a->_x1 * Gui::InvGuiScale;
const float y0 = a->_y0 * Gui::InvGuiScale;
const float y1 = a->_y1 * Gui::InvGuiScale;
t.vertexUV(x0, y1, 0, uu, vv+sz);
t.vertexUV(x1, y1, 0, uu+sz,vv+sz);
t.vertexUV(x1, y0, 0, uu+sz,vv);
t.vertexUV(x0, y0, 0, uu, vv);
}
static void drawPolygonArea(Tesselator& t, PolygonArea* a, int x, int y) {
float pm = 1.0f / 256.0f;
float sz = 64.0f * pm;
float uu = (float)(x) * pm;
float vv = (float)(y) * pm;
float uvs[] = {uu, vv, uu+sz, vv, uu+sz, vv+sz, uu, vv+sz};
const int o = 0;
for (int j = 0; j < a->_numPoints; ++j) {
t.vertexUV(a->_x[j] * Gui::InvGuiScale, a->_y[j] * Gui::InvGuiScale, 0, uvs[(o+j+j)&7], uvs[(o+j+j+1)&7]);
}
}
void TouchscreenInput_TestFps::render( float a ) {
//return;
//static Stopwatch sw;
//sw.start();
//glColor4f2(1, 0, 1, 1.0f);
//glDisable2(GL_CULL_FACE);
glDisable2(GL_ALPHA_TEST);
glEnable2(GL_BLEND);
glBlendFunc2(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
_minecraft->textures->loadAndBindTexture("gui/gui.png");
//glDisable2(GL_TEXTURE_2D);
rebuild();
//drawArrayVTC(_bufferId, 5 * 2 * 3, 24);
glDisable2(GL_BLEND);
//glEnable2(GL_TEXTURE_2D);
//glEnable2(GL_CULL_FACE);
//sw.stop();
//sw.printEvery(100, "buttons");
}
const RectangleArea& TouchscreenInput_TestFps::getRectangleArea()
{
return _boundingRectangle;
}
const RectangleArea& TouchscreenInput_TestFps::getPauseRectangleArea()
{
return *aPause;
}
void TouchscreenInput_TestFps::rebuild() {
if (_options->hideGui)
return;
Tesselator& t = Tesselator::instance;
//LOGI("instance is: %p, %p, %p, %p, %p FOR %d\n", &t, aLeft, aRight, aUp, aDown, aJump, _bufferId);
//t.setAccessMode(Tesselator::ACCESS_DYNAMIC);
t.begin();
const int imageU = 0;
const int imageV = 107;
const int imageSize = 26;
bool northDiagonals = !isChangingFlightHeight && (_northJump || _forward);
// render left button
if (northDiagonals || isChangingFlightHeight) t.colorABGR(cDiscreet);
else if (isButtonDown(AREA_DPAD_W)) t.colorABGR(cPressed);
else t.colorABGR(cReleased);
drawRectangleArea(t, aLeft, imageU + imageSize, imageV, (float)imageSize);
// render right button
if (northDiagonals || isChangingFlightHeight) t.colorABGR(cDiscreet);
else if (isButtonDown(AREA_DPAD_E)) t.colorABGR(cPressed);
else t.colorABGR(cReleased);
drawRectangleArea(t, aRight, imageU + imageSize * 3, imageV, (float)imageSize);
// render forward button
if (isButtonDown(AREA_DPAD_N)) t.colorABGR(cPressed);
else t.colorABGR(cReleased);
if (isChangingFlightHeight)
{
drawRectangleArea(t, aUp, imageU + imageSize * 2, imageV + imageSize, (float)imageSize);
}
else
{
drawRectangleArea(t, aUp, imageU, imageV, (float)imageSize);
}
// render diagonals, if available
if (northDiagonals)
{
t.colorABGR(cReleased);
drawRectangleArea(t, aUpLeft, imageU, imageV + imageSize, (float)imageSize);
drawRectangleArea(t, aUpRight, imageU + imageSize, imageV + imageSize, (float)imageSize);
}
// render backwards button
if (northDiagonals) t.colorABGR(cDiscreet);
else if (isButtonDown(AREA_DPAD_S)) t.colorABGR(cPressed);
else t.colorABGR(cReleased);
if (isChangingFlightHeight)
{
drawRectangleArea(t, aDown, imageU + imageSize * 3, imageV + imageSize, (float)imageSize);
}
else
{
drawRectangleArea(t, aDown, imageU + imageSize * 2, imageV, (float)imageSize);
}
// render jump / flight button
if (_renderFlightImage && northDiagonals) t.colorABGR(cDiscreet);
else if (isButtonDown(AREA_DPAD_C)) t.colorABGR(cPressed);
else t.colorABGR(cReleased);
if (_renderFlightImage)
{
drawRectangleArea(t, aJump, imageU + imageSize * 4, imageV + imageSize, (float)imageSize);
}
else
{
drawRectangleArea(t, aJump, imageU + imageSize * 4, imageV, (float)imageSize);
}
#ifdef __APPLE__
if (!_minecraft->screen) {
if (isButtonDown(AREA_PAUSE)) t.colorABGR(cPressedPause);
else t.colorABGR(cReleasedPause);
drawRectangleArea(t, aPause, 200, 64, 18.0f);
}
#endif /*__APPLE__*/
//t.end(true, _bufferId);
//return;
t.draw();
//RenderChunk _render = t.end(true, _bufferId);
//t.setAccessMode(Tesselator::ACCESS_STATIC);
//_bufferId = _render.vboId;
}

View File

@@ -0,0 +1,79 @@
#ifndef NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchscreenInput_H__
#define NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchscreenInput_H__
//package net.minecraft.client.player;
#include "../IMoveInput.h"
#include "../../../gui/GuiComponent.h"
#include "TouchAreaModel.h"
#include "../../../renderer/RenderChunk.h"
class Options;
class Player;
class Minecraft;
class PolygonArea;
// @todo: extract a separate MoveInput (-> merge XperiaPlayInput)
class TouchscreenInput_TestFps: public IMoveInput,
public GuiComponent
{
public:
static const int KEY_UP = 0;
static const int KEY_DOWN = 1;
static const int KEY_LEFT = 2;
static const int KEY_RIGHT = 3;
static const int KEY_JUMP = 4;
static const int KEY_SNEAK = 5;
static const int KEY_CRAFT = 6;
static const int NumKeys = 7;
TouchscreenInput_TestFps(Minecraft* mc, Options* options);
~TouchscreenInput_TestFps();
void onConfigChanged(const Config& c);
void tick(Player* player);
void render(float a);
void setKey(int key, bool state);
void releaseAllKeys();
const RectangleArea& getRectangleArea();
const RectangleArea& getPauseRectangleArea();
private:
void clear();
RectangleArea _boundingRectangle;
bool _keys[NumKeys];
Options* _options;
bool _pressedJump;
bool _forward;
bool _northJump;
bool _renderFlightImage;
TouchAreaModel _model;
Minecraft* _minecraft;
RectangleArea* aLeft;
RectangleArea* aRight;
RectangleArea* aUp;
RectangleArea* aDown;
RectangleArea* aPause;
//RectangleArea* aUpJump;
RectangleArea* aJump;
RectangleArea* aUpLeft;
RectangleArea* aUpRight;
bool _pauseIsDown;
RenderChunk _render;
bool _allowHeightChange;
float _sneakTapTime;
bool _buttons[8];
bool isButtonDown(int areaId);
void rebuild();
};
#endif /*NET_MINECRAFT_CLIENT_PLAYER_INPUT_TOUCHSCREEN_TouchscreenInput_H__*/