Skip to content

Commit

Permalink
fixed context menu bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Sep 17, 2023
1 parent 5c594ec commit 568c328
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
Binary file modified 1.4/Assemblies/AchtungMod.dll
Binary file not shown.
25 changes: 13 additions & 12 deletions Source/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public static void InstallDefs()
.DoIf(def => DefDatabase<JobDef>.GetNamedSilentFail(def.defName) == null, DefDatabase<JobDef>.Add);
}

bool ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell)
bool ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell, Action gotoAction)
{
if (actions == null)
return true;
var menuAdded = false;
var optionTaken = false;
if (actions.Count(false) > 0)
{
var optionTaken = false;
if (cell.InBounds(map) && forceMenu == false)
{
var autoTakableOptions = actions.GetAutoTakeableActions();
Expand All @@ -82,6 +82,12 @@ bool ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell)
}
if (menuAdded)
EndDragging();
if (optionTaken == false && menuAdded == false && actions.EveryoneHasGoto && gotoAction != null)
{
actions.allPawns.Do(pawn => Tools.SetDraftStatus(pawn, true, false));
gotoAction();
return true;
}
if (menuAdded == false && Achtung.Settings.positioningEnabled == false)
return true;
Event.current.Use();
Expand All @@ -99,10 +105,7 @@ public bool MouseDown(Vector3 pos, int button, bool longPress)
colonists = Tools.GetSelectedColonists();

if (colonists.Count == 0)
{
suppressMenu = true;
return true;
}

if (isDragging && button == (int)Button.left && groupMovement == false)
{
Expand All @@ -112,10 +115,7 @@ public bool MouseDown(Vector3 pos, int button, bool longPress)
}

if (button != (int)Button.right)
{
suppressMenu = true;
return true;
}

var actions = doForceMenu ? new MultiActions(colonists, UI.MouseMapPosition()) : null;
var achtungPressed = Tools.IsModKeyPressed(Achtung.Settings.achtungKey);
Expand Down Expand Up @@ -149,7 +149,7 @@ public bool MouseDown(Vector3 pos, int button, bool longPress)
{
if (actions == null)
return true;
return ShowMenu(actions, forceMenu, map, cell);
return ShowMenu(actions, forceMenu, map, cell, null);
}

if (achtungPressed)
Expand All @@ -162,7 +162,7 @@ public bool MouseDown(Vector3 pos, int button, bool longPress)
return true;
}

return ShowMenu(actions, forceMenu, map, cell);
return ShowMenu(actions, forceMenu, map, cell, () => StartDragging(pos, achtungPressed));
}

private void StartDragging(Vector3 pos, bool asGroup)
Expand Down Expand Up @@ -199,7 +199,6 @@ private void EndDragging()
Event.current.Use();
}
isDragging = false;
suppressMenu = false;
}

public void MouseDrag(Vector3 pos)
Expand Down Expand Up @@ -420,7 +419,9 @@ public bool HandleEvents()
switch (Event.current.rawType)
{
case EventType.MouseDown:
longPressThreshold = Environment.TickCount + Achtung.Settings.menuDelay;
if (Event.current.button == (int)Button.right)
suppressMenu = false;
longPressThreshold = Event.current.button == (int)Button.right ? Environment.TickCount + Achtung.Settings.menuDelay : -1;
runOriginal = MouseDown(pos, Event.current.button, false);
MouseDrag(pos);
break;
Expand Down
9 changes: 5 additions & 4 deletions Source/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ static class Game_UpdatePlay_Patch

// static readonly TimeSlice timeSlicer = new(3f, 0, 1000000, () => it.MoveNext());

static float maxDeltaTime = 0.04f;
const int iterations = 5000;
const int minTfpsLimit = 2;
static float maxDeltaTime = 0.04f;
static int prevFrames = 0, tfps = 6;
static int prevTenthSecond = -1;

Expand Down Expand Up @@ -162,11 +163,11 @@ public static void Postfix()
tfps = prevFrames;
prevFrames = 0;
}
if (tfps >= 5)
if (tfps >= minTfpsLimit + 1)
maxDeltaTime += 0.001f;
else if (tfps == 4)
else if (tfps == minTfpsLimit)
maxDeltaTime -= 0.001f;
else if (tfps < 4)
else if (tfps < minTfpsLimit)
maxDeltaTime = 0.04f;

var n = (maxDeltaTime - Time.deltaTime) * iterations;
Expand Down
23 changes: 16 additions & 7 deletions Source/MultiActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ namespace AchtungMod
public class MultiActions
{
public Vector3 clickPos;
public readonly List<Pawn> allPawns;
readonly List<MultiAction> allActions;
int totalColonistsInvolved = 0;
readonly bool everyoneHasGoto = false;
readonly int totalColonistsInvolved = 0;
int gotoActionCount = 0;

public MultiActions(IEnumerable<Colonist> colonists, Vector3 clickPos)
{
this.clickPos = clickPos;
allPawns = colonists.Select(colonist => colonist.pawn).ToList();
allActions = new List<MultiAction>();
colonists.Do(colonist =>
{
AddColonist(colonist);
totalColonistsInvolved++;
});
totalColonistsInvolved = colonists.Count();
colonists.Do(AddColonist);
if (gotoActionCount == totalColonistsInvolved)
if (colonists.Any(colonist => colonist.pawn.Drafted))
everyoneHasGoto = true;
}

public bool EveryoneHasGoto => everyoneHasGoto;

public void AddColonist(Colonist colonist)
{
var existingLabels = new HashSet<string>();
Expand All @@ -45,8 +51,11 @@ public void AddColonist(Colonist colonist)

public void AddMultiAction(Colonist colonist, bool draftMode, FloatMenuOption option)
{
var action = new MultiAction(colonist, draftMode, option);
if (Tools.IsGoHereOption(option) == false)
allActions.Add(new MultiAction(colonist, draftMode, option));
allActions.Add(action);
else
gotoActionCount++;
}

public int Count(bool onlyActive)
Expand Down

0 comments on commit 568c328

Please sign in to comment.