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 /cObjectHandler.cpp | |
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
Diffstat (limited to 'cObjectHandler.cpp')
-rw-r--r-- | cObjectHandler.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
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; +} |