Skip to content

Commit 1e558ba

Browse files
committed
Make DialogWindow layout closer to Unity dialog
* Order and space the option buttons similar to Unity dialog * Also display title in the dialog window. Change-Id: I8fd465b777c6217abd70afa9f02d892219421e34
1 parent b05aae9 commit 1e558ba

File tree

1 file changed

+61
-31
lines changed

1 file changed

+61
-31
lines changed

source/VersionHandlerImpl/src/DialogWindow.cs

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,18 @@ public delegate void DisplayDelegate(
141141
/// GUIStyle to render label with word-wrap.
142142
/// </summary>
143143
static public GUIStyle DefaultLabelStyle {
144-
get {
145-
if (defaultLabelStyle != null) {
146-
return defaultLabelStyle;
147-
} else {
148-
defaultLabelStyle = new GUIStyle(EditorStyles.label);
149-
defaultLabelStyle.wordWrap = true;
150-
151-
return defaultLabelStyle;
152-
}
153-
}
144+
get; private set;
154145
}
155-
static private GUIStyle defaultLabelStyle;
146+
147+
/// <summary>
148+
/// GUIStyle to render dialog title.
149+
/// </summary>
150+
static private GUIStyle DefaultTitleStyle;
156151

157152
/// <summary>
158153
/// GUIStyle to render dialog message.
159154
/// </summary>
160-
static private GUIStyle DefaultMessageStyle {
161-
get {
162-
if (defaultMessageStyle != null) {
163-
return defaultMessageStyle;
164-
} else {
165-
defaultMessageStyle = new GUIStyle(EditorStyles.largeLabel);
166-
defaultMessageStyle.fontStyle = FontStyle.Bold;
167-
defaultMessageStyle.wordWrap = true;
168-
169-
return defaultMessageStyle;
170-
}
171-
}
172-
}
173-
static private GUIStyle defaultMessageStyle;
155+
static private GUIStyle DefaultMessageStyle;
174156

175157
// Context for this dialog window.
176158
private DialogContext dialogContext = new DialogContext();
@@ -266,6 +248,28 @@ public static void Display(string title, string message, Option defaultOption,
266248
windowCloseOption, complete, renderContent, renderButtons, init);
267249
}
268250

251+
/// <summary>
252+
/// Initialize GUIStyles used by this dialog.
253+
/// </summary>
254+
void InitializeStyles() {
255+
if (DefaultLabelStyle == null) {
256+
DefaultLabelStyle = new GUIStyle(EditorStyles.label);
257+
DefaultLabelStyle.wordWrap = true;
258+
}
259+
260+
if (DefaultTitleStyle == null) {
261+
DefaultTitleStyle = new GUIStyle(EditorStyles.largeLabel);
262+
DefaultTitleStyle.fontStyle = FontStyle.Bold;
263+
DefaultTitleStyle.wordWrap = true;
264+
}
265+
266+
if (DefaultMessageStyle == null) {
267+
DefaultMessageStyle = new GUIStyle(EditorStyles.boldLabel);
268+
DefaultMessageStyle.wordWrap = true;
269+
}
270+
271+
}
272+
269273
/// <summary>
270274
/// Render the dialog according to the context.
271275
/// </summary>
@@ -283,8 +287,15 @@ void OnGUI() {
283287
}, runNow: false);
284288
}
285289

290+
InitializeStyles();
291+
286292
Rect rect = EditorGUILayout.BeginVertical();
287293

294+
if (!String.IsNullOrEmpty(dialogContext.Title)) {
295+
GUILayout.Label(dialogContext.Title, EditorStyles.boldLabel);
296+
EditorGUILayout.Space();
297+
}
298+
288299
// Render the dialog message.
289300
GUILayout.Label(dialogContext.Message, DefaultMessageStyle);
290301
EditorGUILayout.Space();
@@ -321,12 +332,11 @@ void OnGUI() {
321332
/// Get a list of pairs of the text and enum of each non-empty option.
322333
/// </summary>
323334
/// <return>A list of key-value pair where key is text and value is enum.</return>
324-
private List<KeyValuePair<string, Option>> GetOptionMap() {
335+
private List<KeyValuePair<string, Option>> GetOptionList() {
325336
List<KeyValuePair<string, Option>> options = new List<KeyValuePair<string, Option>>();
326-
if (!String.IsNullOrEmpty (dialogContext.Option0String)) {
327-
options.Add(new KeyValuePair<string, Option>(
328-
dialogContext.Option0String, Option.Selected0));
329-
}
337+
338+
// Order the buttons similar to Unity dialog.
339+
// [ Option 1 (Cancel) ] [ Option 2 (Alt) ] [ Option 0 (Ok) ]
330340
if (!String.IsNullOrEmpty (dialogContext.Option1String)) {
331341
options.Add(new KeyValuePair<string, Option>(
332342
dialogContext.Option1String, Option.Selected1));
@@ -335,6 +345,10 @@ private List<KeyValuePair<string, Option>> GetOptionMap() {
335345
options.Add(new KeyValuePair<string, Option>(
336346
dialogContext.Option2String, Option.Selected2));
337347
}
348+
if (!String.IsNullOrEmpty (dialogContext.Option0String)) {
349+
options.Add(new KeyValuePair<string, Option>(
350+
dialogContext.Option0String, Option.Selected0));
351+
}
338352
return options;
339353
}
340354

@@ -345,8 +359,24 @@ private void RenderOptionButtons () {
345359
Option selected = Option.SelectedNone;
346360

347361
// Render options with non-empty text.
348-
var options = GetOptionMap ();
362+
var options = GetOptionList();
363+
364+
// Space the button similar to Unity dialog. Ex.
365+
// | [ Option 1 (Cancel) ] [ Option 2 (Alt) ] [ Option 0 (Ok) ] |
366+
// | [ Option 1 (Cancel) ] [ Option 0 (Ok) ] |
367+
// | [ Option 0 (Ok) ] |
368+
List<int> spacesBeforeButtons = new List<int>(options.Count == 3 ?
369+
new int[3] { 0, 2, 0 } :
370+
new int[3] { 2, 0, 0 } );
371+
349372
for (int i = 0; i < options.Count; ++i) {
373+
// Place spaces before the button.
374+
if (i < spacesBeforeButtons.Count ) {
375+
for (int j = 0; j < spacesBeforeButtons[i]; ++j) {
376+
EditorGUILayout.Space();
377+
}
378+
}
379+
350380
var pair = options [i];
351381
if (GUILayout.Button(pair.Key)) {
352382
selected = pair.Value;

0 commit comments

Comments
 (0)