AGS: Refactor GUISlider::Draw(), make it easier to understand

From upstream 9394a2df433f63a4753e1dbbaf5a2a6fdff6e69a
This commit is contained in:
Paul Gilbert
2022-04-22 21:18:01 -07:00
parent f9921b0112
commit dec832ad4f
2 changed files with 60 additions and 53 deletions
+58 -53
View File
@@ -41,6 +41,7 @@ GUISlider::GUISlider() {
_scEventCount = 1;
_scEventNames[0] = "Change";
_scEventArgs[0] = "GUIControl *control";
_handleRange = 0;
}
bool GUISlider::IsHorizontal() const {
@@ -56,51 +57,61 @@ bool GUISlider::IsOverControl(int x, int y, int leeway) const {
}
void GUISlider::Draw(Shared::Bitmap *ds) {
Rect bar;
Rect handle;
int thickness;
// Clamp Value
// TODO: this is necessary here because some Slider fields are still public
if (MinValue >= MaxValue)
MaxValue = MinValue + 1;
Value = Math::Clamp(Value, MinValue, MaxValue);
// Test if sprite is available
// TODO: react to sprites initialization/deletion instead!
if (_GP(spriteset)[HandleImage] == nullptr)
HandleImage = 0;
// it's a horizontal slider
if (IsHorizontal()) {
thickness = Height / 3;
bar.Left = X + 1;
bar.Top = Y + Height / 2 - thickness;
bar.Right = X + Width - 1;
bar.Bottom = Y + Height / 2 + thickness + 1;
handle.Left = (int)(((float)(Value - MinValue) / (float)(MaxValue - MinValue)) * (float)(Width - 4) - 2) + bar.Left + 1;
handle.Top = bar.Top - (thickness - 1);
handle.Right = handle.Left + get_fixed_pixel_size(4);
handle.Bottom = bar.Bottom + (thickness - 1);
if (HandleImage > 0) {
// store the centre of the pic rather than the top
handle.Top = bar.Top + (bar.Bottom - bar.Top) / 2 + get_fixed_pixel_size(1);
handle.Left += get_fixed_pixel_size(2);
}
handle.Top += data_to_game_coord(HandleOffset);
handle.Bottom += data_to_game_coord(HandleOffset);
// Depending on slider's orientation, thickness is either Height or Width
const int thickness = IsHorizontal() ? Height : Width;
// "thick_f" is the factor for calculating relative element positions
const int thick_f = thickness / 3; // one third of the control's thickness
// Bar thickness
const int bar_thick = thick_f * 2 + 2;
// Calculate handle size
Size handle_sz;
if (HandleImage > 0) // handle is a sprite
{
handle_sz = Size(get_adjusted_spritewidth(HandleImage),
get_adjusted_spriteheight(HandleImage));
} else // handle is a drawn rectangle
{
if (IsHorizontal())
handle_sz = Size(get_fixed_pixel_size(4) + 1, bar_thick + (thick_f - 1) * 2);
else
handle_sz = Size(bar_thick + (thick_f - 1) * 2, get_fixed_pixel_size(4) + 1);
}
// Calculate bar and handle positions
Rect bar;
Rect handle;
int handle_range;
if (IsHorizontal()) // horizontal slider
{
// Value pos is a coordinate corresponding to current slider's value
bar = RectWH(X + 1, Y + Height / 2 - thick_f, Width - 1, bar_thick);
handle_range = Width - 4;
int value_pos = (int)(((float)(Value - MinValue) * (float)handle_range) / (float)(MaxValue - MinValue));
handle = RectWH((bar.Left + get_fixed_pixel_size(2)) - (handle_sz.Width / 2) + 1 + value_pos - 2,
bar.Top + (bar.GetHeight() - handle_sz.Height) / 2,
handle_sz.Width, handle_sz.Height);
handle.MoveToY(handle.Top + data_to_game_coord(HandleOffset));
}
// vertical slider
else {
thickness = Width / 3;
bar.Left = X + Width / 2 - thickness;
bar.Top = Y + 1;
bar.Right = X + Width / 2 + thickness + 1;
bar.Bottom = Y + Height - 1;
handle.Top = (int)(((float)(MaxValue - Value) / (float)(MaxValue - MinValue)) * (float)(Height - 4) - 2) + bar.Top + 1;
handle.Left = bar.Left - (thickness - 1);
handle.Bottom = handle.Top + get_fixed_pixel_size(4);
handle.Right = bar.Right + (thickness - 1);
if (HandleImage > 0) {
// store the centre of the pic rather than the left
handle.Left = bar.Left + (bar.Right - bar.Left) / 2 + get_fixed_pixel_size(1);
handle.Top += get_fixed_pixel_size(2);
}
handle.Left += data_to_game_coord(HandleOffset);
handle.Right += data_to_game_coord(HandleOffset);
bar = RectWH(X + Width / 2 - thick_f, Y + 1, bar_thick, Height - 1);
handle_range = Height - 4;
int value_pos = (int)(((float)(MaxValue - Value) * (float)handle_range) / (float)(MaxValue - MinValue));
handle = RectWH(bar.Left + (bar.GetWidth() - handle_sz.Width) / 2,
(bar.Top + get_fixed_pixel_size(2)) - (handle_sz.Height / 2) + 1 + value_pos - 2,
handle_sz.Width, handle_sz.Height);
handle.MoveToX(handle.Left + data_to_game_coord(HandleOffset));
}
color_t draw_color;
@@ -129,7 +140,7 @@ void GUISlider::Draw(Shared::Bitmap *ds) {
} else {
// normal grey background
draw_color = ds->GetCompatibleColor(16);
ds->FillRect(Rect(bar.Left + 1, bar.Top + 1, bar.Right - 1, bar.Bottom - 1), draw_color);
ds->FillRect(bar, draw_color);
draw_color = ds->GetCompatibleColor(8);
ds->DrawLine(Line(bar.Left, bar.Top, bar.Left, bar.Bottom), draw_color);
ds->DrawLine(Line(bar.Left, bar.Top, bar.Right, bar.Top), draw_color);
@@ -138,21 +149,14 @@ void GUISlider::Draw(Shared::Bitmap *ds) {
ds->DrawLine(Line(bar.Left, bar.Bottom, bar.Right, bar.Bottom), draw_color);
}
if (HandleImage > 0) {
// an image for the slider handle
// TODO: react to sprites initialization/deletion instead!
if (_GP(spriteset)[HandleImage] == nullptr)
HandleImage = 0;
handle.Left -= get_adjusted_spritewidth(HandleImage) / 2;
handle.Top -= get_adjusted_spriteheight(HandleImage) / 2;
if (HandleImage > 0) // handle is a sprite
{
draw_gui_sprite(ds, HandleImage, handle.Left, handle.Top, true);
handle.Right = handle.Left + get_adjusted_spritewidth(HandleImage);
handle.Bottom = handle.Top + get_adjusted_spriteheight(HandleImage);
} else {
} else // handle is a drawn rectangle
{
// normal grey tracker handle
draw_color = ds->GetCompatibleColor(7);
ds->FillRect(Rect(handle.Left, handle.Top, handle.Right, handle.Bottom), draw_color);
ds->FillRect(handle, draw_color);
draw_color = ds->GetCompatibleColor(15);
ds->DrawLine(Line(handle.Left, handle.Top, handle.Right, handle.Top), draw_color);
ds->DrawLine(Line(handle.Left, handle.Top, handle.Left, handle.Bottom), draw_color);
@@ -162,6 +166,7 @@ void GUISlider::Draw(Shared::Bitmap *ds) {
}
_cachedHandle = handle;
_handleRange = handle_range;
}
bool GUISlider::OnMouseDown() {
@@ -175,9 +180,9 @@ void GUISlider::OnMouseMove(int x, int y) {
return;
if (IsHorizontal())
Value = (int)(((float)((x - X) - 2) / (float)(Width - 4)) * (float)(MaxValue - MinValue)) + MinValue;
Value = (int)(((float)((x - X) - 2) * (float)(MaxValue - MinValue)) / (float)_handleRange) + MinValue;
else
Value = (int)(((float)(((Y + Height) - y) - 2) / (float)(Height - 4)) * (float)(MaxValue - MinValue)) + MinValue;
Value = (int)(((float)(((Y + Height) - y) - 2) * (float)(MaxValue - MinValue)) / (float)_handleRange) + MinValue;
Value = Math::Clamp(Value, MinValue, MaxValue);
NotifyParentChanged();
+2
View File
@@ -65,6 +65,8 @@ private:
// The following variables are not persisted on disk
// Cached coordinates of slider handle
Rect _cachedHandle;
// The length of the handle movement range, in pixels
int _handleRange;
};
} // namespace Shared