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(); } } }
bool BattleSquad::move(int tgx, int tgy, unsigned int &eventflag, bool costap) { MapDataManager* mapdatamanager = MapDataManager::getSingletonPtr(); if(mapdatamanager->getPassable(tgx, tgy, getFaction())) { float mapapcost = 0.0f; if(getHorseId() != "none") mapapcost = mapdatamanager->getCavApCost(tgx, tgy, getFaction()); else mapapcost = mapdatamanager->getInfApCost(tgx, tgy, getFaction()); LuaTempContext* luatempcontext = new LuaTempContext(); luatempcontext->strMap["squadid"] = getSquadId(); luatempcontext->intMap["srcx"] = getGridX(); luatempcontext->intMap["srcy"] = getGridY(); luatempcontext->intMap["tgtx"] = tgx; luatempcontext->intMap["tgty"] = tgy; luatempcontext->floatMap["apcost"] = mapapcost; Trigger("MoveTo", luatempcontext); mapdatamanager->Trigger("MoveTo", luatempcontext); mapapcost = luatempcontext->floatMap["apcost"]; delete luatempcontext; if(costap) { float ap = getActionPoint(); if(mapapcost <= ap) { ap -= mapapcost; setActionPoint(ap); if(mapapcost <= 2) { eventflag |= MOVEEVENT_CHARGE; int curdir = GetDirection(getGridX(), getGridY(), tgx, tgy); eventflag &= 0x03f; eventflag |= SetChargeDir(curdir); } else { eventflag &= ~MOVEEVENT_CHARGE; } return true; } } else { return true; } } eventflag |= MOVEEVENT_WRONG; return false; }
friendGrid::friendGrid() { m_nWidth = 50; m_nLength = 50; // personInGrid personArray[25][25] = { personInGrid(0, 0) }; m_nOriginX = m_nWidth / 2; // Dropping the fraction, this works. m_nOriginY = m_nLength / 2; m_personArray = new personInGrid**[m_nLength + 1]; // Create a dynamic multidimensional array. for (int iii = 0; iii < m_nLength + 1; iii++) { m_personArray[iii] = new personInGrid*[m_nWidth + 1]; } for (int iii = 0; iii <= m_nLength; iii++) // For every row... { for (int bbb = 0; bbb <= m_nWidth; bbb++) // For every item in that row... { m_personArray[iii][bbb] = new personInGrid(getGridX(bbb), getGridY(iii)); } } }
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 }
bool ofxGuiGrid::mouseDragged( 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 ) { return true; } if ( mIsSelectable && mValidSelection) { int id = mouseToGridId( inside ); if ( id + mIndexOffset < utils->getCount() && id != -1 && !utils->isSelected( id + mIndexOffset ) ){ if ( !mDragging ) { //! Get the X/Y mouse position offset this->mDraggingXOffset = clickingPoint.x - getGridX( id ); this->mDraggingYOffset = clickingPoint.y - getGridY( 0 ); //! just one "line", so y == 0 mDragging = true; //! do not get the x/y again for this time. } mGlobals->mListener->handleGui( mParamId, kofxGui_Set_Grid_Dragging, &mCamIndex, sizeof(int) ); } } } return mMouseIsDown; }
void ofxGuiGrid::createImages() { //! Allocate the space gridImages = new ofxGuiImage*[mXGrid*mYGrid]; int index = 0; //! Setup each image for ( int j = 0; j < mYGrid; ++j ) { for ( int i = 0; i < mXGrid; ++i ) { index = i + j * mXGrid; gridImages[index] = new ofxGuiImage(); gridImages[index]->init( CAMERAS_ID_OFFSET + index, "Cam " + ofToString(index), getGridX(i) + 1, getGridY(j) + 1, roundInt(mGridWidth - 2), roundInt(mGridHeight - 2) ); } } }
float BattleSquad::getAttr(int attrtype , int calctype) { DataLibrary* datalib = DataLibrary::getSingletonPtr(); std::vector<std::string> modifierlist = datalib->getChildList(mPath + std::string("/ModifierList")); if(modifierlist.size() == 0) return 0.0f; if(attrtype == ATTR_RANGEDDEFENCE ) attrtype = ATTR_DEFENCE; float base = 0.0f; float mbouse = 0.0f; float mbane = 0.0f; float resist = 0.0f; float cbouse = 0.0f; float cbane = 0.0f; std::vector<std::string>::iterator ite; for(ite = modifierlist.begin(); ite != modifierlist.end(); ite++) { int type = ATTRMODIFIER_BASE; float attrval = 0.0f; std::string datapath = mPath + std::string("/ModifierList/") + (*ite); datalib->getData(datapath + std::string("/Type"), type); switch(attrtype) { case ATTR_ATTACK: datalib->getData(datapath + std::string("/Attack"), attrval); break; case ATTR_RANGEDATTACK: datalib->getData(datapath + std::string("/RangedAttack"), attrval); break; case ATTR_DEFENCE: datalib->getData(datapath + std::string("/Defence"), attrval); break; case ATTR_FORM: datalib->getData(datapath + std::string("/Formation"), attrval); break; case ATTR_INITIATIVE: datalib->getData(datapath + std::string("/Initiative"), attrval); break; case ATTR_ACTIONPOINT: datalib->getData(datapath + std::string("/ActionPoint"), attrval); break; case ATTR_DETECTION: datalib->getData(datapath + std::string("/Detection"), attrval); break; case ATTR_COVERT: datalib->getData(datapath + std::string("/Covert"), attrval); break; case ATTR_TOUGHNESS: datalib->getData(datapath + std::string("/Toughness"), attrval); break; case ATTR_CONTER: datalib->getData(datapath + std::string("/Conter"), attrval); break; } switch(type) { case ATTRMODIFIER_BASE: base += attrval; break; case ATTRMODIFIER_MAGIC: if(attrval > mbouse) mbouse = attrval; if(attrval < mbane) mbane = attrval; break; case ATTRMODIFIER_COMMAND: if(attrval > cbouse) cbouse = attrval; if(attrval < cbane) cbane = attrval; break; case ATTRMODIFIER_RESISTANCE: if(attrval > resist) resist = attrval; break; } } float bouse = cbouse + mbouse; float bane = cbane + mbane; float terrainbouse = 0.0f; switch(attrtype) { case ATTR_DEFENCE: terrainbouse = MapDataManager::getSingleton().getDefModify(getGridX(), getGridY(), getTeam()); break; case ATTR_COVERT: terrainbouse = MapDataManager::getSingleton().getCovert(getGridX(), getGridY(), getTeam()); break; } if(terrainbouse > 0.0f) bouse += terrainbouse; else bane += terrainbouse; if(bane < -resist) { bane += resist; resist = 0.0f; } else { bane = 0.0f; resist += bane; } bouse = bouse + bane; switch(calctype) { case ATTRCALC_FULL: return base + bouse; break; case ATTRCALC_ONLYBASE: return base; break; case ATTRCALC_ONLYBONUS: return bouse; break; } return 0.0f; }
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; }