Browse Source

Apply SonarCloud suggestions

Ivan Savenko 1 năm trước cách đây
mục cha
commit
742149062a

+ 12 - 9
client/eventsSDL/InputSourceGameController.cpp

@@ -58,7 +58,7 @@ void InputSourceGameController::openGameController(int index)
 		logGlobal->error("Fail to open game controller %d!", index);
 		return;
 	}
-	GameControllerPtr controllerPtr(controller, gameControllerDeleter);
+	GameControllerPtr controllerPtr(controller, &gameControllerDeleter);
 
 	// Need to save joystick index for event. Joystick index may not be equal to index sometimes.
 	int joystickIndex = getJoystickIndex(controllerPtr.get());
@@ -74,7 +74,7 @@ void InputSourceGameController::openGameController(int index)
 		return;
 	}
 
-	gameControllerMap.emplace(joystickIndex, std::move(controllerPtr));
+	gameControllerMap.try_emplace(joystickIndex, std::move(controllerPtr));
 }
 
 int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
@@ -86,7 +86,7 @@ int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
 	SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
 	if(instanceID < 0)
 		return -1;
-	return (int)instanceID;
+	return instanceID;
 }
 
 void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
@@ -191,7 +191,7 @@ void InputSourceGameController::tryToConvertCursor()
 	if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
 	{
 		const Point & cursorPosition = GH.getCursorPosition();
-		CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
+		CCS->curh->changeCursor(Cursor::ShowType::SOFTWARE);
 		CCS->curh->cursorMove(cursorPosition.x, cursorPosition.y);
 		GH.input().setCursorPosition(cursorPosition);
 	}
@@ -243,7 +243,7 @@ void InputSourceGameController::handleUpdate()
 		return;
 	}
 
-	int32_t deltaTime = std::chrono::duration_cast<std::chrono::seconds>(nowMs - lastCheckTime).count();
+	int32_t deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(nowMs - lastCheckTime).count();
 	handleCursorUpdate(deltaTime);
 	handleScrollUpdate(deltaTime);
 	lastCheckTime = nowMs;
@@ -251,15 +251,17 @@ void InputSourceGameController::handleUpdate()
 
 void InputSourceGameController::handleCursorUpdate(int32_t deltaTimeMs)
 {
+	float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
+
 	if(cursorAxisValueX == 0)
 		cursorPlanDisX = 0;
 	else
-		cursorPlanDisX += ((float)deltaTimeMs / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
+		cursorPlanDisX += deltaTimeSeconds * AXIS_MOVE_SPEED * cursorAxisValueX / AXIS_MAX_ZOOM;
 
 	if(cursorAxisValueY == 0)
 		cursorPlanDisY = 0;
 	else
-		cursorPlanDisY += ((float)deltaTimeMs / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
+		cursorPlanDisY += deltaTimeSeconds * AXIS_MOVE_SPEED * cursorAxisValueY / AXIS_MAX_ZOOM;
 
 	int moveDisX = getMoveDis(cursorPlanDisX);
 	int moveDisY = getMoveDis(cursorPlanDisY);
@@ -287,8 +289,9 @@ void InputSourceGameController::handleScrollUpdate(int32_t deltaTimeMs)
 		scrollPlanDisX = scrollPlanDisY = 0;
 		return;
 	}
-	scrollPlanDisX += ((float)deltaTimeMs / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
-	scrollPlanDisY += ((float)deltaTimeMs / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
+	float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
+	scrollPlanDisX += deltaTimeSeconds * AXIS_MOVE_SPEED * scrollAxisValueX / AXIS_MAX_ZOOM;
+	scrollPlanDisY += deltaTimeSeconds * AXIS_MOVE_SPEED * scrollAxisValueY / AXIS_MAX_ZOOM;
 	int moveDisX = getMoveDis(scrollPlanDisX);
 	int moveDisY = getMoveDis(scrollPlanDisY);
 	if(moveDisX != 0 || moveDisY != 0)

+ 4 - 4
client/gui/CursorHandler.cpp

@@ -291,17 +291,17 @@ void CursorHandler::show()
 	cursor->setVisible(true);
 }
 
-Cursor::ShowType CursorHandler::getShowType()
+Cursor::ShowType CursorHandler::getShowType() const
 {
 	return showType;
 }
 
-void CursorHandler::ChangeCursor(Cursor::ShowType showType)
+void CursorHandler::changeCursor(Cursor::ShowType newShowType)
 {
-	if(this->showType == showType)
+	if(newShowType == showType)
 		return;
 
-	switch(showType)
+	switch(newShowType)
 	{
 		case Cursor::ShowType::SOFTWARE:
 			cursor.reset(new CursorSoftware());

+ 2 - 2
client/gui/CursorHandler.h

@@ -186,6 +186,6 @@ public:
 	/// change cursor's positions to (x, y)
 	void cursorMove(const int & x, const int & y);
 
-	Cursor::ShowType getShowType();
-	void ChangeCursor(Cursor::ShowType showType);
+	Cursor::ShowType getShowType() const;
+	void changeCursor(Cursor::ShowType showType);
 };