Skip to content

Commit

Permalink
better nuiScrollView with multi touch
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Metrot committed May 28, 2014
1 parent 52b222b commit 1adfd20
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
13 changes: 6 additions & 7 deletions include/nuiScrollView.h
Expand Up @@ -33,12 +33,13 @@ class nuiScrollView : public nuiSimpleContainer

virtual bool Clear();

virtual bool MouseClicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Button);
virtual bool MouseUnclicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Button);
virtual bool MouseMoved(nuiSize X, nuiSize Y);
virtual bool MouseClicked(const nglMouseInfo& rInfo);
virtual bool MouseUnclicked(const nglMouseInfo& rInfo);
virtual bool MouseMoved(const nglMouseInfo& rInfo);
virtual bool MouseWheelMoved(const nglMouseInfo& rInfo);
virtual bool MouseCanceled(const nglMouseInfo& rInfo);

void Dragged(nuiSize X, nuiSize Y);
void Dragged(const nglMouseInfo& rInfo);

const nuiRect& GetChildrenUnionRect() { return mChildrenUnionRect; }

Expand Down Expand Up @@ -140,9 +141,7 @@ class nuiScrollView : public nuiSimpleContainer
bool mMinimalResize;
nuiRect mOldIdealRect;

bool mLeftClick;
nuiSize mClickX;
nuiSize mClickY;
int mLeftClick;
double mClickValueH;
double mClickValueV;
nuiSize mSpeedX;
Expand Down
90 changes: 49 additions & 41 deletions src/Layout/nuiScrollView.cpp
Expand Up @@ -89,7 +89,7 @@ void nuiScrollView::Init(nuiScrollBar* pHorizontalScrollBar, nuiScrollBar* pVert
mDragEnabled = false;
mHideScrollBars = false;

mLeftClick = false;
mLeftClick = 0;


if (pHorizontalScrollBar)
Expand Down Expand Up @@ -709,12 +709,12 @@ bool nuiScrollView::MouseWheelMoved(const nglMouseInfo& rInfo)
return true;
}

bool nuiScrollView::MouseClicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Button)
bool nuiScrollView::MouseClicked(const nglMouseInfo& rInfo)
{
bool res = false;
bool autoHideScrollbars = true;
bool v = IsKeyDown(NK_LSHIFT) || IsKeyDown(NK_RSHIFT);
if (Button & nglMouseInfo::ButtonWheelUp)
if (rInfo.Buttons & nglMouseInfo::ButtonWheelUp)
{
if (v)
{
Expand Down Expand Up @@ -744,7 +744,7 @@ bool nuiScrollView::MouseClicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Butto
}

}
else if (Button & nglMouseInfo::ButtonWheelDown)
else if (rInfo.Buttons & nglMouseInfo::ButtonWheelDown)
{
if (v)
{
Expand Down Expand Up @@ -774,32 +774,30 @@ bool nuiScrollView::MouseClicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Butto
}
}
}
else if (Button & nglMouseInfo::ButtonWheelLeft)
else if (rInfo.Buttons & nglMouseInfo::ButtonWheelLeft)
{
if (mpHorizontal && !mForceNoHorizontal)
{
mpHorizontal->GetRange().Decrement();
res = true;
}
}
else if (Button & nglMouseInfo::ButtonWheelRight)
else if (rInfo.Buttons & nglMouseInfo::ButtonWheelRight)
{
if (mpHorizontal && !mForceNoHorizontal)
{
mpHorizontal->GetRange().Increment();
res = true;
}
}
else if (Button & nglMouseInfo::ButtonLeft && mDragEnabled)
else if (rInfo.Buttons & nglMouseInfo::ButtonLeft && mDragEnabled && !mLeftClick)
{
mLeftClick = true;
mLeftClick++;
mTimerOn = false;
mSpeedX = 0;
mSpeedY = 0;
mClickX = X;
mClickY = Y;
mLastX = X;
mLastY = Y;
mLastX = rInfo.X;
mLastY = rInfo.Y;
mClickValueH = GetRange(nuiHorizontal)->GetValue();
mClickValueV = GetRange(nuiVertical)->GetValue();

Expand All @@ -821,46 +819,49 @@ bool nuiScrollView::MouseClicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Butto
return res;
}

bool nuiScrollView::MouseUnclicked(nuiSize X, nuiSize Y, nglMouseInfo::Flags Button)
bool nuiScrollView::MouseUnclicked(const nglMouseInfo& rInfo)
{
if (!mLeftClick)
return false;

Dragged(X, Y);
mLeftClick = false;
Dragged(rInfo);
mLeftClick--;

nglTime now;
double elapsed = now.GetValue() - mLastTime.GetValue();
if (elapsed > 0.05)
{
mSpeedX = 0;
mSpeedY = 0;
}
else
if (!mLeftClick)
{
if (mSpeedX != 0 || mSpeedY != 0)
nglTime now;
double elapsed = now.GetValue() - mLastTime.GetValue();
if (elapsed > 0.05)
{
mTimerOn = true;
mSpeedX = 0;
mSpeedY = 0;
}
else
{
if (mSpeedX != 0 || mSpeedY != 0)
{
mTimerOn = true;
}
}
}

if (mHideScrollBars)
{
HideScrollBars();
if (mHideScrollBars)
{
HideScrollBars();
}
}
return true;
}

bool nuiScrollView::MouseMoved(nuiSize X, nuiSize Y)
bool nuiScrollView::MouseMoved(const nglMouseInfo& rInfo)
{
if (!mLeftClick)
return false;

nglTime now;
float elapsed = now.GetValue() - mLastTime.GetValue();

nuiSize vectX = mLastX - X;
nuiSize vectY = mLastY - Y;
nuiSize vectX = mLastX - rInfo.X;
nuiSize vectY = mLastY - rInfo.Y;
nuiSize module = sqrt(vectX * vectX + vectY * vectY);
module = 1;
elapsed = 1;
Expand All @@ -875,17 +876,20 @@ bool nuiScrollView::MouseMoved(nuiSize X, nuiSize Y)

// NGL_OUT("Scroll: %f = %f + %f * INERTIA / %f \n", mSpeedY, tmpY, vectY, elapsed);

mLastX = X;
mLastY = Y;
mLastX = rInfo.X;
mLastY = rInfo.Y;
mLastTime = now;
Dragged(X, Y);
Dragged(rInfo);
return true;
}

void nuiScrollView::Dragged(nuiSize X, nuiSize Y)
void nuiScrollView::Dragged(const nglMouseInfo& rInfo)
{
nuiSize diffX = mClickX - X;
nuiSize diffY = mClickY - Y;
float OldX = rInfo.Counterpart->X;
float OldY = rInfo.Counterpart->Y;
GlobalToLocal(OldX, OldY);
nuiSize diffX = OldX - rInfo.X;
nuiSize diffY = OldY - rInfo.Y;

if (mpHorizontal && !mForceNoHorizontal)
{
Expand Down Expand Up @@ -949,6 +953,12 @@ void nuiScrollView::Dragged(nuiSize X, nuiSize Y)
}
}

bool nuiScrollView::MouseCanceled(const nglMouseInfo& rInfo)
{
mLeftClick--;
}


nuiScrollBar* nuiScrollView::GetScrollBar(nuiOrientation Orientation)
{
return Orientation == nuiHorizontal ? mpHorizontal : mpVertical;
Expand Down Expand Up @@ -1285,12 +1295,10 @@ bool nuiScrollView::PreMouseClicked(const nglMouseInfo& rInfo)
mTouched = true;
if (mSpeedX != 0 || mSpeedY != 0)
{
mLeftClick = true;
mLeftClick++;
mTimerOn = false;
mSpeedX = 0;
mSpeedY = 0;
mClickX = rInfo.X;
mClickY = rInfo.Y;
mLastX = rInfo.X;
mLastY = rInfo.Y;
mXOffset = GetXPos();
Expand Down

0 comments on commit 1adfd20

Please sign in to comment.