using Raumgleiter.BaseRGDK; using Raumgleiter.BaseRGDK.JSON; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Raumgleiter.RGDKDevLab { public class MtexUI_004 : MonoBehaviour { MtexDownloadFunctions mtexDownloadFunctions = new MtexDownloadFunctions(); public GameObject Screen001, Screen002, Screen003, ColorPicker; public GameObject GridLayout, GridLayout1, myCanvas, ButtonPrefab01, TogglePrefab01; public Text NumberOfMaterials, MaterialInfo, CurrentScreen; public Button ButtonBackToMaterials, ButtonBackToTypes, ButtonNextPage, ButtonPreviousPage, ButtonSaveMatrial; public RawImage TexturePreview; private int TextSize = 9; private List Screens; private List TypeClassList; private List MatClassList; //private Dictionary urlVariabels = new Dictionary() { //{ "manufacturer_id ", 999999}, { "type_id", 999999 }, { "color_id", 999999}, {"per_page", 20}, {"page", 1} }; private Dictionary> urlVariables = new Dictionary>(); /// Matrial and Texture private Material newMaterial; private Vector2 materialTiling = new Vector2(1f, 1f); private Texture newTexture, newBump, newNormal; private string materialName, totalEntries; void Start() { // make a list of screens for easy switching Screens = new List() { Screen001, Screen002, Screen003 }; // get the types of materials for the start screen StartCoroutine(StartScreen()); StartCoroutine(GetColors()); // add listeners to buttons ButtonBackToMaterials.onClick.AddListener(delegate { ToggleScreens(Screen002); }); ButtonBackToTypes.onClick.AddListener(delegate { ToggleScreens(Screen001); }); ButtonNextPage.onClick.AddListener(delegate { GetNext(); }); ButtonPreviousPage.onClick.AddListener(delegate { GetPrevious(); }); ButtonSaveMatrial.onClick.AddListener(delegate { SaveMaterial(); }); // add Variables to Dictionary for URL building var manufacturer_id_list = new List(); manufacturer_id_list.Add(999999); urlVariables.Add("manufacturer_id", manufacturer_id_list); var type_id_list = new List(); type_id_list.Add(999999); urlVariables.Add("type_id", type_id_list); var color_id_list = new List(); urlVariables.Add("color_id", color_id_list); var per_page_list = new List(); per_page_list.Add(36); urlVariables.Add("per_page", per_page_list); var page_list = new List(); page_list.Add(1); urlVariables.Add("page", page_list); } private IEnumerator GetColors() { var ColorClassList = new List(); yield return mtexDownloadFunctions.GetColors(value => ColorClassList = value); for (int i = 0; i < ColorClassList.Count; i++) { Debug.Log(ColorClassList[i].name); Debug.Log(ColorClassList[i].id); //Button thisButton = AddButton(ColorPicker, ButtonPrefab01, thisButtonName, ColorClassList[i].id); Toggle thisToggle = AddToggle(ColorPicker, TogglePrefab01, ColorClassList[i].name, ColorClassList[i].id); thisToggle.onValueChanged.AddListener(delegate { ColorToggleValueChanged(thisToggle); }); } } private void ColorToggleValueChanged(Toggle thisToggle) { if (thisToggle.isOn) { Debug.Log("if " + thisToggle.isOn); urlVariables["color_id"].Add(int.Parse(thisToggle.name)); Debug.Log("if " + int.Parse(thisToggle.name)); } else { Debug.Log("else " + thisToggle.isOn); urlVariables["color_id"].Remove(int.Parse(thisToggle.name)); } urlVariables["page"][0] = 1; Debug.Log(MtexDownloadFunctions.BuildUrl2(urlVariables)); StartCoroutine(UpdateMaterialsAllScreen()); } private void SaveMaterial() { MtexDownloadFunctions.SaveMaterial(newMaterial, materialName, (Texture2D)newTexture, (Texture2D)newNormal); } private IEnumerator StartScreen() { // activate Screen 001 ToggleScreens(Screen001); CurrentScreen.text = "Material Types"; // Destroy old grid with types foreach (Transform child in Screen001.transform) { if (child.name == "Grid") { GameObject.Destroy(child.gameObject); } } // add new grid var thisGrid = Instantiate(GridLayout1, Screen001.transform); thisGrid.name = "Grid"; thisGrid.SetActive(true); // check for total entries yield return mtexDownloadFunctions.GetTypes(value => TypeClassList = value); var totalEntries = TypeClassList.Count; for (int i = 0; i < totalEntries; i++) { try { var thisButtonName = TypeClassList[i].name; Button thisButton = AddButton(thisGrid, ButtonPrefab01, thisButtonName, TypeClassList[i].id); thisButton.onClick.AddListener(delegate { MaterialsAllScreen(int.Parse(thisButton.name)); }); } catch { Debug.Log("MtexUI_004::StartScreen: Probably clicked to fast next..."); } } } private void MaterialsAllScreen(int this_type_id) { urlVariables["type_id"][0] = this_type_id; StartCoroutine(UpdateMaterialsAllScreen()); } private IEnumerator UpdateMaterialsAllScreen() { // activate Screen 002 ToggleScreens(Screen002); // add name to screen //foreach (var mtexType in TypeClassList) //{ // if (mtexType.id == urlVariables["type_id"][0]) // { // CurrentScreen.text = mtexType.name; // } //} // Destroy old grid with materials foreach (Transform child in Screen002.transform) { if (child.name == "Grid") { Destroy(child.gameObject); } } // add new grid var thisGrid = AddGridToScreen(Screen002); var materialsUrl = MtexDownloadFunctions.BuildUrl2(urlVariables); // check for total entries yield return MtexDownloadFunctions.GetTotalEntries(materialsUrl, value => totalEntries = value); // get all materials of given type yield return mtexDownloadFunctions.GetMaterials(materialsUrl, value => MatClassList = value); // populate grid with materials of given type for (int i = 0; i < MatClassList.Count; i++) { try { var MaterialName = MatClassList[i].name; Button thisButton = AddButton(thisGrid, ButtonPrefab01, MaterialName, MatClassList[i].id); thisButton.onClick.AddListener(delegate { StartCoroutine(MaterialScreen(int.Parse(thisButton.name))); }); // Start async coroutine for adding material Picture StartCoroutine(GetPreviewPicture(thisButton)); } catch { Debug.Log("MtexUI_004::MaterialsScreen: Probably clicked to fast next..."); } } NumberOfMaterials.text = urlVariables["page"][0] + " of " + Math.Ceiling(int.Parse(totalEntries) / (float)urlVariables["per_page"][0]); } private IEnumerator MaterialScreen(int mat_id, string textureSize = "small") { // activate Screen 003 ToggleScreens(Screen003); // add name to screen foreach (var mtexmtexMaterial in MatClassList) { if (mtexmtexMaterial.id == mat_id) { materialName = mtexmtexMaterial.name; CurrentScreen.text = mtexmtexMaterial.name; } } // create empty json JSONNode json = JSONNode.Parse(""); // get selected material info and download textures yield return mtexDownloadFunctions.GetMaterial(mat_id, value => json = value); yield return mtexDownloadFunctions.GetMainTexture(json, value => newTexture = value, textureSize); yield return mtexDownloadFunctions.GetBumpTexture(json, value => newBump = value, textureSize); // check for bump - if there is a bump, convert to normal, else clear normal if (newBump != null) { newNormal = TextureHelper.BumpToNormalMap((Texture2D)newBump); } else { newNormal = null; } // create material and update preview newMaterial = MtexDownloadFunctions.CreateMaterial((Texture2D)newTexture, (Texture2D)newNormal, mat_id.ToString()); TexturePreview.texture = newTexture; // create material description MaterialInfo.text = mtexDownloadFunctions.MaterialDescription(json, newTexture); } private IEnumerator GetPreviewPicture(Button thisButton) { // convert button name to material id var mat_id = int.Parse(thisButton.name); // create empty json JSONNode json = JSONNode.Parse(""); // create empty textre Texture TexturePreview = new Texture(); // get selected material info and download textures yield return mtexDownloadFunctions.GetMaterial(mat_id, value => json = value); yield return mtexDownloadFunctions.GetMainTexture(json, value => TexturePreview = value); // add picture to button try { thisButton.GetComponent().sprite = Sprite.Create((Texture2D)TexturePreview, new Rect(0.0f, 0.0f, TexturePreview.width, TexturePreview.height), new Vector2(0.5f, 0.5f), 100.0f); } catch { Debug.Log("MtexUI_004::GetPreviewPicture: Probably clicked to fast next..."); } } private void ToggleScreens(GameObject CurrentScreen) { for (int i = 0; i < Screens.Count; i++) { Screens[i].SetActive(false); } CurrentScreen.SetActive(true); } private GameObject AddGridToScreen(GameObject Screen) { var MyGridObjectInstance = Instantiate(GridLayout, Screen.transform); MyGridObjectInstance.name = "Grid"; MyGridObjectInstance.SetActive(true); return MyGridObjectInstance; } private Toggle AddToggle(GameObject ParentGO, GameObject thisPrefab, string thisName, int thisId) { var thisInstance = Instantiate(thisPrefab, new Vector3(0, 0, 0), Quaternion.identity); //ButtonInstance.tag = id.ToString(); thisInstance.transform.SetParent(ParentGO.transform); thisInstance.name = thisId.ToString(); // thisInstance.name = thisId.ToString() + "_" + thisName; thisInstance.GetComponentInChildren().text = thisId + "\n" + thisName; Toggle b = thisInstance.GetComponent(); return b; } private Button AddButton(GameObject ParentGO, GameObject ButtonPrefab, string ButtonName, int id) { var ButtonInstance = Instantiate(ButtonPrefab, new Vector3(0, 0, 0), Quaternion.identity); //ButtonInstance.tag = id.ToString(); ButtonInstance.transform.SetParent(ParentGO.transform); ButtonInstance.name = id.ToString(); ButtonInstance.GetComponentInChildren().text = id + "\n" + ButtonName; Button b = ButtonInstance.GetComponent