PR#2 Update SGameConsole

This commit is contained in:
Chris 2020-02-04 11:37:18 -05:00
parent cc56b39209
commit 5b146632cf
1 changed files with 89 additions and 52 deletions

View File

@ -1,9 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Framework; using StardewModdingAPI.Framework;
using StardewModdingAPI.Internal.ConsoleWriting; using StardewModdingAPI.Internal.ConsoleWriting;
using StardewValley; using StardewValley;
@ -11,68 +8,53 @@ using StardewValley.Menus;
namespace StardewModdingAPI namespace StardewModdingAPI
{ {
class SGameConsole : IClickableMenu public class SGameConsole : IClickableMenu
{ {
public static SGameConsole Instance; public static SGameConsole Instance;
public bool isVisible; public bool isVisible;
private readonly LinkedList<KeyValuePair<ConsoleLogLevel, string>> consoleMessageQueue = new LinkedList<KeyValuePair<ConsoleLogLevel, string>>(); private readonly LinkedList<KeyValuePair<ConsoleLogLevel, string>> consoleMessageQueue = new LinkedList<KeyValuePair<ConsoleLogLevel, string>>();
private readonly TextBox textBox; private MobileScrollbox scrollbox;
private readonly MobileScrollbox scrollbox;
private Rectangle textBoxBounds; private ClickableTextureComponent commandButton;
private SpriteFont smallFont; private SpriteFont smallFont;
private TextBoxEvent textBoxEvent;
private Vector2 size;
private bool scrolling = false; private bool scrolling = false;
private int scrollLastFakeY = 0;
private int scrollLastY = 0;
private int MaxScrollBoxHeight => (int)(Game1.graphics.PreferredBackBufferHeight * 20 / Game1.NativeZoomLevel);
private int MaxTextAreaWidth => (int)((Game1.graphics.PreferredBackBufferWidth - 32) / Game1.NativeZoomLevel);
internal SGameConsole() internal SGameConsole()
{ {
Instance = this; Instance = this;
this.isVisible = true; this.isVisible = true;
this.textBox = new TextBox(null, null, Game1.dialogueFont, Game1.textColor)
{
X = 0,
Y = 100,
Width = IClickableMenu.viewport.Width,
Height = IClickableMenu.viewport.Height
};
this.scrollbox = new MobileScrollbox(0, 0, 1280, 320, this.consoleMessageQueue.Count, new Rectangle(0, 0, 1280, 320));
this.textBoxBounds = new Rectangle(this.textBox.X, this.textBox.Y, this.textBox.Width, this.textBox.Height);
this.scrollbox.Bounds = this.textBoxBounds;
this.textBoxEvent = new TextBoxEvent(this.textBoxEnter);
} }
internal void InitializeContent(LocalizedContentManager content) internal void InitializeContent(LocalizedContentManager content)
{ {
this.scrollbox = new MobileScrollbox(0, 0, this.MaxTextAreaWidth, (int)(Game1.graphics.PreferredBackBufferHeight / Game1.NativeZoomLevel), this.MaxScrollBoxHeight,
new Rectangle(0, 0, (int)(Game1.graphics.PreferredBackBufferWidth / Game1.NativeZoomLevel), (int)(Game1.graphics.PreferredBackBufferHeight / Game1.NativeZoomLevel)));
this.smallFont = content.Load<SpriteFont>(@"Fonts\SmallFont"); this.smallFont = content.Load<SpriteFont>(@"Fonts\SmallFont");
this.size = this.smallFont.MeasureString("aA");
} }
public void Show() public void Show()
{ {
if (this.upperRightCloseButton == null) if (this.upperRightCloseButton == null)
this.initializeUpperRightCloseButton(); this.initializeUpperRightCloseButton();
if (this.commandButton == null)
this.commandButton = new ClickableTextureComponent(new Rectangle(16, 0, 64, 64), Game1.mobileSpriteSheet, new Rectangle(0, 44, 16, 16), 4f, false);
Game1.activeClickableMenu = this; Game1.activeClickableMenu = this;
this.isVisible = true; this.isVisible = true;
} }
public override void receiveLeftClick(int x, int y, bool playSound = true) public override void receiveLeftClick(int x, int y, bool playSound = true)
{ {
if (this.textBoxBounds.Contains(x, y))
{
this.scrollbox.receiveLeftClick(x, y);
this.scrolling = this.scrollbox.panelScrolling;
this.textBox.Selected = true;
this.textBox.OnEnterPressed += this.textBoxEvent;
this.textBox.Update();
Game1.keyboardDispatcher.Subscriber = this.textBox;
this.textBoxEnter(this.textBox);
}
if (this.upperRightCloseButton.bounds.Contains(x, y)) if (this.upperRightCloseButton.bounds.Contains(x, y))
{ {
@ -80,31 +62,61 @@ namespace StardewModdingAPI
Game1.activeClickableMenu = null; Game1.activeClickableMenu = null;
Game1.playSound("bigDeSelect"); Game1.playSound("bigDeSelect");
} }
else if (this.commandButton.bounds.Contains(x, y))
{
Game1.activeClickableMenu = new NamingMenu(this.textBoxEnter, "Command", "")
{
randomButton = new ClickableTextureComponent(new Rectangle(-100, -100, 0, 0), Game1.mobileSpriteSheet, new Rectangle(87, 22, 20, 20), 4f, false)
};
this.isVisible = false;
Game1.playSound("bigDeSelect");
}
else
{
this.scrollLastFakeY = y;
this.scrollLastY = y;
this.scrolling = true;
this.scrollbox.receiveLeftClick(x, y);
}
} }
public void textBoxEnter(TextBox text) public void textBoxEnter(string text)
{ {
this.textBox.OnEnterPressed -= this.textBoxEvent; string command = text.Trim();
string command = text.Text.Trim(); if (command.Length > 0)
{
SGame.instance.CommandQueue.Enqueue(command); if (command.EndsWith(";"))
{
command = command.TrimEnd(';');
SGame.instance.CommandQueue.Enqueue(command);
this.exitThisMenu();
return;
}
SGame.instance.CommandQueue.Enqueue(command);
}
this.isVisible = true;
Game1.activeClickableMenu = this;
} }
public override void leftClickHeld(int x, int y) public override void leftClickHeld(int x, int y)
{ {
if (this.scrolling) if (this.scrolling)
{ {
int tmp = y;
y = this.scrollLastFakeY + this.scrollLastY - y;
this.scrollLastY = tmp;
this.scrollLastFakeY = y;
this.scrollbox.leftClickHeld(x, y); this.scrollbox.leftClickHeld(x, y);
this.scrollbox.setYOffsetForScroll(9999);
} }
} }
public override void releaseLeftClick(int x, int y) public override void releaseLeftClick(int x, int y)
{ {
this.scrolling = false;
this.scrollbox.releaseLeftClick(x, y); this.scrollbox.releaseLeftClick(x, y);
} }
public void WriteLine(string consoleMessage, ConsoleLogLevel level) internal void WriteLine(string consoleMessage, ConsoleLogLevel level)
{ {
lock (this.consoleMessageQueue) lock (this.consoleMessageQueue)
{ {
@ -121,18 +133,42 @@ namespace StardewModdingAPI
this.scrollbox.update(time); this.scrollbox.update(time);
} }
private string _parseText(string text)
{
string line = string.Empty;
string returnString = string.Empty;
string[] strings = text.Split("\n");
foreach (string t in strings)
{
string[] wordArray = t.Split(' ');
foreach (string word in wordArray)
{
if (this.smallFont.MeasureString(line + word).X > this.MaxTextAreaWidth)
{
returnString = returnString + line + '\n';
line = string.Empty;
}
line = line + word + ' ';
}
returnString = returnString + line + '\n';
line = string.Empty;
}
returnString.TrimEnd('\n');
return returnString;
}
public override void draw(SpriteBatch b) public override void draw(SpriteBatch b)
{ {
float y = Game1.game1.screen.Height - this.size.Y; this.scrollbox.setUpForScrollBoxDrawing(b);
lock (this.consoleMessageQueue) lock (this.consoleMessageQueue)
{ {
float offset = 0;
foreach (var log in this.consoleMessageQueue) foreach (var log in this.consoleMessageQueue)
{ {
string text = log.Value; string text = this._parseText(log.Value);
if (text.Contains("\n")) Vector2 size = this.smallFont.MeasureString(text);
{ float y = Game1.game1.screen.Height - size.Y - offset - this.scrollbox.getYOffsetForScroll();
text = text.Replace("\n", ""); offset += size.Y;
}
switch (log.Key) switch (log.Key)
{ {
case ConsoleLogLevel.Critical: case ConsoleLogLevel.Critical:
@ -155,18 +191,19 @@ namespace StardewModdingAPI
b.DrawString(this.smallFont, text, new Vector2(16, y), Color.LightGray); b.DrawString(this.smallFont, text, new Vector2(16, y), Color.LightGray);
break; break;
} }
this.size = this.smallFont.MeasureString(text); if (offset > this.MaxScrollBoxHeight)
if (y < 0)
{ {
break; break;
} }
y -= this.size.Y;
} }
} }
this.scrollbox.finishScrollBoxDrawing(b);
if (Context.IsWorldReady) if (Context.IsWorldReady)
{
this.upperRightCloseButton.draw(b); this.upperRightCloseButton.draw(b);
this.commandButton.draw(b);
}
} }
} }
} }