summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cObject.cpp74
-rw-r--r--cObject.h14
-rw-r--r--cObjectHandler.cpp92
-rw-r--r--cObjectHandler.h18
4 files changed, 174 insertions, 24 deletions
diff --git a/cObject.cpp b/cObject.cpp
index 8b18e41..118fe6f 100644
--- a/cObject.cpp
+++ b/cObject.cpp
@@ -2,6 +2,50 @@
cObject::cObject(int _sx, int _sy)
{
+ bSizeSet = false;
+
+ setSize(_sx, _sy);
+}
+
+cObject::~cObject()
+{
+ destruct();
+}
+
+sPos cObject::getPosition()
+{
+ return pos;
+}
+
+void cObject::setPosition(sPos _pos)
+{
+ pos = _pos;
+}
+
+
+void cObject::setPosition(int _x, int _y)
+{
+ pos.x = _x;
+ pos.y = _y;
+}
+
+
+sObject cObject::getObject()
+{
+ return sObject{pos, wColor, cScreen, sizeX, sizeY};
+}
+
+//protected
+cObject::cObject()
+{
+ bSizeSet = false;
+}
+
+void cObject::setSize(int _sx, int _sy)
+{
+ if(bSizeSet)
+ return;
+
bBlockRender = true; //Block inherited render capabilities of parent
sizeX = _sx;
@@ -22,10 +66,15 @@ cObject::cObject(int _sx, int _sy)
wColor[o][i] = _COL_DEFAULT;
}
}
+
+ bSizeSet = true;
}
-cObject::~cObject()
+void cObject::destruct()
{
+ if(!bSizeSet)
+ return;
+
for (int i = 0; i < sizeX; i++) {
free(cScreen[i]);
free(wColor[i]);
@@ -33,27 +82,6 @@ cObject::~cObject()
free(cScreen);
free(wColor);
-}
-
-sPos cObject::getPosition()
-{
- return pos;
-}
-
-void cObject::setPosition(sPos _pos)
-{
- pos = _pos;
-}
-
-void cObject::setPosition(int _x, int _y)
-{
- pos.x = _x;
- pos.y = _y;
-}
-
-
-sObject cObject::getObject()
-{
- return sObject{pos, wColor, cScreen, sizeX, sizeY};
+ bSizeSet = false;
}
diff --git a/cObject.h b/cObject.h
index c3114c3..607af4e 100644
--- a/cObject.h
+++ b/cObject.h
@@ -20,7 +20,7 @@ public:
//_sx : SizeX
//_sy : SizeY
- ~cObject();
+ virtual ~cObject();
sPos getPosition();
@@ -30,7 +30,19 @@ public:
sObject getObject();
+ virtual void onClick(sPos _pos, unsigned int _button){};
+
+ virtual void onChar(unsigned char _c){};
+
+protected: //For child classes
+ cObject();
+
+ void setSize(int _sx, int _sy);
+
+ void destruct();
+
private:
//wColor, cScreen, sizeX and sizeY are inherited from cRender
sPos pos;
+ bool bSizeSet;
};
diff --git a/cObjectHandler.cpp b/cObjectHandler.cpp
index d35653a..f1252d3 100644
--- a/cObjectHandler.cpp
+++ b/cObjectHandler.cpp
@@ -3,11 +3,18 @@
cObjectHandler::cObjectHandler(cRender *_render)
{
render = _render;
+ iActiveObject = 0;
+
+ objects.push_back(NULL); //Create first Object as Catcher for Events
+
+ buildHitmap();
}
int cObjectHandler::createObject(cObject *_object)
{
objects.push_back(_object);
+
+ buildHitmap();
return objects.size() - 1;
}
@@ -26,6 +33,7 @@ int cObjectHandler::moveObject(int _object, sPos _pos, int _mode)
else if (_mode == _MOVE_ABSOULUTE)
objects[_object]->setPosition(_pos);
+ buildHitmap();
return 0;
}
@@ -34,6 +42,7 @@ int cObjectHandler::destroyObject(int _object)
delete objects[_object];
objects[_object] = NULL;
+ buildHitmap();
return 0;
}
@@ -61,3 +70,86 @@ int cObjectHandler::write()
return 0;
}
+
+int cObjectHandler::clickEvent(sPos _pos, unsigned int _button)
+{
+ if(objects[ iHitMap[_pos.x][_pos.y] ])
+ objects[ iHitMap[_pos.x][_pos.y] ]->onClick(_pos, _button);
+ else
+ return 1;
+
+ return 0;
+}
+
+int cObjectHandler::charEvent(unsigned char _c)
+{
+ if(objects.size() > iActiveObject)
+ {
+ if(objects[iActiveObject])
+ {
+ objects[iActiveObject]->onChar(_c);
+ }
+ else
+ return 1;
+ }
+
+ return 0;
+}
+
+void cObjectHandler::buildHitmap()
+{
+ //Rebuild 2D vector
+ sPos size = render->getSize();
+
+ vector<unsigned int> cp;
+
+ while(size.y > cp.size())
+ {
+ cp.push_back(0);
+ }
+
+ while (size.x > iHitMap.size())
+ {
+ iHitMap.push_back(cp);
+ }
+
+ while (size.x <= iHitMap.size())
+ {
+ iHitMap.pop_back();
+ }
+
+ //Write object IDs to iHitMap
+ for(unsigned int i = 0; i < objects.size(); i++)
+ {
+ if(objects[i])
+ {
+ sPos oPos = objects[i]->getPosition();
+ sPos oSize = objects[i]->getSize();
+
+ for(unsigned int x = oPos.x; x < oPos.x + oSize.x; x++)
+ {
+ for(unsigned int y = oPos.y; y < oPos.y + oSize.y; y++)
+ {
+ if(x < size.x && y < size.y) //Objects can be outside the screen.
+ iHitMap[x][y] = i;
+ }//for
+ }//for
+ }//if
+ }//for
+}//buildHitmap
+
+void cObjectHandler::focusNext()
+{
+ iActiveObject++;
+
+ if(iActiveObject >= objects.size())
+ iActiveObject = 0;
+}
+
+void cObjectHandler::focus(unsigned int _id)
+{
+ if(_id >= objects.size())
+ iActiveObject = objects.size();
+ else
+ iActiveObject = _id;
+}
diff --git a/cObjectHandler.h b/cObjectHandler.h
index 1749c9b..ee83877 100644
--- a/cObjectHandler.h
+++ b/cObjectHandler.h
@@ -31,7 +31,25 @@ public:
int write();
//writes all objects in objects[] to render buffer
+ int clickEvent(sPos _pos, unsigned int _button);
+ //Calls onClick of cObject at _pos, focuses Object
+ //returns 0 if successfull, 1 if Object is empty
+
+ int charEvent(unsigned char _c);
+ //Calls onChar of active cObject, default 0
+ //returns 0 if successfull, 1 if focused Object is empty
+
+ void focusNext();
+ //Focuses next Object
+
+ void focus(unsigned int _id);
+ //Focuses specific Object
+
private:
+ void buildHitmap();
+
vector<cObject*> objects;
+ vector<vector<unsigned int>> iHitMap;
cRender *render;
+ unsigned long int iActiveObject;
};