Merge pull request #80 from progtime-net/GUInew

Gu inew
This commit is contained in:
SergoDobro 2024-08-19 12:41:45 +03:00 committed by GitHub
commit 06ce01e3a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 408 additions and 54 deletions

View file

@ -0,0 +1,91 @@
using Microsoft.Xna.Framework.Graphics;
using MonogameLibrary.UI.Base;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonogameLibrary.UI.Enums;
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Content;
namespace MonogameLibrary.UI.Elements;
public class HoverWindow : DrawableUIElement
{
public string tagItem;
public string discriptions;
public Dictionary<string, int> resourcesNeededToCraft;
private List<Label> Labels = new List<Label>();
public float scale2;
public string fontName2;
public Color fontColor2;
public string itemTextureName1;
private DrawableUIElement icon;
private Label itemName;
private Label discription;
private int labelIndex = 0;
public HoverWindow(UIManager manager, int layerIndex = 0, string textureName = "") : base(manager, layerIndex, textureName)
{
}
public void Initialize(ContentManager content)
{
icon = new DrawableUIElement(Manager)
{
rectangle = new Rectangle(rectangle.X + rectangle.Width / 20, rectangle.Y + rectangle.Width / 20, rectangle.Width / 5, rectangle.Width / 5),
mainColor = Color.White, textureName = itemTextureName1
};
icon.LoadTexture(content);
itemName = new Label(Manager)
{
rectangle = new Rectangle(rectangle.X + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 20, rectangle.Y + rectangle.Width / 10, rectangle.Width / 3 * 2, rectangle.Width / 5 ),
fontColor = fontColor2, text = tagItem, scale = scale2, fontName = fontName2, mainColor = Color.Transparent, textAligment = TextAligment.Left
};
itemName.LoadTexture(content);
discription = new Label(Manager)
{
rectangle = new Rectangle(rectangle.X + rectangle.Width / 20, rectangle.Y + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 20, rectangle.Width / 5, rectangle.Width / 5),
fontColor = fontColor2, text = discriptions, scale = scale2 / 1.5f, fontName = fontName2, mainColor = Color.Transparent, textAligment = TextAligment.Left
};
discription.LoadTexture(content);
foreach (var resource in resourcesNeededToCraft)
{
Label res = new Label(Manager)
{
rectangle = new Rectangle(rectangle.X + rectangle.Width / 2 - rectangle.Width / 3 / 2, rectangle.Y + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 5 + rectangle.Width / 20 + (rectangle.Width / 10 + rectangle.Width / 40) * labelIndex, rectangle.Width / 3, rectangle.Width / 10),
fontColor = fontColor2, text = resource.Key + " " + resource.Value, scale = scale2, fontName = fontName2, mainColor = Color.Transparent
};
res.LoadTexture(content);
Labels.Add(res);
labelIndex++;
}
}
public override void LoadTexture(ContentManager content)
{
base.LoadTexture(content);
}
public void Update(GameTime gameTime)
{
labelIndex = 0;
icon.rectangle = new Rectangle(rectangle.X + rectangle.Width / 20, rectangle.Y + rectangle.Width / 20, rectangle.Width / 5, rectangle.Width / 5);
itemName.rectangle = new Rectangle(rectangle.X + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 20,
rectangle.Y + rectangle.Width / 10, rectangle.Width / 3 * 2, rectangle.Width / 5);
discription.rectangle = new Rectangle(rectangle.X + rectangle.Width / 20,
rectangle.Y + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 20, rectangle.Width / 5,
rectangle.Width / 5);
foreach (var label in Labels)
{
label.rectangle = new Rectangle(rectangle.X + rectangle.Width / 2 - rectangle.Width / 3 / 2,
rectangle.Y + rectangle.Width / 20 + rectangle.Width / 5 + rectangle.Width / 5 + rectangle.Width / 20 +
(rectangle.Width / 10 + rectangle.Width / 40) * labelIndex, rectangle.Width / 3, rectangle.Width / 10);
labelIndex++;
}
}
}

View file

@ -9,10 +9,11 @@ using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace MonogameLibrary.UI.Elements;
public class ItemDisplayButton : DrawableUIElement
public class ItemDisplayButton : Button
{
public int count;
public string itemTextureName;
@ -23,6 +24,11 @@ public class ItemDisplayButton : DrawableUIElement
public string text1;
public float scale1;
private DrawableUIElement icon;
private HoverState presentState = HoverState.None;
private HoverWindow hoverWindow = null;
private ContentManager content;
public string discriptions1;
public Dictionary<string, int> resourcesNeededToCraft1;
public ItemDisplayButton(UIManager manager) : base(manager)
@ -51,6 +57,7 @@ public class ItemDisplayButton : DrawableUIElement
public override void LoadTexture(ContentManager content)
{
this.content = content;
icon.LoadTexture(content);
base.LoadTexture(content);
if (itemTextureName == "")
@ -72,6 +79,47 @@ public class ItemDisplayButton : DrawableUIElement
}
}
public bool Update(GameTime gameTime)
{
if (hoverState == HoverState.Hovering)
{
if (presentState != hoverState)
{
hoverWindow = new HoverWindow(Manager)
{
rectangle = new Rectangle(Mouse.GetState().Position.X, Mouse.GetState().Position.Y, rectangle.Width, rectangle.Height * 10),
mainColor = Color.Gray,
tagItem = text1,
discriptions = discriptions1,
resourcesNeededToCraft = resourcesNeededToCraft1,
fontColor2 = fontColor1,
fontName2 = fontName1,
scale2 = scale1,
itemTextureName1 = itemTextureName
};
hoverWindow.Initialize(content);
hoverWindow.LoadTexture(content);
}
hoverWindow.rectangle.X = Mouse.GetState().Position.X;
hoverWindow.rectangle.Y = Mouse.GetState().Position.Y;
hoverWindow.Update(gameTime);
}
else if (hoverState == HoverState.None)
{
if (presentState != hoverState)
{
return true;
}
}
presentState = hoverState;
return false;
}
public override void Draw(SpriteBatch _spriteBatch)
{
base.Draw(_spriteBatch);

View file

@ -9,6 +9,7 @@ using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace MonogameLibrary.UI.Elements;
@ -23,6 +24,12 @@ public class ItemDisplayLabel : DrawableUIElement
public string text1;
public float scale1;
private DrawableUIElement icon;
private HoverState presentState = HoverState.None;
private HoverWindow hoverWindow = null;
public string discriptions1;
public Dictionary<string, int> resourcesNeededToCraft1;
public HoverState hoverState = HoverState.None;
private ContentManager content;
public ItemDisplayLabel(UIManager manager) : base(manager)
@ -51,6 +58,7 @@ public class ItemDisplayLabel : DrawableUIElement
public override void LoadTexture(ContentManager content)
{
this.content = content;
icon.LoadTexture(content);
base.LoadTexture(content);
if (itemTextureName == "")
@ -71,9 +79,72 @@ public class ItemDisplayLabel : DrawableUIElement
}
}
}
public bool Update(GameTime gameTime)
{
if (rectangle.Intersects(new Rectangle(Mouse.GetState().Position, Point.Zero)))
{
hoverState = HoverState.Hovering;
}
else
{
hoverState = HoverState.None;
}
if (hoverState == HoverState.Hovering)
{
if (presentState != hoverState)
{
if (resourcesNeededToCraft1 == null)
{
resourcesNeededToCraft1 = new Dictionary<string, int>();
}
hoverWindow = new HoverWindow(Manager)
{
rectangle = new Rectangle(Mouse.GetState().Position.X, Mouse.GetState().Position.Y, rectangle.Width, rectangle.Height * 10),
mainColor = Color.Gray,
tagItem = text1,
discriptions = discriptions1,
resourcesNeededToCraft = resourcesNeededToCraft1,
fontColor2 = fontColor1,
fontName2 = fontName1,
scale2 = scale1,
itemTextureName1 = itemTextureName
};
hoverWindow.Initialize(content);
hoverWindow.LoadTexture(content);
}
hoverWindow.rectangle.X = Mouse.GetState().Position.X;
hoverWindow.rectangle.Y = Mouse.GetState().Position.Y;
hoverWindow.Update(gameTime);
}
else if (hoverState == HoverState.None)
{
if (presentState != hoverState)
{
return true;
}
}
presentState = hoverState;
return false;
}
public override void Draw(SpriteBatch _spriteBatch)
{
if (hoverState == HoverState.None)
{
_spriteBatch.Draw(texture, rectangle, Color.White);
}
else if (hoverState == HoverState.Hovering)
{
_spriteBatch.Draw(texture, rectangle, new Color(211, 211, 211));
}
base.Draw(_spriteBatch);
_spriteBatch.Draw(texture, rectangle, Color.White);
}
}

View file

@ -34,6 +34,13 @@
/processorParam:TextureFormat=Compressed
/build:Fonts/Font3.spritefont
#begin Fonts/Font4.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:Fonts/Font4.spritefont
#begin MapData/TileMaps/main.tmj
/copy:MapData/TileMaps/main.tmj

View file

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Troubleside-lgjxX.ttf</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>50</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Regular</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<!-- <DefaultCharacter>*</DefaultCharacter> -->
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start>32</Start>
<End>127</End>
</CharacterRegion>
<CharacterRegion>
<Start>&#1040;</Start>
<End>&#1112;</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>

Binary file not shown.

View file

@ -19,17 +19,20 @@ namespace ZoFo.GameCore.GUI;
public class BaseGUI : AbstractGUI
{
private DrawableUIElement menuBackground;
private List<ItemDisplayLabel> ItemDisplayButtonsList;
private List<ItemDisplayLabel> ItemDisplayLabelsList;
private List<ItemDisplayButton> ItemDisplayButtonsList;
private int buttonIndex = 0;
private string textureName;
protected override void CreateUI()
{
ItemDisplayButtonsList = new List<ItemDisplayLabel>();
ItemDisplayLabelsList = new List<ItemDisplayLabel>();
ItemDisplayButtonsList = new List<ItemDisplayButton>();
int width = AppManager.Instance.CurentScreenResolution.X;
int height = AppManager.Instance.CurentScreenResolution.Y;
Dictionary<string, int> playerItems = AppManager.Instance.playerData.items;
Dictionary<string, ItemInfo> items = AppManager.Instance.ItemManager.tagItemPairs;
int numberCraftItem = 0;
menuBackground = new DrawableUIElement(Manager)
{
@ -46,67 +49,110 @@ public class BaseGUI : AbstractGUI
fontName = "Fonts\\Font"
});
DrawableUIElement baseHudBack = new DrawableUIElement(Manager)
DrawableUIElement baseItemBack = new DrawableUIElement(Manager)
{
rectangle = new Rectangle(width / 2 - (int)(width / 1.5) / 2, height / 2 - (int)(height / 1.5) / 2,
(int)(width / 1.5), (int)(height / 1.5)),
rectangle = new Rectangle(width / 2 - (height / 16 + (int)(width / 2.5)) / 2,
height / 2 - (int)(height / 1.5) / 2,
height / 40 + width / 5, (int)(height / 1.5)),
mainColor = Color.LightGray
};
Elements.Add(baseHudBack);
Elements.Add(baseItemBack);
DrawableUIElement baseCraftBack = new DrawableUIElement(Manager)
{
rectangle = new Rectangle(width / 2 + height / 160, height / 2 - (int)(height / 1.5) / 2,
height / 40 + width / 5, (int)(height / 1.5)),
mainColor = Color.LightGray
};
Elements.Add(baseCraftBack);
//player itams
foreach (var item in playerItems)
{
textureName = AppManager.Instance.ItemManager.GetItemInfo(item.Key).textureName;
var temp = new ItemDisplayLabel(Manager)
if (item.Value > 0)
{
rectangle = new Rectangle(width / 2 - (int)(width / 1.5) / 2 + height / 80,
height / 2 - (int)(height / 1.5) / 2 + height / 80 + (height / 20 + height / 80) * (buttonIndex),
(int)(width / 5), (int)(height / 20)),
text1 = item.Key,
scale1 = 0.4f,
count = item.Value,
itemTextureName = textureName,
fontColor1 = Color.White,
mainColor = Color.Gray,
fontName1 = "Fonts\\Font3"
};
Elements.Add(temp);
temp.Initialize();
temp.LoadTexture(AppManager.Instance.Content);
ItemDisplayButtonsList.Add(temp);
ItemInfo itemInfo = AppManager.Instance.ItemManager.GetItemInfo(item.Key);
var temp = new ItemDisplayLabel(Manager)
{
rectangle = new Rectangle(width / 2 - (height / 16 + (int)(width / 2.5)) / 2 + height / 80,
height / 2 - (int)(height / 1.5) / 2 + height / 80 +
(height / 20 + height / 80) * (buttonIndex),
(int)(width / 5), (int)(height / 20)),
text1 = item.Key,
scale1 = 0.4f,
count = item.Value,
itemTextureName = itemInfo.textureName,
fontColor1 = Color.White,
mainColor = Color.Gray,
fontName1 = "Fonts\\Font4",
discriptions1 = itemInfo.description,
resourcesNeededToCraft1 = itemInfo.resourcesNeededToCraft
};
Elements.Add(temp);
temp.Initialize();
temp.LoadTexture(AppManager.Instance.Content);
ItemDisplayLabelsList.Add(temp);
buttonIndex++;
buttonIndex++;
}
}
// craftable items
buttonIndex = 0;
foreach (var item in items)
{
ItemInfo itemInfo = AppManager.Instance.ItemManager.GetItemInfo(item.Key);
if (itemInfo.isCraftable)
try
{
var temp = new ItemDisplayLabel(Manager)
ItemInfo itemInfo = AppManager.Instance.ItemManager.GetItemInfo(item.Key);
if (itemInfo.isCraftable)
{
rectangle = new Rectangle(width / 2 - (int)(width / 1.5) / 2 + height / 40 + width / 5,
height / 2 - (int)(height / 1.5) / 2 + height / 80 +
(height / 20 + height / 80) * (buttonIndex),
(int)(width / 5), (int)(height / 20)),
text1 = item.Key,
scale1 = 0.4f,
count = 0,
itemTextureName = itemInfo.textureName,
fontColor1 = Color.White,
mainColor = Color.Gray,
fontName1 = "Fonts\\Font3"
};
Elements.Add(temp);
temp.Initialize();
temp.LoadTexture(AppManager.Instance.Content);
ItemDisplayButtonsList.Add(temp);
Dictionary<string, int> needToCraft =
AppManager.Instance.ItemManager.GetItemInfo(item.Key).resourcesNeededToCraft;
numberCraftItem = playerItems[needToCraft.Keys.First()] / needToCraft.Values.First();
foreach (var itemNeedToCraft in needToCraft)
{
int xValue = playerItems[itemNeedToCraft.Key] / itemNeedToCraft.Value;
if (numberCraftItem > xValue)
{
numberCraftItem = xValue;
}
}
buttonIndex++;
if (numberCraftItem > 0)
{
var temp = new ItemDisplayButton(Manager)
{
rectangle = new Rectangle(
width / 2 - (height / 16 + (int)(width / 2.5)) / 2 + height / 20 + width / 5,
height / 2 - (int)(height / 1.5) / 2 + height / 80 +
(height / 20 + height / 80) * (buttonIndex),
(int)(width / 5), (int)(height / 20)),
text1 = item.Key,
scale1 = 0.4f,
count = numberCraftItem,
itemTextureName = itemInfo.textureName,
fontColor1 = Color.White,
mainColor = Color.Gray,
fontName1 = "Fonts\\Font4",
discriptions1 = itemInfo.description,
resourcesNeededToCraft1 = itemInfo.resourcesNeededToCraft
};
Elements.Add(temp);
temp.Initialize();
temp.LoadTexture(AppManager.Instance.Content);
ItemDisplayButtonsList.Add(temp);
temp.LeftButtonPressed += () =>
{
AppManager.Instance.playerData.CraftItem(item.Key);
AppManager.Instance.SetGUI(new BaseGUI());
};
buttonIndex++;
}
}
}
catch
{
}
}
@ -123,5 +169,19 @@ public class BaseGUI : AbstractGUI
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
foreach (var item in ItemDisplayButtonsList)
{
if (item.Update(gameTime))
{
AppManager.Instance.SetGUI(new BaseGUI());
}
}
foreach (var item in ItemDisplayLabelsList)
{
if (item.Update(gameTime))
{
AppManager.Instance.SetGUI(new BaseGUI());
}
}
}
}

View file

@ -56,6 +56,7 @@ namespace ZoFo.GameCore.GameManagers
Content.RootDirectory = "Content";
IsMouseVisible = true;
server = new Server();
playerData = new PlayerData();
ItemManager = new ItemManager.ItemManager();
Instance = this;

View file

@ -14,16 +14,18 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
public string textureName;
Texture2D itemTexture;
public bool isCraftable;
public string description;
public Dictionary<string, int> resourcesNeededToCraft;
public ItemInfo (string tag)
{
this.tag = tag;
}
public ItemInfo(string tag,string textureName,bool isCraftable, Dictionary<string, int> resourcesNeededToCraft)
public ItemInfo(string tag, string description, string textureName, bool isCraftable, Dictionary<string, int> resourcesNeededToCraft)
{
this.tag = tag;
this.textureName = textureName;
this.description = description;
this.isCraftable = isCraftable;
this.resourcesNeededToCraft = resourcesNeededToCraft;
}

View file

@ -25,13 +25,13 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
public void Initialize()
{
tagItemPairs = new Dictionary<string, ItemInfo>();
tagItemPairs.Add("wood", new ItemInfo("wood","Textures\\Test\\wood",false,null));
tagItemPairs.Add("rock", new ItemInfo("rock", "Textures\\Test\\rock", false, null));
tagItemPairs.Add("steel", new ItemInfo("steel", "Textures\\Test\\steel", false, null));
tagItemPairs.Add("pickaxe", new ItemInfo("steel", "Textures\\Test\\pickaxe", true, new Dictionary<string, int>()
tagItemPairs.Add("wood", new ItemInfo("wood", "бревна кусок", "Textures\\Test\\wood",false,null));
tagItemPairs.Add("rock", new ItemInfo("rock", "пять галек", "Textures\\Test\\rock", false, null));
tagItemPairs.Add("steel", new ItemInfo("steel", "метал, метал, \nжелезо, метал", "Textures\\Test\\steel", false, null));
tagItemPairs.Add("pickaxe", new ItemInfo("steel", "прямой путь к \nстановлению каменьщиком", "Textures\\Test\\pickaxe", true, new Dictionary<string, int>()
{
{"wood", 2},
{"Steel", 3}
{"steel", 3}
}));
}

View file

@ -35,14 +35,24 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
{
items[item.Key] -= item.Value;
}
if (items.Keys.Contains(itemTag))
{
items[itemTag] += 1;
}
else
{
items.Add(itemTag, 1);
}
return true;
}
public void LoadPlayerData()
{
//TODO
items = new Dictionary<string, int>();
items.Add("wood", 2);
items.Add("wood", 5);
items.Add("steel", 110);
items.Add("rock", 6);
}