void ofxGuiGrid::draw() { selectedColor(); glPushMatrix(); glTranslatef( mObjX, mObjY, 0.0f ); if ( bDblClickMode ) { this->dblClickImage->draw(); } else { if ( mParamName != "" ) { drawParamString( 0.0, 0.0, mParamName, false ); } ofFill(); //! Background //glColor4f( mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a ); ofNoFill(); //! Grids for ( int j = 0; j < mYGrid; j++ ) { for ( int i = 0; i < mXGrid; i++ ) { int index = i + j * mXGrid; ofNoFill(); if ( mSelectedId == index ) { drawSelectedRect( getGridX(i), getGridY(j), getGridWidth(), getGridHeight() ); } else { glColor4f( mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a ); //ofRect( mCtrX + mBorder + i * mSpacing + i * mGridWidth, // mCtrY + mBorder + j * mSpacing + j * mGridHeight, // mGridWidth, mGridHeight ); ofRect( getGridX(i), getGridY(j), getGridWidth(), getGridHeight() ); } if ( gridImages[index] != NULL ) { gridImages[index]->draw(); } } } } ofNoFill(); //! Frame glColor4f( mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a ); ofRect( mCtrX, mCtrY, mCtrWidth, mCtrHeight ); glPopMatrix(); if ( !bDblClickMode ) { for ( int i = 0; i < mObjects.size(); ++i ) { mObjects[i]->draw(); } } }
void OrthographicGridPathfinder::moveSelectedPathfindingCell(int worldX, int worldY) { int col = worldX/getGridWidth(); int row = worldY/getGridHeight(); selectedPathfindingCell.setCenterX(getColumnCenterX(col)); selectedPathfindingCell.setCenterY((float)getRowCenterY(row)); }
float CRole::getDistanceSqInGrid(IGridRole* role) { Point p1 = getPositionInGrid(); Point p2 = role->getPositionInGrid(); int i,j,m,n; float smallest = FLT_MAX; for (i = 0; i < getGridWidth(); ++i) { for (j = 0; j < getGridHeight(); ++j) { Point pos1(p1.x + i, p1.y + j); for (m = 0; m < role->getGridWidth(); ++m) { for (n = 0; n < role->getGridHeight(); ++n) { Point pos2(p2.x + m, p2.y + n); float dist = pos1.getDistanceSq(pos2); if (dist < smallest) { // CCLOG("(%f,%f --> %f, %f) %f", pos1.x, pos1.y, pos2.x, pos2.y, dist); smallest = dist; } } } } } return smallest; }
Point CRole::getPositionInGrid() { CLogicGrid* lg = getLogicGrid(); return lg->getGridPos(); int w = getGridWidth(); int h = getGridHeight(); Point pos = lg->getGridPos(); pos.x += (w - 1) * 0.5f; pos.y += (h - 1) * 0.5f; return pos; }
Organism& World::getLandscapePosition(int x, int y) { if (x >= 0 && x <= this->getGridHeight() - 1 && y >= 0 && y <= getGridWidth() - 1) { return *this->landscape[x][y]; } else { Organism *test = new Ant; delete(test); test = NULL; return *test; } }
OrthographicGridPathfinder::OrthographicGridPathfinder(Game *initGame) { game = initGame; // WE'LL DETERMINE THE NUMBER OF ROWS AND COLUMNS BASED ON TEH FIRST // COLLIDABLE TILED LAYER WE FIND World *world = game->getGSM()->getWorld(); numColumns = world->getCollidableGridColumns(); numRows = world->getCollidableGridRows(); initGrid(); selectedPathfindingCell.setCenterX(0.0f); selectedPathfindingCell.setCenterY(0.0f); selectedPathfindingCell.setWidth(getGridWidth()); selectedPathfindingCell.setHeight(getGridHeight()); }
int ofxGuiGrid::mouseToGridId( ofxPoint2f p ) { int index = 0; for ( int j = 0; j < mYGrid; ++j ) { for ( int i = 0; i < mXGrid; ++i ) { int index = i + j * mXGrid; if ( p.x >= getGridX(i) && p.x <= getGridX(i) + getGridWidth() && p.y >= getGridY(j) && p.y <= getGridY(j) + getGridHeight() ) { return index; // return the index of grid } } } return -1; //! No id found }
FXImage* GUISUMOAbstractView::checkGDALImage(Decal& d) { #ifdef HAVE_GDAL GDALAllRegister(); GDALDataset* poDataset = (GDALDataset*)GDALOpen(d.filename.c_str(), GA_ReadOnly); if (poDataset == 0) { return 0; } const int xSize = poDataset->GetRasterXSize(); const int ySize = poDataset->GetRasterYSize(); // checking for geodata in the picture and try to adapt position and scale if (d.width <= 0.) { double adfGeoTransform[6]; if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None) { Position topLeft(adfGeoTransform[0], adfGeoTransform[3]); const double horizontalSize = xSize * adfGeoTransform[1]; const double verticalSize = ySize * adfGeoTransform[5]; Position bottomRight(topLeft.x() + horizontalSize, topLeft.y() + verticalSize); if (GeoConvHelper::getProcessing().x2cartesian(topLeft) && GeoConvHelper::getProcessing().x2cartesian(bottomRight)) { d.width = bottomRight.x() - topLeft.x(); d.height = topLeft.y() - bottomRight.y(); d.centerX = (topLeft.x() + bottomRight.x()) / 2; d.centerY = (topLeft.y() + bottomRight.y()) / 2; //WRITE_MESSAGE("proj: " + toString(poDataset->GetProjectionRef()) + " dim: " + toString(d.width) + "," + toString(d.height) + " center: " + toString(d.centerX) + "," + toString(d.centerY)); } else { WRITE_WARNING("Could not convert coordinates in " + d.filename + "."); } } } #endif if (d.width <= 0.) { d.width = getGridWidth(); d.height = getGridHeight(); } // trying to read the picture #ifdef HAVE_GDAL const int picSize = xSize * ySize; FXColor* result; if (!FXMALLOC(&result, FXColor, picSize)) { WRITE_WARNING("Could not allocate memory for " + d.filename + "."); return 0; } for (int j = 0; j < picSize; j++) { result[j] = FXRGB(0, 0, 0); } bool valid = true; for (int i = 1; i <= poDataset->GetRasterCount(); i++) { GDALRasterBand* poBand = poDataset->GetRasterBand(i); int shift = -1; if (poBand->GetColorInterpretation() == GCI_RedBand) { shift = 0; } else if (poBand->GetColorInterpretation() == GCI_GreenBand) { shift = 1; } else if (poBand->GetColorInterpretation() == GCI_BlueBand) { shift = 2; } else if (poBand->GetColorInterpretation() == GCI_AlphaBand) { shift = 3; } else { WRITE_MESSAGE("Unknown color band in " + d.filename + ", maybe fox can parse it."); valid = false; break; } assert(xSize == poBand->GetXSize() && ySize == poBand->GetYSize()); if (poBand->RasterIO(GF_Read, 0, 0, xSize, ySize, ((unsigned char*)result) + shift, xSize, ySize, GDT_Byte, 4, 4 * xSize) == CE_Failure) { valid = false; break; } } GDALClose(poDataset); if (valid) { return new FXImage(getApp(), result, IMAGE_OWNED | IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP, xSize, ySize); } FXFREE(&result); #endif return 0; }
//----------------------------------------------------------------------------// Sizef GridLayoutContainer::getGrid() const { return Sizef(static_cast<float>(getGridWidth()), static_cast<float>(getGridHeight())); }
float OrthographicGridPathfinder::getColumnCenterX(int column) { int gridWidth = getGridWidth(); float centerX = (gridWidth * column) + (gridWidth/2.0f); return centerX; }
void OrthographicGridPathfinder::buildPath( list<PathNode> *pathToFill, float startX, float startY, float endX, float endY) { // SO BUILD THE PATH pathToFill->clear(); vector<bool> *pathGridPointer = &pathGrid; GameStateManager *gsm = game->getGSM(); World *world = gsm->getWorld(); int gridWidth = getGridWidth(); int gridHeight = getGridHeight(); // DETERMINE THE START COLUMN AND START ROW int startColumn = (int)(startX/gridWidth); int startRow = (int)(startY/gridHeight); // DETERMINE THE END COLUMN AND END ROW int endColumn = (int)(endX/gridWidth); int endRow = (int)(endY/gridHeight); // IF THE DESTINATION IS A COLLIDABLE TILE LOCATION // THEN EXIT int endIndex = getGridIndex(endColumn, endRow); bool endIndexIsWalkable = pathGrid[getGridIndex(endColumn, endRow)]; if (!endIndexIsWalkable) return; map<int, PathNode> openNodes; map<int, PathNode> closedNodes; // list<PathNode> openList; // list<PathNode> closedList; bool pathFound = false; bool nodesToAdd[9]; PathNode startNode; startNode.column = startColumn; startNode.row = startRow; startNode.parentNode = NULL; startNode.G = 0; PathNode endNode; endNode.column = endColumn; endNode.row = endRow; endNode.parentNode = NULL; startNode.H = calculateH(startNode, &endNode); // openList.push_back(startNode); int nodeIndex = getGridIndex(startColumn, startRow); openNodes[nodeIndex] = startNode; while (!pathFound) { // IF THERE ARE NO MORE NODES TO SEARCH THROUGH TO FIND // OUR DESTINATION THEN WE'RE DONE // if (openList.size() == 0) if (openNodes.size() == 0) { pathToFill->clear(); return; } // FIRST GET THE CLOSEST NODE FROM THE OPEN LIST // PathNode *foundNode = findCheapestNode(&openList); PathNode *foundNode = findCheapestNode(&openNodes); PathNode cheapestNode; cheapestNode.column = foundNode->column; cheapestNode.row = foundNode->row; cheapestNode.G = foundNode->G; cheapestNode.H = foundNode->H; cheapestNode.parentNode = foundNode->parentNode; // removeNodeFromList(&cheapestNode, &openList); openNodes.erase(getGridIndex(cheapestNode.column, cheapestNode.row)); // IS THE CHEAPEST NODE THE DESTINATION? if ((cheapestNode.column == endNode.column) && (cheapestNode.row == endNode.row)) { // WE'VE REACHED THE DESTINATION pathFound = true; PathNode *traveller = &cheapestNode; while (traveller != NULL) { PathNode nodeToAdd; nodeToAdd.column = traveller->column; nodeToAdd.row = traveller->row; pathToFill->push_front(nodeToAdd); traveller = traveller->parentNode; } } else { // WE'LL NEED TO LOOK AT THE CHEAPEST NODE'S NEIGHBORS // FIRST LET'S PUT IT INTO THE CLOSED LIST SO WE DON'T // END UP IN AN INFINITELY CIRCULAR LOOP // closedList.push_back(cheapestNode); closedNodes[getGridIndex(cheapestNode.column, cheapestNode.row)] = cheapestNode; // PathNode *nodeJustAdded = &closedList.back(); PathNode *nodeJustAdded = &closedNodes[getGridIndex(cheapestNode.column, cheapestNode.row)]; // NOW FIGURE OUT WHICH NEIGHBORS MIGHT BE USABLE // findNeighborsToCheck(world, nodesToAdd, nodeJustAdded, &closedList); findNeighborsToCheck(nodesToAdd, nodeJustAdded, &closedNodes); // NOW ADD THE NEIGHBORS TO OUR OPEN LIST // addNeighbors(nodesToAdd, nodeJustAdded, &endNode, &openList, &closedList); addNeighbors(nodesToAdd, nodeJustAdded, &endNode, &openNodes, &closedNodes); } } PathNode lastNode = pathToFill->back(); destinationPathfindingCell.setCenterX(getColumnCenterX(lastNode.column)); destinationPathfindingCell.setCenterY(getRowCenterY(lastNode.row)); destinationPathfindingCell.setWidth(this->getGridWidth()); destinationPathfindingCell.setHeight(this->getGridHeight()); }
//----------------------------------------------------------------------------// Sizef GridLayoutContainer::getGrid() const { return Sizef(getGridWidth(),getGridHeight()); }
bool ofxGuiGrid::mousePressed( int x, int y, int button ) { if ( !mIsActive ) { //! This control is not active now return false; } ofxPoint2f inside = mouseToLocal( x, y ); mMouseIsDown = isPointInsideMe( inside ); if ( mMouseIsDown ) { // ----------------------- if ( bDblClickMode ) { // TODO mNowClickTime = ofGetSystemTime(); if ( mNowClickTime - mPrevClickTime <= OFXGUI_DBLCLICK_INTERVAL ) { mPrevClickTime = 0; // reset switchDblClickMode( false ); return true; } mPrevClickTime = mNowClickTime; return true; } // ----------------------- //! Check the controls first bool handled = false; for ( int i = 0; i < mObjects.size(); i++ ) { handled = (ofxGuiObject*)mObjects[i]->mousePressed( x, y, button ); if ( handled ) { return true; // we got the result } } // ----------------------- int id = mouseToGridId( inside ); setSelectedId( id ); mGlobals->mListener->handleGui( mParamId, kofxGui_Set_Int, &mCamIndex, sizeof(int) ); mDragging = false; //! reset the value if ( id != -1 ) { clickingPoint = inside; mValidSelection = true; } else { mValidSelection = false; } // ----------------------- //! Double click check mNowClickTime = ofGetSystemTime(); if ( id != -1 && utils->isUsed( id ) && mNowClickTime - mPrevClickTime <= OFXGUI_DBLCLICK_INTERVAL ) { mPrevClickTime = 0; // reset the time switchDblClickMode( true ); return true; } mPrevClickTime = mNowClickTime; // ------------------------ //! ----- Add controls ----- //! Control temp values int x, y, textWidth; string btnText = ""; ofxGuiButton* btn; //! The reset button if ( bShowResetBtn ) { removeControl( mResetBtnId ); if ( id != -1 && utils->isUsed( id )) { //! The position is relate with the panel x = mObjX + getGridX( id % mXGrid ) + 5; y = mObjY + getGridY( id / mXGrid ) + getGridHeight() - 5 - OFXGUI_BUTTON_HEIGHT; //! If the grid is too small, we remove the text btnText = "Reset"; textWidth = mGlobals->mParamFont.stringWidth( btnText ); if ( getGridWidth() < textWidth + 5 + OFXGUI_BUTTON_HEIGHT ) { btnText = ""; } btn = (ofxGuiButton*)addButton( mResetBtnId, btnText, x, y, OFXGUI_BUTTON_HEIGHT, OFXGUI_BUTTON_HEIGHT, kofxGui_Button_Off, kofxGui_Button_Trigger ); btn->setHighlightMode( true ); } } //! The setting button if ( bShowSettingBtn ) { removeControl( mSettingBtnId ); if ( id != -1 && utils->isUsed( id ) ) { y -= 15; btnText = "Setting"; textWidth = mGlobals->mParamFont.stringWidth( btnText ); if ( getGridWidth() < textWidth + 5 + OFXGUI_BUTTON_HEIGHT ) { btnText = ""; } btn = (ofxGuiButton*)addButton( mSettingBtnId, btnText, x, y, OFXGUI_BUTTON_HEIGHT, OFXGUI_BUTTON_HEIGHT, kofxGui_Button_Off, kofxGui_Button_Trigger ); btn->setHighlightMode( true ); } } } return mMouseIsDown; }