Added support for left mouse click
This commit is contained in:
@@ -110,6 +110,10 @@ class JGE
|
|||||||
static std::multimap<LocalKeySym, JButton> keyBinds;
|
static std::multimap<LocalKeySym, JButton> keyBinds;
|
||||||
typedef std::multimap<LocalKeySym, JButton>::iterator keycodes_it;
|
typedef std::multimap<LocalKeySym, JButton>::iterator keycodes_it;
|
||||||
|
|
||||||
|
// Mouse attributes
|
||||||
|
int mLastLeftClickX;
|
||||||
|
int mlastLeftClickY;
|
||||||
|
|
||||||
friend void Run();
|
friend void Run();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -272,7 +276,17 @@ class JGE
|
|||||||
void ReleaseKey(const LocalKeySym);
|
void ReleaseKey(const LocalKeySym);
|
||||||
void ReleaseKey(const JButton);
|
void ReleaseKey(const JButton);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Mouse events
|
||||||
|
/// x and y are int coordinates relative to SCREEN_WIDTH and SCREEN_HEIGHT
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
void LeftClicked(int x, int y);
|
||||||
|
|
||||||
|
void LeftClickedProcessed();
|
||||||
|
|
||||||
|
// Getter, may have to move that in the JGuiListener
|
||||||
|
// Returns false if nothing has been clicked, true otherwise
|
||||||
|
bool GetLeftClickCoordinates(int& x, int& y);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
/// Get if the system is ended/paused or not.
|
/// Get if the system is ended/paused or not.
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ class JGuiObject
|
|||||||
virtual bool Leaving(JButton key); // when focus is transferring away from this obj, true to go ahead
|
virtual bool Leaving(JButton key); // when focus is transferring away from this obj, true to go ahead
|
||||||
virtual bool ButtonPressed(); // action button pressed, return false to ignore
|
virtual bool ButtonPressed(); // action button pressed, return false to ignore
|
||||||
|
|
||||||
|
// Used for mouse support so that the GUI engine can found out which Object was selected
|
||||||
|
virtual bool getTopLeft(int& top, int& left) {return false;};
|
||||||
|
|
||||||
int GetId();
|
int GetId();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -262,9 +262,33 @@ void JGE::ResetInput()
|
|||||||
{
|
{
|
||||||
while (!keyBuffer.empty()) keyBuffer.pop();
|
while (!keyBuffer.empty()) keyBuffer.pop();
|
||||||
holds.clear();
|
holds.clear();
|
||||||
|
LeftClickedProcessed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JGE::LeftClicked(int x, int y)
|
||||||
|
{
|
||||||
|
mLastLeftClickX = x;
|
||||||
|
mlastLeftClickY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JGE::LeftClickedProcessed()
|
||||||
|
{
|
||||||
|
mLastLeftClickX = -1;
|
||||||
|
mlastLeftClickY = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool JGE::GetLeftClickCoordinates(int& x, int& y)
|
||||||
|
{
|
||||||
|
if(mLastLeftClickX != -1 || mlastLeftClickY != -1)
|
||||||
|
{
|
||||||
|
x = mLastLeftClickX;
|
||||||
|
y = mlastLeftClickY;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
JGE::JGE()
|
JGE::JGE()
|
||||||
{
|
{
|
||||||
mApp = NULL;
|
mApp = NULL;
|
||||||
@@ -295,6 +319,7 @@ void JGE::Init()
|
|||||||
JRenderer::GetInstance();
|
JRenderer::GetInstance();
|
||||||
JFileSystem::GetInstance();
|
JFileSystem::GetInstance();
|
||||||
JSoundSystem::GetInstance();
|
JSoundSystem::GetInstance();
|
||||||
|
LeftClickedProcessed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void JGE::SetDelta(float delta)
|
void JGE::SetDelta(float delta)
|
||||||
|
|||||||
@@ -148,6 +148,37 @@ bool JGuiController::CheckUserInput(JButton key){
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{ // a dude may have clicked somewhere, we're gonna select the closest object from where he clicked
|
||||||
|
int x, y;
|
||||||
|
unsigned int distance2;
|
||||||
|
unsigned int minDistance2 = -1;
|
||||||
|
int n = mCurr;
|
||||||
|
if(mEngine->GetLeftClickCoordinates(x, y))
|
||||||
|
{
|
||||||
|
for(int i = 0; i < mCount; i++)
|
||||||
|
{
|
||||||
|
int top, left;
|
||||||
|
if(mObjects[i]->getTopLeft(top, left))
|
||||||
|
{
|
||||||
|
distance2 = (top-y)*(top-y) + (left-x)*(left-x);
|
||||||
|
if(distance2 < minDistance2)
|
||||||
|
{
|
||||||
|
minDistance2 = distance2;
|
||||||
|
n = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(JGE_BTN_DOWN))
|
||||||
|
{
|
||||||
|
mCurr = n;
|
||||||
|
mObjects[mCurr]->Entering();
|
||||||
|
}
|
||||||
|
mEngine->LeftClickedProcessed();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
void JGuiController::Update(float dt)
|
void JGuiController::Update(float dt)
|
||||||
|
|||||||
Reference in New Issue
Block a user