Skip to content

Commit

Permalink
Implement scrolling DaggerfallMessageBox
Browse files Browse the repository at this point in the history
Also add a quick hacky fix for correctly aligning single button.
  • Loading branch information
Interkarma committed May 5, 2020
1 parent 8a6803a commit f148f77
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Assets/Scripts/Game/UserInterface/MultiFormatTextLabel.cs
Expand Up @@ -47,6 +47,8 @@ public class MultiFormatTextLabel : BaseScreenComponent
bool wrapText = false;
bool wrapWords = false;
int maxTextWidth = 0;
int maxTextHeight = 0;
int actualTextHeight = 0;

int minTextureDimTextLabel = TextLabel.limitMinTextureDim; // set this with property MinTextureDim to higher values if you experience scaling issues with small texts (e.g. inventory infopanel)

Expand Down Expand Up @@ -74,6 +76,17 @@ public int MaxTextWidth
set { maxTextWidth = value; }
}

public int MaxTextHeight
{
get { return maxTextHeight; }
set { maxTextHeight = value; }
}

public int ActualTextHeight
{
get { return actualTextHeight; }
}

/// <summary>
/// used to set min texture dims of textlabel to higher values if there would be aspect or scaling issues with small texts otherwise (e.g. some single-lined textlabels in inventory infopanel)
/// </summary>
Expand Down Expand Up @@ -264,6 +277,28 @@ public int LineHeight
}
}

public void UpdateRestrictedRenderArea()
{
for (int i = 0; i < labels.Count; i++)
{
TextLabel textLabel = labels[i];
textLabel.RestrictedRenderAreaCoordinateType = RestrictedRenderAreaCoordinateType;
textLabel.RectRestrictedRenderArea = RectRestrictedRenderArea;
textLabel.RestrictedRenderAreaCustomParent = RestrictedRenderAreaCustomParent;
labels[i] = textLabel;
}
}

public void ChangeScrollPosition(int amount)
{
for (int i = 0; i < labels.Count; i++)
{
TextLabel textLabel = labels[i];
textLabel.Position = new Vector2(textLabel.Position.x, textLabel.Position.y + amount);
labels[i] = textLabel;
}
}

#region Protected Methods

protected virtual void SetRowLeading(int amount)
Expand Down Expand Up @@ -348,6 +383,12 @@ void LayoutTextElements(TextFile.Token[] tokens)
}
}

if (maxTextHeight > 0 && totalHeight > maxTextHeight)
{
actualTextHeight = totalHeight;
totalHeight = maxTextHeight;
}

Size = new Vector2(totalWidth, totalHeight);
}

Expand Down
72 changes: 72 additions & 0 deletions Assets/Scripts/Game/UserInterfaceWindows/DaggerfallMessageBox.cs
Expand Up @@ -31,7 +31,9 @@ public class DaggerfallMessageBox : DaggerfallPopupWindow

Panel imagePanel = new Panel();
Panel messagePanel = new Panel();
Panel scrollingPanel = new Panel();
Panel buttonPanel = new Panel();
VerticalScrollBar scrollBar = new VerticalScrollBar();
MultiFormatTextLabel label = new MultiFormatTextLabel();
List<Button> buttons = new List<Button>();
int buttonSpacing = 32;
Expand Down Expand Up @@ -213,6 +215,12 @@ protected override void Setup()
DaggerfallUI.Instance.SetDaggerfallPopupStyle(DaggerfallUI.PopupStyle.Parchment, messagePanel);
NativePanel.Components.Add(messagePanel);

scrollingPanel.HorizontalAlignment = HorizontalAlignment.Center;
scrollingPanel.VerticalAlignment = VerticalAlignment.Top;
scrollingPanel.OnMouseScrollUp += ScrollingPanel_OnMouseScrollUp;
scrollingPanel.OnMouseScrollDown += ScrollingPanel_OnMouseScrollDown;
messagePanel.Components.Add(scrollingPanel);

label.HorizontalAlignment = HorizontalAlignment.Center;
label.VerticalAlignment = VerticalAlignment.Middle;
messagePanel.Components.Add(label);
Expand All @@ -225,6 +233,11 @@ protected override void Setup()
imagePanel.VerticalAlignment = VerticalAlignment.Top;
messagePanel.Components.Add(imagePanel);

scrollBar.HorizontalAlignment = HorizontalAlignment.Right;
scrollBar.VerticalAlignment = VerticalAlignment.Top;
scrollBar.OnScroll += ScrollBar_OnScroll;
messagePanel.Components.Add(scrollBar);

IsSetup = true;
}

Expand Down Expand Up @@ -402,6 +415,7 @@ public void SetTextTokens(int id, IMacroContextProvider mcp = null)

TextFile.Token[] tokens = DaggerfallUnity.Instance.TextProvider.GetRSCTokens(id);
SetTextTokens(tokens, mcp);
UpdatePanelSizes();
}

/// <summary>
Expand All @@ -412,6 +426,24 @@ public void SetHighlightColor(Color highlightColor)
label.HighlightColor = highlightColor;
}

/// <summary>
/// Enables vertical scrolling of message panel.
/// Width is determined by widest text line as with non-scrolling message box.
/// Must call this before setting text.
/// </summary>
/// <param name="height">Capped height of visible area of message panel. Anything past this size will become srollable.</param>
public void EnableVerticalScrolling(int height)
{
if (height > 0)
{
label.MaxTextHeight = height;
label.RestrictedRenderAreaCoordinateType = BaseScreenComponent.RestrictedRenderArea_CoordinateType.CustomParent;
label.RestrictedRenderAreaCustomParent = scrollingPanel;
label.UpdateRestrictedRenderArea();
}
UpdatePanelSizes();
}

#endregion

#region Private Methods
Expand Down Expand Up @@ -447,9 +479,12 @@ void UpdatePanelSizes()
buttonPanel.Size = finalSize;

// Position buttons to be buttonTextDistance pixels below the repositioned text
// HACK: Lower vertical position if only a single button so that it aligns like two or more buttons
if (buttons.Count > 0)
{
float buttonY = messagePanel.Size.y - ((messagePanel.Size.y - label.Size.y) / 2) - buttonPanel.Size.y - messagePanel.BottomMargin;
if (buttons.Count == 1)
buttonY += 11;
buttonPanel.Position = new Vector2(buttonPanel.Position.x, buttonY);
}

Expand All @@ -473,6 +508,24 @@ void UpdatePanelSizes()
height = minimum;

messagePanel.Size = new Vector2(width, height);

if (label.MaxTextHeight > 0 && label.ActualTextHeight > label.MaxTextHeight)
{
scrollingPanel.Size = new Vector2(label.Size.x, GetScrollingPanelHeight());
scrollBar.Enabled = true;
scrollBar.Size = new Vector2(8, GetScrollingPanelHeight());
scrollBar.TotalUnits = label.ActualTextHeight + 1;
scrollBar.DisplayUnits = GetScrollingPanelHeight();
}
else
{
scrollBar.Enabled = false;
}
}

int GetScrollingPanelHeight()
{
return label.MaxTextHeight + 9;
}

void SetupBox(TextFile.Token[] tokens, CommonMessageBoxButtons buttons, IMacroContextProvider mcp = null)
Expand Down Expand Up @@ -532,6 +585,25 @@ private void ParentPanel_OnMouseClick(BaseScreenComponent sender, Vector2 positi
}
}

int lastScrollIndex = 0;
int currentScrollIndex = 0;
private void ScrollBar_OnScroll()
{
lastScrollIndex = currentScrollIndex;
currentScrollIndex = scrollBar.ScrollIndex;
label.ChangeScrollPosition(lastScrollIndex - currentScrollIndex);
}

private void ScrollingPanel_OnMouseScrollUp(BaseScreenComponent sender)
{
scrollBar.ScrollIndex -= 6;
}

private void ScrollingPanel_OnMouseScrollDown(BaseScreenComponent sender)
{
scrollBar.ScrollIndex += 6;
}

#endregion

#region Events
Expand Down

0 comments on commit f148f77

Please sign in to comment.