Class Extensions
A set of extensions for dealing with various things required by Tiny Life.
public static class Extensions
- Inheritance
-
Extensions
- Inherited Members
Fields
ObjectBindingFlags
The default bindings flags that are used when creating new instances of object types like Furniture, Action etc.
public const BindingFlags ObjectBindingFlags = Instance | Public | NonPublic | CreateInstance | OptionalParamBinding
Field Value
Methods
AddGamepadScrolling(Panel)
Adds the ability to use a gamepad to scroll to the given MLEM.Ui.Elements.Panel.
public static void AddGamepadScrolling(this Panel panel)
Parameters
panelPanelThe panel to add gamepad scrolling to.
AddHoverAnimation(Button)
Adds a hover animation to the given MLEM.Ui.Elements.Button. The hover animation will automatically start playing when the mouse enters the button.
public static Button AddHoverAnimation(this Button button)
Parameters
buttonButtonThe button to add the hover animation to
Returns
- Button
The button, for chaining
AddProgressArrows(Element, Func<float>, Func<float>, Func<float>, Func<Color>)
Adds a set of three progress arrows as an overlay to the passed element and displays a varying number of them based on additional parameters. This method is used for the skill, need and relationship bars.
public static (Group, Image[]) AddProgressArrows(this Element element, Func<float> getValue, Func<float> minDelta, Func<float> deltaPerArrow, Func<Color> color)
Parameters
elementElementThe element to add the progress arrows to.
getValueFunc<float>A function that returns a current value.
minDeltaFunc<float>A function that determines how much the value returned by
getValueneeds to change every update frame for an arrow to display.deltaPerArrowFunc<float>A function that determines how much the value returned by
getValueneeds to change every update frame to display additional arrows beyond the first one.colorFunc<Color>A function that returns the color the arrows should have.
Returns
- (Group, Image[])
AnyActive(CompletionType, Func<CompletionType>)
A helper method to return Active if one of the completion types is Active. If this is not the case, the first completion type that is different is returned.
public static CompletionType AnyActive(this CompletionType type, Func<CompletionType> other)
Parameters
typeCompletionTypeThe first completion type
otherFunc<CompletionType>A function to gather the second completion type
Returns
- CompletionType
Active if one of the completion types is Active, or the
other's completion type
AnyActive(CompletionType, CompletionType)
A helper method to return Active if one of the completion types is Active. If this is not the case, the first completion type that is different is returned.
public static CompletionType AnyActive(this CompletionType type, CompletionType other)
Parameters
typeCompletionTypeThe first completion type
otherCompletionTypeA function to gather the second completion type
Returns
- CompletionType
Active if one of the completion types is Active, or the
other's completion type
AnyInactive(CompletionType, Func<CompletionType>)
A helper method to return Active if both of the completion types are Active. If this is not the case, the first completion type that is different is returned.
public static CompletionType AnyInactive(this CompletionType type, Func<CompletionType> other)
Parameters
typeCompletionTypeThe first completion type
otherFunc<CompletionType>A function to gather the second completion type
Returns
- CompletionType
The first non-active completion type, or Active if there is none
AnyInactive(CompletionType, CompletionType)
A helper method to return Active if both of the completion types are Active. If this is not the case, the first completion type that is different is returned.
public static CompletionType AnyInactive(this CompletionType type, CompletionType other)
Parameters
typeCompletionTypeThe first completion type
otherCompletionTypeA function to gather the second completion type
Returns
- CompletionType
The first non-active completion type, or Active if there is none
AppendIfAny<T>(IEnumerable<T>, T)
A special version of ConcatIfAny<T>(IEnumerable<T>, IEnumerable<T>) that appends the given element to the end of the given enumerable if the enumerable already has at least one element.
public static IEnumerable<T> AppendIfAny<T>(this IEnumerable<T> enumerable, T toAppend) where T : Action
Parameters
enumerableIEnumerable<T>The enumerable.
toAppendTThe element to append.
Returns
- IEnumerable<T>
A concatenation of
enumerablefollowed bytoAppendifenumerablecontains at least one element, or an empty collection otherwise.
Type Parameters
TThe type of the enumerable and element to append to it.
BothValid(CanExecuteResult, Func<CanExecuteResult>)
A helper method to return Valid only if both passed CanExecuteResult values are Valid.
If this is not the case, the validity of the result is returned if it is not Valid, and otherwise, the validity of the other is returned.
public static CanExecuteResult BothValid(this CanExecuteResult result, Func<CanExecuteResult> other)
Parameters
resultCanExecuteResultThe first result
otherFunc<CanExecuteResult>The second result
Returns
- CanExecuteResult
The validity of both results
BothValid(CanExecuteResult, CanExecuteResult)
A helper method to return Valid only if both passed CanExecuteResult values are Valid.
If this is not the case, the validity of the result is returned if it is not Valid, and otherwise, the validity of the other is returned.
public static CanExecuteResult BothValid(this CanExecuteResult result, CanExecuteResult other)
Parameters
resultCanExecuteResultThe first result
otherCanExecuteResultThe second result
Returns
- CanExecuteResult
The validity of both results
ConcatIfAny<T>(IEnumerable<T>, IEnumerable<T>)
A helper method intended to be used by MultiAction (or MultiActionHandler in general) to concatenate the two given IEnumerable<T>s, but only if both of them have at least one element each. This is useful as Initialize(ICollection<Action>) expects an empty collection if the action should fail.
public static IEnumerable<T> ConcatIfAny<T>(this IEnumerable<T> first, IEnumerable<T> second) where T : Action
Parameters
firstIEnumerable<T>The first enumerable.
secondIEnumerable<T>The second enumerable.
Returns
- IEnumerable<T>
A concatenation of
firstfollowed bysecondif both contain at least one element, or an empty collection otherwise.
Type Parameters
TThe type of the enumerables.
DrawIsoArea(SpriteBatch, RectangleF, float, Color, int, Func<Vector2, float, float>, float?)
Draws an isometric rectangle with the given data using a pixelated tile texture. If the area has a MLEM.Maths.RectangleF.Width or MLEM.Maths.RectangleF.Height of 0, a line is drawn instead. If the area is empty (it has a MLEM.Maths.RectangleF.Width and MLEM.Maths.RectangleF.Height of 0), a small X is drawn instead.
public static void DrawIsoArea(this SpriteBatch batch, RectangleF area, float floor, Color color, int thickness, Func<Vector2, float, float> depth = null, float? defaultDepth = null)
Parameters
batchSpriteBatchThe sprite batch to draw with
areaRectangleFThe area, in world space, to draw a rectangle around
floorfloatThe floor the area is on.
colorColorThe color that the rectangle should have
thicknessintThe thickness of the line to draw, in pixels.
depthFunc<Vector2, float, float>An optional function that returns depth that each position should have.
defaultDepthfloat?The default depth to use if
depthis null.
DrawIsoArea(SpriteBatch, RectangleF, float, Color, int, float)
Draws an isometric rectangle with the given data using a pixelated tile texture. If the area has a MLEM.Maths.RectangleF.Width or MLEM.Maths.RectangleF.Height of 0, a line is drawn instead. If the area is empty (it has a MLEM.Maths.RectangleF.Width and MLEM.Maths.RectangleF.Height of 0), a small X is drawn instead.
public static void DrawIsoArea(this SpriteBatch batch, RectangleF area, float floor, Color color, int thickness, float depth)
Parameters
batchSpriteBatchThe sprite batch to draw with
areaRectangleFThe area, in world space, to draw a rectangle around
floorfloatThe floor the area is on.
colorColorThe color that the rectangle should have
thicknessintThe thickness of the line to draw, in pixels.
depthfloatThe depth that the area should have.
Get(DefaultSound)
Returns the loaded Microsoft.Xna.Framework.Audio.SoundEffect for this default sound.
public static SoundEffect Get(this DefaultSound sound)
Parameters
soundDefaultSoundThe default sound to get the instance of.
Returns
- SoundEffect
The loaded sound.
GetChanceModifier(Quality)
Returns a chance modifier for the given Quality, which is always greater than 0 and less than or equal to 1. This is useful for calculating the chance of something happening based on the quality of an object. The returned values are as follows: Terrible: 0.05, Good: 0.55, Great: 0.75, Perfect: 0.9, Masterpiece: 1, Else: 0.4.
public static float GetChanceModifier(this Quality quality)
Parameters
qualityQualityThe quality to query
Returns
- float
The chance modifier for this quality
GetDaysBeforeAging(AgeGroup, LifeSpan)
Returns the amount of days that have to pass until a person in the given AgeGroup with the given LifeSpan ages up (or dies of old age in the case of Elder).
public static int GetDaysBeforeAging(this AgeGroup group, LifeSpan lifeSpan)
Parameters
groupAgeGroupThe age group whose days before aging to retrieve.
lifeSpanLifeSpanThe life span whose multiplier to use.
Returns
- int
The amount of days before a person ages up.
Exceptions
- ArgumentOutOfRangeException
Thrown when the given
lifeSpanis not a valid non-combined flag.
GetDecorativeRating(Quality)
Returns the decorative rating of an object with the given Quality. The returned values are as follows: Terrible: -2, Good: 1, Great: 1, Perfect: 2, Masterpiece: 3, Else: 0.
public static int GetDecorativeRating(this Quality quality)
Parameters
qualityQualityThe quality to query
Returns
- int
The decorative rating for this quality
GetDefaultDirtyOverlayTextureName(ObjectCategory)
Returns the default DirtyOverlayTextureName to use for the given set of ObjectCategory flags. If none of the flags match a default overlay texture, this method will return null.
public static string GetDefaultDirtyOverlayTextureName(this ObjectCategory category)
Parameters
categoryObjectCategoryThe categories to get the default texture for
Returns
- string
The default texture
GetDefaultTab(ObjectCategory)
Returns the default FurnitureTool.Tab flags that the given ObjectCategory flag combination should be sorted into
public static FurnitureTool.Tab GetDefaultTab(this ObjectCategory categories)
Parameters
categoriesObjectCategoryThe categories that an object has
Returns
- FurnitureTool.Tab
The tabs most fitting for the object
GetDescription(ClothesIntention, AgeGroup?)
Returns a set of strings (split into multiple lines) that describes the given ClothesIntention flags in a localized fashion.
public static IEnumerable<string> GetDescription(this ClothesIntention intentions, AgeGroup? age = null)
Parameters
intentionsClothesIntentionageAgeGroup?
Returns
- IEnumerable<string>
This clothes item's intention description
GetMostFittingStyle(PregnancyAbility)
Returns the most fitting StylePreference for the given PregnancyAbility.
public static StylePreference GetMostFittingStyle(this PregnancyAbility ability)
Parameters
abilityPregnancyAbilityThe pregnancy ability.
Returns
- StylePreference
The most fitting style preference.
GetMostFittingStyle(VoiceStyle)
Returns the most fitting StylePreference for the given VoiceStyle.
public static StylePreference GetMostFittingStyle(this VoiceStyle voice)
Parameters
voiceVoiceStyleThe voice style.
Returns
- StylePreference
The most fitting style preference.
GetOpposite(GenealogyType)
Returns the opposite of the given GenealogyType. The "opposite" is the relationship that the other partner of this GenealogyType will have. For example, the opposite of Parent is Child and vice versa.
public static GenealogyType GetOpposite(this GenealogyType type)
Parameters
typeGenealogyTypeThe genealogy type
Returns
- GenealogyType
The genealogy type's opposite
GetPersonality(FoodRestriction, PersonLike)
Returns the PersonalityType that matches the given FoodType.FoodRestriction flag. If multiple values are set, the personality that is considered most important is returned.
If the person is set, a personality will only be returned if the person has it.
public static PersonalityType GetPersonality(this FoodType.FoodRestriction restriction, PersonLike person = null)
Parameters
restrictionFoodType.FoodRestrictionThe restriction whose personality to return.
personPersonLikeThe person who needs to have the personality for it to be returned. If this is null, the most important personality is always returned.
Returns
- PersonalityType
The matching personality for the given food restriction flag.
GetPregnancyHours(LifeSpan)
Returns the amount of hours that a person should be pregnant for based on the given lifeSpan.
public static int GetPregnancyHours(this LifeSpan lifeSpan)
Parameters
lifeSpanLifeSpanThe life span.
Returns
- int
The amount of hours that a pregnancy should last.
GetPriceModifier(Quality)
Returns a multiplier that an object's price should be multiplied with based on the given Quality. The returned values are as follows: Terrible: 0.5, Good: 1.5, Great: 2, Perfect: 3, Masterpiece: 5, Else: 1.
public static float GetPriceModifier(this Quality quality)
Parameters
qualityQualityThe quality to query
Returns
- float
The price modifier for the object
GetRequiredMatchPercentage(ClothesIntention)
Returns the percentage that an Outfit has to match the given ClothesIntention for it to be allowed to be used for an outfit. To find the current percentage of an Outfit, use GetIntentionPercentage(ClothesIntention).
public static float GetRequiredMatchPercentage(this ClothesIntention intention)
Parameters
intentionClothesIntentionThe intention to query.
Returns
- float
The percentage that the intention needs to match for it to be allowed.
GetSpeedMultiplier(GameSpeed)
public static float GetSpeedMultiplier(this GameSpeed speed)
Parameters
speedGameSpeed
Returns
GetTimeRange(FoodTime)
Returns the time range in hours of a day (between 0 and 24) for which the given FoodType.FoodTime is appropriate. Note that returned values may hvae a higher start than end time, which means the time passes midnight and "wraps around".
public static (int Start, int End) GetTimeRange(this FoodType.FoodTime time)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
When a non-defined
timeis passed.
GetTransitive(GenealogyType, GenealogyType)
Given the GenealogyType of two relationships, returns the transitive genealogy that results from the two given genealogies. The given genealogies are the ones in relationships between PersonLike A and B, and B and C, respectively. The returned GenealogyType is the genealogy between PersonLike A and C. If null is returned, the transitive genealogy cannot reliably be gathered from the given information.
public static GenealogyType? GetTransitive(this GenealogyType aToB, GenealogyType bToC)
Parameters
aToBGenealogyTypeThe genealogy between A and B.
bToCGenealogyTypeThe genealogy between B and C.
Returns
- GenealogyType?
The genealogy between A and C, or null if the genealogy cannot be reliably gathered.
Exceptions
- ArgumentOutOfRangeException
If any undefined GenealogyType is passed as a parameter.
Icon(MouseButton)
Returns a texture region that contains the icon that the given MLEM.Input.MouseButton should display.
public static TextureRegion Icon(this MouseButton button)
Parameters
buttonMouseButtonThe button whose icon to return.
Returns
- TextureRegion
The button's icon, or null if the button is invalid or has no icon.
Icon(Buttons)
Returns a texture region that contains the icon that the given Microsoft.Xna.Framework.Input.Buttons should display. This method is used by GamepadHint(Anchor, Func<Options, Buttons>, Vector2, Vector2?, Func<bool>).
public static TextureRegion Icon(this Buttons button)
Parameters
buttonButtonsThe button whose icon to return.
Returns
- TextureRegion
The button's icon, or null if the button is invalid or has no icon.
IsActiveRoot(Element)
Returns whether the given MLEM.Ui.Elements.Element is currently considered to be part of the active MLEM.Ui.RootElement.
public static bool IsActiveRoot(this Element element)
Parameters
elementElementThe element to query.
Returns
- bool
Whether the element is part of the active root.
IsAgeAppropriate(GenealogyType, AgeGroup, AgeGroup)
Returns whether the given GenealogyType is appropriate for a relationship between the given AgeGroup values.
public static bool IsAgeAppropriate(this GenealogyType type, AgeGroup myAge, AgeGroup otherAge)
Parameters
typeGenealogyTypeThe genealogy type in question
myAgeAgeGroupThe age group of the person that should have the
type.otherAgeAgeGroupThe age group of the other person.
Returns
- bool
Whether the given genealogy type is appropriate for the given age
IsAvailableForAge(ClothesIntention, AgeGroup)
Returns whether the given ClothesIntention is avilable for the given AgeGroup. This method only returns false for certain intentions for the baby age group.
public static bool IsAvailableForAge(this ClothesIntention intention, AgeGroup age)
Parameters
intentionClothesIntentionThe intention.
ageAgeGroupThe age group.
Returns
- bool
Whether the given
intentionis avilable for the givenage.
IsBetween((int Start, int End), int)
Returns whether the given time range is between the given time. If the time range ranges into the next day, the correct value is still calculated.
public static bool IsBetween(this (int Start, int End) schedule, int time)
Parameters
schedule(int Start, int End)The schedule with its start and end time. Can be between two days.
timeintThe current time.
Returns
- bool
Whether the
timeis within theschedule.
IsHorizontal(Direction2)
Returns whether the passed direction is considered horizontal. A horizontal direction is MLEM.Maths.Direction2.Left and MLEM.Maths.Direction2.Right. All other directions return false.
public static bool IsHorizontal(this Direction2 dir)
Parameters
dirDirection2The direction to check horizontalness for
Returns
- bool
Whether or not the direction is horizontal
IsLockedFor(LockType, PersonLike, Lot)
Returns whether the given LockType is considered "locked" for the given PersonLike on the given Lot
public static bool IsLockedFor(this LockType type, PersonLike person, Lot lot)
Parameters
typeLockTypeThe lock type to query
personPersonLikeThe person to query
lotLotThe lot that the object locked with the given
typeis on
Returns
- bool
Whether the lock type is considered locked for the person
IsPartOfPerson(ClothesLayer)
Returns whether the given ClothesLayer is part of a person physically. This methdo returns true for Body and Eyes.
public static bool IsPartOfPerson(this ClothesLayer layer)
Parameters
layerClothesLayerThe clothes layer
Returns
- bool
Whether the clothes layer is part of the person
IsTextFieldSelected(UiSystem)
An extension method that determines whether any text fields in the given MLEM.Ui.UiSystem are currently selected.
public static bool IsTextFieldSelected(this UiSystem system)
Parameters
systemUiSystemThe ui system whose children to check.
Returns
- bool
Whether a MLEM.Ui.Elements.TextField is currently selected.
IsVertical(Direction2)
Returns whether the passed direction is considered vertical. A vertical direction is MLEM.Maths.Direction2.Up and MLEM.Maths.Direction2.Down. All other directions return false.
public static bool IsVertical(this Direction2 dir)
Parameters
dirDirection2The direction to check verticalness for
Returns
- bool
Whether or not the direction is vertical
JsonCopy<T>(T)
Copies the given object using a Newtonsoft.Json.JsonSerializer created using CreateSerializerSettings(SerializerContext, SerializerUsage, EventHandler<ErrorEventArgs>, bool).
public static T JsonCopy<T>(this T obj)
Parameters
objTThe object to copy
Returns
- T
A copy of the object
Type Parameters
TThe type of the object
Length((int Start, int End))
Returns the length of the given schedule.
If the time range ranges into the next day, the correct value is still calculated.
public static int Length(this (int Start, int End) schedule)
Parameters
Returns
- int
The length of the given
schedule.
Multiplier(LifeSpan)
Returns the multiplier that the given LifeSpan applies to a person's life span.
public static float Multiplier(this LifeSpan lifeSpan)
Parameters
lifeSpanLifeSpanThe life span whose multiplier to retrieve.
Returns
- float
The life span's multiplier.
Exceptions
- ArgumentOutOfRangeException
Thrown when the given
lifeSpanis not defined, or Infinite.
PrependIfAny<T>(IEnumerable<T>, T)
A special version of ConcatIfAny<T>(IEnumerable<T>, IEnumerable<T>) that prepends the given element to the end of the given enumerable if the enumerable already has at least one element.
public static IEnumerable<T> PrependIfAny<T>(this IEnumerable<T> enumerable, T toPrepend) where T : Action
Parameters
enumerableIEnumerable<T>The enumerable.
toPrependTThe element to prepend.
Returns
- IEnumerable<T>
A concatenation of
toPrependfollowed byenumerableifenumerablecontains at least one element, or an empty collection otherwise.
Type Parameters
TThe type of the enumerable and element to prepended to it.
ScreenContent(Channel)
An extension method that returns a string representing the screen content for the given channel, which can be passed to TurnOn(string).
public static string ScreenContent(this Television.Channel channel)
Parameters
channelTelevision.ChannelThe channel whose screen content to return.
Returns
- string
The channel's screen content.
SetInWorldSnapping(Tooltip)
Causes the given tooltip to snap to the in-world cursor automatically.
public static void SetInWorldSnapping(this Tooltip tooltip)
Parameters
tooltipTooltipThe tooltip.
SetPauseGame(RootElement, bool)
Sets a flag for a MLEM.Ui.RootElement that causes it to pause the game if it is displayed
public static RootElement SetPauseGame(this RootElement root, bool value = true)
Parameters
rootRootElementThe root element that should pause the game
valueboolWhether the element should pause the game
Returns
- RootElement
The root element passed, for chaining
Slugify(string)
"Slugifies" the given name, effectively removing all special characters and diacritics and turning it into a string that the game can use for file names and JSON entry keys. This method is used for the name of people, households and lots.
public static string Slugify(this string name)
Parameters
namestringThe name to slugify.
Returns
- string
The slugified name.
ToLocalizedString(Quality)
Returns a localized string for the given Quality.
public static string ToLocalizedString(this Quality quality)
Parameters
qualityQualityThe quality
Returns
- string
A localized string representing the quality
ToScreenPos(Vector2, float, bool, bool, bool)
Converts the given world-space position into a draw-space position
public static Vector2 ToScreenPos(this Vector2 position, float floor, bool center = false, bool applyCameraRotation = true, bool borderOffset = false)
Parameters
positionVector2The position to convert
floorfloatThe floor that the position is on.
centerboolIf this is true, the position will be offset by half a tile, centering it on the screen
applyCameraRotationboolWhether the game's current CameraRotation should be applied using ApplyCameraRotation(Vector2, bool, Direction2?)
borderOffsetboolWhether the map border offset should be applied to the camera rotation using ApplyCameraRotation(Vector2, bool, Direction2?). This value only has an effect if
applyCameraRotationis true
Returns
- Vector2
The draw-space position
ToSellingPriceString(float)
Returns a localized string that says "Sells for", followed by the passed value in tiny bucks
public static string ToSellingPriceString(this float value)
Parameters
valuefloatThe value to display
Returns
- string
A selling price string that contains the value, in tiny bucks
ToSimpleLocalizedString(TimeSpan)
Returns a string, localized using Localization, that represents the largest value of the time span. If the time span includes multiple days, a string representing just days is returned. Otherwise, the full hours are returned, and otherwise, the full minutes are returned.
public static string ToSimpleLocalizedString(this TimeSpan time)
Parameters
timeTimeSpanThe time to localize
Returns
- string
A simple localized string for the time span
ToTimestampString(TimeSpan)
Turns the given timeSpan into a nicely displayed timestamp in 12- or 24-hour format, based on the currently selected Localization language.
public static string ToTimestampString(this TimeSpan timeSpan)
Parameters
timeSpanTimeSpanThe time span to display.
Returns
- string
The displayed timestamp.
ToVisualY(Vector2)
Returns the given offset, but translated based on the game's current CameraRotation. For any vector that is passed to this method, the translation that it represents will not be based on cardinal directions, but on directions related to the screen's orientation.
public static Vector2 ToVisualY(this Vector2 pos)
Parameters
posVector2The position to turn into a visual y position
Returns
- Vector2
The given position translated into virtual y space
ToWorking(Pose)
Converts the given Pose to a version in which a person is looking as if they were working on something. For all standing poses, this returns WorkingStanding, and for all sitting poses, WorkingSitting is returned. If a pose has no working variant, the pose itself is returned.
public static Pose ToWorking(this Pose pose)
Parameters
posePoseThe pose to convert to a working pose
Returns
- Pose
The working version of the given pose
ToWorldPos(Vector2, float, bool)
Converts the given draw-space position into a world-space position
public static Vector2 ToWorldPos(this Vector2 position, float floor, bool applyCameraRotation = true)
Parameters
positionVector2The position to convert
floorfloatThe floor that the position is on.
applyCameraRotationboolWhether the game's current CameraRotation should be applied using ApplyInverseCameraRotation(Vector2, Direction2?)
Returns
- Vector2
The world-space position
Weekday(TimeSpan)
Returns the DayOfWeek that the given in-game time represents.
public static DayOfWeek Weekday(this TimeSpan time)
Parameters
timeTimeSpanThe in-game time to query.
Returns
- DayOfWeek
The weekday.