void CNcbiResource::HandleRequest( CCgiContext& ctx ) { bool defCom = false; try { TCmdList::iterator it = find_if( m_cmd.begin(), m_cmd.end(), PRequested<CNcbiCommand>( ctx ) ); unique_ptr<CNcbiCommand> cmd( ( it == m_cmd.end() ) ? ( defCom = true, GetDefaultCommand() ) : (*it)->Clone() ); cmd->Execute( ctx ); #if !defined(NCBI_COMPILER_GCC) || NCBI_COMPILER_VERSION >= 300 || defined(_IO_THROW) } catch( IOS_BASE::failure& /* e */ ) { throw; #endif } catch( std::exception& e ) { _TRACE( e.what() ); ctx.PutMsg( string("Error handling request: ") + e.what() ); if( !defCom ) { unique_ptr<CNcbiCommand> cmd( GetDefaultCommand() ); cmd->Execute( ctx ); } } }
bool CGuiHandler::MousePress(int x,int y,int button) { if(AboveGui(x,y)){ activeMousePress=true; return true; } if(inCommand>=0){ activeMousePress=true; return true; } if(button==SDL_BUTTON_RIGHT){ activeMousePress=true; defaultCmdMemory=GetDefaultCommand(x,y); return true; } return false; }
void CGuiHandler::DrawMapStuff(void) { if(activeMousePress){ int cc=-1; int button=SDL_BUTTON_LEFT; if(inCommand!=-1){ cc=inCommand; } else { if(mouse->buttons[SDL_BUTTON_RIGHT].pressed && mouse->activeReceiver==this){ cc=defaultCmdMemory;//GetDefaultCommand(mouse->lastx,mouse->lasty); button=SDL_BUTTON_RIGHT; } } if(cc>=0 && cc<commands.size()){ switch(commands[cc].type){ case CMDTYPE_ICON_FRONT: if(mouse->buttons[button].movement>30){ if(commands[cc].params.size()>0) if(commands[cc].params.size()>1) DrawFront(button,atoi(commands[cc].params[0].c_str()),atof(commands[cc].params[1].c_str())); else DrawFront(button,atoi(commands[cc].params[0].c_str()),0); else DrawFront(button,1000,0); } break; case CMDTYPE_ICON_UNIT_OR_AREA: case CMDTYPE_ICON_UNIT_FEATURE_OR_AREA: case CMDTYPE_ICON_AREA:{ float maxRadius=100000; if(commands[cc].params.size()==1) maxRadius=atof(commands[cc].params[0].c_str()); if(mouse->buttons[button].movement>4){ float dist=ground->LineGroundCol(mouse->buttons[button].camPos,mouse->buttons[button].camPos+mouse->buttons[button].dir*9000); if(dist<0){ break; } float3 pos=mouse->buttons[button].camPos+mouse->buttons[button].dir*dist; dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist<0){ break; } float3 pos2=camera->pos+mouse->dir*dist; DrawArea(pos,min(maxRadius,pos.distance2D(pos2))); } break;} default: break; } } } //draw buildings we are about to build if(inCommand>=0 && inCommand<commands.size() && commands[guihandler->inCommand].type==CMDTYPE_ICON_BUILDING){ float dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist>0){ string s=commands[guihandler->inCommand].name; UnitDef *unitdef = unitDefHandler->GetUnitByName(s); if(unitdef){ float3 pos=camera->pos+mouse->dir*dist; std::vector<float3> buildPos; if(keys[SDLK_LSHIFT] && mouse->buttons[SDL_BUTTON_LEFT].pressed){ float dist=ground->LineGroundCol(mouse->buttons[SDL_BUTTON_LEFT].camPos,mouse->buttons[SDL_BUTTON_LEFT].camPos+mouse->buttons[SDL_BUTTON_LEFT].dir*9000); float3 pos2=mouse->buttons[SDL_BUTTON_LEFT].camPos+mouse->buttons[SDL_BUTTON_LEFT].dir*dist; buildPos=GetBuildPos(pos2,pos,unitdef); } else { buildPos=GetBuildPos(pos,pos,unitdef); } for(std::vector<float3>::iterator bpi=buildPos.begin();bpi!=buildPos.end();++bpi){ float3 pos=*bpi; if(uh->ShowUnitBuildSquare(pos,unitdef)) glColor4f(0.7,1,1,0.4); else glColor4f(1,0.5,0.5,0.4); unitDrawer->SetupForGhostDrawing (); S3DOModel* model=modelParser->Load3DO((unitdef->model.modelpath).c_str() ,1, gu->myTeam); glPushMatrix(); glTranslatef3(pos); //glCallList(model->displist); model->DrawStatic(); unitDrawer->CleanUpGhostDrawing(); glPopMatrix(); if(unitdef->weapons.size()>0){ //draw range glDisable(GL_TEXTURE_2D); glColor4f(1,0.3,0.3,0.7); glBegin(GL_LINE_STRIP); for(int a=0;a<=40;++a){ float3 wpos(cos(a*2*PI/40)*unitdef->weapons[0].def->range,0,sin(a*2*PI/40)*unitdef->weapons[0].def->range); wpos+=pos; float dh=ground->GetHeight(wpos.x,wpos.z)-pos.y; float modRange=unitdef->weapons[0].def->range-dh*unitdef->weapons[0].def->heightmod; wpos=float3(cos(a*2*PI/40)*(modRange),0,sin(a*2*PI/40)*(modRange)); wpos+=pos; wpos.y=ground->GetHeight(wpos.x,wpos.z)+8; glVertexf3(wpos); } glEnd(); } if(unitdef->extractRange>0){ //draw range glDisable(GL_TEXTURE_2D); glColor4f(1,0.3,0.3,0.7); glBegin(GL_LINE_STRIP); for(int a=0;a<=40;++a){ float3 wpos(cos(a*2*PI/40)*unitdef->extractRange,0,sin(a*2*PI/40)*unitdef->extractRange); wpos+=pos; wpos.y=ground->GetHeight(wpos.x,wpos.z)+8; glVertexf3(wpos); } glEnd(); } } } } } if(keys[SDLK_LSHIFT]){ CUnit* unit=0; float dist2=helper->GuiTraceRay(camera->pos,mouse->dir,9000,unit,20,false); if(unit && ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectating)){ //draw weapon range if(unit->maxRange>0){ glDisable(GL_TEXTURE_2D); glColor4f(1,0.3,0.3,0.7); glBegin(GL_LINE_STRIP); float h=unit->pos.y; for(int a=0;a<=40;++a){ float3 pos(cos(a*2*PI/40)*unit->maxRange,0,sin(a*2*PI/40)*unit->maxRange); pos+=unit->pos; float dh=ground->GetHeight(pos.x,pos.z)-h; pos=float3(cos(a*2*PI/40)*(unit->maxRange-dh*unit->weapons.front()->heightMod),0,sin(a*2*PI/40)*(unit->maxRange-dh*unit->weapons.front()->heightMod)); pos+=unit->pos; pos.y=ground->GetHeight(pos.x,pos.z)+8; glVertexf3(pos); } glEnd(); } if(unit->unitDef->decloakDistance>0){ //draw decloak distance glDisable(GL_TEXTURE_2D); glColor4f(0.3,0.3,1,0.7); glBegin(GL_LINE_STRIP); for(int a=0;a<=40;++a){ float3 pos(cos(a*2*PI/40)*unit->unitDef->decloakDistance,0,sin(a*2*PI/40)*unit->unitDef->decloakDistance); pos+=unit->pos; pos.y=ground->GetHeight(pos.x,pos.z)+8; glVertexf3(pos); } glEnd(); } if(unit->unitDef->kamikazeDist>0){ //draw self destruct and damage distance glDisable(GL_TEXTURE_2D); glColor4f(0.8,0.8,0.1,0.7); glBegin(GL_LINE_STRIP); for(int a=0;a<=40;++a){ float3 pos(cos(a*2*PI/40)*unit->unitDef->kamikazeDist,0,sin(a*2*PI/40)*unit->unitDef->kamikazeDist); pos+=unit->pos; pos.y=ground->GetHeight(pos.x,pos.z)+8; glVertexf3(pos); } glEnd(); if(!unit->unitDef->selfDExplosion.empty()){ glColor4f(0.8,0.1,0.1,0.7); WeaponDef* wd=weaponDefHandler->GetWeapon(unit->unitDef->selfDExplosion); glBegin(GL_LINE_STRIP); for(int a=0;a<=40;++a){ float3 pos(cos(a*2*PI/40)*wd->areaOfEffect,0,sin(a*2*PI/40)*wd->areaOfEffect); pos+=unit->pos; pos.y=ground->GetHeight(pos.x,pos.z)+8; glVertexf3(pos); } glEnd(); } } } } //draw range circles if attack orders are imminent int defcmd=GetDefaultCommand(mouse->lastx,mouse->lasty); if((inCommand>0 && inCommand<commands.size() && commands[inCommand].id==CMD_ATTACK) || (inCommand==-1 && defcmd>0 && commands[defcmd].id==CMD_ATTACK)){ for(std::set<CUnit*>::iterator si=selectedUnits.selectedUnits.begin();si!=selectedUnits.selectedUnits.end();++si){ CUnit* unit=*si; if(unit->maxRange>0 && ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectating)){ glDisable(GL_TEXTURE_2D); glColor4f(1,0.3,0.3,0.7); glBegin(GL_LINE_STRIP); float h=unit->pos.y; for(int a=0;a<=40;++a){ float3 pos(cos(a*2*PI/40)*unit->maxRange,0,sin(a*2*PI/40)*unit->maxRange); pos+=unit->pos; float dh=ground->GetHeight(pos.x,pos.z)-h; pos=float3(cos(a*2*PI/40)*(unit->maxRange-dh*unit->weapons.front()->heightMod),0,sin(a*2*PI/40)*(unit->maxRange-dh*unit->weapons.front()->heightMod)); pos+=unit->pos; pos.y=ground->GetHeight(pos.x,pos.z)+8; glVertexf3(pos); } glEnd(); } } } }
void CGuiHandler::DrawButtons() { glDisable(GL_DEPTH_TEST); if(needShift && !keys[SDLK_LSHIFT]){ if(showingMetal){ showingMetal=false; groundDrawer->SetExtraTexture(0,0,false); } inCommand=-1; needShift=false; } if (selectedUnits.CommandsChanged()) {LayoutIcons();fadein=100;} else if (fadein>0) fadein-=5; int mouseIcon=IconAtPos(mouse->lastx,mouse->lasty); GLfloat x1,x2,y1,y2; GLfloat scalef; // Rita "container"ruta if (total_active_icons > 0) { glDisable(GL_TEXTURE_2D); glColor4f(0.2f,0.2f,0.2f,0.4f); glBegin(GL_QUADS); GLfloat fx = 0;//-.2f*(1-fadein/100.0)+.2f; glVertex2f(buttonBox.x1-fx, buttonBox.y1); glVertex2f(buttonBox.x1-fx, buttonBox.y2); glVertex2f(buttonBox.x2-fx, buttonBox.y2); glVertex2f(buttonBox.x2-fx, buttonBox.y1); glVertex2f(buttonBox.x1-fx, buttonBox.y1); glEnd(); } // För varje knapp (rita den) int buttonStart=max(0,(int)(min(activePage*NUMICOPAGE,(int)total_active_icons))); int buttonEnd=max(0,int(min((activePage+1)*NUMICOPAGE,(int)total_active_icons))); for(unsigned int nr=buttonStart;nr<buttonEnd;nr++) { if(commands[nr].onlyKey) continue; x1 = curricon[nr].x1; y1 = curricon[nr].y1; x2 = curricon[nr].x2; y2 = curricon[nr].y2; glDisable(GL_TEXTURE_2D); if(mouseIcon==nr || nr ==inCommand){ glBegin(GL_QUADS); if(mouse->buttons[SDL_BUTTON_LEFT].pressed) glColor4f(0.5,0,0,0.2f); else if (nr == inCommand) glColor4f(0.5,0,0,0.8f); else glColor4f(0,0,0.5,0.2f); glVertex2f(x1,y1); glVertex2f(x2,y1); glVertex2f(x2,y2); glVertex2f(x1,y2); glVertex2f(x1,y1); glEnd(); } //else //glBegin(GL_LINE_STRIP); // "markerad" (lägg mörkt fält över) glEnable(GL_TEXTURE_2D); glColor4f(1,1,1,0.8f); if(UnitDef *ud = unitDefHandler->GetUnitByName(commands[nr].name)) { // unitikon glBindTexture(GL_TEXTURE_2D, unitDefHandler->GetUnitImage(ud)); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(x1,y1); glTexCoord2f(1,0); glVertex2f(x2,y1); glTexCoord2f(1,1); glVertex2f(x2,y2); glTexCoord2f(0,1); glVertex2f(x1,y2); glEnd(); if(!commands[nr].params.empty()){ //skriv texten i första param ovanpå string toPrint=commands[nr].params[0]; if (toPrint.length() > 6) scalef = toPrint.length()/4.0; else scalef = 6.0/4.0; glTranslatef(x1+.004f,y1-.039f,0.0f); glScalef(0.02f/scalef,0.03f/scalef,0.1f); font->glPrint("%s",toPrint.c_str()); } } else { if (iconmap.find(curricon[nr].id) != iconmap.end() && iconmap[curricon[nr].id].has_bitmap) { // Bitmapikon glBindTexture(GL_TEXTURE_2D, iconmap[curricon[nr].id].texture); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(x1,y1); glTexCoord2f(1,0); glVertex2f(x2,y1); glTexCoord2f(1,1); glVertex2f(x2,y2); glTexCoord2f(0,1); glVertex2f(x1,y2); glEnd(); } // skriv text string toPrint=commands[nr].name; if(commands[nr].type==CMDTYPE_ICON_MODE && (atoi(commands[nr].params[0].c_str())+1)<commands[nr].params.size()){ toPrint=commands[nr].params[atoi(commands[nr].params[0].c_str())+1]; } if (toPrint.length() > 6) scalef = toPrint.length()/4.0; else scalef = 6.0/4.0; glTranslatef(x1+.004f,y1-.039f,0.0f); glScalef(0.02f/scalef,0.03f/scalef,0.1f); font->glPrint("%s",toPrint.c_str()); } glLoadIdentity(); } if (total_active_icons > 0) { glColor4f(1,1,1,0.6f); if(selectedUnits.selectedGroup!=-1){ font->glPrintAt(0.02,0.13,0.6,"Group %i selected ",selectedUnits.selectedGroup); } else { font->glPrintAt(0.02,0.13,0.6,"Selected units %i",selectedUnits.selectedUnits.size()); } } if(GetReceiverAt(mouse->lastx,mouse->lasty)){ mouse->cursorText=""; } else { if(inCommand>=0 && inCommand<commands.size()){ mouse->cursorText=commands[guihandler->inCommand].name; } else { int defcmd; if(mouse->buttons[SDL_BUTTON_RIGHT].pressed && mouse->activeReceiver==this) defcmd=defaultCmdMemory; else defcmd=GetDefaultCommand(mouse->lastx,mouse->lasty); if(defcmd>=0 && defcmd<commands.size()) mouse->cursorText=commands[defcmd].name; else mouse->cursorText=""; } } }
Command CGuiHandler::GetCommand(int mousex, int mousey, int buttonHint, bool preview) { Command defaultRet; defaultRet.id=CMD_STOP; int button; if(buttonHint>=SDL_BUTTON_LEFT) button=buttonHint; else if(inCommand!=-1) button=SDL_BUTTON_LEFT; else if(mouse->buttons[SDL_BUTTON_RIGHT].pressed) button=SDL_BUTTON_RIGHT; else return defaultRet; int tempInCommand=inCommand; if (button == SDL_BUTTON_RIGHT && preview) { // right click -> default cmd, in preview we might not have default cmd memory set if(mouse->buttons[SDL_BUTTON_RIGHT].pressed) tempInCommand=defaultCmdMemory; else tempInCommand=GetDefaultCommand(mousex,mousey); } if(tempInCommand>=0 && tempInCommand<commands.size()){ switch(commands[tempInCommand].type){ case CMDTYPE_ICON:{ Command c; c.id=commands[tempInCommand].id; CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); if(button==SDL_BUTTON_LEFT && !preview) info->AddLine("CMDTYPE_ICON left button press in incommand test? This shouldnt happen"); return c;} case CMDTYPE_ICON_MAP:{ float dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist<0){ return defaultRet; } float3 pos=camera->pos+mouse->dir*dist; Command c; c.id=commands[tempInCommand].id; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} case CMDTYPE_ICON_BUILDING:{ float dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist<0){ return defaultRet; } string s=commands[guihandler->inCommand].name; UnitDef *unitdef = unitDefHandler->GetUnitByName(s); if(!unitdef){ return defaultRet; } float3 pos=camera->pos+mouse->dir*dist; std::vector<float3> buildPos; if(keys[SDLK_LSHIFT] && button==SDL_BUTTON_LEFT){ float dist=ground->LineGroundCol(mouse->buttons[SDL_BUTTON_LEFT].camPos,mouse->buttons[SDL_BUTTON_LEFT].camPos+mouse->buttons[SDL_BUTTON_LEFT].dir*9000); float3 pos2=mouse->buttons[SDL_BUTTON_LEFT].camPos+mouse->buttons[SDL_BUTTON_LEFT].dir*dist; buildPos=GetBuildPos(pos2,pos,unitdef); } else { buildPos=GetBuildPos(pos,pos,unitdef); } int a=0; //limit the number of max commands possible to send to avoid overflowing the network buffer for(std::vector<float3>::iterator bpi=buildPos.begin();bpi!=--buildPos.end() && a<200;++bpi){ ++a; float3 pos=*bpi; Command c; c.id=commands[tempInCommand].id; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); if(!preview) selectedUnits.GiveCommand(c); } pos=*(--buildPos.end()); Command c; c.id=commands[tempInCommand].id; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} case CMDTYPE_ICON_UNIT: { CUnit* unit=0; Command c; c.id=commands[tempInCommand].id; float dist2=helper->GuiTraceRay(camera->pos,mouse->dir,9000,unit,20,true); if (!unit){ return defaultRet; } c.params.push_back(unit->id); CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} case CMDTYPE_ICON_UNIT_OR_MAP: { Command c; c.id=commands[tempInCommand].id; CUnit* unit=0; float dist2=helper->GuiTraceRay(camera->pos,mouse->dir,9000,unit,20,true); if(dist2>8900){ return defaultRet; } if (unit!=0) { // clicked on unit c.params.push_back(unit->id); } else { // clicked in map float3 pos=camera->pos+mouse->dir*dist2; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); } CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} case CMDTYPE_ICON_FRONT:{ Command c; float dist=ground->LineGroundCol(mouse->buttons[button].camPos,mouse->buttons[button].camPos+mouse->buttons[button].dir*9000); if(dist<0){ return defaultRet; } float3 pos=mouse->buttons[button].camPos+mouse->buttons[button].dir*dist; c.id=commands[tempInCommand].id; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); if(mouse->buttons[button].movement>30){ //only create the front if the mouse has moved enough dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist<0){ return defaultRet; } float3 pos2=camera->pos+mouse->dir*dist; if(!commands[tempInCommand].params.empty() && pos.distance2D(pos2)>atoi(commands[tempInCommand].params[0].c_str())){ float3 dif=pos2-pos; dif.Normalize(); pos2=pos+dif*atoi(commands[tempInCommand].params[0].c_str()); } c.params.push_back(pos2.x); c.params.push_back(pos2.y); c.params.push_back(pos2.z); } CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} case CMDTYPE_ICON_UNIT_OR_AREA: case CMDTYPE_ICON_UNIT_FEATURE_OR_AREA: case CMDTYPE_ICON_AREA:{ float maxRadius=100000; if(commands[tempInCommand].params.size()==1) maxRadius=atof(commands[tempInCommand].params[0].c_str()); Command c; c.id=commands[tempInCommand].id; if(mouse->buttons[button].movement<4){ CUnit* unit=0; CFeature* feature=0; float dist2=helper->GuiTraceRay(camera->pos,mouse->dir,9000,unit,20,true); float dist3=helper->GuiTraceRayFeature(camera->pos,mouse->dir,9000,feature); if(dist2>8900 && (commands[tempInCommand].type!=CMDTYPE_ICON_UNIT_FEATURE_OR_AREA || dist3>8900)){ return defaultRet; } if (feature!=0 && dist3<dist2 && commands[tempInCommand].type==CMDTYPE_ICON_UNIT_FEATURE_OR_AREA) { // clicked on feature c.params.push_back(MAX_UNITS+feature->id); } else if (unit!=0 && commands[tempInCommand].type!=CMDTYPE_ICON_AREA) { // clicked on unit c.params.push_back(unit->id); } else { // clicked in map float3 pos=camera->pos+mouse->dir*dist2; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); c.params.push_back(0);//zero radius } } else { //created area float dist=ground->LineGroundCol(mouse->buttons[button].camPos,mouse->buttons[button].camPos+mouse->buttons[button].dir*9000); if(dist<0){ return defaultRet; } float3 pos=mouse->buttons[button].camPos+mouse->buttons[button].dir*dist; c.params.push_back(pos.x); c.params.push_back(pos.y); c.params.push_back(pos.z); dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*9000); if(dist<0){ return defaultRet; } float3 pos2=camera->pos+mouse->dir*dist; c.params.push_back(min(maxRadius,pos.distance2D(pos2))); } CreateOptions(c,(button==SDL_BUTTON_LEFT?0:1)); return c;} default: return defaultRet; } } else { if(!preview) inCommand=-1; } return defaultRet; }