factor out layout policy for ScrollPanelWidget; add GridLayout option.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@@ -202,6 +202,8 @@
|
||||
<Compile Include="Widgets\WorldInteractionControllerWidget.cs" />
|
||||
<Compile Include="World.cs" />
|
||||
<Compile Include="WorldUtils.cs" />
|
||||
<Compile Include="Widgets\ListLayout.cs" />
|
||||
<Compile Include="Widgets\GridLayout.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
||||
|
||||
46
OpenRA.Game/Widgets/GridLayout.cs
Normal file
46
OpenRA.Game/Widgets/GridLayout.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.Widgets
|
||||
{
|
||||
public class GridLayout : ILayout
|
||||
{
|
||||
ScrollPanelWidget widget;
|
||||
int2 pos;
|
||||
|
||||
public GridLayout(ScrollPanelWidget w) { widget = w; }
|
||||
|
||||
public void AdjustChild(Widget w)
|
||||
{
|
||||
if (widget.Children.Count == 0)
|
||||
{
|
||||
widget.ContentHeight = widget.ItemSpacing;
|
||||
pos = new int2(widget.ItemSpacing, widget.ItemSpacing);
|
||||
}
|
||||
|
||||
if (pos.X + widget.ItemSpacing + w.Bounds.Width > widget.Bounds.Width - widget.ScrollbarWidth)
|
||||
{
|
||||
/* start a new row */
|
||||
pos.X = widget.ItemSpacing;
|
||||
pos.Y = widget.ContentHeight;
|
||||
}
|
||||
|
||||
w.Bounds.X += pos.X;
|
||||
w.Bounds.Y += pos.Y;
|
||||
|
||||
pos.X += widget.Bounds.Width + widget.ItemSpacing;
|
||||
|
||||
widget.ContentHeight = Math.Max(widget.ContentHeight, pos.Y + widget.ItemSpacing + w.Bounds.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
28
OpenRA.Game/Widgets/ListLayout.cs
Normal file
28
OpenRA.Game/Widgets/ListLayout.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
namespace OpenRA.Widgets
|
||||
{
|
||||
public class ListLayout : ILayout
|
||||
{
|
||||
ScrollPanelWidget widget;
|
||||
|
||||
public ListLayout(ScrollPanelWidget w) { widget = w; }
|
||||
|
||||
public void AdjustChild(Widget w)
|
||||
{
|
||||
if (widget.Children.Count == 0)
|
||||
widget.ContentHeight = widget.ItemSpacing;
|
||||
|
||||
w.Bounds.Y += widget.ContentHeight;
|
||||
widget.ContentHeight += w.Bounds.Height + widget.ItemSpacing;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ using OpenRA.Graphics;
|
||||
|
||||
namespace OpenRA.Widgets
|
||||
{
|
||||
public interface ILayout { void AdjustChild(Widget w); }
|
||||
|
||||
public class ScrollPanelWidget : Widget
|
||||
{
|
||||
public int ScrollbarWidth = 24;
|
||||
@@ -22,6 +24,7 @@ namespace OpenRA.Widgets
|
||||
public int ButtonDepth = ChromeMetrics.Get<int>("ButtonDepth");
|
||||
public string Background = "scrollpanel-bg";
|
||||
public int ContentHeight = 0;
|
||||
public ILayout Layout;
|
||||
protected float ListOffset = 0;
|
||||
protected bool UpPressed = false;
|
||||
protected bool DownPressed = false;
|
||||
@@ -32,12 +35,7 @@ namespace OpenRA.Widgets
|
||||
protected Rectangle scrollbarRect;
|
||||
protected Rectangle thumbRect;
|
||||
|
||||
public ScrollPanelWidget() : base() {}
|
||||
protected ScrollPanelWidget(ScrollPanelWidget other)
|
||||
: base(other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public ScrollPanelWidget() : base() { Layout = new ListLayout(this); }
|
||||
|
||||
public override void RemoveChildren()
|
||||
{
|
||||
@@ -48,11 +46,7 @@ namespace OpenRA.Widgets
|
||||
public override void AddChild(Widget child)
|
||||
{
|
||||
// Initial setup of margins/height
|
||||
if (Children.Count == 0)
|
||||
ContentHeight = ItemSpacing;
|
||||
|
||||
child.Bounds.Y += ContentHeight;
|
||||
ContentHeight += child.Bounds.Height + ItemSpacing;
|
||||
Layout.AdjustChild(child);
|
||||
base.AddChild(child);
|
||||
}
|
||||
|
||||
@@ -192,7 +186,5 @@ namespace OpenRA.Widgets
|
||||
|
||||
return (UpPressed || DownPressed || ThumbPressed);
|
||||
}
|
||||
|
||||
public override Widget Clone() { return new ScrollPanelWidget(this); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace OpenRA.Widgets
|
||||
public Widget Parent = null;
|
||||
public Func<bool> IsVisible;
|
||||
public Widget() { IsVisible = () => Visible; }
|
||||
protected readonly List<Widget> Children = new List<Widget>();
|
||||
public readonly List<Widget> Children = new List<Widget>();
|
||||
|
||||
public Widget(Widget widget)
|
||||
{
|
||||
@@ -283,6 +283,7 @@ namespace OpenRA.Widgets
|
||||
public virtual void MouseEntered() {}
|
||||
public virtual void MouseExited() {}
|
||||
public virtual bool HandleMouseInput(MouseInput mi) { return false; }
|
||||
|
||||
public bool HandleMouseInputOuter(MouseInput mi)
|
||||
{
|
||||
// Are we able to handle this event?
|
||||
@@ -305,6 +306,7 @@ namespace OpenRA.Widgets
|
||||
}
|
||||
|
||||
public virtual bool HandleKeyPress(KeyInput e) { return false; }
|
||||
|
||||
public virtual bool HandleKeyPressOuter(KeyInput e)
|
||||
{
|
||||
if (!IsVisible())
|
||||
@@ -322,6 +324,7 @@ namespace OpenRA.Widgets
|
||||
}
|
||||
|
||||
public virtual void Draw() {}
|
||||
|
||||
public virtual void DrawOuter()
|
||||
{
|
||||
if (IsVisible())
|
||||
@@ -333,6 +336,7 @@ namespace OpenRA.Widgets
|
||||
}
|
||||
|
||||
public virtual void Tick() {}
|
||||
|
||||
public virtual void TickOuter()
|
||||
{
|
||||
if (IsVisible())
|
||||
|
||||
Reference in New Issue
Block a user