-
Notifications
You must be signed in to change notification settings - Fork 324
/
Checkbox.cs
168 lines (133 loc) · 4.76 KB
/
Checkbox.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using DaggerfallWorkshop.Game.UserInterfaceWindows;
namespace DaggerfallWorkshop.Game.UserInterface
{
public class Checkbox : Panel
{
#region Fields
const string uncheckedTextureFilename = "checkbox_unchecked";
const string checkedTextureFilename = "checkbox_checked";
int checkTextHorzOffset = 2;
int checkTextVertOffset = 1;
TextLabel label = new TextLabel();
bool isChecked = false;
Color checkboxColor = Color.white;
Texture2D uncheckedTexture = null;
Texture2D checkedTexture = null;
Vector2 checkTextureSize = Vector2.zero;
bool previousSDFState;
public delegate void OnToggleStateHandler();
public event OnToggleStateHandler OnToggleState;
#endregion
#region Properties
public Color CheckBoxColor
{
get { return checkboxColor; }
set { checkboxColor = value; }
}
public TextLabel Label
{
get { return label; }
}
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; }
}
public int TextHorzOffset
{
get { return checkTextHorzOffset; }
set { checkTextHorzOffset = value; }
}
public int TextVertOffset
{
get { return checkTextVertOffset; }
set { checkTextVertOffset = value; }
}
#endregion
#region Constructors
public Checkbox()
: base()
{
label.Parent = this;
label.ShadowPosition = Vector2.zero;
OnMouseClick += Checkbox_OnMouseClick;
checkboxColor = DaggerfallUI.DaggerfallDefaultTextColor;
LoadResources();
}
#endregion
#region Overrides
public override void Update()
{
base.Update();
if (label.Font != null && label.Font.IsSDFCapable != previousSDFState)
{
label.RefreshLayout();
previousSDFState = label.Font.IsSDFCapable;
}
float width = checkTextureSize.x + checkTextHorzOffset + label.Size.x;
float height = Mathf.Max(checkTextureSize.y, label.Size.y);
Size = new Vector2(width, height);
}
public override void Draw()
{
base.Draw();
// Exit if textures not set
if (checkTextureSize == Vector2.zero ||
uncheckedTexture == null ||
checkedTexture == null)
{
return;
}
// Calculate rect and align checkbox or text to tallest element
Rect rect = Rectangle;
float checkHeight = checkTextureSize.y;
float textHeight = label.Size.y;
float textOffset = 0;
if (checkHeight > textHeight)
textOffset = (checkHeight - textHeight) / 2;
else
rect.y += ((Size.y - checkTextureSize.y) / 2f) * LocalScale.y;
rect.width = checkTextureSize.x * LocalScale.x;
rect.height = checkTextureSize.y * LocalScale.y;
// Draw checkbox in current state
if (!isChecked)
DaggerfallUI.DrawTexture(rect, uncheckedTexture, ScaleMode.StretchToFill, true, checkboxColor);
else
DaggerfallUI.DrawTexture(rect, checkedTexture, ScaleMode.StretchToFill, true, checkboxColor);
// Draw label
label.Position = new Vector2(checkTextureSize.x + checkTextHorzOffset, textOffset + checkTextVertOffset);
label.Draw();
}
#endregion
#region Private Methods
void LoadResources()
{
uncheckedTexture = Resources.Load<Texture2D>(uncheckedTextureFilename);
checkedTexture = Resources.Load<Texture2D>(checkedTextureFilename);
checkTextureSize = new Vector2(uncheckedTexture.width, uncheckedTexture.height);
}
#endregion
#region Event Handlers
private void Checkbox_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
isChecked = !isChecked;
if (OnToggleState != null)
OnToggleState();
}
#endregion
}
}