diff --git a/src/platform/nacl/nacl-instance.cpp b/src/platform/nacl/nacl-instance.cpp index 0c54056b..374b245b 100644 --- a/src/platform/nacl/nacl-instance.cpp +++ b/src/platform/nacl/nacl-instance.cpp @@ -34,17 +34,55 @@ void lol_nacl_main(int argc, char **argv, char **envp) {} namespace lol { +/* + * NACL Input implementation class + * This is a ripoff of the SDL one + */ + +class NaClInputData +{ + friend class NaClInstance; + +private: + void Tick(float seconds); + bool IsViewportSizeValid() { return (m_app.x > 0.f && m_app.y > 0.f && m_screen.x > 0.f && m_screen.y > 0.f); } + void InitViewportSize(); + + static void SetMousePos(ivec2 position); + + NaClInputData() : + m_prevmouse(ivec2::zero), + m_mousecapture(false) + { + InitViewportSize(); + } + + Array m_input_events; + InputDeviceInternal* m_mouse; + InputDeviceInternal* m_keyboard; + + vec2 m_app; + vec2 m_screen; + bool m_mousecapture; +}; + NaClInstance::NaClInstance(PP_Instance instance) : pp::Instance(instance), + m_input_data(new NaClInputData()), m_size(0, 0) { - RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); + RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL); + RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); + + m_data->m_keyboard = InputDeviceInternal::CreateStandardKeyboard(); + m_data->m_mouse = InputDeviceInternal::CreateStandardMouse(); } NaClInstance::~NaClInstance() { // Destroy the cube view while GL context is current. m_opengl_ctx->MakeContextCurrent(this); + delete m_input_data; } static double const DELTA_MS = 1000.0 / 60.0; @@ -58,29 +96,8 @@ void NaClInstance::TickCallback(void* data, int32_t result) pp::Module::Get()->core()->CallOnMainThread( DELTA_MS, pp::CompletionCallback(&TickCallback, data), PP_OK); - /* 12/09/2013 : Deactivated to get build back. - PP_GamepadsSampleData all_pads_data; - instance->m_pad_interface->Sample(instance->pp_instance(), &all_pads_data); - - for (int i = 0; i < all_pads_data.length; i++) - { - PP_GamepadSampleData const& pad_data = all_pads_data.items[i]; - - if (i >= instance->m_sticks.Count()) - { - Stick *stick = Input::CreateStick(); - instance->m_sticks.Push(stick); - } - - instance->m_sticks[i]->SetAxisCount(pad_data.axes_length); - for (int j = 0; j < pad_data.axes_length; j++) - instance->m_sticks[i]->SetAxis(j, pad_data.axes[j]); - - instance->m_sticks[i]->SetButtonCount(pad_data.buttons_length); - for (int j = 0; j < pad_data.buttons_length; j++) - instance->m_sticks[i]->SetButton(j, pad_data.buttons[j] > 0.5f); - } - */ + //Tick input + m_input_data->Tick(seconds); } Mutex NaClInstance::main_mutex; @@ -161,23 +178,8 @@ void NaClInstance::DidChangeView(const pp::Rect& position, const pp::Rect& clip) bool NaClInstance::HandleInputEvent(const pp::InputEvent& event) { - ///--------------------------------- - /* 12/09/2013 : Deactivated to get build back. - switch (event.GetType()) - { - case PP_INPUTEVENT_TYPE_MOUSEDOWN: - Input::SetMouseButton(pp::MouseInputEvent(event).GetButton()); - break; - case PP_INPUTEVENT_TYPE_MOUSEUP: - Input::UnsetMouseButton(pp::MouseInputEvent(event).GetButton()); - break; - case PP_INPUTEVENT_TYPE_MOUSEMOVE: - Input::SetMousePos(ivec2(pp::MouseInputEvent(event).GetPosition().x(), m_opengl_ctx->GetSize().height() - 1 - pp::MouseInputEvent(event).GetPosition().y())); - break; - default: - break; - } - */ + m_input_events << event; + return true; } @@ -191,5 +193,105 @@ void NaClInstance::DrawSelf() m_opengl_ctx->FlushContext(); } +void NaClInputData::Tick(float seconds) +{ + //Init cursor position, if mouse didn't move. + ivec2 mousepos = m_mouse->GetCursorPixelPos(0); + vec2 mousepos_prev = vec2(mousepos); + + /* Handle keyboard and WM events */ + for (int i = 0; i < m_input_events.Count(); ++i) + { + pp::InputEvent &e = m_input_events[i]; + switch (e.GetType()) + { + case PP_INPUTEVENT_TYPE_UNDEFINED: + { + break; + } + case PP_INPUTEVENT_TYPE_MOUSEDOWN: + case PP_INPUTEVENT_TYPE_MOUSEUP: + { + pp::MouseInputEvent em = &pp::MouseInputEvent(e); + m_mouse->SetKey(em.GetButton() - 1, em.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN); + break; + } + case PP_INPUTEVENT_TYPE_MOUSELEAVE: + case PP_INPUTEVENT_TYPE_MOUSEENTER: + { + /* TODO: "InScreen" hardcoded, not nice */ + pp::MouseInputEvent em = &pp::MouseInputEvent(e); + m_mouse->SetKey(3, em.GetType() == PP_INPUTEVENT_TYPE_MOUSELEAVE); + break; + } + case PP_INPUTEVENT_TYPE_MOUSEMOVE: + { + pp::MouseInputEvent em = &pp::MouseInputEvent(e); + mousepos = ivec2(em.GetPosition().x(), em.GetPosition().y()); + break; + } + case PP_INPUTEVENT_TYPE_WHEEL: + { + /* TODO: MOUSE WHEEL NOT IMPLEMENTED IN LOL */ + break; + } + // Use ? + //case PP_INPUTEVENT_TYPE_RAWKEYDOWN: + //case PP_INPUTEVENT_TYPE_CHAR: + case PP_INPUTEVENT_TYPE_KEYDOWN: + case PP_INPUTEVENT_TYPE_KEYUP: + { + pp::KeyboardInputEvent ek = &pp::KeyboardInputEvent(e); + m_keyboard->SetKey(ek.SetKeyCode(), ek.GetType() == PP_INPUTEVENT_TYPE_KEYUP); + break; + } + } + } + + /* Handle mouse input */ + if (IsViewportSizeValid()) + { + if (mousepos.x >= 0 && mousepos.x < m_app.x && mousepos.y >= 0 && mousepos.y < m_app.y) + { + vec2 vmousepos = vec2(mousepos); + m_mouse->SetCursor(0, vmousepos / m_app, mousepos); + // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick + m_mouse->SetAxis(0, (vmousepos.x - mousepos_prev.x) * 100.0f / m_screen.x); + // Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction) + m_mouse->SetAxis(1,-(vmousepos.y - mousepos_prev.y) * 100.0f / m_screen.y); + } + + if (m_mousecapture) + { + /* + mousepos = ivec2(m_app * .5f); + NaClInputData::SetMousePos(mousepos); + */ + } + } +} + +//---- +void NaClInputData::InitViewportSize() +{ + if (g_scene) + { + m_app = vec2(Video::GetSize()); + //Dunno if its the good idea. + m_screen = vec2(Video::GetSize()); + } + else + { + m_app = vec2(-1.f); + m_screen = vec2(-1.f); + } +} + +void NaClInputData::SetMousePos(ivec2 position) +{ + //? How to do that ? + //SDL_WarpMouse((uint16_t)position.x, (uint16_t)position.y); +} + } // namespace lol diff --git a/src/platform/nacl/nacl-instance.h b/src/platform/nacl/nacl-instance.h index 69b6b12e..2e61d1cc 100644 --- a/src/platform/nacl/nacl-instance.h +++ b/src/platform/nacl/nacl-instance.h @@ -54,8 +54,8 @@ private: /* Gamepad support */ PPB_Gamepad const *m_pad_interface; - //12/09/2013 : Should use new system. Array m_sticks; - Array m_sticks; + //12/09/2013 : Should use new system. + NaClInputData m_input_data; /* Communication with the application object */ struct Args diff --git a/test/meshviewer.cpp b/test/meshviewer.cpp index efbb503d..b167dee6 100644 --- a/test/meshviewer.cpp +++ b/test/meshviewer.cpp @@ -26,6 +26,7 @@ static int const TEXTURE_WIDTH = 256; #define NO_NACL_EM (!__native_client__ && !EMSCRIPTEN) #define NACL_EM (__native_client__ || EMSCRIPTEN) +#define NO_NACL_EM_INPUT (1 && NO_NACL_EM) #define R_M 1.f #if NACL_EM @@ -56,6 +57,7 @@ static int const TEXTURE_WIDTH = 256; #define WITH_TEXTURE 0 + #define HAS_KBOARD (m_input_usage & (1<GetKey(index).IsReleased()); } bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsPressed()); } bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsDown()); } @@ -217,58 +219,58 @@ public: bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsPressed()); } bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsDown()); } float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE)?(m_controller->GetAxis(index).GetValue()):(0.f); } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT void Init() { m_init = true; m_input_usage = 0; -#if NO_NACL_EM +#if NO_NACL_EM_INPUT /* Register an input controller for the keyboard */ m_controller = new Controller("Default", MAX_KEYS, MAX_AXIS); - if (InputDevice::Get("Mouse")) + if (InputDevice::Get(g_name_mouse.C())) { m_input_usage |= (1<GetKey(MSE_CAM_ROT).Bind("Mouse", "Left"); - m_controller->GetKey(MSE_CAM_POS).Bind("Mouse", "Right"); - m_controller->GetKey(MSE_CAM_FOV).Bind("Mouse", "Middle"); - m_controller->GetAxis(MSEX_CAM_Y).Bind("Mouse", "Y"); - m_controller->GetAxis(MSEX_CAM_X).Bind("Mouse", "X"); + m_controller->GetKey(MSE_CAM_ROT).BindMouse("Left"); + m_controller->GetKey(MSE_CAM_POS).BindMouse("Right"); + m_controller->GetKey(MSE_CAM_FOV).BindMouse("Middle"); + m_controller->GetAxis(MSEX_CAM_Y).BindMouse("Y"); + m_controller->GetAxis(MSEX_CAM_X).BindMouse("X"); } - if (InputDevice::Get("Keyboard")) + if (InputDevice::Get(g_name_keyboard.C())) { m_input_usage |= (1<GetKey(KEY_CAM_UP ).Bind("Keyboard", "Up"); - m_controller->GetKey(KEY_CAM_DOWN ).Bind("Keyboard", "Down"); - m_controller->GetKey(KEY_CAM_LEFT ).Bind("Keyboard", "Left"); - m_controller->GetKey(KEY_CAM_RIGHT).Bind("Keyboard", "Right"); + m_controller->GetKey(KEY_CAM_UP ).BindKeyboard("Up"); + m_controller->GetKey(KEY_CAM_DOWN ).BindKeyboard("Down"); + m_controller->GetKey(KEY_CAM_LEFT ).BindKeyboard("Left"); + m_controller->GetKey(KEY_CAM_RIGHT).BindKeyboard("Right"); //Camera keyboard position switch - m_controller->GetKey(KEY_CAM_POS ).Bind("Keyboard", "LeftShift"); - m_controller->GetKey(KEY_CAM_FOV ).Bind("Keyboard", "LeftCtrl"); + m_controller->GetKey(KEY_CAM_POS ).BindKeyboard("LeftShift"); + m_controller->GetKey(KEY_CAM_FOV ).BindKeyboard("LeftCtrl"); //Camera unzoom switch - m_controller->GetKey(KEY_CAM_RESET).Bind("Keyboard", "Space"); + m_controller->GetKey(KEY_CAM_RESET).BindKeyboard("Space"); //Mesh change - m_controller->GetKey(KEY_MESH_NEXT).Bind("Keyboard", "PageUp"); - m_controller->GetKey(KEY_MESH_PREV).Bind("Keyboard", "PageDown"); + m_controller->GetKey(KEY_MESH_NEXT).BindKeyboard("PageUp"); + m_controller->GetKey(KEY_MESH_PREV).BindKeyboard("PageDown"); //Base setup - m_controller->GetKey(KEY_F1).Bind("Keyboard", "F1"); - m_controller->GetKey(KEY_F2).Bind("Keyboard", "F2"); - m_controller->GetKey(KEY_F3).Bind("Keyboard", "F3"); - m_controller->GetKey(KEY_F4).Bind("Keyboard", "F4"); - m_controller->GetKey(KEY_F5).Bind("Keyboard", "F5"); - m_controller->GetKey(KEY_ESC).Bind("Keyboard", "Escape"); + m_controller->GetKey(KEY_F1).BindKeyboard("F1"); + m_controller->GetKey(KEY_F2).BindKeyboard("F2"); + m_controller->GetKey(KEY_F3).BindKeyboard("F3"); + m_controller->GetKey(KEY_F4).BindKeyboard("F4"); + m_controller->GetKey(KEY_F5).BindKeyboard("F5"); + m_controller->GetKey(KEY_ESC).BindKeyboard("Escape"); } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT m_camera = new Camera(); @@ -325,13 +327,13 @@ public: m_first_tick = true; //TODO : This should probably be "standard LoL behaviour" -#if NO_NACL_EM +#if NO_NACL_EM_INPUT { //Shutdown logic if (KeyReleased(KEY_ESC)) Ticker::Shutdown(); } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT //Compute render mesh count float a_j = lol::abs(m_render_max[1]); @@ -340,9 +342,9 @@ public: m_render_max[1] = a_j * ((RATIO_WH * 1.f) / ((i_trans != 0.f)?(i_trans):(RATIO_WH))) - RATIO_HW * .3f; //Mesh Change -#if NO_NACL_EM +#if NO_NACL_EM_INPUT m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, m_meshes.Count() - 1); -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds); #if ALL_FEATURES @@ -366,7 +368,7 @@ public: bool is_hsc = false; vec2 tmpv = vec2::zero; -#if NO_NACL_EM +#if NO_NACL_EM_INPUT is_pos = KeyDown(KEY_CAM_POS) || KeyDown(MSE_CAM_POS); is_fov = KeyDown(KEY_CAM_FOV) || KeyDown(MSE_CAM_FOV); @@ -383,7 +385,7 @@ public: tmpv += vec2((float)KeyDown(KEY_CAM_UP ) - (float)KeyDown(KEY_CAM_DOWN), (float)KeyDown(KEY_CAM_RIGHT) - (float)KeyDown(KEY_CAM_LEFT)); -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT //Base data vec2 rot = (!is_pos && !is_fov)?(tmpv):(vec2(.0f)); rot = vec2(rot.x, rot.y); @@ -403,7 +405,7 @@ public: m_rot += m_rot_speed * seconds; -#if NO_NACL_EM +#if NO_NACL_EM_INPUT if (m_reset_timer >= 0.f) m_reset_timer -= seconds; if (KeyPressed(KEY_CAM_RESET)) @@ -425,7 +427,7 @@ public: m_zoom += m_zoom_speed * seconds; m_hist_scale += m_hist_scale_speed * seconds; } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT //clamp vec2 rot_mesh = vec2(SmoothClamp(m_rot.x, -ROT_CLAMP, ROT_CLAMP, ROT_CLAMP * .1f), m_rot.y); @@ -436,13 +438,13 @@ public: vec2 hist_scale_mesh = vec2(SmoothClamp(m_hist_scale.x, 0.f, HST_CLAMP, HST_CLAMP * .1f), SmoothClamp(m_hist_scale.y, 0.f, HST_CLAMP, HST_CLAMP * .1f)); -#if NO_NACL_EM +#if NO_NACL_EM_INPUT if (KeyDown(KEY_CAM_RESET) && m_reset_timer < 0.f) { pos_mesh = vec2::zero; zoom_mesh = 0.f; } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT m_rot_mesh = vec2(damp(m_rot_mesh.x, rot_mesh.x, .2f, seconds), damp(m_rot_mesh.y, rot_mesh.y, .2f, seconds)); m_pos_mesh = vec2(damp(m_pos_mesh.x, pos_mesh.x, .2f, seconds), damp(m_pos_mesh.y, pos_mesh.y, .2f, seconds)); @@ -639,7 +641,7 @@ public: return; //TODO : This should probably be "standard LoL behaviour" -#if NO_NACL_EM +#if NO_NACL_EM_INPUT { if (KeyReleased(KEY_F1)) Video::SetDebugRenderMode(DebugRenderMode::Default); @@ -652,7 +654,7 @@ public: if (KeyReleased(KEY_F5)) Video::SetDebugRenderMode(DebugRenderMode::UV); } -#endif //NO_NACL_EM +#endif //NO_NACL_EM_INPUT #if NO_NACL_EM && WITH_TEXTURE if (!m_default_texture) @@ -716,9 +718,7 @@ public: //Camera projection mat4 new_proj = mat_obj_offset * mat_count_offset * mat_align * mat_count_scale * save_proj; m_camera->SetProjection(new_proj); -//#if NO_NACL_EM m_meshes[i]->Render(m_mat); -//#endif //NO_NACL_EM g_renderer->Clear(ClearMask::Depth); } m_camera->SetProjection(save_proj);