Skip to content

Commit

Permalink
0.2.0.3 release. Resize and better uigrid and uidragablepanel
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidPack committed Jun 24, 2017
1 parent 96ae5a3 commit 52641e0
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 85 deletions.
39 changes: 15 additions & 24 deletions RecipeBrowserUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ public RecipeBrowserUI(UserInterface ui) : base(ui)

public override void OnInitialize()
{
mainPanel = new UIDragablePanel(true);
mainPanel = new UIDragablePanel(true, true, true);
mainPanel.SetPadding(6);
mainPanel.Left.Set(400f, 0f);
mainPanel.Top.Set(400f, 0f);
mainPanel.Width.Set(415f, 0f);
mainPanel.MinWidth.Set(415f, 0f);
mainPanel.MaxWidth.Set(784f, 0f);
mainPanel.Height.Set(350, 0f);
mainPanel.MinHeight.Set(243, 0f);
mainPanel.MaxHeight.Set(1000, 0f);
mainPanel.BackgroundColor = new Color(73, 94, 171);
Append(mainPanel);

Expand All @@ -66,11 +70,7 @@ public override void OnInitialize()
mainPanel.Append(queryItem);

var modFilterButton = new UIHoverImageButton(RecipeBrowser.instance.GetTexture("Images/filterMod"), "Mod Filter: All");
//modFilterButton.Top.Set(37, 0f);
//modFilterButton.Left.Set(10, 0f);
//modFilterButton.Left.Set(379, 0f);
//modFilterButton.Top.Set(30, 0f);
modFilterButton.Left.Set(194, 0f);
modFilterButton.Left.Set(-208, 1f);
modFilterButton.Top.Set(-4, 0f);
modFilterButton.OnClick += ModFilterButton_OnClick;
modFilterButton.OnRightClick += ModFilterButton_OnRightClick;
Expand Down Expand Up @@ -109,15 +109,12 @@ public override void OnInitialize()
//mainPanel.Append(inventoryFilter);

itemNameFilter = new NewUITextBox("Filter by Name");
//itemNameFilter.TextColor = Color.Black;
//itemNameFilter.SetPadding(0);
itemNameFilter.OnTextChanged += () => { ValidateItemFilter(); updateNeeded = true; };
itemNameFilter.OnTabPressed += () => { itemDescriptionFilter.Focus(); };
itemNameFilter.Top.Pixels = 0f;
itemNameFilter.Left.Pixels = 225f;
//itemNameFilter.Left.Set(text2.GetInnerDimensions().Width, 0f);
itemNameFilter.Left.Set(-178, 1f);
itemNameFilter.Width.Set(150, 0f);
itemNameFilter.Height.Set(25, 0f);
//searchFilter.VAlign = 0.5f;
mainPanel.Append(itemNameFilter);

Texture2D texture = RecipeBrowser.instance.GetTexture("UIElements/closeButton");
Expand All @@ -128,39 +125,30 @@ public override void OnInitialize()
mainPanel.Append(closeButton);

itemDescriptionFilter = new NewUITextBox("Filter by tooltip");
//itemNameFilter.SetPadding(0);
itemDescriptionFilter.OnTextChanged += () => { ValidateItemDescription(); updateNeeded = true; };
itemDescriptionFilter.OnTabPressed += () => { itemNameFilter.Focus(); };
itemDescriptionFilter.Top.Pixels = 30f;
itemDescriptionFilter.Left.Pixels = 225f;
//itemNameFilter.Left.Set(text2.GetInnerDimensions().Width, 0f);
itemDescriptionFilter.Left.Set(-178, 1f);
itemDescriptionFilter.Width.Set(150, 0f);
itemDescriptionFilter.Height.Set(25, 0f);
//searchFilter.VAlign = 0.5f;
mainPanel.Append(itemDescriptionFilter);

inlaidPanel = new UIPanel();
inlaidPanel.SetPadding(6);
inlaidPanel.Top.Pixels = 60;
//inlaidPanel.Width.Set(-25f, 1f);
inlaidPanel.Width.Set(0, 1f);
//inlaidPanel.Height.Set(155, 0f);
// Use to be 155, now is 100% minus top minus what is below.
inlaidPanel.Height.Set(-60 - 121, 1f);
inlaidPanel.BackgroundColor = Color.DarkBlue;
mainPanel.Append(inlaidPanel);

recipeGrid = new UIGrid(9);
//recipeGrid.Top.Pixels = 60;
recipeGrid.Width.Set(-25f, 1f);
//recipeGrid.Height.Set(130, 0f);
recipeGrid = new UIGrid();
recipeGrid.Width.Set(-20f, 1f);
recipeGrid.Height.Set(0, 1f);
recipeGrid.ListPadding = 2f;
inlaidPanel.Append(recipeGrid);

var lootItemsScrollbar = new FixedUIScrollbar(userInterface);
lootItemsScrollbar.SetView(100f, 1000f);
//lootItemsScrollbar.Top.Pixels = 60;
//lootItemsScrollbar.Height.Set(130, 0f);
lootItemsScrollbar.Height.Set(0, 1f);
lootItemsScrollbar.Left.Set(-20, 1f);
inlaidPanel.Append(lootItemsScrollbar);
Expand All @@ -172,6 +160,9 @@ public override void OnInitialize()
recipeInfo.Height.Set(120, 0f);
mainPanel.Append(recipeInfo);

mainPanel.AddDragTarget(recipeInfo);
mainPanel.AddDragTarget(RadioButtonGroup);

updateNeeded = true;
modIndex = mods.Length - 1;
}
Expand Down
1 change: 1 addition & 0 deletions UIElements/NewUITextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ protected override void DrawSelf(SpriteBatch spriteBatch)
}
if (JustPressed(Keys.Enter))
{
Main.drawingPlayerChat = false;
if (unfocusOnEnter) Unfocus();
OnEnterPressed?.Invoke();
}
Expand Down
92 changes: 55 additions & 37 deletions UIElements/UIDragablePanel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.Graphics;
Expand All @@ -11,67 +12,81 @@ class UIDragablePanel : UIPanel
{
private static Texture2D dragTexture;
private Vector2 offset;
private bool dragable;
private bool dragging;
private bool resizeableX;
private bool resizeableY;
private bool resizeable => resizeableX || resizeableY;
private bool resizeing;
private bool resizeable;
//private int minX, minY, maxX, maxY;
private List<UIElement> additionalDragTargets;

public UIDragablePanel(bool resizeable = false)
// TODO, move panel back in if offscreen? prevent drag off screen?
public UIDragablePanel(bool dragable = true, bool resizeableX = false, bool resizeableY = false)
{
OnMouseDown += DragStart;
OnMouseUp += DragEnd;
this.resizeable = resizeable;
this.dragable = dragable;
this.resizeableX = resizeableX;
this.resizeableY = resizeableY;
if (dragTexture == null)
{
dragTexture = TextureManager.Load("Images/UI/PanelBorder");
}
additionalDragTargets = new List<UIElement>();
}

//public override void MouseDown(UIMouseEvent evt)
public void AddDragTarget(UIElement element)
{
additionalDragTargets.Add(element);
}

//public void SetMinMaxWidth(int min, int max)
//{
// offset = new Vector2(evt.MousePosition.X - Left.Pixels, evt.MousePosition.Y - Top.Pixels);
// dragging = true;
// this.minX = min;
// this.maxX = max;
//}

//public override void MouseUp(UIMouseEvent evt)
//public void SetMinMaxHeight(int min, int max)
//{
// Vector2 end = evt.MousePosition;
// dragging = false;
// this.minY = min;
// this.maxY = max;
//}

// Left.Set(end.X - offset.X, 0f);
// Top.Set(end.Y - offset.Y, 0f);
public override void MouseDown(UIMouseEvent evt)
{
DragStart(evt);
base.MouseDown(evt);
}

// Recalculate();
//}
public override void MouseUp(UIMouseEvent evt)
{
DragEnd(evt);
base.MouseUp(evt);
}

private void DragStart(UIMouseEvent evt, UIElement listeningElement)
private void DragStart(UIMouseEvent evt)
{
CalculatedStyle innerDimensions = GetInnerDimensions();
if (evt.Target == this || evt.Target == RecipeBrowserUI.instance.recipeInfo || evt.Target == RecipeBrowserUI.instance.RadioButtonGroup)
if (evt.Target == this || additionalDragTargets.Contains(evt.Target))
{
if (new Rectangle((int)(innerDimensions.X + innerDimensions.Width - 12), (int)(innerDimensions.Y + innerDimensions.Height - 12), 12 + 6, 12 + 6).Contains(evt.MousePosition.ToPoint()))
if (resizeable && new Rectangle((int)(innerDimensions.X + innerDimensions.Width - 12), (int)(innerDimensions.Y + innerDimensions.Height - 12), 12 + 6, 12 + 6).Contains(evt.MousePosition.ToPoint()))
{
offset = new Vector2(evt.MousePosition.X - innerDimensions.X - innerDimensions.Width - 6, evt.MousePosition.Y - innerDimensions.Y - innerDimensions.Height - 6);
resizeing = true;
}
else
else if (dragable)
{
offset = new Vector2(evt.MousePosition.X - Left.Pixels, evt.MousePosition.Y - Top.Pixels);
dragging = true;
}
}
}

private void DragEnd(UIMouseEvent evt, UIElement listeningElement)
private void DragEnd(UIMouseEvent evt)
{
if (evt.Target == this || evt.Target == RecipeBrowserUI.instance.recipeInfo || evt.Target == RecipeBrowserUI.instance.RadioButtonGroup)
if (evt.Target == this || additionalDragTargets.Contains(evt.Target))
{
//Vector2 end = evt.MousePosition;
dragging = false;
resizeing = false;

//Left.Set(end.X - offset.X, 0f);
//Top.Set(end.Y - offset.Y, 0f);
//Recalculate();
}
}

Expand All @@ -91,8 +106,16 @@ protected override void DrawSelf(SpriteBatch spriteBatch)
}
if (resizeing)
{
Height.Pixels = Utils.Clamp(Main.MouseScreen.Y - dimensions.Y - offset.Y, 243, 1000);
//Width.Pixels = Utils.Clamp(Main.MouseScreen.X - dimensions.X - offset.X, 415, 1000);
if (resizeableX)
{
//Width.Pixels = Utils.Clamp(Main.MouseScreen.X - dimensions.X - offset.X, minX, maxX);
Width.Pixels = Main.MouseScreen.X - dimensions.X - offset.X;
}
if (resizeableY)
{
//Height.Pixels = Utils.Clamp(Main.MouseScreen.Y - dimensions.Y - offset.Y, minY, maxY);
Height.Pixels = Main.MouseScreen.Y - dimensions.Y - offset.Y;
}
Recalculate();
}
base.DrawSelf(spriteBatch);
Expand All @@ -105,19 +128,14 @@ protected override void DrawSelf(SpriteBatch spriteBatch)
private void DrawDragAnchor(SpriteBatch spriteBatch, Texture2D texture, Color color)
{
CalculatedStyle dimensions = GetDimensions();
//CalculatedStyle innerDimensions = GetInnerDimensions();

//Rectangle hitbox = new Rectangle((int)(innerDimensions.X + innerDimensions.Width - 12), (int)(innerDimensions.Y + innerDimensions.Height - 12), 12 + 6, 12 + 6);
//Main.spriteBatch.Draw(Main.magicPixel, hitbox, Color.LightBlue * 0.6f);

Point point = new Point((int)dimensions.X, (int)dimensions.Y);
Point point2 = new Point(point.X + (int)dimensions.Width - 12, point.Y + (int)dimensions.Height - 12);
int width = point2.X - point.X - 12;
int height = point2.Y - point.Y - 12;
//spriteBatch.Draw(texture, new Rectangle(point2.X, point2.Y, 12, 12), new Rectangle?(new Rectangle(12 + 4, 12 + 4, 12, 12)), color);
spriteBatch.Draw(texture, new Rectangle(point2.X - 2, point2.Y - 2, 12 - 2, 12 - 2), new Rectangle?(new Rectangle(12 + 4, 12 + 4, 12, 12)), color);
spriteBatch.Draw(texture, new Rectangle(point2.X - 4, point2.Y - 4, 12 - 4, 12 - 4), new Rectangle?(new Rectangle(12 + 4, 12 + 4, 12, 12)), color);
spriteBatch.Draw(texture, new Rectangle(point2.X - 6, point2.Y - 6, 12 - 6, 12 - 6), new Rectangle?(new Rectangle(12 + 4, 12 + 4, 12, 12)), color);
Point point = new Point((int)(dimensions.X + dimensions.Width - 12), (int)(dimensions.Y + dimensions.Height - 12));
spriteBatch.Draw(texture, new Rectangle(point.X - 2, point.Y - 2, 12 - 2, 12 - 2), new Rectangle(12 + 4, 12 + 4, 12, 12), color);
spriteBatch.Draw(texture, new Rectangle(point.X - 4, point.Y - 4, 12 - 4, 12 - 4), new Rectangle(12 + 4, 12 + 4, 12, 12), color);
spriteBatch.Draw(texture, new Rectangle(point.X - 6, point.Y - 6, 12 - 6, 12 - 6), new Rectangle(12 + 4, 12 + 4, 12, 12), color);
}
}
}
45 changes: 25 additions & 20 deletions UIElements/UIGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public int Count
}
}

int cols = 1;

public UIGrid(int columns = 1)
// todo, vertical/horizontal orientation, left to right, etc?
public UIGrid()
{
cols = columns;
this._innerList.OverflowHidden = false;
this._innerList.Width.Set(0f, 1f);
this._innerList.Height.Set(0f, 1f);
Expand All @@ -75,7 +73,7 @@ public void Goto(UIGrid.ElementSearchMethod searchMethod, bool center = false)
this._scrollbar.ViewPosition = this._items[i].Top.Pixels;
if (center)
{
this._scrollbar.ViewPosition = this._items[i].Top.Pixels - GetInnerDimensions().Height/2 + _items[i].GetOuterDimensions().Height/2;
this._scrollbar.ViewPosition = this._items[i].Top.Pixels - GetInnerDimensions().Height / 2 + _items[i].GetOuterDimensions().Height / 2;
}
return;
}
Expand All @@ -90,6 +88,15 @@ public virtual void Add(UIElement item)
this._innerList.Recalculate();
}

public virtual void AddRange(IEnumerable<UIElement> items)
{
this._items.AddRange(items);
foreach (var item in items)
this._innerList.Append(item);
this.UpdateOrder();
this._innerList.Recalculate();
}

public virtual bool Remove(UIElement item)
{
this._innerList.RemoveChild(item);
Expand Down Expand Up @@ -120,30 +127,28 @@ public override void ScrollWheel(UIScrollWheelEvent evt)

public override void RecalculateChildren()
{
float availableWidth = GetInnerDimensions().Width;
base.RecalculateChildren();
float top = 0f;
float left = 0f;
float maxRowHeight = 0f;
for (int i = 0; i < this._items.Count; i++)
{
this._items[i].Top.Set(top, 0f);
this._items[i].Left.Set(left, 0f);
this._items[i].Recalculate();
if (i % cols == cols - 1)
var item = this._items[i];
var outerDimensions = item.GetOuterDimensions();
if (left + outerDimensions.Width > availableWidth && left > 0)
{
top += this._items[i].GetOuterDimensions().Height + this.ListPadding;
top += maxRowHeight + this.ListPadding;
left = 0;
maxRowHeight = 0;
}
else
{
left += this._items[i].GetOuterDimensions().Width + this.ListPadding;
}
//num += this._items[i].GetOuterDimensions().Height + this.ListPadding;
}
if (_items.Count > 0)
{
//top += ListPadding + _items[0].GetOuterDimensions().Height;
maxRowHeight = Math.Max(maxRowHeight, outerDimensions.Height);
item.Left.Set(left, 0f);
left += outerDimensions.Width + this.ListPadding;
item.Top.Set(top, 0f);
item.Recalculate();
}
this._innerListHeight = top;
this._innerListHeight = top + maxRowHeight;
}

private void UpdateScrollbar()
Expand Down
5 changes: 3 additions & 2 deletions UIElements/UIItemSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class UIItemSlot : UIElement
public int itemType;
public Item item;

public UIItemSlot(Item item)
public UIItemSlot(Item item, float scale = .75f)
{
this.scale = scale;
this.item = item;
this.itemType = item.type;
this.Width.Set(defaultBackgroundTexture.Width * scale, 0f);
Expand Down Expand Up @@ -55,7 +56,7 @@ protected override void DrawSelf(SpriteBatch spriteBatch)
int height = rectangle2.Height;
int width = rectangle2.Width;
float drawScale = 1f;
float availableWidth = 32; // (float)defaultBackgroundTexture.Width * scale;
float availableWidth = (float)defaultBackgroundTexture.Width * scale;
if (width > availableWidth || height > availableWidth)
{
if (width > height)
Expand Down
2 changes: 1 addition & 1 deletion UIElements/UIRecipeSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UIRecipeSlot : UIItemSlot
public int index;
public bool recentlyDiscovered;

public UIRecipeSlot(int index) : base(Main.recipe[index].createItem)
public UIRecipeSlot(int index, float scale = 0.75f) : base(Main.recipe[index].createItem, scale)
{
this.index = index;
}
Expand Down
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = jopojelly
version = 0.2.0.2
version = 0.2.0.3
displayName = Recipe Browser
homepage = http://forums.terraria.org/index.php?threads/cheat-sheet.41407/
buildIgnore = .vs\*, Properties\*, *.csproj, *.user, obj\*, bin\*, *.config, unused\*
Expand Down

0 comments on commit 52641e0

Please sign in to comment.