Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Play Map button to map editor #20120

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
158 changes: 138 additions & 20 deletions OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ public class IngameMenuLogic : ChromeLogic
[TranslationReference]
const string ExitMapEditorConfirm = "dialog-exit-map-editor.confirm";

[TranslationReference]
const string PlayMapWarningTitle = "dialog-play-map-warning.title";

[TranslationReference]
const string PlayMapWarningPrompt = "dialog-play-map-warning.prompt";

[TranslationReference]
const string PlayMapWarningCancel = "dialog-play-map-warning.cancel";

[TranslationReference]
const string ExitToMapEditorTitle = "dialog-exit-to-map-editor.title";

[TranslationReference]
const string ExitToMapEditorPrompt = "dialog-exit-to-map-editor.prompt";

[TranslationReference]
const string ExitToMapEditorConfirm = "dialog-exit-to-map-editor.confirm";

[TranslationReference]
const string ExitToMapEditorCancel = "dialog-exit-to-map-editor.cancel";

readonly Widget menu;
readonly Widget buttonContainer;
readonly ButtonWidget buttonTemplate;
Expand All @@ -133,6 +154,8 @@ public class IngameMenuLogic : ChromeLogic
bool leaving;
bool hideMenu;

static bool lastGameEditor = false;
Mailaender marked this conversation as resolved.
Show resolved Hide resolved

[ObjectCreator.UseCtor]
public IngameMenuLogic(Widget widget, ModData modData, World world, Action onExit, WorldRenderer worldRenderer,
IngameInfoPanel initialPanel, Dictionary<string, MiniYaml> logicArgs)
Expand All @@ -145,6 +168,7 @@ public class IngameMenuLogic : ChromeLogic
var buttonHandlers = new Dictionary<string, Action>
{
{ "ABORT_MISSION", CreateAbortMissionButton },
{ "BACK_TO_EDITOR", CreateBackToEditorButton },
{ "RESTART", CreateRestartButton },
{ "SURRENDER", CreateSurrenderButton },
{ "LOAD_GAME", CreateLoadGameButton },
Expand All @@ -153,6 +177,7 @@ public class IngameMenuLogic : ChromeLogic
{ "SETTINGS", CreateSettingsButton },
{ "RESUME", CreateResumeButton },
{ "SAVE_MAP", CreateSaveMapButton },
{ "PLAY_MAP", CreatePlayMapButton },
{ "EXIT_EDITOR", CreateExitEditorButton }
};

Expand Down Expand Up @@ -234,6 +259,7 @@ public static void OnQuit(World world)
exitDelay += 40 * mpe.Info.FadeLength;
}

lastGameEditor = false;
Game.RunAfterDelay(exitDelay, () =>
{
if (!Game.IsCurrentWorld(world))
Expand Down Expand Up @@ -478,35 +504,127 @@ void CreateSaveMapButton()
};
}

void CreateExitEditorButton()
void CreatePlayMapButton()
{
if (world.Type != WorldType.Editor)
return;

var actionManager = world.WorldActor.Trait<EditorActionManager>();
var button = AddButton("EXIT_EDITOR", ExitMapButton);
AddButton("PLAY_MAP", "Play Map")
.OnClick = () =>
{
hideMenu = true;
var uid = modData.MapCache.GetUpdatedMap(world.Map.Uid);
var map = uid == null ? null : modData.MapCache[uid];
if (map == null || (map.Visibility != MapVisibility.Lobby && map.Visibility != MapVisibility.MissionSelector))
{
ConfirmationDialogs.ButtonPrompt(modData,
title: PlayMapWarningTitle,
text: PlayMapWarningPrompt,
onCancel: ShowMenu,
cancelText: PlayMapWarningCancel);

return;
}

ExitEditor(actionManager, () =>
{
lastGameEditor = true;

Ui.CloseWindow();
Ui.ResetTooltips();
void CloseMenu()
{
mpe?.Fade(MenuPaletteEffect.EffectType.None);
onExit();
}

if (map.Visibility == MapVisibility.Lobby)
{
ConnectionLogic.Connect(Game.CreateLocalServer(uid),
"",
() => Game.OpenWindow("SERVER_LOBBY", new WidgetArgs
{
{ "onExit", CloseMenu },
{ "onStart", () => { } },
{ "skirmishMode", true }
}),
() => Game.CloseServer());
}
else if (map.Visibility == MapVisibility.MissionSelector)
{
Game.OpenWindow("MISSIONBROWSER_PANEL", new WidgetArgs
{
{ "onExit", CloseMenu },
{ "onStart", () => { } },
{ "initialMap", uid }
});
}
});
};
}

// Show dialog only if updated since last save
button.OnClick = () =>
{
var map = modData.MapCache.GetUpdatedMap(world.Map.Uid);
var deletedOrUnavailable = map == null || modData.MapCache[map].Status != MapStatus.Available;
if (actionManager.HasUnsavedItems() || deletedOrUnavailable)
void CreateBackToEditorButton()
{
if (world.Type != WorldType.Regular || !lastGameEditor)
return;

AddButton("BACK_TO_EDITOR", "Back To Editor")
.OnClick = () =>
{
hideMenu = true;
void OnConfirm()
{
lastGameEditor = false;
var map = modData.MapCache.GetUpdatedMap(world.Map.Uid);
if (map == null)
Game.LoadShellMap();
else
{
DiscordService.UpdateStatus(DiscordState.InMapEditor);
Game.LoadEditor(map);
}
}

ConfirmationDialogs.ButtonPrompt(modData,
title: ExitMapEditorTitle,
text: deletedOrUnavailable ? ExitMapEditorPromptDeleted : ExitMapEditorPromptUnsaved,
confirmText: deletedOrUnavailable ? ExitMapEditorAnywayConfirm : ExitMapEditorConfirm,
onConfirm: () => { OnQuit(world); leaving = true; },
onCancel: ShowMenu);
}
else
{
OnQuit(world);
leaving = true;
}
};
title: ExitToMapEditorTitle,
text: ExitToMapEditorPrompt,
onConfirm: OnConfirm,
onCancel: ShowMenu,
confirmText: ExitToMapEditorConfirm,
cancelText: ExitToMapEditorCancel);
};
}

void CreateExitEditorButton()
{
if (world.Type != WorldType.Editor)
return;

var actionManager = world.WorldActor.Trait<EditorActionManager>();
AddButton("EXIT_EDITOR", ExitMapButton)
.OnClick = () => ExitEditor(actionManager, () => OnQuit(world));
}

void ExitEditor(EditorActionManager actionManager, Action onSuccess)
{
var map = modData.MapCache.GetUpdatedMap(world.Map.Uid);
var deletedOrUnavailable = map == null || modData.MapCache[map].Status != MapStatus.Available;
if (actionManager.HasUnsavedItems() || deletedOrUnavailable)
{
hideMenu = true;
ConfirmationDialogs.ButtonPrompt(modData,
title: ExitMapEditorTitle,
text: deletedOrUnavailable ? ExitMapEditorPromptDeleted : ExitMapEditorPromptUnsaved,
confirmText: deletedOrUnavailable ? ExitMapEditorAnywayConfirm : ExitMapEditorConfirm,
onConfirm: () => { onSuccess(); leaving = true; },
onCancel: ShowMenu);
}
else
{
onSuccess();
leaving = true;
}
}
}
}
2 changes: 1 addition & 1 deletion mods/cnc/chrome/ingame-menu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Container@INGAME_MENU:
Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM
Logic: IngameMenuLogic
Buttons: EXIT_EDITOR, SAVE_MAP, ABORT_MISSION, SURRENDER, RESTART, LOAD_GAME, SAVE_GAME, MUSIC, SETTINGS, RESUME
Buttons: EXIT_EDITOR, PLAY_MAP, SAVE_MAP, ABORT_MISSION, BACK_TO_EDITOR, SURRENDER, RESTART, LOAD_GAME, SAVE_GAME, MUSIC, SETTINGS, RESUME
ButtonStride: 130, 0
Children:
Image@EVA:
Expand Down
2 changes: 1 addition & 1 deletion mods/common/chrome/ingame-menu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Container@INGAME_MENU:
Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM
Logic: IngameMenuLogic
Buttons: RESUME, LOAD_GAME, SAVE_GAME, SETTINGS, MUSIC, SURRENDER, RESTART, ABORT_MISSION, SAVE_MAP, EXIT_EDITOR
Buttons: RESUME, LOAD_GAME, SAVE_GAME, SETTINGS, MUSIC, SURRENDER, RESTART, BACK_TO_EDITOR, ABORT_MISSION, SAVE_MAP, PLAY_MAP, EXIT_EDITOR
ButtonStride: 0, 40
Children:
Background@BORDER:
Expand Down
12 changes: 12 additions & 0 deletions mods/common/languages/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ dialog-exit-map-editor =
.confirm-anyway = Exit anyway
.confirm = Exit

dialog-play-map-warning =
.title = Warning
.prompt = The map may have been deleted or has
errors preventing it from being loaded.
.cancel = Okay

dialog-exit-to-map-editor =
.title = Leave Mission
.prompt = Leave this game and return to the editor?
.confirm = Back To Editor
.cancel = Stay

## IngamePowerBarLogic
## IngamePowerCounterLogic
label-power-usage = Power Usage: { $usage }/{ $capacity }
Expand Down
2 changes: 1 addition & 1 deletion mods/d2k/chrome/ingame-menu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Container@INGAME_MENU:
Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM
Logic: IngameMenuLogic
Buttons: RESUME, LOAD_GAME, SAVE_GAME, SETTINGS, MUSIC, SURRENDER, RESTART, ABORT_MISSION, SAVE_MAP, EXIT_EDITOR
Buttons: RESUME, LOAD_GAME, SAVE_GAME, SETTINGS, MUSIC, SURRENDER, RESTART, BACK_TO_EDITOR, ABORT_MISSION, SAVE_MAP, PLAY_MAP, EXIT_EDITOR
ButtonStride: 0, 40
Children:
Label@VERSION_LABEL:
Expand Down