Refactor scrollpanel items into their own widget
This commit is contained in:
@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
{
|
{
|
||||||
Map map;
|
Map map;
|
||||||
Widget scrollpanel;
|
Widget scrollpanel;
|
||||||
Widget itemTemplate;
|
ScrollItemWidget itemTemplate;
|
||||||
|
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
internal CncMapChooserLogic([ObjectCreator.Param] Widget widget,
|
internal CncMapChooserLogic([ObjectCreator.Param] Widget widget,
|
||||||
@@ -52,7 +52,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
panel.GetWidget<CncMenuButtonWidget>("BUTTON_INSTALL").OnClick = () => InstallMap();
|
panel.GetWidget<CncMenuButtonWidget>("BUTTON_INSTALL").OnClick = () => InstallMap();
|
||||||
|
|
||||||
scrollpanel = panel.GetWidget<ScrollPanelWidget>("MAP_LIST");
|
scrollpanel = panel.GetWidget<ScrollPanelWidget>("MAP_LIST");
|
||||||
itemTemplate = scrollpanel.GetWidget<ContainerWidget>("MAP_TEMPLATE");
|
itemTemplate = scrollpanel.GetWidget<ScrollItemWidget>("MAP_TEMPLATE");
|
||||||
EnumerateMaps();
|
EnumerateMaps();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,14 +65,11 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
if (!m.Selectable)
|
if (!m.Selectable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var template = itemTemplate.Clone() as ContainerWidget;
|
var item = ScrollItemWidget.Setup(itemTemplate, () => m == map, () => map = m);
|
||||||
template.GetBackground = () => (template.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (m == map) ? "button-pressed" : null);
|
item.GetWidget<LabelWidget>("TITLE").GetText = () => m.Title;
|
||||||
template.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; map = m; return true; };
|
item.GetWidget<LabelWidget>("PLAYERS").GetText = () => "{0}".F(m.PlayerCount);
|
||||||
template.IsVisible = () => true;
|
item.GetWidget<LabelWidget>("TYPE").GetText = () => m.Type;
|
||||||
template.GetWidget<LabelWidget>("TITLE").GetText = () => m.Title;
|
scrollpanel.AddChild(item);
|
||||||
template.GetWidget<LabelWidget>("PLAYERS").GetText = () => "{0}".F(m.PlayerCount);
|
|
||||||
template.GetWidget<LabelWidget>("TYPE").GetText = () => m.Type;
|
|
||||||
scrollpanel.AddChild(template);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -337,5 +337,43 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
ShowDropPanel(w, dropDown, dismissAfter, () => true);
|
ShowDropPanel(w, dropDown, dismissAfter, () => true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ScrollItemWidget : CncMenuButtonWidget
|
||||||
|
{
|
||||||
|
public ScrollItemWidget()
|
||||||
|
: base()
|
||||||
|
{
|
||||||
|
IsVisible = () => false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ScrollItemWidget(ScrollItemWidget other)
|
||||||
|
: base(other)
|
||||||
|
{
|
||||||
|
IsVisible = () => false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Func<bool> IsSelected = () => false;
|
||||||
|
|
||||||
|
public override void DrawInner()
|
||||||
|
{
|
||||||
|
var state = IsSelected() ? "button-pressed" :
|
||||||
|
RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" :
|
||||||
|
null;
|
||||||
|
|
||||||
|
if (state != null)
|
||||||
|
WidgetUtils.DrawPanel(state, RenderBounds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Widget Clone() { return new ScrollItemWidget(this); }
|
||||||
|
|
||||||
|
public static ScrollItemWidget Setup(ScrollItemWidget template, Func<bool> isSelected, Action onClick)
|
||||||
|
{
|
||||||
|
var w = template.Clone() as ScrollItemWidget;
|
||||||
|
w.IsVisible = () => true;
|
||||||
|
w.IsSelected = isSelected;
|
||||||
|
w.OnClick = onClick;
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,19 +58,16 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
currentMod = Mod.AllMods[Game.modData.Manifest.Mods[0]];
|
currentMod = Mod.AllMods[Game.modData.Manifest.Mods[0]];
|
||||||
|
|
||||||
// Mod list
|
// Mod list
|
||||||
var modTemplate = modList.GetWidget("MOD_TEMPLATE");
|
var modTemplate = modList.GetWidget<ScrollItemWidget>("MOD_TEMPLATE");
|
||||||
|
|
||||||
foreach (var m in Mod.AllMods)
|
foreach (var m in Mod.AllMods)
|
||||||
{
|
{
|
||||||
var mod = m.Value;
|
var mod = m.Value;
|
||||||
var template = modTemplate.Clone() as ContainerWidget;
|
var item = ScrollItemWidget.Setup(modTemplate, () => currentMod == mod, () => currentMod = mod);
|
||||||
template.GetBackground = () => (template.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (currentMod == mod) ? "button-pressed" : null);
|
item.GetWidget<LabelWidget>("TITLE").GetText = () => mod.Title;
|
||||||
template.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; currentMod = mod; return true; };
|
item.GetWidget<LabelWidget>("VERSION").GetText = () => mod.Version;
|
||||||
template.IsVisible = () => true;
|
item.GetWidget<LabelWidget>("AUTHOR").GetText = () => mod.Author;
|
||||||
template.GetWidget<LabelWidget>("TITLE").GetText = () => mod.Title;
|
modList.AddChild(item);
|
||||||
template.GetWidget<LabelWidget>("VERSION").GetText = () => mod.Version;
|
|
||||||
template.GetWidget<LabelWidget>("AUTHOR").GetText = () => mod.Author;
|
|
||||||
modList.AddChild(template);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -104,28 +104,18 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
random = music.Shuffle(Game.CosmeticRandom).ToArray();
|
random = music.Shuffle(Game.CosmeticRandom).ToArray();
|
||||||
|
|
||||||
var ml = panel.GetWidget<ScrollPanelWidget>("MUSIC_LIST");
|
var ml = panel.GetWidget<ScrollPanelWidget>("MUSIC_LIST");
|
||||||
var itemTemplate = ml.GetWidget<ContainerWidget>("MUSIC_TEMPLATE");
|
var itemTemplate = ml.GetWidget<ScrollItemWidget>("MUSIC_TEMPLATE");
|
||||||
|
|
||||||
foreach (var s in music)
|
foreach (var s in music)
|
||||||
{
|
{
|
||||||
var song = s;
|
var song = s;
|
||||||
if (currentSong == null)
|
if (currentSong == null)
|
||||||
currentSong = song;
|
currentSong = song;
|
||||||
|
|
||||||
var template = itemTemplate.Clone() as ContainerWidget;
|
var item = ScrollItemWidget.Setup(itemTemplate, () => currentSong == song, () => { currentSong = song; Play(); });
|
||||||
template.GetBackground = () => (template.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (song == currentSong) ? "button-pressed" : null);
|
item.GetWidget<LabelWidget>("TITLE").GetText = () => Rules.Music[song].Title;
|
||||||
template.OnMouseDown = mi =>
|
item.GetWidget<LabelWidget>("LENGTH").GetText = () => SongLengthLabel(song);
|
||||||
{
|
ml.AddChild(item);
|
||||||
if (mi.Button != MouseButton.Left) return false;
|
|
||||||
currentSong = song;
|
|
||||||
Play();
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template.IsVisible = () => true;
|
|
||||||
template.GetWidget<LabelWidget>("TITLE").GetText = () => Rules.Music[song].Title;
|
|
||||||
template.GetWidget<LabelWidget>("LENGTH").GetText = () => SongLengthLabel(song);
|
|
||||||
ml.AddChild(template);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
var rl = panel.GetWidget<ScrollPanelWidget>("REPLAY_LIST");
|
var rl = panel.GetWidget<ScrollPanelWidget>("REPLAY_LIST");
|
||||||
var replayDir = Path.Combine(Platform.SupportDir, "Replays");
|
var replayDir = Path.Combine(Platform.SupportDir, "Replays");
|
||||||
|
|
||||||
var template = panel.GetWidget("REPLAY_TEMPLATE");
|
var template = panel.GetWidget<ScrollItemWidget>("REPLAY_TEMPLATE");
|
||||||
|
|
||||||
rl.RemoveChildren();
|
rl.RemoveChildren();
|
||||||
if (Directory.Exists(replayDir))
|
if (Directory.Exists(replayDir))
|
||||||
@@ -92,15 +92,14 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddReplay(ScrollPanelWidget list, string filename, Widget template)
|
void AddReplay(ScrollPanelWidget list, string filename, ScrollItemWidget template)
|
||||||
{
|
{
|
||||||
var entry = template.Clone() as ContainerWidget;
|
var item = ScrollItemWidget.Setup(template,
|
||||||
|
() => currentSummary != null && currentSummary.Filename == filename,
|
||||||
|
() => SelectReplay(filename));
|
||||||
var f = Path.GetFileName(filename);
|
var f = Path.GetFileName(filename);
|
||||||
entry.GetWidget<LabelWidget>("TITLE").GetText = () => f;
|
item.GetWidget<LabelWidget>("TITLE").GetText = () => f;
|
||||||
entry.GetBackground = () => (entry.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (currentSummary != null && currentSummary.Filename == filename) ? "button-pressed" : null);
|
list.AddChild(item);
|
||||||
entry.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; SelectReplay(filename); return true; };
|
|
||||||
entry.IsVisible = () => true;
|
|
||||||
list.AddChild(entry);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
public class CncServerBrowserLogic : IWidgetDelegate
|
public class CncServerBrowserLogic : IWidgetDelegate
|
||||||
{
|
{
|
||||||
GameServer currentServer;
|
GameServer currentServer;
|
||||||
Widget serverTemplate;
|
ScrollItemWidget serverTemplate;
|
||||||
bool refreshing;
|
bool refreshing;
|
||||||
enum SearchStatus
|
enum SearchStatus
|
||||||
{
|
{
|
||||||
@@ -120,7 +120,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
panel.GetWidget<CncMenuButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
|
panel.GetWidget<CncMenuButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
|
||||||
|
|
||||||
// Server list
|
// Server list
|
||||||
serverTemplate = sl.GetWidget("SERVER_TEMPLATE");
|
serverTemplate = sl.GetWidget<ScrollItemWidget>("SERVER_TEMPLATE");
|
||||||
|
|
||||||
// Display the progress label over the server list
|
// Display the progress label over the server list
|
||||||
// The text is only visible when the list is empty
|
// The text is only visible when the list is empty
|
||||||
@@ -194,17 +194,14 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
{
|
{
|
||||||
var game = loop;
|
var game = loop;
|
||||||
|
|
||||||
var template = serverTemplate.Clone() as ContainerWidget;
|
var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game);
|
||||||
template.GetBackground = () => (template.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (currentServer == game) ? "button-pressed" : null);
|
item.GetWidget<LabelWidget>("TITLE").GetText = () => game.Name;
|
||||||
template.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; currentServer = game; return true; };
|
|
||||||
template.IsVisible = () => true;
|
|
||||||
template.GetWidget<LabelWidget>("TITLE").GetText = () => game.Name;
|
|
||||||
// TODO: Use game.MapTitle once the server supports it
|
// TODO: Use game.MapTitle once the server supports it
|
||||||
template.GetWidget<LabelWidget>("MAP").GetText = () => {var map = GetMap(game.Map); return map == null ? "Unknown" : map.Title;};
|
item.GetWidget<LabelWidget>("MAP").GetText = () => {var map = GetMap(game.Map); return map == null ? "Unknown" : map.Title;};
|
||||||
// TODO: Use game.MaxPlayers once the server supports it
|
// TODO: Use game.MaxPlayers once the server supports it
|
||||||
template.GetWidget<LabelWidget>("PLAYERS").GetText = () => GetPlayersLabel(game);
|
item.GetWidget<LabelWidget>("PLAYERS").GetText = () => GetPlayersLabel(game);
|
||||||
template.GetWidget<LabelWidget>("IP").GetText = () => game.Address;
|
item.GetWidget<LabelWidget>("IP").GetText = () => game.Address;
|
||||||
sl.AddChild(template);
|
sl.AddChild(item);
|
||||||
|
|
||||||
if (i == 0) currentServer = game;
|
if (i == 0) currentServer = game;
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
@@ -38,13 +38,12 @@ Container@MAPCHOOSER_PANEL:
|
|||||||
Width:504
|
Width:504
|
||||||
Height:455
|
Height:455
|
||||||
Children:
|
Children:
|
||||||
Container@MAP_TEMPLATE:
|
ScrollItem@MAP_TEMPLATE:
|
||||||
Id:MAP_TEMPLATE
|
Id:MAP_TEMPLATE
|
||||||
Width:PARENT_RIGHT-27
|
Width:PARENT_RIGHT-27
|
||||||
Height:25
|
Height:25
|
||||||
X:2
|
X:2
|
||||||
Y:0
|
Y:0
|
||||||
Visible:false
|
|
||||||
Children:
|
Children:
|
||||||
Label@TITLE:
|
Label@TITLE:
|
||||||
X:10
|
X:10
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ Container@MODS_PANEL:
|
|||||||
Width:710
|
Width:710
|
||||||
Height:455
|
Height:455
|
||||||
Children:
|
Children:
|
||||||
Container@MOD_TEMPLATE:
|
ScrollItem@MOD_TEMPLATE:
|
||||||
Id:MOD_TEMPLATE
|
Id:MOD_TEMPLATE
|
||||||
Width:PARENT_RIGHT-27
|
Width:PARENT_RIGHT-27
|
||||||
Height:25
|
Height:25
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ Container@MUSIC_PANEL:
|
|||||||
Width:330
|
Width:330
|
||||||
Height:275
|
Height:275
|
||||||
Children:
|
Children:
|
||||||
Container@MUSIC_TEMPLATE:
|
ScrollItem@MUSIC_TEMPLATE:
|
||||||
Id:MUSIC_TEMPLATE
|
Id:MUSIC_TEMPLATE
|
||||||
Width:PARENT_RIGHT-27
|
Width:PARENT_RIGHT-27
|
||||||
Height:25
|
Height:25
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ Container@REPLAYBROWSER_PANEL:
|
|||||||
Width:286
|
Width:286
|
||||||
Height:270
|
Height:270
|
||||||
Children:
|
Children:
|
||||||
Container@REPLAY_TEMPLATE:
|
ScrollItem@REPLAY_TEMPLATE:
|
||||||
Id:REPLAY_TEMPLATE
|
Id:REPLAY_TEMPLATE
|
||||||
Width:PARENT_RIGHT-27
|
Width:PARENT_RIGHT-27
|
||||||
Height:25
|
Height:25
|
||||||
|
|||||||
@@ -25,13 +25,12 @@ Container@SERVERBROWSER_PANEL:
|
|||||||
Width:710
|
Width:710
|
||||||
Height:315
|
Height:315
|
||||||
Children:
|
Children:
|
||||||
Container@SERVER_TEMPLATE:
|
ScrollItem@SERVER_TEMPLATE:
|
||||||
Id:SERVER_TEMPLATE
|
Id:SERVER_TEMPLATE
|
||||||
Width:PARENT_RIGHT-27
|
Width:PARENT_RIGHT-27
|
||||||
Height:25
|
Height:25
|
||||||
X:2
|
X:2
|
||||||
Y:0
|
Y:0
|
||||||
Visible:false
|
|
||||||
Children:
|
Children:
|
||||||
Label@TITLE:
|
Label@TITLE:
|
||||||
X:10
|
X:10
|
||||||
|
|||||||
Reference in New Issue
Block a user