using System.Collections.Generic; using System.Linq; using UnityEngine; public class ResizeRoomCommand : ICommand { Transform _sceneTransform; List _oldWallCenterPoints; List _newWallCenterPoints; List _oldWallStartPoints; List _newWallStartPoints; List _oldWallWidths; List _newWallWidths; Dictionary _oldObjectPositions; Dictionary _newObjectPositions; float _oldHeight; float _oldWide; float _oldDepth; float _newHeight; float _newWide; float _newDepth; List oldWallPointsDataList = new List(); List newWallPointsDataList = new List(); List oldWallLineDataList = new List(); List newWallLineDataList = new List(); bool _isInitialized; public ResizeRoomCommand() { _oldHeight = _G.HEIGHT; _oldWide = _G.WIDE; _oldDepth = _G.DEPTH; InitWallValues(_G.OW, ref _oldWallCenterPoints); InitWallValues(_G.WallsWidth, ref _oldWallWidths); InitWallValues(_G.WallsPointStart, ref _oldWallStartPoints); //_oldWallCenterPoints = _G.OW.GetRange(0, _G.OW.Count); //_oldWallWidths = _G.wW.GetRange(0, _G.wW.Count); //_oldWallStartPoints = _G.wP.GetRange(0, _G.wP.Count); // TODO : keep object positions? _sceneTransform = SceneModeManager.Scene; InitObjectPositions(ref _oldObjectPositions); InitWallValues(false); } public void Execute() { //WallCreationManager._instance.GetParentWallPoints; if (!_isInitialized) { _newHeight = _G.HEIGHT; _newWide = _G.WIDE; _newDepth = _G.DEPTH; InitWallValues(_G.WallsPointCenter, ref _newWallCenterPoints); InitWallValues(_G.WallsWidth, ref _newWallWidths); InitWallValues(_G.WallsPointStart, ref _newWallStartPoints); InitWallValues(true); } CreateRoom(true); if (!_isInitialized) { InitObjectPositions(ref _newObjectPositions); } _isInitialized = true; } public string GetAction() { return "Resize Room"; } public void Undo() { CreateRoom(false); } public void UpdateCommand() { return; } void CreateRoom(bool isExecute) { _G.HEIGHT = isExecute ? _newHeight : _oldHeight; _G.WIDE = isExecute ? _newWide : _oldWide; _G.DEPTH = isExecute ? _newDepth : _oldDepth; UpdateWallValues(isExecute, ref _G.WallsPointCenter, _newWallCenterPoints, _oldWallCenterPoints); UpdateWallValues(isExecute, ref _G.WallsWidth, _newWallWidths, _oldWallWidths); UpdateWallValues(isExecute, ref _G.WallsPointStart, _newWallStartPoints, _oldWallStartPoints); if (isExecute) { RoomCreation.SetRoomPlanFromLists(newWallPointsDataList, newWallLineDataList, _newHeight); } else { RoomCreation.SetRoomPlanFromLists(oldWallPointsDataList, oldWallLineDataList, _oldHeight); } RoomCreation.CreateRoomTo3D(); DOIT.ResetRoomSelect(); DASH.REFRESH(); UpdateObjectPositions(isExecute); GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(false); } void InitWallValues(List copyFrom, ref List copyTo) { copyTo = copyFrom.GetRange(0, copyFrom.Count); } //void InitWallValues(Vector2[] copyFrom, ref Vector2[] copyTo) //{ // copyTo = new Vector2[copyFrom.Length]; // copyFrom.CopyTo(copyTo, 0); //} // //void InitWallValues(float[] copyFrom, ref float[] copyTo) //{ // copyTo = new float[copyFrom.Length]; // copyFrom.CopyTo(copyTo, 0); //} private void InitWallValues(bool isExecute) { foreach (RectTransform pointTransform in WallCreationManager.GetWallPointContainer()) { WallPoint point = pointTransform.GetComponent(); WallPointData pointsData = point.GetCopy(); if (isExecute) { newWallPointsDataList.Add(pointsData); } else { oldWallPointsDataList.Add(pointsData); } } if(!isExecute) { foreach (RectTransform lineTransform in WallCreationManager.GetWallLineContainer()) { WallLine wallLine = lineTransform.GetComponent(); string wall = "w" + wallLine.name[8..]; int.TryParse(wallLine.name[8..], out int wallidx); if (wallLine._category == "Interior") { wallLine.mat.Clear(); for (int i = 0; i < wallLine.interiorWallFacesNumber.Count; i++) { //wall = "w" + (wallidx + i).ToString(); wall = "w" + (wallidx + i).ToString(); GameObject go = GameObject.Find(wall); if (go != null) { wallLine.mat.Add(go.GetComponentInChildren().material); } } } else if (wallLine._category == "Exterior") { // une face GameObject go = GameObject.Find(wall); if (go != null) { wallLine.mat.Clear(); wallLine.mat.Add(go.GetComponentInChildren().material); } } } } foreach (RectTransform lineTransform in WallCreationManager.GetWallLineContainer()) { WallLine wallLine = lineTransform.GetComponent(); if (isExecute) { newWallLineDataList.Add(wallLine.GetCopy()); } else { oldWallLineDataList.Add(wallLine.GetCopy()); } } } void InitObjectPositions(ref Dictionary positions) { positions = new Dictionary(); foreach (Transform t in _sceneTransform) { positions.Add(t.name, t.position); } } void UpdateWallValues(bool isExecute, ref List destinationArray, List executeValues, List undoValues) { List copyArray = isExecute ? executeValues : undoValues; destinationArray = copyArray.GetRange(0, copyArray.Count); } //void UpdateWallValues(bool isExecute, ref Vector2[] destinationArray, Vector2[] executeValues, Vector2[] undoValues) //{ // Vector2[] copyArray = isExecute ? executeValues : undoValues; // copyArray.CopyTo(destinationArray, 0); //} // //void UpdateWallValues(bool isExecute, ref float[] destinationArray, float[] executeValues, float[] undoValues) //{ // float[] copyArray = isExecute ? executeValues : undoValues; // copyArray.CopyTo(destinationArray, 0); //} void UpdateObjectPositions(bool isExecute) { if (!_isInitialized) { return; } foreach (Transform t in _sceneTransform) { t.position = isExecute ? _newObjectPositions[t.name] : _oldObjectPositions[t.name]; Vector3 pos = t.position; int objectIndex = Get.GetObjectIndex(t.name); if (objectIndex != 299) { _G.OBJs[objectIndex][15] = pos.x.ToString(); _G.OBJs[objectIndex][16] = pos.y.ToString(); _G.OBJs[objectIndex][17] = pos.z.ToString(); } } } }