Rewrite ProductionTabsWidget.
This commit is contained in:
@@ -15,17 +15,41 @@ using OpenRA.FileFormats;
|
|||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.Mods.RA;
|
using OpenRA.Mods.RA;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Cnc.Widgets
|
namespace OpenRA.Mods.Cnc.Widgets
|
||||||
{
|
{
|
||||||
class ProductionTabsWidget : Widget
|
class ProductionTabsWidget : Widget
|
||||||
{
|
{
|
||||||
public string QueueType = null;
|
string queueType;
|
||||||
string cachedQueueType = null;
|
public string QueueType
|
||||||
public string PaletteWidget = null;
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return queueType;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
queueType = value;
|
||||||
|
ListOffset = 0;
|
||||||
|
ResetButtons();
|
||||||
|
Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget)
|
||||||
|
.CurrentQueue = VisibleQueues.Keys.FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<Pair<Rectangle, ProductionQueue>> buttons = new List<Pair<Rectangle,ProductionQueue>>();
|
public string PaletteWidget = null;
|
||||||
List<ProductionQueue> VisibleQueues = new List<ProductionQueue>();
|
public float ScrollVelocity = 4f;
|
||||||
|
public int TabWidth = 30;
|
||||||
|
public int ArrowWidth = 20;
|
||||||
|
Dictionary<ProductionQueue, Rectangle> VisibleQueues = new Dictionary<ProductionQueue, Rectangle>();
|
||||||
|
|
||||||
|
int ContentWidth = 0;
|
||||||
|
float ListOffset = 0;
|
||||||
|
bool leftPressed = false;
|
||||||
|
bool rightPressed = false;
|
||||||
|
Rectangle leftButtonRect;
|
||||||
|
Rectangle rightButtonRect;
|
||||||
|
|
||||||
readonly World world;
|
readonly World world;
|
||||||
|
|
||||||
@@ -35,62 +59,126 @@ namespace OpenRA.Mods.Cnc.Widgets
|
|||||||
this.world = world;
|
this.world = world;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Tick()
|
public override void DrawInner()
|
||||||
|
{
|
||||||
|
var rb = RenderBounds;
|
||||||
|
|
||||||
|
leftButtonRect = new Rectangle(rb.X, rb.Y, ArrowWidth, rb.Height);
|
||||||
|
rightButtonRect = new Rectangle(rb.Right - ArrowWidth, rb.Y, ArrowWidth, rb.Height);
|
||||||
|
|
||||||
|
var leftDisabled = ListOffset >= 0;
|
||||||
|
var rightDisabled = ListOffset <= Bounds.Width - rightButtonRect.Width - leftButtonRect.Width - ContentWidth;
|
||||||
|
|
||||||
|
WidgetUtils.DrawPanel("panel-black", rb);
|
||||||
|
ButtonWidget.DrawBackground("button", leftButtonRect, leftDisabled,
|
||||||
|
leftPressed, leftButtonRect.Contains(Viewport.LastMousePos));
|
||||||
|
ButtonWidget.DrawBackground("button", rightButtonRect, rightDisabled,
|
||||||
|
rightPressed, rightButtonRect.Contains(Viewport.LastMousePos));
|
||||||
|
|
||||||
|
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", leftPressed || leftDisabled ? "up_pressed" : "up_arrow"),
|
||||||
|
new float2(leftButtonRect.Left + 2, leftButtonRect.Top + 2));
|
||||||
|
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", rightPressed || rightDisabled ? "down_pressed" : "down_arrow"),
|
||||||
|
new float2(rightButtonRect.Left + 2, rightButtonRect.Top + 2));
|
||||||
|
|
||||||
|
Game.Renderer.EnableScissor(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height);
|
||||||
|
|
||||||
|
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
|
||||||
|
// TODO: Draw children buttons
|
||||||
|
var i = 1;
|
||||||
|
foreach (var queue in VisibleQueues)
|
||||||
|
{
|
||||||
|
ButtonWidget.DrawBackground("button", queue.Value, false, queue.Key == palette.CurrentQueue, queue.Value.Contains(Viewport.LastMousePos));
|
||||||
|
|
||||||
|
SpriteFont font = Game.Renderer.Fonts["TinyBold"];
|
||||||
|
var text = i.ToString();
|
||||||
|
int2 textSize = font.Measure(text);
|
||||||
|
int2 position = new int2(queue.Value.X + (queue.Value.Width - textSize.X)/2, queue.Value.Y + (queue.Value.Height - textSize.Y)/2);
|
||||||
|
font.DrawTextWithContrast(text, position, Color.White, Color.Black, 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Game.Renderer.DisableScissor();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scroll(int direction)
|
||||||
|
{
|
||||||
|
ListOffset += direction*ScrollVelocity;
|
||||||
|
ListOffset = Math.Min(0,Math.Max(Bounds.Width - rightButtonRect.Width - leftButtonRect.Width - ContentWidth, ListOffset));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResetButtons()
|
||||||
{
|
{
|
||||||
VisibleQueues.Clear();
|
VisibleQueues.Clear();
|
||||||
|
ContentWidth = 0;
|
||||||
|
var rb = RenderBounds;
|
||||||
|
var origin = new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y);
|
||||||
|
|
||||||
VisibleQueues = world.ActorsWithTrait<ProductionQueue>()
|
var queues = world.ActorsWithTrait<ProductionQueue>()
|
||||||
.Where(p => p.Actor.Owner == world.LocalPlayer && p.Trait.Info.Type == QueueType)
|
.Where(p => p.Actor.Owner == world.LocalPlayer && p.Trait.Info.Type == QueueType)
|
||||||
.Select(p => p.Trait).ToList();
|
.Select(p => p.Trait).ToList();
|
||||||
|
|
||||||
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
|
foreach (var queue in queues)
|
||||||
if (VisibleQueues.Count() == 0)
|
|
||||||
palette.CurrentQueue = null;
|
|
||||||
else if (palette.CurrentQueue == null || cachedQueueType != QueueType)
|
|
||||||
{
|
{
|
||||||
palette.CurrentQueue = VisibleQueues.First();
|
var rect = new Rectangle(origin.X + ContentWidth, origin.Y, TabWidth, rb.Height);
|
||||||
cachedQueueType = QueueType;
|
VisibleQueues.Add(queue, rect);
|
||||||
|
ContentWidth += TabWidth - 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Tick()
|
||||||
|
{
|
||||||
|
if (leftPressed) Scroll(1);
|
||||||
|
if (rightPressed) Scroll(-1);
|
||||||
|
|
||||||
|
ResetButtons();
|
||||||
base.Tick();
|
base.Tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool LoseFocus(MouseInput mi)
|
||||||
|
{
|
||||||
|
leftPressed = rightPressed = false;
|
||||||
|
return base.LoseFocus(mi);
|
||||||
|
}
|
||||||
|
|
||||||
public override bool HandleMouseInput(MouseInput mi)
|
public override bool HandleMouseInput(MouseInput mi)
|
||||||
{
|
{
|
||||||
if (mi.Event != MouseInputEvent.Down)
|
if (mi.Button == MouseButton.WheelDown)
|
||||||
|
{
|
||||||
|
Scroll(-1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mi.Button == MouseButton.WheelUp)
|
||||||
|
{
|
||||||
|
Scroll(1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mi.Button != MouseButton.Left)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var queue = buttons.Where(a => a.First.Contains(mi.Location))
|
if (mi.Event == MouseInputEvent.Down && !TakeFocus(mi))
|
||||||
.Select(a => a.Second).FirstOrDefault();
|
return false;
|
||||||
if (queue == null)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
|
if (!Focused)
|
||||||
|
return false;
|
||||||
|
|
||||||
palette.CurrentQueue = queue;
|
if (Focused && mi.Event == MouseInputEvent.Up)
|
||||||
return true;
|
return LoseFocus(mi);
|
||||||
}
|
|
||||||
|
|
||||||
public override void DrawInner()
|
leftPressed = leftButtonRect.Contains(mi.Location.X, mi.Location.Y);
|
||||||
{
|
rightPressed = rightButtonRect.Contains(mi.Location.X, mi.Location.Y);
|
||||||
if (!IsVisible()) return;
|
|
||||||
buttons.Clear();
|
|
||||||
int x = 0;
|
|
||||||
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
|
|
||||||
|
|
||||||
// Giant hack
|
var queue = VisibleQueues.Where(a => a.Value.Contains(mi.Location))
|
||||||
var width = 30;
|
.Select(a => a.Key).FirstOrDefault();
|
||||||
|
|
||||||
foreach (var queue in VisibleQueues)
|
if (queue != null)
|
||||||
{
|
{
|
||||||
var foo = queue;
|
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
|
||||||
var rect = new Rectangle(RenderBounds.X + x,RenderBounds.Y,width, RenderBounds.Height);
|
palette.CurrentQueue = queue;
|
||||||
var state = palette.CurrentQueue == queue ? 2 :
|
|
||||||
rect.Contains(Viewport.LastMousePos) ? 1 : 0;
|
|
||||||
x += width;
|
|
||||||
|
|
||||||
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("button", "background"), new int2(rect.Location));
|
|
||||||
buttons.Add(Pair.New(rect, foo));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (leftPressed || rightPressed || queue != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -213,13 +213,13 @@ Container@INGAME_ROOT:
|
|||||||
Id:PRODUCTION_TABS
|
Id:PRODUCTION_TABS
|
||||||
PaletteWidget:PRODUCTION_PALETTE
|
PaletteWidget:PRODUCTION_PALETTE
|
||||||
X:WINDOW_RIGHT - 200
|
X:WINDOW_RIGHT - 200
|
||||||
Y:260
|
Y:259
|
||||||
Width:192
|
Width:190
|
||||||
Height:15
|
Height:20
|
||||||
ProductionPalette:
|
ProductionPalette:
|
||||||
Id:PRODUCTION_PALETTE
|
Id:PRODUCTION_PALETTE
|
||||||
X:WINDOW_RIGHT - 200
|
X:WINDOW_RIGHT - 200
|
||||||
Y:275
|
Y:280
|
||||||
TabClick: button.aud
|
TabClick: button.aud
|
||||||
Background@FMVPLAYER:
|
Background@FMVPLAYER:
|
||||||
Id:FMVPLAYER
|
Id:FMVPLAYER
|
||||||
|
|||||||
Reference in New Issue
Block a user