Skip to content

Commit d76ad80

Browse files
committed
TF editor improvements: Made it easier to select control points, especially at the borders.
1 parent ba54467 commit d76ad80

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

Assets/Editor/TransferFunctionEditorWindow.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ private void OnGUI()
6969

7070
Color oldColour = GUI.color; // Used for setting GUI.color when drawing UI elements
7171

72-
float contentWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 100.0f) * 2.0f);
72+
float contentWidth = Mathf.Min(this.position.width, (this.position.height - 100.0f) * 2.0f);
7373
float contentHeight = contentWidth * 0.5f;
7474

75-
Rect bgRect = new Rect(0.0f, 0.0f, contentWidth, contentHeight);
76-
Rect paletteRect = new Rect(bgRect.x, bgRect.y + bgRect.height + 20, contentWidth, 20.0f);
75+
// Interaction area (slightly larger than the histogram rect)
76+
Rect interactRect = new Rect(0.0f, 0.0f, contentWidth, contentHeight);
77+
// Histogram rect (histogram view and alpha control points)
78+
Rect histRect = new Rect(interactRect.x + 20.0f, interactRect.y + 20.0f, interactRect.width - 40.0f, interactRect.height - 40.0f);
79+
// Colour palette rect (colour control points)
80+
Rect paletteRect = new Rect(histRect.x, histRect.y + histRect.height + 20, histRect.width, 20.0f);
7781

7882
// TODO: Don't do this every frame
7983
tf.GenerateTexture();
@@ -90,23 +94,23 @@ private void OnGUI()
9094
// Draw histogram
9195
tfGUIMat.SetTexture("_TFTex", tf.GetTexture());
9296
tfGUIMat.SetTexture("_HistTex", histTex);
93-
Graphics.DrawTexture(bgRect, tf.GetTexture(), tfGUIMat);
97+
Graphics.DrawTexture(histRect, tf.GetTexture(), tfGUIMat);
9498

9599
// Draw colour palette
96100
Texture2D tfTexture = tf.GetTexture();
97101
tfPaletteGUIMat.SetTexture("_TFTex", tf.GetTexture());
98102
Graphics.DrawTexture(new Rect(paletteRect.x, paletteRect.y, paletteRect.width, paletteRect.height), tfTexture, tfPaletteGUIMat);
99103

100104
// Release selected colour/alpha points if mouse leaves window
101-
if (movingAlphaPointIndex != -1 && !bgRect.Contains(currentEvent.mousePosition))
105+
if (movingAlphaPointIndex != -1 && !interactRect.Contains(currentEvent.mousePosition))
102106
movingAlphaPointIndex = -1;
103-
if (movingColPointIndex != -1 && !(currentEvent.mousePosition.x >= bgRect.x && currentEvent.mousePosition.x <= bgRect.x + bgRect.width))
107+
if (movingColPointIndex != -1 && !(currentEvent.mousePosition.x >= paletteRect.x && currentEvent.mousePosition.x <= paletteRect.x + paletteRect.width))
104108
movingColPointIndex = -1;
105109

106110
// Mouse down => Move or remove selected colour control point
107111
if (currentEvent.type == EventType.MouseDown && paletteRect.Contains(currentEvent.mousePosition))
108112
{
109-
float mousePos = (currentEvent.mousePosition.x - bgRect.x) / bgRect.width;
113+
float mousePos = (currentEvent.mousePosition.x - paletteRect.x) / paletteRect.width;
110114
int pointIndex = PickColourControlPoint(mousePos);
111115
if (pointIndex != -1)
112116
{
@@ -130,7 +134,7 @@ private void OnGUI()
130134
// Mouse down => Move or remove selected alpha control point
131135
if (currentEvent.type == EventType.MouseDown)
132136
{
133-
Vector2 mousePos = new Vector2((currentEvent.mousePosition.x - bgRect.x) / bgRect.width, 1.0f - (currentEvent.mousePosition.y - bgRect.y) / bgRect.height);
137+
Vector2 mousePos = new Vector2((currentEvent.mousePosition.x - histRect.x) / histRect.width, 1.0f - (currentEvent.mousePosition.y - histRect.y) / histRect.height);
134138
int pointIndex = PickAlphaControlPoint(mousePos);
135139
if (pointIndex != -1)
136140
{
@@ -153,24 +157,24 @@ private void OnGUI()
153157
if (movingAlphaPointIndex != -1)
154158
{
155159
TFAlphaControlPoint alphaPoint = tf.alphaControlPoints[movingAlphaPointIndex];
156-
alphaPoint.dataValue = Mathf.Clamp((currentEvent.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f);
157-
alphaPoint.alphaValue = Mathf.Clamp(1.0f - (currentEvent.mousePosition.y - bgRect.y) / bgRect.height, 0.0f, 1.0f);
160+
alphaPoint.dataValue = Mathf.Clamp((currentEvent.mousePosition.x - histRect.x) / histRect.width, 0.0f, 1.0f);
161+
alphaPoint.alphaValue = Mathf.Clamp(1.0f - (currentEvent.mousePosition.y - histRect.y) / histRect.height, 0.0f, 1.0f);
158162
tf.alphaControlPoints[movingAlphaPointIndex] = alphaPoint;
159163
}
160164

161165
// Move selected colour control point
162166
if (movingColPointIndex != -1)
163167
{
164168
TFColourControlPoint colPoint = tf.colourControlPoints[movingColPointIndex];
165-
colPoint.dataValue = Mathf.Clamp((currentEvent.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f);
169+
colPoint.dataValue = Mathf.Clamp((currentEvent.mousePosition.x - paletteRect.x) / paletteRect.width, 0.0f, 1.0f);
166170
tf.colourControlPoints[movingColPointIndex] = colPoint;
167171
}
168172

169173
// Draw colour control points
170174
for (int iCol = 0; iCol < tf.colourControlPoints.Count; iCol++)
171175
{
172176
TFColourControlPoint colPoint = tf.colourControlPoints[iCol];
173-
Rect ctrlBox = new Rect(bgRect.x + bgRect.width * colPoint.dataValue, bgRect.y + bgRect.height + 20, 10, 20);
177+
Rect ctrlBox = new Rect(histRect.x + histRect.width * colPoint.dataValue, histRect.y + histRect.height + 20, 10, 20);
174178
GUI.color = Color.red;
175179
GUI.skin.box.fontSize = 6;
176180
GUI.Box(ctrlBox, "*");
@@ -179,11 +183,13 @@ private void OnGUI()
179183
// Draw alpha control points
180184
for (int iAlpha = 0; iAlpha < tf.alphaControlPoints.Count; iAlpha++)
181185
{
186+
const int pointSize = 10;
182187
TFAlphaControlPoint alphaPoint = tf.alphaControlPoints[iAlpha];
183-
Rect ctrlBox = new Rect(bgRect.x + bgRect.width * alphaPoint.dataValue, bgRect.y + (1.0f - alphaPoint.alphaValue) * bgRect.height, 10, 10);
184-
GUI.color = oldColour;
188+
Rect ctrlBox = new Rect(histRect.x + histRect.width * alphaPoint.dataValue - pointSize / 2, histRect.y + (1.0f - alphaPoint.alphaValue) * histRect.height - pointSize / 2, pointSize, pointSize);
189+
GUI.color = Color.red;
185190
GUI.skin.box.fontSize = 6;
186191
GUI.Box(ctrlBox, "*");
192+
GUI.color = oldColour;
187193
}
188194

189195
if (currentEvent.type == EventType.MouseUp)
@@ -195,23 +201,23 @@ private void OnGUI()
195201
// Add points
196202
if (currentEvent.type == EventType.MouseDown && currentEvent.button == 1)
197203
{
198-
if (bgRect.Contains(new Vector2(currentEvent.mousePosition.x, currentEvent.mousePosition.y)))
199-
tf.alphaControlPoints.Add(new TFAlphaControlPoint(Mathf.Clamp((currentEvent.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f), Mathf.Clamp(1.0f - (currentEvent.mousePosition.y - bgRect.y) / bgRect.height, 0.0f, 1.0f)));
204+
if (histRect.Contains(new Vector2(currentEvent.mousePosition.x, currentEvent.mousePosition.y)))
205+
tf.alphaControlPoints.Add(new TFAlphaControlPoint(Mathf.Clamp((currentEvent.mousePosition.x - histRect.x) / histRect.width, 0.0f, 1.0f), Mathf.Clamp(1.0f - (currentEvent.mousePosition.y - histRect.y) / histRect.height, 0.0f, 1.0f)));
200206
else
201-
tf.colourControlPoints.Add(new TFColourControlPoint(Mathf.Clamp((currentEvent.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f), Random.ColorHSV()));
207+
tf.colourControlPoints.Add(new TFColourControlPoint(Mathf.Clamp((currentEvent.mousePosition.x - histRect.x) / histRect.width, 0.0f, 1.0f), Random.ColorHSV()));
202208
selectedColPointIndex = -1;
203209
}
204210

205211
// Save TF
206-
if(GUI.Button(new Rect(0.0f, bgRect.y + bgRect.height + 50.0f, 70.0f, 30.0f), "Save"))
212+
if(GUI.Button(new Rect(histRect.x, histRect.y + histRect.height + 50.0f, 70.0f, 30.0f), "Save"))
207213
{
208214
string filepath = EditorUtility.SaveFilePanel("Save transfer function", "", "default.tf", "tf");
209215
if(filepath != "")
210216
TransferFunctionDatabase.SaveTransferFunction(tf, filepath);
211217
}
212218

213219
// Load TF
214-
if(GUI.Button(new Rect(75.0f, bgRect.y + bgRect.height + 50.0f, 70.0f, 30.0f), "Load"))
220+
if(GUI.Button(new Rect(histRect.x + 75.0f, histRect.y + histRect.height + 50.0f, 70.0f, 30.0f), "Load"))
215221
{
216222
string filepath = EditorUtility.OpenFilePanel("Save transfer function", "", "tf");
217223
if(filepath != "")
@@ -222,7 +228,7 @@ private void OnGUI()
222228
}
223229
}
224230
// Clear TF
225-
if(GUI.Button(new Rect(150.0f, bgRect.y + bgRect.height + 50.0f, 70.0f, 30.0f), "Clear"))
231+
if(GUI.Button(new Rect(histRect.x + 150.0f, histRect.y + histRect.height + 50.0f, 70.0f, 30.0f), "Clear"))
226232
{
227233
tf = volRendObject.transferFunction = new TransferFunction();
228234
tf.alphaControlPoints.Add(new TFAlphaControlPoint(0.1f, 0.0f));
@@ -235,12 +241,12 @@ private void OnGUI()
235241
if (selectedColPointIndex != -1)
236242
{
237243
TFColourControlPoint colPoint = tf.colourControlPoints[selectedColPointIndex];
238-
colPoint.colourValue = EditorGUI.ColorField(new Rect(225, bgRect.y + bgRect.height + 50, 100.0f, 40.0f), colPoint.colourValue);
244+
colPoint.colourValue = EditorGUI.ColorField(new Rect(histRect.x + 225, histRect.y + histRect.height + 50, 100.0f, 40.0f), colPoint.colourValue);
239245
tf.colourControlPoints[selectedColPointIndex] = colPoint;
240246
}
241247

242248
GUI.skin.label.wordWrap = false;
243-
GUI.Label(new Rect(0.0f, bgRect.y + bgRect.height + 85.0f, 700.0f, 30.0f), "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete.");
249+
GUI.Label(new Rect(histRect.x, histRect.y + histRect.height + 85.0f, 720.0f, 30.0f), "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete.");
244250

245251
GUI.color = oldColour;
246252
}

0 commit comments

Comments
 (0)