Skip to content

Commit eccb8f2

Browse files
committed
Feat: complete dropdown modules
1 parent 0137d70 commit eccb8f2

File tree

8 files changed

+453
-51
lines changed

8 files changed

+453
-51
lines changed

Assets/JCSUnity/Scripts/JCS_Camera.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ protected virtual void Start()
123123

124124
// add to on screen resize callback.
125125
var screens = JCS_ScreenSettings.instance;
126-
screens.onScreenResize += OnScreenResize;
127-
screens.onScreenIdle += OnScreenIdle;
126+
127+
screens.onResizableResize += OnResizableResize;
128+
screens.onResizableIdle += OnResizableIdle;
128129
}
129130

130131
protected virtual void Update()
@@ -658,7 +659,7 @@ public void SetPosition(Vector3 vec)
658659
/// <summary>
659660
/// Callback when screen not resizing.
660661
/// </summary>
661-
protected virtual void OnScreenIdle()
662+
protected virtual void OnResizableIdle()
662663
{
663664
if (mLastVerticalFOV != mCamera.fieldOfView)
664665
{
@@ -669,7 +670,7 @@ protected virtual void OnScreenIdle()
669670
/// <summary>
670671
/// Resize the game if screen size changes.
671672
/// </summary>
672-
protected virtual void OnScreenResize()
673+
protected virtual void OnResizableResize()
673674
{
674675
if (mCamera.orthographic)
675676
OnResizeOrthographic();

Assets/JCSUnity/Scripts/Settings/JCS_ScreenSettings.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright © 2018 by Shen, Jen-Chieh $
88
*/
9+
using System;
910
using UnityEngine;
1011
using MyBox;
1112

@@ -18,8 +19,16 @@ public class JCS_ScreenSettings : JCS_Settings<JCS_ScreenSettings>
1819
{
1920
/* Variables */
2021

21-
public EmptyFunction onScreenResize = null;
22-
public EmptyFunction onScreenIdle = null;
22+
public Action onChanged = null;
23+
public Action onChangedResolution = null;
24+
public Action onChangedMode = null;
25+
26+
public Action onResizableResize = null;
27+
public Action onResizableIdle = null;
28+
29+
private Resolution mPrevResolution = default(Resolution);
30+
31+
private FullScreenMode mPrevScreenMode = FullScreenMode.FullScreenWindow;
2332

2433
#if UNITY_EDITOR
2534
[Separator("Helper Variables (JCS_ScreenManager)")]
@@ -148,6 +157,10 @@ private void Awake()
148157
if (RESIZE_TO_ASPECT_EVERYTIME_SCENE_LOADED)
149158
ForceAspectScreenOnce();
150159
}
160+
161+
// Initialize.
162+
mPrevResolution = Screen.currentResolution;
163+
mPrevScreenMode = Screen.fullScreenMode;
151164
}
152165

153166
private void Start()
@@ -165,9 +178,41 @@ private void Start()
165178

166179
private void LateUpdate()
167180
{
181+
OnChanged();
182+
168183
DoScreenType();
169184
}
170185

186+
private void OnChanged()
187+
{
188+
Resolution currentResolution = Screen.currentResolution;
189+
FullScreenMode fullScreenMode = Screen.fullScreenMode;
190+
191+
bool resolutionChanged =
192+
mPrevResolution.width != currentResolution.width ||
193+
mPrevResolution.height != currentResolution.height;
194+
195+
bool modeChanged = mPrevScreenMode != fullScreenMode;
196+
197+
if (resolutionChanged)
198+
{
199+
onChangedResolution?.Invoke();
200+
}
201+
202+
if (modeChanged)
203+
{
204+
onChangedMode?.Invoke();
205+
}
206+
207+
if (modeChanged || resolutionChanged)
208+
{
209+
onChanged?.Invoke();
210+
211+
mPrevResolution = currentResolution;
212+
mPrevScreenMode = fullScreenMode;
213+
}
214+
}
215+
171216
/// <summary>
172217
/// Return true, if we should use resizalbe panels.
173218
/// </summary>
@@ -438,7 +483,8 @@ private void DoResizableScreen()
438483

439484
if (CURRENT_SCREEN_SIZE.width == width && CURRENT_SCREEN_SIZE.height == height)
440485
{
441-
if (onScreenIdle != null) onScreenIdle.Invoke();
486+
if (onResizableIdle != null)
487+
onResizableIdle.Invoke();
442488
return;
443489
}
444490

@@ -460,7 +506,8 @@ private void DoResizableScreen()
460506
CURRENT_SCREEN_SIZE.height = height;
461507

462508
// Do callback.
463-
if (onScreenResize != null) onScreenResize.Invoke();
509+
if (onResizableResize != null)
510+
onResizableResize.Invoke();
464511
}
465512
}
466513
}

Assets/JCSUnity/Scripts/UI/Dropdown/JCS_DropdownScreenResolution.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using UnityEngine;
33
using TMPro;
44
using MyBox;
5+
using System.Linq;
6+
57

68
#if UNITY_EDITOR
79
using UnityEditor;
@@ -21,17 +23,12 @@ public class JCS_DropdownScreenResolution : MonoBehaviour
2123

2224
[Separator("Initialize Variables (JCS_DropdownScreenResolution)")]
2325

24-
[Tooltip("A list of resolutions to use.")]
25-
[SerializeField]
26-
private List<string> mResolutions = null;
27-
2826
[Tooltip("If true, remove all other options at the beginning.")]
2927
[SerializeField]
3028
private bool mRemoveAllOptions = true;
3129

3230
/* Setter & Getter */
3331

34-
public List<string> Resolutions { get { return mResolutions; } set { this.mResolutions = value; } }
3532
public bool RemoveAllOptions { get { return mRemoveAllOptions; } set { this.mRemoveAllOptions = value; } }
3633

3734
/* Functions */
@@ -45,6 +42,13 @@ private void Awake()
4542
AddListener();
4643
}
4744

45+
private void Start()
46+
{
47+
var screens = JCS_ScreenSettings.instance;
48+
49+
screens.onChangedResolution += Refresh;
50+
}
51+
4852
private void AddListener()
4953
{
5054
mDropdown.onValueChanged.AddListener(delegate
@@ -64,14 +68,20 @@ public void Refresh()
6468
if (mRemoveAllOptions)
6569
mDropdown.ClearOptions();
6670

67-
foreach (string resolution in mResolutions)
71+
foreach (Resolution res in Screen.resolutions.Reverse())
6872
{
69-
JCS_UIUtil.Dropdown_AddOption(mDropdown, resolution);
73+
string text = FormatName(res.width, res.height);
74+
75+
int index = JCS_UIUtil.Dropdown_GetItemIndex(mDropdown, text);
76+
77+
// Prevent adding the same options.
78+
if (index == -1)
79+
JCS_UIUtil.Dropdown_AddOption(mDropdown, text);
7080
}
7181

7282
// Default to the current screen resolution.
7383
{
74-
string res = Screen.width + "x" + Screen.height;
84+
string res = FormatName(Screen.width, Screen.height);
7585

7686
JCS_UIUtil.Dropdown_SetSelection(mDropdown, res);
7787
}
@@ -97,5 +107,10 @@ private void OnValueChanged(TMP_Dropdown dropdown)
97107
text);
98108
#endif
99109
}
110+
111+
private string FormatName(int width, int height)
112+
{
113+
return width + "x" + height;
114+
}
100115
}
101116
}

Assets/JCSUnity/Scripts/UI/Dropdown/JCS_DropdownWindowedMode.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using UnityEngine;
33
using TMPro;
44
using MyBox;
5-
using UnityEditor;
65

76
namespace JCSUnity
87
{
@@ -16,17 +15,14 @@ public class JCS_DropdownWindowedMode : MonoBehaviour
1615

1716
private TMP_Dropdown mDropdown = null;
1817

19-
[Separator("Initialize Variables (JCS_DropdownWindowedMode)")]
20-
21-
[Tooltip("A list of windowed option to use.")]
22-
[SerializeField]
23-
[ReadOnly]
2418
private List<string> mOptions = new List<string>()
2519
{
2620
"Full Screen",
2721
"Windowed",
2822
};
2923

24+
[Separator("Initialize Variables (JCS_DropdownWindowedMode)")]
25+
3026
[Tooltip("If true, remove all other options at the beginning.")]
3127
[SerializeField]
3228
private bool mRemoveAllOptions = true;
@@ -47,6 +43,13 @@ private void Awake()
4743
AddListener();
4844
}
4945

46+
private void Start()
47+
{
48+
var screens = JCS_ScreenSettings.instance;
49+
50+
screens.onChangedMode += Refresh;
51+
}
52+
5053
private void AddListener()
5154
{
5255
mDropdown.onValueChanged.AddListener(delegate
@@ -99,11 +102,9 @@ private string ModeToString(FullScreenMode mode)
99102
{
100103
case FullScreenMode.FullScreenWindow:
101104
return "Full Screen";
102-
case FullScreenMode.Windowed:
105+
default:
103106
return "Windowed";
104107
}
105-
106-
return JCS_UIUtil.Dropdown_GetSelectedValue(mDropdown);
107108
}
108109

109110
private FullScreenMode StringToMode(string text)

0 commit comments

Comments
 (0)