Remove a pile of unnecessary state management from Scrollpanels.

This commit is contained in:
Paul Chote
2011-01-01 16:47:52 +13:00
parent e2b739cd3e
commit d90dec9c8e
22 changed files with 57 additions and 115 deletions

View File

@@ -19,6 +19,7 @@ namespace OpenRA.Widgets
public readonly string Background = "dialog3"; public readonly string Background = "dialog3";
public readonly int ScrollbarWidth = 24; public readonly int ScrollbarWidth = 24;
public readonly float ScrollVelocity = 4f; public readonly float ScrollVelocity = 4f;
public readonly int ItemSpacing = 2;
public int ContentHeight = 0; public int ContentHeight = 0;
float ListOffset = 0; float ListOffset = 0;
@@ -35,16 +36,24 @@ namespace OpenRA.Widgets
protected ScrollPanelWidget(ScrollPanelWidget other) protected ScrollPanelWidget(ScrollPanelWidget other)
: base(other) : base(other)
{ {
Background = other.Background; throw new NotImplementedException();
upButtonRect = other.upButtonRect; }
downButtonRect = other.downButtonRect;
scrollbarRect = other.scrollbarRect; public void ClearChildren()
backgroundRect = other.backgroundRect; {
thumbRect = other.thumbRect; Children.Clear();
ContentHeight = 0;
}
public override void AddChild(Widget child)
{
// Initial setup of margins/height
if (Children.Count == 0)
ContentHeight = ItemSpacing;
UpPressed = other.UpPressed; child.Bounds.Y += ContentHeight;
DownPressed = other.DownPressed; ContentHeight += child.Bounds.Height + ItemSpacing;
ThumbPressed = other.ThumbPressed; base.AddChild(child);
} }
public override void DrawInner( WorldRenderer wr ) {} public override void DrawInner( WorldRenderer wr ) {}

View File

@@ -287,7 +287,7 @@ namespace OpenRA.Widgets
child.Tick(); child.Tick();
} }
public void AddChild(Widget child) public virtual void AddChild(Widget child)
{ {
child.Parent = this; child.Parent = this;
Children.Add( child ); Children.Add( child );

View File

@@ -293,10 +293,8 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
{ {
// This causes problems for people who are in the process of editing their names (the widgets vanish from beneath them) // This causes problems for people who are in the process of editing their names (the widgets vanish from beneath them)
// Todo: handle this nicer // Todo: handle this nicer
Players.Children.Clear(); Players.ClearChildren();
Players.ContentHeight = 0;
int offset = 0;
foreach (var slot in orderManager.LobbyInfo.Slots) foreach (var slot in orderManager.LobbyInfo.Slots)
{ {
var s = slot; var s = slot;
@@ -446,19 +444,8 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
} }
template.Id = "SLOT_{0}".F(s.Index); template.Id = "SLOT_{0}".F(s.Index);
template.Parent = Players;
template.Bounds = new Rectangle(template.Bounds.X, template.Bounds.Y + offset, template.Bounds.Width, template.Bounds.Height);
template.IsVisible = () => true; template.IsVisible = () => true;
Players.AddChild(template); Players.AddChild(template);
offset += template.Bounds.Height;
// Hack to ensure correct ContentHeight
if (Players.ContentHeight == 0)
Players.ContentHeight += template.Bounds.Y;
Players.ContentHeight += template.Bounds.Height;
} }
} }

View File

@@ -55,7 +55,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
}; };
var itemTemplate = ml.GetWidget<ContainerWidget>("MAP_TEMPLATE"); var itemTemplate = ml.GetWidget<ContainerWidget>("MAP_TEMPLATE");
int offset = itemTemplate.Bounds.Y;
foreach (var kv in Game.modData.AvailableMaps.OrderBy(kv => kv.Value.Title)) foreach (var kv in Game.modData.AvailableMaps.OrderBy(kv => kv.Value.Title))
{ {
var map = kv.Value; var map = kv.Value;
@@ -66,19 +65,10 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
template.Id = "MAP_{0}".F(map.Uid); template.Id = "MAP_{0}".F(map.Uid);
template.GetBackground = () => ((Map == map) ? "dialog2" : null); template.GetBackground = () => ((Map == map) ? "dialog2" : null);
template.OnMouseDown = mi => { Map = map; return true; }; template.OnMouseDown = mi => { Map = map; return true; };
template.Parent = ml;
template.Bounds = new Rectangle(template.Bounds.X, offset, template.Bounds.Width, template.Bounds.Height);
template.IsVisible = () => true; template.IsVisible = () => true;
template.GetWidget<LabelWidget>("TITLE").GetText = () => " " + map.Title; template.GetWidget<LabelWidget>("TITLE").GetText = () => " " + map.Title;
template.GetWidget<LabelWidget>("TYPE").GetText = () => map.Type + " "; template.GetWidget<LabelWidget>("TYPE").GetText = () => map.Type + " ";
ml.AddChild(template); ml.AddChild(template);
offset += template.Bounds.Height;
// Padding hack
if (ml.ContentHeight == 0)
ml.ContentHeight += 2*template.Bounds.Y;
ml.ContentHeight += template.Bounds.Height;
} }
} }
} }

View File

@@ -98,7 +98,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var ml = bg.GetWidget<ScrollPanelWidget>("MUSIC_LIST"); var ml = bg.GetWidget<ScrollPanelWidget>("MUSIC_LIST");
var itemTemplate = ml.GetWidget<LabelWidget>("MUSIC_TEMPLATE"); var itemTemplate = ml.GetWidget<LabelWidget>("MUSIC_TEMPLATE");
int offset = itemTemplate.Bounds.Y;
if (!Rules.Music.Where(m => m.Value.Exists).Any()) if (!Rules.Music.Where(m => m.Value.Exists).Any())
{ {
@@ -122,24 +121,12 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi); bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi);
return true; return true;
}; };
template.Parent = ml;
template.Bounds = new Rectangle(template.Bounds.X, offset, template.Bounds.Width, template.Bounds.Height); template.IsVisible = () => true;
template.IsVisible = () => true;
template.GetWidget<LabelWidget>("TITLE").GetText = () => " " + Rules.Music[song].Title; template.GetWidget<LabelWidget>("TITLE").GetText = () => " " + Rules.Music[song].Title;
template.GetWidget<LabelWidget>("LENGTH").GetText = () => "{0:D1}:{1:D2}".F(Rules.Music[song].Length / 60, Rules.Music[song].Length % 60); template.GetWidget<LabelWidget>("LENGTH").GetText = () => "{0:D1}:{1:D2}".F(Rules.Music[song].Length / 60, Rules.Music[song].Length % 60);
ml.AddChild(template); ml.AddChild(template);
offset += template.Bounds.Height;
// Padding hack
if (ml.ContentHeight == 0)
ml.ContentHeight += 2*template.Bounds.Y;
ml.ContentHeight += template.Bounds.Height;
} }
} }

View File

@@ -40,11 +40,9 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var template = widget.GetWidget<LabelWidget>("REPLAY_TEMPLATE"); var template = widget.GetWidget<LabelWidget>("REPLAY_TEMPLATE");
CurrentReplay = null; CurrentReplay = null;
rl.Children.Clear(); rl.ClearChildren();
rl.ContentHeight = 0;
var offset = template.Bounds.Y;
foreach (var replayFile in Directory.GetFiles(replayDir, "*.rep").Reverse()) foreach (var replayFile in Directory.GetFiles(replayDir, "*.rep").Reverse())
AddReplay(rl, replayFile, template, ref offset); AddReplay(rl, replayFile, template);
widget.GetWidget("WATCH_BUTTON").OnMouseUp = mi => widget.GetWidget("WATCH_BUTTON").OnMouseUp = mi =>
{ {
@@ -100,27 +98,15 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
} }
} }
void AddReplay(ScrollPanelWidget list, string filename, LabelWidget template, ref int offset) void AddReplay(ScrollPanelWidget list, string filename, LabelWidget template)
{ {
var entry = template.Clone() as LabelWidget; var entry = template.Clone() as LabelWidget;
entry.Id = "REPLAY_"; entry.Id = "REPLAY_";
entry.GetText = () => " {0}".F(Path.GetFileName(filename)); entry.GetText = () => " {0}".F(Path.GetFileName(filename));
entry.GetBackground = () => (CurrentReplay == filename) ? "dialog2" : null; entry.GetBackground = () => (CurrentReplay == filename) ? "dialog2" : null;
entry.OnMouseDown = mi => { CurrentReplay = filename; return true; }; entry.OnMouseDown = mi => { CurrentReplay = filename; return true; };
entry.Parent = list;
entry.Bounds = new Rectangle(entry.Bounds.X, offset, template.Bounds.Width, template.Bounds.Height);
entry.IsVisible = () => true; entry.IsVisible = () => true;
list.AddChild(entry); list.AddChild(entry);
if (offset == template.Bounds.Y)
CurrentReplay = filename;
offset += template.Bounds.Height;
// Padding hack
if (list.ContentHeight == 0)
list.ContentHeight += 2*template.Bounds.Y;
list.ContentHeight += template.Bounds.Height;
} }
} }

View File

@@ -139,7 +139,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var sl = bg.GetWidget<ScrollPanelWidget>("SERVER_LIST"); var sl = bg.GetWidget<ScrollPanelWidget>("SERVER_LIST");
sl.Children.Clear(); sl.ClearChildren();
currentServer = null; currentServer = null;
if (games == null) if (games == null)
@@ -161,8 +161,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = false; r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = false;
sl.ContentHeight = 0;
int offset = ServerTemplate.Bounds.Y;
int i = 0; int i = 0;
foreach (var loop in gamesWaiting) foreach (var loop in gamesWaiting)
{ {
@@ -174,20 +172,10 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
game.Address); game.Address);
template.GetBackground = () => (currentServer == game) ? "dialog2" : null; template.GetBackground = () => (currentServer == game) ? "dialog2" : null;
template.OnMouseDown = mi => { currentServer = game; return true; }; template.OnMouseDown = mi => { currentServer = game; return true; };
template.Parent = sl;
template.Bounds = new Rectangle(template.Bounds.X, offset, template.Bounds.Width, template.Bounds.Height);
template.IsVisible = () => true; template.IsVisible = () => true;
sl.AddChild(template); sl.AddChild(template);
if (i == 0) currentServer = game; if (i == 0) currentServer = game;
offset += template.Bounds.Height;
// Padding hack
if (sl.ContentHeight == 0)
sl.ContentHeight += 2*template.Bounds.Y;
sl.ContentHeight += template.Bounds.Height;
i++; i++;
} }
} }

View File

@@ -57,7 +57,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var vl = bg.GetWidget<ScrollPanelWidget>("VIDEO_LIST"); var vl = bg.GetWidget<ScrollPanelWidget>("VIDEO_LIST");
var itemTemplate = vl.GetWidget<LabelWidget>("VIDEO_TEMPLATE"); var itemTemplate = vl.GetWidget<LabelWidget>("VIDEO_TEMPLATE");
int offset = itemTemplate.Bounds.Y;
foreach (var kv in Rules.Movies) foreach (var kv in Rules.Movies)
{ {
@@ -79,14 +78,8 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
player.Load(video); player.Load(video);
return true; return true;
}; };
template.Parent = vl;
template.Bounds = new Rectangle(template.Bounds.X, offset, template.Bounds.Width, template.Bounds.Height);
template.IsVisible = () => true; template.IsVisible = () => true;
vl.AddChild(template); vl.AddChild(template);
offset += template.Bounds.Height;
vl.ContentHeight += template.Bounds.Height;
} }
} }
} }

View File

@@ -32,15 +32,16 @@ Background@SERVER_LOBBY:
Id:PLAYERS Id:PLAYERS
X:20 X:20
Y:67 Y:67
ItemSpacing:5
Width:504 Width:504
Height:235 Height:235
Children: Children:
Container@TEMPLATE_LOCAL: Container@TEMPLATE_LOCAL:
Id:TEMPLATE_LOCAL Id:TEMPLATE_LOCAL
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
TextField@NAME: TextField@NAME:
@@ -109,9 +110,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_REMOTE: Container@TEMPLATE_REMOTE:
Id:TEMPLATE_REMOTE Id:TEMPLATE_REMOTE
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
Label@NAME: Label@NAME:
@@ -181,9 +182,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_EMPTY: Container@TEMPLATE_EMPTY:
Id:TEMPLATE_EMPTY Id:TEMPLATE_EMPTY
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
Label@NAME: Label@NAME:
@@ -212,9 +213,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_EMPTY_HOST: Container@TEMPLATE_EMPTY_HOST:
Id:TEMPLATE_EMPTY_HOST Id:TEMPLATE_EMPTY_HOST
X:5 X:5
Y:5 Y:0
Width:400 Width:400
Height:30 Height:25
Visible:false Visible:false
Children: Children:
DropDownButton@NAME: DropDownButton@NAME:
@@ -448,7 +449,7 @@ Background@MAP_CHOOSER:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Children: Children:
Label@TITLE: Label@TITLE:

View File

@@ -220,7 +220,7 @@ Background@MUSIC_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Children: Children:
Label@TITLE: Label@TITLE:

View File

@@ -28,7 +28,7 @@ Background@REPLAYBROWSER_BG:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Container@REPLAY_INFO: Container@REPLAY_INFO:
Id:REPLAY_INFO Id:REPLAY_INFO

View File

@@ -122,7 +122,7 @@ Background@JOINSERVER_BG:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Label@JOINSERVER_PROGRESS_TITLE: Label@JOINSERVER_PROGRESS_TITLE:
Id:JOINSERVER_PROGRESS_TITLE Id:JOINSERVER_PROGRESS_TITLE

View File

@@ -33,7 +33,7 @@ Background@VIDEOPLAYER_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Button@BUTTON_PLAYPAUSE: Button@BUTTON_PLAYPAUSE:
Id:BUTTON_PLAYPAUSE Id:BUTTON_PLAYPAUSE

View File

@@ -419,7 +419,7 @@ Background@MAP_CHOOSER:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Background@MAPCHOOSER_MAP_BG: Background@MAPCHOOSER_MAP_BG:
X:PARENT_RIGHT-268 X:PARENT_RIGHT-268

View File

@@ -222,7 +222,7 @@ Background@MUSIC_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Children: Children:
Label@TITLE: Label@TITLE:

View File

@@ -124,7 +124,7 @@ Background@JOINSERVER_BG:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Label@JOINSERVER_PROGRESS_TITLE: Label@JOINSERVER_PROGRESS_TITLE:
Id:JOINSERVER_PROGRESS_TITLE Id:JOINSERVER_PROGRESS_TITLE

View File

@@ -34,7 +34,7 @@ Background@VIDEOPLAYER_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Button@BUTTON_PLAYPAUSE: Button@BUTTON_PLAYPAUSE:
Id:BUTTON_PLAYPAUSE Id:BUTTON_PLAYPAUSE

View File

@@ -32,15 +32,16 @@ Background@SERVER_LOBBY:
Id:PLAYERS Id:PLAYERS
X:20 X:20
Y:67 Y:67
ItemSpacing:5
Width:504 Width:504
Height:235 Height:235
Children: Children:
Container@TEMPLATE_LOCAL: Container@TEMPLATE_LOCAL:
Id:TEMPLATE_LOCAL Id:TEMPLATE_LOCAL
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
TextField@NAME: TextField@NAME:
@@ -109,9 +110,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_REMOTE: Container@TEMPLATE_REMOTE:
Id:TEMPLATE_REMOTE Id:TEMPLATE_REMOTE
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
Label@NAME: Label@NAME:
@@ -181,9 +182,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_EMPTY: Container@TEMPLATE_EMPTY:
Id:TEMPLATE_EMPTY Id:TEMPLATE_EMPTY
X:5 X:5
Y:5 Y:0
Width:475 Width:475
Height:30 Height:25
Visible:false Visible:false
Children: Children:
Label@NAME: Label@NAME:
@@ -212,9 +213,9 @@ Background@SERVER_LOBBY:
Container@TEMPLATE_EMPTY_HOST: Container@TEMPLATE_EMPTY_HOST:
Id:TEMPLATE_EMPTY_HOST Id:TEMPLATE_EMPTY_HOST
X:5 X:5
Y:5 Y:0
Width:400 Width:400
Height:30 Height:25
Visible:false Visible:false
Children: Children:
DropDownButton@NAME: DropDownButton@NAME:
@@ -448,7 +449,7 @@ Background@MAP_CHOOSER:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Children: Children:
Label@TITLE: Label@TITLE:

View File

@@ -221,7 +221,7 @@ Background@MUSIC_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Children: Children:
Label@TITLE: Label@TITLE:

View File

@@ -28,7 +28,7 @@ Background@REPLAYBROWSER_BG:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Container@REPLAY_INFO: Container@REPLAY_INFO:
Id:REPLAY_INFO Id:REPLAY_INFO

View File

@@ -122,7 +122,7 @@ Background@JOINSERVER_BG:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Label@JOINSERVER_PROGRESS_TITLE: Label@JOINSERVER_PROGRESS_TITLE:
Id:JOINSERVER_PROGRESS_TITLE Id:JOINSERVER_PROGRESS_TITLE

View File

@@ -33,7 +33,7 @@ Background@VIDEOPLAYER_MENU:
Height:25 Height:25
ClickThrough:false ClickThrough:false
X:2 X:2
Y:2 Y:0
Visible:false Visible:false
Button@BUTTON_PLAYPAUSE: Button@BUTTON_PLAYPAUSE:
Id:BUTTON_PLAYPAUSE Id:BUTTON_PLAYPAUSE