-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
MainTabWindow_Animals.cs
162 lines (141 loc) · 5 KB
/
MainTabWindow_Animals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// MainTabWindow_Animals.cs
// Copyright Karel Kroeze, 2017-2017
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using UnityEngine;
using Verse;
using static AnimalTab.Constants;
namespace AnimalTab
{
public class MainTabWindow_Animals: RimWorld.MainTabWindow_Animals
{
private static MainTabWindow_Animals _instance;
public MainTabWindow_Animals()
{
_instance = this;
}
public static MainTabWindow_Animals Instance => _instance;
protected override PawnTableDef PawnTableDef => PawnTableDefOf.Animals;
protected override float ExtraTopSpace => Constants.ExtraTopSpace;
protected override float ExtraBottomSpace
{
get
{
if ( Filter )
return Constants.ExtraBottomSpace + ExtraFilterSpace;
return Constants.ExtraBottomSpace;
}
}
public override void DoWindowContents( Rect rect )
{
DoFilterBar( rect );
base.DoWindowContents( rect );
}
private static IEnumerable<FilterWorker> _filters;
public static IEnumerable<FilterWorker> Filters
{
get
{
if ( _filters == null )
_filters = DefDatabase<FilterDef>.AllDefsListForReading.Select( f => f.Worker );
return _filters;
}
}
protected override IEnumerable<Pawn> Pawns => Filter ? FilteredPawns : AllPawns;
public IEnumerable<Pawn> FilteredPawns
{
get
{
if (_filteredPawns == null)
RecachePawns();
return _filteredPawns;
}
}
private IEnumerable<Pawn> _filteredPawns;
public IEnumerable<Pawn> AllPawns
{
get
{
if (_allPawns == null)
RecachePawns();
return _allPawns;
}
}
private IEnumerable<Pawn> _allPawns;
// Note that we're overriding this _only_ because it's called from Notify_PawnsChanged, and unlike that method,
// this one is virtual.
protected override void SetInitialSizeAndPosition()
{
// don't give a damn about this part.
base.SetInitialSizeAndPosition();
// cache our pawn lists.
RecachePawns();
}
public void RecachePawns()
{
_allPawns = base.Pawns;
_filteredPawns = base.Pawns.Where( p => Filters.All( f => f.Allows( p ) ) );
}
private void DoFilterBar( Rect rect )
{
var barWidth = Filters.Count() * ( FilterButtonSize + Margin ) + Margin;
Rect buttonRect = new Rect(rect.xMax - Margin - ButtonSize, rect.yMax - Margin - ButtonSize, ButtonSize, ButtonSize);
Rect barRect = new Rect( buttonRect.xMin - Margin - barWidth, rect.yMax - Margin - ButtonSize, barWidth, ButtonSize );
Rect countRect = new Rect( rect.xMin + Margin, barRect.yMin - Margin - ButtonSize * 2/3f, rect.width - ButtonSize - Margin * 3, ButtonSize * 2/3f );
DrawFilterButton( buttonRect );
if ( Filter )
{
DrawFilters( barRect, Filters );
DrawCounts( countRect );
}
}
private void DrawCounts( Rect rect )
{
Text.Anchor = TextAnchor.UpperRight;
Text.Font = GameFont.Tiny;
GUI.color = Color.grey;
Widgets.Label( rect, "AnimalTab.XofYShown".Translate( Pawns.Count(), AllPawns.Count() ) );
GUI.color = Color.white;
Text.Font = GameFont.Medium;
Text.Anchor = TextAnchor.UpperLeft;
}
private void DrawFilters( Rect rect, IEnumerable<FilterWorker> filters )
{
Widgets.DrawBoxSolid( rect, new Color(0f, 0f, 0f, .2f) );
Rect filterRect = new Rect( Margin, ( ButtonSize - FilterButtonSize ) / 2f, FilterButtonSize, FilterButtonSize );
try
{
GUI.BeginGroup( rect );
foreach ( var filter in filters )
{
filter.Draw( filterRect );
filterRect.x += FilterButtonSize + Margin;
}
}
finally
{
GUI.EndGroup();
}
}
private bool _filter;
public bool Filter
{
get => _filter;
set
{
if ( _filter == value )
return;
_filter = value;
Notify_PawnsChanged();
Notify_ResolutionChanged();
}
}
private void DrawFilterButton( Rect rect )
{
if ( Widgets.ButtonImage( rect, Resources.Filter, Filter ? GenUI.MouseoverColor : Color.white,
Filter ? Color.white : GenUI.MouseoverColor ) )
Filter = !Filter;
}
}
}