Skip to content

Commit 6fe6594

Browse files
authored
Merge pull request #13 from gwaredd/fix-scrolling
Less nonsense
2 parents 5fcfe3f + 715f9a5 commit 6fe6594

File tree

4 files changed

+41
-106
lines changed

4 files changed

+41
-106
lines changed

Editor/Scripts/Layout/Context.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public Context( GUISkin skin, HandlerImages images, HandlerNavigate navigate )
1818
HandlerImages mImages;
1919
HandlerNavigate mNagivate;
2020

21-
public void SelectPage( string path ) { mNagivate.SelectPage( path ); }
22-
public Texture FetchImage( string url ) { return mImages.FetchImage( url ); }
21+
public void SelectPage( string path ) { mNagivate.SelectPage( path ); }
22+
public Texture FetchImage( string url ) { return mImages.FetchImage( url ); }
2323

24-
public float LineHeight { get { return mStyleGUI.lineHeight; } }
25-
public float MinWidth { get { return LineHeight * 2.0f; } }
26-
public float IndentSize { get { return LineHeight * 1.5f; } }
24+
public float LineHeight { get { return mStyleGUI.lineHeight; } }
25+
public float MinWidth { get { return LineHeight * 2.0f; } }
26+
public float IndentSize { get { return LineHeight * 1.5f; } }
2727

2828
public void Reset() { Apply( Style.Default ); }
2929
public GUIStyle Apply( Style style ) { mStyleGUI = mStyleConverter.Apply( style ); return mStyleGUI; }

Editor/Scripts/Layout/Layout.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
////////////////////////////////////////////////////////////////////////////////
22

3-
using UnityEngine;
4-
53
namespace MG.MDV
64
{
75
public class Layout
86
{
9-
Context mContext;
7+
Context mContext;
108
BlockContainer mDocument;
119

1210
public Layout( Context context, BlockContainer doc )
@@ -25,7 +23,7 @@ public Block Find( string id )
2523
public void Arrange( float maxWidth )
2624
{
2725
mContext.Reset();
28-
mDocument.Arrange( mContext, Vector2.zero, maxWidth );
26+
mDocument.Arrange( mContext, MarkdownViewer.Margin, maxWidth );
2927
}
3028

3129
public void Draw()

Editor/Scripts/MarkdownEditor.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,24 @@ public override bool UseDefaultMargins()
5555
return false;
5656
}
5757

58-
#if UNITY_2020_1_OR_NEWER
59-
public override void OnInspectorGUI()
60-
{
61-
DrawEditor();
62-
}
63-
64-
protected override void OnHeaderGUI()
65-
{
66-
// DrawEditor();
67-
}
68-
#elif UNITY_2019_2_OR_NEWER
69-
// TODO: workaround for bug in 2019.2
70-
// https://forum.unity.com/threads/oninspectorgui-not-being-called-on-defaultasset-in-2019-2-0f1.724328/
7158
protected override void OnHeaderGUI()
7259
{
60+
#if UNITY_2019_2_OR_NEWER && !UNITY_2020_1_OR_NEWER
61+
// TODO: workaround for bug in 2019.2
62+
// https://forum.unity.com/threads/oninspectorgui-not-being-called-on-defaultasset-in-2019-2-0f1.724328/
7363
DrawEditor();
64+
#endif
7465
}
75-
#else
66+
7667
public override void OnInspectorGUI()
7768
{
69+
#if !UNITY_2019_2_OR_NEWER || UNITY_2020_1_OR_NEWER
7870
DrawEditor();
79-
}
8071
#endif
72+
}
8173

8274

83-
//------------------------------------------------------------------------------
75+
//------------------------------------------------------------------------------
8476

8577
private Editor mDefaultEditor;
8678

Editor/Scripts/MarkdownViewer.cs

Lines changed: 26 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ namespace MG.MDV
77
{
88
public class MarkdownViewer
99
{
10+
public static readonly Vector2 Margin = new Vector2( 6.0f, 4.0f );
11+
1012
private GUISkin mSkin = null;
1113
private string mText = string.Empty;
1214
private string mCurrentPath = string.Empty;
1315
private HandlerImages mHandlerImages = new HandlerImages();
1416
private HandlerNavigate mHandlerNavigate = new HandlerNavigate();
1517

16-
private Vector2 mScrollPos = Vector2.zero;
1718
private Layout mLayout = null;
1819
private bool mRaw = false;
1920

@@ -33,7 +34,7 @@ public MarkdownViewer( GUISkin skin, string path, string content )
3334
mHandlerNavigate.CurrentPath = mCurrentPath;
3435
mHandlerNavigate.History = mHistory;
3536
mHandlerNavigate.FindBlock = ( id ) => mLayout.Find( id );
36-
mHandlerNavigate.ScrollTo = ( pos ) => mScrollPos.y = pos;
37+
mHandlerNavigate.ScrollTo = ( pos ) => {}; // TODO: mScrollPos.y = pos;
3738
}
3839

3940

@@ -74,102 +75,52 @@ Layout ParseDocument()
7475

7576
//------------------------------------------------------------------------------
7677

77-
#if !UNITY_2018
78-
private float mHeight = 1.0f;
79-
#endif
80-
81-
protected Rect GetEditorRect()
78+
private void ClearBackground( float height )
8279
{
83-
#if !UNITY_2018
84-
85-
// fix for Screen.height
86-
var offset = 450.0f;
87-
var scale = 2.0f;
88-
89-
// calculate working space
90-
if( Event.current.type == EventType.Layout )
91-
{
92-
mHeight = ( Screen.height - offset ) / scale;
93-
}
94-
95-
// reserve space (we are going to paint on it directly)
96-
GUILayout.Space( mHeight );
97-
98-
// return working rect
99-
return new Rect( 0.0f, 10.0f, EditorGUIUtility.currentViewWidth, mHeight );
100-
#else
101-
GUILayout.FlexibleSpace();
102-
var rectContainer = GUILayoutUtility.GetLastRect();
103-
rectContainer.width = EditorGUIUtility.currentViewWidth;
104-
return rectContainer;
105-
106-
#endif
80+
var rectFullScreen = new Rect( 0.0f, 0.0f, Screen.width, Mathf.Max( height, Screen.height ) );
81+
GUI.DrawTexture( rectFullScreen, mSkin.window.normal.background, ScaleMode.StretchToFill, false );
10782
}
10883

84+
10985
//------------------------------------------------------------------------------
11086

11187
public void Draw()
11288
{
11389
GUI.skin = mSkin;
11490
GUI.enabled = true;
11591

116-
// content rect
117-
118-
var rectContainer = GetEditorRect();
119-
92+
// useable width of inspector windows
12093

121-
// clear background
94+
var contentWidth = EditorGUIUtility.currentViewWidth - mSkin.verticalScrollbar.fixedWidth - 2.0f * Margin.x;
12295

123-
var rectFullScreen = new Rect( 0.0f, rectContainer.yMin - 4.0f, Screen.width, Screen.height );
124-
GUI.DrawTexture( rectFullScreen, GUI.skin.window.normal.background, ScaleMode.StretchToFill, false );
125-
126-
// scroll window
127-
128-
var padLeft = 8.0f;
129-
var padRight = 4.0f;
130-
var padHoriz = padLeft + padRight;
131-
var scrollWidth = GUI.skin.verticalScrollbar.fixedWidth;
132-
var minWidth = rectContainer.width - scrollWidth - padHoriz;
133-
var maxHeight = ContentHeight( minWidth );
134-
135-
var hasScrollbar = maxHeight >= rectContainer.height;
136-
var contentWidth = hasScrollbar ? minWidth : rectContainer.width - padHoriz;
137-
var rectContent = new Rect( -padLeft, 0.0f, contentWidth, maxHeight );
13896

13997
// draw content
14098

141-
using( var scroll = new GUI.ScrollViewScope( rectContainer, mScrollPos, rectContent ) )
99+
if( mRaw )
142100
{
143-
mScrollPos = scroll.scrollPosition;
101+
var style = mSkin.GetStyle( "raw" );
102+
var width = contentWidth - mSkin.button.fixedHeight;
103+
var height = style.CalcHeight( new GUIContent( mText ), width );
144104

145-
if( mRaw )
146-
{
147-
rectContent.width = minWidth - GUI.skin.button.fixedWidth;
148-
DrawRaw( rectContent );
149-
}
150-
else
151-
{
152-
DrawMarkdown( rectContent );
153-
}
105+
ClearBackground( height );
106+
EditorGUILayout.SelectableLabel( mText, style, new GUILayoutOption[] { GUILayout.Width( width ), GUILayout.Height( height ) } );
107+
}
108+
else
109+
{
110+
ClearBackground( mLayout.Height );
111+
DrawMarkdown( contentWidth );
154112
}
155113

156-
DrawToolbar( rectContainer, hasScrollbar ? scrollWidth + padRight : padRight );
157-
}
158-
159-
//------------------------------------------------------------------------------
160-
161-
float ContentHeight( float width )
162-
{
163-
return mRaw ? GUI.skin.GetStyle( "raw" ).CalcHeight( new GUIContent( mText ), width ) : mLayout.Height;
114+
DrawToolbar( contentWidth );
164115
}
165116

166117
//------------------------------------------------------------------------------
167118

168-
void DrawToolbar( Rect rect, float marginRight )
119+
void DrawToolbar( float contentWidth )
169120
{
170121
var style = GUI.skin.button;
171122
var size = style.fixedHeight;
172-
var btn = new Rect( rect.xMax - size - marginRight, rect.yMin, size, size );
123+
var btn = new Rect( Margin.x + contentWidth - size, Margin.y, size, size );
173124

174125
if( GUI.Button( btn, string.Empty, GUI.skin.GetStyle( mRaw ? "btnRaw" : "btnFile" ) ) )
175126
{
@@ -202,14 +153,7 @@ void DrawToolbar( Rect rect, float marginRight )
202153

203154
//------------------------------------------------------------------------------
204155

205-
void DrawRaw( Rect rect )
206-
{
207-
EditorGUI.SelectableLabel( rect, mText, GUI.skin.GetStyle( "raw" ) );
208-
}
209-
210-
//------------------------------------------------------------------------------
211-
212-
void DrawMarkdown( Rect rect )
156+
void DrawMarkdown( float width )
213157
{
214158
switch( Event.current.type )
215159
{
@@ -224,7 +168,8 @@ void DrawMarkdown( Rect rect )
224168
break;
225169

226170
case EventType.Layout:
227-
mLayout.Arrange( rect.width );
171+
GUILayout.Space( mLayout.Height );
172+
mLayout.Arrange( width );
228173
break;
229174

230175
default:

0 commit comments

Comments
 (0)