public NewMapLogic(Action onExit, Action<string> onSelect, Ruleset modRules, Widget widget, World world)
{
panel = widget;
panel.Get<ButtonWidget>("CANCEL_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
var tilesetDropDown = panel.Get<DropDownButtonWidget>("TILESET");
var tilesets = modRules.TileSets.Select(t => t.Key).ToList();
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{
var item = ScrollItemWidget.Setup(template,
() => tilesetDropDown.Text == option,
() => { tilesetDropDown.Text = option; });
item.Get<LabelWidget>("LABEL").GetText = () => option;
return item;
};
tilesetDropDown.Text = tilesets.First();
tilesetDropDown.OnClick = () =>
tilesetDropDown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, tilesets, setupItem);
var widthTextField = panel.Get<TextFieldWidget>("WIDTH");
var heightTextField = panel.Get<TextFieldWidget>("HEIGHT");
panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = () =>
{
int width, height;
int.TryParse(widthTextField.Text, out width);
int.TryParse(heightTextField.Text, out height);
// Require at least a 2x2 playable area so that the
// ground is visible through the edge shroud
width = Math.Max(2, width);
height = Math.Max(2, height);
var maxTerrainHeight = Game.ModData.Manifest.MaximumTerrainHeight;
var tileset = modRules.TileSets[tilesetDropDown.Text];
var map = new Map(tileset, width + 2, height + maxTerrainHeight + 2);
var tl = new MPos(1, 1);
var br = new MPos(width, height + maxTerrainHeight);
map.SetBounds(tl, br);
map.PlayerDefinitions = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length).ToMiniYaml();
map.FixOpenAreas(modRules);
Action<string> afterSave = uid =>
{
// HACK: Work around a synced-code change check.
// It's not clear why this is needed here, but not in the other places that load maps.
Game.RunAfterTick(() =>
{
ConnectionLogic.Connect(System.Net.IPAddress.Loopback.ToString(),
Game.CreateLocalServer(uid), "",
() => Game.LoadEditor(uid),
() => { Game.CloseServer(); onExit(); });
});
Ui.CloseWindow();
onSelect(uid);
};
Ui.OpenWindow("SAVE_MAP_PANEL", new WidgetArgs()
{
{ "onSave", afterSave },
{ "onExit", () => { Ui.CloseWindow(); onExit(); } },
{ "map", map },
{ "playerDefinitions", map.PlayerDefinitions },
{ "actorDefinitions", map.ActorDefinitions }
});
};
}