/// <summary> /// Return the screen size according to the GUI mode. /// </summary> /// <returns> /// Return a Vector2 with screen width and height. /// </returns> private Vector2 GetScreenSize() { float screenWidth = 0.0f; float screenHeight = 0.0f; var screenS = JCS_ScreenSettings.instance; var cam = JCS_Camera.main; RectTransform slidePanel = mPanelHolder.slidePanels[0]; var panelRoot = slidePanel.GetComponent <JCS_PanelRoot>(); if (panelRoot == null) { panelRoot = slidePanel.parent.GetComponent <JCS_PanelRoot>(); } switch (mUnityGUIType) { case JCS_UnityGUIType.uGUI_2D: { if (panelRoot != null) { JCS_ScreenSizef size = screenS.StartingScreenSize(); screenWidth = size.width; screenHeight = size.height; } else { screenWidth = screenS.STANDARD_SCREEN_SIZE.width; screenHeight = screenS.STANDARD_SCREEN_SIZE.height; } } break; case JCS_UnityGUIType.nGUI_3D: { screenWidth = cam.CamRectSize.x; screenHeight = cam.CamRectSize.y; } break; } return(new Vector2(screenWidth, screenHeight)); }
/// <summary> /// Resize the game if screen size changes. /// </summary> protected virtual void OnResizeGame() { JCS_ScreenSettings ss = JCS_ScreenSettings.instance; float currentScreenRatio = ss.CURRENT_SCREEN_SIZE.width / ss.CURRENT_SCREEN_SIZE.height; float startingScreenRatio = (float)ss.STARTING_SCREEN_SIZE.width / (float)ss.STARTING_SCREEN_SIZE.height; if (currentScreenRatio > startingScreenRatio) { // Set the limit if reach the starting screen ratio. ss.CURRENT_SCREEN_SIZE.width = (float)ss.STARTING_SCREEN_SIZE.width; ss.CURRENT_SCREEN_SIZE.height = (float)ss.STARTING_SCREEN_SIZE.height; } float prevRatio = ss.PREV_SCREEN_SIZE.width / ss.PREV_SCREEN_SIZE.height; float newRatio = ss.CURRENT_SCREEN_SIZE.width / ss.CURRENT_SCREEN_SIZE.height; float divRatio = prevRatio / newRatio; mCamera.orthographicSize *= divRatio; mCamera.fieldOfView *= divRatio; if (mSceneJustLoad) { JCS_ScreenSizef bs = ss.BlackspaceSize(); float bw = bs.width; float bh = bs.height; // Width does not need to be calculate, but // need to set back to the original value. // Hence, the `mRecordOrthographicSize` and // `mRecordFieldOfView` variables. if (bw > bh) { mCamera.orthographicSize = mRecordOrthographicSize; mCamera.fieldOfView = mRecordFieldOfView; } // Calculating the proper hight. else { // Calculate what the height suppose to be! float supposeHeight = ((float)JCS_Screen.width * (float)ss.STARTING_SCREEN_SIZE.height) / (float)ss.STARTING_SCREEN_SIZE.width; // Use the 'suppose height' to find the proper // height ratio. float heightRatio = ((float)JCS_Screen.height / supposeHeight); mCamera.orthographicSize = heightRatio * mRecordOrthographicSize; mCamera.fieldOfView = heightRatio * mRecordFieldOfView; } mSceneJustLoad = false; } /* Store it to screen settings. */ { ss.ORTHOGRAPHIC_SIZE = mCamera.orthographicSize; ss.FIELD_OF_VIEW = mCamera.fieldOfView; } }
/// <summary> /// Handle the mobile swipe input. /// </summary> private void DoMobileSwipe() { var si = JCS_SlideInput.instance; if (si == null) { return; } bool enableSlidePanel = true; if (mInteractableSwipe && si.Touched) { Vector3 deltaPos = si.DeltaPos; bool cancelX = false; bool cancelY = false; if (mFreezeX) { cancelX = true; } if (mFreezeY) { cancelY = true; } /* Fix so you don't swipe over boundaries! */ { bool positiveX = JCS_Mathf.IsPositive(deltaPos.x); bool positiveY = JCS_Mathf.IsPositive(deltaPos.y); if (mCurrentPage.x <= mMinPageX && positiveX || mCurrentPage.x >= mMaxPageX && !positiveX) { cancelX = true; } if (mCurrentPage.y <= mMinPageY && positiveY || mCurrentPage.y >= mMaxPageY && !positiveY) { cancelY = true; } } if (cancelX) { deltaPos.x = 0.0f; } if (cancelY) { deltaPos.y = 0.0f; } // If you can move at least one dimension, if (!cancelX || !cancelY) { enableSlidePanel = false; } if (!enableSlidePanel) { mPanelHolder.DeltaMove(deltaPos); } } mPanelHolder.EnableSlidePanels(enableSlidePanel); if (mInteractableSwipe && JCS_Input.GetMouseButtonUp(JCS_MouseButton.LEFT)) { Vector3 posDiff = si.DragDistance; JCS_ScreenSizef vs = JCS_ScreenSettings.instance.VisibleScreenSize(); var target_vs = new JCS_ScreenSizef(vs.width * mSwipeArea.x, vs.height * mSwipeArea.y); var speedX = si.DragDistance.x / si.TouchTime; var speedY = si.DragDistance.y / si.TouchTime; bool reachedX = posDiff.x > target_vs.width; // distance bool speedExceedX = speedX > mSwipeSpeedX; // speed if (!mFreezeX && (reachedX || speedExceedX)) { if (JCS_Mathf.IsPositive(si.DragDisplacement.x)) { SwitchScene(JCS_2D4Direction.LEFT); } else { SwitchScene(JCS_2D4Direction.RIGHT); } if (afterSwiped != null) { afterSwiped.Invoke(mCurrentPage); } } bool reachedY = posDiff.y > target_vs.height; // distance bool speedExceedY = speedY > mSwipeSpeedY; // speed if (!mFreezeY && (reachedY || speedExceedY)) { if (JCS_Mathf.IsPositive(si.DragDisplacement.y)) { SwitchScene(JCS_2D4Direction.BOTTOM); } else { SwitchScene(JCS_2D4Direction.TOP); } if (afterSwiped != null) { afterSwiped.Invoke(mCurrentPage); } } } }
/// <summary> /// Handle the mobile swipe input. /// </summary> private void DoMobileSwipe() { JCS_SlideInput si = JCS_InputManager.instance.GetGlobalSlideInput(); if (si == null) { return; } bool enableSlidePanel = true; if (si.Touched) { Vector3 deltaPos = si.DeltaPos; bool cancelX = false; bool cancelY = false; if (mFreezeX) { cancelX = true; } if (mFreezeY) { cancelY = true; } /* Fix so you don't swipe over boundaries! */ { bool positiveX = JCS_Mathf.IsPositive(deltaPos.x); bool positiveY = JCS_Mathf.IsPositive(deltaPos.y); if (mCurrentPage.x <= mMinPageX && positiveX || mCurrentPage.x >= mMaxPageX && !positiveX) { cancelX = true; } if (mCurrentPage.y <= mMinPageY && positiveY || mCurrentPage.y >= mMaxPageY && !positiveY) { cancelY = true; } } if (cancelX) { deltaPos.x = 0.0f; } if (cancelY) { deltaPos.y = 0.0f; } // If you can move at least one dimension, if (!cancelX || !cancelY) { enableSlidePanel = false; } if (!enableSlidePanel) { mPanelHolder.DeltaMove(deltaPos); } } mPanelHolder.EnableSlidePanels(enableSlidePanel); if (JCS_Input.GetMouseButtonUp(JCS_MouseButton.LEFT)) { Vector3 posDiff = si.DragDistance; JCS_ScreenSizef vs = JCS_ScreenSettings.instance.VisibleScreenSize(); JCS_ScreenSizef target_vs = new JCS_ScreenSizef(vs.width * mSwipeArea.x, vs.height * mSwipeArea.y); if (!mFreezeX && posDiff.x > target_vs.width) { if (JCS_Mathf.IsPositive(si.DragDisplacement.x)) { SwitchScene(JCS_2D4Direction.LEFT); } else { SwitchScene(JCS_2D4Direction.RIGHT); } } if (!mFreezeY && posDiff.y > target_vs.height) { if (JCS_Mathf.IsPositive(si.DragDisplacement.y)) { SwitchScene(JCS_2D4Direction.BOTTOM); } else { SwitchScene(JCS_2D4Direction.TOP); } } } }