Sign in to follow this  
shadowvv

how to implement mouse drag function

Recommended Posts

hi,guys!

I want to read a class to encapsulate I/O operation.

The class get mouse input data and then put that in a buffer.

The problem is every time the data only include which button is pressed or whether the mouse is moving.

I do not know how to get them all to implement mouse drag function.

Any help is appreciated

code:
[code]

DIPROPDWORD dipROPdw;
dipROPdw.diph.dwSize = sizeof(DIPROPDWORD);
dipROPdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipROPdw.diph.dwObj = 0;
dipROPdw.diph.dwHow = DIPH_DEVICE;
dipROPdw.dwData = MOUSEBUFFERNUM;
if (FAILED(m_pMouseDevice->SetProperty(DIPROP_BUFFERSIZE,&dipROPdw.diph)))
{
::MessageBoxA(NULL,"failed to set mouse buffer!","Error!",MB_OK);
return SF_ERROR;
}



for (int i = 0;i<MOUSEBUFFERNUM;i++)
{
if(FAILED(m_pMouseDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&m_mouseBuffer[i],&dwReadNum,0)))
{
m_pMouseDevice->Acquire();
}
}


int SFCInputController::mouseDragLeft(DWORD button)
{
for (int i = 0;i<MOUSEBUFFERNUM;i++)
{
if(m_mouseBuffer[i].dwOfs == button && m_mouseBuffer[i].dwData & 0x80)
{
if (m_mouseBuffer[i].dwOfs == DIMOFS_X && m_mouseBuffer[i].dwData < 0)
{
return true;
}
else
return false;

}
else
return false;
}
}
[/code]

Share this post


Link to post
Share on other sites
You'll need to store previous states as well to detect whether the mouse has just been clicked or is being hold.
[code]mousePreviousState = mouseCurrentState
mouseCurrentState = UpdateState()[/code]
This example is simplified, you should be having a second buffer. When your previous state was up, and your current state is down you start dragging.

Share this post


Link to post
Share on other sites
Your application should have a MouseDown, MouseUp, and MouseMoved function to capture all the mouse events.
I am posting snippets of code that are not 100% accurate, but you should get the idea of how to implement it

Your application should also have the previous state of the mouse:
bool LMouseButtonDown, RMouseButtonDown;

there should also be a global variable storing the current mouse position, as well as the last mouse position
[code]

bool MouseButtonDown(const uint32_t &button){
if(button == MOUSE_RBUTTON) RButtonDown=true;
if(button == MOUSE_LBUTTON) LButtonDown = true;
}
bool MouseButtonUp(const uint32_t &button){
if(button == MOUSE_RBUTTON) RButtonDown=false;
if(button == MOUSE_LBUTTON) LButtonDown = false;
}

bool MouseMoved(){
if(LButtonDown){// we are clicking and holding and draggin because the LButtonDown is set to true and the mouse is moving!!
Put code here for mouse dragging stuff
this is where you would use the last mouseposition and the new mouse position to see where to mouse moving from to implement your dragging function
}
}
[/code]


that is about it. I tried to be as simple as possible so you could get the idea.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this