Browse Source

added new TouchEvent::WHEEL_DIR event, WHEEL_UP/DOWN - deprecated
added new Demo Camera multi touch test

dmuratshin 9 years ago
parent
commit
ecff8af14d

+ 1 - 0
examples/Demo/proj.win32/Demo.vcxproj

@@ -144,6 +144,7 @@
     <ClInclude Include="../src/TestUserShader2.h" />
     <ClInclude Include="../src/TestUserShader2.h" />
     <ClInclude Include="../src/example.h" />
     <ClInclude Include="../src/example.h" />
     <ClInclude Include="../src/test.h" />
     <ClInclude Include="../src/test.h" />
+    <ClInclude Include="..\src\TestCamera.h" />
     <ClInclude Include="..\src\TestEdges.h" />
     <ClInclude Include="..\src\TestEdges.h" />
     <ClInclude Include="..\src\TestSignedDistanceFont.h" />
     <ClInclude Include="..\src\TestSignedDistanceFont.h" />
     <ClInclude Include="..\src\TestTweenPostProcessing.h" />
     <ClInclude Include="..\src\TestTweenPostProcessing.h" />

+ 1 - 0
examples/Demo/proj.win32/Demo.vcxproj.filters

@@ -37,5 +37,6 @@
     <ClInclude Include="..\src\TestEdges.h" />
     <ClInclude Include="..\src\TestEdges.h" />
     <ClInclude Include="..\src\TestTweenPostProcessing.h" />
     <ClInclude Include="..\src\TestTweenPostProcessing.h" />
     <ClInclude Include="..\src\TestSignedDistanceFont.h" />
     <ClInclude Include="..\src\TestSignedDistanceFont.h" />
+    <ClInclude Include="..\src\TestCamera.h" />
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 159 - 0
examples/Demo/src/TestCamera.h

@@ -0,0 +1,159 @@
+#pragma once
+#include "test.h"
+#include <map>
+
+DECLARE_SMART(Camera, spCamera);
+class Camera : public Actor
+{
+public:
+
+    spActor _content;
+
+    Camera()
+    {
+        addEventListener(TouchEvent::TOUCH_DOWN, CLOSURE(this, &Camera::onEvent));
+        addEventListener(TouchEvent::TOUCH_UP, CLOSURE(this, &Camera::onEvent));
+        addEventListener(TouchEvent::MOVE, CLOSURE(this, &Camera::onEvent));
+        addEventListener(TouchEvent::WHEEL_DIR, CLOSURE(this, &Camera::onEvent));
+
+        _transform.identity();
+    }
+
+    void setContent(spActor content)
+    {
+        _content = content;
+        addChild(content);
+    }
+
+    struct touch
+    {
+        Vector2 current;
+        Vector2 previous;
+    };
+
+    std::map<int, touch> _touches;
+
+    void onEvent(Event* ev)
+    {
+        TouchEvent* te = safeCast<TouchEvent*>(ev);
+        const Vector2& pos = te->localPosition;
+
+
+
+        if (te->type == TouchEvent::TOUCH_DOWN)
+        {
+            touch& t = _touches[te->index];
+            t.previous = t.current = pos;
+        }
+
+        if (te->type == TouchEvent::TOUCH_UP)
+        {
+            auto it = _touches.find(te->index);
+            if (it != _touches.end())
+                _touches.erase(it);
+        }
+
+        if (te->type == TouchEvent::WHEEL_DIR)
+        {
+            if (te->wheelDirection.y != 0.0f)
+            {
+                float scale = te->wheelDirection.y < 0 ? 0.95f : 1.05f;
+
+                _transform.translate(-Vector3(pos.x, pos.y, 0));
+                _transform.scale(Vector3(scale, scale, 1));
+                _transform.translate(Vector3(pos.x, pos.y, 0));
+            }
+        }
+
+        if (te->type == TouchEvent::MOVE && !_touches.empty())
+        {
+            touch& t = _touches[te->index];
+            t.previous = t.current;
+
+            t.current = pos;
+
+            if (_touches.size() == 1)
+            {
+                Vector2 offset = t.current - t.previous;
+                _transform.translate(Vector3(offset.x, offset.y, 0));
+            }
+            else
+            {
+                touch* p1, *p2;
+                for (auto& t : _touches)
+                {
+                    if (t.first == te->index)
+                        p1 = &t.second;
+                    else
+                        p2 = &t.second;
+                }
+
+                Vector2 center = (p1->current + p2->current) / 2;
+                Vector2 prevCenter = (p1->previous + p2->previous) / 2;
+
+                float dist = p1->current.distance(p2->current);
+                float prevDist = p1->previous.distance(p2->previous);
+                float scale = dist / prevDist;
+
+
+                Vector2 offset = center - prevCenter;
+
+                p1->previous = p1->current;
+                p2->previous = p2->current;
+
+                _transform.translate(Vector3(offset.x, offset.y, 0));
+
+
+                _transform.translate(-Vector3(center.x, center.y, 0));
+                _transform.scale(Vector3(scale, scale, 1));
+                _transform.translate(Vector3(center.x, center.y, 0));
+            }
+        }
+
+        update();
+    }
+
+    void doUpdate(const UpdateState& us)
+    {
+
+    }
+
+    void update()
+    {
+        Transform tr(_transform);
+        _content->setTransform(tr);
+    }
+
+    Matrix _transform;
+};
+
+
+class TestCamera : public Test
+{
+public:
+    TestCamera()
+    {
+        spCamera cam = new Camera;
+        cam->attachTo(content);
+        cam->setSize(content->getSize());
+
+
+        spSprite map = new Sprite();
+        map->setResAnim(resources.getResAnim("map"));
+        cam->setContent(map);
+
+        spButton button = new Button;
+
+        button->setPosition(map->getSize() / 2);
+        button->setResAnim(resourcesUI.getResAnim("button"));
+        button->attachTo(map);
+        button->setAnchor(0.5f, 0.5f);
+        button->addEventListener(TouchEvent::CLICK, CLOSURE(this, &TestCamera::testClick));
+
+    }
+
+    void testClick(Event* event)
+    {
+        notify("clicked");
+    }
+};

+ 3 - 0
examples/Demo/src/example.cpp

@@ -28,6 +28,7 @@
 #include "TestSignedDistanceFont.h"
 #include "TestSignedDistanceFont.h"
 #include "TestTweenPostProcessing.h"
 #include "TestTweenPostProcessing.h"
 #include "TestEdges.h"
 #include "TestEdges.h"
+#include "TestCamera.h"
 
 
 #ifdef __S3E__
 #ifdef __S3E__
 #include "s3eKeyboard.h"
 #include "s3eKeyboard.h"
@@ -93,6 +94,7 @@ public:
         addButton("counter", "Counter");
         addButton("counter", "Counter");
         addButton("tweentext", "Tween Text");
         addButton("tweentext", "Tween Text");
         addButton("tweenshine", "Tween Shine");
         addButton("tweenshine", "Tween Shine");
+        addButton("mtz", "MultiTouch Camera/Zoom");
     }
     }
 
 
     void showTest(spActor actor)
     void showTest(spActor actor)
@@ -136,6 +138,7 @@ public:
         if (id == "sdf") showTest(new TestSignedDistanceFont);
         if (id == "sdf") showTest(new TestSignedDistanceFont);
         if (id == "tweenpp") showTest(new TestTweenPostProcessing);
         if (id == "tweenpp") showTest(new TestTweenPostProcessing);
         if (id == "edges") showTest(new TestEdges);
         if (id == "edges") showTest(new TestEdges);
+        if (id == "mtz") showTest(new TestCamera);
         if (id == "openbrowser")
         if (id == "openbrowser")
         {
         {
             core::execute("http://oxygine.org/");
             core::execute("http://oxygine.org/");

+ 8 - 3
oxygine/src/Input.cpp

@@ -59,12 +59,17 @@ namespace oxygine
         stage->handleEvent(&me);
         stage->handleEvent(&me);
     }
     }
 
 
-    void Input::sendPointerWheelEvent(spStage stage, int scroll, PointerState* ps)
+    void Input::sendPointerWheelEvent(spStage stage, const Vector2& dir, PointerState* ps)
     {
     {
-        TouchEvent me(scroll > 0 ? TouchEvent::WHEEL_UP : TouchEvent::WHEEL_DOWN, true, ps->getPosition());
+        TouchEvent me(dir.y > 0 ? TouchEvent::WHEEL_UP : TouchEvent::WHEEL_DOWN, true, ps->getPosition());
         me.index = ps->getIndex();
         me.index = ps->getIndex();
-
         stage->handleEvent(&me);
         stage->handleEvent(&me);
+
+
+        TouchEvent te(TouchEvent::WHEEL_DIR, true, ps->getPosition());
+        te.index = ps->getIndex();
+        te.wheelDirection = dir;
+        stage->handleEvent(&te);
     }
     }
 
 
 
 

+ 1 - 1
oxygine/src/Input.h

@@ -38,7 +38,7 @@ namespace oxygine
 
 
         void sendPointerButtonEvent(spStage, MouseButton button, float x, float y, float pressure, int type, PointerState*);
         void sendPointerButtonEvent(spStage, MouseButton button, float x, float y, float pressure, int type, PointerState*);
         void sendPointerMotionEvent(spStage, float x, float y, float pressure, PointerState*);
         void sendPointerMotionEvent(spStage, float x, float y, float pressure, PointerState*);
-        void sendPointerWheelEvent(spStage, int scroll, PointerState*);
+        void sendPointerWheelEvent(spStage, const Vector2& dir, PointerState*);
 
 
         bool _multiTouch;
         bool _multiTouch;
     };
     };

+ 6 - 3
oxygine/src/TouchEvent.h

@@ -17,14 +17,15 @@ namespace oxygine
             MOVE,
             MOVE,
             TOUCH_DOWN,
             TOUCH_DOWN,
             TOUCH_UP,
             TOUCH_UP,
-            WHEEL_UP,
-            WHEEL_DOWN,
+            WHEEL_UP,//DEPRECATED, use WHEEL_DIR with TouchEvent::wheelDirection
+            WHEEL_DOWN,//DEPRECATED, use WHEEL_DIR with TouchEvent::wheelDirection
+            WHEEL_DIR,
 
 
             __LAST//system
             __LAST//system
         };
         };
 
 
 
 
-        TouchEvent(eventType type, bool Bubbles = true, const Vector2& locPosition = Vector2(0, 0)) : Event(type, Bubbles), localPosition(locPosition), position(locPosition), mouseButton(MouseButton_Touch), pressure(1.0f), index(1), __clickDispatched(false) {}
+        TouchEvent(eventType type, bool Bubbles = true, const Vector2& locPosition = Vector2(0, 0)) : Event(type, Bubbles), localPosition(locPosition), position(locPosition), mouseButton(MouseButton_Touch), pressure(1.0f), index(1), __clickDispatched(false), wheelDirection(0, 0) {}
 
 
         /**position in local space for Event::currentTarget Actor*/
         /**position in local space for Event::currentTarget Actor*/
         Vector2 localPosition;
         Vector2 localPosition;
@@ -36,6 +37,8 @@ namespace oxygine
         MouseButton mouseButton;
         MouseButton mouseButton;
         pointer_index index;
         pointer_index index;
 
 
+        Vector2 wheelDirection;//actual only for WHEEL_DIR event
+
         const PointerState* getPointer() const;
         const PointerState* getPointer() const;
 
 
         static bool isTouchEvent(int eventID) { return eventID > __FIRST && eventID < __LAST; }
         static bool isTouchEvent(int eventID) { return eventID > __FIRST && eventID < __LAST; }

+ 1 - 1
oxygine/src/core/oxygine.cpp

@@ -688,7 +688,7 @@ namespace oxygine
                         break;
                         break;
                     }
                     }
                     case SDL_MOUSEWHEEL:
                     case SDL_MOUSEWHEEL:
-                        input->sendPointerWheelEvent(getStageByWindow(event.window.windowID), event.wheel.y, &input->_pointerMouse);
+                        input->sendPointerWheelEvent(getStageByWindow(event.window.windowID), Vector2(event.wheel.x, event.wheel.y), &input->_pointerMouse);
                         break;
                         break;
                     case SDL_KEYDOWN:
                     case SDL_KEYDOWN:
                     {
                     {