Esempio n. 1
0
void Graphics::CallbackEventOnMouseMotion(int x, int y)
{
    double mouseX, mouseY;
    MousePosition(x, y, &mouseX, &mouseY);
    m_graphics->HandleEventOnMouseMotion(mouseX , mouseY);
    glutPostRedisplay();
}
Esempio n. 2
0
File: GridCell.c Progetto: osen/gc
void GridCellDraw(struct GridCell *ctx)
{
  SDL_Rect rect = {0};
  struct Camera *camera = NULL;
  int mouseX = 0;
  int mouseY = 0;
  SDL_Rect mouseRect = {0};

  camera = ctx->world->camera;

  rect.x = ctx->x;
  rect.y = ctx->y;
  rect.w = GRIDCELL_SIZE;
  rect.h = GRIDCELL_SIZE;

  MousePosition(ctx->world->mouse, &mouseX, &mouseY);
  CameraTranslate(camera, &mouseX, &mouseY);
  //mouseRect.x = mouseX + ctx->world->camera->x;
  //mouseRect.y = mouseY + ctx->world->camera->y;
  mouseRect.x = mouseX;
  mouseRect.y = mouseY;
  mouseRect.w = 1;
  mouseRect.h = 1;

  if(ctx->type == 0)
  {
    CameraDrawImage(camera, ctx->image, ctx->x, ctx->y);
  }
  else
  {
    CameraDrawImage(camera, ctx->blockImage, ctx->x, ctx->y);
  }

  ctx->active = 0;

  if(check_collision(rect, mouseRect) == 1)
  {
    ctx->active = 1;

    if(MouseClicked(ctx->world->mouse) == 1)
    {
      ctx->type = 1;

      //printf("Connections: ^%p >%p \\/%p <%p \\%p /%p \\%p /%p\n",
      //  ctx->top, ctx->right, ctx->bottom, ctx->left,
      //  ctx->topLeft, ctx->topRight, ctx->bottomRight, ctx->bottomLeft);
    }

    CameraDrawImage(camera, ctx->cursorImage, ctx->x, ctx->y);
  }

  if(_GridCellHasNeighbourActive(ctx) == 1)
  {
    CameraDrawImage(camera, ctx->highlightImage, ctx->x, ctx->y);
  }
}
Esempio n. 3
0
void Graphics::CallbackEventOnMouse(int button, int state, int x, int y)
{
    if(m_graphics && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
	double mouseX, mouseY;
	MousePosition(x, y, &mouseX, &mouseY);
	m_graphics->HandleEventOnMouseLeftBtnDown(mouseX , mouseY);
	glutPostRedisplay();
    }	    
}
Esempio n. 4
0
void HUDLayer::OnDraw()
/*****************************************************************************/
{
   Drawing::SetColor(154, 205, 50);
   ostringstream text1, text2;
   XYPoint mousePos = MousePosition();
   DevicePoint mousePosDev = MousePositionDevice();
   text1<<"Mouse (XY): ("<<mousePos.x<<", "<<mousePos.y<<")";
   text2<<"Mouse (Device): ("<<mousePosDev.x<<", "<<mousePosDev.y<<")";
   const int rightYOffset = 230;
	Text(DevicePoint(WidthInPixels() - rightYOffset, 28), text2.str(), FN_BITMAP_8_BY_13);
	Text(DevicePoint(WidthInPixels() - rightYOffset, 10), text1.str(), FN_BITMAP_8_BY_13);

}
/**
 * Absolute Translation conversion from mouse position on the screen to widget axis movement/rotation.
 */
void FMouseDeltaTracker::AbsoluteTranslationConvertMouseToDragRot(FSceneView* InView, FEditorViewportClient* InViewportClient,FVector& OutDrag, FRotator& OutRotation, FVector& OutScale ) const
{
	OutDrag = FVector::ZeroVector;
	OutRotation = FRotator::ZeroRotator;
	OutScale = FVector::ZeroVector;

	check ( InViewportClient->GetCurrentWidgetAxis() != EAxisList::None );

	//calculate mouse position
	check(InViewportClient->Viewport);
	FVector2D MousePosition(InViewportClient->Viewport->GetMouseX(), InViewportClient->Viewport->GetMouseY());

	InViewportClient->Widget->AbsoluteTranslationConvertMouseMovementToAxisMovement(InView, InViewportClient, InViewportClient->GetWidgetLocation(), MousePosition, OutDrag, OutRotation, OutScale );
}
Esempio n. 6
0
bool HUD_Object::MouseCollide()
{
	POINT ptMousePos = MousePosition();

	if (ptMousePos.x < m_rObjectBox.m_fX )
		return false;

	if (ptMousePos.y < m_rObjectBox.m_fY)
		return false;

	if (ptMousePos.x > m_rObjectBox.m_fWidth + m_rObjectBox.m_fX)
		return false;

	if (ptMousePos.y > m_rObjectBox.m_fHeight + m_rObjectBox.m_fY)
		return false;

	return true;
}
Esempio n. 7
0
// int main(int argc, char* argv[])
int GameMain()
{
//    assert(argc || argv[0]); // Fixes the compiler complaining about unused values;

    GameState* game_state = CreateNewGameState("EnGen", 1600, 900);
    Renderer* renderer = game_state->renderer;
    game_state->active_scene = PushScene(&game_state->permanent_memory, MAX_GAME_ENTITES);

    TileMap* tilemap = game_state->active_scene->tilemap;

    for (int32 i = 0; i < 10; ++i)
    {
        Vec2 pos = { (float)i, 2.f };
        AddTileToMap(tilemap, pos);
    }

    for (int32 i = 0; i < 10; ++i)
    {
        Vec2 pos = { 0, (float)i };
        AddTileToMap(tilemap, pos);
    }

    for (int32 i = 0; i < 10; ++i)
    {
        Vec2 pos = { 10.f, (float)i };
        AddTileToMap(tilemap, pos);
    }

    UIWindow* ui = PushStruct(&game_state->permanent_memory, UIWindow);
    SetTitle(ui, "Editor UI!");

    SetSize(ui, { 0.1f, 0.3f, 0.2f, 0.2f }, 0.05f);

    UIWindow* ui2 = PushStruct(&game_state->permanent_memory, UIWindow);
    SetTitle(ui2, "Editor UI2!");

    SetSize(ui2, { 0.2f, 0.3f, 0.2f, 0.2f }, 0.05f);

    InitializeDebugConsole();

#if 0
    InitializeAudio();
    char* test_sound_file = "C:\\projects\\imperial_march.wav";
    bool test_sound_loaded = LoadWavFile(test_sound_file);
    if(test_sound_loaded)
    {
        printf("Loaded File\n");
    }
    PauseAudio(false);
#endif

    Camera default_camera = {}; // maybe put this in game_state?
    default_camera.position = vec2(0, 0);
    default_camera.viewport_size.x = 16;
    default_camera.viewport_size.y = 9;

    uint32 frame_count = 0;
    uint32 fps = 0;
    double last_fps_time = 0;

    bool running = true;

    while (running)
    {
        ProfileBeginFrame();
        ProfileBeginSection(Profile_Frame);
        ProfileBeginSection(Profile_Input);

        Platform_RunMessageLoop(game_state->input);

        Camera* draw_camera = game_state->active_camera ? game_state->active_camera : &default_camera;

        game_state->window.resolution = Platform_GetResolution();
        UpdateMouseWorldPosition(game_state->input, game_state->window.resolution, draw_camera->viewport_size, draw_camera->position);

        ProfileEndSection(Profile_Input);

        Vec2i mouse_pos = MousePosition(game_state->input);
        //DebugPrintf("Mouse World Position: (%.2f, %.2f)", mouse_pos.x, mouse_pos.y);
        DebugPrintf("Mouse World Position: (%d, %d)", mouse_pos.x, mouse_pos.y);
        DebugPrintf("Main Camera Position: (%.2f, %.2f)", default_camera.position.x, default_camera.position.y);


        DebugPrintf("Key Pressed: %s", IsDown(game_state->input, KeyCode_a) ? "TRUE" : "FALSE");

        if (OnDown(game_state->input, KeyCode_ESCAPE))
        {
            running = false;
            break;
        }

#if 0 // TODO: Platform layer
        if (OnDown(game_state->input, KeyCode_z))
        {
            ForceColorClear();
            SwapBuffer(game_state);
            //WindowSetScreenMode(&game_state->window, ScreenMode_Windowed);
        }
        else if (OnDown(game_state->input, KeyCode_c))
        {
            ForceColorClear();
            SwapBuffer(game_state);
            //WindowSetScreenMode(&game_state->window, ScreenMode_Borderless);
        }
#endif

        static bool draw_debug = true;
        if (OnDown(game_state->input, KeyCode_BACKQUOTE))
        {
            draw_debug = !draw_debug;
        }

        Renderer* debug_renderer = draw_debug ? renderer : 0;

        TimeBeginFrame(game_state);

        // Update the scene first, pushing draw calls if necessary.
        // Then call begin_frame which builds matrices and clears buffers;
        float current_time = CurrentTime(game_state);
        if (current_time - last_fps_time > 1.0f)
        {
            last_fps_time = current_time;
            fps = frame_count;
            frame_count = 0;
        }
        frame_count++;
        DebugPrintf("FPS: \t\t%d \tFrames: \t%d", fps, FrameCount(game_state));

        DebugControlCamera(game_state, &default_camera);

        // TODO(cgenova): separate update and render calls so that things can be set up when rendering begins;
        BeginFrame(renderer, &game_state->window);

        ProfileBeginSection(Profile_SceneUpdate);

        DebugPrintPushColor(vec4(1.0f, 0, 0, 1.0f));
        DebugPrintf("Active scene entity usage: (%d / %d)", game_state->active_scene->active_entities, MAX_GAME_ENTITES);
        DebugPrintPopColor();

        UpdateSceneEntities(game_state, game_state->active_scene);
        DrawSceneEntities(game_state->active_scene, renderer);

        ProfileEndSection(Profile_SceneUpdate);

#if 1 // Spaghetti test
        const size_t num_verts = 200;
        static SimpleVertex v[num_verts];
        static bool initialized = false;
        if (!initialized)
        {
            initialized = true;
            for (uint32 i = 0; i < num_verts; ++i)
            {
                SimpleVertex verts = {};
                verts.position = vec2((float)(i / 50.f) - 2.f, (float)i);
                verts.color = vec4(1, 1, 0, 1.f);
                v[i] = verts;
            }
        }
        else
        {
            for (uint32 i = 0; i < num_verts; ++i)
            {
                v[i].position.y = sin(CurrentTime(game_state) + i / (PI * 20));
            }
        }

        PrimitiveDrawParams spaghetti_params = {};
        spaghetti_params.line_draw_flags |= PrimitiveDraw_Smooth;
        //      spaghetti_params.line_draw_flags |= Draw_ScreenSpace;
        spaghetti_params.line_width = 0;
        DrawLine(renderer, v, num_verts, spaghetti_params);
#endif

        DrawTileMap(game_state, game_state->active_scene->tilemap);

        UpdateUIWindow(game_state, ui);
        UpdateUIWindow(game_state, ui2);

        RenderDrawBuffer(renderer, draw_camera);

        ProfileEndSection(Profile_Frame);
        ProfileEndFrame(debug_renderer, TARGET_FPS);
        DebugDrawConsole(debug_renderer);

        // NOTE:
        // For drawing Debug info, the profiling in this section will be discarded,
        // but it is only drawing text and the debug graph.
        RenderDrawBuffer(renderer, draw_camera);

        SwapBuffer(game_state);

        // TODO(cgenova): High granularity sleep function!

        ResetArena(&game_state->temporary_memory);

    }// End main loop

    return 1;
}
/**
 * Adds delta movement into the tracker.
 */
void FMouseDeltaTracker::AddDelta(FEditorViewportClient* InViewportClient, FKey InKey, const int32 InDelta, bool InNudge)
{
	const bool LeftMouseButtonDown = InViewportClient->Viewport->KeyState(EKeys::LeftMouseButton);
	const bool RightMouseButtonDown = InViewportClient->Viewport->KeyState(EKeys::RightMouseButton);
	const bool MiddleMouseButtonDown = InViewportClient->Viewport->KeyState(EKeys::MiddleMouseButton);
	const bool bAltDown = InViewportClient->IsAltPressed();
	const bool bShiftDown = InViewportClient->IsShiftPressed();
	const bool bControlDown = InViewportClient->IsCtrlPressed();

	if( !LeftMouseButtonDown && !MiddleMouseButtonDown && !RightMouseButtonDown && !InNudge )
	{
		return;
	}

	// Accumulate raw delta
	RawDelta += FVector(InKey == EKeys::MouseX ? InDelta : 0,
						InKey == EKeys::MouseY ? InDelta : 0,
						0);

	// Note that AddDelta has been called since StartTracking
	bHasReceivedAddDelta = true;

	// If we are using a drag tool, the widget isn't involved so set it to having no active axis.  This
	// means we will get unmodified mouse movement returned to us by other functions.

	const EAxisList::Type SaveAxis = InViewportClient->GetCurrentWidgetAxis();

	// If the user isn't dragging with the left mouse button, clear out the axis 
	// as the widget only responds to the left mouse button.
	//
	// We allow an exception for dragging with the left and/or right mouse button while holding control
	// as that simulates moving objects with the gizmo
	//
	// We also allow the exception of the middle mouse button when Alt is pressed as it 
	// allows movement of only the pivot.
	const bool bIsOrthoObjectRotation = bControlDown && InViewportClient->IsOrtho();
	const bool bUsingDragTool = UsingDragTool();
	const bool bUsingAxis = !bUsingDragTool && (LeftMouseButtonDown || (bAltDown && MiddleMouseButtonDown) || ((bIsOrthoObjectRotation || bControlDown) && RightMouseButtonDown));

	ConditionalBeginUsingDragTool( InViewportClient );

	if( bUsingDragTool || !InViewportClient->IsTracking() || !bUsingAxis )
	{
		InViewportClient->SetCurrentWidgetAxis( EAxisList::None );
	}

	FVector Wk = InViewportClient->TranslateDelta( InKey, InDelta, InNudge );

	EndScreen += Wk;

	if( InViewportClient->GetCurrentWidgetAxis() != EAxisList::None )
	{
		// Affect input delta by the camera speed

		FWidget::EWidgetMode WidgetMode = InViewportClient->GetWidgetMode();
		bool bIsRotation = (WidgetMode == FWidget::WM_Rotate) 
			|| ( ( WidgetMode == FWidget::WM_TranslateRotateZ ) && ( InViewportClient->GetCurrentWidgetAxis() == EAxisList::ZRotation ) )
			|| ( ( WidgetMode == FWidget::WM_2D) && (InViewportClient->GetCurrentWidgetAxis() == EAxisList::Rotate2D ) );
		if (bIsRotation)
		{
			Wk *= GetDefault<ULevelEditorViewportSettings>()->MouseSensitivty;
		}
		else if( WidgetMode == FWidget::WM_Scale && !GEditor->UsePercentageBasedScaling() )
		{
			const float ScaleSpeedMultipler = 0.01f;
			Wk *= ScaleSpeedMultipler;
		}

		// Make rotations occur at the same speed, regardless of ortho zoom

		if( InViewportClient->IsOrtho() )
		{
			if (bIsRotation)
			{
				float Scale = 1.0f;

				if( InViewportClient->IsOrtho() )
				{
					Scale = DEFAULT_ORTHOZOOM / (float)InViewportClient->GetOrthoZoom();
				}

				Wk *= Scale;
			}
		}
		//if Absolute Translation, and not just moving the camera around
		else if (InViewportClient->IsUsingAbsoluteTranslation())
		{
			// Compute a view.
			FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues( 
				InViewportClient->Viewport, 
				InViewportClient->GetScene(),
				InViewportClient->EngineShowFlags )
				.SetRealtimeUpdate( InViewportClient->IsRealtime() ));

			FSceneView* View = InViewportClient->CalcSceneView( &ViewFamily );

			//calculate mouse position
			check(InViewportClient->Viewport);
			FVector2D MousePosition(InViewportClient->Viewport->GetMouseX(), InViewportClient->Viewport->GetMouseY());
			FVector WidgetPosition = InViewportClient->GetWidgetLocation();

			FRotator TempRot;
			FVector TempScale;
			InViewportClient->Widget->AbsoluteTranslationConvertMouseMovementToAxisMovement(View, InViewportClient, WidgetPosition, MousePosition, Wk, TempRot, TempScale );
		}
	}

	End += Wk;
	EndSnapped = End;

	
	if( UsingDragTool() )
	{
		FVector Drag = Wk;
		if( DragTool->bConvertDelta )
		{
			FRotator Rot;
			InViewportClient->ConvertMovementToDragRot( Wk, Drag, Rot );
		}

		if ( InViewportClient->IsPerspective() )
		{
			DragTool->AddDelta(Wk);
		}
		else
		{
			DragTool->AddDelta( Drag );
		}

		InViewportClient->SetCurrentWidgetAxis( SaveAxis );
	}
	else
	{
		switch( InViewportClient->GetWidgetMode() )
		{
			case FWidget::WM_Translate:
				FSnappingUtils::SnapPointToGrid( EndSnapped, FVector(GEditor->GetGridSize(),GEditor->GetGridSize(),GEditor->GetGridSize()) );
				break;

			case FWidget::WM_Scale:
				FSnappingUtils::SnapScale( EndSnapped, FVector(GEditor->GetGridSize(),GEditor->GetGridSize(),GEditor->GetGridSize()) );
				break;

			case FWidget::WM_Rotate:
			{
				FRotator Rotation( EndSnapped.X, EndSnapped.Y, EndSnapped.Z );
				FSnappingUtils::SnapRotatorToGrid( Rotation );
				EndSnapped = FVector( Rotation.Pitch, Rotation.Yaw, Rotation.Roll );
			}
			break;
			case FWidget::WM_TranslateRotateZ:
			case FWidget::WM_2D:
			{
				if (InViewportClient->GetCurrentWidgetAxis() == EAxisList::Rotate2D)
				{
					FRotator Rotation( EndSnapped.X, EndSnapped.Y, EndSnapped.Z );
					FSnappingUtils::SnapRotatorToGrid( Rotation );
					EndSnapped = FVector( Rotation.Pitch, Rotation.Yaw, Rotation.Roll );
				}
				else
				{
					//translation (either xy plane or z)
					FSnappingUtils::SnapPointToGrid( EndSnapped, FVector(GEditor->GetGridSize(),GEditor->GetGridSize(),GEditor->GetGridSize()) );
				}
			}

			default:
				break;
		}
	}

}