Button refactor

This commit is contained in:
Andy Adshead 2019-01-27 16:06:03 +00:00
commit 5fe7b87215

View file

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using OpenTK; using OpenTK;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
using Ryujinx.Profiler.UI.SharpFontHelpers; using Ryujinx.Profiler.UI.SharpFontHelpers;
@ -10,84 +7,101 @@ namespace Ryujinx.Profiler.UI
{ {
public class ProfileButton public class ProfileButton
{ {
private int Padding; // Store font service
private FontService FontService; private FontService _fontService;
private int X, Y, Right, Top, Height;
private int TextX, TextY; // Layout information
private string Label; private int _left, _right;
private Action Clicked; private int _bottom, _top;
private bool Visible; private int _height;
private int _padding;
// Label information
private int _labelX, _labelY;
private string _label;
// Misc
private Action _clicked;
private bool _visible;
public ProfileButton(FontService fontService, Action clicked) public ProfileButton(FontService fontService, Action clicked)
: this(fontService, clicked, 0, 0, 0, 0, 0) : this(fontService, clicked, 0, 0, 0, 0, 0)
{ {
Visible = false; _visible = false;
} }
public ProfileButton(FontService fontService, Action clicked, int x, int y, int padding, int height, int width) public ProfileButton(FontService fontService, Action clicked, int x, int y, int padding, int height, int width)
: this(fontService, "", clicked, x, y, padding, height, width) : this(fontService, "", clicked, x, y, padding, height, width)
{ {
Visible = false; _visible = false;
} }
public ProfileButton(FontService fontService, string label, Action clicked, int x, int y, int padding, int height, int width = -1) public ProfileButton(FontService fontService, string label, Action clicked, int x, int y, int padding, int height, int width = -1)
{ {
FontService = fontService; _fontService = fontService;
Clicked = clicked; _clicked = clicked;
UpdateSize(label, x, y, padding, height, width); UpdateSize(label, x, y, padding, height, width);
} }
public int UpdateSize(string label, int x, int y, int padding, int height, int width = -1) public int UpdateSize(string label, int x, int y, int padding, int height, int width = -1)
{ {
Visible = true; _visible = true;
Label = label; _label = label;
if (width == -1) if (width == -1)
{ {
// Dummy draw to measure size // Dummy draw to measure size
width = (int)FontService.DrawText(label, 0, 0, height, false); width = (int)_fontService.DrawText(label, 0, 0, height, false);
} }
UpdateSize(x, y, padding, width, height); UpdateSize(x, y, padding, width, height);
return Right - X;
return _right - _left;
} }
public void UpdateSize(int x, int y, int padding, int width, int height) public void UpdateSize(int x, int y, int padding, int width, int height)
{ {
Height = height; _height = height;
X = x; _left = x;
Y = y; _bottom = y;
TextX = x + padding / 2; _labelX = x + padding / 2;
TextY = y + padding / 2; _labelY = y + padding / 2;
Top = y + height + padding; _top = y + height + padding;
Right = x + width + padding; _right = x + width + padding;
} }
public void Draw() public void Draw()
{ {
if (!Visible) if (!_visible)
{ {
return; return;
} }
// Draw backing rectangle
GL.Begin(PrimitiveType.Triangles); GL.Begin(PrimitiveType.Triangles);
GL.Color3(Color.Black); GL.Color3(Color.Black);
GL.Vertex2(X, Y); GL.Vertex2(_left, _bottom);
GL.Vertex2(X, Top); GL.Vertex2(_left, _top);
GL.Vertex2(Right, Top); GL.Vertex2(_right, _top);
GL.Vertex2(Right, Top); GL.Vertex2(_right, _top);
GL.Vertex2(Right, Y); GL.Vertex2(_right, _bottom);
GL.Vertex2(X, Y); GL.Vertex2(_left, _bottom);
GL.End(); GL.End();
FontService.DrawText(Label, TextX, TextY, Height); // Use font service to draw label
_fontService.DrawText(_label, _labelX, _labelY, _height);
} }
public bool ProcessClick(int x, int y) public bool ProcessClick(int x, int y)
{ {
if (x > X && x < Right && // If button contains x, y
y > Y && y < Top) if (x > _left && x < _right &&
y > _bottom && y < _top)
{ {
Clicked(); _clicked();
return true; return true;
} }