@@ -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