changeset 25:eae13b04b06f

Working on Gui drag and drop
author koryspansel <koryspansel@bendbroadband.com>
date Fri, 16 Sep 2011 13:11:35 -0700
parents 4ee162fc3537
children 3a63df04f3c0
files LightClone/Source/GuiElement.cpp LightClone/Source/GuiElement.h LightClone/Source/GuiInterface.cpp LightClone/Source/GuiInterface.h
diffstat 4 files changed, 176 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/Source/GuiElement.cpp	Fri Sep 16 09:29:53 2011 -0700
+++ b/LightClone/Source/GuiElement.cpp	Fri Sep 16 13:11:35 2011 -0700
@@ -59,27 +59,33 @@
 }
 
 /*
- * Attach
+ * SetInterface
+ */
+void GuiElement::SetInterface(GuiInterface* pInstance)
+{
+	pInterface = pInstance;
+}
+
+/*
+ * GetInterface
  */
-void GuiElement::Attach(GuiElement* pInstance)
+GuiInterface* GuiElement::GetInterface()
+{
+	return pInterface;
+}
+
+/*
+ * SetParent
+ */
+void GuiElement::SetParent(GuiElement* pInstance)
 {
 	pContainer = pInstance;
 }
 
 /* 
- * Detach
+ * GetParent
  */
-GuiElement* GuiElement::Detach()
-{
-	GuiElement* pInstance = pContainer;
-	pContainer = NULL;
-	return pInstance;
-}
-
-/*
- * GetContainer
- */
-GuiElement* GuiElement::GetContainer()
+GuiElement* GuiElement::GetParent()
 {
 	return pContainer;
 }
@@ -216,7 +222,8 @@
 		eCode = kChildren.Add(pElement);
 		if(eCode == Error_Success)
 		{
-			pElement->Attach(this);	
+			pElement->SetInterface(pInterface);
+			pElement->SetParent(this);
 		}
 	}
 
@@ -235,7 +242,7 @@
 		eCode = kChildren.Remove(pElement);
 		if(eCode == Error_Success)
 		{
-			pElement->Detach();
+			pElement->SetParent(NULL);
 		}
 	}
 
@@ -243,22 +250,6 @@
 }
 
 /*
- * AllowDrag
- */
-bool GuiElement::AllowDrag() const
-{
-	return false;
-}
-
-/*
- * AllowDrop
- */
-bool GuiElement::AllowDrop() const
-{
-	return false;
-}
-
-/*
  * OnMouseDown
  */
 void GuiElement::OnMouseDown(uint32 nButton, float fX, float fY)
@@ -280,13 +271,6 @@
 }
 
 /*
- * OnDrag
- */
-void GuiElement::OnDrag(float fX, float fY)
-{
-}
-
-/*
  * OnDrop
  */
 void GuiElement::OnDrop(GuiElement* pSource, float fX, float fY)
--- a/LightClone/Source/GuiElement.h	Fri Sep 16 09:29:53 2011 -0700
+++ b/LightClone/Source/GuiElement.h	Fri Sep 16 13:11:35 2011 -0700
@@ -13,6 +13,11 @@
 #include "GuiEventMap.h"
 
 /*
+ * GuiInterface
+ */
+class GuiInterface;
+
+/*
  * GuiElement
  */
 class GuiElement : private GuiEventMap
@@ -27,6 +32,11 @@
 protected:
 
 	/*
+	 * pInterface
+	 */
+	GuiInterface* pInterface;
+
+	/*
 	 * pContainer
 	 */
 	GuiElement* pContainer;
@@ -89,19 +99,24 @@
 	virtual void Render(RenderContext& kContext, Camera& kCamera);
 
 	/*
-	 * Attach
+	 * SetInterface
+	 */
+	void SetInterface(GuiInterface* pInstance);
+
+	/*
+	 * GetInterface
 	 */
-	void Attach(GuiElement* pInstance);
+	GuiInterface* GetInterface();
+
+	/*
+	 * SetParent
+	 */
+	void SetParent(GuiElement* pInstance);
 
 	/* 
-	 * Detach
+	 * GetParent
 	 */
-	GuiElement* Detach();
-
-	/*
-	 * GetContainer
-	 */
-	GuiElement* GetContainer();
+	GuiElement* GetParent();
 
 	/*
 	 * SetPosition
@@ -169,16 +184,6 @@
 	ErrorCode Remove(GuiElement* pElement);
 
 	/*
-	 * AllowDrag
-	 */
-	virtual bool AllowDrag() const;
-
-	/*
-	 * AllowDrop
-	 */
-	virtual bool AllowDrop() const;
-
-	/*
 	 * OnMouseDown
 	 */
 	virtual void OnMouseDown(uint32 nButton, float fX, float fY);
@@ -194,11 +199,6 @@
 	virtual void OnMouseMove(float fX, float fY);
 
 	/*
-	 * OnDrag
-	 */
-	virtual void OnDrag(float fX, float fY);
-
-	/*
 	 * OnDrop
 	 */
 	virtual void OnDrop(GuiElement* pSource, float fX, float fY);
--- a/LightClone/Source/GuiInterface.cpp	Fri Sep 16 09:29:53 2011 -0700
+++ b/LightClone/Source/GuiInterface.cpp	Fri Sep 16 13:11:35 2011 -0700
@@ -7,9 +7,10 @@
 /*
  * GuiInterface
  */
-GuiInterface::GuiInterface() : pInputManager(NULL), pDragSource(NULL)
+GuiInterface::GuiInterface() : pInputManager(NULL), pDragElement(NULL), pCaptureElement(NULL), pFocusElement(NULL)
 {
-	pCursor = new GuiCursor();
+	pInterface	= this;
+	pCursor		= new GuiCursor();
 }
 
 /*
@@ -51,8 +52,10 @@
  */
 void GuiInterface::Update(float fElapsed)
 {
-	const float fX = pInputManager->GetMouseX();
-	const float fY = pInputManager->GetMouseY();
+	const float fMouseX	= pInputManager->GetMouseX();
+	const float fMouseY	= pInputManager->GetMouseY();
+	const float fDeltaX	= pInputManager->GetMouseDeltaX();
+	const float fDeltaY	= pInputManager->GetMouseDeltaY();
 
 	GuiElement::Update(fElapsed);
 
@@ -64,47 +67,69 @@
 		OutputDebugStringA(kBuffer);
 		*/
 
-		pCursor->SetPosition(fX, fY);
+		pCursor->SetPosition(fMouseX, fMouseY);
 		pCursor->Update(fElapsed);
 	}
 
-	if(pInputManager->IsButtonDown(MouseButton_Left) && !pInputManager->WasButtonDown(MouseButton_Left))
+	GuiElement* pElement = Pick(fMouseX, fMouseY);
+
+	if(fMouseDeltaX != 0.0f || fMouseDeltaY != 0.0f)
 	{
-		GuiElement* pElement = Pick(fX, fY);
-		if(pElement)
+		//Mouse moved
+		if(pCaptureElement)
 		{
-			pElement->OnMouseDown(MouseButton_Left, fX, fY);
 
-			//TODO: pElement could also be the source of a drag operation
 		}
 	}
-	else
 
-	if(!pInputManager->IsButtonDown(MouseButton_Left) && pInputManager->WasButtonDown(MouseButton_Left))
+	for(uint32 nButton = MouseButton_Left; nButton < MouseButton_Count; ++nButton)
 	{
-		GuiElement* pElement = Pick(fX, fY);
-		if(pElement)
+		// button down
+		if(pInputManager->IsButtonDown(nButton) && !pInputManager->WasButtonDown(nButton))
 		{
-			GuiElement* pContainer = pElement;
-
-			if(pDragSource)
+			if(pCaptureElement)
 			{
-				while(pContainer && !pElement->AllowDrop())
-				{
-					pContainer = pContainer->GetContainer();
-				}
-
-				if(pContainer)
-				{
-					pContainer->OnDrop(pDragSource, fX, fY);
-				}
+				//TODO: Does capture make sense for mouse down events?
+				pElement->OnMouseDown(nButton, fMouseX, fMouseY);
 			}
 			else
 			{
-				pElement->OnMouseUp(MouseButton_Left, fX, fY);
+				if(pElement)
+				{
+					pElement->OnMouseDown(nButton, fMouseX, fMouseY);
+				}
 			}
+		}
+		else
 
-			pDragSource = NULL;
+		// button up
+		if(!pInputManager->IsButtonDown(nButton) && pInputManager->WasButtonDown(nButton))
+		{
+			if(pDragElement)
+			{
+				GuiElement* pElement = Pick(fMouseX, fMouseY);
+				if(pElement)
+				{
+					pElement->OnDrop(pDragElement, fMouseX, fMouseY);
+				}
+
+				pDragElement = NULL;
+			}
+			else
+			{
+				if(pCaptureElement)
+				{
+					pCaptureElement->OnMouseUp(nButton, fMouseX, fMouseY);
+				}
+				else
+				{
+					GuiElement* pElement = Pick(fMouseX, fMouseY);
+					if(pElement)
+					{
+						pElement->OnMouseUp(nButton, fMouseX, fMouseY);
+					}
+				}
+			}
 		}
 	}
 }
@@ -125,9 +150,50 @@
 		if(pCursor)
 		{
 			pCursor->Render(kContext, kCamera);
+
+			if(pDragElement)
+			{
+				//TODO: Move to cursor position
+				//pDragElement->Render(kContext, kCamera);
+			}
 		}
 	}
 
 	//TODO: End batch
 	//kContext.EndBatch()
 }
+
+/*
+ * AcquireCursor
+ */
+ErrorCode GuiInterface::AcquireCursor(GuiElement* pSource)
+{
+	if(!pCaptureElement)
+	{
+		return pCaptureElement = pSource, Error_Success;
+	}
+
+	return Error_Fail;
+}
+
+/*
+ * ReleaseCursor
+ */
+void GuiInterface::ReleaseCursor()
+{
+	pCaptureElement = NULL;
+}
+
+/*
+ * BeginDrag
+ */
+void GuiInterface::BeginDrag(GuiElement* pSource)
+{
+}
+
+/*
+ * EndDrag
+ */
+void GuiInterface::EndDrag()
+{
+}
--- a/LightClone/Source/GuiInterface.h	Fri Sep 16 09:29:53 2011 -0700
+++ b/LightClone/Source/GuiInterface.h	Fri Sep 16 13:11:35 2011 -0700
@@ -21,15 +21,25 @@
 	InputManager* pInputManager;
 
 	/*
-	 * pDragSource
-	 */
-	GuiElement* pDragSource;
-
-	/*
 	 * pCursor
 	 */
 	GuiCursor* pCursor;
 
+	/*
+	 * pDragElement
+	 */
+	GuiElement* pDragElement;
+
+	/*
+	 * pCaptureElement
+	 */
+	GuiElement* pCaptureElement;
+
+	/*
+	 * pFocusElement
+	 */
+	GuiElement* pFocusElement;
+
 public:
 
 	/*
@@ -57,6 +67,26 @@
 	 */
 	void Render(RenderContext& kContext, Camera& kCamera);
 
+	/*
+	 * AcquireCursor
+	 */
+	ErrorCode AcquireCursor(GuiElement* pSource);
+
+	/*
+	 * ReleaseCursor
+	 */
+	void ReleaseCursor();
+
+	/*
+	 * BeginDrag
+	 */
+	void BeginDrag(GuiElement* pSource);
+
+	/*
+	 * EndDrag
+	 */
+	void EndDrag();
+
 private:
 
 	/*