Further separation of client-specific code

Still not done here, just gonna push a commit now so I can pull this from elsewhere.
This commit is contained in:
juanjp600
2017-06-16 16:02:07 -03:00
parent e4a878113f
commit 7168a534ed
64 changed files with 3733 additions and 2954 deletions
@@ -0,0 +1,82 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Barotrauma.Networking;
using Lidgren.Network;
using System.Collections.Generic;
using Barotrauma.Items.Components;
namespace Barotrauma
{
partial class CharacterInventory : Inventory
{
public Vector2[] SlotPositions;
private GUIButton[] useOnSelfButton;
void InitProjSpecific()
{
useOnSelfButton = new GUIButton[2];
if (icons == null) icons = TextureLoader.FromFile("Content/UI/inventoryIcons.png");
SlotPositions = new Vector2[limbSlots.Length];
int rectWidth = 40, rectHeight = 40;
int spacing = 10;
for (int i = 0; i < SlotPositions.Length; i++)
{
switch (i)
{
//head, torso, legs
case 0:
case 1:
case 2:
SlotPositions[i] = new Vector2(
spacing,
GameMain.GraphicsHeight - (spacing + rectHeight) * (3 - i));
break;
//lefthand, righthand
case 3:
case 4:
SlotPositions[i] = new Vector2(
spacing * 2 + rectWidth + (spacing + rectWidth) * (i - 2),
GameMain.GraphicsHeight - (spacing + rectHeight) * 3);
useOnSelfButton[i - 3] = new GUIButton(
new Rectangle((int)SlotPositions[i].X, (int)(SlotPositions[i].Y - spacing - rectHeight),
rectWidth, rectHeight), "Use", "")
{
UserData = i,
OnClicked = UseItemOnSelf
};
break;
case 5:
SlotPositions[i] = new Vector2(
spacing * 2 + rectWidth + (spacing + rectWidth) * (i - 5),
GameMain.GraphicsHeight - (spacing + rectHeight) * 3);
break;
default:
SlotPositions[i] = new Vector2(
spacing * 2 + rectWidth + (spacing + rectWidth) * ((i - 6) % 5),
GameMain.GraphicsHeight - (spacing + rectHeight) * ((i > 10) ? 2 : 1));
break;
}
}
}
private bool UseItemOnSelf(GUIButton button, object obj)
{
if (!(obj is int)) return false;
int slotIndex = (int)obj;
return UseItemOnSelf(slotIndex);
}
}
}
@@ -0,0 +1,89 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
partial class ItemLabel : ItemComponent, IDrawableComponent
{
private GUITextBlock textBlock;
private Color textColor;
[HasDefaultValue("", true), Editable(100)]
public string Text
{
get { return textBlock.Text.Replace("\n", ""); }
set
{
if (value == TextBlock.Text || item.Rect.Width < 5) return;
if (textBlock.Rect.Width != item.Rect.Width || textBlock.Rect.Height != item.Rect.Height)
{
textBlock = null;
}
TextBlock.Text = value;
}
}
[Editable, HasDefaultValue("0.0,0.0,0.0,1.0", true)]
public string TextColor
{
get { return ToolBox.Vector4ToString(textColor.ToVector4()); }
set
{
textColor = new Color(ToolBox.ParseToVector4(value));
if (textBlock != null) textBlock.TextColor = textColor;
}
}
[Editable, HasDefaultValue(1.0f, true)]
public float TextScale
{
get { return textBlock == null ? 1.0f : textBlock.TextScale; }
set
{
if (textBlock != null) textBlock.TextScale = MathHelper.Clamp(value, 0.1f, 10.0f);
}
}
private GUITextBlock TextBlock
{
get
{
if (textBlock == null)
{
textBlock = new GUITextBlock(new Rectangle(item.Rect.X, -item.Rect.Y, item.Rect.Width, item.Rect.Height), "",
Color.Transparent, textColor,
Alignment.TopLeft, Alignment.Center,
null, null, true);
textBlock.Font = GUI.SmallFont;
textBlock.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
textBlock.TextDepth = item.Sprite.Depth - 0.0001f;
textBlock.TextScale = TextScale;
}
return textBlock;
}
}
public override void Move(Vector2 amount)
{
textBlock.Rect = new Rectangle(item.Rect.X, -item.Rect.Y, item.Rect.Width, item.Rect.Height);
}
public ItemLabel(Item item, XElement element)
: base(item, element)
{
}
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
var drawPos = new Vector2(
item.DrawPosition.X - item.Rect.Width / 2.0f,
-(item.DrawPosition.Y + item.Rect.Height / 2.0f));
textBlock.Draw(spriteBatch, drawPos - textBlock.Rect.Location.ToVector2());
}
}
}
@@ -0,0 +1,191 @@
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Barotrauma
{
partial class FixRequirement
{
private static GUIFrame frame;
public bool CanBeFixed(Character character, GUIComponent reqFrame = null)
{
foreach (string itemName in requiredItems)
{
Item item = character.Inventory.FindItem(itemName);
bool itemFound = (item != null);
if (reqFrame != null)
{
GUIComponent component = reqFrame.children.Find(c => c.UserData as string == itemName);
GUITextBlock text = component as GUITextBlock;
if (text != null) text.TextColor = itemFound ? Color.LightGreen : Color.Red;
}
}
foreach (Skill skill in requiredSkills)
{
float characterSkill = character.GetSkillLevel(skill.Name);
bool sufficientSkill = characterSkill >= skill.Level;
if (reqFrame != null)
{
GUIComponent component = reqFrame.children.Find(c => c.UserData as Skill == skill);
GUITextBlock text = component as GUITextBlock;
if (text != null) text.TextColor = sufficientSkill ? Color.LightGreen : Color.Red;
}
}
return CanBeFixed(character);
}
private static void CreateGUIFrame(Item item)
{
int width = 400, height = 500;
int y = 0;
frame = new GUIFrame(new Rectangle(0, 0, width, height), null, Alignment.Center, "");
frame.Padding = new Vector4(20.0f, 20.0f, 20.0f, 20.0f);
frame.UserData = item;
new GUITextBlock(new Rectangle(0, 0, 200, 20), "Attempting to fix " + item.Name, "", frame);
y = y + 40;
foreach (FixRequirement requirement in item.FixRequirements)
{
GUIFrame reqFrame = new GUIFrame(
new Rectangle(0, y, 0, 20 + Math.Max(requirement.requiredItems.Count, requirement.requiredSkills.Count) * 15),
Color.Transparent, null, frame);
reqFrame.UserData = requirement;
var fixButton = new GUIButton(new Rectangle(0, 0, 50, 20), "Fix", "", reqFrame);
fixButton.OnClicked = FixButtonPressed;
fixButton.UserData = requirement;
var tickBox = new GUITickBox(new Rectangle(70, 0, 20, 20), requirement.name, Alignment.Left, reqFrame);
tickBox.Enabled = false;
int y2 = 20;
foreach (string itemName in requirement.requiredItems)
{
var itemBlock = new GUITextBlock(new Rectangle(30, y2, 200, 15), itemName, "", reqFrame);
itemBlock.Font = GUI.SmallFont;
itemBlock.UserData = itemName;
y2 += 15;
}
y2 = 20;
foreach (Skill skill in requirement.requiredSkills)
{
var skillBlock = new GUITextBlock(new Rectangle(0, y2, 200, 15), skill.Name + " - " + skill.Level, "", Alignment.Right, Alignment.TopLeft, reqFrame);
skillBlock.Font = GUI.SmallFont;
skillBlock.UserData = skill;
y2 += 15;
}
y += reqFrame.Rect.Height;
}
}
private static bool FixButtonPressed(GUIButton button, object obj)
{
FixRequirement requirement = obj as FixRequirement;
if (requirement == null) return true;
Item item = frame.UserData as Item;
if (item == null) return true;
if (!requirement.CanBeFixed(Character.Controlled, button.Parent)) return true;
if (GameMain.Client != null)
{
GameMain.Client.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.Repair, item.FixRequirements.IndexOf(requirement) });
}
else if (GameMain.Server != null)
{
GameMain.Server.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.Status });
requirement.Fixed = true;
}
else
{
requirement.Fixed = true;
}
return true;
}
private static void UpdateGUIFrame(Item item, Character character)
{
if (frame == null) return;
bool unfixedFound = false;
foreach (GUIComponent child in frame.children)
{
FixRequirement requirement = child.UserData as FixRequirement;
if (requirement == null) continue;
if (requirement.Fixed)
{
child.Color = Color.LightGreen * 0.3f;
child.GetChild<GUITickBox>().Selected = true;
}
else
{
bool canBeFixed = requirement.CanBeFixed(character, child);
unfixedFound = true;
//child.GetChild<GUITickBox>().Selected = canBeFixed;
GUITickBox tickBox = child.GetChild<GUITickBox>();
if (tickBox.Selected)
{
tickBox.Selected = canBeFixed;
requirement.Fixed = canBeFixed;
}
child.Color = Color.Red * 0.2f;
//tickBox.State = GUIComponent.ComponentState.None;
}
}
if (!unfixedFound)
{
item.Condition = 100.0f;
frame = null;
}
}
public static void DrawHud(SpriteBatch spriteBatch, Item item, Character character)
{
if (frame == null) return;
frame.Draw(spriteBatch);
}
public static void AddToGUIUpdateList()
{
if (frame == null) return;
frame.AddToGUIUpdateList();
}
public static void UpdateHud(Item item, Character character)
{
if (frame == null || frame.UserData != item)
{
CreateGUIFrame(item);
}
UpdateGUIFrame(item, character);
if (frame == null) return;
frame.Update((float)Timing.Step);
}
}
}
+203
View File
@@ -0,0 +1,203 @@
using System.Linq;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Barotrauma.Networking;
using System;
using System.Collections.Generic;
using Barotrauma.Items.Components;
namespace Barotrauma
{
partial class InventorySlot
{
public GUIComponent.ComponentState State;
public bool IsHighlighted
{
get
{
return State == GUIComponent.ComponentState.Hover;
}
}
public Color Color;
public Color BorderHighlightColor;
private CoroutineHandle BorderHighlightCoroutine;
public void ShowBorderHighlight(Color color, float fadeInDuration, float fadeOutDuration)
{
if (BorderHighlightCoroutine != null)
{
BorderHighlightCoroutine = null;
}
BorderHighlightCoroutine = CoroutineManager.StartCoroutine(UpdateBorderHighlight(color, fadeInDuration, fadeOutDuration));
}
private IEnumerable<object> UpdateBorderHighlight(Color color, float fadeInDuration, float fadeOutDuration)
{
float t = 0.0f;
while (t < fadeInDuration + fadeOutDuration)
{
BorderHighlightColor = (t < fadeInDuration) ?
Color.Lerp(Color.Transparent, color, t / fadeInDuration) :
Color.Lerp(color, Color.Transparent, (t - fadeInDuration) / fadeOutDuration);
t += CoroutineManager.DeltaTime;
yield return CoroutineStatus.Running;
}
yield return CoroutineStatus.Success;
}
}
partial class Inventory
{
public virtual void Draw(SpriteBatch spriteBatch, bool subInventory = false)
{
if (slots == null || isSubInventory != subInventory) return;
for (int i = 0; i < capacity; i++)
{
if (slots[i].Disabled) continue;
//don't draw the item if it's being dragged out of the slot
bool drawItem = draggingItem == null || draggingItem != Items[i] || slots[i].IsHighlighted;
DrawSlot(spriteBatch, slots[i], Items[i], drawItem);
}
if (draggingItem != null &&
(draggingSlot == null || (!draggingSlot.Rect.Contains(PlayerInput.MousePosition) && draggingItem.ParentInventory == this)))
{
Rectangle dragRect = new Rectangle(
(int)PlayerInput.MousePosition.X - 10,
(int)PlayerInput.MousePosition.Y - 10,
40, 40);
DrawSlot(spriteBatch, new InventorySlot(dragRect), draggingItem);
}
for (int i = 0; i < capacity; i++)
{
if (slots[i].IsHighlighted && !slots[i].Disabled && Items[i] != null)
{
string toolTip = "";
if (GameMain.DebugDraw)
{
toolTip = Items[i].ToString();
}
else
{
toolTip = string.IsNullOrEmpty(Items[i].Description) ?
Items[i].Name :
Items[i].Name + '\n' + Items[i].Description;
}
DrawToolTip(spriteBatch, toolTip, slots[i].Rect);
break;
}
}
}
protected void DrawToolTip(SpriteBatch spriteBatch, string toolTip, Rectangle highlightedSlot)
{
int maxWidth = 300;
toolTip = ToolBox.WrapText(toolTip, maxWidth, GUI.Font);
Vector2 textSize = GUI.Font.MeasureString(toolTip);
Vector2 rectSize = textSize * 1.2f;
Vector2 pos = new Vector2(highlightedSlot.Right, highlightedSlot.Y - rectSize.Y);
pos.X = (int)(pos.X + 3);
pos.Y = (int)pos.Y;
GUI.DrawRectangle(spriteBatch, pos, rectSize, Color.Black * 0.8f, true);
GUI.Font.DrawString(spriteBatch, toolTip,
new Vector2((int)(pos.X + rectSize.X * 0.5f), (int)(pos.Y + rectSize.Y * 0.5f)),
Color.White, 0.0f,
new Vector2((int)(textSize.X * 0.5f), (int)(textSize.Y * 0.5f)),
1.0f, SpriteEffects.None, 0.0f);
}
public void DrawSubInventory(SpriteBatch spriteBatch, int slotIndex)
{
var item = Items[slotIndex];
if (item == null) return;
var container = item.GetComponent<ItemContainer>();
if (container == null) return;
if (container.Inventory.slots == null || !container.Inventory.isSubInventory) return;
int itemCapacity = container.Capacity;
#if DEBUG
System.Diagnostics.Debug.Assert(slotIndex >= 0 && slotIndex < Items.Length);
#else
if (slotIndex < 0 || slotIndex >= Items.Length) return;
#endif
var slot = slots[slotIndex];
Rectangle containerRect = new Rectangle(slot.Rect.X - 5, slot.Rect.Y - (40 + 10) * itemCapacity - 5,
slot.Rect.Width + 10, slot.Rect.Height + (40 + 10) * itemCapacity + 10);
GUI.DrawRectangle(spriteBatch, new Rectangle(containerRect.X, containerRect.Y, containerRect.Width, containerRect.Height - slot.Rect.Height - 5), Color.Black * 0.8f, true);
GUI.DrawRectangle(spriteBatch, containerRect, Color.White);
container.Inventory.Draw(spriteBatch, true);
if (!containerRect.Contains(PlayerInput.MousePosition))
{
if (draggingItem == null || draggingItem.Container != item) selectedSlot = -1;
}
}
protected void DrawSlot(SpriteBatch spriteBatch, InventorySlot slot, Item item, bool drawItem = true)
{
Rectangle rect = slot.Rect;
GUI.DrawRectangle(spriteBatch, rect, (slot.IsHighlighted ? Color.Red * 0.4f : slot.Color), true);
if (item != null && drawItem)
{
if (item.Condition < 100.0f)
{
GUI.DrawRectangle(spriteBatch, new Rectangle(rect.X, rect.Bottom - 8, rect.Width, 8), Color.Black * 0.8f, true);
GUI.DrawRectangle(spriteBatch,
new Rectangle(rect.X, rect.Bottom - 8, (int)(rect.Width * item.Condition / 100.0f), 8),
Color.Lerp(Color.Red, Color.Green, item.Condition / 100.0f) * 0.8f, true);
}
var containedItems = item.ContainedItems;
if (containedItems != null && containedItems.Length == 1 && containedItems[0].Condition < 100.0f)
{
GUI.DrawRectangle(spriteBatch, new Rectangle(rect.X, rect.Y, rect.Width, 8), Color.Black * 0.8f, true);
GUI.DrawRectangle(spriteBatch,
new Rectangle(rect.X, rect.Y, (int)(rect.Width * containedItems[0].Condition / 100.0f), 8),
Color.Lerp(Color.Red, Color.Green, containedItems[0].Condition / 100.0f) * 0.8f, true);
}
}
GUI.DrawRectangle(spriteBatch, rect, (slot.IsHighlighted ? Color.Red * 0.4f : slot.Color), false);
if (slot.BorderHighlightColor != Color.Transparent)
{
Rectangle highlightRect = slot.Rect;
highlightRect.Inflate(3, 3);
GUI.DrawRectangle(spriteBatch, highlightRect, slot.BorderHighlightColor, false, 0, 5);
}
if (item == null || !drawItem) return;
item.Sprite.Draw(spriteBatch, new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), item.Color);
}
}
}