Goodbye hardcoded chrome!
This commit is contained in:
@@ -30,16 +30,11 @@ namespace OpenRA
|
||||
{
|
||||
class Chrome : IHandleInput
|
||||
{
|
||||
//todo: remove when have a real chat widget
|
||||
public static int ChatWidth = 760;
|
||||
|
||||
public readonly Renderer renderer;
|
||||
public readonly LineRenderer lineRenderer;
|
||||
|
||||
SpriteRenderer rgbaRenderer { get { return renderer.RgbaSpriteRenderer; } }
|
||||
SpriteRenderer shpRenderer { get { return renderer.WorldSpriteRenderer; } }
|
||||
|
||||
readonly List<Pair<RectangleF, Action<bool>>> buttons = new List<Pair<RectangleF, Action<bool>>>();
|
||||
|
||||
internal MapStub currentMap;
|
||||
|
||||
@@ -70,67 +65,10 @@ namespace OpenRA
|
||||
if (!world.GameHasStarted) return;
|
||||
if (world.LocalPlayer == null) return;
|
||||
++ticksSinceLastMove;
|
||||
}
|
||||
|
||||
public void Draw( World world )
|
||||
{
|
||||
buttons.Clear();
|
||||
renderer.Device.DisableScissor();
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawMapChooser()
|
||||
{
|
||||
buttons.Clear();
|
||||
|
||||
var w = 800;
|
||||
var h = 600;
|
||||
var r = new Rectangle( (Game.viewport.Width - w) / 2, (Game.viewport.Height - h) / 2, w, h );
|
||||
var y = r.Top + 50;
|
||||
|
||||
// Don't bother showing a subset of the data
|
||||
// This will be fixed properly when we move the map list to widgets
|
||||
foreach (var kv in Game.AvailableMaps)
|
||||
{
|
||||
var map = kv.Value;
|
||||
if (!map.Selectable)
|
||||
continue;
|
||||
|
||||
var itemRect = new Rectangle(r.Left + 50, y - 2, r.Width - 340, 20);
|
||||
if (map == currentMap)
|
||||
{
|
||||
rgbaRenderer.Flush();
|
||||
DrawDialogBackground(itemRect, "dialog2");
|
||||
}
|
||||
|
||||
renderer.RegularFont.DrawText(map.Title, new int2(r.Left + 60, y), Color.White);
|
||||
rgbaRenderer.Flush();
|
||||
var closureMap = map;
|
||||
AddButton(itemRect, _ => { currentMap = closureMap; });
|
||||
y += 20;
|
||||
}
|
||||
AddButton(r, _ => { });
|
||||
}
|
||||
public void Draw(World world) { rootWidget.Draw(world); shpRenderer.Flush(); rgbaRenderer.Flush(); lineRenderer.Flush(); }
|
||||
|
||||
|
||||
public void DrawWidgets(World world) { rootWidget.Draw(world); shpRenderer.Flush(); rgbaRenderer.Flush(); lineRenderer.Flush(); }
|
||||
|
||||
public void DrawLobby()
|
||||
{
|
||||
buttons.Clear();
|
||||
|
||||
if( Game.LobbyInfo.GlobalSettings.Map == null )
|
||||
currentMap = null;
|
||||
else
|
||||
currentMap = Game.AvailableMaps[ Game.LobbyInfo.GlobalSettings.Map ];
|
||||
}
|
||||
|
||||
void AddButton(RectangleF r, Action<bool> b) { buttons.Add(Pair.New(r, b)); }
|
||||
|
||||
void DrawDialogBackground(Rectangle r, string collection)
|
||||
{
|
||||
WidgetUtils.DrawPanel(collection, r);
|
||||
}
|
||||
|
||||
public int ticksSinceLastMove = 0;
|
||||
public int2 lastMousePos;
|
||||
public bool HandleInput(World world, MouseInput mi)
|
||||
@@ -146,16 +84,6 @@ namespace OpenRA
|
||||
lastMousePos = mi.Location;
|
||||
ticksSinceLastMove = 0;
|
||||
}
|
||||
|
||||
var action = buttons.Where(a => a.First.Contains(mi.Location.ToPoint()))
|
||||
.Select(a => a.Second).FirstOrDefault();
|
||||
|
||||
if (action == null)
|
||||
return false;
|
||||
|
||||
if (mi.Event == MouseInputEvent.Down)
|
||||
action(mi.Button == MouseButton.Left);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -175,13 +103,7 @@ namespace OpenRA
|
||||
if (selectedWidget != null)
|
||||
return true;
|
||||
|
||||
return rootWidget.HitTest(mousePos)
|
||||
|| buttons.Any(a => a.First.Contains(mousePos.ToPoint()));
|
||||
}
|
||||
|
||||
void DrawCentered(string text, int2 pos, Color c)
|
||||
{
|
||||
renderer.BoldFont.DrawText(text, pos - new int2(renderer.BoldFont.Measure(text).X / 2, 0), c);
|
||||
return rootWidget.HitTest(mousePos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,9 +86,6 @@ namespace OpenRA.Graphics
|
||||
Chrome.rootWidget.OpenWindow("INGAME_ROOT");
|
||||
gameWasStarted = true;
|
||||
}
|
||||
|
||||
Game.chrome.Draw( world );
|
||||
Timer.Time( "chromedraw: {0}" );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -131,12 +128,7 @@ namespace OpenRA.Graphics
|
||||
|
||||
Timer.Time( "connectionState: {0}" );
|
||||
}
|
||||
Game.chrome.DrawWidgets(world);
|
||||
if (Chrome.rootWidget.GetWidget("SERVER_LOBBY").Visible)
|
||||
Game.chrome.DrawLobby();
|
||||
else if (Chrome.rootWidget.GetWidget("MAP_CHOOSER").Visible)
|
||||
Game.chrome.DrawMapChooser();
|
||||
|
||||
Game.chrome.Draw(world);
|
||||
Timer.Time( "widgets: {0}" );
|
||||
|
||||
var cursorName = Game.chrome.HitTest(mousePos) ? "default" : Game.controller.ChooseCursor( world );
|
||||
|
||||
@@ -66,28 +66,7 @@ namespace OpenRA.Widgets
|
||||
|
||||
public void AddLine(Color c, string from, string text)
|
||||
{
|
||||
var sizeOwner = Game.chrome.renderer.RegularFont.Measure(from);
|
||||
var sizeText = Game.chrome.renderer.RegularFont.Measure(text);
|
||||
|
||||
if (sizeOwner.X + sizeText.X + 10 <= Chrome.ChatWidth)
|
||||
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = text });
|
||||
else
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (var w in text.Split(' '))
|
||||
{
|
||||
if ( Game.chrome.renderer.RegularFont.Measure(sb.ToString() + ' ' + w).X > Chrome.ChatWidth )
|
||||
{
|
||||
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = sb.ToString() } );
|
||||
sb = new StringBuilder();
|
||||
sb.Append(w);
|
||||
}
|
||||
else
|
||||
sb.Append( ' ' + w);
|
||||
}
|
||||
if (sb.Length != 0)
|
||||
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = sb.ToString() } );
|
||||
}
|
||||
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = text });
|
||||
|
||||
if (Notification != null)
|
||||
Sound.Play(Notification);
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace OpenRA.Widgets.Delegates
|
||||
|
||||
var mapButton = lobby.GetWidget("CHANGEMAP_BUTTON");
|
||||
mapButton.OnMouseUp = mi => {
|
||||
r.GetWidget("MAP_CHOOSER").SpecialOneArg(MapUid);
|
||||
r.OpenWindow("MAP_CHOOSER");
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -28,19 +28,22 @@ namespace OpenRA.Widgets.Delegates
|
||||
{
|
||||
public class MapChooserDelegate : IWidgetDelegate
|
||||
{
|
||||
MapStub Map = null;
|
||||
public MapChooserDelegate()
|
||||
{
|
||||
var r = Chrome.rootWidget;
|
||||
var bg = r.GetWidget("MAP_CHOOSER");
|
||||
bg.SpecialOneArg = (map) => RefreshMapList(map);
|
||||
var ml = bg.GetWidget("MAP_LIST");
|
||||
|
||||
bg.GetWidget<MapPreviewWidget>("MAPCHOOSER_MAP_PREVIEW").Map = () => {return Game.chrome.currentMap;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_TITLE").GetText = () => {return Game.chrome.currentMap.Title;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_SIZE").GetText = () => {return "{0}x{1}".F(Game.chrome.currentMap.Width, Game.chrome.currentMap.Height);};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_THEATER").GetText = () => {return Rules.TileSets[Game.chrome.currentMap.Tileset].Name;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_PLAYERS").GetText = () => {return Game.chrome.currentMap.PlayerCount.ToString();};
|
||||
bg.GetWidget<MapPreviewWidget>("MAPCHOOSER_MAP_PREVIEW").Map = () => {return Map;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_TITLE").GetText = () => {return Map.Title;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_SIZE").GetText = () => {return "{0}x{1}".F(Map.Width, Map.Height);};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_THEATER").GetText = () => {return Rules.TileSets[Map.Tileset].Name;};
|
||||
bg.GetWidget<LabelWidget>("CURMAP_PLAYERS").GetText = () => {return Map.PlayerCount.ToString();};
|
||||
|
||||
bg.GetWidget("BUTTON_OK").OnMouseUp = mi => {
|
||||
Game.IssueOrder(Order.Chat("/map " + Game.chrome.currentMap.Uid));
|
||||
Game.IssueOrder(Order.Chat("/map " + Map.Uid));
|
||||
r.CloseWindow();
|
||||
return true;
|
||||
};
|
||||
@@ -49,6 +52,37 @@ namespace OpenRA.Widgets.Delegates
|
||||
r.CloseWindow();
|
||||
return true;
|
||||
};
|
||||
|
||||
var itemTemplate = ml.GetWidget<ButtonWidget>("MAP_TEMPLATE");
|
||||
int offset = 0;
|
||||
foreach (var kv in Game.AvailableMaps)
|
||||
{
|
||||
var map = kv.Value;
|
||||
if (!map.Selectable)
|
||||
continue;
|
||||
|
||||
var template = itemTemplate.Clone() as ButtonWidget;
|
||||
template.Id = "MAP_{0}".F(map.Uid);
|
||||
template.GetText = () => map.Title;
|
||||
template.OnMouseUp = mi => {Map = map; return true;};
|
||||
template.Parent = ml;
|
||||
|
||||
template.Bounds = new Rectangle(0, offset, template.Bounds.Width, template.Bounds.Height);
|
||||
template.IsVisible = () => true;
|
||||
ml.AddChild(template);
|
||||
|
||||
offset += template.Bounds.Height + 5;
|
||||
}
|
||||
}
|
||||
|
||||
public void RefreshMapList(object uidobj)
|
||||
{
|
||||
// Set the default selected map
|
||||
var uid = uidobj as string;
|
||||
if (uid != null)
|
||||
Map = Game.AvailableMaps[ uid ];
|
||||
else
|
||||
Map = Game.AvailableMaps.FirstOrDefault().Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace OpenRA.Widgets
|
||||
public static Stack<string> WindowList = new Stack<string>();
|
||||
|
||||
// Common Funcs that most widgets will want
|
||||
public Action<object> SpecialOneArg = (arg) => {};
|
||||
public Func<MouseInput,bool> OnMouseDown = mi => {return false;};
|
||||
public Func<MouseInput,bool> OnMouseUp = mi => {return false;};
|
||||
public Func<MouseInput,bool> OnMouseMove = mi => {return false;};
|
||||
|
||||
@@ -673,6 +673,20 @@ Container:
|
||||
Height:20
|
||||
Text:Choose Map
|
||||
Bold:True
|
||||
Container@MAP_LIST:
|
||||
Id:MAP_LIST
|
||||
X:50
|
||||
Y:50
|
||||
Width:450
|
||||
Height:500
|
||||
Children:
|
||||
Button@MAP_TEMPLATE:
|
||||
Id:MAP_TEMPLATE
|
||||
Width:440
|
||||
Height:25
|
||||
X:0
|
||||
Y:0
|
||||
Visible:false
|
||||
Label@CURMAP_TITLE_LABEL:
|
||||
Id:CURMAP_TITLE_LABEL
|
||||
X:PARENT_RIGHT - 200 - WIDTH
|
||||
|
||||
Reference in New Issue
Block a user