Skip to content

Commit

Permalink
add some null checks for possible weird modded plants
Browse files Browse the repository at this point in the history
  • Loading branch information
FluffierThanThou committed May 22, 2020
1 parent 887ac80 commit f1b1b40
Showing 1 changed file with 120 additions and 122 deletions.
242 changes: 120 additions & 122 deletions Source/ManagerTabs/ManagerTab_Foraging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Karel Kroeze
// ManagerTab_Foraging.cs
// 2016-12-09
// ManagerTab_Foraging.cs
// Copyright Karel Kroeze, 2020-2020

using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -106,30 +105,82 @@ public void DoContent( Rect rect )
}
}

public float DrawThreshold( Vector2 pos, float width )
public void DoLeftRow( Rect rect )
{
var currentCount = _selected.Trigger.CurrentCount;
var designatedCount = _selected.CurrentDesignatedCount;
var targetCount = _selected.Trigger.TargetCount;
var start = pos;
Widgets.DrawMenuSection( rect );

_selected.Trigger.DrawTriggerConfig( ref pos, width, ListEntryHeight,
"FMG.TargetCount".Translate(
currentCount, designatedCount, targetCount ),
"FMG.TargetCountTooltip".Translate(
currentCount, designatedCount, targetCount ),
_selected.Designations, () => _selected.Sync = Utilities.SyncDirection.FilterToAllowed,
_selected.DesignationLabel );
// content
var height = _leftRowHeight;
var scrollView = new Rect( 0f, 0f, rect.width, height );
if ( height > rect.height )
scrollView.width -= ScrollbarWidth;

Utilities.DrawToggle( ref pos, width,
"FM.Foraging.SyncFilterAndAllowed".Translate(),
"FM.Foraging.SyncFilterAndAllowed.Tip".Translate(),
ref _selected.SyncFilterAndAllowed );
Utilities.DrawReachabilityToggle( ref pos, width, ref _selected.CheckReachable );
Utilities.DrawToggle( ref pos, width, "FM.PathBasedDistance".Translate(),
"FM.PathBasedDistance.Tip".Translate(), ref _selected.PathBasedDistance,
true );
Widgets.BeginScrollView( rect, ref _scrollPosition, scrollView );
var scrollContent = scrollView;

GUI.BeginGroup( scrollContent );
var cur = Vector2.zero;
var i = 0;

foreach ( var job in _jobs )
{
var row = new Rect( 0f, cur.y, scrollContent.width, LargeListEntryHeight );
Widgets.DrawHighlightIfMouseover( row );
if ( _selected == job ) Widgets.DrawHighlightSelected( row );

if ( i++ % 2 == 1 ) Widgets.DrawAltRect( row );

var jobRect = row;

if ( ManagerTab_Overview.DrawOrderButtons( new Rect( row.xMax - 50f, row.yMin, 50f, 50f ), manager,
job ) ) Refresh();
jobRect.width -= 50f;

job.DrawListEntry( jobRect, false );
if ( Widgets.ButtonInvisible( jobRect ) ) _selected = job;

cur.y += LargeListEntryHeight;
}

// row for new job.
var newRect = new Rect( 0f, cur.y, scrollContent.width, LargeListEntryHeight );
Widgets.DrawHighlightIfMouseover( newRect );

if ( i % 2 == 1 ) Widgets.DrawAltRect( newRect );

Text.Anchor = TextAnchor.MiddleCenter;
Widgets.Label( newRect, "<" + "FMG.NewForagingJob".Translate() + ">" );
Text.Anchor = TextAnchor.UpperLeft;

if ( Widgets.ButtonInvisible( newRect ) ) Selected = new ManagerJob_Foraging( manager );

TooltipHandler.TipRegion( newRect, "FMG.NewForagingJobTooltip".Translate() );

cur.y += LargeListEntryHeight;

_leftRowHeight = cur.y;
GUI.EndGroup();
Widgets.EndScrollView();
}

public override void DoWindowContents( Rect canvas )
{
// set up rects
var leftRow = new Rect( 0f, 0f, DefaultLeftRowSize, canvas.height );
var contentCanvas = new Rect( leftRow.xMax + Margin, 0f, canvas.width - leftRow.width - Margin,
canvas.height );

// draw overview row
DoLeftRow( leftRow );

// draw job interface if something is selected.
if ( Selected != null ) DoContent( contentCanvas );
}

public float DrawAreaRestriction( Vector2 pos, float width )
{
var start = pos;
AreaAllowedGUI.DoAllowedAreaSelectors( ref pos, width, ref _selected.ForagingArea, manager );
return pos.y - start.y;
}

Expand All @@ -143,19 +194,37 @@ public float DrawMaturePlants( Vector2 pos, float width )
return ListEntryHeight;
}

public float DrawAreaRestriction( Vector2 pos, float width )
public float DrawPlantList( Vector2 pos, float width )
{
var start = pos;
AreaAllowedGUI.DoAllowedAreaSelectors( ref pos, width, ref _selected.ForagingArea, manager );
return pos.y - start.y;

// list of keys in allowed trees list (all plans that yield wood in biome, static)
var allowedPlants = _selected.AllowedPlants;
var plants = allowedPlants.Keys.ToList();

var rowRect = new Rect(
pos.x,
pos.y,
width,
ListEntryHeight );

// toggle for each plant
foreach ( var plant in plants )
{
Utilities.DrawToggle( rowRect, plant.LabelCap, plant.description, _selected.AllowedPlants[plant],
() => _selected.SetPlantAllowed( plant, !_selected.AllowedPlants[plant] ) );
rowRect.y += ListEntryHeight;
}

return rowRect.yMin - start.y;
}

public float DrawPlantShortcuts( Vector2 pos, float width )
{
var start = pos;

// list of keys in allowed trees list (all plans that yield wood in biome, static)
Dictionary<ThingDef,bool> allowedPlants = _selected.AllowedPlants;
var allowedPlants = _selected.AllowedPlants;
var plants = allowedPlants.Keys.ToList();

var rowRect = new Rect(
Expand All @@ -176,7 +245,7 @@ public float DrawPlantShortcuts( Vector2 pos, float width )

// toggle edible
rowRect.y += ListEntryHeight;
var edible = plants.Where( p => p.plant.harvestedThingDef.IsNutritionGivingIngestible ).ToList();
var edible = plants.Where( p => p.plant?.harvestedThingDef?.IsNutritionGivingIngestible ?? false ).ToList();
Utilities.DrawToggle(
rowRect,
"<i>" + "FM.Foraging.Edible".Translate() + "</i>",
Expand All @@ -188,7 +257,7 @@ public float DrawPlantShortcuts( Vector2 pos, float width )

// toggle shrooms
rowRect.y += ListEntryHeight;
var shrooms = plants.Where( p => p.plant.cavePlant ).ToList();
var shrooms = plants.Where( p => p.plant?.cavePlant ?? false ).ToList();
Utilities.DrawToggle(
rowRect,
"<i>" + "FM.Foraging.Mushrooms".Translate() + "</i>",
Expand All @@ -201,101 +270,32 @@ public float DrawPlantShortcuts( Vector2 pos, float width )
return rowRect.yMax - start.y;
}

public float DrawPlantList( Vector2 pos, float width )
{
var start = pos;

// list of keys in allowed trees list (all plans that yield wood in biome, static)
Dictionary<ThingDef, bool> allowedPlants = _selected.AllowedPlants;
var plants = allowedPlants.Keys.ToList();

var rowRect = new Rect(
pos.x,
pos.y,
width,
ListEntryHeight );

// toggle for each plant
foreach ( var plant in plants )
{
Utilities.DrawToggle( rowRect, plant.LabelCap, plant.description, _selected.AllowedPlants[plant],
() => _selected.SetPlantAllowed( plant, !_selected.AllowedPlants[plant] ) );
rowRect.y += ListEntryHeight;
}

return rowRect.yMin - start.y;
}

public void DoLeftRow( Rect rect )
public float DrawThreshold( Vector2 pos, float width )
{
Widgets.DrawMenuSection( rect );

// content
var height = _leftRowHeight;
var scrollView = new Rect( 0f, 0f, rect.width, height );
if ( height > rect.height )
scrollView.width -= ScrollbarWidth;

Widgets.BeginScrollView( rect, ref _scrollPosition, scrollView );
var scrollContent = scrollView;

GUI.BeginGroup( scrollContent );
var cur = Vector2.zero;
var i = 0;

foreach ( var job in _jobs )
{
var row = new Rect( 0f, cur.y, scrollContent.width, LargeListEntryHeight );
Widgets.DrawHighlightIfMouseover( row );
if ( _selected == job ) Widgets.DrawHighlightSelected( row );

if ( i++ % 2 == 1 ) Widgets.DrawAltRect( row );

var jobRect = row;

if ( ManagerTab_Overview.DrawOrderButtons( new Rect( row.xMax - 50f, row.yMin, 50f, 50f ), manager,
job ) ) Refresh();
jobRect.width -= 50f;

job.DrawListEntry( jobRect, false );
if ( Widgets.ButtonInvisible( jobRect ) ) _selected = job;

cur.y += LargeListEntryHeight;
}

// row for new job.
var newRect = new Rect( 0f, cur.y, scrollContent.width, LargeListEntryHeight );
Widgets.DrawHighlightIfMouseover( newRect );

if ( i % 2 == 1 ) Widgets.DrawAltRect( newRect );

Text.Anchor = TextAnchor.MiddleCenter;
Widgets.Label( newRect, "<" + "FMG.NewForagingJob".Translate() + ">" );
Text.Anchor = TextAnchor.UpperLeft;

if ( Widgets.ButtonInvisible( newRect ) ) Selected = new ManagerJob_Foraging( manager );

TooltipHandler.TipRegion( newRect, "FMG.NewForagingJobTooltip".Translate() );

cur.y += LargeListEntryHeight;

_leftRowHeight = cur.y;
GUI.EndGroup();
Widgets.EndScrollView();
}
var currentCount = _selected.Trigger.CurrentCount;
var designatedCount = _selected.CurrentDesignatedCount;
var targetCount = _selected.Trigger.TargetCount;
var start = pos;

public override void DoWindowContents( Rect canvas )
{
// set up rects
var leftRow = new Rect( 0f, 0f, DefaultLeftRowSize, canvas.height );
var contentCanvas = new Rect( leftRow.xMax + Margin, 0f, canvas.width - leftRow.width - Margin,
canvas.height );
_selected.Trigger.DrawTriggerConfig( ref pos, width, ListEntryHeight,
"FMG.TargetCount".Translate(
currentCount, designatedCount, targetCount ),
"FMG.TargetCountTooltip".Translate(
currentCount, designatedCount, targetCount ),
_selected.Designations,
() => _selected.Sync = Utilities.SyncDirection.FilterToAllowed,
_selected.DesignationLabel );

// draw overview row
DoLeftRow( leftRow );
Utilities.DrawToggle( ref pos, width,
"FM.Foraging.SyncFilterAndAllowed".Translate(),
"FM.Foraging.SyncFilterAndAllowed.Tip".Translate(),
ref _selected.SyncFilterAndAllowed );
Utilities.DrawReachabilityToggle( ref pos, width, ref _selected.CheckReachable );
Utilities.DrawToggle( ref pos, width, "FM.PathBasedDistance".Translate(),
"FM.PathBasedDistance.Tip".Translate(), ref _selected.PathBasedDistance,
true );

// draw job interface if something is selected.
if ( Selected != null ) DoContent( contentCanvas );
return pos.y - start.y;
}

public override void PreOpen()
Expand All @@ -314,7 +314,5 @@ public void Refresh()
// update selected ( also update thingfilter _only_ if the job is not managed yet )
_selected?.RefreshAllowedPlants();
}


}
}

0 comments on commit f1b1b40

Please sign in to comment.