public MFGuiQuad GetAvailableQuad() { LogFuncCall("GetAvailableQuad", name); // Get an available sprite: if (availableQuads.Count < 1) { EnlargeQuadArrays(allocBlockSize); // If we're out of available sprites, allocate some more: } // Use a sprite from the list of available blocks: int index = availableQuads.Dequeue(); // Assign the new sprite: MFGuiQuad quad = quads[index]; // Save this to an active list now that it is in-use: if (activeQuads.Count == activeQuads.Capacity) { activeQuads.Capacity += 10; } activeQuads.Add(quad.index); // activate this renderer if (MFGuiManager.Instance != null) { MFGuiManager.Instance.RegisterRendererForActivation(this); } // Done return(quad); }
// Enlarges the sprite array by the specified count and also resizes // the UV and vertex arrays by the necessary corresponding amount. // Returns the index of the first newly allocated element // (ex: if the sprite array was already 10 elements long and is // enlarged by 10 elements resulting in a total length of 20, // EnlargeArrays() will return 10, indicating that element 10 is the // first of the newly allocated elements.) void EnlargeQuadArrays(int count) { if (quads == null) { InitArrays(); } // Resize sprite array: MFGuiQuad[] tempQuads = quads; quads = new MFGuiQuad[tempQuads.Length + count]; tempQuads.CopyTo(quads, 0); // Vertices: Vector3[] tempVerts = vertices; vertices = new Vector3[vertices.Length + count * 4]; tempVerts.CopyTo(vertices, 0); // UVs: Vector2[] tempUVs = UVs; UVs = new Vector2[UVs.Length + count * 4]; tempUVs.CopyTo(UVs, 0); // Colors: Color[] tempColors = colors; colors = new Color[colors.Length + count * 4]; tempColors.CopyTo(colors, 0); // Triangle indices: int[] tempTris = triIndices; triIndices = new int[triIndices.Length + count * 6]; tempTris.CopyTo(triIndices, 0); // Setup the newly-added sprites and Add them to the list of available // sprite blocks. Also initialize the triangle indices while we're at it: for (int idx = tempQuads.Length; idx < quads.Length; ++idx) { // Create and setup quad quads[idx] = new MFGuiQuad(idx); // Add as an available quad availableQuads.Enqueue(idx); // Init triangle indices: // Clockwise winding triIndices[idx * 6 + 0] = idx * 4 + 0; // 0_ 1 0 ___ 3 triIndices[idx * 6 + 1] = idx * 4 + 3; // | / Verts: | / | triIndices[idx * 6 + 2] = idx * 4 + 1; // 2|/ 1|/__|2 triIndices[idx * 6 + 3] = idx * 4 + 3; // 3 triIndices[idx * 6 + 4] = idx * 4 + 2; // /| triIndices[idx * 6 + 5] = idx * 4 + 1; // 5/_|4 } countChanged = true; }
// Updates the color values of the specified sprite and copies the // new values into the mesh object. public void UpdateColors(MFGuiQuad quad, Color color) { LogFuncCall("UpdateColors", name); int vidx = 4 * quad.index; colors[vidx + 0] = color; colors[vidx + 1] = color; colors[vidx + 2] = color; colors[vidx + 3] = color; colorsChanged = true; }
// Updates the UVs of the specified sprite and copies the new values // into the mesh object. public void UpdateUV(MFGuiQuad quad, Vector2 uvCoords, Vector2 uvDimensions) { LogFuncCall("UpdateUV", name); int vidx = 4 * quad.index; UVs[vidx + 0] = uvCoords + Vector2.up * uvDimensions.y; // Upper-left UVs[vidx + 1] = uvCoords; // Lower-left UVs[vidx + 2] = uvCoords + Vector2.right * uvDimensions.x; // Lower-right UVs[vidx + 3] = uvCoords + uvDimensions; // Upper-right uvsChanged = true; }
public void UpdateVertices(MFGuiQuad quad, Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4) { LogFuncCall("UpdateVertices", name); int vidx = 4 * quad.index; vertices[vidx + 0] = v1; vertices[vidx + 1] = v2; vertices[vidx + 2] = v3; vertices[vidx + 3] = v4; vertsChanged = true; }
public void FreeQuad(MFGuiQuad quad) { LogFuncCall("RemoveQuad", name); int vidx = 4 * quad.index; vertices[vidx + 0] = Vector3.zero; vertices[vidx + 1] = Vector3.zero; vertices[vidx + 2] = Vector3.zero; vertices[vidx + 3] = Vector3.zero; activeQuads.Remove(quad.index); availableQuads.Enqueue(quad.index); vertsChanged = true; }
void UpdateQuads(bool updateVerts, bool updateUVs, bool updateColor) { // deduce actual bounds float[] xAxis, yAxis; byte nonEmpty = ComputeSegments(out xAxis, out yAxis); // deduce full update bool fullUpdate = nonEmpty != m_Quads.Length; // refresh quad list if (fullUpdate == true) { RefreshQuadList(nonEmpty); } float originWidth = m_UVCoords.Width; float originHeight = m_UVCoords.Height; m_GuiRenderer.UVSpaceToPixelSpace(ref originWidth, ref originHeight); float widthMult = originWidth / m_Size.x; float heightMult = originHeight / m_Size.y; // udpate quads // we are going in bottom-up direction // so we need to go from the end of the list int quadIdx = 0; for (int yIdx = 2; yIdx >= 0; --yIdx) { float y1 = yAxis[yIdx + 1]; float y2 = yAxis[yIdx]; // do not create quads for empty row float posY1, posY2; ComputeRealValues(2 - yIdx, m_Size.y, y1 * heightMult, y2 * heightMult, out posY1, out posY2); if (posY2 - posY1 <= 0.0f) { continue; } for (int xIdx = 0; xIdx < 3; ++xIdx) { float x1 = xAxis[xIdx]; float x2 = xAxis[xIdx + 1]; // do not create quads for empty column float posX1, posX2; ComputeRealValues(xIdx, m_Size.x, x1 * widthMult, x2 * widthMult, out posX1, out posX2); if (posX2 - posX1 <= 0.0f) { continue; } MFGuiQuad quad = m_Quads[quadIdx]; if (quad == null) { continue; } // update screen coords if (fullUpdate == true || updateVerts == true) { Rect rect = new Rect(m_Offset.x + posX1, m_Offset.y + posY1, posX2 - posX1, posY2 - posY1); if (nonEmpty > 1) { rect.x = rect.x - m_Size.x * 0.5f + rect.width * 0.5f; rect.y = rect.y - m_Size.y * 0.5f + rect.height * 0.5f; } quad.UpdateVertices(m_GuiRenderer, matrix, rect, 0.0f); } // update UV coords if (fullUpdate == true || updateUVs == true) { float uvX1, uvX2; ComputeRealValues(xIdx, m_UVCoords.Width, x1, x2, out uvX1, out uvX2); float uvY1, uvY2; ComputeRealValues(2 - yIdx, m_UVCoords.Height, y1, y2, out uvY1, out uvY2); Vector2 uvCoords = new Vector2(m_UVCoords.U + uvX1, m_UVCoords.V + uvY1); Vector2 uvDimensions = new Vector2(uvX2 - uvX1, uvY2 - uvY1); quad.UpdateUVs(m_GuiRenderer, uvCoords, uvDimensions); } // update color if (fullUpdate == true || updateColor == true) { quad.SetColor(m_GuiRenderer, m_Color); } // go for new quad quadIdx += 1; } } }