Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Color customization to work priorities #86

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# top-most EditorConfig file
root = true

# 4 space indentation
[*.{cs,xml}]
indent_style = space
indent_size = 4
2 changes: 2 additions & 0 deletions Languages/English/Keyed/Keyed-English.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<WorkTab.24HourModeTip>Should times be shown in 12h (e.g. noon, 9p.m.) or 24h (e.g. 12:00, 21:00) mode?</WorkTab.24HourModeTip>
<WorkTab.MaxPriority>Levels of priority</WorkTab.MaxPriority>
<WorkTab.MaxPriorityTip>How many levels of priority should we use? (Limited to between 4 and 9)</WorkTab.MaxPriorityTip>
<WorkTab.ShowPriorityColors>Show Priority Level Colors</WorkTab.ShowPriorityColors>
<WorkTab.PriorityColorsTip>Priority 1 Jobs are the leftmost color. Each next level uses the next color, and the last two colors are blended for any remaining levels</WorkTab.PriorityColorsTip>
<WorkTab.PlaySounds>Sounds</WorkTab.PlaySounds>
<WorkTab.PlaySoundsTip>Play sounds when a priority is changed?</WorkTab.PlaySoundsTip>
<WorkTab.PlayCrunch>Crunchy sounds</WorkTab.PlayCrunch>
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Mod( ModContentPack content ) : base( content ) {
public override void DoSettingsWindowContents(Rect inRect)
{
base.DoSettingsWindowContents(inRect);
Settings.DoWindowContents(inRect);
GetSettings<Settings>().DoWindowContents(inRect);
}

public override string SettingsCategory()
Expand Down
43 changes: 29 additions & 14 deletions Source/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Settings.cs
// 2017-05-22

using System.Collections.Generic;
using System.Resources;
using UnityEngine;
using Verse;
Expand All @@ -10,20 +11,28 @@ namespace WorkTab
{
public class Settings: ModSettings
{
public static int maxPriority = 9;
public static bool playCrunch = true;
public static bool playSounds = true;
public static bool TwentyFourHourMode = true;
public static bool disableScrollwheel = false;
public static bool verticalLabels = true;
private static bool _fontFix = true;

public int maxPriority = 9;
public bool showPriorityColors = false;
public List<string> priorityColors;
public bool playCrunch = true;
public bool playSounds = true;
public bool TwentyFourHourMode = true;
public bool disableScrollwheel = false;
public bool verticalLabels = true;
private bool _fontFix = true;

public static Settings Get()
{
return LoadedModManager.GetMod<WorkTab.Mod>().GetSettings<Settings>();
}
public Settings()
{
ApplyFontFix( _fontFix );
priorityColors = new List<string> { "00ff00", "e6cf89", "808080" };
}

public static void ApplyFontFix( bool state )
public void ApplyFontFix( bool state )
{
LongEventHandler.ExecuteWhenFinished( delegate
{
Expand All @@ -34,15 +43,16 @@ public static void ApplyFontFix( bool state )
} );
}

// buffers
private static bool _fontFixBuffer = _fontFix;
private static string _maxPriorityBuffer = maxPriority.ToString();
// buffers;

public static void DoWindowContents( Rect rect )
{
public void DoWindowContents( Rect rect )
{
var options = new Listing_Standard();
options.Begin(rect);
options.TextFieldNumericLabeled<int>("WorkTab.MaxPriority".Translate(), ref maxPriority, ref _maxPriorityBuffer, 4, 9, "WorkTab.MaxPriorityTip".Translate(), 1 / 8f);
options.TextFieldNumericLabeled<int>("WorkTab.MaxPriority".Translate(), ref maxPriority, maxPriority.ToString(), 4, 9, "WorkTab.MaxPriorityTip".Translate(), 1 / 8f);
options.CheckboxLabeled("WorkTab.ShowPriorityColors".Translate(), ref showPriorityColors, "WorkTab.PriorityColorsTip".Translate());
if (showPriorityColors)
options.ColorBoxes(ref priorityColors, "WorkTab.PriorityColorsTip".Translate());
options.CheckboxLabeled("WorkTab.24HourMode".Translate(), ref TwentyFourHourMode, "WorkTab.24HourModeTip".Translate() );
options.CheckboxLabeled("WorkTab.PlaySounds".Translate(), ref playSounds, "WorkTab.PlaySoundsTip".Translate());
playCrunch = playSounds && playCrunch; // disabling sounds also disables crunch.
Expand Down Expand Up @@ -76,6 +86,8 @@ public static void DoWindowContents( Rect rect )
public override void ExposeData()
{
Scribe_Values.Look(ref maxPriority, "MaxPriority", 9);
Scribe_Values.Look(ref showPriorityColors, "ShowCustomColors", false);
Scribe_Collections.Look(ref priorityColors, "PriorityColors"); // Doesn't seem to accept defaults
Scribe_Values.Look(ref TwentyFourHourMode, "TwentyFourHourMode", true);
Scribe_Values.Look(ref playSounds, "PlaySounds", true);
Scribe_Values.Look(ref playCrunch, "PlayCrunch", true);
Expand All @@ -86,6 +98,9 @@ public override void ExposeData()
// apply font-fix on load
if ( Scribe.mode == LoadSaveMode.PostLoadInit )
ApplyFontFix( _fontFix );

if (priorityColors == null)
priorityColors = new List<string> { "00ff00", "e6cf89", "808080" };
}

#endregion
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/VanillaWorkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public static class VanillaWorkSettings

public static int GetVanillaPriority( this Pawn pawn, WorkTypeDef worktype )
{
//public override float GetPriority(Pawn pawn)
if (pawn.workSettings == null || !pawn.workSettings.EverWork)
{
return 0;
}

if ( prioritiesFieldInfo == null )
{
prioritiesFieldInfo = typeof( Pawn_WorkSettings ).GetField( "priorities", AccessTools.all );
Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/Int_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class Int_Extensions
public static string FormatHour( this int hour )
{
// 24-hour is simple
if ( Settings.TwentyFourHourMode )
if (Settings.Get().TwentyFourHourMode )
return hour.ToString( "D2" ) + ":00";

// noon/midnight are special
Expand Down
56 changes: 53 additions & 3 deletions Source/Extensions/Listing_Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using Verse;

namespace WorkTab
Expand All @@ -15,7 +17,7 @@ public static void CheckboxLabeled(this Listing_Standard listing, string label,
listing.Gap(listing.verticalSpacing);
}

public static void TextFieldNumericLabeled<T>(this Listing_Standard listing, string label, ref T value, ref string buffer, float min = 0, float max = float.MaxValue, string tooltip = null, float textfieldWidthFraction = 1 / 3f) where T : struct
public static void TextFieldNumericLabeled<T>(this Listing_Standard listing, string label, ref T value, string buffer, float min = 0, float max = float.MaxValue, string tooltip = null, float textfieldWidthFraction = 1 / 3f) where T : struct
{
Rect rect = listing.GetRect(Text.LineHeight);
Rect fieldRect = rect;
Expand All @@ -26,7 +28,55 @@ public static void TextFieldNumericLabeled<T>(this Listing_Standard listing, str
TooltipHandler.TipRegion(rect, tooltip);
Widgets.DrawHighlightIfMouseover(rect);
Widgets.Label(labelRect, label);
Widgets.TextFieldNumeric(fieldRect, ref value, ref buffer, min, max );
Widgets.TextFieldNumeric(fieldRect, ref value, ref buffer, min, max);
listing.Gap(listing.verticalSpacing);
}

public static void ColorBoxes(this Listing_Standard listing, ref List<string> colors, string tooltip = null)
{
Rect rect = listing.GetRect(Text.LineHeight);
if (!tooltip.NullOrEmpty())
TooltipHandler.TipRegion(rect, tooltip);

Rect boxRect = rect;
boxRect.width = Text.LineHeight;
boxRect.x += Text.LineHeight + 1;
Rect hexRect = boxRect;
hexRect.x += boxRect.width + 1;
hexRect.width = 60; // 60 to fit "FFFFFF"
for (int i = 0; i < colors.Count; i++)
{
if(i == colors.Count - 1)
{
Widgets.TextArea(boxRect, "...", true);
boxRect.x += boxRect.width + 1;
hexRect.x += boxRect.width + 1;
}
string color = colors[i];
Color c = new Color();
ColorUtility.TryParseHtmlString("#"+color, out c);
Widgets.DrawBoxSolid(boxRect, c);
Widgets.DrawBox(boxRect);
string newHex = Widgets.TextField(hexRect, color, 6, new Regex("[0-9a-fA-F]*"));
colors[i] = newHex;
float xdiff = boxRect.width + 2 + hexRect.width;
boxRect.x += xdiff;
hexRect.x += xdiff;
}

Rect lessRect = rect;
lessRect.width = Text.LineHeight;
Rect moreRect = rect;
moreRect.width = Text.LineHeight;
moreRect.x = rect.xMax - Text.LineHeight;

if (colors.Count > 2)
if (Widgets.ButtonText(lessRect, "-"))
colors.RemoveAt(colors.Count - 2);
if (colors.Count < 9)
if (Widgets.ButtonText(moreRect, "+"))
colors.Insert(colors.Count - 1, "FFFFFF");

listing.Gap(listing.verticalSpacing);
}
}
Expand Down
102 changes: 99 additions & 3 deletions Source/Extensions/Pawn_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace WorkTab
{
public static class Pawn_Extensions
{
public static int GetPriority( this Pawn pawn, WorkTypeDef worktype, int hour )
public static int GetMinPriority(this Pawn pawn, WorkTypeDef worktype, int hour)
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);
Expand All @@ -33,6 +33,73 @@ public static int GetPriority( this Pawn pawn, WorkTypeDef worktype, int hour )
// or, in simple mode, just 3.
return 3;
}
public static int GetMaxPriority(this Pawn pawn, WorkTypeDef worktype, int hour)
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);

// get priorities for all workgivers in worktype
var priorities = worktype.WorkGivers().Select( wg => GetPriority( pawn, wg, hour ) ).Where( p => p > 0 );

// if there are no active priorities, return zero
if ( !priorities.Any() )
return 0;

// otherwise, return the highest number (lowest priority).
if (Find.PlaySettings.useWorkPriorities)
return priorities.Max();

// or, in simple mode, just 3.
return 3;
}
public static bool AnyGiverMissingPriority(this Pawn pawn, WorkTypeDef worktype, int hour)
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);

// get priorities for all workgivers in worktype
return worktype.WorkGivers().Any(wg => GetPriority(pawn, wg, hour) == 0);
}
public static int GetPriority( this Pawn pawn, WorkTypeDef worktype, int hour )
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);

// get priorities for all workgivers in worktype
var priorities = worktype.WorkGivers().Select( wg => GetPriority( pawn, wg, hour ) );

// if there are no active priorities, return zero
if ( !priorities.Any() )
return 0;

// otherwise, return the most common number
if (Find.PlaySettings.useWorkPriorities)
{
//count each priority level, track highest
Dictionary<int, int> priorityCount = new Dictionary<int, int>();
int highestCount = 0;
int commonPriority = 0;

foreach(var p in priorities)
{
int count = 1;
if (priorityCount.ContainsKey(p))
count = priorityCount[p] + 1;
priorityCount[p] = count;

if (count > highestCount)
{
highestCount = count;
commonPriority = p;
}
}

return commonPriority;
}

// or, in simple mode, just 3.
return 3;
}

public static int[] GetPriorities(this Pawn pawn, WorkTypeDef worktype)
{
Expand Down Expand Up @@ -92,10 +159,10 @@ public static void SetPriority( this Pawn pawn, WorkGiverDef workgiver, int prio
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);
if ( priority > Settings.maxPriority )
if ( priority > Settings.Get().maxPriority )
priority = 0;
if ( priority < 0 )
priority = Settings.maxPriority;
priority = Settings.Get().maxPriority;

Logger.Trace( $"Setting {pawn.LabelShort}'s {workgiver.defName} priority for {hour} to {priority}" );
PriorityManager.Set[pawn][workgiver][hour] = priority;
Expand All @@ -110,6 +177,35 @@ public static void DisableAll( this Pawn pawn )
pawn.SetPriority( worktype, 0, null );
}

public static void ChangePriority( this Pawn pawn, WorkTypeDef worktype, int diff, List<int> hours )
{
foreach (int hour in (hours ?? WholeDay))
ChangePriority(pawn, worktype, diff, hour, false);

PriorityManager.Get[pawn].Recache(worktype);
}

public static void ChangePriority(Pawn pawn, WorkTypeDef worktype, int diff, int hour, bool recache = true )
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);

foreach (WorkGiverDef workgiver in worktype.WorkGivers())
ChangePriority(pawn, workgiver, diff, hour, false);

if (recache)
PriorityManager.Get[pawn].Recache(worktype);
}

public static void ChangePriority( this Pawn pawn, WorkGiverDef workgiver, int diff, int hour, bool recache = true )
{
if (hour < 0)
hour = GenLocalDate.HourOfDay(pawn);

int priority = pawn.GetPriority(workgiver, hour) + diff;
SetPriority(pawn, workgiver, priority, hour, recache);
}

public static bool CapableOf( this Pawn pawn, WorkGiverDef workgiver )
{
return !workgiver.requiredCapacities.Any( c => !pawn.health.capacities.CapableOf( c ) );
Expand Down
12 changes: 6 additions & 6 deletions Source/Extensions/WorkGiver_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static void DecrementPriority( this WorkGiverDef workgiver, Pawn pawn, in
pawn.SetPriority( workgiver, priority - 1, hours );

// play sounds
if ( Settings.playSounds && playSound && priority > 1 )
if (Settings.Get().playSounds && playSound && priority > 1 )
SoundDefOf.AmountIncrement.PlayOneShotOnCamera();
if ( Settings.playSounds && playSound && priority == 1 )
if (Settings.Get().playSounds && playSound && priority == 1 )
SoundDefOf.AmountDecrement.PlayOneShotOnCamera();
}

Expand All @@ -42,9 +42,9 @@ public static void IncrementPriority( this WorkGiverDef workgiver, Pawn pawn, in
pawn.SetPriority( workgiver, priority + 1, hours );

// play sounds
if ( Settings.playSounds && playSound && priority == 0 )
if (Settings.Get().playSounds && playSound && priority == 0 )
SoundDefOf.AmountIncrement.PlayOneShotOnCamera();
if ( Settings.playSounds && playSound && priority > 0 )
if (Settings.Get().playSounds && playSound && priority > 0 )
SoundDefOf.AmountDecrement.PlayOneShotOnCamera();
}

Expand All @@ -60,7 +60,7 @@ public static void DecrementPriority( this WorkGiverDef workgiver, List<Pawn> pa
hour = GenLocalDate.HourOfDay( pawns.FirstOrDefault() );

// play sounds
if ( Settings.playSounds && playSound && pawns.Any( p => p.GetPriority( workgiver, hour ) != 1 ) )
if (Settings.Get().playSounds && playSound && pawns.Any( p => p.GetPriority( workgiver, hour ) != 1 ) )
SoundDefOf.AmountIncrement.PlayOneShotOnCamera();

// decrease priorities that are not 1 only (no wrapping around once we're at max priority)
Expand All @@ -80,7 +80,7 @@ public static void IncrementPriority( this WorkGiverDef workgiver, List<Pawn> pa
hour = GenLocalDate.HourOfDay( pawns.FirstOrDefault() );

// play sounds
if ( Settings.playSounds && playSound && pawns.Any( p => p.GetPriority( workgiver, hour ) > 0 ) )
if (Settings.Get().playSounds && playSound && pawns.Any( p => p.GetPriority( workgiver, hour ) > 0 ) )
SoundDefOf.AmountDecrement.PlayOneShotOnCamera();

// increase priorities that are > 0 only (no wrapping around once we're at min priority
Expand Down
Loading