Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crosshair support for Android #7865

Merged
merged 7 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move crosshair drawing calculation into game.cpp
Crosshair drawing calculation is the same as non-touch control, except when in first-person camera.
Rename setting: use_crosshair -> touch_use_crosshair
  • Loading branch information
srifqi committed Sep 28, 2022
commit ac6d75d7eea003950284845afb6c4e2a81535991
2 changes: 1 addition & 1 deletion builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ 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
touch_use_crosshair (Use crosshair for touch screen) bool false

# (Android) Fixes the position of virtual joystick.
# If disabled, virtual joystick will center to first-touch's position.
Expand Down
17 changes: 7 additions & 10 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ bool Game::startup(bool *kill,

#ifdef HAVE_TOUCHSCREENGUI
m_android_touchtarget = g_settings->getBool("touchtarget");
m_android_use_crosshair = g_settings->getBool("use_crosshair");
m_android_use_crosshair = g_settings->getBool("touch_use_crosshair");
#endif

g_client_translations->clear();
Expand Down Expand Up @@ -2990,7 +2990,8 @@ void Game::updateCamera(f32 dtime)

#ifdef HAVE_TOUCHSCREENGUI
if (g_touchscreengui)
g_touchscreengui->setCameraMode(camera->getCameraMode());
g_touchscreengui->setUseCrosshair(m_android_use_crosshair ||
camera->getCameraMode() == CAMERA_MODE_THIRD);
srifqi marked this conversation as resolved.
Show resolved Hide resolved
#endif

// Make the player visible depending on camera mode.
Expand Down Expand Up @@ -3103,8 +3104,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud)
shootline.end = shootline.start + camera_direction * BS * d;

#ifdef HAVE_TOUCHSCREENGUI
if (g_settings->getBool("touchtarget") && g_touchscreengui &&
!g_settings->getBool("use_crosshair") &&
if (m_android_touchtarget && g_touchscreengui && !m_android_use_crosshair &&
camera->getCameraMode() == CAMERA_MODE_FIRST) {
shootline = g_touchscreengui->getShootline();
// Scale shootline to the acual distance the player can reach
Expand Down Expand Up @@ -3999,15 +3999,12 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
bool draw_wield_tool = (m_game_ui->m_flags.show_hud &&
(player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
(camera->getCameraMode() == CAMERA_MODE_FIRST));
#ifndef HAVE_TOUCHSCREENGUI
bool draw_crosshair = (
(player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
(camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT));
#else
bool draw_crosshair = !m_android_touchtarget ||
(m_android_use_crosshair &&
camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT) ||
camera->getCameraMode() == CAMERA_MODE_THIRD;
#ifdef HAVE_TOUCHSCREENGUI
if (!m_android_use_crosshair && camera->getCameraMode() == CAMERA_MODE_FIRST)
draw_crosshair = false;
#endif
m_rendering_engine->draw_scene(skycolor, m_game_ui->m_flags.show_hud,
m_game_ui->m_flags.show_minimap, draw_wield_tool, draw_crosshair);
Expand Down
2 changes: 1 addition & 1 deletion src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ void set_default_settings()
#ifdef HAVE_TOUCHSCREENGUI
settings->setDefault("touchtarget", "true");
settings->setDefault("touchscreen_threshold","20");
settings->setDefault("use_crosshair", "false");
settings->setDefault("touch_use_crosshair", "false");
settings->setDefault("fixed_virtual_joystick", "false");
settings->setDefault("virtual_joystick_triggers_aux1", "false");
settings->setDefault("clickable_chat_weblinks", "false");
Expand Down
35 changes: 15 additions & 20 deletions src/gui/touchscreengui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ 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_aux1 = g_settings->getBool("virtual_joystick_triggers_aux1");
m_use_crosshair = g_settings->getBool("use_crosshair");
m_screensize = m_device->getVideoDriver()->getScreenSize();
button_size = MYMIN(m_screensize.Y / 4.5f,
RenderingEngine::getDisplayDensity() *
Expand Down Expand Up @@ -679,16 +678,15 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
auto *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;
translated->MouseInput.Shift = false;
translated->MouseInput.Control = false;
translated->MouseInput.ButtonStates = 0;
translated->MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
if (m_use_crosshair || m_camera_mode == CAMERA_MODE_THIRD) {
if (m_draw_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;
}
m_receiver->OnEvent(*translated);
delete translated;
Expand Down Expand Up @@ -809,11 +807,10 @@ 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);
m_move_sent_as_mouse_event = false;
if (m_use_crosshair || m_camera_mode == CAMERA_MODE_THIRD)
if (m_draw_crosshair)
m_move_downlocation = v2s32(m_screensize.X / 2, m_screensize.Y / 2);
else
m_move_downlocation = v2s32(event.TouchInput.X, event.TouchInput.Y);
}
}
}
Expand All @@ -833,8 +830,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)

if (m_has_move_id) {
if (event.TouchInput.ID == m_move_id &&
(!m_move_sent_as_mouse_event || m_use_crosshair ||
m_camera_mode == CAMERA_MODE_THIRD)) {
(!m_move_sent_as_mouse_event || m_draw_crosshair)) {
double distance = sqrt(
(m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) *
(m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) +
Expand All @@ -859,7 +855,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
m_camera_pitch = MYMIN(MYMAX(m_camera_pitch + (dy * d), -180), 180);

// update shootline
if (m_use_crosshair || m_camera_mode == CAMERA_MODE_THIRD) {
if (m_draw_crosshair) {
TurkeyMcMac marked this conversation as resolved.
Show resolved Hide resolved
X = m_screensize.X / 2;
Y = m_screensize.Y / 2;
}
Expand Down Expand Up @@ -1023,18 +1019,17 @@ 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 || m_camera_mode == CAMERA_MODE_THIRD) {
mX = m_screensize.X / 2;
mY = m_screensize.Y / 2;
v2s32 mPos = v2s32(m_key_events[0].x, m_key_events[0].y);
if (m_draw_crosshair) {
mPos.X = m_screensize.X / 2;
mPos.Y = m_screensize.Y / 2;
}

auto *translated = new SEvent();
memset(translated, 0, sizeof(SEvent));
translated->EventType = EET_MOUSE_INPUT_EVENT;
translated->MouseInput.X = mX;
translated->MouseInput.Y = mY;
translated->MouseInput.X = mPos.X;
translated->MouseInput.Y = mPos.Y;
translated->MouseInput.Shift = false;
translated->MouseInput.Control = false;
translated->MouseInput.ButtonStates = EMBSM_RIGHT;
Expand All @@ -1043,7 +1038,7 @@ bool TouchScreenGUI::doubleTapDetection()
m_shootline = m_device
->getSceneManager()
->getSceneCollisionManager()
->getRayFromScreenCoordinates(v2s32(mX, mY));
->getRayFromScreenCoordinates(mPos);

translated->MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
verbosestream << "TouchScreenGUI::translateEvent right click press" << std::endl;
Expand Down Expand Up @@ -1146,7 +1141,7 @@ void TouchScreenGUI::step(float dtime)
if (delta > MIN_DIG_TIME_MS) {
s32 mX = m_move_downlocation.X;
s32 mY = m_move_downlocation.Y;
if (m_use_crosshair || m_camera_mode == CAMERA_MODE_THIRD) {
if (m_draw_crosshair) {
mX = m_screensize.X / 2;
mY = m_screensize.Y / 2;
}
Expand Down
7 changes: 2 additions & 5 deletions src/gui/touchscreengui.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class TouchScreenGUI
void step(float dtime);
void resetHud();
void registerHudItem(int index, const rect<s32> &rect);
inline void setCameraMode(CameraMode camera_mode) { m_camera_mode = camera_mode; }
inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
void Toggle(bool visible);

void hide();
Expand All @@ -217,9 +217,6 @@ class TouchScreenGUI
double m_camera_yaw_change = 0.0;
double m_camera_pitch = 0.0;

// camera mode (used for crosshair)
CameraMode m_camera_mode = CAMERA_MODE_FIRST;

// forward, backward, left, right
touch_gui_button_id m_joystick_names[5] = {
forward_id, backward_id, left_id, right_id, aux1_id};
Expand All @@ -245,7 +242,7 @@ class TouchScreenGUI
bool m_joystick_has_really_moved = false;
bool m_fixed_joystick = false;
bool m_joystick_triggers_aux1 = false;
bool m_use_crosshair = false;
bool m_draw_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