Cnc scrollpanel with support for rollover etc.

This commit is contained in:
Paul Chote
2011-05-07 21:43:32 +12:00
parent d349c5341e
commit 188a16fc08
6 changed files with 84 additions and 16 deletions

View File

@@ -22,15 +22,15 @@ namespace OpenRA.Widgets
public int ItemSpacing = 2;
public int ContentHeight = 0;
float ListOffset = 0;
bool UpPressed = false;
bool DownPressed = false;
bool ThumbPressed = false;
Rectangle upButtonRect;
Rectangle downButtonRect;
Rectangle backgroundRect;
Rectangle scrollbarRect;
Rectangle thumbRect;
protected float ListOffset = 0;
protected bool UpPressed = false;
protected bool DownPressed = false;
protected bool ThumbPressed = false;
protected Rectangle upButtonRect;
protected Rectangle downButtonRect;
protected Rectangle backgroundRect;
protected Rectangle scrollbarRect;
protected Rectangle thumbRect;
public ScrollPanelWidget() : base() {}
protected ScrollPanelWidget(ScrollPanelWidget other)
@@ -115,7 +115,7 @@ namespace OpenRA.Widgets
void Scroll(int direction)
{
ListOffset += direction*ScrollVelocity;
ListOffset = Math.Min(0,Math.Max(RenderBounds.Height - ContentHeight, ListOffset));
ListOffset = Math.Min(0,Math.Max(Bounds.Height - ContentHeight, ListOffset));
}
public override void Tick ()

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Cnc.Widgets
public class CncLobbyLogic : IWidgetDelegate
{
Widget LocalPlayerTemplate, RemotePlayerTemplate, EmptySlotTemplate, EmptySlotTemplateHost;
ScrollPanelWidget chatPanel;
CncScrollPanelWidget chatPanel;
Widget chatTemplate;
ScrollPanelWidget Players;
@@ -185,7 +185,7 @@ namespace OpenRA.Mods.Cnc.Widgets
return true;
};
chatPanel = lobby.GetWidget<ScrollPanelWidget>("CHAT_DISPLAY");
chatPanel = lobby.GetWidget<CncScrollPanelWidget>("CHAT_DISPLAY");
chatTemplate = chatPanel.GetWidget("CHAT_TEMPLATE");
}
@@ -218,6 +218,7 @@ namespace OpenRA.Mods.Cnc.Widgets
template.Bounds.Height += (t.Bounds.Height - oldHeight);
chatPanel.AddChild(template);
chatPanel.ScrollToBottom();
}
void UpdatePlayerColor(float hf, float sf, float lf, float r)

View File

@@ -82,5 +82,72 @@ namespace OpenRA.Mods.Cnc.Widgets
new float2(rect.Left + 2, rect.Top + 2));
}
}
public class CncScrollPanelWidget : ScrollPanelWidget
{
public CncScrollPanelWidget()
: base() { }
protected CncScrollPanelWidget(CncScrollPanelWidget widget)
: base(widget) { }
public override void Draw()
{
if (!IsVisible())
return;
var rb = RenderBounds;
var ScrollbarHeight = rb.Height - 2 * ScrollbarWidth;
var thumbHeight = ContentHeight == 0 ? 0 : (int)(ScrollbarHeight*Math.Min(rb.Height*1f/ContentHeight, 1f));
var thumbOrigin = rb.Y + ScrollbarWidth + (int)((ScrollbarHeight - thumbHeight)*(-1f*ListOffset/(ContentHeight - rb.Height)));
if (thumbHeight == ScrollbarHeight)
thumbHeight = 0;
backgroundRect = new Rectangle(rb.X, rb.Y, rb.Width - ScrollbarWidth + 1, rb.Height);
upButtonRect = new Rectangle(rb.Right - ScrollbarWidth, rb.Y, ScrollbarWidth, ScrollbarWidth);
downButtonRect = new Rectangle(rb.Right - ScrollbarWidth, rb.Bottom - ScrollbarWidth, ScrollbarWidth, ScrollbarWidth);
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 = "panel-gray";
string thumbBg = (Focused && thumbRect.Contains(Viewport.LastMousePos)) ? "button-pressed" :
thumbRect.Contains(Viewport.LastMousePos) ? "button-hover" : "button";
WidgetUtils.DrawPanel(scrollbarBg, scrollbarRect);
WidgetUtils.DrawPanel("panel-gray", backgroundRect);
WidgetUtils.DrawPanel(upButtonBg, upButtonRect);
WidgetUtils.DrawPanel(downButtonBg, downButtonRect);
if (thumbHeight > 0)
WidgetUtils.DrawPanel(thumbBg, thumbRect);
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", "up_arrow"),
new float2(upButtonRect.Left + 4, upButtonRect.Top + 4));
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", "down_arrow"),
new float2(downButtonRect.Left + 4, downButtonRect.Top + 4));
Game.Renderer.EnableScissor(backgroundRect.X + 1, backgroundRect.Y + 1, backgroundRect.Width - 2, backgroundRect.Height - 2);
foreach (var child in Children)
child.Draw();
Game.Renderer.DisableScissor();
}
public void ScrollToBottom()
{
ListOffset = Math.Min(0,Bounds.Height - ContentHeight);
}
}
}

View File

@@ -45,7 +45,7 @@ Container@SERVER_LOBBY:
Width:130
Height:20
Text: Allow Cheats
ScrollPanel@PLAYERS:
CncScrollPanel@PLAYERS:
Id:PLAYERS
X:15
Y:30
@@ -323,7 +323,7 @@ Container@SERVER_LOBBY:
Height:25
Align:Right
Text:Chat:
ScrollPanel@CHAT_DISPLAY:
CncScrollPanel@CHAT_DISPLAY:
Id:CHAT_DISPLAY
X:15
Y:285

View File

@@ -31,7 +31,7 @@ Container@MAP_CHOOSER:
Y:1
Width:192
Height:192
ScrollPanel@MAP_LIST:
CncScrollPanel@MAP_LIST:
Id:MAP_LIST
X:15
Y:30

View File

@@ -18,7 +18,7 @@ Container@SERVERBROWSER_PANEL:
Height:500
Background:panel-black
Children:
ScrollPanel@SERVER_LIST:
CncScrollPanel@SERVER_LIST:
Id:SERVER_LIST
X:15
Y:30