[UI] Basics of creating and adding ITabs to the game?

Started by MoreCowbell, March 21, 2017, 07:46:14 PM

Previous topic - Next topic

MoreCowbell

I'm looking to make a spell mod and while I can seem to figure out most of the effect stuff, I cannot for the life of me add a "Spells" tab to the inspect pane of all pawns who have a hediff. Like I just can't get my tab to show up no matter what I do, I set up a DrawTabs override, a ITab_Pawn_Spells class and SpellsCardUtility class. Basically following from outfitter and psychology's code as an attempt to learn, but like I said I can't get it to show up.

Anyone kind enough to either walk me through the process or post an example code of say, creating a blank tab named like "Test" or something that pops a blank window up? Or one with options if that isnt hard. That shows up either in order (is that possible? my outfitter shows on the top right and is the only tab on that row) Or just at a fixed position.

PS: I am using HugsLib

Thanks a ton for any help, I've looked around and decompiled and all I just can't seem to get this part so any help would be amazing.


Q2:I'm pretty lost, but I sorta know the code for searching through the hediffs and using IsVisable() in itab. But is there something wrong with just doing that for visability?

Q3: Is there an easy way to attach another effect to a corpse other than consume such as making it a drug (like would that work?)? And would it be consumed as a drug then.

Edit: Just realized I should attach my code, like I said I'm not sure why its not working and all.

AssemblyInfo.cs: I copied the guid, is that a problem?
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle("VampireMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VampireMod")]
[assembly: AssemblyCopyright("${AuthorCopyright}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("91fe8442-bf33-48f7-9112-78844a2a366d")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]



VampireModeBase.cs
using HugsLib;
using HugsLib.Settings;
using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
using Verse;
using Verse.AI.Group;
using Verse.Grammar;

namespace VampireMod
{
public class VampireModBase : ModBase
{




public override string ModIdentifier
{
get
{
return "VampireMod";
}
}




public override void DefsLoaded()
{
if (base.ModIsActive)
{
foreach (ThingDef current in DefDatabase<ThingDef>.AllDefsListForReading)
{
if (current.thingClass == typeof(Pawn))
{
current.inspectorTabs.Add(typeof(ITab_Pawn_Spells));
try
{
current.inspectorTabsResolved.Add(InspectTabManager.GetSharedInstance(typeof(ITab_Pawn_Spells)));
}
catch (Exception ex)
{
Log.Error(string.Concat(new object[]
{
"Could not instantiate inspector tab of type ",
typeof(ITab_Pawn_Spells),
": ",
ex
}));
}

}
}
}
}


}
}



ITab_Pawn_Spells.cs
using RimWorld;
using System;
using UnityEngine;
using Verse;
using HugsLib.GuiInject;

namespace VampireMod
{
public class ITab_Pawn_Spells : ITab
{
//public static readonly ITab_Pawn_Spells.<>c <>9 = new ITab_Pawn_Spells.<>c();
public override bool IsVisible
{
get
{

foreach (Hediff current in this.PawnToShowInfoAbout.health.hediffSet.hediffs)
{
bool flag1 = current.Label.ToString().ToLower().Equals("secret of mana");
if (flag1)
{
return true;
}
}
return false;
//return true;
}


}

private Pawn PawnToShowInfoAbout
{
get
{
Pawn pawn = null;
if (base.SelPawn != null)
{
pawn = base.SelPawn;
}
else
{
Corpse corpse = base.SelThing as Corpse;
if (corpse != null)
{
pawn = corpse.InnerPawn;
}
}
if (pawn == null)
{
Log.Error("Character tab found no selected pawn to display.");
return null;
}
base.SelThing.def.inspectorTabs.Add(typeof(ITab_Pawn_Spells));
base.SelThing.def.inspectorTabsResolved.Add(InspectTabManager.GetSharedInstance(typeof(ITab_Pawn_Spells)));
return pawn;
}
}

public ITab_Pawn_Spells()
{
this.size = new Vector2(500f, 550f);
this.labelKey = "TabSpells";
this.tutorTag = "Spells";
}

protected override void FillTab()
{
Pawn pawnForSpells = this.PawnToShowInfoAbout;
if (pawnForSpells == null)
{
Log.Error("Spell tab found no selected pawn to display.");
return;
}
Rect outRect = new Rect(0f, 20f, this.size.x, this.size.y - 20f);
SpellCardUtility.DrawPawnSpellCard(outRect, this.PawnToShowInfoAbout);

}
}
}


SpellCardUtility.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;
using RimWorld;

namespace VampireMod
{
public class SpellCardUtility
{

public static void DrawPawnSpellCard(Rect outRect, Pawn pawn)
{
outRect = outRect.Rounded();
Rect rect = new Rect(outRect.x, outRect.y, outRect.width, outRect.height).Rounded();
rect.yMin += 11f;
DrawSpellList(rect, pawn);
}




public static void DrawSpellList(Rect inRect, Pawn pawn)
{
var buttonSize = new Vector2(inRect.x/1.5f, inRect.y/7f);
var newRect = new Rect(0, inRect.height - buttonSize.y, buttonSize.x, buttonSize.y).CenteredOnXIn(inRect);
if (Widgets.ButtonText(newRect, "Hello",true,false,true))
{
// do stuff
}

}
}
}

Wishmaster

Hello.

Detouring DrawTabs is not a good idea.

You can add tabs without detouring anything.

Your second sample of code is very close to the right solution.

pawnDef.inspectorTabs.Add(newTabType); // missing         
pawnDef.inspectorTabsResolved.Add(InspectTabManager.GetSharedInstance(newTabType));

MoreCowbell

Just got it crazy enough, I made a simple no detour needed test tab mod. And it seems to work. Issue was that I was checking for typeof(Pawn) when psychology changed it to PsychologyPawn. I'm somewhat new to C# is there a way to check if it extends Pawn or is pawn? Otherwise I sorta figured out that one.


Fluffy (l2032)

if type checking was the problem, I think you could also simply use the 'is' operator?

if (pawn is Pawn) // should work for pawn: Pawn as well as pawn: PsychologyPawn