Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions GenericModConfigMenu/Framework/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public void AddTextOption(IManifest mod, Func<string> getValue, Action<string> s
this.AddSimpleOption(mod, name, tooltip, getValue, setValue, fieldId);
}

/// <inheritdoc />
public void AddTextOption(IManifest mod, Func<string> getValue, Action<string> setValue, int? width, int? height, Func<string> name = null, Func<string> tooltip = null, string[] allowedValues = null, Func<string, string> formatAllowedValue = null, string fieldId = null)
{
if (allowedValues?.Any() == true)
this.AddChoiceOption(mod, name, tooltip, getValue, setValue, allowedValues, formatAllowedValue, fieldId);
else
this.AddSimpleOption(mod, name, tooltip, getValue, setValue, fieldId, width, height);
}

/// <inheritdoc />
public void AddKeybind(IManifest mod, Func<SButton> getValue, Action<SButton> setValue, Func<string> name = null, Func<string> tooltip = null, string fieldId = null)
{
Expand Down Expand Up @@ -515,7 +524,7 @@ public void SubscribeToChange(IManifest mod, Action<string, string> changeHandle
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="fieldId">The unique field ID used when raising field-changed events, or <c>null</c> to generate a random one.</param>
private void AddSimpleOption<T>(IManifest mod, Func<string> name, Func<string> tooltip, Func<T> getValue, Action<T> setValue, string fieldId)
private void AddSimpleOption<T>(IManifest mod, Func<string> name, Func<string> tooltip, Func<T> getValue, Action<T> setValue, string fieldId, int? width = null, int? height = null)
{
mod ??= this.mod;
this.AssertNotNull(name);
Expand All @@ -528,7 +537,7 @@ private void AddSimpleOption<T>(IManifest mod, Func<string> name, Func<string> t
if (!valid.Contains(typeof(T)))
throw new ArgumentException("Invalid config option type.");

modConfig.AddOption(new SimpleModOption<T>(fieldId, name, tooltip, modConfig, getValue, setValue));
modConfig.AddOption(new SimpleModOption<T>(fieldId, name, tooltip, modConfig, getValue, setValue, width, height));
}

/// <summary>Add a numeric option with optional clamping.</summary>
Expand Down
8 changes: 6 additions & 2 deletions GenericModConfigMenu/Framework/ModOption/SimpleModOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public virtual T Value
}
}


public virtual int? Width { get; set; }
public virtual int? Height { get; set; }

/*********
** Public methods
*********/
Expand All @@ -50,13 +52,15 @@ public virtual T Value
/// <param name="mod">The mod config UI that contains this option.</param>
/// <param name="getValue">Get the latest value from the mod config.</param>
/// <param name="setValue">Update the mod config with the given value.</param>
public SimpleModOption(string fieldId, Func<string> name, Func<string> tooltip, ModConfig mod, Func<T> getValue, Action<T> setValue)
public SimpleModOption(string fieldId, Func<string> name, Func<string> tooltip, ModConfig mod, Func<T> getValue, Action<T> setValue, int? width = null, int? height = null)
: base(fieldId, name, tooltip, mod)
{
this.GetValue = getValue;
this.SetValue = setValue;

this.CachedValue = this.GetValue();
this.Width = width;
this.Height = height;
}

/// <inheritdoc />
Expand Down
4 changes: 4 additions & 0 deletions GenericModConfigMenu/Framework/SpecificModConfigMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ public SpecificModConfigMenu(ModConfig config, int scrollSpeed, string page, Act
String = option.Value,
Callback = (Element e) => option.Value = (e as Textbox).String
};
if (option.Width.HasValue)
(optionElement as Textbox).SetWidth = option.Width.Value;
if (option.Height.HasValue)
(optionElement as Textbox).SetHeight = option.Height.Value;
break;

case SectionTitleModOption _:
Expand Down
13 changes: 13 additions & 0 deletions GenericModConfigMenu/IGenericModConfigMenuApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public interface IGenericModConfigMenuApi
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddTextOption(IManifest mod, Func<string> getValue, Action<string> setValue, Func<string> name, Func<string> tooltip = null, string[] allowedValues = null, Func<string, string> formatAllowedValue = null, string fieldId = null);

/// <summary>Add a string option at the current position in the form with specified width and height.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="width">The pixel width of the text box.</param>
/// <param name="height">The pixel height of the text box.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="allowedValues">The values that can be selected, or <c>null</c> to allow any.</param>
/// <param name="formatAllowedValue">Get the display text to show for a value from <paramref name="allowedValues"/>, or <c>null</c> to show the values as-is.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddTextOption(IManifest mod, Func<string> getValue, Action<string> setValue, int? width, int? height, Func<string> name = null, Func<string> tooltip = null, string[] allowedValues = null, Func<string, string> formatAllowedValue = null, string fieldId = null);

/// <summary>Add a key binding at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
Expand Down
32 changes: 28 additions & 4 deletions SpaceShared/UI/Textbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public bool Selected

public Action<Element> Callback { get; set; }

public int SetWidth { get; set; } = 192;
public int SetHeight { get; set; } = 48;
/// <inheritdoc />
public override int Width => 192;
public override int Width => SetWidth;

/// <inheritdoc />
public override int Height => 48;
public override int Height => SetHeight;


/*********
Expand Down Expand Up @@ -82,12 +84,34 @@ public override void Draw(SpriteBatch b)
if (this.IsHidden())
return;

b.Draw(this.Tex, this.Position, Color.White);
if (this.Width == Tex.Width && this.Height == Tex.Height)
{
b.Draw(this.Tex, this.Position, Color.White);
}
else
{
var textureWidthMinus16 = this.Tex.Width - 16;
var textureHeightMinus16 = this.Tex.Height - 16;
var textureWidthMinus32 = this.Tex.Width - 32;
var textureHeightMinus32 = this.Tex.Height - 32;
// Draw the corners
b.Draw(this.Tex, new Rectangle((int)this.Position.X, (int)this.Position.Y, 16, 16), new Rectangle(0, 0, 16, 16), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X + this.Width - 16, (int)this.Position.Y, 16, 16), new Rectangle(textureWidthMinus16, 0, 16, 16), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X, (int)this.Position.Y + this.Height - 16, 16, 16), new Rectangle(0, textureHeightMinus16, 16, 16), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X + this.Width - 16, (int)this.Position.Y + this.Height - 16, 16, 16), new Rectangle(textureWidthMinus16, textureHeightMinus16, 16, 16), Color.White);
// Draw the edges
b.Draw(this.Tex, new Rectangle((int)this.Position.X + 16, (int)this.Position.Y, this.Width - 32, 16), new Rectangle(16, 0, textureWidthMinus32, 16), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X + 16, (int)this.Position.Y + this.Height - 16, this.Width - 32, 16), new Rectangle(16, textureHeightMinus16, textureWidthMinus32, 16), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X, (int)this.Position.Y + 16, 16, this.Height - 32), new Rectangle(0, 16, 16, textureHeightMinus32), Color.White);
b.Draw(this.Tex, new Rectangle((int)this.Position.X + this.Width - 16, (int)this.Position.Y + 16, 16, this.Height - 32), new Rectangle(textureWidthMinus16, 16, 16, textureHeightMinus32), Color.White);
// Draw the center
b.Draw(this.Tex, new Rectangle((int)this.Position.X + 16, (int)this.Position.Y + 16, this.Width - 32, this.Height - 32), new Rectangle(16, 16, textureWidthMinus32, textureHeightMinus32), Color.White);
}

// Copied from game code - caret
string text = this.String;
Vector2 vector2;
for (vector2 = this.Font.MeasureString(text); vector2.X > 192f; vector2 = this.Font.MeasureString(text))
for (vector2 = this.Font.MeasureString(text); vector2.X > this.Width; vector2 = this.Font.MeasureString(text))
text = text.Substring(1);
if (DateTime.UtcNow.Millisecond % 1000 >= 500 && this.Selected)
b.Draw(Game1.staminaRect, new Rectangle((int)this.Position.X + 16 + (int)vector2.X + 2, (int)this.Position.Y + 8, 4, 32), Game1.textColor);
Expand Down