Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class ToolModeSelector : MonoBehaviour
{
private Button[] Buttons;
// Start is called before the first frame update
void Start()
{
//Requires buttons to be in the same order as the toolmode enum
//We could fully generate the buttons instead if we have icons with file names matching the enums
var buttons = GetComponentsInChildren<Button>();
Buttons = buttons.OrderBy(x => x.transform.position.x).ToArray();
for(int i = 0; i< Buttons.Length;++i)
{
int copiedIndex = i; //this is important
var button = Buttons[i];
button.onClick.AddListener(()=> Select(copiedIndex));
}
Buttons[(int)CommunicationEvents.ActiveToolMode].transform.localScale *= 2;
}
public void Select(int id)
{
Buttons[(int)CommunicationEvents.ActiveToolMode].transform.localScale /= 2;
CommunicationEvents.ToolModeChangedEvent.Invoke((ToolMode)id);
Buttons[(int)CommunicationEvents.ActiveToolMode].transform.localScale *= 2;
}
// Update is called once per frame
void Update()
{ //Check if the ToolMode was switched
CheckToolModeSelection();
}
//Checks if the ToolMode was switched by User, and handle it
void CheckToolModeSelection()
{
if (Input.GetButtonDown("ToolMode"))
{
ToolMode tempActiveToolMode = CommunicationEvents.ActiveToolMode;
int id = ((int)tempActiveToolMode + 1) % System.Enum.GetNames(typeof(ToolMode)).Length;
// tempActiveToolMode =(ToolMode) id ;
//Invoke the Handler for the Facts
// CommunicationEvents.ToolModeChangedEvent.Invoke(tempActiveToolMode);
Select(id);
}else if(Input.GetAxis("Mouse ScrollWheel") !=0){
int move = (int) Mathf.Sign(Input.GetAxis("Mouse ScrollWheel"));
ToolMode tempActiveToolMode = CommunicationEvents.ActiveToolMode;
int id = ((int)tempActiveToolMode + move) % System.Enum.GetNames(typeof(ToolMode)).Length;
if (id < 0) id = System.Enum.GetNames(typeof(ToolMode)).Length-1;
Select(id);
}
}
}