#RequireContext CMapEditorPlugin
#Include "TextLib" as TL
// Script Version 2022-12-27
Text ChangePlatformType(Text model, Text type) {
declare Text newModel = model;
declare Text newType = "Platform"^type;
if (type == "Wall") newType = "DecoWall";
if (TL::StartsWith("Open", newModel)) newType = "Open"^type;
newModel = TL::RegexReplace("^((Platform|Open)(Tech|Dirt|Grass|Ice|Plastic))|DecoWall", newModel, "G", newType);
return newModel;
}
Text ChangeDecoType(Text model, Text type) {
declare Text newModel = model;
declare Text newType = type;
if (type == "Grass") newType = "";
if (TL::StartsWith("DecoPlatform", newModel)) newType = "DecoPlatform"^newType;
if (TL::StartsWith("DecoHill", newModel)) newType = "DecoHill"^newType;
log(newType);
newModel = TL::RegexReplace("^Deco(Platform|Hill)(Dirt|Ice){0,1}", newModel, "G", newType);
return newModel;
}
Text ChangeRoadType(Text model, Text type) {
declare Text newModel = model;
declare Text newType = "Road"^type;
if (type == "Wall") newType = "TrackWall";
newModel = TL::RegexReplace("^Road(Tech|Ice|Dirt|Bump|Water)|TrackWall", newModel, "G", newType);
log(newModel);
return newModel;
}
Text ForceLR(Text model, Text type) {
return TL::RegexReplace("(Left|Right)", model, "", type);
}
Text ForceUD(Text model, Text type) {
return TL::RegexReplace("(Up|Down)", model, "", type);
}
CBlockModel MirrorV(Text inModel) {
declare Text model = inModel;
declare Text[] UpMatch = TL::RegexMatch("Up", model, "");
declare Text[] DownMatch = TL::RegexMatch("Down", model, "");
if (UpMatch.count > 0) {
model = TL::Replace(model,"Up", "Down");
return GetBlockModelFromName(model);
}
if (DownMatch.count > 0) {
model = TL::Replace(model, "Down", "Up");
return GetBlockModelFromName(model);
}
return CBlockModel;
}
CBlockModel MirrorH(Text inModel) {
declare Text model = inModel;
declare Text[] LeftMatch = TL::RegexMatch("Left", model, "");
declare Text[] RightMatch = TL::RegexMatch("Right", model, "");
if (LeftMatch.count > 0) {
model = TL::Replace(model,"Left", "Right");
return GetBlockModelFromName(model);
}
if (RightMatch.count > 0) {
model = TL::Replace(model, "Right", "Left");
return GetBlockModelFromName(model);
}
return CBlockModel;
}
CBlockModel CastType(Text type, Text inModel) {
declare Text model = inModel;
declare Text[] matchDeco = TL::RegexMatch("^Deco(Platform|Hill)", Cursor.BlockModel.Name, "");
if (matchDeco.count > 0) {
declare Text newType = TL::RegexReplace("^d", type, "", "");
model = ChangeDecoType(model, newType);
return GetBlockModelFromName(model);
}
if (TL::StartsWith("s", type)) {
declare Text newType = TL::RegexReplace("^s", type, "", "");
model = ChangePlatformType(model, newType);
return GetBlockModelFromName(model);
}
if (TL::StartsWith("r", type)) {
declare Text newType = TL::RegexReplace("^r", type, "", "");
model = ChangeRoadType(model, newType);
return GetBlockModelFromName(model);
}
if (type == "MirrorH") {
return MirrorH(model);
}
if (type == "MirrorV") {
return MirrorV(model);
}
/*
declare Text[] UD = ["Up", "Down"];
declare Text[] LR = ["Left", "Right"];
if (UD.exists(type)) {
model = ForceUD(model, type);
return GetBlockModelFromName(model);
}
if (LR.exists(type)) {
model = ForceLR(model, type);
return GetBlockModelFromName(model);
}
*/
return CBlockModel;
}
Void Show(Text frame) {
ManialinkPage.GetFirstChild("surface").Hide();
ManialinkPage.GetFirstChild("deco").Hide();
ManialinkPage.GetFirstChild("road").Hide();
ManialinkPage.GetFirstChild("all").Hide();
// Show the frame
ManialinkPage.GetFirstChild(frame).Show();
}
main() {
declare CMapEditorPlugin::PlaceMode oldPlaceMode;
declare Boolean process = True;
LayersDefaultManialinkVersion = 3;
ManialinkText = """
""";
while(True) {
yield;
if (oldPlaceMode != PlaceMode) {
oldPlaceMode = PlaceMode;
if (PlaceMode == CMapEditorPlugin::PlaceMode::CopyPaste) {
ManialinkPage.GetFirstChild("surface").Hide();
ManialinkPage.GetFirstChild("deco").Hide();
ManialinkPage.GetFirstChild("road").Hide();
//@todo set to show when we can get copypaste selection
ManialinkPage.GetFirstChild("all").Hide();
ShowCustomSelection();
process = False;
} else {
HideCustomSelection();
process = True;
}
}
foreach (Event in PendingEvents) {
if (Event.Type == CMapEditorPluginEvent::Type::KeyPress) {
if (Event.KeyName == "Q") {
if (Cursor.BlockModel == Null) continue;
Cursor.BlockModel = MirrorH(Cursor.BlockModel.Name);
}
if (Event.KeyName == "W") {
if (Cursor.BlockModel == Null) continue;
Cursor.BlockModel = MirrorV(Cursor.BlockModel.Name);
}
}
if (Event.Type == CMapEditorPluginEvent::Type::LayerCustomEvent) {
if (Event.CustomEventType == "ChangeType") {
if (TL::StartsWith("a", Event.CustomEventData[0])) {
foreach (Coord in CustomSelectionCoords) {
declare CBlock _Block = GetBlock(Coord);
if (_Block.BlockModel == Null) continue;
declare BlockModel = CastType(Event.CustomEventData[0], _Block.BlockModel.Name);
PlaceBlock(BlockModel, Coord, _Block.Dir);
}
} else {
if (Cursor.BlockModel == Null) continue;
Cursor.BlockModel = CastType(Event.CustomEventData[0], Cursor.BlockModel.Name);
}
}
}
if (Event.Type == CMapEditorPluginEvent::Type::CursorChange) {
if (!process) continue;
ManialinkPage.GetFirstChild("all").Hide();
if (Cursor.BlockModel != Null) {
declare Text[] matchRoad = TL::RegexMatch("^Road|TrackWall" , Cursor.BlockModel.Name, "");
declare Text[] matchPlatform = TL::RegexMatch("^Platform|DecoWall", Cursor.BlockModel.Name, "");
declare Text[] matchDeco = TL::RegexMatch("^Deco(Platform|Hill)", Cursor.BlockModel.Name, "");
if (matchRoad.count > 0) {
Show("road");
}
if (matchPlatform.count > 0) {
Show("surface");
}
if (matchDeco.count > 0) {
Show("deco");
}
(ManialinkPage.GetFirstChild("cursor") as CMlLabel).Value = Cursor.BlockModel.Name;
}
}
}
}
}