@@ -79,7 +79,7 @@ void OnMouseDown(MouseDownEvent e)
7979 target . RegisterCallback < MouseMoveEvent > ( OnMouseMove ) ;
8080 e . StopPropagation ( ) ;
8181 target . CaptureMouse ( ) ;
82- m_StartMouse = resizedBase . WorldToLocal ( e . mousePosition ) ;
82+ m_StartMouse = e . mousePosition ;
8383 m_StartSize = new Vector2 ( resizedTarget . resolvedStyle . width , resizedTarget . resolvedStyle . height ) ;
8484 m_StartPosition = new Vector2 ( resizedTarget . resolvedStyle . left , resizedTarget . resolvedStyle . top ) ;
8585
@@ -104,8 +104,10 @@ void OnMouseMove(MouseMoveEvent e)
104104
105105 VisualElement resizedTarget = resizedElement . parent ;
106106 VisualElement resizedBase = resizedTarget . parent ;
107+ var validResizeBase = resizedBase ;
108+ FindValidBaseElement ( ref validResizeBase ) ;
107109
108- Vector2 mousePos = resizedBase . WorldToLocal ( e . mousePosition ) ;
110+ Vector2 mousePos = e . mousePosition ;
109111 if ( ! m_DragStarted )
110112 {
111113 if ( resizedTarget is IResizable )
@@ -149,27 +151,48 @@ void OnMouseMove(MouseMoveEvent e)
149151 {
150152 if ( ( direction & ResizerDirection . Right ) != 0 )
151153 {
152- resizedTarget . style . width = Mathf . Clamp ( m_StartSize . x + mousePos . x - m_StartMouse . x , m_MinSize . x , Mathf . Min ( m_MaxSize . x , resizedBase . layout . xMax - resizedTarget . layout . xMin ) ) ;
154+ var worldZero = validResizeBase . LocalToWorld ( Vector2 . zero ) ;
155+ var localZero = resizedBase . WorldToLocal ( worldZero ) ;
156+ var localWidth = validResizeBase . layout . width / resizedTarget . worldTransform . lossyScale . x ;
157+ var newWidth = Mathf . Min ( m_StartSize . x + ( mousePos . x - m_StartMouse . x ) / resizedTarget . worldTransform . lossyScale . x , this . m_MaxSize . x ) ;
158+ var newRight = Mathf . Clamp ( resizedTarget . layout . xMin + newWidth , resizedTarget . layout . xMin + m_MinSize . x , localZero . x + localWidth ) ;
159+ resizedTarget . style . width = newRight - resizedTarget . layout . xMin ;
153160 }
154161 else if ( ( direction & ResizerDirection . Left ) != 0 )
155162 {
156- float delta = mousePos . x - m_StartMouse . x ;
157- float previousLeft = resizedTarget . style . left . value . value ;
158-
159- resizedTarget . style . left = Mathf . Clamp ( delta + m_StartPosition . x , 0 , resizedTarget . layout . xMax - m_MinSize . x ) ;
160- resizedTarget . style . width = resizedTarget . resolvedStyle . width + previousLeft - resizedTarget . style . left . value . value ;
163+ var worldZero = validResizeBase . LocalToWorld ( Vector2 . zero ) ;
164+ var localZero = resizedBase . WorldToLocal ( worldZero ) ;
165+ var delta = ( mousePos . x - m_StartMouse . x ) / resizedTarget . worldTransform . lossyScale . x ;
166+ var newLeft = Mathf . Clamp ( m_StartPosition . x + delta , localZero . x , localZero . x + validResizeBase . layout . width ) ;
167+ var newWidth = resizedTarget . layout . xMax - newLeft ;
168+ if ( newWidth >= m_MinSize . x && newWidth <= m_MaxSize . x )
169+ {
170+ resizedTarget . style . left = newLeft ;
171+ resizedTarget . style . width = newWidth ;
172+ }
161173 }
162174 if ( ( direction & ResizerDirection . Bottom ) != 0 )
163175 {
164- resizedTarget . style . height = Mathf . Min ( m_MaxSize . y , Mathf . Max ( m_MinSize . y , m_StartSize . y + mousePos . y - m_StartMouse . y ) ) ;
176+ var worldZero = validResizeBase . LocalToWorld ( Vector2 . zero ) ;
177+ var localZero = resizedBase . WorldToLocal ( worldZero ) ;
178+ var localHeight = validResizeBase . layout . height / resizedTarget . worldTransform . lossyScale . x ;
179+ var newHeight = m_StartSize . y + ( mousePos . y - m_StartMouse . y ) / resizedTarget . worldTransform . lossyScale . x ;
180+ var newBottom = Mathf . Clamp ( resizedTarget . layout . yMin + newHeight , resizedTarget . layout . yMin + m_MinSize . y , localZero . y + localHeight ) ;
181+ resizedTarget . style . height = newBottom - resizedTarget . layout . yMin ;
165182 }
166183 else if ( ( direction & ResizerDirection . Top ) != 0 )
167184 {
168- float delta = mousePos . y - m_StartMouse . y ;
169- float previousTop = resizedTarget . style . top . value . value ;
170-
171- resizedTarget . style . top = Mathf . Clamp ( delta + m_StartPosition . y , 0 , m_StartSize . y - 1 ) ;
172- resizedTarget . style . height = resizedTarget . resolvedStyle . height + previousTop - resizedTarget . style . top . value . value ;
185+ var worldZero = validResizeBase . LocalToWorld ( Vector2 . zero ) ;
186+ var localZero = resizedBase . WorldToLocal ( worldZero ) ;
187+ var localHeight = validResizeBase . layout . height / resizedTarget . worldTransform . lossyScale . x ;
188+ var delta = ( mousePos . y - m_StartMouse . y ) / resizedTarget . worldTransform . lossyScale . x ;
189+ var newTop = Mathf . Clamp ( m_StartPosition . y + delta , localZero . y , localZero . y + localHeight ) ;
190+ var newHeight = resizedTarget . layout . yMax - newTop ;
191+ if ( newHeight >= m_MinSize . y && newHeight <= m_MaxSize . y )
192+ {
193+ resizedTarget . style . top = newTop ;
194+ resizedTarget . style . height = newHeight ;
195+ }
173196 }
174197 }
175198 e . StopPropagation ( ) ;
@@ -195,5 +218,20 @@ void OnMouseUp(MouseUpEvent e)
195218 m_Active = false ;
196219 }
197220 }
221+
222+ void FindValidBaseElement ( ref VisualElement resizedBase )
223+ {
224+ // For unknown reason, layers have zero height, which completely break resizing algorithm
225+ // So we look for a parent with proper dimension
226+ while ( resizedBase . layout . width == 0 || resizedBase . layout . height == 0 )
227+ {
228+ if ( resizedBase . parent == null )
229+ {
230+ break ;
231+ }
232+
233+ resizedBase = resizedBase . parent ;
234+ }
235+ }
198236 }
199237}
0 commit comments