Skip to content

Commit

Permalink
2.0.0: Rewrite, new tools.
Browse files Browse the repository at this point in the history
- Most of the mod was rewritten for a cleaner codebase and better performance
- Added Forbid selection tool
- Added Unforbid everything tool
- Added Mass selection tool
- Added hotkeys for quick tool selection
- Tools have received custom textures
  • Loading branch information
UnlimitedHugs committed Sep 14, 2016
1 parent add3da5 commit 2ce2b6d
Show file tree
Hide file tree
Showing 36 changed files with 660 additions and 249 deletions.
11 changes: 9 additions & 2 deletions AllowTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Designator_AllowTool.cs" />
<Compile Include="Source\ItemDesignationDragger.cs" />
<Compile Include="Source\AllowToolController.cs" />
<Compile Include="Source\AllowToolUtility.cs" />
<Compile Include="Source\Designator_Allow.cs" />
<Compile Include="Source\Designator_AllowAll.cs" />
<Compile Include="Source\Designator_Forbid.cs" />
<Compile Include="Source\Designator_MassSelect.cs" />
<Compile Include="Source\Designator_SelectableThings.cs" />
<Compile Include="Source\UnlimitedDesignationDragger.cs" />
<Compile Include="Source\ModInitializer.cs" />
<Compile Include="Source\ModInitializerComponent.cs" />
<Compile Include="Source\ThingDesignatorDef.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Binary file added ArtSource/allow.psd
Binary file not shown.
Binary file added ArtSource/allowAll.psd
Binary file not shown.
Binary file added ArtSource/allowHighlight.psd
Binary file not shown.
Binary file added ArtSource/forbid.psd
Binary file not shown.
Binary file added ArtSource/forbidHighlight.psd
Binary file not shown.
Binary file added ArtSource/selectAll.psd
Binary file not shown.
Binary file added ArtSource/selectAllHighlight.psd
Binary file not shown.
7 changes: 4 additions & 3 deletions Mods/AllowTool/About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<targetVersion>0.15.0</targetVersion>
<url>https://ludeon.com/forums/index.php?topic=17218.0</url>
<description>&lt;size=20&gt;Description&lt;/size&gt;
Allows to unforbid all forbidden items in an area.
A set of tools to make handling forbidden items easier.

&lt;size=20&gt;How to use&lt;/size&gt;
To select the tool, open the Architect menu, click Orders, then Unforbid.
Click and drag over an area with forbidden items.
The tools are located in the Architect menu, under the Orders tab. Hovering over each tool shows what it does and how to use it.

Each tool can also be selected with a hotkey without opening the menu. See the Keyboard configuration panel in the options menu to customize the hotkeys.
</description>


Expand Down
Binary file modified Mods/AllowTool/Assemblies/AllowTool.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>

<KeyBindingCategoryDefs>

<KeyBindingCategoryDef>
<defName>AllowTool</defName>
<label>Allow Tool</label>
<description>Shortcuts for designators provided by the Allow Tool mod.</description>
<checkForConflicts></checkForConflicts>
</KeyBindingCategoryDef>

</KeyBindingCategoryDefs>
32 changes: 32 additions & 0 deletions Mods/AllowTool/Defs/KeyBindingDefs/KeyBindings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>

<KeyBindingDefs>

<KeyBindingDef>
<category>AllowTool</category>
<defName>DesignatorAllow</defName>
<label>Designator: Allow</label>
<defaultKeyCodeA>U</defaultKeyCodeA>
</KeyBindingDef>

<KeyBindingDef>
<category>AllowTool</category>
<defName>DesignatorForbid</defName>
<label>Designator: Forbid</label>
<defaultKeyCodeA>I</defaultKeyCodeA>
</KeyBindingDef>

<KeyBindingDef>
<category>AllowTool</category>
<defName>DesignatorAllowAll</defName>
<label>Designator: Allow all</label>
</KeyBindingDef>

<KeyBindingDef>
<category>AllowTool</category>
<defName>DesignatorMassSelect</defName>
<label>Designator: Mass select</label>
<defaultKeyCodeA>O</defaultKeyCodeA>
</KeyBindingDef>

</KeyBindingDefs>
83 changes: 83 additions & 0 deletions Mods/AllowTool/Defs/ThingDesignatorDefs/Designators.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>

<AllowTool.ThingDesignatorDef>
<defName>AllowDesignator</defName>
<!-- the class that handles the behaviour of this designator. Must extend AllowTool.Designator_SelectableThing -->
<designatorClass>AllowTool.Designator_Allow</designatorClass>
<!-- hides the designator -->
<hidden>false</hidden>
<!-- the category def name to add this designator to -->
<category>Orders</category>
<!-- the designator type to insert this designator after -->
<insertAfter>Designator_Claim</insertAfter>
<!-- the label on the button, optional -->
<label>Unforbid selection</label>
<!-- button tooltip, optional -->
<description>Unforbid all items in the selected area.
Click and drag over an area to use.</description>
<!-- the icon texture for the button -->
<iconTex>allow</iconTex>
<!-- the texture to appear over a tile with items this designator is dragged over -->
<dragHighlightTex>allowHighlight</dragHighlightTex>
<!-- sound def played after a successful designation -->
<soundSucceded>TickHigh</soundSucceded>
<!-- key binding def used to activate the designator -->
<hotkeyDef>DesignatorAllow</hotkeyDef>
<!-- a string id to be displayed as a message on designation success. Optional. -->
<messageSuccess>Allow_area_success</messageSuccess>
<!-- a string id to be displayed as a message on designation failure. Optional. -->
<messageFailure>Allow_area_failure</messageFailure>
</AllowTool.ThingDesignatorDef>

<AllowTool.ThingDesignatorDef>
<defName>ForbidDesignator</defName>
<designatorClass>AllowTool.Designator_Forbid</designatorClass>
<hidden>false</hidden>
<category>Orders</category>
<insertAfter>AllowTool.Designator_Allow</insertAfter>
<label>Forbid selection</label>
<description>Forbid all items in the selected area.
Click and drag over an area to use.</description>
<iconTex>forbid</iconTex>
<dragHighlightTex>forbidHighlight</dragHighlightTex>
<soundSucceded>TickLow</soundSucceded>
<hotkeyDef>DesignatorForbid</hotkeyDef>
<messageSuccess>Forbid_area_success</messageSuccess>
<messageFailure>Forbid_area_failure</messageFailure>
</AllowTool.ThingDesignatorDef>

<AllowTool.ThingDesignatorDef>
<defName>AllowAllDesignator</defName>
<designatorClass>AllowTool.Designator_AllowAll</designatorClass>
<hidden>false</hidden>
<category>Orders</category>
<insertAfter>AllowTool.Designator_Forbid</insertAfter>
<label>Unforbid everything</label>
<description>Unforbids all forbidden items on the map.
Click to activate.</description>
<iconTex>allowAll</iconTex>
<dragHighlightTex>allowHighlight</dragHighlightTex>
<soundSucceded>TickHigh</soundSucceded>
<hotkeyDef>DesignatorAllowAll</hotkeyDef>
<messageSuccess>Allow_all_success</messageSuccess>
<messageFailure>Allow_all_failure</messageFailure>
</AllowTool.ThingDesignatorDef>

<AllowTool.ThingDesignatorDef>
<defName>MassSelectDesignator</defName>
<designatorClass>AllowTool.Designator_MassSelect</designatorClass>
<hidden>false</hidden>
<category>Orders</category>
<insertAfter>AllowTool.Designator_AllowAll</insertAfter>
<label>Mass select</label>
<description>Selects everything in an area, ignoring common sense and selection limits. Selecting too much stuff at once can slow down the game- use with care.
Hold Shift to add to your current selection.
Hold Ctrl/Option to invert selection.</description>
<iconTex>massSelect</iconTex>
<dragHighlightTex>massSelectHighlight</dragHighlightTex>
<soundSucceded>ThingSelected</soundSucceded>
<hotkeyDef>DesignatorMassSelect</hotkeyDef>
</AllowTool.ThingDesignatorDef>

</Defs>
9 changes: 0 additions & 9 deletions Mods/AllowTool/Languages/English/Keyed/AllowTool.xml

This file was deleted.

13 changes: 13 additions & 0 deletions Mods/AllowTool/Languages/English/Keyed/AllowToolStrings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<LanguageData>

<Allow_area_success>Allowed {0} items.</Allow_area_success>
<Allow_area_failure>No forbidden items found.</Allow_area_failure>

<Forbid_area_success>Forbidden {0} items.</Forbid_area_success>
<Forbid_area_failure>No forbiddable items found.</Forbid_area_failure>

<Allow_all_success>Allowed {0} things across the map.</Allow_all_success>
<Allow_all_failure>No forbidden things found.</Allow_all_failure>

</LanguageData>
Binary file added Mods/AllowTool/Textures/allow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/allowAll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/allowHighlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/forbid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/forbidHighlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/massSelect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mods/AllowTool/Textures/massSelectHighlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.0")]
[assembly: AssemblyFileVersion("1.5.0")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0")]
97 changes: 97 additions & 0 deletions Source/AllowToolController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using Verse;

namespace AllowTool {
/**
* The hub of the mod.
* Injects the custom designators and handles hotkey presses.
*/
public class AllowToolController {
private const int MapSceneIndex = 1;

private static FieldInfo resolvedDesignatorsField;
private static AllowToolController instance;
public static AllowToolController Instance {
get { return instance ?? (instance = new AllowToolController()); }
}

private readonly List<HotkeyEntry> activeHotkeys = new List<HotkeyEntry>();

public UnlimitedDesignationDragger Dragger { get; private set; }

private AllowToolController() {
Dragger = new UnlimitedDesignationDragger();
InitReflectionFields();
}

public void Update() {
Dragger.Update();
}

public void OnGUI() {
if(Event.current.type != EventType.KeyDown) return;
CheckForHotkeyPresses();
}

public void OnLevelLoaded(int level) {
if(level != MapSceneIndex) return;
if (DefDatabase<ThingDesignatorDef>.DefCount == 0) {
activeHotkeys.Clear(); // mod was unloaded
}
TryInjectDesignators();
}

private void TryInjectDesignators() {
var numDesignatorsInjected = 0;
foreach (var designatorDef in DefDatabase<ThingDesignatorDef>.AllDefs) {
if (designatorDef.hidden || designatorDef.Injected) continue;
var resolvedDesignators = (List<Designator>)resolvedDesignatorsField.GetValue(designatorDef.Category);
var insertIndex = -1;
for (var i = 0; i < resolvedDesignators.Count; i++) {
if(resolvedDesignators[i].GetType() != designatorDef.insertAfter) continue;
insertIndex = i;
break;
}
if (insertIndex >= 0) {
var designator = (Designator) Activator.CreateInstance(designatorDef.designatorClass, designatorDef);
resolvedDesignators.Insert(insertIndex + 1, designator);
if (designatorDef.hotkeyDef != null) {
activeHotkeys.Add(new HotkeyEntry(designatorDef.hotkeyDef, designator));
}
numDesignatorsInjected++;
} else {
AllowToolUtility.Error(string.Format("Failed to inject {0} after {1}", designatorDef.defName, designatorDef.insertAfter.Name));
}
designatorDef.Injected = true;
}
if (numDesignatorsInjected > 0) {
AllowToolUtility.Log("Injected " + numDesignatorsInjected + " designators");
}
}

private void InitReflectionFields() {
resolvedDesignatorsField = typeof (DesignationCategoryDef).GetField("resolvedDesignators", BindingFlags.NonPublic | BindingFlags.Instance);
if (resolvedDesignatorsField == null) AllowToolUtility.Error("failed to reflect DesignationCategoryDef.resolvedDesignators");
}

private void CheckForHotkeyPresses() {
for (int i = 0; i < activeHotkeys.Count; i++) {
if(!activeHotkeys[i].key.JustPressed) continue;
activeHotkeys[i].designator.ProcessInput(Event.current);
break;
}
}

private class HotkeyEntry {
public readonly KeyBindingDef key;
public readonly Designator designator;
public HotkeyEntry(KeyBindingDef key, Designator designator) {
this.key = key;
this.designator = designator;
}
}
}
}
13 changes: 13 additions & 0 deletions Source/AllowToolUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace AllowTool {
public class AllowToolUtility {
private const string logPrefix = "[AllowTool] ";

public static void Log(object message) {
Verse.Log.Message(logPrefix + message);
}

public static void Error(object message) {
Verse.Log.Error(logPrefix + message);
}
}
}
27 changes: 27 additions & 0 deletions Source/Designator_Allow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using RimWorld;
using Verse;

namespace AllowTool {
public class Designator_Allow : Designator_SelectableThings {
public Designator_Allow(ThingDesignatorDef def) : base(def) {
}

protected override bool ThingIsRelevant(Thing item) {
var comp = item is ThingWithComps ? (item as ThingWithComps).GetComp<CompForbiddable>() : null;
return comp != null && comp.Forbidden;
}

override protected int ProcessCell(IntVec3 c) {
var hitCount = 0;
var cellThings = Find.ThingGrid.ThingsListAtFast(c);
for (var i = 0; i < cellThings.Count; i++) {
var thing = cellThings[i];
if (thing.def.selectable && thing.IsForbidden(Faction.OfPlayer)) {
thing.SetForbidden(false);
hitCount++;
}
}
return hitCount;
}
}
}
43 changes: 43 additions & 0 deletions Source/Designator_AllowAll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using RimWorld;
using UnityEngine;
using Verse;
using Verse.Sound;

namespace AllowTool {
public class Designator_AllowAll : Designator_SelectableThings {
public Designator_AllowAll(ThingDesignatorDef def) : base(def) {
}

public override void ProcessInput(Event ev) {
if (!CheckCanInteract()) return;
AllowAllTheThings();
}

protected override bool ThingIsRelevant(Thing item) {
return false;
}

protected override int ProcessCell(IntVec3 cell) {
return 0;
}

private void AllowAllTheThings() {
var things = Find.ListerThings.AllThings;
var tallyCount = 0;
for (var i = 0; i < things.Count; i++) {
var thing = things[i];
var comp = thing is ThingWithComps ? (thing as ThingWithComps).GetComp<CompForbiddable>() : null;
if (comp != null && comp.Forbidden) {
comp.Forbidden = false;
tallyCount++;
}
}
if (tallyCount > 0) {
if (def.messageSuccess != null) Messages.Message(def.messageSuccess.Translate(tallyCount.ToString()), MessageSound.Silent);
def.soundSucceded.PlayOneShotOnCamera();
} else {
if (def.messageFailure != null) Messages.Message(def.messageFailure.Translate(), MessageSound.RejectInput);
}
}
}
}
Loading

0 comments on commit 2ce2b6d

Please sign in to comment.