Remove some duplication from widgets that draw fake buttons.

This commit is contained in:
Paul Chote
2011-05-17 13:14:48 +12:00
parent b3f9725872
commit 14af766427
4 changed files with 21 additions and 35 deletions

View File

@@ -99,15 +99,10 @@ namespace OpenRA.Widgets
Font = "Bold";
var font = Game.Renderer.Fonts[Font];
var state = IsDisabled() ? "button-disabled" :
Depressed ? "button-pressed" :
rb.Contains(Viewport.LastMousePos) ? "button-hover" :
"button";
WidgetUtils.DrawPanel(state, rb);
var text = GetText();
var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
DrawBackground(rb, IsDisabled(), Depressed, rb.Contains(Viewport.LastMousePos));
font.DrawText(text,
new int2(rb.X + UsableWidth / 2, rb.Y + Bounds.Height / 2)
- new int2(font.Measure(text).X / 2,
@@ -116,6 +111,16 @@ namespace OpenRA.Widgets
public override Widget Clone() { return new ButtonWidget(this); }
public virtual int UsableWidth { get { return Bounds.Width; } }
public static void DrawBackground(Rectangle rect, bool disabled, bool pressed, bool hover)
{
var state = disabled ? "button-disabled" :
pressed ? "button-pressed" :
hover ? "button-hover" :
"button";
WidgetUtils.DrawPanel(state, rect);
}
}
public class DropDownButtonWidget : ButtonWidget

View File

@@ -77,26 +77,16 @@ namespace OpenRA.Widgets
scrollbarRect = new Rectangle(rb.Right - ScrollbarWidth, rb.Y + ScrollbarWidth - 1, ScrollbarWidth, ScrollbarHeight + 2);
thumbRect = new Rectangle(rb.Right - ScrollbarWidth, thumbOrigin, ScrollbarWidth, thumbHeight);
string upButtonBg = (thumbHeight == 0 || ListOffset >= 0) ? "button-disabled" :
UpPressed ? "button-pressed" :
upButtonRect.Contains(Viewport.LastMousePos) ? "button-hover" : "button";
string downButtonBg = (thumbHeight == 0 || ListOffset <= Bounds.Height - ContentHeight) ? "button-disabled" :
DownPressed ? "button-pressed" :
downButtonRect.Contains(Viewport.LastMousePos) ? "button-hover" : "button";
string scrollbarBg = "scrollpanel-bg";
string thumbBg = (Focused && thumbRect.Contains(Viewport.LastMousePos)) ? "button-pressed" :
thumbRect.Contains(Viewport.LastMousePos) ? "button-hover" : "button";
WidgetUtils.DrawPanel(Background, backgroundRect);
WidgetUtils.DrawPanel(upButtonBg, upButtonRect);
WidgetUtils.DrawPanel(downButtonBg, downButtonRect);
WidgetUtils.DrawPanel(scrollbarBg, scrollbarRect);
WidgetUtils.DrawPanel("scrollpanel-bg", scrollbarRect);
ButtonWidget.DrawBackground(upButtonRect, (thumbHeight == 0 || ListOffset >= 0),
UpPressed, upButtonRect.Contains(Viewport.LastMousePos));
ButtonWidget.DrawBackground(downButtonRect, (thumbHeight == 0 || ListOffset <= Bounds.Height - ContentHeight),
DownPressed, downButtonRect.Contains(Viewport.LastMousePos));
if (thumbHeight > 0)
WidgetUtils.DrawPanel(thumbBg, thumbRect);
ButtonWidget.DrawBackground(thumbRect, false, (Focused && thumbRect.Contains(Viewport.LastMousePos)),
thumbRect.Contains(Viewport.LastMousePos));
var upOffset = !UpPressed || thumbHeight == 0 || ListOffset >= 0 ? 4 : 4 + ButtonDepth;
var downOffset = !DownPressed || thumbHeight == 0 || ListOffset <= Bounds.Height - ContentHeight

View File

@@ -197,12 +197,7 @@ namespace OpenRA.Widgets
WidgetUtils.DrawPanel("slider-track", trackRect);
// Thumb
var state = IsDisabled() ? "button-disabled" :
isMoving ? "button-pressed" :
tr.Contains(Viewport.LastMousePos) ? "button-hover" :
"button";
WidgetUtils.DrawPanel(state, tr);
ButtonWidget.DrawBackground(tr, IsDisabled(), isMoving, tr.Contains(Viewport.LastMousePos));
}
}
}

View File

@@ -28,14 +28,10 @@ namespace OpenRA.Mods.Cnc.Widgets
public override void DrawInner()
{
var state = IsDisabled() ? "button-disabled" :
Depressed ? "button-pressed" :
RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" :
"button";
var font = Game.Renderer.Fonts[Font];
var rect = RenderBounds;
WidgetUtils.DrawPanel(state, new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height)));
ButtonWidget.DrawBackground(new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height)),
IsDisabled(), Depressed, RenderBounds.Contains(Viewport.LastMousePos));
var textSize = font.Measure(Text);
font.DrawText(Text,