Stardew Valley

This guide requires you to be the creator of the mod you want to add. I can't figure out a way to access other people's mods yet.

Adding an icon to the mobile phone doesn't interfere with ordinary workings of a mod, it just adds a new way of triggering it.

To add a trigger for your mod to the mobile phone, here are the basic steps:

First, create an app icon graphics file (48x48 pixels is default, better stick with that for now).

Second, create an Interface class in your mod like this:

public interface IMobilePhoneApi
{
bool AddApp(string id, string name, Action action, Texture2D icon);
Vector2 GetScreenPosition();
Vector2 GetScreenSize();
Vector2 GetScreenSize(bool rotated);
Rectangle GetPhoneRectangle();
Rectangle GetScreenRectangle();
bool GetPhoneRotated();
void SetPhoneRotated(bool value);
bool GetPhoneOpened();
void SetPhoneOpened(bool value);
bool GetAppRunning();
void SetAppRunning(bool value);
string GetRunningApp();
void SetRunningApp(string value);
void PlayRingTone();
void PlayNotificationTone();
NPC GetCallingNPC();
bool IsCallingNPC();
}


Third, access the interface in a GameLaunched method:

  private void GameLoop_GameLaunched(object sender, StardewModdingAPI.Events.GameLaunchedEventArgs e)
  {
api = Helper.ModRegistry.GetApi<IMobilePhoneApi>("aedenthorn.MobilePhone");
if (api != null)
{
Texture2D appIcon = Helper.Content.Load<Texture2D>(Path.Combine("assets", "app_icon.png"));
bool success = api.AddApp(Helper.ModRegistry.ModID, "Random Quote", ShowRandomQuote, appIcon);
Monitor.Log($"loaded phone app successfully: {success}", LogLevel.Debug);
}
  }



Replace "Random Quote" with your mod's name (at the moment this doesn't actually do anything useful). Replace ShowRandomQuote with the name of your mod's method that you want to trigger (method must have no arguments).

Fourth, add the above method to the GameLaunched event in your mod's Entry method:

  Helper.Events.GameLoop.GameLaunched += GameLoop_GameLaunched;


That's it! Let me know if it doesn't work, or if it does and you're as surprised as I was :)


For mods that want to run on the phone screen, keep the following in mind:

Your mod should take care to let the phone know that an app is running by calling the interface's SetAppRunning(true), and when it finishes with SetAppRunning(false). This will stop the home screen icons from triggering.

You should also SetRunningApp(Helper.ModRegistry.ModID) if you need to check whether your app is still running.

Before drawing or receiving clicks, you should check IsCallingNPC() and cancel if true, as that should draw over every app while it's happening.

When drawing on the phone, use the interface methods to find the screen position and size. The phone uses Display.RenderedWorld, so anything above that will show over the phone.

Also, remember that the phone can be rotated, meaning you'll have to account for that if you want to make it look like the app is running on the screen. Something like checking for if the phone size has changed or just retrieving the screen size from the api every time you draw to the screen.

Article information

Added on

Edited on

Written by

aedenthorn

1 comment

  1. legovader09
    legovader09
    • member
    • 2 kudos
    Great documentation! Just like to note that the Action method can in fact have arguments, at least when passed down as an anonymous function. 

    For example:
    () => MethodName("Random Text", true)
    Will still work, at least in my case, and I suppose depends on what said method actually does. In my instance it's a matter of displaying a dialog box.