Been experimenting with doing UI from lua the last twenty minutes. Seeing what it's possible to achieve.
Here's some stuff to go on.
Here is c# code for the dialog boxes used in the game, should be possible to sort out something like this using luanet
public class DialogResultOK : UiEventArgs
{
}
public class DialogResultCancel : UiEventArgs
{
}
class Dialog : Window
{
public enum DialogResult
{
NotFinished,
OK,
Cancel
}
public DialogResult Result = DialogResult.NotFinished;
string mSingleMessage;
bool mCloseOnOK = false;
public bool CloseOnOK
{
get { return mCloseOnOK; }
set { mCloseOnOK = value; }
}
public Dialog(string message)
: this(message, true, false)
{
mCloseOnOK = true;
}
public Dialog(List<string> message, bool OK, bool Cancel)
: base(Game.UI, null, new Rectangle(0, 0, 300, 128))
{
string singlemessage = "";
foreach (string s in message)
{
singlemessage += s + "\n";
}
float textScale = 2;
SetMessage(singlemessage, textScale);
Init(OK, Cancel);
}
public Dialog(string message, bool OK, bool Cancel)
: base(Game.UI, null, new Rectangle(0, 0, 300, 400))
{
float textScale = 2;
SetMessage(message, textScale);
Init(OK, Cancel);
}
private void SetMessage(string message, float textScale)
{
mSingleMessage = message;
int width = 300;
while (width <= 600)
{
int height = (int)Manager.Font.GetStringHeight(mSingleMessage, new RectangleF(0, 0, width - 32, Manager.Rect.Height), textScale);
if (height < 400)
{
Width = width;
Height = height;
break;
}
width += 25;
}
Width += 32;
Height += 128 + 32;
}
private void Init(bool OK, bool Cancel)
{
AnchorH = AnchorH.Centre;
AnchorV = AnchorV.Centre;
HasCloseButton = false;
Moveable = false;
Resizeable = false;
int bw = 96;
int sep = 8;
if (!OK && !Cancel) OK = true;
Wuwei.UI.Button ok = null;
if (OK)
{
ok = new Wuwei.UI.Button(Manager, Pane, new Rectangle(0, 0, bw, 64));
ok.OnReleasedMessage = new DialogResultOK().WithDestinationWidget(this);
ok.Text = Game.GetString("OK");
ok.TextSize = 2;
ok.AnchorV = AnchorV.Bottom;
ok.AnchorH = AnchorH.Centre;
ok.AnchorOffsetBottom = 4;
if (Cancel)
{
ok.AnchorOffsetRight = (uint)(bw + sep) / 2;
}
Pane.AddWidget(ok);
}
Wuwei.UI.Button cancel = null;
if (Cancel)
{
cancel = new Wuwei.UI.Button(Manager, Pane, new Rectangle(0, 0, bw, 64));
cancel.OnReleasedMessage = new DialogResultCancel().WithDestinationWidget(this);
cancel.Text = Game.GetString("Cancel");
cancel.TextSize = 2;
cancel.AnchorV = AnchorV.Bottom;
cancel.AnchorH = AnchorH.Centre;
cancel.AnchorOffsetBottom = 4;
if (OK)
{
cancel.AnchorOffsetLeft = (uint)(bw + sep) / 2;
}
Pane.AddWidget(cancel);
}
X = (Game.Instance.ScreenWidth / 2) - (Width / 2);
Y = (Game.Instance.ScreenHeight / 2) - (Height / 2);
UiEventArgs m = new OnResizeEventArgs().WithDestinationWidget(this);
ReceiveMessage(this, m);
Update();
if (ok != null)
{
ok.ReceiveMessage(this, m);
ok.Update();
}
if (cancel != null)
{
cancel.ReceiveMessage(this, m);
cancel.Update();
}
ReceiveMessage(this, new OnResizeEventArgs());
}
public override void Draw()
{
base.Draw();
if (mSingleMessage != null)
{
RectangleF rect = WorldRect;
rect.Width -= 32;
rect.X += 16;
rect.Y += 16;
rect.Height -= 72;
Manager.Font.Draw(mSingleMessage, rect, Wuwei.Drawing.BitmapFont.HorizontalJustification.Left, Wuwei.Drawing.BitmapFont.VerticalJustification.Top, 0, new ColorF(1, 1, 1, DrawAlpha), 2.0f, true);
Wuwei.Drawing.Batcher.Instance.Flush();
}
}
public override bool ReceiveMessage(Widget SourceWidget, UiEventArgs message)
{
if (message is DialogResultOK)
{
Result = DialogResult.OK;
if (mCloseOnOK)
{
ReceiveMessage(this, new CloseEventArgs());
}
return true;
}
else if (message is DialogResultCancel)
{
Result = DialogResult.Cancel;
return true;
}
return base.ReceiveMessage(SourceWidget, message);
}
}