diff options
| author | hazel <hazel@hazelthats.me> | 2026-01-26 22:04:39 +0100 |
|---|---|---|
| committer | hazel <hazel@hazelthats.me> | 2026-01-26 22:04:39 +0100 |
| commit | 567c422f8cd42eba2437f9a8c2522716a1649be7 (patch) | |
| tree | 93c5b296f3b7c14b626d0aadf5cad37764c41c74 /source/ui/guis | |
| download | celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.gz celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.bz2 celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.zip | |
celesteia archive, last updated april 9th 2024
Signed-off-by: hazel <hazel@hazelthats.me>
Diffstat (limited to 'source/ui/guis')
| -rw-r--r-- | source/ui/guis/DebugGUI.cs | 66 | ||||
| -rw-r--r-- | source/ui/guis/GUI.cs | 48 | ||||
| -rw-r--r-- | source/ui/guis/MainMenu.cs | 225 | ||||
| -rw-r--r-- | source/ui/guis/game/GameGUI.cs | 349 |
4 files changed, 688 insertions, 0 deletions
diff --git a/source/ui/guis/DebugGUI.cs b/source/ui/guis/DebugGUI.cs new file mode 100644 index 0000000..7ee44ae --- /dev/null +++ b/source/ui/guis/DebugGUI.cs @@ -0,0 +1,66 @@ +using System.Diagnostics; +using Celesteia.Resources; +using Celesteia.UI; +using Celesteia.UI.Elements; +using Celesteia.UI.Properties; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using MonoGame.Extended; + +namespace Celesteia.GUIs { + public class DebugGUI : GUI { + private new GameInstance Game => (GameInstance) base.Game; + public DebugGUI(GameInstance game) : base(game, Rect.ScreenFull) {} + + private Label updateLabel; + private Label drawLabel; + + public override void LoadContent(ContentManager Content) { + float fontSize = 12f; + + Label template = new Label(new Rect( + AbsoluteUnit.WithValue(10), + AbsoluteUnit.WithValue(10), + AbsoluteUnit.WithValue(200), + AbsoluteUnit.WithValue(50) + )) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.GetFontType("Hobo")) + .SetFontSize(fontSize) + .SetTextAlignment(TextAlignment.Top | TextAlignment.Left) + ); + float textSpacing = 4f; + float textRow(int number) => 10f + number * (fontSize + textSpacing); + + Root.AddChild(template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(0)))).SetText($"Celesteia {GameInstance.Version}")); + Root.AddChild(updateLabel = template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(1)))).SetText("")); + Root.AddChild(drawLabel = template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(2)))).SetText("")); + + Debug.WriteLine("Loaded Debug GUI."); + } + + private double updateTime; + private double lastUpdate; + public override void Update(GameTime gameTime, out bool clickedAnything) { + clickedAnything = false; + + updateTime = gameTime.TotalGameTime.TotalMilliseconds - lastUpdate; + lastUpdate = gameTime.TotalGameTime.TotalMilliseconds; + + updateLabel.SetText($"Update: {updateTime.ToString("0.00")}ms"); + } + + private double drawTime; + private double lastDraw; + public override void Draw(GameTime gameTime) + { + drawTime = gameTime.TotalGameTime.TotalMilliseconds - lastDraw; + lastDraw = gameTime.TotalGameTime.TotalMilliseconds; + + drawLabel.SetText($"Draw: {drawTime.ToString("0.00")}ms"); + + if (GameInstance.DebugMode) base.Draw(gameTime); + } + } +}
\ No newline at end of file diff --git a/source/ui/guis/GUI.cs b/source/ui/guis/GUI.cs new file mode 100644 index 0000000..6d00070 --- /dev/null +++ b/source/ui/guis/GUI.cs @@ -0,0 +1,48 @@ +using System.Diagnostics; +using Celesteia.UI; +using Celesteia.UI.Elements; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace Celesteia.GUIs { + public class GUI { + public GameInstance Game; + + public IContainer Root; + + public GUI(GameInstance Game, Rect rect) { + this.Game = Game; + this.Root = new Container(rect); + } + + public virtual void LoadContent(ContentManager Content) { + Debug.WriteLine("Loaded GUI."); + } + + public virtual void Update(GameTime gameTime, out bool clickedAnything) { + if (!Game.IsActive) { + clickedAnything = false; + return; + } + + Root.Update(gameTime, out clickedAnything); + } + + // Draw all elements. + public virtual void Draw(GameTime gameTime) { + + Game.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointWrap, null, null, null); + + if (UIReferences.GUIEnabled) Root.Draw(Game.SpriteBatch); + + Game.SpriteBatch.End(); + } + + // If the menu is referred to as a boolean, return whether it is non-null (true) or null (false). + public static implicit operator bool(GUI gui) + { + return !object.ReferenceEquals(gui, null); + } + } +}
\ No newline at end of file diff --git a/source/ui/guis/MainMenu.cs b/source/ui/guis/MainMenu.cs new file mode 100644 index 0000000..dd03d5a --- /dev/null +++ b/source/ui/guis/MainMenu.cs @@ -0,0 +1,225 @@ +using System.Diagnostics; +using Celesteia.Game.ECS; +using Celesteia.Resources; +using Celesteia.Screens; +using Celesteia.UI; +using Celesteia.UI.Elements; +using Celesteia.UI.Properties; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace Celesteia.GUIs { + public class MainMenu : GUI { + public MainMenu(GameInstance game) : base(game, Rect.ScreenFull) {} + + private Texture2D logo; + private Texture2D buttonTexture; + + private IContainer MainScreen; + private IContainer OptionsScreen; + private IContainer NewWorldScreen; + + private IContainer LogoPivot; + private Label Progress; + + private Button buttonTemplate; + private float buttonRow(int number) => number * (buttonHeight + buttonSpacing); + private float buttonHeight = 56f; + private float buttonSpacing = 10f; + + public override void LoadContent(ContentManager Content) { + logo = Game.Content.Load<Texture2D>("celesteia/logo"); + buttonTexture = Game.Content.Load<Texture2D>("sprites/ui/button"); + + buttonTemplate = new Button(new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(buttonRow(0)), + AbsoluteUnit.WithValue(250f), + AbsoluteUnit.WithValue(56f) + )) + .SetPivotPoint(new Vector2(.5f)) + .SetTexture(buttonTexture) + .MakePatches(4) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.GetFontType("Hobo")) + .SetFontSize(24f) + .SetTextAlignment(TextAlignment.Center)) + .SetColorGroup(new ButtonColorGroup(Color.White, Color.Black, Color.Violet, Color.DarkViolet)); + + // Load all the screens. + LoadMainScreen(); + LoadOptionsScreen(); + LoadNewWorldScreen(); + LoadGlobals(); + + base.LoadContent(Content); + } + + private void LoadMainScreen() { + Root.AddChild(MainScreen = new Container(Rect.ScreenFull)); + MainScreen.SetEnabled(true); + + IContainer menu = new Container(new Rect( + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal), + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical), + AbsoluteUnit.WithValue(250f), + AbsoluteUnit.WithValue(0f) + )); + + menu.AddChild(LogoPivot = new Container(new Rect( + AbsoluteUnit.WithValue(0f), + new ScreenSpaceUnit(-.25f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical), + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(0f) + ))); + + float logoRatio = logo.Height / (float) logo.Width; + LogoPivot.AddChild( + new Image(new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(0f), + new ScreenSpaceUnit(.4f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal), + new ScreenSpaceUnit(.4f * logoRatio, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal) + )) + .SetTexture(logo) + .SetColor(Color.White) + .SetPivotPoint(new Vector2(.5f)) + ); + + menu.AddChild( + buttonTemplate.Clone() + .SetNewRect(buttonTemplate.GetRect() + .SetY(AbsoluteUnit.WithValue(buttonRow(0))) + .SetWidth(new RelativeUnit(1f, menu.GetRect(), RelativeUnit.Orientation.Horizontal)) + ) + .SetOnMouseUp(async (button, position) => { + ShowNewWorldScreen(); + Debug.WriteLine("Generating world..."); + GameWorld _gameWorld = await Game.Worlds.LoadNewWorld((progressReport) => { + Progress.SetText(progressReport); + Debug.WriteLine(" " + progressReport); + }); + Game.LoadScreen( + new GameplayScreen(Game, _gameWorld), + new MonoGame.Extended.Screens.Transitions.FadeTransition(Game.GraphicsDevice, Color.Black) + ); + }) + .SetText("Start Game") + ); + + menu.AddChild( + buttonTemplate.Clone() + .SetNewRect(buttonTemplate.GetRect() + .SetY(AbsoluteUnit.WithValue(buttonRow(1))) + .SetWidth(new RelativeUnit(1f, menu.GetRect(), RelativeUnit.Orientation.Horizontal)) + ) + .SetOnMouseUp((button, position) => { ShowOptionsScreen(); }) + .SetText("Options") + ); + + menu.AddChild( + buttonTemplate.Clone() + .SetNewRect(buttonTemplate.GetRect() + .SetY(AbsoluteUnit.WithValue(buttonRow(2))) + .SetWidth(new RelativeUnit(1f, menu.GetRect(), RelativeUnit.Orientation.Horizontal)) + ) + .SetOnMouseUp((button, position) => { Game.Exit(); }) + .SetText("Quit Game") + .SetColorGroup(new ButtonColorGroup(Color.White, Color.Black, Color.Red, Color.DarkRed)) + ); + + MainScreen.AddChild(menu); + } + private void ShowMainScreen() { + MainScreen.SetEnabled(true); + OptionsScreen.SetEnabled(false); + NewWorldScreen.SetEnabled(false); + } + + private void LoadOptionsScreen() { + Root.AddChild(OptionsScreen = new Container(Rect.ScreenFull)); + OptionsScreen.SetEnabled(false); + + IContainer menu = new Container(new Rect( + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal), + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical), + AbsoluteUnit.WithValue(450f), + AbsoluteUnit.WithValue(0f) + )); + + menu.AddChild( + buttonTemplate.Clone() + .SetNewRect(buttonTemplate.GetRect() + .SetY(AbsoluteUnit.WithValue(buttonRow(0))) + .SetWidth(new RelativeUnit(1f, menu.GetRect(), RelativeUnit.Orientation.Horizontal)) + ) + .SetOnMouseUp((button, position) => { ShowMainScreen(); }) + .SetText("Back to Main Menu") + ); + + OptionsScreen.AddChild(menu); + } + private void ShowOptionsScreen() { + MainScreen.SetEnabled(false); + OptionsScreen.SetEnabled(true); + NewWorldScreen.SetEnabled(false); + } + + private void LoadNewWorldScreen() { + Root.AddChild(NewWorldScreen = new Container(Rect.ScreenFull)); + NewWorldScreen.SetEnabled(false); + + NewWorldScreen.AddChild(Progress = new Label( + new Rect( + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal), + new ScreenSpaceUnit(.5f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical), + AbsoluteUnit.WithValue(200), + AbsoluteUnit.WithValue(50) + )) + .SetPivotPoint(new Vector2(0.5f, 0.5f)) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.DEFAULT) + .SetFontSize(24f) + .SetTextAlignment(TextAlignment.Center) + ) + .SetText("") + ); + } + private void ShowNewWorldScreen() { + MainScreen.SetEnabled(false); + OptionsScreen.SetEnabled(false); + NewWorldScreen.SetEnabled(true); + } + + private void LoadGlobals() { + Container copyPivot = new Container(new Rect( + new RelativeUnit(.5f, Root.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, Root.GetRect(), RelativeUnit.Orientation.Vertical), + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(12f) + )); + copyPivot.SetPivot(new Vector2(.5f, 1f)); + Root.AddChild(copyPivot); + + copyPivot.AddChild(new Label( + new Rect( + AbsoluteUnit.WithValue(12f), + AbsoluteUnit.WithValue(0f), + new RelativeUnit(1f, Root.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, copyPivot.GetRect(), RelativeUnit.Orientation.Vertical) + ) + ) + .SetTextProperties(new TextProperties() + .SetFont(ResourceManager.Fonts.DEFAULT) + .SetColor(Color.White) + .SetFontSize(12f) + .SetTextAlignment(TextAlignment.Bottom | TextAlignment.Center) + ) + .SetText("(c) leafal.io 2022-2023, all rights reserved.") + .SetPivotPoint(new Vector2(0.5f, 1f))); + } + } +}
\ No newline at end of file diff --git a/source/ui/guis/game/GameGUI.cs b/source/ui/guis/game/GameGUI.cs new file mode 100644 index 0000000..b8e6f4a --- /dev/null +++ b/source/ui/guis/game/GameGUI.cs @@ -0,0 +1,349 @@ +using System.Collections.Generic; +using System.Diagnostics; +using Celesteia.Game.Components.Items; +using Celesteia.Game.Input; +using Celesteia.Resources; +using Celesteia.UI; +using Celesteia.UI.Elements; +using Celesteia.UI.Elements.Game; +using Celesteia.UI.Elements.Game.Controls; +using Celesteia.UI.Elements.Game.Tooltips; +using Celesteia.UI.Properties; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.TextureAtlases; + +namespace Celesteia.GUIs.Game { + public class GameGUI : GUI + { + private new GameInstance Game => (GameInstance) base.Game; + public GameGUI(GameInstance Game) : base(Game, Rect.ScreenFull) { } + + + public ItemStack CursorItem; + + private IContainer _pauseMenu; + + public ControlTips Controls { get; private set; } + + private IContainer ItemManagement; + + private InventorySlot _slotTemplate; + private CraftingRecipeSlot _recipeTemplate; + private IContainer Hotbar; + + + private Texture2D slotTexture; + private TextureAtlas slotPatches; + private Texture2D windowTexture; + private Texture2D tooltipTexture; + + private Inventory _inventory; + private List<InventorySlot> _slots; + + private int _hotbarSelection = 0; + public int HotbarSelection { + get => _hotbarSelection; + set { + _hotbarSelection = MathHelper.Clamp(value, 0, HotbarSlots - 1); + UpdateSelected(); + } + } + public readonly int HotbarSlots = 9; + + private IContainer _mousePivot; + private IContainer _inventoryScreen; + private IContainer _craftingScreen; + + private InventoryScreenState _state; + public InventoryScreenState State { + get => _state; + set { + _state = value; + _inventoryScreen.SetEnabled((int)_state > 0); + _craftingScreen.SetEnabled((int)_state > 1); + + if ((int)_state < 1 && CursorItem != null) { + ItemStack item = CursorItem; + CursorItem = null; + + _inventory.AddItem(item); + } + } + } + + private ItemTooltipDisplay _itemDisplay; + private bool _itemDisplayEnabled = false; + private CraftingTooltipDisplay _craftingDisplay; + private bool _craftingDisplayEnabled = false; + + public void SetReferenceInventory(Inventory inventory) { + _inventory = inventory; + + _slotTemplate.SetReferenceInventory(_inventory); + + LoadHotbar(); + LoadInventoryScreen(); + LoadCraftingScreen(); + + State = _state; + } + + public override void LoadContent(ContentManager Content) + { + Root.AddChild(Controls = new ControlTips(new Rect( + AbsoluteUnit.WithValue(8f), + new RelativeUnit(0.5f, Root.GetRect(), RelativeUnit.Orientation.Vertical), + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(0f) + ))); + + slotTexture = Content.Load<Texture2D>("sprites/ui/button"); + windowTexture = Content.Load<Texture2D>("sprites/ui/button"); + tooltipTexture = Content.Load<Texture2D>("sprites/ui/window"); + slotPatches = TextureAtlas.Create("patches", slotTexture, 4, 4); + + LoadTooltipDisplays(Content); + LoadPauseMenu(Content); + + _slotTemplate = new InventorySlot(new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(-InventorySlot.SLOT_SPACING), + AbsoluteUnit.WithValue(InventorySlot.SLOT_SIZE), + AbsoluteUnit.WithValue(InventorySlot.SLOT_SIZE) + )) + .SetTexture(slotTexture) + .SetPatches(slotPatches, 4) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.GetFontType("Hobo")) + .SetFontSize(16f) + .SetTextAlignment(TextAlignment.Bottom | TextAlignment.Center) + ) + .SetOnMouseIn((item) => { + if ((int)State < 1) return; + _itemDisplay.SetItem(item); + _itemDisplayEnabled = true; + }) + .SetOnMouseOut(() => _itemDisplayEnabled = false); + + _recipeTemplate = new CraftingRecipeSlot(new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(-CraftingRecipeSlot.SLOT_SPACING), + AbsoluteUnit.WithValue(CraftingRecipeSlot.SLOT_SIZE), + AbsoluteUnit.WithValue(CraftingRecipeSlot.SLOT_SIZE) + )) + .SetTexture(slotTexture) + .SetPatches(slotPatches, 4) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.GetFontType("Hobo")) + .SetFontSize(16f) + .SetTextAlignment(TextAlignment.Bottom | TextAlignment.Right) + ) + .SetOnMouseIn((recipe) => { + if ((int)State < 2) return; + _craftingDisplay.SetRecipe(recipe); + _craftingDisplayEnabled = true; + }) + .SetOnMouseOut(() => _craftingDisplayEnabled = false); + + _slots = new List<InventorySlot>(); + + ItemManagement = new Container(Rect.ScreenFull); + Root.AddChild(ItemManagement); + + LoadHotbar(); + + Debug.WriteLine("Loaded Game GUI."); + } + + public bool Paused { get; private set; } + public void TogglePause() { + Paused = !Paused; + UpdatePauseMenu(); + } + + private void LoadPauseMenu(ContentManager Content) { + _pauseMenu = new PauseMenu(this, Rect.ScreenFull, + new Button(new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(250f), + AbsoluteUnit.WithValue(56f) + )) + .SetPivotPoint(new Vector2(.5f)) + .SetTexture(Content.Load<Texture2D>("sprites/ui/button")) + .MakePatches(4) + .SetTextProperties(new TextProperties() + .SetColor(Color.White) + .SetFont(ResourceManager.Fonts.GetFontType("Hobo")) + .SetFontSize(24f) + .SetTextAlignment(TextAlignment.Center)) + .SetColorGroup(new ButtonColorGroup(Color.White, Color.Black, Color.Violet, Color.DarkViolet)) + ); + + Root.AddChild(_pauseMenu); + + UpdatePauseMenu(); + } + + private void LoadTooltipDisplays(ContentManager Content) { + _mousePivot = new Container(new Rect( + AbsoluteUnit.WithValue(0f) + )); + + _itemDisplay = new ItemTooltipDisplay(new Rect( + AbsoluteUnit.WithValue(16f), + AbsoluteUnit.WithValue(16f), + AbsoluteUnit.WithValue(256f), + AbsoluteUnit.WithValue(64f) + ), tooltipTexture); + _itemDisplay.SetPivot(new Vector2(0f, 1f)); + + _mousePivot.AddChild(_itemDisplay); + + _craftingDisplay = new CraftingTooltipDisplay(new Rect( + AbsoluteUnit.WithValue(16f), + AbsoluteUnit.WithValue(16f), + AbsoluteUnit.WithValue(256f), + AbsoluteUnit.WithValue(64f) + ), tooltipTexture); + _craftingDisplay.SetPivot(new Vector2(0f, 0f)); + + _mousePivot.AddChild(_craftingDisplay); + } + + private void LoadHotbar() { + if (Hotbar != null) { + Hotbar.Dispose(); + _slots.Clear(); + } + + Hotbar = new Container(new Rect( + new RelativeUnit(0.5f, ItemManagement.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, ItemManagement.GetRect(), RelativeUnit.Orientation.Vertical), + AbsoluteUnit.WithValue((HotbarSlots * InventorySlot.SLOT_SIZE) + ((HotbarSlots - 1) * InventorySlot.SLOT_SPACING)), + AbsoluteUnit.WithValue(InventorySlot.SLOT_SIZE) + )); + Hotbar.SetPivot(new Vector2(0.5f, 1f)); + + for (int i = 0; i < HotbarSlots; i++) { + int slotNumber = i; + InventorySlot slot = _slotTemplate.Clone() + .SetNewRect(_slotTemplate.GetRect().SetX(AbsoluteUnit.WithValue(i * InventorySlot.SLOT_SIZE + (i * InventorySlot.SLOT_SPACING)))) + .SetSlot(slotNumber) + .SetOnMouseUp((button, point) => { + if ((int)State < 1) { + HotbarSelection = slotNumber; + UpdateSelected(); + } else { + ItemStack itemInSlot = _inventory.GetSlot(slotNumber); + + _inventory.SetSlot(slotNumber, CursorItem); + CursorItem = itemInSlot; + } + }); + slot.SetPivot(new Vector2(0f, 0f)); + slot.SetEnabled(true); + + _slots.Add(slot); + Hotbar.AddChild(slot); + } + + UpdateSelected(); + + ItemManagement.AddChild(Hotbar); + } + + private void LoadInventoryScreen() { + int remainingSlots = _inventory.Capacity - HotbarSlots; + int rows = (remainingSlots / HotbarSlots); + + Container pivot = new Container(new Rect( + new RelativeUnit(0.5f, ItemManagement.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, ItemManagement.GetRect(), RelativeUnit.Orientation.Vertical), + new AbsoluteUnit((HotbarSlots * InventorySlot.SLOT_SIZE) + ((HotbarSlots + 1) * InventorySlot.SLOT_SPACING)), + new AbsoluteUnit((rows * InventorySlot.SLOT_SIZE) + ((rows + 1) * InventorySlot.SLOT_SPACING)) + )); + + _inventoryScreen = new InventoryWindow(this, new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(-(InventorySlot.SLOT_SIZE + (2 * InventorySlot.SLOT_SPACING))), + new RelativeUnit(1f, pivot.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, pivot.GetRect(), RelativeUnit.Orientation.Vertical) + ), windowTexture, _inventory, remainingSlots, HotbarSlots, _slotTemplate); + _inventoryScreen.SetPivot(new Vector2(0.5f, 1f)); + + pivot.AddChild(_inventoryScreen); + ItemManagement.AddChild(pivot); + } + + private void LoadCraftingScreen() { + int remainingSlots = _inventory.Capacity - HotbarSlots; + int rows = (remainingSlots / HotbarSlots); + + Container pivot = new Container(new Rect( + new RelativeUnit(0.5f, ItemManagement.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(0f, ItemManagement.GetRect(), RelativeUnit.Orientation.Vertical), + new AbsoluteUnit((HotbarSlots * CraftingRecipeSlot.SLOT_SIZE) + ((HotbarSlots + 1) * CraftingRecipeSlot.SLOT_SPACING)), + new AbsoluteUnit((rows * CraftingRecipeSlot.SLOT_SIZE) + ((rows + 1) * CraftingRecipeSlot.SLOT_SPACING)) + )); + + _craftingScreen = new CraftingWindow(this, new Rect( + AbsoluteUnit.WithValue(0f), + AbsoluteUnit.WithValue(CraftingRecipeSlot.SLOT_SPACING), + new RelativeUnit(1f, pivot.GetRect(), RelativeUnit.Orientation.Horizontal), + new RelativeUnit(1f, pivot.GetRect(), RelativeUnit.Orientation.Vertical) + ), windowTexture, _inventory, _recipeTemplate); + _craftingScreen.SetPivot(new Vector2(0.5f, 0f)); + + pivot.AddChild(_craftingScreen); + ItemManagement.AddChild(pivot); + } + + public override void Update(GameTime gameTime, out bool clickedAnything) + { + _mousePivot.MoveTo(MouseHelper.Position); + _itemDisplay.SetEnabled(_itemDisplayEnabled && (int)_state > 0); + _craftingDisplay.SetEnabled(_craftingDisplayEnabled && (int)_state > 1); + + base.Update(gameTime, out clickedAnything); + } + + private Color _slightlyTransparent = new Color(255, 255, 255, 175); + private Vector2 scale = new Vector2(2f); + public override void Draw(GameTime gameTime) + { + base.Draw(gameTime); + + Game.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null); + + if (CursorItem != null) Game.SpriteBatch.Draw(CursorItem.Type.Sprite, MouseHelper.Position.ToVector2(), _slightlyTransparent, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f); + else { + _itemDisplay.Draw(Game.SpriteBatch); + _craftingDisplay.Draw(Game.SpriteBatch); + } + + Game.SpriteBatch.End(); + } + + public ItemStack GetSelectedItem() { + return _inventory.GetSlot(HotbarSelection); + } + + private void UpdateSelected() { + _slots.ForEach(slot => slot.SetSelected(false)); + _slots[HotbarSelection].SetSelected(true); + } + + private void UpdatePauseMenu() { + _pauseMenu.SetEnabled(Paused); + } + } + + public enum InventoryScreenState { + Closed, Inventory, Crafting + } +}
\ No newline at end of file |
