-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumteHudUI.cs
More file actions
147 lines (114 loc) · 4.78 KB
/
NumteHudUI.cs
File metadata and controls
147 lines (114 loc) · 4.78 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Nethereum.Contracts.UnityERC1155;
using UnityEditor;
namespace Nethereum.Unity.Behaviours.UI
{
public class NumteHudUI : MonoBehaviour
{
[SerializeField]
EthereumAccountBehaviour _accountBehaviour;
[SerializeField]
Text _nameText;
[SerializeField]
Text _contractText;
[SerializeField]
Text _tokenText;
[SerializeField]
Text _tokensOwnedText;
[SerializeField]
Text _latestTransactionsText;
int _targetTokenIndex = 0;
long _targetTokenId = 0;
List<System.Numerics.BigInteger> _tokensAvailable = new List<System.Numerics.BigInteger>();
private float _timePassedInSeconds = 0.0f;
private int _lastTimeThreshold = 0;
// Start is called before the first frame update
void Start()
{
UpdateTokenInfo();
UpdateDisplay();
}
// Update is called once per frame
void Update()
{
AdjustTimer();
}
private void AdjustTimer()
{
_timePassedInSeconds += Time.deltaTime;
int timePassed = (int)_timePassedInSeconds;
if ((timePassed > 1) && (timePassed > _lastTimeThreshold))
{
_lastTimeThreshold = timePassed;
UpdateTokenInfo();
UpdateDisplay();
}
}
public void DecrementTokenIndex()
{
_targetTokenIndex = (_targetTokenIndex-1) < 0 ? 0 : (_targetTokenIndex - 1);
Debug.Log("DEBUG: NumteHudUI::DecrementTokenIndex() -> Incremented index to [" + _targetTokenIndex + "]");
}
public void IncrementTokenIndex()
{
Debug.Log("DEBUG: NumteHudUI::IncrementTokenIndex() -> The tokens with available balances are: (" +
String.Join(",", _tokensAvailable) + ")");
_targetTokenIndex =
(_targetTokenIndex+1) < _tokensAvailable.Count ? (_targetTokenIndex+1) : _targetTokenIndex;
Debug.Log("DEBUG: NumteHudUI::IncrementTokenIndex() -> Incremented index to [" + _targetTokenIndex + "]");
}
public void RefundTokens()
{
if ((_accountBehaviour != null) && (_targetTokenId > 0))
{
System.Numerics.BigInteger targetTokenIdBI = _targetTokenId;
var tokensOwned = _accountBehaviour.GetTokensOwned(targetTokenIdBI);
if (tokensOwned > 0)
{
if (EditorUtility.DisplayDialog("Refund tokens?",
"Are you sure you want to refund [" + tokensOwned + "] tokens?",
"Refund Them", "Keep Them"))
{
Debug.Log("NumteHudSimpleUI::RefundTokens() -> ID[" + _targetTokenId +
"], Balance[" + tokensOwned + "]");
_accountBehaviour.RefundOwnedTokens(_targetTokenId, tokensOwned);
}
}
}
}
private void UpdateDisplay()
{
if ((_accountBehaviour != null) && (_targetTokenId > 0))
{
System.Numerics.BigInteger targetTokenIdBI = _targetTokenId;
_nameText.text = _accountBehaviour.Name;
_contractText.text = _accountBehaviour.Contract.GetRootNode().ContractName;
_tokenText.text = _accountBehaviour.GetTokenName(targetTokenIdBI);
Debug.Log("NumteHudSimpleUI::UpdateDisplay() -> Name(" + _nameText.text +
"), Contract(" + _contractText.text + "), Token(" + _tokenText.text + ")");
var tokensOwned = _accountBehaviour.GetTokensOwned(targetTokenIdBI);
if (tokensOwned > 0)
{
_tokensOwnedText.text = Convert.ToString(tokensOwned);
}
_latestTransactionsText.text = _accountBehaviour.GetUserFriendlyLatestTransactionsReport();
}
}
private void UpdateTokenInfo()
{
if (_accountBehaviour != null)
{
_tokensAvailable = _accountBehaviour.GetTokenIdsWithBalances();
Debug.Log("DEBUG: NumteHudUI::UpdateTokenInfo() -> The tokens with available balances are: (" +
String.Join(",", _tokensAvailable) + ")");
_targetTokenId =
(_tokensAvailable.Count > 0) ?
UnityERC1155ServiceFactory.ConvertBigIntegerToLong(_tokensAvailable[_targetTokenIndex]) : 0;
}
}
}
}