diff options
author | jonas <himself@jonasgunz.de> | 2018-12-28 13:04:01 +0100 |
---|---|---|
committer | jonas <himself@jonasgunz.de> | 2018-12-28 13:04:01 +0100 |
commit | c5af4c3057a904f2a037a624273c1c7a4b0295a5 (patch) | |
tree | 4af973d27706db05a5fba267fa93370429a056d3 | |
parent | 3cfea2815e3edfeb15fd7f0173957254babc7326 (diff) | |
download | termgl-c5af4c3057a904f2a037a624273c1c7a4b0295a5.tar.gz |
Adding Click and Char event handling and focusing to cObjectHandler
cObject: is now able to be parent class
cObjectHandler: Implemented
-rw-r--r-- | cObject.cpp | 74 | ||||
-rw-r--r-- | cObject.h | 14 | ||||
-rw-r--r-- | cObjectHandler.cpp | 92 | ||||
-rw-r--r-- | cObjectHandler.h | 18 |
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; } @@ -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; }; |