Skip to content

Commit

Permalink
Add Play button to map editor
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Aug 29, 2022
1 parent d742de9 commit 7f0793a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
113 changes: 112 additions & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class IngameMenuLogic : ChromeLogic
bool leaving;
bool hideMenu;

public static bool LastGameEditor = false;

[ObjectCreator.UseCtor]
public IngameMenuLogic(Widget widget, ModData modData, World world, Action onExit, WorldRenderer worldRenderer,
IngameInfoPanel initialPanel, Dictionary<string, MiniYaml> logicArgs)
Expand All @@ -49,6 +51,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 @@ -57,6 +60,7 @@ public class IngameMenuLogic : ChromeLogic
{ "SETTINGS", CreateSettingsButton },
{ "RESUME", CreateResumeButton },
{ "SAVE_MAP", CreateSaveMapButton },
{ "PLAY_MAP", CreatePlayMapButton },
{ "EXIT_EDITOR", CreateExitEditorButton }
};

Expand Down Expand Up @@ -141,8 +145,13 @@ void OnQuit()

Game.RunAfterDelay(exitDelay, () =>
{
if (!Game.IsCurrentWorld(world))
if (!Game.IsCurrentWorld(world) && !LastGameEditor)
{
LastGameEditor = false;
return;
}
LastGameEditor = false;
Game.Disconnect();
Ui.ResetAll();
Expand Down Expand Up @@ -378,6 +387,108 @@ void CreateSaveMapButton()
};
}

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

var actionManager = world.WorldActor.Trait<EditorActionManager>();

Action<string, MapVisibility> openPlayMenu = (uid, visibility) =>
{
if (visibility == MapVisibility.Lobby)
{
ConnectionLogic.Connect(Game.CreateLocalServer(uid),
"",
() => Game.OpenWindow("SERVER_LOBBY", new WidgetArgs
{
{ "onExit", () => { ShowMenu(); } },
{ "onStart", () => { } },
{ "skirmishMode", true }
}),
() => { Game.CloseServer(); });
}
else if (visibility == MapVisibility.MissionSelector)
{
Game.OpenWindow("MISSIONBROWSER_PANEL", new WidgetArgs
{
{ "onExit", () => { ShowMenu(); } },
{ "onStart", () => { } },
{ "initialMap", uid }
});
}
};

var button = AddButton("PLAY_MAP", "Play Map");
button.OnClick = () =>
{
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))
{
hideMenu = true;
ConfirmationDialogs.ButtonPrompt(
title: "Warning",
text: "The map may have been deleted or has\n errors preventing it from being loaded",
onConfirm: () => { LastGameEditor = true; openPlayMenu(uid, map.Visibility); },
onCancel: ShowMenu,
confirmText: "Okay",
cancelText: "Okay");
return;
}
hideMenu = true;
if (actionManager.HasUnsavedItems())
{
hideMenu = true;
ConfirmationDialogs.ButtonPrompt(
title: "Exit Map Editor",
text: "Exit and lose all unsaved changes?",
onConfirm: () => { LastGameEditor = true; openPlayMenu(uid, map.Visibility); },
onCancel: ShowMenu);
}
else
{
LastGameEditor = true;
openPlayMenu(uid, map.Visibility);
}
};
}

void CreateBackToEditorButton()
{
if (world.Type != WorldType.Regular || !LastGameEditor)
return;

var button = AddButton("BACK_TO_EDITOR", "Back To Editor");

button.OnClick = () =>
{
hideMenu = true;
Action 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(
title: "Leave Mission",
text: "Leave this game and return to the editor?",
onConfirm: onConfirm,
onCancel: ShowMenu,
confirmText: "Back To Editor",
cancelText: "Stay");
};
}

void CreateExitEditorButton()
{
if (world.Type != WorldType.Editor)
Expand Down
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
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

0 comments on commit 7f0793a

Please sign in to comment.