void CreatePuzzle() { blocks = new Block[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; if (y == blocksPerLine - 1 && x == 0) { emptyBlock = block; } } } Camera.main.orthographicSize = blocksPerLine * .55f; inputs = new Queue <Block>(); }
public List <Texture2D> GetSpritesFromTexture_3D(Character _character, int blocks) { Texture2D temp = null; List <Texture2D> tempList = new List <Texture2D>(); Texture2D[,] imageSlices = new Texture2D[blocks, blocks]; if (_characterSpriteDictionary.TryGetValue(_character, out temp)) { imageSlices = ImageSlicer.GetSlices(temp, blocks); } if (temp != null) { for (int y = 0; y < blocks; y++) { for (int x = 0; x < blocks; x++) { tempList.Add(imageSlices[x, y]); } } return(tempList); } print("Error In Color Manager - GetColorValue Method"); return(null); }
void CreatePuzzle() { Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Cube); blockObject.transform.position = new Vector2(x + 6, y + 1);//-Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.Init(new Vector2Int(x, y), imageSlices[x, y]); if (y == 0 && x == blocksPerLine - 1) { blockObject.SetActive(false); emptyBlock = block; } } } //Camera.main.orthographicSize = blocksPerLine * .55f; }
void createPuzzle() { blocks = new Block[ammountBlockPerLine, ammountBlockPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlicer(image, ammountBlockPerLine); for (int y = 0; y < ammountBlockPerLine; y++) { for (int x = 0; x < ammountBlockPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (ammountBlockPerLine - 1) * 0.5f + new Vector2(x, y); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishMoving += OnBlockFinishedMove; block.initial(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; if (y == 0 && x == ammountBlockPerLine - 1) { emptyBlock = block; } } } Camera.main.orthographicSize = ammountBlockPerLine; inputs = new Queue <Block>(); //Vector2 newPos = gameObject.transform.position; //newPos.y += -1; //transform.position = newPos; }
public void CreatePuzzle() { blocks = new Block[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Cube); blockObject.transform.position = -Vector3.one * (blocksPerLine - 1) * .5f + new Vector3(x, y, 0); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector3Int(x, y, 0), imageSlices[blocksPerLine - x - 1, blocksPerLine - y - 1]); blocks[x, y] = block; if (y == 0 && x == blocksPerLine - 1) { emptyBlock = block; } } } Camera.main.transform.position = new Vector3(0, 1, -blocksPerLine * 2f); inputs = new Queue <Block>(); }
private void InitQuads() { Texture2D[,] slices = ImageSlicer.GetSlices(image, size); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); quad.transform.position = new Vector3(x - (size / 2f) + 0.5f, y - (size / 2f) + 0.5f, 0f); quad.transform.parent = transform; PuzzlePiece puzzlePiece = quad.AddComponent <PuzzlePiece>(); puzzlePiece.OnPuzzlePiecePressed += EnqueuePuzzlePiece; puzzlePiece.OnPuzzlePieceFinishedSliding += OnPuzzlePieceFinishedMoving; puzzlePiece.Init(new Vector2Int(x, y), slices[y, x]); _puzzle[x, y] = puzzlePiece; if (y == 0 && x == size - 1) { _hiddenPiece = puzzlePiece; } } } Camera.main.orthographicSize = size * 0.55f; }
public void InstantiateQuads(int blocksPerLine) { _blocks = new Block[blocksPerLine, blocksPerLine]; var imageSlices = ImageSlicer.GetSlices(image, this.blocksPerLine); var offset = blocksPerLine / 2f - 0.5f; for (int row = 0; row < blocksPerLine; row++) { for (int column = 0; column < blocksPerLine; column++) { var instantiatePosition = new Vector3(row - offset, column - offset, 0f); var quad = GameObject.CreatePrimitive(PrimitiveType.Quad); quad.transform.position = instantiatePosition; quad.transform.SetParent(transform); quad.gameObject.name = $"quad{row}x{column}"; var block = quad.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += HandleBlockFinishedMoving; block.Init(new Vector2Int(row, column), imageSlices[row, column]); _blocks[row, column] = block; } } _emptyBlock = _blocks[blocksPerLine - 1, blocksPerLine - 1]; }
void CreatePuzzle() { blocks = new BlockEvent[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); for (int i = 0; i < blocksPerLine; i++) { for (int j = 0; j < blocksPerLine; j++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * 0.5f + new Vector2(j, i) + new Vector2(transform.position.x, transform.position.y); blockObject.transform.position = new Vector3(blockObject.transform.position.x, blockObject.transform.position.y, transform.position.z - 0.1f); blockObject.transform.parent = transform; BlockEvent block = blockObject.AddComponent <BlockEvent>(); block.OnBlockPressed += PlayerMouseBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(j, i), imageSlices[j, i]); blocks[j, i] = block; if (i == 0 && j == blocksPerLine - 1) { emptyBlock = block; } } } cam.orthographicSize = blocksPerLine * 0.55f; inputs = new Queue <BlockEvent>(); }
public IActionResult OnGetSheet(Guid deckId) { var deck = _deckStore.Get(deckId); var cardCount = deck.Items.Sum(i => i.Amount); return(new FileContentResult(ImageSlicer.Composite(DataImages(deck), cardCount), "image/png")); }
void CreatePuzzle() { blocks = new Block[blocksPerLine, blocksPerColumn]; // création de la liste des blocs Texture2D[,] imageSlices = ImageSlicer.GetSlices(brailleWord, blocksPerLine); //Vector3Int [,] MatriceLettre= transformationMatrice(mot_a_coder); //Texture2D [,] ImageLettre = transformationImage(mot_a_coder); for (int y = 0; y < blocksPerColumn; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); // récupération d'un bloc = Quad blockObject.transform.position = new Vector2(-Vector2.one.x * (blocksPerLine - 1) * .5f + new Vector2(x, y).x, -Vector2.one.y * (blocksPerColumn - 1) * .5f + new Vector2(x, y).y); // on s'arrange pour que ça soit symétrique par rapport à la caméra blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); //block.Init(new Vector2Int(x, y), ImageLettre[x,y], MatriceLettre[x, y]); //on donne l'image (une partie découpée de l'image d'origine) au bloc blocks[x, y] = block; blockObject.tag = letterAndPos(x, y); if (y == blocksPerColumn - 1 && x == blocksPerLine - 1) { blockObject.SetActive(false); // on enlève l'image du bloc en bas à droite emptyBlock = block; } } } for (int i = 0; i < nombrePermutation; i++) // on réalise nombrePermutation permutations entre deux blocs pour mélanger le puzzle { int a = Random.Range(0, blocksPerLine); // int b = Random.Range(0, blocksPerColumn); // x int c = Random.Range(0, blocksPerLine); int d = Random.Range(0, blocksPerColumn); Vector2Int targetCoord = blocks[a, b].coord; blocks[a, b].coord = blocks[c, d].coord; blocks[c, d].coord = targetCoord; Vector2 targetPosition = blocks[a, b].transform.position; blocks[a, b].transform.position = blocks[c, d].transform.position; blocks[c, d].transform.position = targetPosition; //Vector3 targetMatriceLettre = blocks[a,b].lettre; //blocks[a, b].lettre = blocks[c, d].lettre; //blocks[c, d].lettre = targetMatriceLettre; } Camera.main.orthographicSize = Mathf.Max(blocksPerLine, blocksPerColumn) * .55f; //modification de l'image pour que ça correponde à l'écran inputs = new Queue <Block>(); state = PuzzleState.InPlay; }
//Puzzle creation private void CreatePuzzle() { //Disable Game Over Panel GameOverPanel.SetActive(false); Image[] imageComponentBlock = new Image[BlocksPerLine * BlocksPerLine]; Blocks = new Block[BlocksPerLine, BlocksPerLine]; ImageSlices = ImageSlicer.GetSpriteSlices(ImageSprite, BlocksPerLine); //Auto Grid Layout For Column Arranging AutoGridLayout autoGridLayout = transform.Find("GameFieldParent").Find("GameField").GetComponent<AutoGridLayout>(); autoGridLayout._column = BlocksPerLine; //Add Outline //transform.Find("GameFieldParent").Find("GameField").GetComponent<Outline>().enabled = true; //Creating Image GameObject Component for (int i = 0; i< BlocksPerLine * BlocksPerLine; i++) { GameObject newObj = new GameObject("Block"); Image newImage = newObj.AddComponent<Image>(); imageComponentBlock[i] = newImage; newObj.GetComponent<RectTransform>().SetParent(transform.Find("GameFieldParent").Find("GameField")); } //Creating grid with block component. for (int row = 0; row < BlocksPerLine; row++) { for (int column = 0; column < BlocksPerLine; column++) { foreach (Image singleImage in imageComponentBlock) { if (singleImage.gameObject.GetComponent<Block>() == null) { //Adding Block Script To Game Object Block block = singleImage.gameObject.AddComponent<Block>(); block.SetImage(ImageSlices[row, column]); block.OnBlockPressed += MoveBlockByPlayer; block.Number = 1; block.Row = row; block.Column = column; if (row == BlocksPerLine - 1 && column == BlocksPerLine - 1) { block.Number = 0; EmptyBlock = block; EmptyRow = row; EmptyColumn = column; } Blocks[row, column] = block; break; } } } } }
void CreatePuzzle() { blocks = new Block[blocksPerLine, blocksPerColumn]; // creation blocklist Texture2D[,] imageSlices = ImageSlicer.GetSlices(brailleWord, blocksPerLine); for (int y = 0; y < blocksPerColumn; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); // create block = Quad blockObject.transform.position = new Vector2(-Vector2.one.x * (blocksPerLine - 1) * .5f + new Vector2(x, y).x, -Vector2.one.y * (blocksPerColumn - 1) * .5f + new Vector2(x, y).y); // symetrical to camera blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; blockObject.tag = letterAndPos(x, y); // apply a tag for each kind of letter and if it's top or bottom part if (y == blocksPerColumn - 1 && x == blocksPerLine - 1) { blockObject.SetActive(false); // disable block bottom right emptyBlock = block; } } } for (int i = 0; i < nombrePermutation; i++) //shuffling { int a = Random.Range(0, blocksPerLine); // y int b = Random.Range(0, blocksPerColumn); // x int c = Random.Range(0, blocksPerLine); int d = Random.Range(0, blocksPerColumn); Vector2Int targetCoord = blocks[a, b].coord; blocks[a, b].coord = blocks[c, d].coord; blocks[c, d].coord = targetCoord; Vector2 targetPosition = blocks[a, b].transform.position; blocks[a, b].transform.position = blocks[c, d].transform.position; blocks[c, d].transform.position = targetPosition; } Camera.main.orthographicSize = Mathf.Max(blocksPerLine, blocksPerColumn) * .55f; // adapt camera to game inputs = new Queue <Block>(); state = PuzzleState.InPlay; }
public IActionResult OnPostAsync(Guid gameId, int cardsPerRow, int cardCount, IFormFile cardSheet, IFormFile infoSheet) { var cards = new Card[] { }; if (infoSheet != null) { using (var reader = new System.IO.StreamReader(infoSheet.OpenReadStream())) { cards = CardList.FromString(reader.ReadToEnd()).ToArray(); } } using (var imageSlicer = new ImageSlicer(cardsPerRow, cardCount, cardSheet.OpenReadStream())) { _game = _gameStore.Get().SingleOrDefault(g => g.Id == gameId); if (_game.CardSize == null) { _game.CardSize = imageSlicer.CardSize; _gameStore.UpdateOne(_game.Id, g => g.CardSize = imageSlicer.CardSize); } else if (_game.CardSize != imageSlicer.CardSize) { // Problem, card sizes are different return(RedirectToAction("UploadCards")); } var index = 0; foreach (var imageData in imageSlicer.Slices) { var card = cards.Length > index ? cards[index] : new Card(); card.Id = Guid.NewGuid(); card.GameId = gameId; card.CreatedOn = DateTime.Now; _cardStore.Add(card); _imageStore.Add(new ImageData { Id = card.Id, Data = imageData, OriginalFileName = String.Empty }); index++; } UpdateGameData(gameId); return(RedirectToPage("/Cards", new { gameId })); } }
void CreatePuzzle() { id = (blocksPerLine * blocksPerLine) - (blocksPerLine - 1); blocks = new Block[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); solver.initialPos = new Vector2Int[(blocksPerLine * blocksPerLine)]; solver.currentPos = new Vector2Int[(blocksPerLine * blocksPerLine)]; solver.idOrder = new int[(blocksPerLine * blocksPerLine)]; solver.correctidOrder = new int[(blocksPerLine * blocksPerLine)]; for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y); blockObject.transform.parent = transform; block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); block.id = id; blocks[x, y] = block; if (isDivisible(id, blocksPerLine)) { id = id - (blocksPerLine * 2); } id++; if (y == 0 && x == blocksPerLine - 1) { emptyBlock = block; block.id = 0; } solver.initialPos[block.id] = block.coord; solver.currentPos[block.id] = block.coord; solver.idOrder[block.id] = block.id; solver.correctidOrder[block.id] = block.id; } } Camera.main.orthographicSize = blocksPerLine * .55f; inputs = new Queue <Block>(); }
public List <Sprite> GetSpritesFromTexture(Character _character) { Texture2D temp = null; List <Texture2D> tempList = new List <Texture2D>(); Texture2D[,] imageSlices = new Texture2D[3, 3]; List <Sprite> tempSprites = new List <Sprite>(); if (_characterSpriteDictionary.TryGetValue(_character, out temp)) { imageSlices = ImageSlicer.GetSlices(temp, 3); } if (temp != null) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { tempList.Add(imageSlices[x, y]); } } int size = 2; for (int i = 0; i < tempList.Count; i++) { tempList[i].Resize(size, size); tempList[i].Apply(); } for (int i = 0; i < tempList.Count; i++) { Rect rec = new Rect(0, 0, size, size); print("Width and Height: " + tempList[i].width + " - " + tempList[i].height); tempSprites.Add(Sprite.Create(tempList[i], rec, new Vector2(0, 0), 1)); } return(tempSprites); } print("Error In Color Manager - GetColorValue Method"); return(null); }
public IActionResult Upload(Guid gameId, int cardsPerRow, int cardCount, IFormFile cardSheet) { foreach (var imageData in ImageSlicer.Slice(cardsPerRow, cardCount, cardSheet.OpenReadStream())) { var card = new Card { Id = Guid.NewGuid(), Name = String.Empty, GameId = gameId, CreatedOn = DateTime.Now }; _cardStore.Add(card); _imageStore.Store(card.Id, imageData); } _gameStore.UpdateOne(gameId, g => g.CardCount += cardCount); return(RedirectToAction("Cards", "Games", new { gameId })); }
void CreatePuzzle() { float a = 0f; float b = 0f; blocks = new Block[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(image, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(2.5f + x + a, -2.5f + y + b); blockObject.transform.localScale += new Vector3(0.5f, 0.5f, 0.5f); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; block.BeUnActive(); if (y == 0 && x == blocksPerLine - 1) { emptyBlock = block; } a += 0.5f; } a = 0f; b += 0.4f; } inputs = new Queue <Block>(); StartShuffle(); }
void CreatePuzzle() { blocks = new Block[blocksPerLine, blocksPerLine]; // imgae // Create a copy of the texture by reading and applying the raw texture data. Texture2D texCopy = new Texture2D(image.width, image.height, image.format, image.mipmapCount > 1); texCopy.LoadRawTextureData(image.GetRawTextureData()); //Debug.Log(texCopy.isReadable); texCopy.Apply(); Texture2D[,] imageSlices = ImageSlicer.GetSlices(texCopy, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; if (y == 0 && x == blocksPerLine - 1) { emptyBlock = block; } } } Camera.main.orthographicSize = blocksPerLine; //* .55f; inputs = new Queue <Block>(); }
private void PickImage(int maxSize) { NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) => { Debug.Log("Image path: " + path); if (path != null) { // Create Texture from selected image Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize); /* * Texture2D texCopy = new Texture2D(texture.width, texture.height, texture.format, texture.mipmapCount > 1); * texCopy.LoadRawTextureData(texture.GetRawTextureData()); * texCopy.Apply(); * * byte[] pix = texture.GetRawTextureData(); * Texture2D readableText = new Texture2D(texture.width, texture.height, texture.format, false); * readableText.LoadRawTextureData(pix); * readableText.Apply(); */ RenderTexture renderTex = RenderTexture.GetTemporary( texture.width, texture.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); Graphics.Blit(texture, renderTex); RenderTexture previous = RenderTexture.active; RenderTexture.active = renderTex; Texture2D readableText2 = new Texture2D(texture.width, texture.height); readableText2.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0); readableText2.Apply(); RenderTexture.active = previous; RenderTexture.ReleaseTemporary(renderTex); //if (texture == null) //{ // Debug.Log("Couldn't load texture from " + path); // return; //} blocks = new Block[blocksPerLine, blocksPerLine]; Texture2D[,] imageSlices = ImageSlicer.GetSlices(readableText2, blocksPerLine); for (int y = 0; y < blocksPerLine; y++) { for (int x = 0; x < blocksPerLine; x++) { GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad); blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y); blockObject.transform.parent = transform; Block block = blockObject.AddComponent <Block>(); block.OnBlockPressed += PlayerMoveBlockInput; block.OnFinishedMoving += OnBlockFinishedMoving; block.Init(new Vector2Int(x, y), imageSlices[x, y]); blocks[x, y] = block; if (y == 0 && x == blocksPerLine - 1) { emptyBlock = block; } } } Camera.main.orthographicSize = blocksPerLine; //* .55f; inputs = new Queue <Block>(); StartShuffle(); /* * // Assign texture to a temporary quad and destroy it after 5 seconds * GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); * quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f; * quad.transform.forward = Camera.main.transform.forward; * //1 2 * quad.transform.localScale = new Vector3(1f, readableText2.height / (float)readableText2.width, 1f); * * Material material = quad.GetComponent<Renderer>().material; * if (!material.shader.isSupported) // happens when Standard shader is not included in the build * material.shader = Shader.Find("Legacy Shaders/Diffuse"); * //3 * material.mainTexture = readableText2; * * * Destroy(quad, 5f); * * // If a procedural texture is not destroyed manually, * // it will only be freed after a scene change * //4 * Destroy(readableText2, 5f); **/ } }, "Select a PNG image", "image/png", maxSize); Debug.Log("Permission result: " + permission); }
public void Init() { //初始化法阵槽点 slotArray = new Slot[circleSize, circleSize]; for (int i = 0; i < slots.Count; i++) { slots[i].x = i % circleSize; slots[i].y = i / circleSize; slotArray[i % circleSize, i / circleSize] = slots[i]; } blocks = new List <Block>(); //初始裁剪处理预设,生成裁剪好的拼图 for (int i = 0; i < blockPrefabs.Length; i++) { //设定临时Block数列 Block[,] tempBlocks = new Block[blockPrefabs[i].sliceCount.x, blockPrefabs[i].sliceCount.y]; Texture2D[,] imageSlices = ImageSlicer.Slice(blockPrefabs[i].image, blockPrefabs[i].sliceCount.x, blockPrefabs[i].sliceCount.y); //创建拼图块 for (int y = 0; y < blockPrefabs[i].sliceCount.y; y++) { for (int x = 0; x < blockPrefabs[i].sliceCount.x; x++) { Block block = new Block(); block.id = GetBlockID; block.image = imageSlices[x, y]; block.prefab_unit = blockPrefabs[i].prefab_unit; blocks.Add(block); tempBlocks[x, y] = block; } } //设定相邻块 for (int y = 0; y < blockPrefabs[i].sliceCount.y; y++) { for (int x = 0; x < blockPrefabs[i].sliceCount.x; x++) { if (x > 0) { tempBlocks[x, y].leftID = tempBlocks[x - 1, y].id; } if (x < blockPrefabs[i].sliceCount.x - 1) { tempBlocks[x, y].rightID = tempBlocks[x + 1, y].id; } if (y > 0) { tempBlocks[x, y].upID = tempBlocks[x, y - 1].id; } if (y < blockPrefabs[i].sliceCount.y - 1) { tempBlocks[x, y].downID = tempBlocks[x, y + 1].id; } } } } for (int i = 0; i < blocks.Count; i++) { GameObject block = Instantiate(prefab_block, blockParent); block.GetComponent <RectTransform>().anchoredPosition = new Vector3(150 * i, 0, 0); block.GetComponent <BlockItem>().block = blocks[i]; block.GetComponent <BlockItem>().Init(); } }
private void CreatePuzzle() { generateBoolArray(); puzzleBlocks = new PuzzleBlock[blocksPerLine * blocksPerLine]; Texture2D[,] images = ImageSlicer.GetSlices(image, blocksPerLine); int[] numbersInOrder = new int[blocksPerLine * blocksPerLine]; for (int x = 0; x < blocksPerLine; x++) { for (int y = 0; y < blocksPerLine; y++) { GameObject blockObj = GameObject.CreatePrimitive(PrimitiveType.Quad); int pos = randomPosition(); int blockObj_x = generateX(pos); int blockObj_y = generateY(pos); blockObj.transform.position = -Vector2.one * (blocksPerLine - 1) * 0.5f + new Vector2(blockObj_x, blockObj_y); blockObj.transform.parent = transform; PuzzleBlock puzzleBlock = blockObj.AddComponent <PuzzleBlock>(); puzzleBlock.OnBlockPressed += AddMoveBlockToQueue; puzzleBlock.OnFinishedMoving += OnFinishedMoving; int order = (blocksPerLine * blocksPerLine) - (blocksPerLine - (x + 1) + y * blocksPerLine); if (order == 9) { numbersInOrder[pos] = -1; } else { numbersInOrder[pos] = order; } puzzleBlock.Init(new Vector2Int(blockObj_x, blockObj_y), images[x, y], order); puzzleBlocks[blocksPerLine * x + y] = puzzleBlock; if (y == 0 && x == blocksPerLine - 1) { blockObj.SetActive(false); emptyBlock = puzzleBlock; } } } int[] numbersOrdered = new int[blocksPerLine * blocksPerLine]; int j = 0; for (int i = 6; i < 9; i++) { if (numbersInOrder[i] != -1) { numbersOrdered[j] = numbersInOrder[i]; j++; } } for (int i = 3; i < 6; i++) { if (numbersInOrder[i] != -1) { numbersOrdered[j] = numbersInOrder[i]; j++; } } for (int i = 0; i < 3; i++) { if (numbersInOrder[i] != -1) { numbersOrdered[j] = numbersInOrder[i]; j++; } } int inversions = 0; for (int i = 0; i < numbersOrdered.Length - 1; i++) { for (int j1 = i + 1; j1 < numbersOrdered.Length - 1; j1++) { if (numbersOrdered[j1] > numbersOrdered[i]) { inversions++; } } } if (inversions % 2 == 1) { pab.restartPuzzleButton(); } Camera.main.orthographicSize = blocksPerLine * 0.65f; blocksQueue = new Queue <PuzzleBlock>(); }
public void Apply(string input_dir, string output_dir, string fn, int char_size, double size_ratio, bool preprocessing, int tnum, string lang) { MergeTextStrings mts = new MergeTextStrings(); ImageSlicer imgslicer = new ImageSlicer(); List <TextString> text_string_list = new List <TextString>(); List <string> imgpath_list = imgslicer.Apply(1, 1, 100, input_dir + fn, output_dir); Log.WriteLine("Grouping text strings..."); for (int s = 0; s < imgpath_list.Count; s++) { Bitmap srcimg = null; if (lang == "eng") { using (Bitmap tileimg = new Bitmap(imgpath_list[s])) { RemoveBoarderAndNoiseCC removeBoarderAndNoiseCC = new RemoveBoarderAndNoiseCC(); srcimg = removeBoarderAndNoiseCC.Apply(tileimg, char_size, 0.18); Log.WriteBitmap2FolderExactFileName(output_dir, srcimg, "CDAInput.png"); } } else { using (Bitmap tileimg = new Bitmap(imgpath_list[s])) { RemoveBoarderCC removeBoarderCC = new RemoveBoarderCC(); srcimg = removeBoarderCC.Apply(tileimg); Log.WriteBitmap2FolderExactFileName(output_dir, srcimg, "CDAInput.png"); } } ConditionalDilationAutomatic cda = new ConditionalDilationAutomatic(); cda.ang_threshold = angle_ratio; string outputImagePath = output_dir + s + ".png"; cda.Apply(tnum, srcimg, size_ratio, angle_ratio, preprocessing, outputImagePath); using (Bitmap dilatedimg = new Bitmap(outputImagePath)) { DetectTextStrings detectTS = new DetectTextStrings(); List <TextString> string_list = detectTS.Apply(srcimg, dilatedimg); List <Bitmap> string_img_list = new List <Bitmap>(); for (int i = 0; i < string_list.Count; i++) { string_img_list.Add(string_list[i].srcimg); } int[] offset = imgslicer.xy_offset_list[s]; for (int i = 0; i < string_list.Count; i++) { string_list[i].x_offset = offset[0]; string_list[i].y_offset = offset[1]; mts.AddTextString(string_list[i]); } } using (Bitmap CDAInputwithLabel = new Bitmap(outputImagePath)) { Graphics g = Graphics.FromImage(CDAInputwithLabel); for (int i = 0; i < mts.text_string_list.Count; i++) { Font font = new Font("Arial", 20); g.DrawString(i.ToString(), font, Brushes.Red, mts.text_string_list[i].bbx.X, mts.text_string_list[i].bbx.Y); g.DrawRectangle(new Pen(Color.Green, 4), mts.text_string_list[i].bbx); } Log.WriteBitmap2FolderExactFileName(output_dir, CDAInputwithLabel, "CDAInputwithLabel.png"); g.Dispose(); g = null; } srcimg.Dispose(); srcimg = null; } Log.WriteLine("Detecting long string orientation..."); DetectTextOrientation detectTextOrientation = new DetectTextOrientation(); detectTextOrientation.Apply(mts, tnum); Log.WriteLine("Detecting short string orientation..."); for (int i = 0; i < mts.text_string_list.Count; i++) { if (mts.text_string_list[i].char_list.Count <= 3) { List <int> nearest_string_list = detectTextOrientation.findNearestSrings(i, mts.text_string_list); int initial_orientation_count = mts.text_string_list[i].orientation_list.Count; for (int j = 0; j < nearest_string_list.Count; j++) { mts.text_string_list[i].orientation_list.AddRange(mts.text_string_list[nearest_string_list[j]].orientation_list); } for (int j = initial_orientation_count; j < mts.text_string_list[i].orientation_list.Count; j++) { RotateBilinear filter = new RotateBilinear(mts.text_string_list[i].orientation_list[j]); Bitmap rotateimg = ImageUtils.InvertColors(filter.Apply(ImageUtils.InvertColors(mts.text_string_list[i].srcimg))); mts.text_string_list[i].rotated_img_list.Add(rotateimg); } } } Log.WriteLine("Writing string results..."); mts.WriteBMP(output_dir); }