public static string Serialize(BaseStateGroup data) { var namesStr = data.baseNames.ToList(); var statesStr = data.baseStates.Select(bs => Serialize(bs)).ToList(); var all = new List<List<string>>() { namesStr, statesStr }; return MiniJSON.Json.Serialize(all); }
public static void Test_SerializeBaseStateGroup() { var orig = new BaseStateGroup() { baseNames = new string[] { "BaseA", "BaseB" }, baseStates = new BaseState[] { new BaseState() { blockStates = new BlockState[] { new BlockState(){ blockType= BlockType.Hexagon, x=1.5f, y=1.5f, z=1.5f, qx=2.5f, qy=2.5f, qz=2.5f, qw=2.5f}, new BlockState(){ blockType= BlockType.Square, x=1.5f, y=1.5f, z=1.5f, qx=2.5f, qy=2.5f, qz=2.5f, qw=2.5f}, } }, new BaseState() { blockStates = new BlockState[] { new BlockState(){ blockType= BlockType.Rhombus, x=1.5f, y=1.5f, z=1.5f, qx=2.5f, qy=2.5f, qz=2.5f, qw=2.5f}, new BlockState(){ blockType= BlockType.Trapezoid, x=1.5f, y=1.5f, z=1.5f, qx=2.5f, qy=2.5f, qz=2.5f, qw=2.5f}, } } } }; var text = Serialize(orig); var final = DeSerializeBaseStateGroup(text); // Verify for (int j = 0; j < orig.baseStates.Length; j++) { if (orig.baseNames[j] != final.baseNames[j]) { throw new Exception("FAIL"); } for (int i = 0; i < orig.baseStates[j].blockStates.Length; i++) { if (orig.baseStates[j].blockStates[i].blockType != final.baseStates[j].blockStates[i].blockType || orig.baseStates[j].blockStates[i].qx != final.baseStates[j].blockStates[i].qx) { throw new Exception("FAIL"); } } } }
public BaseStateGroup LoadUserBaseGroup() { var names = GetBaseStateNames(); var baseStates = new List<BaseState>(); foreach (var baseName in names) { baseStates.Add(LoadBaseState(baseName)); } var group = new BaseStateGroup(); group.baseNames = names; group.baseStates = baseStates.ToArray(); return group; }
void OnGUI() { GUI.skin = mainSkin; // TODO: Create a single OnGUI caller class that will make all OnGUI calls if (editorState == EditorState.Starting) { DisplayBaseEditor(baseObject); editorState = EditorState.Active; } if (editorState == EditorState.Active) { // Draw the GUI var border = 10; var buttonHeight = 20; var buttonWidth = 70; var smallButtonSize = 30; var fontWidth = 8; var fontHeight = 16; var fontPadding = 8; GUI.skin.button.fontSize = fontHeight; GUI.skin.label.fontSize = fontHeight; var sw = Screen.width; var sh = Screen.height; var blockButtonSpaceSize = Mathf.Min(80, 1.0f * sh / blockImages.Length); // Draw the Block Buttons if (showBlockButtonsGUI) { int? bIndex = null; int i = 0; foreach (var bImage in blockImages) { var rect = new Rect( sw - blockButtonSpaceSize - border, (i * blockButtonSpaceSize) + border, blockButtonSpaceSize - border, blockButtonSpaceSize - border); if (GUI.RepeatButton(rect, bImage)) { bIndex = i; buttonClickedTime = Time.time; if (handeledBlockToBuildIndex == null) { blockToBuildIndex = i; Debug.Log("Button clicked" + Time.time); } } i++; } if (bIndex == null) { if (Time.time - buttonClickedTime > 0.5f) { handeledBlockToBuildIndex = null; //Debug.Log("No Button clicked" + Time.time); } } } // Draw Height Controls float areaWidth = (border * 4.0f) + (buttonWidth * 1.0f); float areaHeight = (border * 2.0f) + (smallButtonSize * 1.0f) + (fontHeight * 1.0f) + (fontPadding * 2.0f); float areaRight = sw - 2 * border - blockButtonSpaceSize; float areaTop = border; float areaLeft = areaRight - areaWidth; float areaBottom = areaTop + areaHeight; //if (backStyle == null) //{ // backStyle = new GUIStyle(); // var state = new GUIStyleState(); // state.background = (Texture2D)guiBlockBackground.texture; // backStyle.normal = state; //} GUI.Box(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "");//, backStyle); //var screenRelPos = new Vector3(areaLeft / sw, areaTop / sh, 0); //var background = (GUITexture)Instantiate(guiBlockBackground, screenRelPos, guiBlockBackground.transform.rotation); //SetLayer(background.gameObject, editorBackgroundLayerNumber); //background.gameObject.tag = "Editor"; //background.pixelInset = new Rect(-areaWidth / 2.0f, -areaHeight / 2.0f, areaWidth, areaHeight); // Draw current height step var heightStep = (int)((editorHeight / editorHeightStepSize) + 1); string heightText = "Height: " + heightStep; GUI.Label(new Rect(areaLeft + (border * 1), areaTop + (border * 1) + (fontPadding * 1), fontWidth * heightText.Length + fontPadding * 2, fontHeight + fontPadding * 2), heightText); // Draw the Up / Down buttons editorHeightStepValue = GUI.HorizontalSlider( new Rect( areaLeft + (border * 1), areaBottom - (border * 3) - (smallButtonSize * 0.0f), areaRight - areaLeft - (border * 2), border * 2), editorHeightStepValue, 0, 20); if (((int)editorHeightStepValue) != heightStep) { ChangeHeight((int)(editorHeightStepValue - heightStep)); } //if (GUI.Button(new Rect( // areaLeft + (border * 1), // areaBottom - (border * 1) - (smallButtonSize * 1), // smallButtonSize, smallButtonSize), "+")) //{ // ChangeHeight(1); //} //if (GUI.Button(new Rect( // areaRight - (border * 1) - (smallButtonSize * 1), // areaBottom - (border * 1) - (smallButtonSize * 1), // smallButtonSize, smallButtonSize), "-")) //{ // ChangeHeight(-1); //} // Aligners on/off areaWidth = (border * 4.0f) + (buttonWidth * 1.0f); areaHeight = (border * 2.0f) + (buttonHeight * 1.0f); //areaRight = areaRight; areaTop = areaBottom + border; areaLeft = areaRight - areaWidth; areaBottom = areaTop + areaHeight; if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Snap " + (!SimpleJoint.globalDisable ? "On" : "Off"))) { SimpleJoint.globalDisable = !SimpleJoint.globalDisable; } // Save / Load buttons areaTop = areaBottom + border; areaBottom = areaTop + areaHeight; var fileController = GetComponent<FileController>(); if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Save")) { var baseState = fileController.GetGameBaseState(baseObject, maxDistanceFromBase); string[] names = fileController.GetBaseStateNames(); int i = 0; var prefix = "B"; var tempName = prefix + i; while (Array.IndexOf(names, tempName) >= 0) { i++; tempName = prefix + i; } fileController.SaveBaseState(tempName, baseState); } areaTop = areaBottom + border; areaBottom = areaTop + areaHeight; if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Load")) { showLoadList = true; var bNames = new List<string>(); bNames.AddRange(fileController.GetBaseStateNames()); if (shouldShowBossBases) { bossGroup = fileController.LoadBaseGroupFile("BaseData/BossBases"); bNames.AddRange(bossGroup.baseNames); } baseNames = bNames.ToArray(); } areaTop = areaBottom + border; areaBottom = sh - border; if (showLoadList) { var namesHeight = baseNames.Length * buttonHeight; var areaRect = new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop); var gridRect = new Rect(0, 0, areaRight - areaLeft - 2 * border, namesHeight); int selectedIndex; if (areaRect.height > gridRect.height) { selectedIndex = GUI.SelectionGrid(areaRect, -1, baseNames, 1); } else { loadScrollPosition = GUI.BeginScrollView(areaRect, loadScrollPosition, gridRect); selectedIndex = GUI.SelectionGrid(gridRect, -1, baseNames, 1); GUI.EndScrollView(); } if (selectedIndex >= 0) { var baseName = baseNames[selectedIndex]; BaseState baseState = null; if (baseName.StartsWith("Boss")) { var bi = Array.IndexOf(bossGroup.baseNames, baseName); baseState = bossGroup.baseStates[bi]; } else { baseState = fileController.LoadBaseState(baseNames[selectedIndex]); } fileController.SetGameBaseState(blockPrefabs, baseObject, baseState, Consts.editorLayerNumber, maxDistanceFromBase); loadedBaseIndex = selectedIndex; showLoadList = false; // Balance loaded blocks foreach (Transform block in baseObject.transform) { BalancePlacedBlock(block.gameObject); } } //foreach (var name in baseNames) //{ // if (GUI.Button()) // { // var baseState = fileController.LoadBaseState(name); // fileController.SetGameBaseState("Base", baseState); // showLoadList = false; // } //} } areaRight = areaLeft - border; areaTop = border; areaLeft = areaRight - areaWidth; areaBottom = areaTop + areaHeight; // Only show this if a block is touching the base if (baseObject.GetComponent<BaseController>().isTouchingBlock) { if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Done")) { OnEditFinished(); } } if (isAdmin) { areaTop = areaBottom + border; areaBottom = areaTop + areaHeight; if (loadedBaseIndex.HasValue) { if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Delete")) { fileController.DeleteBaseState(baseNames[loadedBaseIndex.Value]); loadedBaseIndex = null; } } #if UNITY_STANDALONE_WIN areaTop = areaBottom + border; areaBottom = areaTop + areaHeight; if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Export")) { showAdminTextArea = !showAdminTextArea; if (showAdminTextArea) { var exportData = fileController.ExportBases(); var tempFileName = System.IO.Path.GetTempFileName(); System.IO.File.WriteAllText(tempFileName, exportData); adminTextAreaText = tempFileName; } } if (showAdminTextArea) { areaTop = areaBottom + border; areaBottom = areaTop + areaHeight; if (GUI.Button(new Rect(areaLeft, areaTop, areaRight - areaLeft, areaBottom - areaTop), "Import")) { //showAdminTextArea = !showAdminTextArea; try { var importData = System.IO.File.ReadAllText(adminTextAreaText); fileController.ImportBases(importData); Debug.Log("Import Succeeded"); } catch (Exception ex) { Debug.Log("FAIL: Import Failed: " + ex.ToString()); } } } if (showAdminTextArea) { var scrollW = areaLeft - 2 * border; var scrollH = sh - 2 * border; //adminTextAreaScrollPosition = GUI.BeginScrollView( // new Rect(border, border, scrollW + 4 * border, scrollH + 4 * border), // adminTextAreaScrollPosition, // new Rect(0, 0, scrollW, scrollH), false, true); adminTextAreaText = GUI.TextArea(new Rect(border, border, scrollW - 2 * border, scrollH - 2 * border), adminTextAreaText); //GUI.EndScrollView(); } #endif } } }