bool LandscapeEditorBase::Input(DAVA::UIEvent *touch)
{
	Vector2 point;
	bool isIntersect = GetLandscapePoint(touch->point, point);
    
    point.x = (float32)((int32)point.x);
    point.y = (float32)((int32)point.y);
    
	landscapePoint = point;
	UpdateCursor();
	
    if(INVALID_TOUCH_ID == touchID || touchID == touch->tid)
    {
        if(UIEvent::BUTTON_1 == touch->tid)
        {
            inverseDrawingEnabled = IsKeyModificatorPressed(DVKEY_ALT);
            
            if(UIEvent::PHASE_BEGAN == touch->phase)
            {
                touchID = touch->tid;
                if(isIntersect)
                {
                    prevDrawPos = Vector2(-100, -100);
                    InputAction(touch->phase, isIntersect);
                }
                return true;
            }
            else if(UIEvent::PHASE_DRAG == touch->phase)
            {
                InputAction(touch->phase, isIntersect);
                if(!isIntersect)
                {
                    prevDrawPos = Vector2(-100, -100);
                }
                return true;
            }
            else if(UIEvent::PHASE_ENDED == touch->phase || UIEvent::PHASE_CANCELLED == touch->phase)
            {
                touchID = INVALID_TOUCH_ID;
                
                if(isIntersect)
                {
                    InputAction(touch->phase, isIntersect);
                    prevDrawPos = Vector2(-100, -100);
                }
                return true;
            }
        }
    }

    return false;
}
void SceneEditorScreenMain::Input(DAVA::UIEvent *event)
{
    if(UIEvent::PHASE_KEYCHAR == event->phase)
    {
        if(IsKeyModificatorPressed(DVKEY_ALT))
        {
            int32 key = event->tid - DVKEY_1;
            if(0 <= key && key < 8)
            {
                BodyItem *iBody = FindCurrentBody();
                Entity *node = iBody->bodyControl->GetSelectedSGNode();
                EditorScene *editorScene = iBody->bodyControl->GetScene();
                editorScene->SetForceLodLayer(node, key);
            }
            else if(DVKEY_0 == event->tid)
            {
                EditorSettings::Instance()->Save();
            }
        }
        
        
        //ckecking help
        UITextField *tf = dynamic_cast<UITextField *>(UIControlSystem::Instance()->GetFocusedControl());
        UITextField *tf1 = dynamic_cast<UITextField *>(focusedControl);
        if(!tf && !tf1)
        {
            if((DVKEY_F1 == event->tid) || (DVKEY_H == event->tid))
            {
                if(helpDialog->GetParent())
                {
                    helpDialog->Close();
                }
                else
                {
                    helpDialog->Show();
                }
            }
            
            if(DVKEY_ESCAPE == event->tid)
            {
                if(materialEditor && materialEditor->GetParent())
                {
                    MaterialsTriggered();
                }
            }

        }
    }
}
bool LandscapeEditorHeightmap::CopyPasteBegin()
{
    bool start = IsKeyModificatorPressed(DVKEY_ALT);
    if(start)
    {
        copyFromCenter = landscapePoint;
        copyToCenter = Vector2(-1.0f, -1.0f);
    }
    else if(Vector2(-1.0f, -1.0f) == copyToCenter)
    {
        copyToCenter = landscapePoint;
    }

	return start;
}
void LandscapeToolsPanelHeightmap::Input(DAVA::UIEvent *currentInput)
{
    if(UIEvent::PHASE_KEYCHAR == currentInput->phase)
    {
        if(IsKeyModificatorPressed(DVKEY_CTRL))
        {
            if(DVKEY_1 == currentInput->tid)
            {
                relative->SetChecked(true, true);
            }
            if(DVKEY_2 == currentInput->tid)
            {
                average->SetChecked(true, true);
            }
            if(DVKEY_3 == currentInput->tid)
            {
                absoluteDropper->SetChecked(true, true);
            }
        }
    }
    
    LandscapeToolsPanel::Input(currentInput);
}
void WASDCameraController::Input(UIEvent * event)
{
	if (currScene == 0)
		return;
	Camera * camera = currScene->GetCurrentCamera();
    if (!camera)return;
    if (event->phase == UIEvent::PHASE_KEYCHAR)
    {   
		if(!IsKeyModificatorsPressed())
		{
			switch (event->tid) 
			{
			case DVKEY_Z:
				{
					LookAtSelection();
					break;
				}
			case DVKEY_T:
				{
					if (!camera)return;

					viewZAngle = 0;
					viewYAngle = MAX_ANGLE;

					Matrix4 mt, mt2;
					mt.CreateRotation(Vector3(0,0,1), DegToRad(viewZAngle));
					mt2.CreateRotation(Vector3(1,0,0), DegToRad(viewYAngle));
					mt2 *= mt;
					Vector3 vect = Vector3(0,0, 200);

					Vector3 position = vect + Vector3(0, 10, 0) * mt2;

					camera->SetTarget(position);
					camera->SetPosition(vect);					
					break;					
				}

			case DVKEY_1:
				{
					EditorSettings::Instance()->SetCameraSpeedIndex(0);
					SetSpeed(EditorSettings::Instance()->GetCameraSpeed());
					break;
				}

			case DVKEY_2:
				{
					EditorSettings::Instance()->SetCameraSpeedIndex(1);
					SetSpeed(EditorSettings::Instance()->GetCameraSpeed());
					break;
				}

			case DVKEY_3:
				{
					EditorSettings::Instance()->SetCameraSpeedIndex(2);
					SetSpeed(EditorSettings::Instance()->GetCameraSpeed());
					break;
				}

			case DVKEY_4:
				{
					EditorSettings::Instance()->SetCameraSpeedIndex(3);
					SetSpeed(EditorSettings::Instance()->GetCameraSpeed());
					break;
				}

			case DVKEY_9:
				{
					if (speed - 50 >= 0)
					{
						speed -= 50;
					}
					break;
				}

			case DVKEY_0:
				{        
					if (speed + 50 <= 5000)
					{
						speed += 50;
					}
					break;
				}

			default:
				break;
			}		
		}
    } 

	bool altBut3 = (selection && event->tid == UIEvent::BUTTON_3 && IsKeyModificatorPressed(DVKEY_ALT));
	
	
	if(UIEvent::PHASE_BEGAN == event->phase)
	{
		startPt = stopPt = event->point;			

		if (altBut3)
		{
			const Vector3 & pos = camera->GetPosition();
			AABBox3 box = selection->GetWTMaximumBoundingBoxSlow();
			center = box.GetCenter();
			radius = (center - pos).Length();
		}
	}
	else if(UIEvent::PHASE_DRAG == event->phase || UIEvent::PHASE_ENDED == event->phase)
	{
		startPt = stopPt;
		stopPt = event->point;

		if (event->tid == UIEvent::BUTTON_2)
		{
			UpdateCam2But(camera);
		}
		else if (altBut3)
		{
			UpdateCamAlt3But(camera);			
		}
		else if (event->tid == UIEvent::BUTTON_3)
		{
			UpdateCam3But(camera);
		}
	}	
}
void HeightmapEditorSystem::UpdateBrushTool(float32 timeElapsed)
{
	if (!toolImage)
	{
		DAVA::Logger::Error("Tool image is empty!");
		return;
	}
	
	EditorHeightmap* editorHeightmap = drawSystem->GetHeightmapProxy();
	
	int32 scaleSize = toolImage->GetWidth();
	Vector2 pos = cursorPosition - Vector2((float32)scaleSize, (float32)scaleSize) / 2.0f;
	{
		switch (activeDrawingType)
		{
			case HEIGHTMAP_DRAW_RELATIVE:
			{
				float32 koef = (strength * timeElapsed);
				if(inverseDrawingEnabled)
				{
					koef = -koef;
				}

				if (IsKeyModificatorPressed(DVKEY_ALT))
				{
					koef = -koef;
				}

				editorHeightmap->DrawRelativeRGBA(toolImage, (int32)pos.x, (int32)pos.y, scaleSize, scaleSize, koef);
				break;
			}
				
			case HEIGHTMAP_DRAW_AVERAGE:
			{
				float32 koef = (averageStrength * timeElapsed) * 2.0f;
				editorHeightmap->DrawAverageRGBA(toolImage, (int32)pos.x, (int32)pos.y, scaleSize, scaleSize, koef);
				break;
			}

			case HEIGHTMAP_DRAW_ABSOLUTE:
			case HEIGHTMAP_DRAW_ABSOLUTE_DROPPER:
			{
				float32 maxHeight = drawSystem->GetLandscapeMaxHeight();
				float32 height = curHeight / maxHeight * Heightmap::MAX_VALUE;
				
				float32 koef = (averageStrength * timeElapsed) * 2.0f;
				editorHeightmap->DrawAbsoluteRGBA(toolImage, (int32)pos.x, (int32)pos.y, scaleSize, scaleSize, koef, height);
				break;
			}

			case HEIGHTMAP_DROPPER:
			{
				float32 curHeight = drawSystem->GetHeightAtPoint(cursorPosition);
				SceneSignals::Instance()->EmitDropperHeightChanged(dynamic_cast<SceneEditor2*>(GetScene()), curHeight);
				return;
			}

			case HEIGHTMAP_COPY_PASTE:
			{
				if (copyPasteFrom == Vector2(-1.f, -1.f) || copyPasteTo == Vector2(-1.f, -1.f))
				{
					return;
				}

				Vector2 posTo = pos;
				
				Vector2 deltaPos = cursorPosition - copyPasteTo;
				Vector2 posFrom = copyPasteFrom + deltaPos - Vector2((float32)scaleSize, (float32)scaleSize)/2.f;
				
				float32 koef = (averageStrength * timeElapsed) * 2.0f;

				editorHeightmap->DrawCopypasteRGBA(toolImage, posFrom, posTo, scaleSize, scaleSize, koef);

				break;
			}
				
			default:
				DAVA::Logger::Error("Invalid drawing type!");
				return;
		}
		
		Rect rect(pos.x, pos.y, (float32)scaleSize, (float32)scaleSize);
		drawSystem->GetHeightmapProxy()->UpdateRect(rect);
		AddRectToAccumulator(heightmapUpdatedRect, rect);
	}
}
void LandscapeEditorHeightmap::InputAction(int32 phase, bool intersects)
{
    bool dropper = IsKeyModificatorPressed(DVKEY_CTRL);
    if(dropper)
    {
        switch(phase)
        {
            case UIEvent::PHASE_BEGAN:
            case UIEvent::PHASE_DRAG:
            case UIEvent::PHASE_ENDED:
            {
                currentTool->height = GetDropperHeight();
                break;
            }
                
            default:
                break;
        }
    }
    else 
    {
        switch(phase)
        {
            case UIEvent::PHASE_BEGAN:
            {
                if(currentTool->absoluteDropperDrawing)
                {
                    currentTool->height = GetDropperHeight();
                }
                
				bool skipUndoPointCreation = false;
                if(LandscapeTool::TOOL_COPYPASTE == currentTool->type)
                {
					//CopyPasteBegin returns true when copyFrom point is set
					//no need to create undo point in this case
					skipUndoPointCreation = CopyPasteBegin();
					
					if (!skipUndoPointCreation)
					{
						DVASSERT(oldTilemap == NULL);
						workingLandscape->UpdateFullTiledTexture();
						oldTilemap = tilemaskTexture->CreateImageFromMemory();
					}
                }

				if (!skipUndoPointCreation)
				{
					StoreOriginalHeightmap();
				}

                editingIsEnabled = true;
                UpdateToolImage();

                break;
            }
                
            case UIEvent::PHASE_DRAG:
            {
                if(editingIsEnabled && !intersects)
                {
                    editingIsEnabled = false;

					CreateUndoPoint();
                }
                else if(!editingIsEnabled && intersects)
                {
                    editingIsEnabled = true;
                    UpdateToolImage();

					StoreOriginalHeightmap();

					if(LandscapeTool::TOOL_COPYPASTE == currentTool->type)
					{
						DVASSERT(oldTilemap == NULL);
						workingLandscape->UpdateFullTiledTexture();
						oldTilemap = tilemaskTexture->CreateImageFromMemory();
					}
                }
                break;
            }
                
            case UIEvent::PHASE_ENDED:
            {
                editingIsEnabled = false;

				CreateUndoPoint();

                break;
            }
                
            default:
                break;
        }
    }
}