ColorBlockWidget instead of ColorButton + refactoring

This commit is contained in:
alzeih
2010-04-24 21:42:50 +12:00
parent 0be74556d0
commit 94df65c162
8 changed files with 201 additions and 174 deletions

View File

@@ -309,7 +309,7 @@
<Compile Include="Widgets\BuildPaletteWidget.cs" /> <Compile Include="Widgets\BuildPaletteWidget.cs" />
<Compile Include="Traits\World\BridgeLayer.cs" /> <Compile Include="Traits\World\BridgeLayer.cs" />
<Compile Include="Widgets\Delegates\LobbyDelegate.cs" /> <Compile Include="Widgets\Delegates\LobbyDelegate.cs" />
<Compile Include="Widgets\ColorButtonWidget.cs" /> <Compile Include="Widgets\ColorBlockWidget.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,45 @@
using System;
using System.Drawing;
namespace OpenRA.Widgets
{
class ColorBlockWidget : Widget
{
public int PaletteIndex = 0;
public Func<int> GetPaletteIndex;
public ColorBlockWidget()
: base()
{
GetPaletteIndex = () => { return PaletteIndex; };
}
public ColorBlockWidget(Widget widget)
:base(widget)
{
PaletteIndex = (widget as ColorBlockWidget).PaletteIndex;
GetPaletteIndex = (widget as ColorBlockWidget).GetPaletteIndex;
}
public override Widget Clone()
{
return new ColorBlockWidget(this);
}
public override void Draw(World world)
{
if (!IsVisible())
{
base.Draw(world);
return;
}
var pos = DrawPosition();
var paletteRect = new RectangleF(pos.X + Game.viewport.Location.X, pos.Y + Game.viewport.Location.Y, Bounds.Width, Bounds.Height);
Game.chrome.lineRenderer.FillRect(paletteRect, Player.PlayerColors(Game.world)[GetPaletteIndex() % Player.PlayerColors(Game.world).Count].c);
base.Draw(world);
}
}
}

View File

@@ -1,54 +0,0 @@
using System;
using System.Drawing;
namespace OpenRA.Widgets
{
class ColorButtonWidget : ButtonWidget
{
public int PaletteIndex = 0;
public Func<int> GetPaletteIndex;
public ColorButtonWidget()
: base()
{
GetPaletteIndex = () => { return PaletteIndex; };
}
public ColorButtonWidget(Widget widget)
:base(widget)
{
PaletteIndex = (widget as ColorButtonWidget).PaletteIndex;
GetPaletteIndex = (widget as ColorButtonWidget).GetPaletteIndex;
}
public override Widget Clone()
{
return new ColorButtonWidget(this);
}
public override void Draw(World world)
{
if (!IsVisible())
{
base.Draw(world);
return;
}
DrawColorBlock();
base.Draw(world);
}
void DrawColorBlock()
{
var pos = DrawPosition();
var paletteRect = new Rectangle(pos.X ,pos.Y + 2 , 65, 22);
Game.chrome.lineRenderer.FillRect(RectangleF.FromLTRB(paletteRect.Left + Game.viewport.Location.X + 5,
paletteRect.Top + Game.viewport.Location.Y + 5,
paletteRect.Right + Game.viewport.Location.X - 5,
paletteRect.Bottom+Game.viewport.Location.Y - 5),
Player.PlayerColors(Game.world)[GetPaletteIndex() % Player.PlayerColors(Game.world).Count].c);
}
}
}

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Widgets.Delegates
Bold = true, Bold = true,
Bounds = new Rectangle(margin + labelWidth + 10, y, labelWidth, 25), Bounds = new Rectangle(margin + labelWidth + 10, y, labelWidth, 25),
Text = "Their Stance", Text = "Their Stance",
Align = "Left", Align = LabelWidget.TextAlign.Left,
}; };
bg.AddChild(ts); bg.AddChild(ts);
@@ -50,7 +50,7 @@ namespace OpenRA.Widgets.Delegates
Bold = true, Bold = true,
Bounds = new Rectangle(margin + 2 * labelWidth + 20, y, labelWidth, 25), Bounds = new Rectangle(margin + 2 * labelWidth + 20, y, labelWidth, 25),
Text = "My Stance", Text = "My Stance",
Align = "Left", Align = LabelWidget.TextAlign.Left,
}; };
bg.AddChild(ms); bg.AddChild(ms);
@@ -66,7 +66,7 @@ namespace OpenRA.Widgets.Delegates
Bounds = new Rectangle(margin, y, labelWidth, 25), Bounds = new Rectangle(margin, y, labelWidth, 25),
Id = "DIPLOMACY_PLAYER_LABEL_{0}".F(p.Index), Id = "DIPLOMACY_PLAYER_LABEL_{0}".F(p.Index),
Text = p.PlayerName, Text = p.PlayerName,
Align = "Left", Align = LabelWidget.TextAlign.Left,
Bold = true, Bold = true,
}; };
@@ -78,7 +78,7 @@ namespace OpenRA.Widgets.Delegates
Bounds = new Rectangle( margin + labelWidth + 10, y, labelWidth, 25), Bounds = new Rectangle( margin + labelWidth + 10, y, labelWidth, 25),
Id = "DIPLOMACY_PLAYER_LABEL_THEIR_{0}".F(p.Index), Id = "DIPLOMACY_PLAYER_LABEL_THEIR_{0}".F(p.Index),
Text = p.PlayerName, Text = p.PlayerName,
Align = "Left", Align = LabelWidget.TextAlign.Left,
Bold = false, Bold = false,
GetText = () => pp.Stances[ Game.world.LocalPlayer ].ToString(), GetText = () => pp.Stances[ Game.world.LocalPlayer ].ToString(),

View File

@@ -47,10 +47,11 @@ namespace OpenRA.Widgets.Delegates
if(client.Index == Game.LocalClient.Index) if(client.Index == Game.LocalClient.Index)
{ {
//TODO: Real Color Button var color = template.GetWidget<ButtonWidget>("COLOR");
var color = template.GetWidget<ColorButtonWidget>("COLOR");
color.OnMouseUp = CyclePalette; color.OnMouseUp = CyclePalette;
color.GetPaletteIndex = () => c.PaletteIndex;
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetPaletteIndex = () => c.PaletteIndex;
var faction = template.GetWidget<ButtonWidget>("FACTION"); var faction = template.GetWidget<ButtonWidget>("FACTION");
faction.OnMouseUp = CycleRace; faction.OnMouseUp = CycleRace;
@@ -70,9 +71,8 @@ namespace OpenRA.Widgets.Delegates
} }
else else
{ {
//TODO: Real Color Label var color = template.GetWidget<ColorBlockWidget>("COLOR");
var color = template.GetWidget<LabelWidget>("COLOR"); color.GetPaletteIndex = () => c.PaletteIndex;
color.GetText = () => c.PaletteIndex.ToString();
var faction = template.GetWidget<LabelWidget>("FACTION"); var faction = template.GetWidget<LabelWidget>("FACTION");
faction.GetText = () => c.Country; faction.GetText = () => c.Country;

View File

@@ -25,8 +25,15 @@ namespace OpenRA.Widgets
{ {
class LabelWidget : Widget class LabelWidget : Widget
{ {
public enum TextAlign
{
Left,
Center,
Right
}
public string Text = ""; public string Text = "";
public string Align = "Left"; public TextAlign Align = TextAlign.Left;
public bool Bold = true; public bool Bold = true;
public Func<string> GetText; public Func<string> GetText;
@@ -58,9 +65,8 @@ namespace OpenRA.Widgets
int2 textSize = font.Measure(text); int2 textSize = font.Measure(text);
int2 position = DrawPosition(); int2 position = DrawPosition();
if (Align == "Center") if (Align == TextAlign.Center)
position = new int2(position.X +Bounds.Width/2, position.Y + Bounds.Height/2) position += new int2((Bounds.Width - textSize.X)/2, 0);
- new int2(textSize.X / 2, textSize.Y/2);
font.DrawText(text, position, Color.White); font.DrawText(text, position, Color.White);
base.Draw(world); base.Draw(world);

View File

@@ -312,11 +312,18 @@ Container:
Y:5 Y:5
Button@COLOR: Button@COLOR:
Id:COLOR Id:COLOR
Text:Color
Width:65 Width:65
Height:25 Height:25
X:100 X:100
Y:0 Y:0
Children:
ColorBlock@COLORBLOCK:
Id:COLORBLOCK
X:5
Y:7
Width:PARENT_RIGHT-10
Height:PARENT_BOTTOM-12
PaletteIndex:0
Button@FACTION: Button@FACTION:
Id:FACTION Id:FACTION
Text:Faction Text:Faction
@@ -341,7 +348,7 @@ Container:
Checkbox@STATUS: Checkbox@STATUS:
Id:STATUS Id:STATUS
X:455 X:455
Y:0 Y:1
Width:20 Width:20
Height:20 Height:20
Container@TEMPLATE_REMOTE Container@TEMPLATE_REMOTE
@@ -359,14 +366,13 @@ Container:
Height:25 Height:25
X:0 X:0
Y:5 Y:5
Label@COLOR: ColorBlock@COLOR:
Id:COLOR Id:COLOR
Text:Color X:105
Width:65 Y:7
Height:25 Width:55
X:100 Height:13
Y:0 PaletteIndex:0
Align:Center
Label@FACTION: Label@FACTION:
Id:FACTION Id:FACTION
Text:Faction Text:Faction
@@ -394,9 +400,60 @@ Container:
Checkbox@STATUS: Checkbox@STATUS:
Id:STATUS Id:STATUS
X:455 X:455
Y:0 Y:1
Width:20 Width:20
Height:20 Height:20
Container@LABEL_CONTAINER:
X:30
Y:50
Children:
Label@LABEL_LOBBY_NAME:
Id:LABEL_LOBBY_NAME
Width:95
Height:25
X:0
Y:0
Text:Name
Label@LABEL_LOBBY_COLOR:
Id:LABEL_LOBBY_COLOR
Width:65
Height:25
X:100
Y:0
Text:Color
Align:Center
Label@LABEL_LOBBY_FACTION
Id:LABEL_LOBBY_FACTION
Width:90
Height:25
X:180
Y:0
Text:Faction
Align:Center
Label@LABEL_LOBBY_SPAWN
Id:LABEL_LOBBY_SPAWN
Width:70
Height:25
X:275
Y:0
Text:Spawn
Align:Center
Label@LABEL_LOBBY_TEAM
Id:LABEL_LOBBY_TEAM
Width:70
Height:25
X:355
Y:0
Text:Team
Align:Center
Label@LABEL_LOBBY_STATUS
Id:LABEL_LOBBY_STATUS
X:455
Y:0
Width:20
Height:20
Text:Ready
Align:Center
Button@CHANGEMAP_BUTTON: Button@CHANGEMAP_BUTTON:
Id:CHANGEMAP_BUTTON Id:CHANGEMAP_BUTTON
Visible:true Visible:true
@@ -405,48 +462,6 @@ Container:
Width:120 Width:120
Height:25 Height:25
Text:Change Map Text:Change Map
Label@LABEL_LOBBY_NAME:
Id:LABEL_LOBBY_NAME
X:40
Y:50
Width:250
Height:25
Text:Name
Label@LABEL_LOBBY_COLOR:
Id:LABEL_LOBBY_COLOR
X:140
Y:50
Width:250
Height:25
Text:Color
Label@LABEL_LOBBY_FACTION
Id:LABEL_LOBBY_FACTION
X:220
Y:50
Width:250
Height:25
Text:Faction
Label@LABEL_LOBBY_SPAWN
Id:LABEL_LOBBY_SPAWN
X:315
Y:50
Width:250
Height:25
Text:Spawn
Label@LABEL_LOBBY_TEAM
Id:LABEL_LOBBY_TEAM
X:395
Y:50
Width:250
Height:25
Text:Team
Label@LABEL_LOBBY_STATUS
Id:LABEL_LOBBY_STATUS
X:475
Y:50
Width:250
Height:25
Text:Ready
Background@MAP_CHOOSER: Background@MAP_CHOOSER:
Id:MAP_CHOOSER Id:MAP_CHOOSER
X:(WINDOW_RIGHT - WIDTH)/2 X:(WINDOW_RIGHT - WIDTH)/2

View File

@@ -310,13 +310,20 @@ Container:
Height:25 Height:25
X:0 X:0
Y:5 Y:5
ColorButton@COLOR: Button@COLOR:
Id:COLOR Id:COLOR
Width:65 Width:65
Height:25 Height:25
X:100 X:100
Y:0 Y:0
PaletteIndex:0 Children:
ColorBlock@COLORBLOCK:
Id:COLORBLOCK
X:5
Y:7
Width:PARENT_RIGHT-10
Height:PARENT_BOTTOM-12
PaletteIndex:0
Button@FACTION: Button@FACTION:
Id:FACTION Id:FACTION
Text:Faction Text:Faction
@@ -341,7 +348,7 @@ Container:
Checkbox@STATUS: Checkbox@STATUS:
Id:STATUS Id:STATUS
X:455 X:455
Y:0 Y:1
Width:20 Width:20
Height:20 Height:20
Container@TEMPLATE_REMOTE Container@TEMPLATE_REMOTE
@@ -359,14 +366,13 @@ Container:
Height:25 Height:25
X:0 X:0
Y:5 Y:5
Label@COLOR: ColorBlock@COLOR:
Id:COLOR Id:COLOR
Text:Color X:105
Width:65 Y:7
Height:25 Width:55
X:100 Height:13
Y:0 PaletteIndex:0
Align:Center
Label@FACTION: Label@FACTION:
Id:FACTION Id:FACTION
Text:Faction Text:Faction
@@ -394,9 +400,60 @@ Container:
Checkbox@STATUS: Checkbox@STATUS:
Id:STATUS Id:STATUS
X:455 X:455
Y:0 Y:1
Width:20 Width:20
Height:20 Height:20
Container@LABEL_CONTAINER:
X:30
Y:50
Children:
Label@LABEL_LOBBY_NAME:
Id:LABEL_LOBBY_NAME
Width:95
Height:25
X:0
Y:0
Text:Name
Label@LABEL_LOBBY_COLOR:
Id:LABEL_LOBBY_COLOR
Width:65
Height:25
X:100
Y:0
Text:Color
Align:Center
Label@LABEL_LOBBY_FACTION
Id:LABEL_LOBBY_FACTION
Width:90
Height:25
X:180
Y:0
Text:Faction
Align:Center
Label@LABEL_LOBBY_SPAWN
Id:LABEL_LOBBY_SPAWN
Width:70
Height:25
X:275
Y:0
Text:Spawn
Align:Center
Label@LABEL_LOBBY_TEAM
Id:LABEL_LOBBY_TEAM
Width:70
Height:25
X:355
Y:0
Text:Team
Align:Center
Label@LABEL_LOBBY_STATUS
Id:LABEL_LOBBY_STATUS
X:455
Y:0
Width:20
Height:20
Text:Ready
Align:Center
Button@CHANGEMAP_BUTTON: Button@CHANGEMAP_BUTTON:
Id:CHANGEMAP_BUTTON Id:CHANGEMAP_BUTTON
Visible:true Visible:true
@@ -405,48 +462,6 @@ Container:
Width:120 Width:120
Height:25 Height:25
Text:Change Map Text:Change Map
Label@LABEL_LOBBY_NAME:
Id:LABEL_LOBBY_NAME
X:40
Y:50
Width:250
Height:25
Text:Name
Label@LABEL_LOBBY_COLOR:
Id:LABEL_LOBBY_COLOR
X:140
Y:50
Width:250
Height:25
Text:Color
Label@LABEL_LOBBY_FACTION
Id:LABEL_LOBBY_FACTION
X:220
Y:50
Width:250
Height:25
Text:Faction
Label@LABEL_LOBBY_SPAWN
Id:LABEL_LOBBY_SPAWN
X:315
Y:50
Width:250
Height:25
Text:Spawn
Label@LABEL_LOBBY_TEAM
Id:LABEL_LOBBY_TEAM
X:395
Y:50
Width:250
Height:25
Text:Team
Label@LABEL_LOBBY_STATUS
Id:LABEL_LOBBY_STATUS
X:475
Y:50
Width:250
Height:25
Text:Ready
Background@MAP_CHOOSER: Background@MAP_CHOOSER:
Id:MAP_CHOOSER Id:MAP_CHOOSER
X:(WINDOW_RIGHT - WIDTH)/2 X:(WINDOW_RIGHT - WIDTH)/2