Skip to content

Commit

Permalink
[adds]
Browse files Browse the repository at this point in the history
 - area overlay
 - dynamicOverlays toggle in minimap def to suppress 'no overlays' error
 - removed some redundant code (Size property where Find.Map.Size is just as simple)
 - some missing translations/icons

[fixed]
 - scale button keeps minimap anchored to the same quadrant of the screen on resize.
  • Loading branch information
FluffierThanThou committed May 18, 2016
1 parent c83e716 commit 55e786e
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 68 deletions.
2 changes: 2 additions & 0 deletions DLL_Project/CommunityCoreLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<Compile Include="Extensions\Vector3_Extensions.cs" />
<Compile Include="Extensions\Def_Extensions.cs" />
<Compile Include="Interfaces\IHelpDefView.cs" />
<Compile Include="MiniMapOverlays\MiniMapOverlay_Area.cs" />
<Compile Include="MiniMaps\MiniMap_Areas.cs" />
<Compile Include="Structures\CompInjectionSet.cs" />
<Compile Include="Defs\MainTab_HelpMenuDef.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
3 changes: 2 additions & 1 deletion DLL_Project/Defs/MiniMapDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class MiniMapDef : Def
public Type miniMapClass = null;

// Can link overlays directly here
public List<MiniMapOverlayDef> overlays = null;
public List<MiniMapOverlayDef> overlays = new List<MiniMapOverlayDef>();
public bool dynamicOverlays = false;

#endregion

Expand Down
90 changes: 43 additions & 47 deletions DLL_Project/MiniMapOverlays/MiniMapOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
using System;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RimWorld;
using UnityEngine;
using Verse;

namespace CommunityCoreLibrary.MiniMap
{

public abstract class MiniMapOverlay // : IConfigurable
{

#region Instance Data
#region Fields

public MiniMapOverlayDef overlayDef;
private bool _hidden = false;

private Texture2D _texture;
private MiniMap minimap;

public MiniMapOverlayDef overlayDef;

#endregion Instance Data
#endregion Fields

#region Properties
#region Constructors

public Texture2D texture
public MiniMapOverlay( MiniMap minimap, MiniMapOverlayDef overlayDef )
{
get
{
if ( _texture == null )
{
_texture = new Texture2D( MiniMap_Utilities.Size.x, MiniMap_Utilities.Size.z );

// not sure clearing pixels is strictly necessary, but can't hurt much
_texture.SetPixels( MiniMap_Utilities.GetClearPixelArray );
_texture.Apply();
}
return _texture;
}
this.minimap = minimap;
this.overlayDef = overlayDef;
_hidden = overlayDef.hiddenByDefault;
}

public virtual bool Hidden
#endregion Constructors

#region Properties

public virtual bool Hidden
{
get
{
Expand All @@ -53,7 +44,7 @@ public virtual bool Hidden
_hidden = value;

// make entire minimap visible if any overlay is made visible
if( !_hidden )
if ( !_hidden )
{
minimap.Hidden = value;
}
Expand All @@ -66,66 +57,71 @@ public virtual bool Hidden
}
}

public string label
public virtual string label
{
get
{
if( overlayDef.labelKey.NullOrEmpty() )
if ( overlayDef.labelKey.NullOrEmpty() )
{
return overlayDef.label;
}
return overlayDef.labelKey.Translate();
}
}

public string LabelCap
public virtual string LabelCap
{
get
{
return label.CapitalizeFirst();
}
}

#endregion

public void ClearTexture( bool apply = false )
public Texture2D texture
{
texture.SetPixels( MiniMap_Utilities.GetClearPixelArray );
if ( apply )
texture.Apply();
}

#region Constructors
get
{
if ( _texture == null )
{
_texture = new Texture2D( Find.Map.Size.x, Find.Map.Size.z );

public MiniMapOverlay( MiniMap minimap, MiniMapOverlayDef overlayDef )
{
this.minimap = minimap;
this.overlayDef = overlayDef;
_hidden = overlayDef.hiddenByDefault;
// not sure clearing pixels is strictly necessary, but can't hurt much
_texture.SetPixels( MiniMap_Utilities.GetClearPixelArray );
_texture.Apply();
}
return _texture;
}
}

#endregion Constructors
#endregion Properties

#region Methods

// Required, update your texture, caller (MiniMap.Update()) will re-Apply()
public abstract void Update();
public void ClearTexture( bool apply = false )
{
texture.SetPixels( MiniMap_Utilities.GetClearPixelArray );
if ( apply )
texture.Apply();
}

// Optional float menu option for this overlay
public virtual List<FloatMenuOption> GetFloatMenuOptions()
public virtual List<FloatMenuOption> GetFloatMenuOptions()
{
// create empty list of options
var options = new List<FloatMenuOption>();

// add a toggle option for overlays that are not always on, and only if there's more than one overlay on this minimap 'mode'
if ( !minimap.miniMapDef.alwaysVisible && minimap.overlayWorkers.Count > 1 )
options.Add( new FloatMenuOption( "MiniMap.ToggleX".Translate( LabelCap ), delegate
options.Add( new FloatMenuOption( "MiniMap.ToggleX".Translate( Hidden ? "Off".Translate().ToUpper() : "On".Translate().ToUpper(), LabelCap ), delegate
{ Hidden = !Hidden; } ) );

// done!
return options;
}

// Required, update your texture, caller (MiniMap.Update()) will re-Apply()
public abstract void Update();

#endregion Methods
}
}
56 changes: 56 additions & 0 deletions DLL_Project/MiniMapOverlays/MiniMapOverlay_Area.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using CommunityCoreLibrary;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;

namespace CommunityCoreLibrary.MiniMap
{
public class MiniMapOverlay_Area : MiniMapOverlay
{
#region Fields

public Area area;
public static float opacity = .5f;

#endregion Fields

#region Constructors

public MiniMapOverlay_Area( MiniMap minimap, MiniMapOverlayDef def, Area area ) : base( minimap, def )
{
this.area = area;

// initial update
Update();
texture.Apply();
}

#endregion Constructors

#region Properties

public override string label => area.Label;

#endregion Properties

#region Methods

public override void Update()
{
Color color = area.Color;
color.a = opacity;
ClearTexture();

foreach ( var cell in area.ActiveCells )
{
texture.SetPixel( cell.x, cell.z, color );
}
}

#endregion Methods
}
}
54 changes: 54 additions & 0 deletions DLL_Project/MiniMaps/MiniMap_Areas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using CommunityCoreLibrary;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;

namespace CommunityCoreLibrary.MiniMap
{
public class MiniMap_Areas : MiniMap
{
#region Constructors

public MiniMap_Areas( MiniMapDef def ) : base( def ) { }

#endregion Constructors

#region Methods

public override void Update()
{
UpdateAreaOverlays();
}

private void UpdateAreaOverlays()
{
// since this is called every frame (FRAME, not tick), just checking area count should be good enough to detect changes.
if ( Find.AreaManager.AllAreas.Count == overlayWorkers.Count )
return;

// check if we need to add area overlays
foreach ( var area in Find.AreaManager.AllAreas )
{
if ( !overlayWorkers.Any( w => ((MiniMapOverlay_Area)w).area == area ) )
overlayWorkers.Add( new MiniMapOverlay_Area( this, new MiniMapOverlayDef(), area ) );
}

// check if we should remove area overlays
if ( overlayWorkers?.Any() ?? false )
{
for ( int i = overlayWorkers.Count - 1; i >= 0; i-- )
{
MiniMapOverlay_Area overlay = overlayWorkers[i] as MiniMapOverlay_Area;
if ( overlay == null || !Find.AreaManager.AllAreas.Contains( overlay.area ) )
overlayWorkers.RemoveAt( i );
}
}
}

#endregion Methods
}
}
10 changes: 1 addition & 9 deletions DLL_Project/MiniMaps/MiniMap_Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ public static class MiniMap_Utilities
{

static Color[] _clearPixelArray;

public static IntVec2 Size
{
get
{
return new IntVec2( Find.Map.Size.x, Find.Map.Size.z );
}
}


public static Color[] GetClearPixelArray
{
get
Expand Down
2 changes: 1 addition & 1 deletion DLL_Project/SubControllers/MiniMapSubController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override bool Validate()
miniMapDef.overlays.AddUnique( overlayDef );
}
}
if( miniMapDef.overlays.NullOrEmpty() )
if( miniMapDef.overlays.NullOrEmpty() && !miniMapDef.dynamicOverlays )
{
CCL_Log.Trace(
Verbosity.NonFatalErrors,
Expand Down
Loading

0 comments on commit 55e786e

Please sign in to comment.