bool PlaceToolInputState::OnPointerDown( const Pointer& pointer ) { // Make sure there is a valid UnitType and Faction selected. if( mSelectedUnitType && mSelectedFaction && pointer.IsActivePointer() ) { EditorState* owner = GetOwnerDerived(); MapView* mapView = owner->GetMapView(); // If this is the active pointer, create a new Unit placement sprite under it. CreateUnitPlacementSprite( mapView->ScreenToWorldCoords( pointer.position ) ); } return false; //InputState::OnPointerDown( pointer ); }
bool PlaceToolInputState::OnPointerMotion( const Pointer& activePointer, const PointersByID& pointersByID ) { bool wasHandled = false; //InputState::OnPointerMotion( activePointer, pointersByID ); if( !wasHandled && mUnitPlacementSprite && activePointer.isMoving ) { EditorState* owner = GetOwnerDerived(); MapView* mapView = owner->GetMapView(); // Move the Unit placement Sprite to the position of the active pointer. MoveUnitPlacementSprite( mapView->ScreenToWorldCoords( activePointer.position ) ); wasHandled = true; } return wasHandled; }
Unit* PlaceToolInputState::CreateUnitAtScreenCoords( const Vec2f& screenCoords ) { Unit* unit = nullptr; if( mSelectedUnitType ) { if( mSelectedFaction ) { EditorState* owner = GetOwnerDerived(); MapView* mapView = owner->GetMapView(); Map* map = owner->GetMap(); // Get the Tile at the screen coords. Vec2f worldPos = mapView->ScreenToWorldCoords( screenCoords ); Vec2s tilePos = mapView->WorldToTileCoords( worldPos ); Map::Iterator tile = map->GetTile( tilePos ); if( tile.IsValid() && tile->IsEmpty() && mSelectedUnitType->CanMoveAcrossTerrain( tile->GetTerrainType() ) ) { // If the selected UnitType can be placed into the Tile, create a new Unit. DebugPrintf( "Placing Unit at tile (%d,%d)!", tilePos.x, tilePos.y ); unit = map->CreateUnit( mSelectedUnitType, mSelectedFaction, tilePos ); } else { WarnFail( "Cannot place Unit into tile (%d,%d)!", tilePos.x, tilePos.y ); } } else { WarnFail( "Cannot create Unit because no Faction was selected!" ); } } else { WarnFail( "Cannot create Unit because no UnitType was selected!" ); } return unit; }