Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
diff Widgets/UnitDisplayWidget.cs @ 113:4a33b3012100 WarFoundry_v0.1RC1
* Tag v0.1RC1 release
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 17 Jan 2011 19:43:47 +0000 |
parents | a35c8be46006 |
children |
line wrap: on
line diff
--- a/Widgets/UnitDisplayWidget.cs Sun Jan 31 20:46:06 2010 +0000 +++ b/Widgets/UnitDisplayWidget.cs Mon Jan 17 19:43:47 2011 +0000 @@ -5,143 +5,389 @@ using System; using Gtk; using IBBoard.Commands; +using IBBoard.GtkSharp; using IBBoard.Lang; using IBBoard.WarFoundry.API; +using IBBoard.WarFoundry.API.Commands; using IBBoard.WarFoundry.API.Objects; -using IBBoard.WarFoundry.API.Commands; +using IBBoard.WarFoundry.API.Util; +using IBBoard.WarFoundry.GUI.GTK.UIControl; +using log4net; +using WFObjects = IBBoard.WarFoundry.API.Objects; +using System.Collections.Generic; +using IBBoard.GtkSharp.Translatable; -namespace IBBoard.WarFoundry.GTK.Widgets +namespace IBBoard.WarFoundry.GUI.GTK.Widgets { [System.ComponentModel.Category("WarFoundry GTK# GUI")] [System.ComponentModel.ToolboxItem(true)] public partial class UnitDisplayWidget : Gtk.Bin - { - private IBBoard.WarFoundry.API.Objects.Unit unit; + { + private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget)); + private WFObjects.Unit unit; private CommandStack stack; - - public UnitDisplayWidget(IBBoard.WarFoundry.API.Objects.Unit sourceUnit, CommandStack commandStack) + private Dictionary<string, NodeView> statsViews = new Dictionary<string, NodeView>(); + + public UnitDisplayWidget(WFObjects.Unit sourceUnit, CommandStack commandStack) { this.Build(); stack = commandStack; unit = sourceUnit; unitName.Text = unit.Name; unitSize.Value = unit.Size; - double max = unit.UnitType.MaxSize; + int maxSize = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize); + int minSize = unit.UnitType.MinSize; + unitSize.SetRange(minSize, maxSize); + unitSize.Sensitive = (maxSize != minSize); + notesView.Buffer.Text = unit.UnitType.Notes; + unit.NameChanged += UnitNameChanged; + unit.UnitSizeChanged += UnitSizeChanged; + unit.UnitEquipmentAmountChanged += HandleUnitUnitEquipmentAmountChanged; + equipmentList.Selection.Changed += HandleEquipmentListSelectionChanged; + SetAbilities(); + SetStats(); + SetWeapons(); + SetAddButtonEnabledState(); + ControlTranslator.TranslateWidget(this); + } + + private void HandleEquipmentListSelectionChanged(object sender, EventArgs e) + { + SetButtonsEnabledState(); + } + + private void SetButtonsEnabledState() + { + UnitEquipmentItem equipItem = GetSelectedEquipmentItem(); + bttnReplaceWeapon.Sensitive = (equipItem != null && equipItem.HasAlternatives()); + bttnEditWeapon.Sensitive = (UnitEquipmentUtil.CanEditEquipmentAmount(unit, equipItem)); + bttnRemoveWeapon.Sensitive = (equipItem != null && !equipItem.IsRequired); + } + + private void SetAddButtonEnabledState() + { + bttnAddWeapon.Sensitive = AddEquipmentUIControl.HasEquipmentToAdd(unit); + } + + private UnitEquipmentItem GetSelectedEquipmentItem() + { + return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList); + } + + private void SetAbilities() + { + CellRendererText renderer = new CellRendererText(); + abilitiesList.AppendColumn("", renderer, new TreeCellDataFunc(RenderAbility)); - if (max == -1) + ListStore model = new ListStore(typeof(Ability)); + + foreach (Ability ability in unit.UnitType.GetRequiredAbilities()) { - max = double.MaxValue; + model.AppendValues(ability); } - unitSize.SetRange(unit.UnitType.MinSize, max); - unit.NameChanged+= new StringValChangedDelegate(UnitNameChanged); - unit.UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChanged); - SetStats(); + abilitiesList.Model = model; + } + + public void RenderAbility(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) + { + object o = model.GetValue(iter, 0); + + if (o is Ability) + { + Ability ability = (Ability)o; + (cell as CellRendererText).Text = ability.Name; + } + } private void SetStats() { - //GameSystem system = unit.Army.GameSystem; - //SystemStats stats = system.StandardSystemStats; - CellRendererText renderer = new CellRendererText(); - unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); - - TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); - Stat[] stats = unit.UnitStatsArray; - - int length = stats.Length; + Stat[][] stats = unit.UnitStatsArraysWithName; + string[] statsIDs = unit.UnitStatsArrayIDs; + int statsCount = stats.Length; + log.DebugFormat("Unit {0} has {1} stats arrays", unit.UnitType.Name, statsCount); - for (int i = 0; i < length; i++) + for (int i = 0; i < statsCount; i++) { - unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc); + NodeView statsGrid = GetStatsView(statsIDs[i]); + TreeStore model = (TreeStore)statsGrid.Model; + log.DebugFormat("Adding row to data table for {0}", statsIDs[i]); + log.DebugFormat("TreeStore supports {0} columns", model.NColumns); + model.AppendValues((object)stats[i]); + } + } + + private NodeView GetStatsView(string statsID) + { + NodeView statsView; + + if (statsViews.ContainsKey(statsID)) + { + statsView = DictionaryUtils.GetValue(statsViews, statsID); + } + else + { + statsView = CreateStatsView(statsID); + statsViews[statsID] = statsView; } - TreeStore model = new TreeStore(typeof(IBBoard.WarFoundry.API.Objects.Unit)); - model.AppendValues(unit); - unitStats.Model = model; + return statsView; } - - private void RenderUnitName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) + + private NodeView CreateStatsView(string statsID) { - object o = model.GetValue(iter, 0); - - if (o is IBBoard.WarFoundry.API.Objects.Unit) + log.DebugFormat("Create NodeView for stats ID {0}", statsID); + SystemStats sysStats = unit.Race.GameSystem.GetSystemStatsForID(statsID); + StatSlot[] sysStatSlots = sysStats.StatSlots; + int statsCount = sysStatSlots.Length; + NodeView statsGrid = CreateNodeView(); + CellRendererText renderer = new CellRendererText(); + statsGrid.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, RenderUnitStat); + + for (int i = 0; i < statsCount; i++) { - IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; - (cell as CellRendererText).Text = u.UnitType.Name; + StatSlot stat = sysStatSlots[i]; + string slotName = stat.Name; + statsGrid.AppendColumn(slotName, renderer, RenderUnitStat); } + + statsGrid.Model = new TreeStore(typeof(Stat[])); + return statsGrid; } - + + private NodeView CreateNodeView() + { + NodeView nodeView = new NodeView(); + statsRepeatBox.Add(nodeView); + return nodeView; + } + private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { object o = model.GetValue(iter, 0); - if (o is IBBoard.WarFoundry.API.Objects.Unit) + if (o is Stat[]) { - IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; - (cell as CellRendererText).Text = u.GetStatValue(column.Title); + Stat[] stats = (Stat[])o; + (cell as CellRendererText).Text = stats[GetStatColumnIndex(column)].SlotValueString; } } - public IBBoard.WarFoundry.API.Objects.Unit Unit + private int GetStatColumnIndex(TreeViewColumn column) + { + int idx = -1; + TreeViewColumn[] cols = ((TreeView)column.TreeView).Columns; + int colCount = cols.Length; + + for (int i = 0; i < colCount; i++) + { + if (cols[i] == column) + { + idx = i; + break; + } + } + + return idx; + } + + private void SetWeapons() + { + CellRendererText renderer = new CellRendererText(); + equipmentList.AppendColumn("", renderer, new TreeCellDataFunc(RenderEquipmentLine)); + + ListStore model = new ListStore(typeof(UnitEquipmentItem)); + + foreach (UnitEquipmentItem item in unit.GetEquipment()) + { + model.AppendValues(item); + } + + equipmentList.Model = model; + } + + public void RenderEquipmentLine(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) + { + object o = model.GetValue(iter, 0); + + if (o is UnitEquipmentItem) + { + UnitEquipmentItem item = (UnitEquipmentItem)o; + (cell as CellRendererText).Text = GetUnitEquipmentText(item); + } + + } + + private string GetUnitEquipmentText(UnitEquipmentItem item) + { + string translation = ""; + + if (item.Cost == 0) + { + translation = Translation.GetTranslation("equipmentAmountWithZeroCost", "{0} ({1} - free)", item.Name, GetAmountString(item)); + } + else + { + translation = Translation.GetTranslation("equipmentAmountWithCost", "{0} ({1} at {2}pts each)", item.Name, GetAmountString(item), item.Cost); + } + + return translation; + } + + private string GetAmountString(UnitEquipmentItem item) + { + double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, item); + string amountString = ""; + + if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item)) + { + int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, item); + + if (amount == 100) + { + amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number); + } + else + { + amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number); + } + } + else + { + amountString = Translation.GetTranslation("equipmentChoiceAmountNumber", "{0}", amount); + } + + return amountString; + } + + public WFObjects.Unit Unit { get { return unit; } } - + private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue) { unitName.Text = newValue; } - + private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) { unitSize.Value = newValue; } - protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args) + private void HandleUnitUnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue) + { + if (oldValue == 0) + { + ((ListStore)equipmentList.Model).AppendValues(obj); + } + else + { + if (newValue == 0) + { + TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj); + ((ListStore)equipmentList.Model).Remove(ref treeIter); + } + } + + SetAddButtonEnabledState(); + equipmentList.QueueDraw(); + } + + protected virtual void OnUnitSizeFocusOut(object o, Gtk.FocusOutEventArgs args) { SetNewUnitSize(); } [GLib.ConnectBefore ()] - protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args) + + protected virtual void OnUnitSizeKeyPress(object o, Gtk.KeyPressEventArgs args) { - if (args.Event.Key == Gdk.Key.Return) + if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) { SetNewUnitSize(); } } - + private void SetNewUnitSize() { - if (unitSize.Value!=unit.Size) + if (unitSize.Value != unit.Size) { SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); stack.Execute(cmd); } } - protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args) + protected virtual void OnUnitNameFocusOut(object o, Gtk.FocusOutEventArgs args) { SetNewUnitName(); } [GLib.ConnectBefore ()] - protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args) + + protected virtual void OnUnitNameKeyPress(object o, Gtk.KeyPressEventArgs args) { - if (args.Event.Key == Gdk.Key.Return) + if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) { SetNewUnitName(); } } - + private void SetNewUnitName() { - if (unitName.Text!=unit.Name) + if (unitName.Text != unit.Name) { SetNameCommand cmd = new SetNameCommand(unit, unitName.Text); stack.Execute(cmd); } } + + private void OnBttnAddEquipmentClicked(object sender, System.EventArgs e) + { + AddEquipment(); + } + + private void AddEquipment() + { + AddEquipmentUIControl addEquipment = new AddEquipmentUIControl(unit, stack); + addEquipment.Show(); + } + + protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e) + { + UnitEquipmentItem item = GetSelectedEquipmentItem(); + log.Debug("Remove " + item); + + if (item != null) + { + SetUnitEquipmentNumericAmountCommand cmd = new SetUnitEquipmentNumericAmountCommand(unit, item, 0); + stack.Execute(cmd); + } + } + + protected virtual void HandleEditButtonClicked(object sender, System.EventArgs e) + { + UnitEquipmentItem item = GetSelectedEquipmentItem(); + log.Debug("Edit " + item); + + if (item != null) + { + EditEquipmentUIControl editEquipment = new EditEquipmentUIControl(unit, item, stack); + editEquipment.Show(); + } + } + + protected virtual void HandleReplaceButtonClicked(object sender, System.EventArgs e) + { + UnitEquipmentItem item = GetSelectedEquipmentItem(); + log.Debug("Replace " + item); + + if (item != null) + { + ReplaceEquipmentUIControl addEquipment = new ReplaceEquipmentUIControl(unit, item, stack); + addEquipment.Show(); + } + } } -} +} \ No newline at end of file