Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ICities;
- using ColossalFramework.UI;
- using ColossalFramework;
- using UnityEngine;
- using System.Collections.Generic;
- namespace RandomTreeBrush
- {
- public class RandomTreeBrushMod : IUserMod
- {
- public string Name { get { return "Random Tree Brush"; } }
- public string Description { get { return "Makes plopping different types of trees much easier"; } }
- }
- public class RandomTreeBrush : ThreadingExtensionBase
- {
- private static float time = 0.1f;
- float timeLeft = time;
- private int treeCount = 0;
- RandomTreeBrushUIHelper uiHelper;
- bool uiCreated = false;
- bool uiHidden = false;
- public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
- {
- TreeTool treeTool = ToolsModifierControl.toolController.CurrentTool as TreeTool;
- if(treeTool == null)
- {
- if(!uiCreated)
- {
- uiHelper = new RandomTreeBrushUIHelper();
- uiHelper.CreateUI();
- uiHidden = uiHelper.HideUI();
- uiCreated = true;
- }
- if(!uiHidden) uiHidden = uiHelper.HideUI();
- return;
- }
- if(uiHidden) uiHidden = uiHelper.ShowUI();
- if(treeCount == 0) treeCount = PrefabCollection<TreeInfo>.LoadedCount();
- if(Input.GetMouseButtonDown(0))
- {
- RandomTreeBrushData.Instance.GetTree();
- }
- }
- }
- public class RandomTreeBrushData
- {
- public List<uint> selectedTrees = new List<uint>();
- public static RandomTreeBrushData instance = null;
- int currentIndex;
- TreeTool treeTool = ToolsModifierControl.toolController.CurrentTool as TreeTool;
- public static RandomTreeBrushData Instance
- {
- get
- {
- if(instance == null)
- {
- instance = new RandomTreeBrushData();
- }
- return instance;
- }
- }
- public void AddTree(uint treeIndex)
- {
- if(selectedTrees.Contains(treeIndex)) return;
- selectedTrees.Add(treeIndex);
- Debug.Log("Added tree " + PrefabCollection<TreeInfo>.PrefabName(treeIndex));
- GetTree();
- }
- public void RemoveTree(uint treeIndex)
- {
- if(!selectedTrees.Contains(treeIndex)) return;
- selectedTrees.Remove(treeIndex);
- Debug.Log("Removed tree " + PrefabCollection<TreeInfo>.PrefabName(treeIndex));
- GetTree();
- }
- public int TreeAmmount()
- {
- return selectedTrees.Count;
- }
- public void GetTree()
- {
- TreeTool treeTool = ToolsModifierControl.toolController.CurrentTool as TreeTool;
- if(selectedTrees.Count > 0)
- {
- /*if(selectedTrees.Count > 1)
- {
- if(Random.value > 0.2f)
- {
- int oldIndex = currentIndex;
- while (currentIndex == oldIndex) currentIndex = Random.Range(0, selectedTrees.Count);
- }
- else if(!selectedTrees.Contains((uint)currentIndex)) currentIndex = Random.Range(0, selectedTrees.Count);
- }*/
- currentIndex = Random.Range(0, selectedTrees.Count);
- TreeInfo treePrefab = PrefabCollection<TreeInfo>.GetPrefab(selectedTrees[currentIndex]);
- treeTool.m_prefab = treePrefab;
- }
- }
- }
- public class RandomTreeBrushUI : UIPanel
- {
- public override void Start()
- {
- this.backgroundSprite = "GenericPanel";
- this.color = new Color32(100, 100, 100, 255);
- this.size = new Vector2(300, 400);
- this.autoLayout = false;
- this.absolutePosition = new Vector3(1620, 300, 0);
- this.name = "RandomTreeBrushPanel";
- UIDragHandle dragHandle = this.AddUIComponent<UIDragHandle>();
- dragHandle.name = "RandomTreeBrushDragHandle";
- dragHandle.size = new Vector2(this.width, 40);
- dragHandle.relativePosition = new Vector3();
- UILabel label = dragHandle.AddUIComponent<UILabel>();
- label.anchor = UIAnchorStyle.CenterHorizontal;
- label.name = "RandomTreeBrushLabel";
- label.relativePosition = new Vector3(0, 14, 0);
- label.text = "Random Tree Brush";
- UITextField searchBox = this.AddUIComponent<UITextField>();
- searchBox.name = "RandomTreeBrushSearchBox";
- searchBox.relativePosition = new Vector3(0, 40);
- searchBox.size = new Vector2(this.width, 20);
- searchBox.focusedBgSprite = "TextFieldPanel";
- searchBox.hoveredBgSprite = "TextFieldPanelHovered";
- searchBox.selectionSprite = "EmptySprite";
- searchBox.builtinKeyNavigation = true;
- searchBox.verticalAlignment = UIVerticalAlignment.Middle;
- searchBox.horizontalAlignment = UIHorizontalAlignment.Center;
- searchBox.text = "Search here";
- searchBox.selectOnFocus = true;
- searchBox.multiline = false;
- UIScrollablePanel scrollablePanel = this.AddUIComponent<UIScrollablePanel>();
- scrollablePanel.name = "RandomTreeBrushScrollablePanel";
- scrollablePanel.size = new Vector2(this.width, this.height - 60);
- scrollablePanel.color = new Color32(255, 255, 255, 255);
- scrollablePanel.isInteractive = true;
- scrollablePanel.backgroundSprite = "GenericPanel";
- scrollablePanel.relativePosition = new Vector3(0, 60, 0);
- scrollablePanel.scrollWheelAmount = 15;
- scrollablePanel.scrollWheelDirection = UIOrientation.Vertical;
- scrollablePanel.builtinKeyNavigation = true;
- scrollablePanel.autoReset = false;
- int treeCount = PrefabCollection<TreeInfo>.LoadedCount();
- UIScrollbar scrollbar = this.AddUIComponent<UIScrollbar>();
- scrollbar.size = new Vector2(15, scrollablePanel.height);
- scrollbar.relativePosition = new Vector3(this.width - scrollbar.width, scrollablePanel.relativePosition.y);
- scrollbar.orientation = UIOrientation.Vertical;
- scrollbar.incrementAmount = 15;
- scrollbar.trackObject = scrollbar.AddUIComponent<UISlicedSprite>();
- scrollbar.value = 0;
- ((UISlicedSprite)(scrollbar.trackObject)).spriteName = "ScrollbarTrack";
- scrollbar.trackObject.size = scrollbar.size;
- scrollbar.trackObject.relativePosition = new Vector3();
- scrollbar.thumbObject = scrollbar.trackObject.AddUIComponent<UISlicedSprite>();
- ((UISlicedSprite)(scrollbar.thumbObject)).spriteName = "ScrollbarThumb";
- scrollablePanel.verticalScrollbar = scrollbar;
- scrollablePanel.clipChildren = true;
- RandomTreeBrushScrollablePanelTemplate[] scrollablePanelTemplates = new RandomTreeBrushScrollablePanelTemplate[treeCount];
- for (uint i = 0; i < treeCount; i++)
- {
- scrollablePanelTemplates[i] = scrollablePanel.AddUIComponent<RandomTreeBrushScrollablePanelTemplate>();
- scrollablePanelTemplates[i].index = i;
- scrollablePanelTemplates[i].relativePosition = new Vector3(0, i * 30, 0);
- }
- searchBox.eventTextChanged += (component, inputString) =>
- {
- Search(searchBox, scrollablePanelTemplates, treeCount);
- };
- searchBox.eventTextCancelled += (component, inputString) =>
- {
- Search(searchBox, scrollablePanelTemplates, treeCount);
- };
- searchBox.eventClick += (component, mouseEvent) =>
- {
- if(searchBox.text != "Search here")
- {
- Search(searchBox, scrollablePanelTemplates, treeCount);
- }
- };
- }
- void Search(UITextField searchBox, RandomTreeBrushScrollablePanelTemplate[] scrollablePanelTemplates, int treeCount)
- {
- int vector = 0;
- for (int i = 0; i < treeCount; i++)
- {
- if (scrollablePanelTemplates[i].checkBox.name.ToLower().Contains(searchBox.text.ToLower()) || searchBox.text == "Search here")
- {
- scrollablePanelTemplates[i].Show();
- scrollablePanelTemplates[i].relativePosition = new Vector3(0, vector);
- vector += 30;
- }
- else
- {
- scrollablePanelTemplates[i].Hide();
- }
- }
- }
- }
- public class RandomTreeBrushUIHelper
- {
- UIComponent uiComponent;
- public void CreateUI()
- {
- UIView ui = UIView.GetAView();
- uiComponent = ui.AddUIComponent(typeof(RandomTreeBrushUI));
- }
- public bool HideUI()
- {
- uiComponent.Hide();
- return true;
- }
- public bool ShowUI()
- {
- uiComponent.Show();
- return false;
- }
- }
- public class RandomTreeBrushScrollablePanelTemplate : UIComponent
- {
- public uint index;
- public UICheckBox checkBox;
- public override void Start()
- {
- this.width = 300;
- this.height = 30;
- this.name = "RandomTreeBrushScrollablePanelTemplate";
- UIPanel panel = this.AddUIComponent<UIPanel>();
- panel.backgroundSprite = "GenericPanel";
- if(index % 2 == 0) panel.color = new Color32(90, 90, 90, 255);
- else panel.color = new Color32(130, 130, 130, 255);
- panel.width = 300;
- panel.height = 30;
- panel.relativePosition = new Vector3();
- panel.name = "RandomTreeBrushScrollablePanelTemplatePanel";
- //========CHECKBOX============
- checkBox = panel.AddUIComponent<UICheckBox>();
- checkBox.width = panel.width;
- checkBox.height = panel.height;
- checkBox.clipChildren = false;
- UISprite sprite = checkBox.AddUIComponent<UISprite>();
- //sprite.spriteName = "ToggleBase";
- sprite.spriteName = "check-unchecked";
- sprite.atlas.name = "InGame";
- sprite.size = new Vector2(16, 16);
- sprite.relativePosition = new Vector3(5, 7, 0);
- checkBox.checkedBoxObject = sprite.AddUIComponent<UISprite>();
- //((UISprite)checkBox.checkedBoxObject).spriteName = "ToggleBaseFocused";
- ((UISprite)checkBox.checkedBoxObject).spriteName = "check-checked";
- ((UISprite)checkBox.checkedBoxObject).atlas.name = "InGame";
- checkBox.checkedBoxObject.size = new Vector2(16f, 16f);
- checkBox.checkedBoxObject.relativePosition = new Vector3();
- checkBox.label = checkBox.AddUIComponent<UILabel>();
- checkBox.label.text = "";
- checkBox.label.textScale = 0.9f;
- checkBox.label.relativePosition = new Vector3(25, 9, 0);
- TreeInfo treeInfo = PrefabCollection<TreeInfo>.GetPrefab(index);
- checkBox.name = treeInfo.GetLocalizedTitle();
- checkBox.text = checkBox.name;
- checkBox.relativePosition = new Vector3();
- checkBox.eventCheckChanged += (component, check) =>
- {
- uint treeIndex = index;
- if (check)
- {
- RandomTreeBrushData.Instance.AddTree(treeIndex);
- }
- else
- {
- RandomTreeBrushData.Instance.RemoveTree(treeIndex);
- }
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment