Skip to content

Commit 94d4810

Browse files
committed
Working on Tabs implementation; stretch toggles (WIP, should rely fully on reserved rect)
1 parent 400ab88 commit 94d4810

2 files changed

Lines changed: 97 additions & 60 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/BeginTabGroupAttributeDrawer.cs

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
44
using UnityEditor;
@@ -142,15 +142,24 @@ protected override void OnGuiBeginSafe(BeginTabGroupAttribute attribute)
142142
if (currentIndex == -1)
143143
currentIndex = 0;
144144

145+
//TODO: GroupId + Optional Label
145146
DrawGroupHeader(attribute.GroupId);
146-
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
147+
148+
//TODO: temp, make it static if needed
149+
var style = new GUIStyle(EditorStyles.helpBox);
150+
style.margin = new RectOffset(0, 0, 0, 0);
151+
style.contentOffset = Vector2.zero;
152+
style.border = new RectOffset(0, 0, 0, 0);
153+
style.padding = new RectOffset(1, 1, 1, 1);
154+
EditorGUILayout.BeginVertical(style);
147155

148156
int newIndex = DrawResponsiveTabs(currentIndex, tabs, attribute.Visual);
149157

150158
if (newIndex != currentIndex)
151159
{
152160
TabState.Set(attribute.GroupId, tabs[newIndex]);
153161

162+
//TODO: is it needed?
154163
GUI.FocusControl(null);
155164
EditorGUIUtility.keyboardControl = 0;
156165
EditorWindow.focusedWindow?.Repaint();
@@ -185,11 +194,7 @@ private static void DrawGroupHeader(string groupId)
185194
GUILayout.Space(TopSpacing);
186195
}
187196

188-
private static int DrawResponsiveTabs(
189-
int currentIndex,
190-
List<string> tabs,
191-
TabGroupVisual visual
192-
)
197+
private static int DrawResponsiveTabs(int currentIndex, List<string> tabs, TabGroupVisual visual)
193198
{
194199
float viewWidth = EditorGUIUtility.currentViewWidth - ViewWidthPadding;
195200

@@ -198,16 +203,13 @@ TabGroupVisual visual
198203

199204
RotateRowsToShowActiveTabLast(rows, currentIndex);
200205

201-
int newIndex = DrawTabRows(rows, currentIndex, tabs, tabWidths, visual);
206+
int newIndex = DrawTabRows(rows, currentIndex, tabs, visual);
202207

203208
return newIndex;
204209
}
205210

206-
private static List<float> CalculateTabWidths(
207-
List<string> tabs,
208-
GUIStyle style,
209-
float viewWidth
210-
)
211+
//TODO: remove it
212+
private static List<float> CalculateTabWidths(List<string> tabs, GUIStyle style, float viewWidth)
211213
{
212214
var tabWidths = new List<float>(tabs.Count);
213215

@@ -297,41 +299,27 @@ private static int FindRowContainingTab(List<List<int>> rows, int tabIndex)
297299
return 0;
298300
}
299301

300-
private static int DrawTabRows(
301-
List<List<int>> rows,
302-
int currentIndex,
303-
List<string> tabs,
304-
List<float> tabWidths,
305-
TabGroupVisual visual
306-
)
302+
private static int DrawTabRows(List<List<int>> rows, int currentIndex, List<string> tabs, TabGroupVisual visual)
307303
{
308304
int newIndex = currentIndex;
309-
310-
EditorGUILayout.BeginVertical();
311-
312-
for (int r = 0; r < rows.Count; r++)
305+
using (new EditorGUILayout.VerticalScope())
313306
{
314-
var row = rows[r];
315-
316-
EditorGUILayout.BeginHorizontal();
317-
GUILayout.FlexibleSpace();
318-
319-
newIndex = DrawTabRow(row, currentIndex, newIndex, tabs, tabWidths, visual);
320-
321-
GUILayout.FlexibleSpace();
322-
EditorGUILayout.EndHorizontal();
307+
for (int r = 0; r < rows.Count; r++)
308+
{
309+
var row = rows[r];
310+
GUILayout.BeginHorizontal();
311+
newIndex = DrawTabButton(row, currentIndex, newIndex, tabs, visual);
312+
GUILayout.EndHorizontal();
313+
}
323314

324-
if (r < rows.Count - 1)
325-
GUILayout.Space(RowSpacing);
315+
GUILayout.Space(RowSpacing);
326316
}
327317

328-
GUILayout.Space(RowSpacing);
329-
EditorGUILayout.EndVertical();
330-
331318
return newIndex;
332319
}
333320

334-
private static int DrawTabRow(
321+
//TODO: old
322+
private static int DrawTabRowOld(
335323
List<int> row,
336324
int currentIndex,
337325
int newIndex,
@@ -382,30 +370,72 @@ TabGroupVisual visual
382370
return newIndex;
383371
}
384372

385-
private static GUIStyle GetStyleForVisual(
386-
TabGroupVisual visual,
387-
int index,
388-
int count,
389-
bool isActive
390-
)
373+
private static int DrawTabButton(List<int> row, int currentIndex, int newIndex, List<string> tabs, TabGroupVisual visual)
374+
{
375+
for (int i = 0; i < row.Count; i++)
376+
{
377+
int tabIndex = row[i];
378+
bool isActive = tabIndex == currentIndex;
379+
var content = new GUIContent(tabs[tabIndex]);
380+
381+
GUIStyle style = GetStyleForVisual(visual, i, row.Count, isActive);
382+
383+
Color prevBg = GUI.backgroundColor;
384+
385+
switch (visual)
386+
{
387+
case TabGroupVisual.Flat:
388+
GUI.backgroundColor = isActive ? ActiveBgColor : GUI.backgroundColor;
389+
break;
390+
391+
case TabGroupVisual.Segmented:
392+
393+
GUI.backgroundColor = isActive
394+
? ActiveBgColor
395+
: GUI.backgroundColor * InactiveBgMultiplier;
396+
break;
397+
398+
default:
399+
GUI.backgroundColor = isActive
400+
? ActiveBgColor
401+
: GUI.backgroundColor * InactiveBgMultiplier;
402+
style = isActive ? ActiveTabStyle : BaseTabStyle;
403+
break;
404+
}
405+
406+
bool pressed = GUILayout.Toggle(isActive, content, style);
407+
GUI.backgroundColor = prevBg;
408+
409+
if (pressed && !isActive)
410+
newIndex = tabIndex;
411+
}
412+
413+
return newIndex;
414+
}
415+
416+
private static GUIStyle GetStyleForVisual(TabGroupVisual visual, int index, int count, bool isActive)
391417
{
392418
switch (visual)
393419
{
394420
case TabGroupVisual.Flat:
395421
return isActive ? FlatActiveStyle : FlatStyle;
396-
397422
case TabGroupVisual.Segmented:
398423
if (count == 1)
424+
{
399425
return isActive ? SegmentActive : SegmentMid;
426+
}
400427

401428
if (index == 0)
429+
{
402430
return SegmentLeft;
431+
}
403432

404433
if (index == count - 1)
434+
{
405435
return SegmentRight;
436+
}
406437

407438
return isActive ? SegmentActive : SegmentMid;
408-
409439
default:
410440
return isActive ? ActiveTabStyle : BaseTabStyle;
411441
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
using System;
1+
using System;
22

33
namespace UnityEngine
44
{
5+
//TODO: move to a separate namespace
56
public enum TabGroupVisual
67
{
78
Default,
8-
Flat, // modern flat buttons
9-
Segmented, // connected segmented control
9+
/// <summary>
10+
/// Modern flat buttons.
11+
/// </summary>
12+
Flat,
13+
/// <summary>
14+
/// Connected segmented control.
15+
/// </summary>
16+
Segmented
1017
}
1118

19+
//TODO: move to a separate scripts
1220
[AttributeUsage(AttributeTargets.Field)]
1321
public sealed class BeginTabGroupAttribute : ToolboxDecoratorAttribute
1422
{
15-
public string GroupId { get; }
16-
public TabGroupVisual Visual { get; }
17-
18-
public BeginTabGroupAttribute(
19-
string groupId = "Default",
20-
TabGroupVisual visual = TabGroupVisual.Default
21-
)
23+
public BeginTabGroupAttribute(string groupId = "Default", TabGroupVisual visual = TabGroupVisual.Default)
2224
{
2325
GroupId = groupId;
2426
Visual = visual;
2527
}
28+
29+
public string GroupId { get; }
30+
31+
public TabGroupVisual Visual { get; }
2632
}
2733

2834
[AttributeUsage(AttributeTargets.Field)]
2935
public sealed class TabAttribute : ToolboxConditionAttribute
3036
{
31-
public string Tab { get; }
32-
3337
public TabAttribute(string tab)
3438
{
3539
Tab = tab;
3640
}
41+
42+
public string Tab { get; }
3743
}
3844

3945
[AttributeUsage(AttributeTargets.Field)]
40-
public sealed class EndTabGroupAttribute : ToolboxDecoratorAttribute { }
46+
public sealed class EndTabGroupAttribute : ToolboxDecoratorAttribute
47+
{ }
4148
}

0 commit comments

Comments
 (0)