Commits

Anonymous committed 2fa3aeb

DarkMotion engine viewport fix

  • Participants
  • Parent commits 020ad28

Comments (0)

Files changed (5)

File BehaviorEx/OrbitCam.cs

             if (Input.IsMouseDown(MouseKey.Left)) {
                 Vec2 mpos = Input.MousePosition;
 
-                if (!_leftButton)  {
+                if (!_leftButton) {
                     _leftButton = true;
                     Input.LockCursor();
                     _lockPos = mpos;
                 _x += delta_pos.X;
                 _y += delta_pos.Y;
                 _lockPos = mpos;
-            }   else    {
+            } else {
                 _leftButton = false;
             }
 
             if (Input.IsMouseDown(MouseKey.Right)) {
                 Vec2 mpos = Input.MousePosition;
 
-                if (!_rightButton)  {
+                if (!_rightButton) {
                     Input.LockCursor();
                     _rightButton = true;
                     _lockPos = mpos;
                 Vec2 delta_pos = (_lockPos - Input.MousePosition) * Sensitivity;
                 Distance += delta_pos.Y;
                 _lockPos = mpos;
-            }   else    {
+            } else {
                 _rightButton = false;
             }
 
             Quat rot2 = Quat.FromMatrix(new Matrix3(rot));
 
             _transform.Rotation = rot2;
-            _transform.Position = Vec3.TransformPosition((new Vec3(0, 0, -Distance)), 
+            _transform.Position = Vec3.TransformPosition((new Vec3(0, 0, -Distance)),
                 Matrix4.CreateFromQuat(rot2)) + Target;
         }
         #endregion

File DarkMotion/EngineLayer.cs

             Engine.ActiveScene = Engine.CreateScene("Main");
 
             GameObject cam = Engine.ActiveScene.CreateObject("Camera", GameObjectType.Camera);
+            _camXform = cam.GetComponent<Transfrom>();
+
             cam.GetComponent<Camera>().Active = true;
             cam.GetComponent<Transfrom>().Position = new Vec3(0, 1, -5);
-            cam.AddBehavior<OrbitCam>();
 
             var w = control.ClientSize.Width;
             var h = control.ClientSize.Height;
             Resize(w, h);
+
+            control.MouseDown += (s, e) => {
+                if (e.Button == MouseButtons.Left)
+                    _leftDown = true;
+                else if (e.Button == MouseButtons.Right)
+                    _rightDown = true;
+
+                _lockMouse = true;
+                _lockPos = new Vec2(e.X, e.Y);
+            };
+
+            control.MouseUp += (s, e) => {
+                if (e.Button == MouseButtons.Left)
+                    _leftDown = false;
+                else if (e.Button == MouseButtons.Right)
+                    _rightDown = false;
+
+                _lockMouse = false;
+            };
+
+            control.MouseMove += (s, e) => {
+                _mousePos = new Vec2(e.X, e.Y);
+            };
+
+            _engControl = control;
         }
 
         public static void EngineUpdate()
         {
+            updateCam();
             if (Update != null)
                 Update();
+
+            Engine.Update();
             App.SwapBuffers();
         }
 
             Engine.ActiveScene.Clear();
             GameObject cam = Engine.ActiveScene.CreateObject("Camera", GameObjectType.Camera);
             cam.GetComponent<Camera>().Active = true;
-            cam.GetComponent<Transfrom>().Position = new Vec3(0, 1, -5);
-            cam.AddBehavior<OrbitCam>();
+            _camXform = cam.GetComponent<Transfrom>();
+
+            _camXform.Position = new Vec3(0, 1, -5);
+
             if (controller != null) {
                 Engine.AddVirtualPath(controller.RootPath, true);
                 var h3da = controller.ReelPath;
                 anim.FilePath = h3da;
                 AnimController.Clips = anim.Clips;
                 AnimController.Bones = anim.Bones;
-                var orbitCam = Engine.ActiveScene.FindObject("Camera").GetBehavior<BehaviorEx.OrbitCam>();
-                orbitCam.Target = character.GetComponent<Bounds>().Sphere.Xyz;
+                 var fCam = Engine.ActiveScene.FindObject("Camera");
+                _target = character.GetComponent<Bounds>().Sphere.Xyz;
             }
         }
 
             
             return CharacterAnimator.CurrentState(layer, out state, out progress);
         }
+
+        private static void updateCam()
+        {
+            if (_lockMouse)
+                Cursor.Position = _engControl.PointToScreen(new System.Drawing.Point((int)_lockPos.X, (int)_lockPos.Y));
+
+            if (_leftDown) {
+                Vec2 delta_pos = (_lockPos - _mousePos) * Sensitivity;
+                _x += delta_pos.X;
+                _y += delta_pos.Y;
+            }
+
+            if (_rightDown) {
+                Vec2 delta_pos = (_lockPos - _mousePos) * Sensitivity;
+                _distance += delta_pos.Y;
+            }
+
+            Matrix4 rot = Matrix4.CreateRotationY(_x * MathUtil.Deg2Rad) * 
+                Matrix4.CreateRotationX(_y * MathUtil.Deg2Rad);
+            Quat rot2 = Quat.FromMatrix(new Matrix3(rot));
+
+            _camXform.Rotation = rot2;
+            _camXform.Position = Vec3.TransformPosition((new Vec3(0, 0, -_distance)),
+                Matrix4.CreateFromQuat(rot2)) + _target;                       
+        }
         #endregion
 
         #region Properties
         public static GameObject Character
         {
-            get 
-            {
-                return Engine.ActiveScene.FindObject("Character");
-            }
+            get { return Engine.ActiveScene.FindObject("Character"); }
         }
 
         public static Animator CharacterAnimator
 
         #region Fields
         public static event App.UpdateHandler Update;
-        static Label _stateLabel = null;
+        private static Control _engControl = null;
+
+        // camera
+        private const float      Sensitivity = .15f;
+        private static Vec3      _target      = Vec3.Zero;
+        private static float     _distance    = 10;
+        private static float     _x           = 0;
+        private static float     _y           = 0;
+        private static Vec2      _lockPos     = Vec2.Zero;
+        private static Vec2      _mousePos    = Vec2.Zero;
+
+        private static bool      _lockMouse   = false;
+        private static bool      _leftDown    = false;
+        private static bool      _rightDown   = false;
+        private static Transfrom _camXform    = null;
         #endregion
     }
 
-}
+}

File DarkMotion/Forms/MainForm.cs

             };
 
             engView.Resize += (s, e) => EngineLayer.Resize(engView.ClientSize.Width, engView.ClientSize.Height);
-            engView.Paint += (s, e) => EngineLayer.EngineUpdate();
             btnSync.Click += (s, e) => EngineLayer.Sync(_animController);
+            Application.Idle += (s, e) => EngineLayer.EngineUpdate();
         }
 
         #region Methods

File SharpHammer/Input.cs

             if (_init)
                 NativeCalls.input_update();
         }
-
         #endregion
 
         #region Properties

File SharpHammer/engine.cs

         static bool _pause = false;
         static bool _init = false;
 
-        static Scene _global_scene = new Scene() {
+        static Scene _globalScene = new Scene() {
             _sceneId = UInt32.MaxValue,
             _name = "global"
         };
 
         public static Scene GlobalScene
         {
-            get { return _global_scene; }
+            get { return _globalScene; }
         }
 
         public static bool Pause