Richard Parker / EALCD
Revision:
1:f04bcaea1d60
Child:
4:f8f7f4f9c58d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manager/EATouchManager.cpp	Thu Mar 04 10:54:06 2010 +0000
@@ -0,0 +1,141 @@
+#include "mbed.h"
+#include "EATouchManager.h"
+
+#include "../screen/EATouch.h"
+#include "EAHitBox.h"
+
+EATouchManager::EATouchManager(EATouch& touch)
+:   _touch(touch),
+    _head(NULL)
+{
+}
+
+EATouchManager::~EATouchManager()
+{
+    // Free any memory associated with the list.
+    clearHitBoxes();
+}
+
+void EATouchManager::tick()
+{
+    short x = 0;
+    short y = 0;
+    bool p = false;
+    
+    // Get the touch.
+    _touch.touch(x, y, p);
+      
+    // Check if hit target.
+    if (p == true)
+    {
+        _doHits(x, y);
+    }
+}
+
+void EATouchManager::_doHits(short x, short y)
+{
+    EAHitBox* current = _head;
+    
+    while (current != NULL)
+    {
+        // Do the check this will fire the pointer function if it does.
+        current->checkContains(x, y);
+        
+        // Get the next item in the linked list.
+        current = current->next();
+    }
+}
+
+void EATouchManager::deleteHitBox(EAHitBox* box)
+{
+    EAHitBox* current = _head;
+    EAHitBox* last = NULL;
+    EAHitBox* next = NULL;
+    
+    // If not valid then just return.
+    if (box == NULL)
+    {
+        return;
+    }
+    
+    while (current != NULL)
+    {
+        // Get the next item in the linked list.
+        next = current->next();
+        
+        // If the pointer matches the one that want to delete then first
+        // remove from the list, put the link in from previous to next
+        // and actually delet the data.
+        if (box == current)
+        {
+            if ((next != NULL) && (last == NULL))
+            {
+                // If there is an item after but none before then the next 
+                // becomes the root item.
+                _head = next;
+            } 
+            else if ((next != NULL) && (last != NULL)) 
+            {
+                // There was a last and there is a next so need to stitch 
+                // list together removing the current.
+                last->setNext(next);
+            }
+            else if ((next == NULL) && (last == NULL))
+            {
+                // If there is no item after this one then nothing else to do.
+                _head = NULL;
+            }
+            else if ((next == NULL) && (last != NULL))
+            {
+                // Last is now the end of the list.
+                last->setNext(NULL);
+            }
+                
+            // Actually get rid of memory.
+            delete current;
+            current = NULL;
+            break;                
+        }
+        
+        // Swap pointers over.
+        last = current;
+        current = next;
+    }
+}
+
+EAHitBox* EATouchManager::createHitBox()
+{
+    EAHitBox* box = new EAHitBox();
+    
+    // If able to create then add the pointer to the list and return.
+    if (box != NULL)
+    {
+        box->setNext(_head);
+        _head = box;
+    }
+    
+    return box;
+}
+
+void EATouchManager::clearHitBoxes()
+{
+    EAHitBox* current = _head;
+    EAHitBox* next = NULL;
+       
+    // Delete each hit box in turn.
+    while (current != NULL)
+    { 
+        next = current->next();
+        
+        delete current;
+        current = NULL;
+        
+        current = next;
+    }
+    
+    // Reset the root.
+    _head = NULL;
+
+}
+
+