public override ICodeBlock GenerateAdditionalMethods(ICodeBlock codeBlock, IElement element)
        {
            if (element is EntitySave)
            {
                EntitySave entitySave = (EntitySave)element;
                if (entitySave.IsScrollableEntityList && !string.IsNullOrEmpty(entitySave.ItemType))
                {
                    string itemTypeWithoutPath = FileManager.RemovePath(entitySave.ItemType);

                    string visibleSettingLine = "";

                    if (entitySave.ImplementsIVisible)
                    {
                        visibleSettingLine = "newItem.Visible = this.Visible;";
                    }
                    #region AddItemToTop

                    codeBlock
                        .Function("void", "AddItemToTop", "")
                            .Line(string.Format("{0} newItem = Factories.{0}Factory.CreateNew(LayerProvidedByContainer);", itemTypeWithoutPath))
                            .Line(visibleSettingLine)
                            .Line("mLastCreatedScrollableItem = newItem;")
                            .Line("newItem.AttachTo(mScrollableHandle, false);")
                            .If("mScrollableItems.Count > 0")
                                .Line("newItem.RelativeY = mScrollableItems[0].RelativeY + mScrollableSpacing;")
                            .End()
                            .Line("newItem.ForceUpdateDependencies();")

                            .Line("mScrollableFirstIndex--;")
                            .Line("mScrollableItems.Insert(0, newItem);")

                            .If("ScrollItemModified != null")
                                .Line("ScrollItemModified(newItem);")
                            .End()
                            .Line("mLastCreatedScrollableItem = null;")
                        .End()
                    #endregion

                    #region AddItemToBottom

                    .Function("void", "AddItemToBottom", "")
                            .Line(string.Format("{0} newItem = Factories.{0}Factory.CreateNew(LayerProvidedByContainer);", itemTypeWithoutPath))
                            .Line("mLastCreatedScrollableItem = newItem;")
                            .Line("newItem.AttachTo(mScrollableHandle, false);")

                            .If("mScrollableItems.Count > 0")
                                .Line("newItem.RelativeY = mScrollableItems.Last.RelativeY - mScrollableSpacing;")
                            .End()
                            .Else()
                                .Line("newItem.RelativeY = 0;")
                            .End()
                            .Line("newItem.ForceUpdateDependencies();")

                            .Line("mScrollableItems.Add(newItem);")

                            .If("ScrollItemModified != null")
                                .Line("ScrollItemModified(newItem);")
                            .End()
                            .Line("mLastCreatedScrollableItem = null;")
                        .End();
                    #endregion

                    codeBlock = codeBlock.Function("void", "ScrollableListActivity", "");
                        
                    var curBlock = codeBlock
                        .If("ListShowing != null")
                            .Line("FlatRedBall.Gui.Cursor cursor = FlatRedBall.Gui.GuiManager.Cursor;");

                    if (entitySave.ImplementsIClickable || entitySave.ImplementsIWindow || entitySave.GetInheritsFromIWindowOrIClickable())
                    {
                        curBlock = curBlock.If("cursor.PrimaryDown && HasCursorOver(GuiManager.Cursor)");
                    }
                    else
                    {
                        curBlock = curBlock.If("cursor.PrimaryDown");
                    }

                    curBlock = curBlock
                        .Line("mScrollableHandle.RelativeY += FlatRedBall.Gui.GuiManager.Cursor.WorldYChangeAt(this.Z, LayerProvidedByContainer);")
                        .Line("FixScrollableHandleRelativeY();")
                        .Line("mScrollableHandle.ForceUpdateDependenciesDeep();")
                        .End();

                    curBlock
                        .Line("PerformScrollableItemAdditionLogic();")

                        .Line("// remove from top")
                        .While("mScrollableItems.Count > 2 && mScrollableItems[1].Y > mScrollableTopBoundary")
                            .Line("RemoveItemFromTop();")
                        .End()

                        .Line("// remove from bottom")
                        .While("mScrollableItems.Count > 2 && mScrollableItems[mScrollableItems.Count - 2].Y < mScrollableBottomBoundary")
                            .Line("RemoveItemFromBottom();")
                        .End()

                        .Line("bool shouldRefreshAll = false;")
                        .While("ListShowing.Count < mScrollableItems.Count")
                            .Line("RemoveItemFromBottom();")
                            .Line("shouldRefreshAll = true;")
                        .End()

                        .If("shouldRefreshAll")
                            .Line("RefreshAllScrollableListItems();")
                        .End();

                    codeBlock
                        .Else()
                            .While("mScrollableItems.Count > 0")
                                .Line("mScrollableItems.Last.Destroy();");

                    codeBlock = codeBlock.End();
                     
                    codeBlock.Function("public bool", "IsScrollableItemNew", string.Format("{0} itemInQuestion", itemTypeWithoutPath))
                            .Line("return itemInQuestion == mLastCreatedScrollableItem;")
                        .End()

                        .Function("void", "RemoveItemFromTop", "")
                            .Line("mScrollableItems[0].Destroy();")
                            .Line("mScrollableFirstIndex++;")
                        .End()

                        .Function("void", "RemoveItemFromBottom", "")
                            .Line("mScrollableItems.Last.Destroy();")
                        .End()

                        .Function("public int", "GetAbsoluteIndexForItem", itemTypeWithoutPath + " item")
                            .Line("int indexInList = mScrollableItems.IndexOf(item);")
                            .Line("return mScrollableFirstIndex + indexInList;")
                        .End()

                        .Property("public bool", "IsAtTop")
                            .Get()
                                .Line("return mScrollableFirstIndex == 0;")
                            .End()
                        .End()

                        .Function("RefreshAllScrollableListItems", "", Type:"void")
                            .If("mListShowingButUsePropertyPlease == null")
                                .While("mScrollableItems.Count > 0")
                                    .Line("mScrollableItems.Last.Destroy();")
                                .End()
                            .End()

                            .Else()
                                .For("int i = 0; i < mScrollableItems.Count; i++")

                                    .If("ScrollItemModified != null")
                                        .Line("ScrollItemModified(mScrollableItems[i]);")
                                    .End()

                                .End()
                            .End()
                        .End()

                        .Function("RefreshToList", "", Type:"void", Public:true)
                            .Line("PerformScrollableItemAdditionLogic();")
                            .While("mScrollableItems.Count > 2 && mScrollableItems[1].Y > mScrollableTopBoundary")
                                .Line("RemoveItemFromTop();")
                            .End()

                            .While("mScrollableItems.Count > 2 && mScrollableItems[mScrollableItems.Count - 2].Y < mScrollableBottomBoundary || ListShowing.Count < mScrollableItems.Count")
                                .Line("RemoveItemFromBottom();")
                            .End()

                            .Line("RefreshAllScrollableListItems();")
                        .End()

                        .Function("void", "FixScrollableHandleRelativeY", "")
                            .Line("mScrollableHandle.RelativeY = System.Math.Min(mScrollableHandle.RelativeY,(ListShowing.Count - 1) * mScrollableSpacing + mScrollableBottomBoundary);")
                            .Line("mScrollableHandle.RelativeY = System.Math.Max(mScrollableHandle.RelativeY, " + entitySave.ListTopBound + ");")
                        .End()

                        .Function("void", "PerformScrollableItemAdditionLogic", "")
                            .Line("// add to bottom")
                            .While("mScrollableFirstIndex + mScrollableItems.Count < ListShowing.Count && (mScrollableItems.Count == 0 || mScrollableItems.Last.Y > mScrollableBottomBoundary)")
                                .Line("AddItemToBottom();")
                            .End()

                            .Line("// add to top")
                            .While("mScrollableFirstIndex > 0 && mScrollableItems.Count < ListShowing.Count && (mScrollableItems.Count == 0 || mScrollableItems[0].Y < mScrollableTopBoundary)")
                                .Line("AddItemToTop();")
                            .End()
                        .End();
                }
            }

            return codeBlock;
        }
Beispiel #2
0
        public override ICodeBlock GenerateAdditionalMethods(ICodeBlock codeBlock, IElement element)
        {
            if (element is EntitySave)
            {
                EntitySave entitySave = (EntitySave)element;
                if (entitySave.IsScrollableEntityList && !string.IsNullOrEmpty(entitySave.ItemType))
                {
                    string itemTypeWithoutPath = FileManager.RemovePath(entitySave.ItemType);

                    string visibleSettingLine = "";

                    if (entitySave.ImplementsIVisible)
                    {
                        visibleSettingLine = "newItem.Visible = this.Visible;";
                    }
                    #region AddItemToTop

                    codeBlock
                    .Function("void", "AddItemToTop", "")
                    .Line(string.Format("{0} newItem = Factories.{0}Factory.CreateNew(LayerProvidedByContainer);", itemTypeWithoutPath))
                    .Line(visibleSettingLine)
                    .Line("mLastCreatedScrollableItem = newItem;")
                    .Line("newItem.AttachTo(mScrollableHandle, false);")
                    .If("mScrollableItems.Count > 0")
                    .Line("newItem.RelativeY = mScrollableItems[0].RelativeY + mScrollableSpacing;")
                    .End()
                    .Line("newItem.ForceUpdateDependencies();")

                    .Line("mScrollableFirstIndex--;")
                    .Line("mScrollableItems.Insert(0, newItem);")

                    .If("ScrollItemModified != null")
                    .Line("ScrollItemModified(newItem);")
                    .End()
                    .Line("mLastCreatedScrollableItem = null;")
                    .End()
                    #endregion

                    #region AddItemToBottom

                    .Function("void", "AddItemToBottom", "")
                    .Line(string.Format("{0} newItem = Factories.{0}Factory.CreateNew(LayerProvidedByContainer);", itemTypeWithoutPath))
                    .Line("mLastCreatedScrollableItem = newItem;")
                    .Line("newItem.AttachTo(mScrollableHandle, false);")

                    .If("mScrollableItems.Count > 0")
                    .Line("newItem.RelativeY = mScrollableItems.Last.RelativeY - mScrollableSpacing;")
                    .End()
                    .Else()
                    .Line("newItem.RelativeY = 0;")
                    .End()
                    .Line("newItem.ForceUpdateDependencies();")

                    .Line("mScrollableItems.Add(newItem);")

                    .If("ScrollItemModified != null")
                    .Line("ScrollItemModified(newItem);")
                    .End()
                    .Line("mLastCreatedScrollableItem = null;")
                    .End();
                    #endregion

                    codeBlock = codeBlock.Function("void", "ScrollableListActivity", "");

                    var curBlock = codeBlock
                                   .If("ListShowing != null")
                                   .Line("FlatRedBall.Gui.Cursor cursor = FlatRedBall.Gui.GuiManager.Cursor;");

                    if (entitySave.ImplementsIClickable || entitySave.ImplementsIWindow || entitySave.GetInheritsFromIWindowOrIClickable())
                    {
                        curBlock = curBlock.If("cursor.PrimaryDown && HasCursorOver(GuiManager.Cursor)");
                    }
                    else
                    {
                        curBlock = curBlock.If("cursor.PrimaryDown");
                    }

                    curBlock = curBlock
                               .Line("mScrollableHandle.RelativeY += FlatRedBall.Gui.GuiManager.Cursor.WorldYChangeAt(this.Z, LayerProvidedByContainer);")
                               .Line("FixScrollableHandleRelativeY();")
                               .Line("mScrollableHandle.ForceUpdateDependenciesDeep();")
                               .End();

                    curBlock
                    .Line("PerformScrollableItemAdditionLogic();")

                    .Line("// remove from top")
                    .While("mScrollableItems.Count > 2 && mScrollableItems[1].Y > mScrollableTopBoundary")
                    .Line("RemoveItemFromTop();")
                    .End()

                    .Line("// remove from bottom")
                    .While("mScrollableItems.Count > 2 && mScrollableItems[mScrollableItems.Count - 2].Y < mScrollableBottomBoundary")
                    .Line("RemoveItemFromBottom();")
                    .End()

                    .Line("bool shouldRefreshAll = false;")
                    .While("ListShowing.Count < mScrollableItems.Count")
                    .Line("RemoveItemFromBottom();")
                    .Line("shouldRefreshAll = true;")
                    .End()

                    .If("shouldRefreshAll")
                    .Line("RefreshAllScrollableListItems();")
                    .End();

                    codeBlock
                    .Else()
                    .While("mScrollableItems.Count > 0")
                    .Line("mScrollableItems.Last.Destroy();");

                    codeBlock = codeBlock.End();

                    codeBlock.Function("public bool", "IsScrollableItemNew", string.Format("{0} itemInQuestion", itemTypeWithoutPath))
                    .Line("return itemInQuestion == mLastCreatedScrollableItem;")
                    .End()

                    .Function("void", "RemoveItemFromTop", "")
                    .Line("mScrollableItems[0].Destroy();")
                    .Line("mScrollableFirstIndex++;")
                    .End()

                    .Function("void", "RemoveItemFromBottom", "")
                    .Line("mScrollableItems.Last.Destroy();")
                    .End()

                    .Function("public int", "GetAbsoluteIndexForItem", itemTypeWithoutPath + " item")
                    .Line("int indexInList = mScrollableItems.IndexOf(item);")
                    .Line("return mScrollableFirstIndex + indexInList;")
                    .End()

                    .Property("public bool", "IsAtTop")
                    .Get()
                    .Line("return mScrollableFirstIndex == 0;")
                    .End()
                    .End()

                    .Function("RefreshAllScrollableListItems", "", Type: "void")
                    .If("mListShowingButUsePropertyPlease == null")
                    .While("mScrollableItems.Count > 0")
                    .Line("mScrollableItems.Last.Destroy();")
                    .End()
                    .End()

                    .Else()
                    .For("int i = 0; i < mScrollableItems.Count; i++")

                    .If("ScrollItemModified != null")
                    .Line("ScrollItemModified(mScrollableItems[i]);")
                    .End()

                    .End()
                    .End()
                    .End()

                    .Function("RefreshToList", "", Type: "void", Public: true)
                    .Line("PerformScrollableItemAdditionLogic();")
                    .While("mScrollableItems.Count > 2 && mScrollableItems[1].Y > mScrollableTopBoundary")
                    .Line("RemoveItemFromTop();")
                    .End()

                    .While("mScrollableItems.Count > 2 && mScrollableItems[mScrollableItems.Count - 2].Y < mScrollableBottomBoundary || ListShowing.Count < mScrollableItems.Count")
                    .Line("RemoveItemFromBottom();")
                    .End()

                    .Line("RefreshAllScrollableListItems();")
                    .End()

                    .Function("void", "FixScrollableHandleRelativeY", "")
                    .Line("mScrollableHandle.RelativeY = System.Math.Min(mScrollableHandle.RelativeY,(ListShowing.Count - 1) * mScrollableSpacing + mScrollableBottomBoundary);")
                    .Line("mScrollableHandle.RelativeY = System.Math.Max(mScrollableHandle.RelativeY, " + entitySave.ListTopBound + ");")
                    .End()

                    .Function("void", "PerformScrollableItemAdditionLogic", "")
                    .Line("// add to bottom")
                    .While("mScrollableFirstIndex + mScrollableItems.Count < ListShowing.Count && (mScrollableItems.Count == 0 || mScrollableItems.Last.Y > mScrollableBottomBoundary)")
                    .Line("AddItemToBottom();")
                    .End()

                    .Line("// add to top")
                    .While("mScrollableFirstIndex > 0 && mScrollableItems.Count < ListShowing.Count && (mScrollableItems.Count == 0 || mScrollableItems[0].Y < mScrollableTopBoundary)")
                    .Line("AddItemToTop();")
                    .End()
                    .End();
                }
            }

            return(codeBlock);
        }