Exemple #1
0
void SceneTree::SyncSelectionFromTree()
{
	SceneEditor2* curScene = treeModel->GetScene();
	if(NULL != curScene)
	{
		QSet<DAVA::Entity*> treeSelectedEntities;

		// select items in scene
		QModelIndexList indexList = selectionModel()->selection().indexes();
		for (int i = 0; i < indexList.size(); ++i)
		{
			DAVA::Entity *entity = treeModel->GetEntity(indexList[i]);

			treeSelectedEntities.insert(entity);
			curScene->selectionSystem->AddSelection(entity);
		}

		// remove from selection system all entities that are not selected in tree
		EntityGroup selGroup = *(curScene->selectionSystem->GetSelection());
		for(size_t i = 0; i < selGroup.Size(); ++i)
		{
			if(!treeSelectedEntities.contains(selGroup.GetEntity(i)))
			{
				curScene->selectionSystem->RemSelection(selGroup.GetEntity(i));
			}
		}
	}
}
Exemple #2
0
		EntityGroup* LoadModel(unsigned int index, Entity* parent = 0) const {
			if(index >= mModelList.Size()) return NULL;
			Model* model = &mModelList[index];
			if(model->mPartList.Size() == 0) return NULL;

			EntityGroup* group = new EntityGroup();
			group->SetID(index);
			bool zms = false;
			for(unsigned int i = 0; i < model->mPartList.Size(); ++i){
				T* entity = new T();

				ModelPart* part = &model->mPartList[i];
				Mesh* mesh = &mMeshList[part->mMesh];
				Material* material = &mMaterialList[part->mMaterial];

				entity->SetMesh(mesh->mPath);
				entity->SetMaterial(*material);
				entity->SetTransform(part->mTransform);

				group->AddChild(entity);

				if(part->mBoneID == -1 && mBoneID != -1) part->mBoneID = (short)mBoneID;
				if(part->mDummyID == -1 && mDummyID != -1) part->mDummyID = (short)mDummyID;

				if(parent){
					SkeletalEntity* skel = (SkeletalEntity*)parent;
					if(part->mBoneID != -1)
						skel->BindEntityToBone(entity, part->mBoneID);
					else if(part->mDummyID != -1)
						skel->BindEntityToDummy(entity, part->mDummyID);
				}
			}

			return group;
		}
Exemple #3
0
TEST(entity_group, can_set_interest_in_component_types) 
{
    ComponentSet components;
    components.add<Component1>();
    components.add<Component2>();
    EntityGroup sut;

    sut.set_interest<Component1, Component2>();
    
    ASSERT_THAT(sut.is_interested(components), Eq(true));
}
EntityGroup SceneSelectionSystem::GetSelecetableFromCollision(const EntityGroup *collisionEntities)
{
	EntityGroup ret;

	if(NULL != collisionEntities)
	{
		for(size_t i = 0; i < collisionEntities->Size(); ++i)
		{
			DAVA::Entity *entity = collisionEntities->GetEntity(i);
			EntityGroupItem item = GetSelectableEntity(entity);

			ret.Add(item);
		}
	}

	return ret;
}
Exemple #5
0
//------------------------------------------------------------------------------
//!
int groupVM( VMState* vm )
{
   WorldContext* context = getContext(vm);
   EntityGroup* group = new EntityGroup();
   context->_world->addGroup( group );

   // Load parameters.
   Reff ref = Reff::identity();
   VM::get( vm, -1, "position", ref.position() );
   Quatf q = Quatf::identity();
   VM::get( vm, -1, "orientation", q );
   ref.orientation( q );

   // Brain.

   // User attributes.

   // Objects (read and transform).
   if( VM::get( vm, -1, "object" ) )
   {
      for( uint i = 1; VM::geti( vm, -1, i ); ++i )
      {
         Entity* e = (Entity*)VM::toPtr( vm, -1 );
         group->addEntity( e );
         e->referential( ref * e->referential() );
         VM::pop( vm, 1 );
      }

      VM::pop( vm, 1 );
   }

   // Groups.
   for( uint i = 1; VM::geti( vm, -1, i ); ++i )
   {
      group->addGroup( (EntityGroup*)VM::toPtr( vm, -1 ) );
      VM::pop( vm, 1 );
   }

   VM::push( vm, group );
   return 1;
}
bool EntityGroup::operator==( const EntityGroup &ss ) const
{
	bool ret = false;

	if(entities.size() == ss.entities.size())
	{
		ret = true;
		for(size_t i = 0; i < entities.size(); ++i)
		{
			if(!ss.HasEntity(entities[i].entity))
			{
				ret = false;
				break;
			}
		}
	}

	return ret = true;
}
Exemple #7
0
int main(int argc, char **argv)
{
  //-------------------------------------------------------------------------------------      
  // Variable declarations
  //-------------------------------------------------------------------------------------      
  bool quit = false;
  SDL_Event event;
  
  Timer fpsTimer;
  int countedFrames = 0;
  Timer capTimer;
  
  int cameraoffset_x = 0;
  int cameraoffset_y = 0;
  const int SCROLL_SPEED = 10;
  
  int mouse_x = 0;
  int mouse_y = 0;
  int screen_width = SCREEN_WIDTH;
  int screen_height = SCREEN_HEIGHT;
  
  bool scroll_active = true;
  bool fullscreen = false;
  bool use_custom_cursor = USE_CUSTOM_CURSOR;
  bool debug_mode = false;
  
  float zoom = 1.0;

  //-------------------------------------------------------------------------------------      
  //Initialization:
  //-------------------------------------------------------------------------------------      
  //initialise
  if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
      logSDLError(std::cout, "SDL_Init");
      return 1;
    }
  if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
    {
      logSDLError(std::cout, "IMG_Init");
      SDL_Quit();
      return 1;
    }
  if (TTF_Init() != 0)
    {
      logSDLError(std::cout, "TTF_Init");
      SDL_Quit();
      return 1;
    }
  
  SDL_Window *window = nullptr;
  window = SDL_CreateWindow("Civ",
			    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
			    SCREEN_WIDTH, SCREEN_HEIGHT,
			    SDL_WINDOW_SHOWN);
  if (window == nullptr)
    {
      logSDLError(std::cout, "CreateWindow");
      return 1;
    }
  
  SDL_Renderer *renderer = nullptr;
  if (VSYNC_ACTIVE)
    {
      renderer = SDL_CreateRenderer(window, -1,
				    SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    }
  else
    {
      renderer = SDL_CreateRenderer(window, -1,
				    SDL_RENDERER_ACCELERATED);
    }
  if (renderer == nullptr)
    {
      logSDLError(std::cout, "CreateRenderer");
      return 1;
    }
  
  if (use_custom_cursor)
    {
      SDL_ShowCursor(false);
    }
  
  //-------------------------------------------------------------------------------------      
  // Create objects:
  //-------------------------------------------------------------------------------------      
  SDL_Texture *texture_cursor  = loadTexture("res/images/cursor.png", renderer, false);
  
  Map map(renderer, window);
  
  Sprite *sprite_selection = new Sprite("res/images/iso/selection.png", 2, 2, renderer, window, TILE_SIZE);

  //make an entity and move it
  std::cout << "Main: INFO: Starting To Make Objects" << std::endl;
  Unit * unit_you = new Unit(0., 0., "You");
  Unit * unit1 = new Unit(4.5, 4.5, "Unit1");
  EntityAction * selected_entity = unit_you;
  //Movement * move1 = new Movement(10., 8.);
  //unit_you->appendAction(move1);
  Movement * move2 = new Movement(0., 0.);
  unit1->appendAction(move2);
  //Attack * attack = new Attack(unit1);
  //unit_you->appendAction(attack);

  //make a group to hold units
  EntityGroup * Units = new EntityGroup(renderer, window);
  Units->addEntity(unit_you);
  Units->addEntity(unit1);

  //set the image filename of the group
  std::cout << "Main: INFO: Setting the Units image file" << std::endl;
  Units->setImage("res/images/units/villager.png");

  //make a text maker
  TextMaker * TextHandler = new TextMaker("res/fonts/KaushanScript-Regular.otf", renderer, window);
  //TextMaker * TextHandler = new TextMaker("res/fonts/Pacifico.ttf", renderer, window);
  //TextMaker * TextHandler = new TextMaker("res/fonts/FFF_Tusj.ttf", renderer, window);
  //TextMaker * TextHandler = new TextMaker("res/fonts/sample.ttf", renderer, window);
  
  //make a temp message
  //TextLine * message = new TextLine(30. + 122., 125., 500., 50., "HELLO WORLD RTS 3", TextHandler);
  //message->setActive(true);
  
  //make a pop_menu
  PopMenu * pop_menu = new PopMenu(renderer, window, TextHandler);
  //pop_menu->loadImage("res/images/menu/menu.png");

  //make a menu group
  MenuGroup * Menus = new MenuGroup();
  Menus->addMenu(pop_menu);

  //make a group to hold resources
  EntityGroup * Resources = new EntityGroup(renderer, window);
  placeResources(Resources, 1, MAP_SIZE/2);

  //make a resource
  //Resource * res_food = new Resource(0., 0., 1, 100.);
  //Resources->addEntity(res_food);
  //res_food->setImage();

  //make a food item and give it to the unit
  Item* food1 = new Item(1., 5.);
  Item* wood1 = new Item(2., 1.);
  unit_you->addItem(food1);
  unit_you->addItem(wood1);
  unit_you->printInventory();
  Item* food2 = new Item(1., 5.);
  unit1->addItem(food2);

  //make buildings
  //make a group to hold buildings
  EntityGroup * Buildings = new EntityGroup(renderer, window);
  Building * hut = new Building(3., 3., 0);
  Buildings->addEntity(hut);
  
  //make a menu
  InfoMenu * info_menu = new InfoMenu(renderer, window, TextHandler);
  info_menu->setActive(false);
  Menus->addMenu(info_menu);

  //TEMP
  /*  Menu * sel_menu = new Menu(0, 0, 500, 600, renderer, window, TextHandler);;
  SelectionMenu * sl = new SelectionMenu(0, 0, 1, 1, sel_menu, Units );
  sel_menu->addSelectionMenu( sl );
  Menus->addMenu( sel_menu );*/
  

  //Start timers:
  fpsTimer.start();

  //-------------------------------------------------------------------------------------      
  // Main loop:
  //-------------------------------------------------------------------------------------      
  while (!quit)
    {
      if (!VSYNC_ACTIVE)
	{
	  capTimer.start();
	}
      
      //-------------------------------------------------------------------------------------      
      // Update Entity states
      //-------------------------------------------------------------------------------------      
      Units->update();
      Resources->update();
      Buildings->update();

      //-------------------------------------------------------------------------------------      
      // Entity operations
      //-------------------------------------------------------------------------------------      
      Units->doActions();
      Buildings->doActions();
      
      //-------------------------------------------------------------------------------------      
      // Screen
      //-------------------------------------------------------------------------------------      
      
      //Mouse position:
      SDL_GetMouseState(&mouse_x, &mouse_y);
      
      //Resolution:
      SDL_GetWindowSize(window, &screen_width, &screen_height);
      
      if (scroll_active)
	{
	  if (mouse_x < 20 ) // cameraoffset_x > map.getMinPixelX(zoom))
	    {
	      cameraoffset_x -= SCROLL_SPEED;
	    }
	  if (mouse_x > screen_width-20 ) // cameraoffset_x < map.getMaxPixelX(zoom)-screen_width+TILE_SIZE*zoom)
	    {
	      cameraoffset_x += SCROLL_SPEED;
	    }
	  if (mouse_y > screen_height-20 ) // cameraoffset_y < map.getMaxPixelY(zoom)-screen_height+TILE_SIZE*zoom)
	    {
	      cameraoffset_y += SCROLL_SPEED;
	    }
	  if (mouse_y < 20 ) // cameraoffset_y > map.getMinPixelY(zoom))
	    {
	      cameraoffset_y -= SCROLL_SPEED;
	    }
	}
      
      //-------------------------------------------------------------------------------------      
      //Events:
      //-------------------------------------------------------------------------------------      
      while (SDL_PollEvent(&event))
	{
	  if (event.type == SDL_QUIT)
	    {
	      quit = true;
	    } 
	  else if (event.type == SDL_KEYDOWN)
	    {
	      if (event.key.keysym.sym == SDLK_ESCAPE)
		{
		  quit = true;
		}
	      else if (event.key.keysym.sym == SDLK_RIGHT)
		{
		  cameraoffset_x += SCROLL_SPEED*2;
		}
	      else if (event.key.keysym.sym == SDLK_LEFT)
		{
		  cameraoffset_x -= SCROLL_SPEED*2;
		}
	      else if (event.key.keysym.sym == SDLK_UP)
		{
		  cameraoffset_y -= SCROLL_SPEED*2;
		}
	      else if (event.key.keysym.sym == SDLK_DOWN)
		{
		  cameraoffset_y += SCROLL_SPEED*2;
		}
	      else if (event.key.keysym.sym == SDLK_f)
		{
		  fullscreen = not(fullscreen);
		  
		  if (fullscreen)
		    {
		      SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
		    }
		  else
		    {
		      SDL_SetWindowFullscreen(window, 0);
		    }
		}
	      else if (event.key.keysym.sym == SDLK_g)
		{
		  map.setDrawGrid(not(map.getDrawGrid()));
		}
	      else if (event.key.keysym.sym == SDLK_c)
		{
		  use_custom_cursor = not(use_custom_cursor);
		  SDL_ShowCursor(not(use_custom_cursor));
		}
	      else if (event.key.keysym.sym == SDLK_d)
		{
		  debug_mode = not(debug_mode);
		}
	      else if (event.key.keysym.sym == SDLK_s)
		{
		  scroll_active = not(scroll_active);
		}
	      else if (event.key.keysym.sym == SDLK_t)
		{
		  /*if ( sel_menu->isActive() == false )
		    sel_menu->setActive(true);
		  std::cout << "Main: Temporary button: There is nothing here" << std::endl;
		  //temporary testing
		  std::cout << "INFO: main: sel_menu has buttons: " << sel_menu->getSizeButtons() << std::endl; 
		  std::cout << "INFO: main: sel_menu has text boxes: " << sel_menu->getSizeTextBoxes() << std::endl; 
		  std::cout << "INFO: main: sel_menu has selection menus: " << sel_menu->getSizeSelectionMenus() << std::endl;
		  }*/
		}
	    }
	  else if (event.type == SDL_MOUSEBUTTONDOWN)
	    {
	      if (event.button.button == SDL_BUTTON_LEFT)
		{
		  //check if mouse collides with a button
		  Menu * menu_called = Menus->collide(event.button.x, event.button.y);
		  if ( menu_called != nullptr )
		    {
		      //true if menu is to be cleared (should probably move this into the child classes of menu
		      // that actually use it, like pop menu)
		      if ( menu_called->outcome() == true)
			{
			  menu_called->setActive(false);
			  menu_called->clear();
			}
		    }
		  //remove menu if not clicked and on
		  else
		    {
		      if (Menus->isActive())
			{
			  Menus->setAllNotActive();
			}
		    }
		  
		  //takes screen pos and gives tile coords
		  float pos_x = getIsoX(event.button.x, event.button.y, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		  float pos_y = getIsoY(event.button.x, event.button.y, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		  
		  if ((pos_x >= 0) and
		      (pos_y >= 0) and
		      (pos_x < map.mWidth) and
		      (pos_y < map.mHeight))
		    {
		      std::cout << "Click at: X=" << pos_x << " Y=" << pos_y << std::endl;
		    }
		}
	      
	      else if (event.button.button == SDL_BUTTON_RIGHT)
		{
		  //pop_menu = Menus->getPopMenu();
		  if (pop_menu != nullptr)
		    {
		      float pos_x = event.button.x;
		      float pos_y = event.button.y;
		      Entity * target_entity;
		      //check for click on unit
		      target_entity = Units->collide(pos_x, pos_y, cameraoffset_x, cameraoffset_y, zoom);
		      if (target_entity == nullptr)
			{
			  //check for click on building
			  target_entity = Buildings->collide(pos_x, pos_y, cameraoffset_x, cameraoffset_y, zoom);
			  if (target_entity == nullptr)
			    {
			      //check for click on Resource
			      target_entity = Resources->collide(pos_x, pos_y, cameraoffset_x, cameraoffset_y, zoom);
			    }
			}
		      if (target_entity != nullptr )
			{
			  std::cout << "You clicked in the region" << std::endl;
			}
		      else
			{
			  std::cout << "Clicked Nothing" << std::endl;
			}
		      
		      //make the pop menu for the available actions
		      //only make the pop_menu if no other menu is active
		      if ( !pop_menu->isActive() && !Menus->isActive() )
			{  
			  pop_menu->setXYPositions(event.button.x, event.button.y, cameraoffset_x, cameraoffset_y, zoom);
			  if (target_entity != nullptr )
			    {
			      makeActionMenu(pop_menu, selected_entity, target_entity, info_menu);
			    }
			  else
			    {
			      makeActionMenu(pop_menu, selected_entity);
			    }
			}
		      else
			{
			  pop_menu->clear();
			}
		    }
		}
	    }
	  else if (event.type == SDL_MOUSEMOTION)
	    {
	      //takes screen pos and gives tile coords
	      int pos_x = getIsoCoordinateX(event.button.x, event.button.y, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
	      int pos_y = getIsoCoordinateY(event.button.x, event.button.y, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
	      
	      sprite_selection->setPosX(pos_x);
	      sprite_selection->setPosY(pos_y);
	    }
	  else if (event.type == SDL_MOUSEWHEEL)
	    {
	      if (event.wheel.y > 0 && scroll_active)
		{
		  if (zoom < 2.0)
		    {
		      //takes screen pos and gives tile coords
		      //int iso_x = getIsoCoordinateX(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom);
		      //int iso_y = getIsoCoordinateY(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom);
		      int iso_x = getIsoX(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		      int iso_y = getIsoY(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		      
		      //Zooming:
		      zoom += 0.25;
		      
		      //Centering:
		      //takes tile coords and gives pixel coords
		      float pixel_x = getPixelX(iso_x, iso_y, 0,0, zoom, TILE_SIZE);
		      float pixel_y = getPixelY(iso_x, iso_y, 0,0, zoom, TILE_SIZE);
		      cameraoffset_x = (pixel_x + (TILE_SIZE*zoom)*0.5) - screen_width*0.5;
		      cameraoffset_y = (pixel_y + (TILE_SIZE*zoom)*0.75) - screen_height*0.5;
		    }
		}
	      else if (event.wheel.y < 0 && scroll_active)
		{
		  if (zoom > 0.26)
		    {
		      //takes screen coords and gives tile coords
		      //int iso_x = getIsoCoordinateX(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom);
		      //int iso_y = getIsoCoordinateY(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom);
		      int iso_x = getIsoX(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		      int iso_y = getIsoY(screen_width/2, screen_height/2, cameraoffset_x, cameraoffset_y, zoom, TILE_SIZE);
		      
		      //Zooming:
		      zoom -= 0.25;
		      
		      //Centering:
		      //takes tile coords and gives pixel coords
		      float pixel_x = getPixelX(iso_x, iso_y, 0,0, zoom, TILE_SIZE);
		      float pixel_y = getPixelY(iso_x, iso_y, 0,0, zoom, TILE_SIZE);
		      cameraoffset_x = (pixel_x + (TILE_SIZE*zoom)*0.5) - screen_width*0.5;
		      cameraoffset_y = (pixel_y + (TILE_SIZE*zoom)*0.75) - screen_height*0.5;
		    }
		}
	    }
	}

      //-------------------------------------------------------------------------------------      
      // Rendering: The Order Here Matters
      //-------------------------------------------------------------------------------------
      SDL_RenderClear(renderer);

      map.render(cameraoffset_x, cameraoffset_y, zoom);
      
      sprite_selection->render(cameraoffset_x, cameraoffset_y, zoom);
      
      //vill->render(cameraoffset_x, cameraoffset_y, zoom);
      
      //std::cout << "INFO: Before rendering EntityGroup" << std::endl;
      Units->render(cameraoffset_x, cameraoffset_y, zoom);
      //std::cout << "INFO: After rendering EntityGroup" << std::endl;

      //render resources
      Resources->render(cameraoffset_x, cameraoffset_y, zoom);

      //render resources
      Buildings->render(cameraoffset_x, cameraoffset_y, zoom);

      //render menu
      Menus->render(cameraoffset_x, cameraoffset_y, zoom);

      //render Temp text
      //message->render();

      if (use_custom_cursor)
	{
	  renderTexture(texture_cursor, renderer, mouse_x, mouse_y);
	}
      
      SDL_RenderPresent(renderer);
      
      //FPS:
      float avgFPS = countedFrames / (fpsTimer.getTicks() / 1000.f);
      ++countedFrames;
      
      if (!VSYNC_ACTIVE)
	{
	  int frameTicks = capTimer.getTicks();
	  if (frameTicks < SCREEN_TICKS_PER_FRAME)
	    {
	      SDL_Delay(SCREEN_TICKS_PER_FRAME - frameTicks);
	    }
	}
      

      //Window title:
      if (debug_mode)
	{
	  int pos_x = getIsoCoordinateX(mouse_x, mouse_y, cameraoffset_x, cameraoffset_y, zoom);
	  int pos_y = getIsoCoordinateY(mouse_x, mouse_y, cameraoffset_x, cameraoffset_y, zoom);
	  
	  std::stringstream ss;
	  ss << "IsoMap";
	  ss << " | mouse: " << mouse_x << " " << mouse_y;
	  ss << " | coordinates: " << pos_x << " " << pos_y;
	  ss << " | cameraoffset: " << cameraoffset_x << " " << cameraoffset_y;
	  ss << " | zoom: " << zoom;
	  ss << " | avgFPS: " << int(avgFPS);
	  ss << " | vsync: " << VSYNC_ACTIVE;
	  const std::string tmp = ss.str();
	  const char *title = tmp.c_str();
	  SDL_SetWindowTitle(window, title);
	}
      else
	{
	  SDL_SetWindowTitle(window, "IsoMap");
	}
    }

  //-------------------------------------------------------------------------------------        
  //Cleanup:
  //-------------------------------------------------------------------------------------      
  delete sprite_selection;
  delete TextHandler;
  delete Units;
  delete Resources;
  delete Buildings;
  delete Menus;

  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
  IMG_Quit();
  SDL_Quit();
  
  return 0;
    }
void SceneSelectionSystem::ProcessUIEvent(DAVA::UIEvent *event)
{
	if (IsLocked() || !selectionAllowed)
	{
		return;
	}

	if(DAVA::UIEvent::PHASE_BEGAN == event->phase)
	{
		// we can select only if mouse isn't over hood axis
		// or if hood is invisible now
		// or if current mode is NORMAL (no modification)
		if(!hoodSystem->IsVisible() ||
			ST_MODIF_OFF == hoodSystem->GetModifMode() ||
			ST_AXIS_NONE == hoodSystem->GetPassingAxis())
		{
			if(event->tid == DAVA::UIEvent::BUTTON_1)
			{
				const EntityGroup* collisionEntities = collisionSystem->ObjectsRayTestFromCamera();
				EntityGroup selectableItems = GetSelecetableFromCollision(collisionEntities);

				DAVA::Entity *firstEntity = selectableItems.GetEntity(0);
				DAVA::Entity *nextEntity = selectableItems.GetEntity(0);

                // sequent selection?
                if(SettingsManager::GetValue(Settings::Scene_SelectionSequent).AsBool())
                {
				    // search possible next item only if now there is no selection or is only single selection
				    if(curSelections.Size() <= 1)
				    {
					    // find first after currently selected items
					    for(size_t i = 0; i < selectableItems.Size(); i++)
					    {
						    DAVA::Entity *entity = selectableItems.GetEntity(i);
						    if(curSelections.HasEntity(entity))
						    {
							    if((i + 1) < selectableItems.Size())
							    {
								    nextEntity = selectableItems.GetEntity(i + 1);
								    break;
							    }
						    }
					    }
				    }
                }

				int curKeyModifiers = QApplication::keyboardModifiers();
				if(curKeyModifiers & Qt::ControlModifier)
				{
					AddSelection(firstEntity);
				}
				else if(curKeyModifiers & Qt::AltModifier)
				{
					RemSelection(firstEntity);
				}
				else
				{
					// if new selection is NULL or is one of already selected items
					// we should change current selection only on phase end
					if(nextEntity == NULL || NULL != curSelections.IntersectedEntity(&selectableItems))
					{
						applyOnPhaseEnd = true;
						lastSelection = nextEntity;
					}
					else
					{
						SetSelection(nextEntity);
					}
				}
			}
		}
	}
	else if(DAVA::UIEvent::PHASE_ENDED == event->phase)
	{
		if(event->tid == DAVA::UIEvent::BUTTON_1)
		{
			if(applyOnPhaseEnd)
			{
				applyOnPhaseEnd = false;
				SetSelection(lastSelection);
			}
		}
	}
}
Exemple #9
0
int main()
{



	IrrlichtDevice *device = createDevice( video::EDT_OPENGL, dimension2d<u32>(1024, 768), 32, false, false, true, 0);
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

	smgr->addCameraSceneNodeFPS();

    const s32 randLength = 1024;
    srand(device->getTimer()->getRealTime());


    //Creating the grid for unit measure, etc
    u32 tileNumber = 128;
	IAnimatedMesh* groundMesh = smgr->addHillPlaneMesh("", dimension2d<float>(8,8),dimension2d<u32>(tileNumber,tileNumber),0,0.0f,dimension2df(0,0),dimension2df(tileNumber,tileNumber));
    IAnimatedMeshSceneNode * groundNode = smgr->addAnimatedMeshSceneNode(groundMesh);
    groundNode->setMaterialTexture(0,driver->getTexture("../media/grid2.png"));
    groundNode->setMaterialFlag(EMF_LIGHTING,false);
    groundNode->setPosition(vector3df(0,-2,0));


    //obstacles for stuff
    EntityGroup obstacles;

    for(int i = 0; i < 20; i++)
    {
        ISceneNode* s = smgr->addSphereSceneNode(20);
        IrrlichtBaseEntity * e = new IrrlichtBaseEntity(s);
        s->setPosition(vector3df(rand()%randLength - (randLength/2),0,rand()%randLength - (randLength/2)));
        obstacles.push_back(e);
    }

    //Nodes for vehicles
    ISceneNode * cube = smgr->addCubeSceneNode(4);
    ISceneNode * cube2 = smgr->addCubeSceneNode(4);
    cube->setMaterialFlag(EMF_LIGHTING,false);
    cube->setMaterialTexture(0,driver->getTexture("../media/v1-solid.png"));
    cube2->setMaterialFlag(EMF_LIGHTING,false);
    cube2->setMaterialTexture(0,driver->getTexture("../media/v2-solid.png"));

    //Creating the actual vehicles
    IrrlichtMobileEntity * Entity1 = new IrrlichtMobileEntity(cube ,vector3df(0,0,0), 1, 90, 40);
    IrrlichtMobileEntity * Entity2 = new IrrlichtMobileEntity(cube2,vector3df(0,0,300), 1, 100, 50);

    //Creating the steering conrollers, constructor also sets steering on entity
    SimpleSteeringController* Entity1Steering = new SimpleSteeringController(Entity1);
    SimpleSteeringController * Entity2Steering = new SimpleSteeringController(Entity2);

    //Setting up other params for behaviors
    Entity1Steering->SetObstacles(obstacles);
    Entity1Steering->SetHideTarget(Entity2);
    Entity1Steering->SetBehaviorFlag(EBF_HIDE,true);
    Entity1Steering->SetBehaviorFlag(EBF_AVOID,true);

    Entity2Steering->SetObstacles(obstacles);
    Entity2Steering->SetPursuitTarget(Entity1);
    Entity2Steering->SetBehaviorFlag(EBF_PURSUIT,true);
    Entity2Steering->SetBehaviorFlag(EBF_AVOID,true);

    //vars for tracking time between frames. This allows framerate independent motion state updates.
    u32 then = device->getTimer()->getTime();
    float timeUpdate = 0;

    while(device->run())
	{
	    const u32 now = device->getTimer()->getTime();
        const float frameDeltaTime = (float)(now - then) / 1000.f; // Time in seconds
        then = now;
        timeUpdate += frameDeltaTime;

        if(timeUpdate > 1)
        {
            core::stringw str = L"desteer v0.0.1  FPS: ";
            str += (s32)driver->getFPS();
			device->setWindowCaption(str.c_str());
			timeUpdate = 0;
		}

        //Do behavior updates before the rendering.
        Entity1->Update(frameDeltaTime);
        Entity2->Update(frameDeltaTime);

        driver->beginScene(true, true, SColor(255,100,101,140));
            smgr->drawAll();
		driver->endScene();
	}
    //Clean up irrlicht.
	device->drop();

	return 0;
}
void ModificationWidget::ApplyScaleValues(ST_Axis axis)
{
	DAVA::float32 scaleValue = 1.0f;

	switch (axis)
	{
	case ST_AXIS_X:
		scaleValue = xAxisModify->value();
		break;
	case ST_AXIS_Y:
		scaleValue = yAxisModify->value();
		break;
	case ST_AXIS_Z:
		scaleValue = zAxisModify->value();
		break;
	default:
		break;
	}

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();

			DAVA::Vector3 pos, scale, rotate;
			if(origMatrix.Decomposition(pos, scale, rotate))
			{
				DAVA::Matrix4 newMatrix;
				DAVA::Matrix4 scaleMatrix;
				DAVA::Matrix4 transformMatrix;

				DAVA::Matrix4 moveToZeroPos;
				DAVA::Matrix4 moveFromZeroPos;

				moveToZeroPos.CreateTranslation(-origMatrix.GetTranslationVector());
				moveFromZeroPos.CreateTranslation(origMatrix.GetTranslationVector());

				DAVA::float32 newEntityScale;
				if(pivotMode == PivotAbsolute)
				{
					if(0 != scale.x)
					{
						newEntityScale = scaleValue / scale.x;
					}
					else
					{
						newEntityScale = 0;
					}
				}
				else
				{
					newEntityScale = scaleValue;
				}

				scaleMatrix.CreateScale(DAVA::Vector3(newEntityScale, newEntityScale, newEntityScale));
				newMatrix = origMatrix * moveToZeroPos * scaleMatrix * moveFromZeroPos;
				newMatrix.SetTranslationVector(origMatrix.GetTranslationVector());

				curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
			}
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ApplyRotateValues(ST_Axis axis)
{
	DAVA::float32 x = DAVA::DegToRad(xAxisModify->value());
	DAVA::float32 y = DAVA::DegToRad(yAxisModify->value());
	DAVA::float32 z = DAVA::DegToRad(zAxisModify->value());

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();

			DAVA::Vector3 pos, scale, rotate;
			if(origMatrix.Decomposition(pos, scale, rotate))
			{
				DAVA::Matrix4 newMatrix;
				DAVA::Matrix4 rotationMatrix;
				DAVA::Matrix4 transformMatrix;

				DAVA::Matrix4 moveToZeroPos;
				DAVA::Matrix4 moveFromZeroPos;

				moveToZeroPos.CreateTranslation(-origMatrix.GetTranslationVector());
				moveFromZeroPos.CreateTranslation(origMatrix.GetTranslationVector());

				if(pivotMode == PivotAbsolute)
				{
					switch (axis)
					{
					case ST_AXIS_X:
						rotationMatrix.CreateRotation(DAVA::Vector3(1, 0, 0), x - rotate.x);
						break;
					case ST_AXIS_Y:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 1, 0), y - rotate.y);
						break;
					case ST_AXIS_Z:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 0, 1), z - rotate.z);
						break;
					default:
						break;
					}
				}
				else
				{
					switch (axis)
					{
					case ST_AXIS_X:
						rotationMatrix.CreateRotation(DAVA::Vector3(1, 0, 0), x);
						break;
					case ST_AXIS_Y:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 1, 0), y);
						break;
					case ST_AXIS_Z:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 0, 1), z);
						break;
					default:
						break;
					}
				}

				newMatrix = origMatrix * moveToZeroPos * rotationMatrix * moveFromZeroPos;
				newMatrix.SetTranslationVector(origMatrix.GetTranslationVector());

				curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
			}
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ApplyMoveValues(ST_Axis axis)
{
	DAVA::float32 x = xAxisModify->value();
	DAVA::float32 y = yAxisModify->value();
	DAVA::float32 z = zAxisModify->value();

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();
			DAVA::Vector3 origPos = origMatrix.GetTranslationVector();
			DAVA::Vector3 newPos = origPos;

			if(pivotMode == PivotAbsolute)
			{
				switch (axis)
				{
				case ST_AXIS_X:
					newPos.x = x;
					break;
				case ST_AXIS_Y:
					newPos.y = y;
					break;
				case ST_AXIS_Z:
					newPos.z = z;
					break;
				default:
					break;
				}
			}
			else
			{
				switch (axis)
				{
				case ST_AXIS_X:
					newPos.x += x;
					break;
				case ST_AXIS_Y:
					newPos.y += y;
					break;
				case ST_AXIS_Z:
					newPos.z += z;
					break;
				default:
					break;
				}
			}

			DAVA::Matrix4 newMatrix = origMatrix;
			newMatrix.SetTranslationVector(newPos);

			curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ReloadValues()
{
	if(modifMode == ST_MODIF_SCALE)
	{
		xLabel->setText("Scale:");

		yLabel->setVisible(false);
		zLabel->setVisible(false);
		yAxisModify->setVisible(false);
		zAxisModify->setVisible(false);
	}
	else
	{
		xLabel->setText("X:");

		yLabel->setVisible(true);
		zLabel->setVisible(true);
		yAxisModify->setVisible(true);
		zAxisModify->setVisible(true);
	}

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();
		if(selection.Size() > 0 && (modifMode == ST_MODIF_MOVE || modifMode == ST_MODIF_ROTATE || modifMode == ST_MODIF_SCALE))
		{
			xAxisModify->setEnabled(true);
			yAxisModify->setEnabled(true);
			zAxisModify->setEnabled(true);

			xAxisModify->showButtons(true);
			yAxisModify->showButtons(true);
			zAxisModify->showButtons(true);

			if(selection.Size() > 1)
			{
				groupMode = true;

				if(pivotMode == PivotRelative)
				{
					xAxisModify->setValue(0);
					yAxisModify->setValue(0);
					zAxisModify->setValue(0);
				}
				else
				{
					xAxisModify->showButtons(false);
					yAxisModify->showButtons(false);
					zAxisModify->showButtons(false);

					xAxisModify->clear();
					yAxisModify->clear();
					zAxisModify->clear();
				}
			}
			else
			{
				groupMode = false;

				if(pivotMode == PivotRelative)
				{
					xAxisModify->setValue(0);
					yAxisModify->setValue(0);
					zAxisModify->setValue(0);
				}
				else
				{
					DAVA::Entity *singleEntity = selection.GetEntity(0);
					if(NULL != singleEntity)
					{

						DAVA::float32 x = 0;
						DAVA::float32 y = 0;
						DAVA::float32 z = 0;

						DAVA::Matrix4 localMatrix = singleEntity->GetLocalTransform();
						switch (modifMode)
						{
						case ST_MODIF_MOVE:
							{
								DAVA::Vector3 translation = localMatrix.GetTranslationVector();
								x = translation.x;
								y = translation.y;
								z = translation.z;
							}
							break;
						case ST_MODIF_ROTATE:
							{
								DAVA::Vector3 pos, scale, rotate;
								if(localMatrix.Decomposition(pos, scale, rotate))
								{
									x = DAVA::RadToDeg(rotate.x);
									y = DAVA::RadToDeg(rotate.y);
									z = DAVA::RadToDeg(rotate.z);
								}
							}
							break;
						case ST_MODIF_SCALE:
							{
								DAVA::Vector3 pos, scale, rotate;
								if(localMatrix.Decomposition(pos, scale, rotate))
								{
									x = scale.x;
									y = scale.y;
									z = scale.z;
								}
							}
							break;
						default:
							break;
						}

						xAxisModify->setValue(x);
						yAxisModify->setValue(y);
						zAxisModify->setValue(z);
					}
				}
			}
		}
		else
		{
			xAxisModify->showButtons(true);
			yAxisModify->showButtons(true);
			zAxisModify->showButtons(true);

			xAxisModify->setEnabled(false);
			yAxisModify->setEnabled(false);
			zAxisModify->setEnabled(false);

			xAxisModify->clear();
			yAxisModify->clear();
			zAxisModify->clear();
		}
	}
}