예제 #1
0
    public void DeleteElement(int uniqueId)
    {
        int index = Elements.FindIndex(e => e.UniqueId() == uniqueId);

        if (index < 0)
        {
            Debug.LogWarning("Element you are tring to delete doesn't exists. (" + uniqueId + ")");
            return;
        }
        ElementsService.RecalculateIndexes(_elementsPool, Elements);
        if (Elements[index].IsNew == false)
        {
            ElementData.Instance.DeleteElement(Elements[index].Id);
        }

        if (Elements[index].ElementType == ElementType.Character)
        {
            // Debug.Log("Element is Character");
            int indexOfDialog = index + 1;
            if (indexOfDialog <= Elements.Count - 1 &&
                Elements[indexOfDialog].ElementType == ElementType.Dialog)
            {
                // Debug.Log("Element " + Elements[indexOfDialog].ElementType + " can also be removed.");
                if (Elements[indexOfDialog].IsNew == false)
                {
                    ElementData.Instance.DeleteElement(Elements[indexOfDialog].Id);
                }
                RemoveElement(indexOfDialog, Elements[indexOfDialog].UniqueId());
            }
        }
        else if (Elements[index].ElementType == ElementType.Dialog)
        {
            int indexOfCharacter = index - 1;
            Debug.Log(Elements[indexOfCharacter].ElementType);
            if (indexOfCharacter >= 0 &&
                Elements[indexOfCharacter].ElementType == ElementType.Character)
            {
                Debug.Log("Element " + Elements[indexOfCharacter].ElementType + " can also be removed.");
                if (Elements[indexOfCharacter].IsNew == false)
                {
                    ElementData.Instance.DeleteElement(Elements[indexOfCharacter].Id);
                }
                RemoveElement(indexOfCharacter, Elements[indexOfCharacter].UniqueId());

                // we need to get the new uniqueId somehow
                ElementsService.RecalculateIndexes(_elementsPool, Elements);

                DeleteElement(uniqueId);
                return;
            }
        }

        RemoveElement(index, uniqueId);

        MoveCarret(true, index - 1);
        TextEditorHotkeyController.Instance.EscapeKey();

        // recalculate indexes again in case the order is screwed
        ElementsService.RecalculateIndexes(_elementsPool, Elements);
    }
예제 #2
0
    public void SaveElements()
    {
        ElementsService.RecalculateIndexes(_elementsPool, Elements);

        foreach (Element element in Elements)
        {
            element.StoryId = StoryService.Instance.Story.Id;
            var el = _elementsPool.FirstOrDefault(e => e.UniqueId == element.UniqueId());

            // UsefullUtils.DumpToConsole(element);

            var isPicture = (el as IElementComponent).TypeId == (int)ElementType.Picture;
            if (isPicture)
            {
                element.Paths     = (el as IPictureComponent).Paths;
                element.FileNames = new List <string>();
            }
            else
            {
                element.Text = (el as ITextComponent).GetText();
            }

            ElementData.Instance.SaveElement(element);
            el.UniqueId   = element.UniqueId();
            element.IsNew = false;

            if (isPicture)
            {
                int index = 0;
                foreach (var path in element.Paths)
                {
                    if (string.IsNullOrWhiteSpace(path))
                    {
                        continue;
                    }
                    element.FileNames.Add("img_" + element.Id + "_0_" + ".jpg");
                    var newPath = StoryService.Instance.Story.GetActivePath() + element.FileNames[index];
                    if (path == newPath)
                    {
                        continue;
                    }
                    File.Copy(path, newPath, true);
                    element.Paths[index] = newPath;
                    index++;
                }
                ElementData.Instance.SaveElement(element);
            }
        }

        TextEditorHotkeyController.Instance.ToggleFileOptions();
    }
예제 #3
0
    public bool AddNewElement(ElementType elementType, bool autoCreate = false)
    {
        var currentIndex  = GetCarretIndex();
        var isLastElement = currentIndex == _elementsPool.Count;

        if (autoCreate && isLastElement == false)
        {
            TextEditorHotkeyController.Instance.MainEdit();
            return(false);
        }

        if ((Elements == null || Elements.Count == 0) && elementType != ElementType.SceneHeading)
        {
            return(false);
        }

        ElementType previousElementType = ElementsService.GetPreviousElementType(
            _elementsPool, Elements,
            _editableIndex, isLastElement
            );

        if (ElementsService.FilterNewElements(elementType, previousElementType) == false)
        {
            return(false);
        }

        if (TextEditorHotkeyController.Instance.ShowOptions)
        {
            TextEditorHotkeyController.Instance.FileMainButtons.SetActive(true);
            TextEditorHotkeyController.Instance.InLineSelection.gameObject.SetActive(false);
        }

        var element = new Element()
        {
            Text        = ElementsService.GetDefaultText(elementType),
            ElementType = elementType,
            Index       = currentIndex,
            IsNew       = true
        };

        Elements.Add(element);
        var el = AddElementInPool(element);

        if (isLastElement == false)
        {
            var newIndex = (_editableIndex + 1);
            el.GameObject.transform.SetSiblingIndex(newIndex);
        }
        else
        {
            MoveCarret(true);
        }
        ElementsService.RecalculateIndexes(_elementsPool, Elements);

        GameService.Instance.InternalWait(() =>
        {
            if (element.ElementType == ElementType.Picture)
            {
                (el as IPictureComponent).AutoSelect();
            }
            else
            {
                (el as ITextComponent).AutoSelect();
            }
            TextEditorHotkeyController.Instance.AppState = AppState.Editing;

            if (isLastElement)
            {
                ScrollController.Instance.ScrollToBottom();
            }
            else
            {
                ScrollController.Instance.KeepElementInView(el.GameObject.GetComponent <RectTransform>());
            }
        });
        return(true);
    }