Skip to content

Commit

Permalink
v0.4, filters, scroll fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidPack committed Feb 10, 2019
1 parent 0b73c46 commit 725893d
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 11 deletions.
8 changes: 7 additions & 1 deletion AutoTrash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ public override void Load()
autoTrashListUI.Activate();
autoTrashUserInterface = new UserInterface();
autoTrashUserInterface.SetState(autoTrashListUI);

UICheckbox.checkboxTexture = GetTexture("checkBox");
UICheckbox.checkmarkTexture = GetTexture("checkMark");
}
}

public override void Unload()
{
instance = null;
autoTrashUserInterface = null;

UICheckbox.checkboxTexture = null;
UICheckbox.checkmarkTexture = null;
}

public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
Expand Down Expand Up @@ -111,7 +117,7 @@ public override void PostSetupContent()
if (RecipeBrowser != null)
{
RecipeBrowser.Call("AddItemFilter", "Not Auto Trashed", "Weapons", GetTexture("RecipeBrowserFilterNotAutotrashedIcon"),
(Predicate<Item>)((Item item) => !Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>().AutoTrashItems.Any(x => x.type == item.type)));
(Predicate<Item>)((Item item) => !Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>().ShouldItemBeTrashed(item)));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions AutoTrash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="AutoTrashPlayer.cs" />
<Compile Include="AutoTrash.cs" />
<Compile Include="ItemSlot.cs" />
<Compile Include="UICheckbox.cs" />
<Compile Include="UIGrid.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion AutoTrashGlobalItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override bool OnPickup(Item item, Player player)
{
return true;
}
if (autoTrashPlayer.AutoTrashEnabled && autoTrashPlayer.AutoTrashItems.Where(x => x.type == item.type).Any())
if (autoTrashPlayer.AutoTrashEnabled && autoTrashPlayer.ShouldItemBeTrashed(item))
{
autoTrashPlayer.LastAutoTrashItem = item;
//Main.item[j] = player.GetItem(player.whoAmI, Main.item[j], false, false);
Expand Down
25 changes: 21 additions & 4 deletions AutoTrashListUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ class AutoTrashListUI : UIState
public UIGrid autoTrashGrid;
public FixedUIScrollbar autoTrashGridScrollbar;
float spacing = 8f;
UICheckbox NoValueCheckbox;

public override void OnInitialize()
{
int checkboxesHeight = 30;

mainPanel = new UIPanel();
mainPanel.SetPadding(0);
mainPanel.Left.Set(300f, 0f);
mainPanel.Top.Set(300f, 0f);
mainPanel.Width.Set(200f, 0f);
mainPanel.Height.Set(300f, 0f);
mainPanel.Height.Set(300f + checkboxesHeight, 0f);
mainPanel.BackgroundColor = new Color(73, 94, 171);
mainPanel.OnMouseDown += DragStart;
mainPanel.OnMouseUp += DragEnd;
Expand All @@ -48,18 +51,25 @@ public override void OnInitialize()
autoTrashGrid.Top.Pixels = 32f + spacing;
autoTrashGrid.Left.Pixels = spacing;
autoTrashGrid.Width.Set(-25f, 1f);
autoTrashGrid.Height.Set(-55f, 1f);
autoTrashGrid.Height.Set(-55f - checkboxesHeight, 1f);
autoTrashGrid.ListPadding = 12f;
autoTrashGrid.OnScrollWheel += OnScrollWheel_FixHotbarScroll;
mainPanel.Append(autoTrashGrid);

autoTrashGridScrollbar = new FixedUIScrollbar();
autoTrashGridScrollbar.SetView(100f, 1000f);
autoTrashGridScrollbar.Top.Pixels = 32f + spacing;
autoTrashGridScrollbar.Height.Set(-50f - spacing, 1f);
autoTrashGridScrollbar.Height.Set(-50f - spacing - checkboxesHeight, 1f);
autoTrashGridScrollbar.HAlign = 1f;
mainPanel.Append(autoTrashGridScrollbar);
autoTrashGrid.SetScrollbar(autoTrashGridScrollbar);

NoValueCheckbox = new UICheckbox("No Value", "Trash all Items with No Value");
NoValueCheckbox.Top.Set(300, 0f);
NoValueCheckbox.Left.Set(12, 0f);
NoValueCheckbox.OnSelectedChanged += (a, b) => Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>().NoValue = NoValueCheckbox.Selected;
mainPanel.Append(NoValueCheckbox);

Append(mainPanel);
}

Expand Down Expand Up @@ -110,6 +120,11 @@ protected override void DrawSelf(SpriteBatch spriteBatch)
}
}

internal static void OnScrollWheel_FixHotbarScroll(UIScrollWheelEvent evt, UIElement listeningElement)
{
Main.LocalPlayer.ScrollHotbar(Terraria.GameInput.PlayerInput.ScrollWheelDelta / 120);
}

internal void UpdateNeeded()
{
updateneeded = true;
Expand All @@ -122,7 +137,7 @@ internal void UpdateCheckboxes()
updateneeded = false;
autoTrashGrid.Clear();

var autoTrashPlayer = Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>(AutoTrash.instance);
var autoTrashPlayer = Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>();

foreach (var item in autoTrashPlayer.AutoTrashItems)
{
Expand All @@ -136,6 +151,8 @@ internal void UpdateCheckboxes()
}
autoTrashGrid.UpdateOrder();
autoTrashGrid._innerList.Recalculate();

NoValueCheckbox.Selected = Main.LocalPlayer.GetModPlayer<AutoTrashPlayer>().NoValue;
}
}

Expand Down
19 changes: 15 additions & 4 deletions AutoTrashPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ internal class AutoTrashPlayer : ModPlayer
public List<Item> AutoTrashItems;
public Item LastAutoTrashItem;

public bool NoValue;
internal bool NoValueBelongs(Item item) => item.value == 0;

public override void Initialize()
{
AutoTrashItems = new List<Item>();
LastAutoTrashItem = new Item();
LastAutoTrashItem.SetDefaults(0, true);
AutoTrashEnabled = false;
NoValue = false;
}

public override TagCompound Save()
{
return new TagCompound
{
["AutoTrashItems"] = AutoTrashItems.Select(ItemIO.Save).ToList(),
["AutoTrashEnabled"] = AutoTrashEnabled
["AutoTrashItems"] = AutoTrashItems,
["AutoTrashEnabled"] = AutoTrashEnabled,
[nameof(NoValue)] = NoValue,
};
}

public override void Load(TagCompound tag)
{
AutoTrashItems = tag.GetList<TagCompound>("AutoTrashItems").Select(ItemIO.Load).ToList();
AutoTrashItems = tag.Get<List<Item>>("AutoTrashItems");
AutoTrashEnabled = tag.GetBool("AutoTrashEnabled");
NoValue = tag.GetBool(nameof(NoValue));

AutoTrash.instance.autoTrashListUI?.UpdateNeeded();
}
Expand All @@ -45,6 +51,11 @@ internal static bool IsModItem(Item item)
return item.type >= ItemID.Count;
}

internal bool ShouldItemBeTrashed(Item item)
{
return AutoTrashItems.Where(x => x.type == item.type).Any() || (NoValue && NoValueBelongs(item));
}

public override bool ShiftClickSlot(Item[] inventory, int context, int slot)
{
if (context == Terraria.UI.ItemSlot.Context.InventoryItem || context == Terraria.UI.ItemSlot.Context.InventoryCoin || context == Terraria.UI.ItemSlot.Context.InventoryAmmo)
Expand Down Expand Up @@ -77,7 +88,7 @@ public override void PreUpdate()
// Fishing uses player.GetItem bypassing AutoTrash.
if (Main.myPlayer == player.whoAmI)
{
if(caughtFish > 0 && AutoTrashItems.Where(x => x.type == caughtFish).Any())
if(caughtFish > 0 && AutoTrashItems.Where(x => x.type == caughtFish).Any()) // type vs Item filter?
{
for (int i = 0; i < 59; i++)
{
Expand Down
85 changes: 85 additions & 0 deletions UICheckbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;

namespace AutoTrash
{
internal class UICheckbox : UIText
{
public static Texture2D checkboxTexture;
public static Texture2D checkmarkTexture;

public event EventHandler OnSelectedChanged;

private bool selected = false;
private bool disabled = false;
internal string hoverText;

public bool Selected
{
get { return selected; }
set
{
if (value != selected)
{
selected = value;
OnSelectedChanged?.Invoke(this, EventArgs.Empty);
}
}
}

public UICheckbox(string text, string hoverText, float textScale = 1, bool large = false) : base(text, textScale, large)
{
this.Left.Pixels += 20;
//TextColor = Color.Blue;
text = " " + text;
this.hoverText = hoverText;
SetText(text);
OnClick += UICheckbox_onLeftClick;
Recalculate();
}

private void UICheckbox_onLeftClick(UIMouseEvent evt, UIElement listeningElement)
{
if (disabled) return;
this.Selected = !Selected;
}

public void SetDisabled(bool disabled = true)
{
this.disabled = disabled;
if (disabled)
{
Selected = false;
}
TextColor = disabled ? Color.Gray : Color.White;
}
public void SetHoverText(string hoverText)
{
this.hoverText = hoverText;
}

protected override void DrawSelf(SpriteBatch spriteBatch)
{
base.DrawSelf(spriteBatch);

CalculatedStyle innerDimensions = base.GetInnerDimensions();
Vector2 pos = new Vector2(innerDimensions.X, innerDimensions.Y - 5);

//Rectangle hitbox = GetInnerDimensions().ToRectangle();
//Main.spriteBatch.Draw(Main.magicPixel, hitbox, Color.Red * 0.6f);

spriteBatch.Draw(checkboxTexture, pos, null, disabled ? Color.Gray : Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
if (Selected)
spriteBatch.Draw(checkmarkTexture, pos, null, disabled ? Color.Gray : Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);

if (IsMouseHovering)
{
Main.hoverItemName = hoverText;
}
}
}
}
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.3.1
version = 0.4
displayName = Auto Trash
homepage = https://forums.terraria.org/index.php?threads/auto-trash.53592/
buildIgnore = .vs\*, Properties\*, *.csproj, *.user, obj\*, bin\*, *.config, .git\*
Expand Down
Binary file added checkBox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkMark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 725893d

Please sign in to comment.