Add item rendering for inventory
This commit is contained in:
parent
90f39b5696
commit
3550bcb1a1
@ -162,8 +162,7 @@ namespace TrueCraft.Client.Modules
|
|||||||
continue;
|
continue;
|
||||||
var position = origin + new Point((int)Scale(i * 20), 0);
|
var position = origin + new Point((int)Scale(i * 20), 0);
|
||||||
var rect = new Rectangle(position, scale);
|
var rect = new Rectangle(position, scale);
|
||||||
IconRenderer.RenderBlockIcon(Game, Items, provider,
|
IconRenderer.RenderBlockIcon(Game, provider, (byte)item.Metadata, rect);
|
||||||
(byte)item.Metadata, rect, Color.White); // TODO: Fuck, metadata was supposed to be a short
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +177,7 @@ namespace TrueCraft.Client.Modules
|
|||||||
var item = Game.Client.Inventory.Hotbar[i];
|
var item = Game.Client.Inventory.Hotbar[i];
|
||||||
if (item.Empty || item.Count == 1)
|
if (item.Empty || item.Count == 1)
|
||||||
continue;
|
continue;
|
||||||
int offset = 12;
|
int offset = 10;
|
||||||
if (item.Count >= 10)
|
if (item.Count >= 10)
|
||||||
offset -= 6;
|
offset -= 6;
|
||||||
var position = origin + new Point((int)Scale(i * 20 + offset), (int)Scale(5));
|
var position = origin + new Point((int)Scale(i * 20 + offset), (int)Scale(5));
|
||||||
|
@ -6,6 +6,10 @@ using TrueCraft.API.Logic;
|
|||||||
using TrueCraft.Client.Input;
|
using TrueCraft.Client.Input;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using TrueCraft.Core.Networking.Packets;
|
using TrueCraft.Core.Networking.Packets;
|
||||||
|
using TrueCraft.API.Windows;
|
||||||
|
using System;
|
||||||
|
using TrueCraft.API;
|
||||||
|
using TrueCraft.Core.Windows;
|
||||||
|
|
||||||
namespace TrueCraft.Client.Modules
|
namespace TrueCraft.Client.Modules
|
||||||
{
|
{
|
||||||
@ -16,7 +20,14 @@ namespace TrueCraft.Client.Modules
|
|||||||
private Texture2D Inventory { get; set; }
|
private Texture2D Inventory { get; set; }
|
||||||
private Texture2D Items { get; set; }
|
private Texture2D Items { get; set; }
|
||||||
private FontRenderer Font { get; set; }
|
private FontRenderer Font { get; set; }
|
||||||
private Texture2D Background { get; set; }
|
private int SelectedSlot { get; set; }
|
||||||
|
|
||||||
|
private enum RenderStage
|
||||||
|
{
|
||||||
|
Sprites,
|
||||||
|
Models,
|
||||||
|
Text
|
||||||
|
}
|
||||||
|
|
||||||
public WindowModule(TrueCraftGame game, FontRenderer font)
|
public WindowModule(TrueCraftGame game, FontRenderer font)
|
||||||
{
|
{
|
||||||
@ -25,8 +36,7 @@ namespace TrueCraft.Client.Modules
|
|||||||
SpriteBatch = new SpriteBatch(game.GraphicsDevice);
|
SpriteBatch = new SpriteBatch(game.GraphicsDevice);
|
||||||
Inventory = game.TextureMapper.GetTexture("gui/inventory.png");
|
Inventory = game.TextureMapper.GetTexture("gui/inventory.png");
|
||||||
Items = game.TextureMapper.GetTexture("gui/items.png");
|
Items = game.TextureMapper.GetTexture("gui/items.png");
|
||||||
Background = new Texture2D(game.GraphicsDevice, 1, 1);
|
SelectedSlot = -1;
|
||||||
Background.SetData<Color>(new[] { new Color(Color.Black, 180) });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly Rectangle InventoryWindowRect = new Rectangle(0, 0, 176, 166);
|
private static readonly Rectangle InventoryWindowRect = new Rectangle(0, 0, 176, 166);
|
||||||
@ -36,12 +46,30 @@ namespace TrueCraft.Client.Modules
|
|||||||
if (Game.Client.CurrentWindow != null)
|
if (Game.Client.CurrentWindow != null)
|
||||||
{
|
{
|
||||||
SpriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
|
SpriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
|
||||||
SpriteBatch.Draw(Background, new Rectangle(0, 0,
|
SpriteBatch.Draw(Game.White1x1, new Rectangle(0, 0,
|
||||||
Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), Color.White);
|
Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), new Color(Color.Black, 180));
|
||||||
switch (Game.Client.CurrentWindow.Type)
|
switch (Game.Client.CurrentWindow.Type)
|
||||||
{
|
{
|
||||||
case -1:
|
case -1:
|
||||||
DrawInventoryWindow();
|
SpriteBatch.Draw(Inventory, new Vector2(
|
||||||
|
Game.GraphicsDevice.Viewport.Width / 2 - Scale(InventoryWindowRect.Width / 2),
|
||||||
|
Game.GraphicsDevice.Viewport.Height / 2 - Scale(InventoryWindowRect.Height / 2)),
|
||||||
|
InventoryWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
||||||
|
DrawInventoryWindow(RenderStage.Sprites);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
SpriteBatch.End();
|
||||||
|
switch (Game.Client.CurrentWindow.Type)
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
|
DrawInventoryWindow(RenderStage.Models);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
SpriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
|
||||||
|
switch (Game.Client.CurrentWindow.Type)
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
|
DrawInventoryWindow(RenderStage.Text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
SpriteBatch.End();
|
SpriteBatch.End();
|
||||||
@ -71,12 +99,75 @@ namespace TrueCraft.Client.Modules
|
|||||||
return base.KeyDown(gameTime, e);
|
return base.KeyDown(gameTime, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawInventoryWindow()
|
private void DrawInventoryWindow(RenderStage stage)
|
||||||
{
|
{
|
||||||
SpriteBatch.Draw(Inventory, new Vector2(
|
DrawWindowArea(Game.Client.Inventory.MainInventory, 8, 84, InventoryWindowRect, stage);
|
||||||
Game.GraphicsDevice.Viewport.Width / 2 - Scale(InventoryWindowRect.Width / 2),
|
DrawWindowArea(Game.Client.Inventory.Hotbar, 8, 142, InventoryWindowRect, stage);
|
||||||
Game.GraphicsDevice.Viewport.Height / 2 - Scale(InventoryWindowRect.Height / 2)),
|
DrawWindowArea(Game.Client.Inventory.CraftingGrid, 88, 26, InventoryWindowRect, stage);
|
||||||
InventoryWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
DrawWindowArea(Game.Client.Inventory.Armor, 8, 8, InventoryWindowRect, stage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawWindowArea(IWindowArea area, int _x, int _y, Rectangle frame, RenderStage stage)
|
||||||
|
{
|
||||||
|
var mouse = Mouse.GetState().Position.ToVector2();
|
||||||
|
var scale = new Point((int)(16 * Game.ScaleFactor * 2));
|
||||||
|
var origin = new Point((int)(
|
||||||
|
Game.GraphicsDevice.Viewport.Width / 2 - Scale(frame.Width / 2) + Scale(_x)),
|
||||||
|
(int)(Game.GraphicsDevice.Viewport.Height / 2 - Scale(frame.Height / 2) + Scale(_y)));
|
||||||
|
for (int i = 0; i < area.Length; i++)
|
||||||
|
{
|
||||||
|
var item = area[i];
|
||||||
|
int x = (int)((i % area.Width) * Scale(18));
|
||||||
|
int y = (int)((i / area.Width) * Scale(18));
|
||||||
|
if (area is CraftingWindowArea)
|
||||||
|
{
|
||||||
|
// yes I know this is a crappy hack, bite me
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
if (area.Width == 2)
|
||||||
|
{
|
||||||
|
x = (int)Scale(144 - _x);
|
||||||
|
y = (int)Scale(36 - _y);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
x = (int)Scale(119);
|
||||||
|
y = (int)Scale(30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
x = (int)((i % area.Width) * Scale(18));
|
||||||
|
y = (int)((i / area.Width) * Scale(18));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var position = origin + new Point(x, y);
|
||||||
|
var rect = new Rectangle(position, scale);
|
||||||
|
if (stage == RenderStage.Sprites && rect.Contains(mouse))
|
||||||
|
{
|
||||||
|
SelectedSlot = area.StartIndex + i;
|
||||||
|
SpriteBatch.Draw(Game.White1x1, rect, Color.LightGray);
|
||||||
|
}
|
||||||
|
if (item.Empty)
|
||||||
|
continue;
|
||||||
|
var provider = Game.ItemRepository.GetItemProvider(item.ID);
|
||||||
|
var texture = provider.GetIconTexture((byte)item.Metadata);
|
||||||
|
if (texture != null && stage == RenderStage.Sprites)
|
||||||
|
IconRenderer.RenderItemIcon(SpriteBatch, Items, provider,
|
||||||
|
(byte)item.Metadata, rect, Color.White);
|
||||||
|
if (texture == null && stage == RenderStage.Models && provider is IBlockProvider)
|
||||||
|
IconRenderer.RenderBlockIcon(Game, provider as IBlockProvider, (byte)item.Metadata, rect);
|
||||||
|
if (stage == RenderStage.Text && item.Count > 1)
|
||||||
|
{
|
||||||
|
int offset = 10;
|
||||||
|
if (item.Count >= 10)
|
||||||
|
offset -= 6;
|
||||||
|
position += new Point((int)Scale(offset), (int)Scale(5));
|
||||||
|
Font.DrawText(SpriteBatch, position.X, position.Y, item.Count.ToString(), Game.ScaleFactor);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update(GameTime gameTime)
|
public override void Update(GameTime gameTime)
|
||||||
@ -85,11 +176,6 @@ namespace TrueCraft.Client.Modules
|
|||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Scales a float depending on the game's scale factor.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private float Scale(float value)
|
private float Scale(float value)
|
||||||
{
|
{
|
||||||
return value * Game.ScaleFactor * 2;
|
return value * Game.ScaleFactor * 2;
|
||||||
|
@ -53,14 +53,34 @@ namespace TrueCraft.Client.Rendering
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public Point MeasureText(string text, float scale = 1.0f)
|
||||||
///
|
{
|
||||||
/// </summary>
|
var dx = 0;
|
||||||
/// <param name="spriteBatch"></param>
|
var height = 0;
|
||||||
/// <param name="x"></param>
|
var font = Fonts[0];
|
||||||
/// <param name="y"></param>
|
for (int i = 0; i < text.Length; i++)
|
||||||
/// <param name="text"></param>
|
{
|
||||||
/// <param name="scale"></param>
|
if (text[i] == '§')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
var code = string.Format("§{0}", text[i]);
|
||||||
|
if (ChatFormat.IsValid(code))
|
||||||
|
font = GetFont(code);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var glyph = font.GetGlyph(text[i]);
|
||||||
|
if (glyph != null)
|
||||||
|
{
|
||||||
|
dx += (int)(glyph.XAdvance * scale);
|
||||||
|
if (glyph.Height > height)
|
||||||
|
height = glyph.Height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Point(dx, height);
|
||||||
|
}
|
||||||
|
|
||||||
public void DrawText(SpriteBatch spriteBatch, int x, int y, string text, float scale = 1.0f, byte alpha = 255)
|
public void DrawText(SpriteBatch spriteBatch, int x, int y, string text, float scale = 1.0f, byte alpha = 255)
|
||||||
{
|
{
|
||||||
var dx = x;
|
var dx = x;
|
||||||
@ -91,12 +111,12 @@ namespace TrueCraft.Client.Rendering
|
|||||||
(int)(glyph.Width * scale),
|
(int)(glyph.Width * scale),
|
||||||
(int)(glyph.Height * scale));
|
(int)(glyph.Height * scale));
|
||||||
var shadowRectangle = new Rectangle(
|
var shadowRectangle = new Rectangle(
|
||||||
dx + (int)(glyph.XOffset * scale) + 2,
|
dx + (int)(glyph.XOffset * scale) + 4,
|
||||||
dy + (int)(glyph.YOffset * scale) + 2,
|
dy + (int)(glyph.YOffset * scale) + 4,
|
||||||
(int)(glyph.Width * scale),
|
(int)(glyph.Width * scale),
|
||||||
(int)(glyph.Height * scale));
|
(int)(glyph.Height * scale));
|
||||||
|
|
||||||
spriteBatch.Draw(font.GetTexture(glyph.Page), shadowRectangle, sourceRectangle, new Color(63, 63, 21, alpha));
|
spriteBatch.Draw(font.GetTexture(glyph.Page), shadowRectangle, sourceRectangle, new Color(21, 21, 21, alpha));
|
||||||
spriteBatch.Draw(font.GetTexture(glyph.Page), destRectangle, sourceRectangle, new Color(color, alpha));
|
spriteBatch.Draw(font.GetTexture(glyph.Page), destRectangle, sourceRectangle, new Color(color, alpha));
|
||||||
dx += (int)(glyph.XAdvance * scale);
|
dx += (int)(glyph.XAdvance * scale);
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,10 @@ namespace TrueCraft.Client.Rendering
|
|||||||
RenderEffect.Texture = game.TextureMapper.GetTexture("terrain.png");
|
RenderEffect.Texture = game.TextureMapper.GetTexture("terrain.png");
|
||||||
RenderEffect.TextureEnabled = true;
|
RenderEffect.TextureEnabled = true;
|
||||||
RenderEffect.VertexColorEnabled = true;
|
RenderEffect.VertexColorEnabled = true;
|
||||||
RenderEffect.EnableDefaultLighting();
|
RenderEffect.LightingEnabled = true;
|
||||||
// TODO: Figure out how to make the lighting give the cubes some sense of depth
|
RenderEffect.DirectionalLight0.Direction = new Vector3(10, -10, -0.8f);
|
||||||
RenderEffect.DirectionalLight0.Direction = new Vector3(-0.75f, -0.75f, -0.75f);
|
RenderEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3();
|
||||||
|
RenderEffect.DirectionalLight0.Enabled = true;
|
||||||
RenderEffect.Projection = Matrix.CreateOrthographicOffCenter(
|
RenderEffect.Projection = Matrix.CreateOrthographicOffCenter(
|
||||||
0, game.GraphicsDevice.Viewport.Width,
|
0, game.GraphicsDevice.Viewport.Width,
|
||||||
0, game.GraphicsDevice.Viewport.Height,
|
0, game.GraphicsDevice.Viewport.Height,
|
||||||
@ -53,8 +54,7 @@ namespace TrueCraft.Client.Rendering
|
|||||||
spriteBatch.Draw(texture, destination, source, color);
|
spriteBatch.Draw(texture, destination, source, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RenderBlockIcon(TrueCraftGame game, Texture2D texture, IBlockProvider provider,
|
public static void RenderBlockIcon(TrueCraftGame game, IBlockProvider provider, byte metadata, Rectangle destination)
|
||||||
byte metadata, Rectangle destination, Color color)
|
|
||||||
{
|
{
|
||||||
var mesh = BlockMeshes[provider.ID];
|
var mesh = BlockMeshes[provider.ID];
|
||||||
if (mesh != null)
|
if (mesh != null)
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
<PlatformTarget>x86</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<Commandlineparameters>localhost TestUser</Commandlineparameters>
|
<Commandlineparameters>localhost SirCmpwn</Commandlineparameters>
|
||||||
<DebugType>
|
<DebugType>
|
||||||
</DebugType>
|
</DebugType>
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
@ -40,6 +40,7 @@ namespace TrueCraft.Client
|
|||||||
public DateTime EndDigging { get; set; }
|
public DateTime EndDigging { get; set; }
|
||||||
public Coordinates3D TargetBlock { get; set; }
|
public Coordinates3D TargetBlock { get; set; }
|
||||||
public AudioManager Audio { get; set; }
|
public AudioManager Audio { get; set; }
|
||||||
|
public Texture2D White1x1 { get; set; }
|
||||||
|
|
||||||
private List<IGameplayModule> InputModules { get; set; }
|
private List<IGameplayModule> InputModules { get; set; }
|
||||||
private List<IGameplayModule> GraphicalModules { get; set; }
|
private List<IGameplayModule> GraphicalModules { get; set; }
|
||||||
@ -119,6 +120,9 @@ namespace TrueCraft.Client
|
|||||||
|
|
||||||
base.Initialize(); // (calls LoadContent)
|
base.Initialize(); // (calls LoadContent)
|
||||||
|
|
||||||
|
White1x1 = new Texture2D(GraphicsDevice, 1, 1);
|
||||||
|
White1x1.SetData<Color>(new[] { Color.White });
|
||||||
|
|
||||||
Audio = new AudioManager();
|
Audio = new AudioManager();
|
||||||
Audio.LoadDefaultPacks(Content);
|
Audio.LoadDefaultPacks(Content);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user