Esempio n. 1
0
    void UpdateElement_Choices(MBAQuestElement_Choices choices, GameObject Template_Toggle)
    {
        int choicesCount = choices.Content.Count;

        for (int i = 0; i < choicesCount; i++)
        {
            int rand = UnityEngine.Random.Range(0, choices.Content.Count);
            MBAQuestElement_Choice choice = choices.Content[rand];
            float      choiceWidth        = choice.Width > 0 ? choice.Width : Template_Toggle.GetComponent <RectTransform>().rect.width;
            float      choiceHeight       = choice.Height > 0 ? choice.Height : Template_Toggle.GetComponent <RectTransform>().rect.height;
            Vector3    currLocalPosition  = CalculateLocalLocation(choices.Relative, choices.LocalPosition, Content) + new Vector2(0, -i * choiceHeight + 10);
            GameObject newToggle          = JWInstantiateUnderParent_UI(Template_Toggle, Content, "", "", true, currLocalPosition, Template_Toggle.transform.localScale, Template_Toggle.transform.localEulerAngles);
            newToggle.GetComponent <RectTransform>().sizeDelta = new Vector2(choiceWidth, choiceHeight);
            newToggle.GetComponent <Toggle>().isOn             = false;
            newToggle.GetComponent <Toggle>().onValueChanged.AddListener(delegate { UpdateSubmitAvailability(); });
            newToggle.name = choice.Text;
            newToggle.GetComponentInChildren <Text>().text = newToggle.name;
            newToggle.GetComponent <MBAMCOption>().isRight = choice.isRight;

            choices.Content.RemoveAt(rand);
        }
    }
Esempio n. 2
0
    IEnumerator UpdateContentCoroutine(string questID, MBAQuestElement_Problem problem)
    {
        Content = transform.FindChild("Content").FindChild("Viewport").FindChild("Content").gameObject;
        Content.transform.localPosition = Vector3.zero;

        List <MBAQuestElement_CampItem> items  = new List <MBAQuestElement_CampItem>();
        List <MBAQuestElement_Stock>    stocks = new List <MBAQuestElement_Stock>();
        int blankCount = 0;
        MBAQuestElement_Choices choices_Temp = new MBAQuestElement_Choices(0.5f, -100, "x");

        GameObject Template_DDM         = Resources.Load <GameObject>("Prefab/Quest/MBAUIPanel_DDM");
        GameObject Template_DDMItem     = Resources.Load <GameObject>("Prefab/Quest/MBAUIPanel_DDMItem");
        GameObject Template_Help        = Resources.Load <GameObject>("Prefab/Quest/MBAUIPanel_ProblemHelp");
        GameObject Template_Text        = Resources.Load <GameObject>("Prefab/UI/Text");
        GameObject Template_InputField  = Resources.Load <GameObject>("Prefab/UI/InputField");
        GameObject Template_Image       = Resources.Load <GameObject>("Prefab/UI/Image");
        GameObject Template_Toggle      = Resources.Load <GameObject>("Prefab/UI/Toggle");
        GameObject Template_Table       = Resources.Load <GameObject>("Prefab/JW/Table/JWPanel_Table");
        GameObject Template_Chart       = Resources.Load <GameObject>("Prefab/JW/Chart/Panel_Chart");
        GameObject Template_Stock       = Resources.Load <GameObject>("Prefab/Quest/MBAUIPanel_Stock");
        GameObject Template_StockRecord = Resources.Load <GameObject>("Prefab/Quest/MBAUIPanel_StockRecord");

        int objNumber = 0;

        foreach (object obj in problem.Content)
        {
            Type objType = obj.GetType();
            switch (objType.Name)
            {
            case "MBAQuestElement_Help":
                if (FindObjectOfType <MBAProblem_Help>())
                {
                    break;
                }
                GameObject newHelp = JWInstantiateUnderParent_UI(Template_Help, gameObject, true, new Vector3(660, -50, 0));
                newHelp.GetComponent <MBAProblem_Help>().UpdateContent(questID, (MBAQuestElement_Help)obj);
                break;

            case "MBAQuestElement_Stock":
                MBAQuestElement_Stock stock = (MBAQuestElement_Stock)obj;
                stocks.Add(stock);
                break;

            case "MBAQuestElement_Blank":
                //    <blank rightanswer="500" length="5" x="0.5" y="-550"></blank>
                blankCount++;
                MBAQuestElement_Blank blank         = (MBAQuestElement_Blank)obj;
                GameObject            newInputField = JWInstantiateUnderParent_UI(Template_InputField, Content, "InputField", blankCount.ToString() + ".", true, CalculateLocalLocation(blank.Relative, blank.LocalPosition, Content), Vector3.one, Vector3.zero);
                newInputField.GetComponent <InputField>().onValueChanged.AddListener(delegate { UpdateSubmitAvailability(); });
                newInputField.GetComponent <MBAProblem_Blank>().RightAnswer = blank.RightAnswer;
                newInputField.GetComponent <MBAProblem_Blank>().Margin      = blank.Margin;
                newInputField.GetComponent <MBAProblem_Blank>().isTemplate  = false;
                foreach (Text text in newInputField.GetComponentsInChildren <Text>())
                {
                    //text.font = Resources.Load<Font>("Art/fonts/Belwe-Bold");
                    text.GetComponent <Text>().color = Color.black;
                }
                float blankWidth  = blank.Width > 0 ? blank.Width : newInputField.GetComponent <RectTransform>().rect.width;
                float blankHeight = blank.Height > 0 ? blank.Height : newInputField.GetComponent <RectTransform>().rect.height;
                newInputField.GetComponent <RectTransform>().sizeDelta = new Vector2(blankWidth, blankHeight);
                break;

            case "MBAQuestElement_Choices":
                UpdateElement_Choices((MBAQuestElement_Choices)obj, Template_Toggle);
                break;

            case "MBAQuestElement_Choice":
                if (choices_Temp.Content.Count <= 0 && objNumber > 0)
                {
                    MBAQuestElement lastElement = (MBAQuestElement)problem.Content[objNumber - 1];
                    choices_Temp = new MBAQuestElement_Choices(lastElement.LocalPosition.x, lastElement.LocalPosition.y - lastElement.Height / 2.0f - 50, lastElement.Relative);
                }
                choices_Temp.Content.Add((MBAQuestElement_Choice)obj);
                break;

            case "MBAQuestElement_Image":
                MBAQuestElement_Image img      = (MBAQuestElement_Image)obj;
                GameObject            newImage = JWInstantiateUnderParent_UI(Template_Image, Content, "Image", LoadSprite(img.Src, questID), true, CalculateLocalLocation(img.Relative, img.LocalPosition, Content), Vector3.one, Vector3.zero);
                float imgWidth  = img.Width > 0 ? img.Width : newImage.GetComponent <RectTransform>().rect.width;
                float imgHeight = img.Height > 0 ? img.Height : newImage.GetComponent <RectTransform>().rect.height;
                newImage.GetComponent <RectTransform>().sizeDelta = new Vector2(imgWidth, imgHeight);
                newImage.GetComponent <Image>().preserveAspect    = true;
                break;

            case "MBAQuestElement_Text":
                Template_Text.GetComponent <Text>().font               = Resources.Load <Font>("Art/fonts/Belwe-Bold");
                Template_Text.GetComponent <Text>().color              = Color.black;
                Template_Text.GetComponent <Text>().lineSpacing        = 1.5f;
                Template_Text.GetComponent <RectTransform>().sizeDelta = new Vector2(((MBAQuestElement)obj).Width, ((MBAQuestElement)obj).Height);
                JWInstantiateUnderParent_UI(Template_Text, Content, "Text", ((MBAQuestElement_Text)obj).Content, true, CalculateLocalLocation(((MBAQuestElement)obj).Relative, ((MBAQuestElement)obj).LocalPosition, Content), Vector3.one, Vector3.zero);
                break;

            case "MBAQuestElement_Camp":
                MBAQuestElement_Camp camp             = (MBAQuestElement_Camp)obj;
                GameObject           newPanelCamp_DDM = JWInstantiateUnderParent_UI(Template_DDM, Content, "Panel", "", true, CalculateLocalLocation(camp.Relative, camp.LocalPosition, Content), Vector3.one, Vector3.zero);
                newPanelCamp_DDM.GetComponent <MBAPanelDDM>().Side = "camp";
                newPanelCamp_DDM.name = camp.Name;
                newPanelCamp_DDM.transform.FindChild("Text_Name").GetComponent <Text>().text = camp.Name;
                float campWidth  = camp.Width > 0 ? camp.Width : newPanelCamp_DDM.GetComponent <RectTransform>().rect.width;
                float campHeight = camp.Height > 0 ? camp.Height : newPanelCamp_DDM.GetComponent <RectTransform>().rect.height;
                newPanelCamp_DDM.GetComponent <RectTransform>().sizeDelta = new Vector2(campWidth, campHeight);
                newPanelCamp_DDM.transform.FindChild("Content").GetComponent <RectTransform>().sizeDelta = new Vector2(campWidth, campHeight);

                MBAPanelDDM newMBAPanelDDM = newPanelCamp_DDM.GetComponent <MBAPanelDDM>();
                foreach (MBAQuestElement_CampItem item in camp.Items)
                {
                    items.Add(item);
                    newMBAPanelDDM.ExpectedItems.Add(item);
                }
                break;

            case "MBAQuestElement_Table":
                MBAQuestElement_Table table    = (MBAQuestElement_Table)obj;
                GameObject            newTable = JWInstantiateUnderParent_UI(Template_Table, Content, "Panel", "", true, CalculateLocalLocation(table.Relative, table.LocalPosition, Content), Vector3.one, Vector3.zero);
                float tableWidth  = table.Width > 0 ? table.Width : newTable.GetComponent <RectTransform>().rect.width;
                float tableHeight = table.Height > 0 ? table.Height : newTable.GetComponent <RectTransform>().rect.height;
                newTable.GetComponent <RectTransform>().sizeDelta = new Vector2(tableWidth, tableHeight);
                //Template_Table
                newTable.GetComponent <JWPanel_Table>().UpdateContent(table);
                break;

            case "MBAQuestElement_Chart":
                MBAQuestElement_Chart chart    = (MBAQuestElement_Chart)obj;
                GameObject            newChart = JWInstantiateUnderParent_UI(Template_Chart, Content, "Panel", "", true, CalculateLocalLocation(chart.Relative, chart.LocalPosition, Content), Vector3.one, Vector3.zero);
                newChart.GetComponent <JWPanel_Chart>().UpdateChart(chart);
                break;

            case "MBAQuestElement_Solution":
                solution = (MBAQuestElement_Solution)obj;
                break;
            }
            objNumber++;
        }

        //UpdateChoices
        UpdateElement_Choices(choices_Temp, Template_Toggle);

        //instantiate stock panel
        if (stocks.Count > 0)
        {
            ToggleDowntownAndSkipButtons(false);
            GameObject newPanel_Stock = JWInstantiateUnderParent(Template_Stock, Content, CalculateLocalLocation(problem.Relative, new Vector2(0.5f, -200), Content));
            int        i = 0;
            foreach (MBAQuestElement_Stock stock in stocks)
            {
                GameObject newStockRecord = JWInstantiateUnderParent(Template_StockRecord, newPanel_Stock, new Vector3(0, 100 - 40 * i, 0));
                newStockRecord.transform.FindChild("Text_StockName").GetComponent <Text>().text = stock.Name;
                newStockRecord.transform.FindChild("Text_OldPrice").GetComponent <Text>().text  = (stock.Oldprice).ToString();
                newStockRecord.transform.FindChild("Text_NewPrice").GetComponent <Text>().text  = (stock.Newprice).ToString();
                newStockRecord.GetComponent <MBAUIPanel_StockRecord>().Why       = stock.Why;
                newStockRecord.GetComponent <MBAUIPanel_StockRecord>().Price_Old = stock.Oldprice;
                newStockRecord.GetComponent <MBAUIPanel_StockRecord>().Price_New = stock.Newprice;

                Text text_NewPrice = newStockRecord.transform.FindChild("Text_NewPrice").GetComponent <Text>();
                text_NewPrice.color = new Color(text_NewPrice.color.r, text_NewPrice.color.g, text_NewPrice.color.b, 0);
                Button button_Why = newStockRecord.transform.FindChild("Text_NewPrice").GetComponentInChildren <Button>();
                if (button_Why != null)
                {
                    button_Why.interactable = false;
                    Image image_ButtonWhy = button_Why.GetComponent <Image>();
                    image_ButtonWhy.color = new Color(image_ButtonWhy.color.r, image_ButtonWhy.color.g, image_ButtonWhy.color.b, 0);
                }

                newStockRecord.GetComponent <MBAUIPanel_StockRecord>().ShouldBuy = stock.ShouldBuy;

                i++;
            }
        }

        //Instantiate DDM Pool
        if (items.Count > 0)
        {
            GameObject newPanelPool_DDM = JWInstantiateUnderParent(Template_DDM, Content, CalculateLocalLocation(problem.Relative, problem.LocalPosition, Content));
            newPanelPool_DDM.GetComponent <MBAPanelDDM>().Side = "pool";
            newPanelPool_DDM.name = problem.Name;
            newPanelPool_DDM.transform.FindChild("Text_Name").GetComponent <Text>().text = problem.Name;
            newPanelPool_DDM.GetComponent <RectTransform>().sizeDelta = new Vector2(problem.Width_Pool, 0);

            int itemsCount = items.Count;
            for (int i = 0; i < itemsCount; i++)
            {
                yield return(new WaitForSeconds(0.1f));

                int rand = UnityEngine.Random.Range(0, items.Count);
                MBAQuestElement_CampItem item = items[rand];

                GameObject newItem = JWInstantiateUnderParent(Template_DDMItem, Content, Vector3.zero);
                newItem.GetComponentInChildren <Text>().text = item.Text;
                if (item.Img == "")
                {
                    Color itemColor = newItem.GetComponent <Image>().color;
                    newItem.GetComponent <Image>().color = new Color(itemColor.r, itemColor.g, itemColor.b, 0);
                }
                else
                {
                    Color itemColor = newItem.GetComponent <Image>().color;
                    newItem.GetComponent <Image>().color          = new Color(itemColor.r, itemColor.g, itemColor.b, 1);
                    newItem.GetComponent <Image>().sprite         = LoadSprite(item.Img);
                    newItem.GetComponent <Image>().preserveAspect = true;
                }
                Transform content_Pool = newPanelPool_DDM.transform.FindChild("Content");
                content_Pool.GetComponent <RectTransform>().sizeDelta = new Vector2(problem.Width_Pool, 0);
                newItem.transform.SetParent(content_Pool);

                items.RemoveAt(rand);
            }
        }
        Content.GetComponent <JWUI>().AutoSize(Content.GetComponent <RectTransform>().rect.width, problem.Height, 0, 100);
    }