Skip to content

Commit

Permalink
Add crosshair support for Android
Browse files Browse the repository at this point in the history
If enabled, a crosshair will be shown to select object.
This will give Android player a way to play like they play on desktop.
  • Loading branch information
srifqi committed Nov 13, 2018
1 parent 6b102ce commit fb20a91
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
4 changes: 4 additions & 0 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ continuous_forward (Continuous forward) bool false
# The length in pixels it takes for touch screen interaction to start.
touchscreen_threshold (Touch screen threshold) int 20 0 100

# (Android) Use crosshair to select object instead of whole screen.
# If enabled, a crosshair will be shown and will be used for selecting object.
use_crosshair (Use crosshair) bool false

# (Android) Fixes the position of virtual joystick.
# If disabled, virtual joystick will center to first-touch's position.
fixed_virtual_joystick (Fixed virtual joystick) bool false
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("touchtarget", "true");
settings->setDefault("TMPFolder","/sdcard/" PROJECT_NAME_C "/tmp/");
settings->setDefault("touchscreen_threshold","20");
settings->setDefault("use_crosshair", "false");
settings->setDefault("fixed_virtual_joystick", "false");
settings->setDefault("virtual_joystick_triggers_aux", "false");
settings->setDefault("smooth_lighting", "false");
Expand Down
3 changes: 2 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3822,7 +3822,8 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
(camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT));
#ifdef HAVE_TOUCHSCREENGUI
try {
draw_crosshair = !g_settings->getBool("touchtarget");
draw_crosshair = !g_settings->getBool("touchtarget") ||
g_settings->getBool("use_crosshair");
} catch (SettingNotFoundException) {
}
#endif
Expand Down
52 changes: 41 additions & 11 deletions src/gui/touchscreengui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver)
m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick");
m_joystick_triggers_special1 = g_settings->getBool("virtual_joystick_triggers_aux");
m_use_crosshair = g_settings->getBool("use_crosshair");
m_screensize = m_device->getVideoDriver()->getScreenSize();
}

Expand Down Expand Up @@ -761,8 +762,13 @@ void TouchScreenGUI::handleReleaseEvent(int evt_id)
SEvent *translated = new SEvent;
memset(translated, 0, sizeof(SEvent));
translated->EventType = EET_MOUSE_INPUT_EVENT;
translated->MouseInput.X = m_move_downlocation.X;
translated->MouseInput.Y = m_move_downlocation.Y;
if (m_use_crosshair) {
translated->MouseInput.X = m_screensize.X / 2;
translated->MouseInput.Y = m_screensize.Y / 2;
} else {
translated->MouseInput.X = m_move_downlocation.X;
translated->MouseInput.Y = m_move_downlocation.Y;
}
translated->MouseInput.Shift = false;
translated->MouseInput.Control = false;
translated->MouseInput.ButtonStates = 0;
Expand Down Expand Up @@ -886,7 +892,11 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
m_move_id = event.TouchInput.ID;
m_move_has_really_moved = false;
m_move_downtime = porting::getTimeMs();
m_move_downlocation = v2s32(event.TouchInput.X, event.TouchInput.Y);
if (m_use_crosshair) {
m_move_downlocation = v2s32(m_screensize.X / 2, m_screensize.Y / 2);
} else {
m_move_downlocation = v2s32(event.TouchInput.X, event.TouchInput.Y);
}
m_move_sent_as_mouse_event = false;
}
}
Expand All @@ -909,7 +919,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event)

if (m_move_id != -1) {
if ((event.TouchInput.ID == m_move_id) &&
(!m_move_sent_as_mouse_event)) {
(!m_move_sent_as_mouse_event ||
m_use_crosshair)) {

double distance = sqrt(
(m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) *
Expand All @@ -926,6 +937,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
// update camera_yaw and camera_pitch
s32 dx = X - m_pointerpos[event.TouchInput.ID].X;
s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y;
m_pointerpos[event.TouchInput.ID] = v2s32(X, Y);

// adapt to similar behaviour as pc screen
double d = g_settings->getFloat("mouse_sensitivity") * 4;
Expand All @@ -936,11 +948,14 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
m_camera_pitch = MYMIN(MYMAX(m_camera_pitch + (dy * d), -180), 180);

// update shootline
if (m_use_crosshair) {
X = m_screensize.X / 2;
Y = m_screensize.Y / 2;
}
m_shootline = m_device
->getSceneManager()
->getSceneCollisionManager()
->getRayFromScreenCoordinates(v2s32(X, Y));
m_pointerpos[event.TouchInput.ID] = v2s32(X, Y);
}
}
else if ((event.TouchInput.ID == m_move_id) &&
Expand Down Expand Up @@ -1109,11 +1124,18 @@ bool TouchScreenGUI::doubleTapDetection()
if (distance > (20 + m_touchscreen_threshold))
return false;

s32 mX = m_key_events[0].x;
s32 mY = m_key_events[0].y;
if (m_use_crosshair) {
mX = m_screensize.X / 2;
mY = m_screensize.Y / 2;
}

SEvent *translated = new SEvent();
memset(translated, 0, sizeof(SEvent));
translated->EventType = EET_MOUSE_INPUT_EVENT;
translated->MouseInput.X = m_key_events[0].x;
translated->MouseInput.Y = m_key_events[0].y;
translated->MouseInput.X = mX;
translated->MouseInput.Y = mY;
translated->MouseInput.Shift = false;
translated->MouseInput.Control = false;
translated->MouseInput.ButtonStates = EMBSM_RIGHT;
Expand All @@ -1122,7 +1144,7 @@ bool TouchScreenGUI::doubleTapDetection()
m_shootline = m_device
->getSceneManager()
->getSceneCollisionManager()
->getRayFromScreenCoordinates(v2s32(m_key_events[0].x, m_key_events[0].y));
->getRayFromScreenCoordinates(v2s32(mX, mY));

translated->MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
verbosestream << "TouchScreenGUI::translateEvent right click press" << std::endl;
Expand Down Expand Up @@ -1221,17 +1243,25 @@ void TouchScreenGUI::step(float dtime)
u64 delta = porting::getDeltaMs(m_move_downtime, porting::getTimeMs());

if (delta > MIN_DIG_TIME_MS) {
s32 mX = m_move_downlocation.X;
s32 mY = m_move_downlocation.Y;
if (m_use_crosshair) {
mX = m_screensize.X / 2;
mY = m_screensize.Y / 2;
}
actionstream << "TouchScreenGUI::step mX = " << mX
<< ", mY = " << mY << std::endl;
m_shootline = m_device
->getSceneManager()
->getSceneCollisionManager()
->getRayFromScreenCoordinates(
v2s32(m_move_downlocation.X,m_move_downlocation.Y));
v2s32(mX, mY));

SEvent translated;
memset(&translated, 0, sizeof(SEvent));
translated.EventType = EET_MOUSE_INPUT_EVENT;
translated.MouseInput.X = m_move_downlocation.X;
translated.MouseInput.Y = m_move_downlocation.Y;
translated.MouseInput.X = mX;
translated.MouseInput.Y = mY;
translated.MouseInput.Shift = false;
translated.MouseInput.Control = false;
translated.MouseInput.ButtonStates = EMBSM_LEFT;
Expand Down
1 change: 1 addition & 0 deletions src/gui/touchscreengui.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class TouchScreenGUI
bool m_joystick_has_really_moved = false;
bool m_fixed_joystick = false;
bool m_joystick_triggers_special1 = false;
bool m_use_crosshair = false;
button_info *m_joystick_btn_off = nullptr;
button_info *m_joystick_btn_bg = nullptr;
button_info *m_joystick_btn_center = nullptr;
Expand Down

0 comments on commit fb20a91

Please sign in to comment.