Browse Source

In Direct3D9 mode, differentiate between left Shift, Ctrl & Alt similarly as GLFW does.

Lasse Öörni 14 years ago
parent
commit
3baf62216e

+ 1 - 1
Bin/Data/Scripts/NinjaSnowWar.as

@@ -415,7 +415,7 @@ void UpdateControls()
             playerControls.Set(CTRL_LEFT, true);
             playerControls.Set(CTRL_LEFT, true);
         if (input.keyDown['D'])
         if (input.keyDown['D'])
             playerControls.Set(CTRL_RIGHT, true);
             playerControls.Set(CTRL_RIGHT, true);
-        if (input.keyDown[KEY_CTRL])
+        if (input.keyDown[KEY_LCTRL])
             playerControls.Set(CTRL_FIRE, true);
             playerControls.Set(CTRL_FIRE, true);
         if (input.keyDown[' '])
         if (input.keyDown[' '])
             playerControls.Set(CTRL_JUMP, true);
             playerControls.Set(CTRL_JUMP, true);

+ 3 - 3
Bin/Data/Scripts/TestScene.as

@@ -184,9 +184,9 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
     if (ui.focusElement is null)
     if (ui.focusElement is null)
     {
     {
         float speedMultiplier = 1.0;
         float speedMultiplier = 1.0;
-        if (input.keyDown[KEY_SHIFT])
+        if (input.keyDown[KEY_LSHIFT])
             speedMultiplier = 5.0;
             speedMultiplier = 5.0;
-        if (input.keyDown[KEY_CTRL])
+        if (input.keyDown[KEY_LCTRL])
             speedMultiplier = 0.1;
             speedMultiplier = 0.1;
 
 
         if (input.keyDown['W'])
         if (input.keyDown['W'])
@@ -201,7 +201,7 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
         if (input.keyPress['1'])
         if (input.keyPress['1'])
         {
         {
             int nextRenderMode = graphics.renderMode;
             int nextRenderMode = graphics.renderMode;
-            if (input.keyDown[KEY_SHIFT])
+            if (input.keyDown[KEY_LSHIFT])
             {
             {
                 --nextRenderMode;
                 --nextRenderMode;
                 if (nextRenderMode < 0)
                 if (nextRenderMode < 0)

+ 12 - 0
Engine/Container/HashBase.h

@@ -44,6 +44,18 @@ template <class T> unsigned MakeHash(const T& value)
     return value.ToHash();
     return value.ToHash();
 }
 }
 
 
+/// Long long hash function
+template<> inline unsigned MakeHash(const long long& value)
+{
+    return (value >> 32) | (value & 0xffffffff);
+}
+
+/// Unsigned long long hash function
+template<> inline unsigned MakeHash(const unsigned long long& value)
+{
+    return (value >> 32) | (value & 0xffffffff);
+}
+
 /// Int hash function
 /// Int hash function
 template<> inline unsigned MakeHash(const int& value)
 template<> inline unsigned MakeHash(const int& value)
 {
 {

+ 0 - 3
Engine/Engine/InputAPI.cpp

@@ -36,9 +36,6 @@ static void RegisterInputConstants(asIScriptEngine* engine)
     engine->RegisterGlobalProperty("const int KEY_BACKSPACE", (void*)&KEY_BACKSPACE);
     engine->RegisterGlobalProperty("const int KEY_BACKSPACE", (void*)&KEY_BACKSPACE);
     engine->RegisterGlobalProperty("const int KEY_TAB", (void*)&KEY_TAB);
     engine->RegisterGlobalProperty("const int KEY_TAB", (void*)&KEY_TAB);
     engine->RegisterGlobalProperty("const int KEY_RETURN", (void*)&KEY_RETURN);
     engine->RegisterGlobalProperty("const int KEY_RETURN", (void*)&KEY_RETURN);
-    engine->RegisterGlobalProperty("const int KEY_SHIFT", (void*)&KEY_SHIFT);
-    engine->RegisterGlobalProperty("const int KEY_CTRL", (void*)&KEY_CTRL);
-    engine->RegisterGlobalProperty("const int KEY_ALT", (void*)&KEY_ALT);
     engine->RegisterGlobalProperty("const int KEY_PAUSE", (void*)&KEY_PAUSE);
     engine->RegisterGlobalProperty("const int KEY_PAUSE", (void*)&KEY_PAUSE);
     engine->RegisterGlobalProperty("const int KEY_CAPSLOCK", (void*)&KEY_CAPSLOCK);
     engine->RegisterGlobalProperty("const int KEY_CAPSLOCK", (void*)&KEY_CAPSLOCK);
     engine->RegisterGlobalProperty("const int KEY_ESC", (void*)&KEY_ESC);
     engine->RegisterGlobalProperty("const int KEY_ESC", (void*)&KEY_ESC);

+ 2 - 2
Engine/Graphics/AnimationState.cpp

@@ -181,7 +181,7 @@ void AnimationState::Apply()
     // Check first if full weight or blending
     // Check first if full weight or blending
     if (weight_ == 1.0f)
     if (weight_ == 1.0f)
     {
     {
-        for (Map<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
+        for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
         {
         {
             const AnimationTrack* track = animation_->GetTrack(i->first_);
             const AnimationTrack* track = animation_->GetTrack(i->first_);
             Bone* bone = i->second_;
             Bone* bone = i->second_;
@@ -244,7 +244,7 @@ void AnimationState::Apply()
     }
     }
     else
     else
     {
     {
-        for (Map<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
+        for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
         {
         {
             const AnimationTrack* track = animation_->GetTrack(i->first_);
             const AnimationTrack* track = animation_->GetTrack(i->first_);
             Bone* bone = i->second_;
             Bone* bone = i->second_;

+ 2 - 2
Engine/Graphics/AnimationState.h

@@ -23,7 +23,7 @@
 
 
 #pragma once
 #pragma once
 
 
-#include "Map.h"
+#include "HashMap.h"
 #include "SharedPtr.h"
 #include "SharedPtr.h"
 
 
 class Animation;
 class Animation;
@@ -90,7 +90,7 @@ private:
     /// Start bone
     /// Start bone
     Bone* startBone_;
     Bone* startBone_;
     /// Mapping of animation track indices to bones
     /// Mapping of animation track indices to bones
-    Map<unsigned, Bone*> trackToBoneMap_;
+    HashMap<unsigned, Bone*> trackToBoneMap_;
     /// Last keyframe on each animation track for optimized keyframe search
     /// Last keyframe on each animation track for optimized keyframe search
     PODVector<unsigned> lastKeyFrame_;
     PODVector<unsigned> lastKeyFrame_;
     /// Looped flag
     /// Looped flag

+ 1 - 1
Engine/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -567,7 +567,7 @@ bool Graphics::BeginFrame()
     ResetStreamFrequencies();
     ResetStreamFrequencies();
     
     
     // Reset immediate mode vertex buffer positions
     // Reset immediate mode vertex buffer positions
-    for (Map<unsigned, unsigned>::Iterator i = immediateVertexBufferPos_.Begin(); i != immediateVertexBufferPos_.End(); ++i)
+    for (HashMap<unsigned, unsigned>::Iterator i = immediateVertexBufferPos_.Begin(); i != immediateVertexBufferPos_.End(); ++i)
         i->second_ = 0;
         i->second_ = 0;
     
     
     numPrimitives_ = 0;
     numPrimitives_ = 0;

+ 3 - 3
Engine/Graphics/Direct3D9/D3D9Graphics.h

@@ -440,11 +440,11 @@ private:
     /// GPU objects
     /// GPU objects
     Vector<GPUObject*> gpuObjects_;
     Vector<GPUObject*> gpuObjects_;
     /// Vertex declarations
     /// Vertex declarations
-    Map<unsigned long long, SharedPtr<VertexDeclaration> > vertexDeclarations_;
+    HashMap<unsigned long long, SharedPtr<VertexDeclaration> > vertexDeclarations_;
     /// Immediate rendering vertex buffers by vertex declaration
     /// Immediate rendering vertex buffers by vertex declaration
-    Map<unsigned, SharedPtr<VertexBuffer> > immediateVertexBuffers_;
+    HashMap<unsigned, SharedPtr<VertexBuffer> > immediateVertexBuffers_;
     /// Immediate rendering vertex buffer start positions
     /// Immediate rendering vertex buffer start positions
-    Map<unsigned, unsigned> immediateVertexBufferPos_;
+    HashMap<unsigned, unsigned> immediateVertexBufferPos_;
     /// Deferred rendering diffuse buffer
     /// Deferred rendering diffuse buffer
     SharedPtr<Texture2D> diffBuffer_;
     SharedPtr<Texture2D> diffBuffer_;
     /// Deferred rendering normal buffer
     /// Deferred rendering normal buffer

+ 1 - 0
Engine/Graphics/Precompiled.h

@@ -23,6 +23,7 @@
 
 
 #pragma once
 #pragma once
 
 
+#include "HashMap.h"
 #include "HashSet.h"
 #include "HashSet.h"
 #include "Map.h"
 #include "Map.h"
 #include "Set.h"
 #include "Set.h"

+ 60 - 26
Engine/Input/Input.cpp

@@ -41,6 +41,52 @@ static Input* inputInstance = 0;
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
 
 
+/// Convert the virtual key code & scan code if necessary. Return non-zero if key should be posted
+int ConvertKeyCode(unsigned wParam, unsigned lParam)
+{
+    unsigned scanCode = (lParam >> 16) & 0x1ff;
+    
+    // Recognize left/right qualifier key from the scan code
+    switch (wParam)
+    {
+    case VK_SHIFT:
+        if (scanCode == 54)
+            return KEY_RSHIFT;
+        else
+            return KEY_LSHIFT;
+        break;
+        
+    case VK_CONTROL:
+        // Control might not be a real key, as Windows posts it whenever Alt-Gr is pressed (inspired by GLFW)
+        {
+            MSG nextMsg;
+            DWORD msgTime = GetMessageTime();
+            
+            if (PeekMessage(&nextMsg, NULL, 0, 0, PM_NOREMOVE))
+            {
+                if ((nextMsg.message == WM_KEYDOWN || nextMsg.message == WM_SYSKEYDOWN) && nextMsg.wParam == VK_MENU &&
+                    (nextMsg.lParam & 0x01000000) != 0 && nextMsg.time == msgTime)
+                    return 0;
+            }
+            
+            if (scanCode & 0x100)
+                return KEY_RCTRL;
+            else
+                return KEY_LCTRL;
+        }
+        break;
+        
+    case VK_MENU:
+        if (scanCode & 0x100)
+            return KEY_RALT;
+        else
+            return KEY_LALT;
+        
+    default:
+        return wParam;
+    }
+}
+
 OBJECTTYPESTATIC(Input);
 OBJECTTYPESTATIC(Input);
 
 
 Input::Input(Context* context) :
 Input::Input(Context* context) :
@@ -187,41 +233,25 @@ bool Input::GetMouseButtonPress(int button) const
 
 
 bool Input::GetQualifierDown(int qualifier) const
 bool Input::GetQualifierDown(int qualifier) const
 {
 {
-    #ifndef USE_OPENGL
-    if (qualifier == QUAL_SHIFT)
-        return GetKeyDown(KEY_SHIFT);
-    if (qualifier == QUAL_CTRL)
-        return GetKeyDown(KEY_CTRL);
-    if (qualifier == QUAL_ALT)
-        return GetKeyDown(KEY_ALT);
-    #else
     if (qualifier == QUAL_SHIFT)
     if (qualifier == QUAL_SHIFT)
         return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
         return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
     if (qualifier == QUAL_CTRL)
     if (qualifier == QUAL_CTRL)
         return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
         return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
     if (qualifier == QUAL_ALT)
     if (qualifier == QUAL_ALT)
         return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
         return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
-    #endif
+    
     return false;
     return false;
 }
 }
 
 
 bool Input::GetQualifierPress(int qualifier) const
 bool Input::GetQualifierPress(int qualifier) const
 {
 {
-    #ifndef USE_OPENGL
-    if (qualifier == QUAL_SHIFT)
-        return GetKeyPress(KEY_SHIFT);
-    if (qualifier == QUAL_CTRL)
-        return GetKeyPress(KEY_CTRL);
-    if (qualifier == QUAL_ALT)
-        return GetKeyPress(KEY_ALT);
-    #else
     if (qualifier == QUAL_SHIFT)
     if (qualifier == QUAL_SHIFT)
         return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
         return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
     if (qualifier == QUAL_CTRL)
     if (qualifier == QUAL_CTRL)
         return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
         return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
     if (qualifier == QUAL_ALT)
     if (qualifier == QUAL_ALT)
         return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
         return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
-    #endif
+    
     return false;
     return false;
 }
 }
 
 
@@ -361,6 +391,7 @@ void Input::SetKey(int key, bool newState)
     
     
     if (newState)
     if (newState)
     {
     {
+        LOGINFO("Keydown: " + String(key));
         if (!keyDown_.Contains(key))
         if (!keyDown_.Contains(key))
         {
         {
             keyDown_.Insert(key);
             keyDown_.Insert(key);
@@ -469,6 +500,7 @@ void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
     int msg = eventData[P_MSG].GetInt();
     int msg = eventData[P_MSG].GetInt();
     int wParam = eventData[P_WPARAM].GetInt();
     int wParam = eventData[P_WPARAM].GetInt();
     int lParam = eventData[P_LPARAM].GetInt();
     int lParam = eventData[P_LPARAM].GetInt();
+    int keyCode;
     
     
     switch (msg)
     switch (msg)
     {
     {
@@ -523,23 +555,25 @@ void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
         break;
         break;
         
         
     case WM_KEYDOWN:
     case WM_KEYDOWN:
-        SetKey(wParam, true);
+        keyCode = ConvertKeyCode(wParam, lParam);
+        if (keyCode)
+            SetKey(keyCode, true);
         eventData[P_HANDLED] = true;
         eventData[P_HANDLED] = true;
         break;
         break;
         
         
     case WM_SYSKEYDOWN:
     case WM_SYSKEYDOWN:
-        SetKey(wParam, true);
-        if (wParam != KEY_F4)
+        keyCode = ConvertKeyCode(wParam, lParam);
+        if (keyCode)
+            SetKey(keyCode, true);
+        if (keyCode != KEY_F4)
             eventData[P_HANDLED] = true;
             eventData[P_HANDLED] = true;
         break;
         break;
         
         
     case WM_KEYUP:
     case WM_KEYUP:
-        SetKey(wParam, false);
-        eventData[P_HANDLED] = true;
-        break;
-        
     case WM_SYSKEYUP:
     case WM_SYSKEYUP:
-        SetKey(wParam, false);
+        keyCode = ConvertKeyCode(wParam, lParam);
+        if (keyCode)
+            SetKey(keyCode, false);
         eventData[P_HANDLED] = true;
         eventData[P_HANDLED] = true;
         break;
         break;
         
         

+ 0 - 6
Engine/Input/InputEvents.h

@@ -99,9 +99,6 @@ static const int QUAL_ALT = 4;
 static const int KEY_BACKSPACE = 0x08;
 static const int KEY_BACKSPACE = 0x08;
 static const int KEY_TAB = 0x09;
 static const int KEY_TAB = 0x09;
 static const int KEY_RETURN = 0x0d;
 static const int KEY_RETURN = 0x0d;
-static const int KEY_SHIFT = 0x10;
-static const int KEY_CTRL = 0x11;
-static const int KEY_ALT = 0x12;
 static const int KEY_PAUSE = 0x13;
 static const int KEY_PAUSE = 0x13;
 static const int KEY_CAPSLOCK = 0x14;
 static const int KEY_CAPSLOCK = 0x14;
 static const int KEY_ESC = 0x1b;
 static const int KEY_ESC = 0x1b;
@@ -170,9 +167,6 @@ static const int KEY_RALT = 0xa5;
 static const int KEY_BACKSPACE = GLFW_KEY_BACKSPACE;
 static const int KEY_BACKSPACE = GLFW_KEY_BACKSPACE;
 static const int KEY_TAB = GLFW_KEY_TAB;
 static const int KEY_TAB = GLFW_KEY_TAB;
 static const int KEY_RETURN = GLFW_KEY_ENTER;
 static const int KEY_RETURN = GLFW_KEY_ENTER;
-static const int KEY_SHIFT = GLFW_KEY_LSHIFT;
-static const int KEY_CTRL = GLFW_KEY_LCTRL;
-static const int KEY_ALT = GLFW_KEY_LALT;
 static const int KEY_PAUSE = GLFW_KEY_PAUSE;
 static const int KEY_PAUSE = GLFW_KEY_PAUSE;
 static const int KEY_CAPSLOCK = GLFW_KEY_CAPS_LOCK;
 static const int KEY_CAPSLOCK = GLFW_KEY_CAPS_LOCK;
 static const int KEY_ESC = GLFW_KEY_ESC;
 static const int KEY_ESC = GLFW_KEY_ESC;