// Broad-phase callback. private void AddPair(ref FixtureProxy proxyA, ref FixtureProxy proxyB) { Fixture fixtureA = proxyA.Fixture; Fixture fixtureB = proxyB.Fixture; int indexA = proxyA.ChildIndex; int indexB = proxyB.ChildIndex; Body bodyA = fixtureA.Body; Body bodyB = fixtureB.Body; // Are the fixtures on the same body? if (bodyA == bodyB) { return; } // Does a contact already exist? ContactEdge edge = bodyB.ContactList; while (edge != null) { if (edge.Other == bodyA) { Fixture fA = edge.Contact.FixtureA; Fixture fB = edge.Contact.FixtureB; int iA = edge.Contact.ChildIndexA; int iB = edge.Contact.ChildIndexB; if (fA == fixtureA && fB == fixtureB && iA == indexA && iB == indexB) { // A contact already exists. return; } if (fA == fixtureB && fB == fixtureA && iA == indexB && iB == indexA) { // A contact already exists. return; } } edge = edge.Next; } // Does a joint override collision? Is at least one body dynamic? if (bodyB.ShouldCollide(bodyA) == false) { return; } //Check default filter if (ShouldCollide(fixtureA, fixtureB) == false) { return; } // Check user filtering. if (ContactFilter != null && ContactFilter(fixtureA, fixtureB) == false) { return; } if (fixtureA.BeforeCollision != null && fixtureA.BeforeCollision(fixtureA, fixtureB) == false) { return; } if (fixtureB.BeforeCollision != null && fixtureB.BeforeCollision(fixtureB, fixtureA) == false) { return; } // Call the factory. Contact c = Contact.Create(fixtureA, indexA, fixtureB, indexB); // Contact creation may swap fixtures. fixtureA = c.FixtureA; fixtureB = c.FixtureB; bodyA = fixtureA.Body; bodyB = fixtureB.Body; // Insert into the world. ContactList.Add(c); // Connect to island graph. // Connect to body A c.NodeA.Contact = c; c.NodeA.Other = bodyB; c.NodeA.Prev = null; c.NodeA.Next = bodyA.ContactList; if (bodyA.ContactList != null) { bodyA.ContactList.Prev = c.NodeA; } bodyA.ContactList = c.NodeA; // Connect to body B c.NodeB.Contact = c; c.NodeB.Other = bodyA; c.NodeB.Prev = null; c.NodeB.Next = bodyB.ContactList; if (bodyB.ContactList != null) { bodyB.ContactList.Prev = c.NodeB; } bodyB.ContactList = c.NodeB; }
protected void UpdateLabel() { SetString(m_sInitialString, false); if (m_sString == null) { return; } if (m_tDimensions.Width > 0) { // Step 1: Make multiline string str_whole = m_sString; int stringLength = str_whole.Length; var multiline_string = new StringBuilder(stringLength); var last_word = new StringBuilder(stringLength); int line = 1, i = 0; bool start_line = false, start_word = false; float startOfLine = -1, startOfWord = -1; int skip = 0; CCRawList<CCNode> children = m_pChildren; for (int j = 0; j < children.count; j++) { CCSprite characterSprite; int justSkipped = 0; while ((characterSprite = (CCSprite) GetChildByTag(j + skip + justSkipped)) == null) { justSkipped++; } skip += justSkipped; if (!characterSprite.Visible) { continue; } if (i >= stringLength) { break; } char character = str_whole[i]; if (!start_word) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (!start_line) { startOfLine = startOfWord; start_line = true; } // Newline. if (character == '\n') { int len = last_word.Length; while (len > 0 && Char.IsWhiteSpace(last_word[len - 1])) { len--; last_word.Remove(len, 1); } multiline_string.Append(last_word); multiline_string.Append('\n'); #if XBOX || XBOX360 last_word.Length = 0; #else last_word.Clear(); #endif start_word = false; start_line = false; startOfWord = -1; startOfLine = -1; i += justSkipped; line++; if (i >= stringLength) break; character = str_whole[i]; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (startOfLine == 0) { startOfLine = startOfWord; start_line = true; } } // Whitespace. if (Char.IsWhiteSpace(character)) { last_word.Append(character); multiline_string.Append(last_word); #if XBOX || XBOX360 last_word.Length = 0; #else last_word.Clear(); #endif start_word = false; startOfWord = -1; i++; continue; } // Out of bounds. if (GetLetterPosXRight(characterSprite) - startOfLine > m_tDimensions.Width) { if (!m_bLineBreakWithoutSpaces) { last_word.Append(character); int len = multiline_string.Length; while (len > 0 && Char.IsWhiteSpace(multiline_string[len - 1])) { len--; multiline_string.Remove(len, 1); } if (multiline_string.Length > 0) { multiline_string.Append('\n'); } line++; start_line = false; startOfLine = -1; i++; } else { int len = last_word.Length; while (len > 0 && Char.IsWhiteSpace(last_word[len - 1])) { len--; last_word.Remove(len, 1); } multiline_string.Append(last_word); multiline_string.Append('\n'); #if XBOX || XBOX360 last_word.Length = 0; #else last_word.Clear(); #endif start_word = false; start_line = false; startOfWord = -1; startOfLine = -1; line++; if (i >= stringLength) break; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (startOfLine == 0) { startOfLine = startOfWord; start_line = true; } j--; } continue; } else { // Character is normal. last_word.Append(character); i++; continue; } } multiline_string.Append(last_word); SetString(multiline_string.ToString(), false); } // Step 2: Make alignment if (m_pHAlignment != CCTextAlignment.Left) { int i = 0; int lineNumber = 0; int str_len = m_sString.Length; var last_line = new CCRawList<char>(); for (int ctr = 0; ctr <= str_len; ++ctr) { if (ctr == str_len || m_sString[ctr] == '\n') { float lineWidth = 0.0f; int line_length = last_line.Count; // if last line is empty we must just increase lineNumber and work with next line if (line_length == 0) { lineNumber++; continue; } int index = i + line_length - 1 + lineNumber; if (index < 0) continue; var lastChar = (CCSprite) GetChildByTag(index); if (lastChar == null) continue; lineWidth = lastChar.Position.X + lastChar.ContentSize.Width / 2.0f; float shift = 0; switch (m_pHAlignment) { case CCTextAlignment.Center: shift = ContentSize.Width / 2.0f - lineWidth / 2.0f; break; case CCTextAlignment.Right: shift = ContentSize.Width - lineWidth; break; default: break; } if (shift != 0) { for (int j = 0; j < line_length; j++) { index = i + j + lineNumber; if (index < 0) continue; var characterSprite = (CCSprite) GetChildByTag(index); characterSprite.Position = characterSprite.Position + new CCPoint(shift, 0.0f); } } i += line_length; lineNumber++; last_line.Clear(); continue; } last_line.Add(m_sString[ctr]); } } if (m_pVAlignment != CCVerticalTextAlignment.Bottom && m_tDimensions.Height > 0) { int lineNumber = 1; int str_len = m_sString.Length; for (int ctr = 0; ctr < str_len; ++ctr) { if (m_sString[ctr] == '\n') { lineNumber++; } } float yOffset = 0; if (m_pVAlignment == CCVerticalTextAlignment.Center) { yOffset = m_tDimensions.Height / 2f - (m_pConfiguration.m_nCommonHeight * lineNumber) / 2f; } else { yOffset = m_tDimensions.Height - m_pConfiguration.m_nCommonHeight * lineNumber; } for (int i = 0; i < str_len; i++) { var characterSprite = GetChildByTag(i); characterSprite.PositionY += yOffset; } } }
private void BufferMove(Element <FixtureProxy> proxy) { _moveBuffer.Add(proxy); }
private CCBMFontConfiguration InitializeFont(string fontName, float fontSize, string charset) { if (Director == null) return null; if (m_pData == null) { InitializeTTFAtlas(1024, 1024); } if (String.IsNullOrEmpty(charset)) { charset = " "; } var chars = new CCRawList<char>(); var fontKey = GetFontKey(fontName, fontSize); CCBMFontConfiguration fontConfig; if (!fontConfigurations.TryGetValue(fontKey, out fontConfig)) { fontConfig = new CCBMFontConfiguration(); fontConfigurations.Add(fontKey, fontConfig); } for (int i = 0; i < charset.Length; i++) { var ch = charset[i]; if (!fontConfig.Glyphs.ContainsKey(ch) && chars.IndexOf(ch) == -1) { chars.Add(ch); } } if (chars.Count == 0) { return fontConfig; } CreateFont(fontName, fontSize, chars); fontConfig.CommonHeight = (int)Math.Ceiling(GetFontHeight()); int[] data = null; for (int i = 0; i < chars.Count; i++) { var s = chars[i].ToString(); var charSize = GetMeasureString(s); int w = (int)Math.Ceiling(charSize.Width + 2); int h = (int)Math.Ceiling(charSize.Height + 2); if (data == null || data.Length < (w * h)) { data = new int[w * h]; } unsafe { int stride; byte* pBase = GetBitmapData(s, out stride); int minX = w; int maxX = 0; int minY = h; int maxY = 0; for (int y = 0; y < h; y++) { var row = (int*)(pBase + y * stride); for (int x = 0; x < w; x++) { if (row[x] != 0) { minX = Math.Min(minX, x); maxX = Math.Max(maxX, x); minY = Math.Min(minY, y); maxY = Math.Max(maxY, y); } } } w = Math.Max(maxX - minX + 1, 1); h = Math.Max(maxY - minY + 1, 1); //maxX = minX + w; //maxY = minY + h; int index = 0; for (int y = minY; y <= maxY; y++) { var row = (int*)(pBase + y * stride); for (int x = minX; x <= maxX; x++) { data[index] = row[x]; index++; } } var region = AllocateRegion(w, h); if (region.x >= 0) { SetRegionData(region, data, w); var info = GetKerningInfo(chars[i]); var fontDef = new CCBMFontConfiguration.CCBMGlyphDef() { Character = chars[i], Subrect = new CCRect(region.x, region.y, region.width, region.height), XOffset = minX, // + (int)Math.Ceiling(info.A), YOffset = minY, XAdvance = (int)Math.Ceiling(info.A + info.B + info.C) }; fontConfig.CharacterSet.Add(chars[i]); fontConfig.Glyphs.Add(chars[i], fontDef); } else { CCLog.Log("Texture atlas is full"); } } } m_bTextureDirty = true; return fontConfig; }
protected void UpdateLabel() { SetString(labelInitialText, false); if (string.IsNullOrEmpty(labelText)) { return; } if (labelDimensions.Width > 0) { // Step 1: Make multiline string str_whole = labelText; int stringLength = str_whole.Length; var multiline_string = new StringBuilder(stringLength); var last_word = new StringBuilder(stringLength); int line = 1, i = 0; bool start_line = false, start_word = false; float startOfLine = -1, startOfWord = -1; int skip = 0; CCRawList<CCNode> children = Children; for (int j = 0; j < children.Count; j++) { CCSprite characterSprite; int justSkipped = 0; while ((characterSprite = (CCSprite)this[(j + skip + justSkipped)]) == null) { justSkipped++; } skip += justSkipped; if (!characterSprite.Visible) { continue; } if (i >= stringLength) { break; } char character = str_whole[i]; if (!start_word) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (!start_line) { startOfLine = startOfWord; start_line = true; } // Newline. if (character == '\n') { int len = last_word.Length; while (len > 0 && Char.IsWhiteSpace(last_word[len - 1])) { len--; last_word.Remove(len, 1); } multiline_string.Append(last_word); multiline_string.Append('\n'); last_word.Clear(); start_word = false; start_line = false; startOfWord = -1; startOfLine = -1; i += justSkipped; line++; if (i >= stringLength) break; character = str_whole[i]; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (startOfLine == 0) { startOfLine = startOfWord; start_line = true; } } // Whitespace. if (Char.IsWhiteSpace(character)) { last_word.Append(character); multiline_string.Append(last_word); last_word.Clear(); start_word = false; startOfWord = -1; i++; continue; } // Out of bounds. if (GetLetterPosXRight(characterSprite) - startOfLine > labelDimensions.Width) { if (!lineBreakWithoutSpaces) { last_word.Append(character); int len = multiline_string.Length; while (len > 0 && Char.IsWhiteSpace(multiline_string[len - 1])) { len--; multiline_string.Remove(len, 1); } if (multiline_string.Length > 0) { multiline_string.Append('\n'); } line++; start_line = false; startOfLine = -1; i++; } else { int len = last_word.Length; while (len > 0 && Char.IsWhiteSpace(last_word[len - 1])) { len--; last_word.Remove(len, 1); } multiline_string.Append(last_word); multiline_string.Append('\n'); last_word.Clear(); start_word = false; start_line = false; startOfWord = -1; startOfLine = -1; line++; if (i >= stringLength) break; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); start_word = true; } if (startOfLine == 0) { startOfLine = startOfWord; start_line = true; } j--; } continue; } else { // Character is normal. last_word.Append(character); i++; continue; } } multiline_string.Append(last_word); SetString(multiline_string.ToString(), false); } // Step 2: Make alignment if (horzAlignment != CCTextAlignment.Left) { int i = 0; int lineNumber = 0; int str_len = labelText.Length; var last_line = new CCRawList<char>(); // If label dim is 0, then we need to use the content size width instead float maxLabelWidth = labelDimensions.Width > 0 ? labelDimensions.Width : ContentSize.Width; for (int ctr = 0; ctr <= str_len; ++ctr) { if (ctr == str_len || labelText[ctr] == '\n') { float lineWidth = 0.0f; int line_length = last_line.Count; // if last line is empty we must just increase lineNumber and work with next line if (line_length == 0) { lineNumber++; continue; } int index = i + line_length - 1 + lineNumber; if (index < 0) continue; var lastChar = (CCSprite)this[index]; if (lastChar == null) continue; lineWidth = lastChar.Position.X + lastChar.ContentSize.Width; var shift = maxLabelWidth - lineWidth; if (horzAlignment == CCTextAlignment.Center) shift /= 2; for (int j = 0; j < line_length; j++) { index = i + j + lineNumber; if (index < 0) continue; var characterSprite = this[index]; characterSprite.PositionX += shift; } i += line_length; lineNumber++; last_line.Clear(); continue; } last_line.Add(labelText[ctr]); } } if (vertAlignment != CCVerticalTextAlignment.Bottom && labelDimensions.Height > 0) { int lineNumber = 1; int str_len = labelText.Length; for (int ctr = 0; ctr < str_len; ++ctr) { if (labelText[ctr] == '\n') { lineNumber++; } } float yOffset = labelDimensions.Height - FontConfiguration.CommonHeight * lineNumber; if (vertAlignment == CCVerticalTextAlignment.Center) { yOffset /= 2f; } for (int i = 0; i < str_len; i++) { var characterSprite = this[i] as CCSprite; if (characterSprite != null && characterSprite.Visible) characterSprite.PositionY += yOffset; } } }
protected void _updateLabel() { this.SetString(this._initialText, false); if (this._text == null) { return; } if (this._dimensions.Width > 0) { // Step 1: Make multiline string alltext = this._text; int textLength = alltext.Length; StringBuilder multilines = new StringBuilder(textLength); StringBuilder lastWord = new StringBuilder(textLength); int line = 1; int i = 0; bool isStartLine = false; bool isStartWord = false; float startOfLine = -1; float startOfWord = -1; int skip = 0; CCRawList<CCNode> children = base.Children; for (int j = 0; j < children.Count; j++) { CCSprite characterSprite; int justSkipped = 0; while ((characterSprite = (CCSprite)GetChildByTag(j + skip + justSkipped)) == null) { justSkipped++; } skip += justSkipped; if (characterSprite.Visible == false) continue; if (i >= textLength) break; char character = alltext[i]; if (isStartWord == false) { startOfWord = GetLetterPosXLeft(characterSprite); isStartWord = true; } if (isStartLine == false) { startOfLine = startOfWord; isStartLine = true; } // Newline. if (character == '\n') { int len = lastWord.Length; while (len > 0 && Char.IsWhiteSpace(lastWord[len - 1])) { len--; lastWord.Remove(len, 1); } multilines.Append(lastWord); multilines.Append('\n'); lastWord.Clear(); isStartWord = false; isStartLine = false; startOfWord = -1; startOfLine = -1; i += justSkipped; line++; if (i >= textLength) break; character = alltext[i]; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); isStartWord = true; } if (startOfLine == 0) { startOfLine = startOfWord; isStartLine = true; } } // Whitespace. if (Char.IsWhiteSpace(character)) { lastWord.Append(character); multilines.Append(lastWord); lastWord.Clear(); isStartWord = false; startOfWord = -1; i++; continue; } // Out of bounds. if (GetLetterPosXRight(characterSprite) - startOfLine > _dimensions.Width) { if (!_isLineBreakWithoutSpaces) { lastWord.Append(character); int len = multilines.Length; while (len > 0 && Char.IsWhiteSpace(multilines[len - 1])) { len--; multilines.Remove(len, 1); } if (multilines.Length > 0) { multilines.Append('\n'); } line++; isStartLine = false; startOfLine = -1; i++; } else { int len = lastWord.Length; while (len > 0 && Char.IsWhiteSpace(lastWord[len - 1])) { len--; lastWord.Remove(len, 1); } multilines.Append(lastWord); multilines.Append('\n'); lastWord.Clear(); isStartWord = false; isStartLine = false; startOfWord = -1; startOfLine = -1; line++; if (i >= textLength) break; if (startOfWord == 0) { startOfWord = GetLetterPosXLeft(characterSprite); isStartWord = true; } if (startOfLine == 0) { startOfLine = startOfWord; isStartLine = true; } j--; } continue; } else { // Character is normal. lastWord.Append(character); i++; continue; } } multilines.Append(lastWord); SetString(multilines.ToString(), false); } // Step 2: Make alignment if (this._horizontalAlignment != CCTextAlignment.Left) { int i = 0; int lineNumber = 0; int textLength = this._text.Length; var lastLine = new CCRawList<char>(); for (int ctr = 0; ctr <= textLength; ++ctr) { if (ctr == textLength || this._text[ctr] == '\n') { float lineWidth = 0.0f; int lineLength = lastLine.Count; if (lineLength == 0) { lineNumber++; continue; } int index = i + lineLength - 1 + lineNumber; if (index < 0) continue; var lastChar = (CCSprite)GetChildByTag(index); if (lastChar == null) continue; lineWidth = lastChar.Position.X + lastChar.ContentSize.Width / 2.0f; float shift = 0; switch (this._horizontalAlignment) { case CCTextAlignment.Center: shift = ContentSize.Width / 2.0f - lineWidth / 2.0f; break; case CCTextAlignment.Right: shift = ContentSize.Width - lineWidth; break; default: break; } if (shift != 0) { for (int j = 0; j < lineLength; j++) { index = i + j + lineNumber; if (index < 0) continue; var characterSprite = (CCSprite)GetChildByTag(index); characterSprite.Position = characterSprite.Position + new CCPoint(shift, 0.0f); } } i += lineLength; lineNumber++; lastLine.Clear(); continue; } lastLine.Add(_text[ctr]); } } if (this._verticalAlignment != CCVerticalTextAlignment.Bottom && this._dimensions.Height > 0) { int lineNumber = 1; int textLength = this._text.Length; for (int ctr = 0; ctr < textLength; ++ctr) { if (this._text[ctr] == '\n') { lineNumber++; } } float yOffset = 0; if (this._verticalAlignment == CCVerticalTextAlignment.Center) { yOffset = this._dimensions.Height / 2f - (this._fontConfig.CommonHeight * lineNumber) / 2f; } else { yOffset = this._dimensions.Height - this._fontConfig.CommonHeight * lineNumber; } for (int i = 0; i < textLength; i++) { var characterSprite = GetChildByTag(i); characterSprite.PositionY += yOffset; } } }
void AddProtectedChild(CCNode child) { positionsAreDirty = true; protectedChildren.Add(child); }
private void RefreshAsset(string name) { var asset = _data.GetAsset(name); _sw.Reset(); foreach (var poly in asset.Polygons) { var v = new CCRawList<Vector3>(poly.Count); for (int i = 0; i < poly.Count; i++) { v.Add(new Vector3(poly[i].X, poly[i].Y, 0)); //v[i].Position = new Vec3 { X = poly[i].X, Y = poly[i].Y }; //v[i].Data = poly[i].Color; } _sw.Start(); _tess.AddContour(v, poly.Orientation); _sw.Stop(); } _sw.Start(); _tess.Tessellate(_windingRule, ElementType.Polygons, _polySize, VertexCombine); _sw.Stop(); var output = new PolygonSet(); for (int i = 0; i < _tess.Elements.Count / 3; i++) { var poly = new Polygon(); for (int j = 0; j < _polySize; j++) { int index = _tess.Elements[i * _polySize + j]; if (index == -1) continue; var v = new PolygonPoint { X = _tess.Vertices[index].X, Y = _tess.Vertices[index].Y, //Color = _tess.Vertices[index].Data != null ? (Color)_tess.Vertices[index].Data : Color.White }; poly.Add(v); } output.Add(poly); } statusMain.Text = string.Format("{0:F3} ms - {1} polygons (of {2} vertices) {3}", _sw.Elapsed.TotalMilliseconds, _tess.Elements.Count / 3, _polySize, _polySize == 3 ? "... triangles" : ""); _canvas.Input = asset.Polygons; _canvas.Output = output; _canvas.Invalidate(); }
private static void ToTess(PolygonSet pset, Tess tess) { foreach (var poly in pset) { var v = new CCRawList<Vector3>(poly.Count); for (int i = 0; i < poly.Count; i++) { v.Add(new Vector3(poly[i].X, poly[i].Y, 0)); } tess.AddContour(v, poly.Orientation); } }