From 72ee43e9470c6fdbc6ed7ff92b85dfa5062c5762 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 6 Jan 2015 18:51:37 +0000 Subject: Added separate event handlers for mouse button clicks and mouse wheel movement. --- src/client/input.cc | 190 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 74 deletions(-) (limited to 'src/client/input.cc') diff --git a/src/client/input.cc b/src/client/input.cc index 7f3a021..cf0c986 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -4,7 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include "audio/audio.h" #include "auxiliary/functions.h" @@ -265,10 +265,6 @@ void init() keyboard = new Keyboard(); - SDL_ShowCursor(SDL_DISABLE); - SDL_WM_GrabInput(SDL_GRAB_ON); -// SDL_EnableUNICODE(1); - Joystick::init(); input_mousecontrol = core::Cvar::get("input_mousecontrol", 1.0f, core::Cvar::Archive); @@ -286,9 +282,6 @@ void init() input_grab = core::Cvar::get("input_grab", 1.0f, core::Cvar::Archive); input_grab->set_info("[bool] grab input"); - if (!input_grab->value()) { - SDL_WM_GrabInput(SDL_GRAB_OFF); - } core::Func *func = 0; func = core::Func::add("ui_control", func_ui_control); @@ -351,11 +344,6 @@ void shutdown() delete keyboard; keyboard = 0; } - - SDL_ShowCursor(SDL_ENABLE); - SDL_WM_GrabInput(SDL_GRAB_OFF); -// SDL_DisableUNICODE(0); - } void action_press(Key *key) @@ -424,16 +412,36 @@ void action_press(Key *key) /* -- camera control ------------------------------ */ case Action::CamLeft: - render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamRight: - render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamUp: - render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamDown: - render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::ZoomIn: render::Camera::set_zoom(-0.1f); @@ -545,10 +553,10 @@ void action_release(Key *key) render::Camera::set_direction(0.0f); break; case Action::CamUp: - render::Camera::set_pitch(0); + render::Camera::set_pitch(0.0f); break; case Action::CamDown: - render::Camera::set_pitch(0); + render::Camera::set_pitch(0.0f); break; case Action::ZoomIn: break; @@ -586,7 +594,7 @@ Key::Modifier modifier() return Key::None; } -void key_pressed(Key *key) +bool console_key_pressed(const Key *key) { if (key->action() && (key->action()->id() == Action::Console)) { local_direction = 0.0f; @@ -597,57 +605,48 @@ void key_pressed(Key *key) render::Camera::set_pitch(0.0f); ui::console()->toggle(); - return; - - } else if (ui::root()->input_key(true, Keyboard::translate_keysym(key->sym(), keyboard_modifiers), keyboard_modifiers)) { - return; + return true; + } else + { + return false; + } +} - } else if (key->bind(modifier()).size() && core::application()->connected()) { +void key_pressed(Key *key) +{ + if (!core::localplayer()->view() && core::localcontrol()) { - if (!core::localplayer()->view() && core::localcontrol()) { - - if (key->action()) { - action_press(key); - return; - } - - const char c = key->bind(modifier()).c_str()[0]; - if (c == '@') { - // target bind - if (targets::current_id()) { - core::cmd() << key->bind(modifier()) << " " << targets::current_id() << "\n"; - } - return; - } else if (c) { - // normal bind - core::cmd() << key->bind(modifier()) << "\n"; - return; - } - - } else { - const char c = key->bind(modifier()).c_str()[0]; - if (c && c != '+' && c != '@') { - // normal bind - core::cmd() << key->bind(modifier()) << "\n"; - return; + if (key->action()) { + action_press(key); + return; + } + + const char c = key->bind(modifier()).c_str()[0]; + if (c == '@') { + // target bind + if (targets::current_id()) { + core::cmd() << key->bind(modifier()) << " " << targets::current_id() << "\n"; } + return; + } else if (c) { + // normal bind + core::cmd() << key->bind(modifier()) << "\n"; + return; + } + + } else { + const char c = key->bind(modifier()).c_str()[0]; + if (c && c != '+' && c != '@') { + // normal bind + core::cmd() << key->bind(modifier()) << "\n"; + return; } } } void key_released(Key *key) { - ui::root()->input_key(false, Keyboard::translate_keysym(key->sym(), keyboard_modifiers), keyboard_modifiers); - - if (core::application()->connected() && core::localcontrol()) { - - // FIXME mouse release selection should be handled inside the hud - // note: mouse button can double as an action key - if ((key->sym() == 512 + SDL_BUTTON_LEFT) && targets::hover() && (key->waspressed() <= (input_mousedelay->value() / 1000.0f))) { - // hovering target selected - targets::set_target(targets::hover()); - } - + if (core::localcontrol()) { if (key->action()) { action_release(key); } @@ -776,7 +775,6 @@ void frame() SDL_Event event; Key *key = 0; bool pressed = false; - bool mouse_moved = false; if (last_key_time + 1.0f < client()->time()) { last_key = 0; @@ -789,25 +787,63 @@ void frame() key = 0; switch (event.type) { - case SDL_VIDEORESIZE: - video::resize(event.resize.w, event.resize.h); + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_RESIZED) + { + video::resize(event.window.data1, event.window.data2); + } break; case SDL_MOUSEMOTION: mouse_x = event.motion.x; mouse_y = event.motion.y; - mouse_moved = true; mouse_lastmoved = client()->time(); + ui::root()->input_mouse((float) mouse_x, (float) mouse_y); break; case SDL_MOUSEBUTTONDOWN: key = keyboard->press(512 + event.button.button); pressed = true; + if (key && (console_key_pressed(key) || ui::root()->input_mouse_button(true, event.button.button))) + { + key = 0; + } break; case SDL_MOUSEBUTTONUP: key = keyboard->release(512 + event.button.button); pressed = false; + if (ui::root()->input_mouse_button(false, event.button.button)) + { + key = 0; + } + else if (key && core::localcontrol()) + { + // FIXME mouse release selection should be handled inside the hud + // note: mouse button can double as an action key + if ((event.button.button == SDL_BUTTON_LEFT) && targets::hover() && (key->waspressed() <= (input_mousedelay->value() / 1000.0f))) + { + // hovering target selected + targets::set_target(targets::hover()); + } + } + break; + + case SDL_MOUSEWHEEL: + if (ui::root()->input_mouse_wheel(math::Vector2f((float)event.wheel.x, (float) event.wheel.y))) + { + key = 0; + } else + { + if (event.wheel.y > 0) + { + key = keyboard->press(600); + } else if (event.wheel.y < 0) + { + key = keyboard->press(601); + } + pressed = true; + } break; case SDL_JOYBUTTONDOWN: @@ -827,14 +863,22 @@ void frame() case SDL_KEYDOWN: keyboard_modifiers = event.key.keysym.mod; - key = keyboard->press(event.key.keysym.sym); + key = keyboard->press(event.key.keysym.scancode); pressed = true; + if (console_key_pressed(key) || ui::root()->input_key(true, Keyboard::translate_keysym(event.key.keysym.sym, keyboard_modifiers), keyboard_modifiers)) + { + key = 0; + } break; case SDL_KEYUP: keyboard_modifiers = event.key.keysym.mod; - key = keyboard->release(event.key.keysym.sym); + key = keyboard->release(event.key.keysym.scancode); pressed = false; + if (ui::root()->input_key(false, Keyboard::translate_keysym(event.key.keysym.sym, keyboard_modifiers), keyboard_modifiers)) + { + key = 0; + } break; case SDL_QUIT: @@ -843,6 +887,7 @@ void frame() break; } + // handle binds if (key) { if (pressed) { key_pressed(key); @@ -854,9 +899,6 @@ void frame() } } - if (mouse_moved) - ui::root()->input_mouse((float) mouse_x, (float) mouse_y); - /* -- handle key repeat --------------------------- */ float delay = 200.0f; // key delay time-out in milliseconds if (input_keydelay) { @@ -880,7 +922,7 @@ void frame() for (Keyboard::Keys::iterator it = keyboard->keys().begin(); it != keyboard->keys().end(); ++it) { key = (*it).second; - if (key && (key->sym() < 512) && key->pressed()) { + if (key && (key->scancode() < 512) && key->pressed()) { if ((key->pressed() + delay < core::application()->time()) && (key->lastpressed() + repeat < core::application()->time())) { key->key_lastpressed = core::application()->time(); key_pressed(key); @@ -999,8 +1041,8 @@ void frame() } } else { - render::Camera::set_direction(0.0f); - render::Camera::set_pitch(0.0f); + //render::Camera::set_direction(0.0f); + //render::Camera::set_pitch(0.0f); // disable autopilot override local_controlflags = local_controlflags & ~core::EntityControlable::ControlFlagOverride; -- cgit v1.2.3