Move IsDisabled checking for OnMouseDown into ButtonWidget. Remove unnecessary bool plumbing.

This commit is contained in:
Paul Chote
2011-07-04 02:22:42 +12:00
parent bbeaf2047b
commit e58e354c4b
6 changed files with 28 additions and 38 deletions

View File

@@ -24,8 +24,7 @@ namespace OpenRA.Widgets
public string Font = ChromeMetrics.Get<string>("ButtonFont"); public string Font = ChromeMetrics.Get<string>("ButtonFont");
public Func<string> GetText; public Func<string> GetText;
public Func<bool> IsDisabled = () => false; public Func<bool> IsDisabled = () => false;
public Func<MouseInput, bool> OnMouseDown = mi => false; public Action<MouseInput> OnMouseDown = _ => {};
public Action OnClick = () => {}; public Action OnClick = () => {};
public Action<KeyInput> OnKeyPress = _ => {}; public Action<KeyInput> OnKeyPress = _ => {};
@@ -88,8 +87,11 @@ namespace OpenRA.Widgets
if (mi.Event == MouseInputEvent.Down) if (mi.Event == MouseInputEvent.Down)
{ {
// OnMouseDown returns false if the button shouldn't be pressed // OnMouseDown returns false if the button shouldn't be pressed
if (!OnMouseDown(mi)) if (!IsDisabled())
{
OnMouseDown(mi);
Depressed = true; Depressed = true;
}
else else
LoseFocus(mi); LoseFocus(mi);
} }

View File

@@ -295,7 +295,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
} }
} }
bool ShowSlotDropDown(DropDownButtonWidget dropdown, Session.Slot slot, Session.Client client) void ShowSlotDropDown(DropDownButtonWidget dropdown, Session.Slot slot, Session.Client client)
{ {
var options = new List<SlotDropDownOption>() var options = new List<SlotDropDownOption>()
{ {
@@ -322,10 +322,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
}; };
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem); dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem);
return true;
} }
bool ShowRaceDropDown(DropDownButtonWidget dropdown, Session.Client client) void ShowRaceDropDown(DropDownButtonWidget dropdown, Session.Client client)
{ {
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (race, itemTemplate) => Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (race, itemTemplate) =>
{ {
@@ -340,10 +339,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
}; };
dropdown.ShowDropDown("RACE_DROPDOWN_TEMPLATE", 150, CountryNames.Keys.ToList(), setupItem); dropdown.ShowDropDown("RACE_DROPDOWN_TEMPLATE", 150, CountryNames.Keys.ToList(), setupItem);
return true;
} }
bool ShowTeamDropDown(DropDownButtonWidget dropdown, Session.Client client) void ShowTeamDropDown(DropDownButtonWidget dropdown, Session.Client client)
{ {
Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) => Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) =>
{ {
@@ -356,10 +354,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var options = Graphics.Util.MakeArray(Map.SpawnPoints.Count()+1, i => i).ToList(); var options = Graphics.Util.MakeArray(Map.SpawnPoints.Count()+1, i => i).ToList();
dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem); dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem);
return true;
} }
bool ShowSpawnDropDown(DropDownButtonWidget dropdown, Session.Client client) void ShowSpawnDropDown(DropDownButtonWidget dropdown, Session.Client client)
{ {
Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) => Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) =>
{ {
@@ -376,10 +373,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var options = Graphics.Util.MakeArray(Map.SpawnPoints.Count() + 1, i => i).Except(taken).ToList(); var options = Graphics.Util.MakeArray(Map.SpawnPoints.Count() + 1, i => i).Except(taken).ToList();
dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem); dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem);
return true;
} }
bool ShowColorDropDown(DropDownButtonWidget color, Session.Client client) void ShowColorDropDown(DropDownButtonWidget color, Session.Client client)
{ {
Action<ColorRamp> onSelect = c => Action<ColorRamp> onSelect = c =>
{ {
@@ -402,7 +398,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
}); });
color.AttachPanel(colorChooser); color.AttachPanel(colorChooser);
return true;
} }
void UpdatePlayerList() void UpdatePlayerList()
@@ -460,7 +455,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
name.IsVisible = () => true; name.IsVisible = () => true;
name.IsDisabled = () => ready; name.IsDisabled = () => ready;
name.GetText = () => client.Name; name.GetText = () => client.Name;
name.OnMouseDown = _ => name.IsDisabled() ? true : ShowSlotDropDown(name, slot, client); name.OnMouseDown = _ => ShowSlotDropDown(name, slot, client);
} }
else else
{ {
@@ -488,14 +483,14 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var color = template.GetWidget<DropDownButtonWidget>("COLOR"); var color = template.GetWidget<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => slot.LockColor || ready; color.IsDisabled = () => slot.LockColor || ready;
color.OnMouseDown = _ => color.IsDisabled() ? true : ShowColorDropDown(color, client); color.OnMouseDown = _ => ShowColorDropDown(color, client);
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK"); var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetColor = () => client.ColorRamp.GetColor(0); colorBlock.GetColor = () => client.ColorRamp.GetColor(0);
var faction = template.GetWidget<DropDownButtonWidget>("FACTION"); var faction = template.GetWidget<DropDownButtonWidget>("FACTION");
faction.IsDisabled = () => slot.LockRace || ready; faction.IsDisabled = () => slot.LockRace || ready;
faction.OnMouseDown = _ => faction.IsDisabled() ? true : ShowRaceDropDown(faction, client); faction.OnMouseDown = _ => ShowRaceDropDown(faction, client);
var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME"); var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME");
factionname.GetText = () => CountryNames[client.Country]; factionname.GetText = () => CountryNames[client.Country];
@@ -505,12 +500,12 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var team = template.GetWidget<DropDownButtonWidget>("TEAM"); var team = template.GetWidget<DropDownButtonWidget>("TEAM");
team.IsDisabled = () => slot.LockTeam || ready; team.IsDisabled = () => slot.LockTeam || ready;
team.OnMouseDown = _ => team.IsDisabled() ? true : ShowTeamDropDown(team, client); team.OnMouseDown = _ => ShowTeamDropDown(team, client);
team.GetText = () => (client.Team == 0) ? "-" : client.Team.ToString(); team.GetText = () => (client.Team == 0) ? "-" : client.Team.ToString();
var spawn = template.GetWidget<DropDownButtonWidget>("SPAWN"); var spawn = template.GetWidget<DropDownButtonWidget>("SPAWN");
spawn.IsDisabled = () => slot.LockSpawn || ready; spawn.IsDisabled = () => slot.LockSpawn || ready;
spawn.OnMouseDown = _ => spawn.IsDisabled() ? true : ShowSpawnDropDown(spawn, client); spawn.OnMouseDown = _ => ShowSpawnDropDown(spawn, client);
spawn.GetText = () => (client.SpawnPoint == 0) ? "-" : client.SpawnPoint.ToString(); spawn.GetText = () => (client.SpawnPoint == 0) ? "-" : client.SpawnPoint.ToString();
if (client.Bot == null) if (client.Bot == null)
@@ -589,7 +584,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var color = template.GetWidget<DropDownButtonWidget>("COLOR"); var color = template.GetWidget<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => ready; color.IsDisabled = () => ready;
color.OnMouseDown = _ => color.IsDisabled() ? true : ShowColorDropDown(color, client); color.OnMouseDown = _ => ShowColorDropDown(color, client);
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK"); var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetColor = () => client.ColorRamp.GetColor(0); colorBlock.GetColor = () => client.ColorRamp.GetColor(0);

View File

@@ -112,7 +112,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
GetText = () => world.LocalPlayer.Stances[ pp ].ToString(), GetText = () => world.LocalPlayer.Stances[ pp ].ToString(),
}; };
myStance.OnMouseDown = mi => { ShowDropDown(pp, myStance); return true; }; myStance.OnMouseDown = mi => ShowDropDown(pp, myStance);
bg.AddChild(myStance); bg.AddChild(myStance);
controls.Add(myStance); controls.Add(myStance);

View File

@@ -226,7 +226,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
} }
} }
bool ShowSlotDropDown(DropDownButtonWidget dropdown, Session.Slot slot, Session.Client client) void ShowSlotDropDown(DropDownButtonWidget dropdown, Session.Slot slot, Session.Client client)
{ {
var options = new List<SlotDropDownOption>() var options = new List<SlotDropDownOption>()
{ {
@@ -253,10 +253,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}; };
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem); dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem);
return true;
} }
bool ShowRaceDropDown(DropDownButtonWidget dropdown, Session.Client client) void ShowRaceDropDown(DropDownButtonWidget dropdown, Session.Client client)
{ {
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (race, itemTemplate) => Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (race, itemTemplate) =>
{ {
@@ -271,10 +270,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}; };
dropdown.ShowDropDown("RACE_DROPDOWN_TEMPLATE", 150, CountryNames.Keys.ToList(), setupItem); dropdown.ShowDropDown("RACE_DROPDOWN_TEMPLATE", 150, CountryNames.Keys.ToList(), setupItem);
return true;
} }
bool ShowTeamDropDown(DropDownButtonWidget dropdown, Session.Client client) void ShowTeamDropDown(DropDownButtonWidget dropdown, Session.Client client)
{ {
Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) => Func<int, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, itemTemplate) =>
{ {
@@ -287,10 +285,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var options = Graphics.Util.MakeArray(Map.PlayerCount, i => i).ToList(); var options = Graphics.Util.MakeArray(Map.PlayerCount, i => i).ToList();
dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem); dropdown.ShowDropDown("TEAM_DROPDOWN_TEMPLATE", 150, options, setupItem);
return true;
} }
bool ShowColorDropDown(DropDownButtonWidget color, Session.Client client) void ShowColorDropDown(DropDownButtonWidget color, Session.Client client)
{ {
var colorChooser = Game.modData.WidgetLoader.LoadWidget( new WidgetArgs() { {"worldRenderer", worldRenderer} }, null, "COLOR_CHOOSER" ); var colorChooser = Game.modData.WidgetLoader.LoadWidget( new WidgetArgs() { {"worldRenderer", worldRenderer} }, null, "COLOR_CHOOSER" );
var hueSlider = colorChooser.GetWidget<SliderWidget>("HUE_SLIDER"); var hueSlider = colorChooser.GetWidget<SliderWidget>("HUE_SLIDER");
@@ -319,7 +316,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}; };
color.AttachPanel(colorChooser); color.AttachPanel(colorChooser);
return true;
} }
void UpdatePlayerList() void UpdatePlayerList()
@@ -383,14 +379,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var color = template.GetWidget<DropDownButtonWidget>("COLOR"); var color = template.GetWidget<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => s.LockColor; color.IsDisabled = () => s.LockColor;
color.OnMouseDown = _ => { if (s.LockColor) return true; return ShowColorDropDown(color, c); }; color.OnMouseDown = _ => ShowColorDropDown(color, c);
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK"); var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetColor = () => c.ColorRamp.GetColor(0); colorBlock.GetColor = () => c.ColorRamp.GetColor(0);
var faction = template.GetWidget<DropDownButtonWidget>("FACTION"); var faction = template.GetWidget<DropDownButtonWidget>("FACTION");
faction.IsDisabled = () => s.LockRace; faction.IsDisabled = () => s.LockRace;
faction.OnMouseDown = _ => { if (s.LockRace) return true; return ShowRaceDropDown(faction, c); }; faction.OnMouseDown = _ => ShowRaceDropDown(faction, c);
var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME"); var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME");
factionname.GetText = () => CountryNames[c.Country]; factionname.GetText = () => CountryNames[c.Country];
@@ -400,7 +396,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var team = template.GetWidget<DropDownButtonWidget>("TEAM"); var team = template.GetWidget<DropDownButtonWidget>("TEAM");
team.IsDisabled = () => s.LockTeam; team.IsDisabled = () => s.LockTeam;
team.OnMouseDown = _ => { if (s.LockTeam) return true; return ShowTeamDropDown(team, c); }; team.OnMouseDown = _ => ShowTeamDropDown(team, c);
team.GetText = () => (c.Team == 0) ? "-" : c.Team.ToString(); team.GetText = () => (c.Team == 0) ? "-" : c.Team.ToString();
var status = template.GetWidget<CheckboxWidget>("STATUS"); var status = template.GetWidget<CheckboxWidget>("STATUS");

View File

@@ -74,7 +74,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}); });
} }
static bool ShowModsDropDown(DropDownButtonWidget dropdown) static void ShowModsDropDown(DropDownButtonWidget dropdown)
{ {
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (m, itemTemplate) => Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (m, itemTemplate) =>
{ {
@@ -86,9 +86,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}; };
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, Mod.AllMods.Keys.ToList(), setupItem); dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, Mod.AllMods.Keys.ToList(), setupItem);
return true;
} }
} }
} }

View File

@@ -28,14 +28,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if (sell != null) if (sell != null)
{ {
sell.Pressed = () => world.OrderGenerator is SellOrderGenerator; sell.Pressed = () => world.OrderGenerator is SellOrderGenerator;
sell.OnMouseDown = mi => { world.ToggleInputMode<SellOrderGenerator>(); return true; }; sell.OnMouseDown = mi => world.ToggleInputMode<SellOrderGenerator>();
} }
var powerdown = moneybin.GetWidget<OrderButtonWidget>("POWER_DOWN"); var powerdown = moneybin.GetWidget<OrderButtonWidget>("POWER_DOWN");
if (powerdown != null) if (powerdown != null)
{ {
powerdown.Pressed = () => world.OrderGenerator is PowerDownOrderGenerator; powerdown.Pressed = () => world.OrderGenerator is PowerDownOrderGenerator;
powerdown.OnMouseDown = mi => { world.ToggleInputMode<PowerDownOrderGenerator>(); return true; }; powerdown.OnMouseDown = mi => world.ToggleInputMode<PowerDownOrderGenerator>();
} }
var repair = moneybin.GetWidget<OrderButtonWidget>("REPAIR"); var repair = moneybin.GetWidget<OrderButtonWidget>("REPAIR");
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{ {
repair.Enabled = () => { return RepairOrderGenerator.PlayerIsAllowedToRepair( world ); }; repair.Enabled = () => { return RepairOrderGenerator.PlayerIsAllowedToRepair( world ); };
repair.Pressed = () => world.OrderGenerator is RepairOrderGenerator; repair.Pressed = () => world.OrderGenerator is RepairOrderGenerator;
repair.OnMouseDown = mi => { world.ToggleInputMode<RepairOrderGenerator>(); return true; }; repair.OnMouseDown = mi => world.ToggleInputMode<RepairOrderGenerator>();
repair.GetLongDesc = () => { return repair.Enabled() ? repair.LongDesc : repair.LongDesc + "\n\nRequires: Construction Yard"; }; repair.GetLongDesc = () => { return repair.Enabled() ? repair.LongDesc : repair.LongDesc + "\n\nRequires: Construction Yard"; };
} }
} }