mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
replace onclicked with much easier to make compatible onpressed
This commit is contained in:
parent
f4e6097602
commit
765acc3f62
6 changed files with 46 additions and 54 deletions
|
@ -74,19 +74,13 @@ namespace Demos {
|
|||
// Setting the x or y coordinate of the size to 1 or a lower number causes the width or height to be a percentage of the parent's width or height
|
||||
// (for example, setting the size's x to 0.75 would make the element's width be 0.75*parentWidth)
|
||||
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Toggle Test Image", "This button shows a grass tile as a test image to show the automatic anchoring of objects.") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
image.IsHidden = !image.IsHidden;
|
||||
}
|
||||
OnPressed = element => image.IsHidden = !image.IsHidden
|
||||
});
|
||||
|
||||
root.AddChild(new VerticalSpace(3));
|
||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Note that the default style does not contain any textures or font files and, as such, is quite bland. However, the default style is quite easy to override."));
|
||||
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle;
|
||||
},
|
||||
OnPressed = element => this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle,
|
||||
PositionOffset = new Vector2(0, 1),
|
||||
// set HasCustomStyle to true before changing style information so that, when changing the style globally
|
||||
// (like above), these custom values don't get undone
|
||||
|
@ -113,13 +107,13 @@ namespace Demos {
|
|||
root.AddChild(new VerticalSpace(3));
|
||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:"));
|
||||
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") {
|
||||
OnClicked = (element, button) => {
|
||||
OnPressed = element => {
|
||||
if (element.Root.Scale < 2)
|
||||
element.Root.Scale += 0.1F;
|
||||
}
|
||||
});
|
||||
root.AddChild(new Button(Anchor.AutoInline, new Vector2(10), "-") {
|
||||
OnClicked = (element, button) => {
|
||||
OnPressed = element => {
|
||||
if (element.Root.Scale > 0.5F)
|
||||
element.Root.Scale -= 0.1F;
|
||||
},
|
||||
|
@ -138,10 +132,7 @@ namespace Demos {
|
|||
this.UiSystem.Add("TestTooltip", tooltip);
|
||||
root.AddChild(new VerticalSpace(3));
|
||||
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Test Tooltip") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
tooltip.IsHidden = !tooltip.IsHidden;
|
||||
}
|
||||
OnPressed = element => tooltip.IsHidden = !tooltip.IsHidden
|
||||
});
|
||||
|
||||
var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1);
|
||||
|
@ -151,7 +142,7 @@ namespace Demos {
|
|||
// This button uses a coroutine from my Coroutine NuGet package (which is included with MLEM.Startup)
|
||||
// but the important thing it does is change its visual scale and offset (check the method below for more info)
|
||||
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Wobble", "This button wobbles around visually when clicked, but this doesn't affect its actual size and positioning") {
|
||||
OnClicked = (element, button) => CoroutineHandler.Start(this.WobbleButton(element)),
|
||||
OnPressed = element => CoroutineHandler.Start(this.WobbleButton(element)),
|
||||
PositionOffset = new Vector2(0, 1)
|
||||
});
|
||||
|
||||
|
@ -166,10 +157,7 @@ namespace Demos {
|
|||
cols[2].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the third column"));
|
||||
|
||||
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Show Info Box") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
ElementHelper.ShowInfoBox(this.UiSystem, Anchor.Center, 100, "This is an easy info box that you can open with just one line of code! It automatically closes when you press the button below as well.");
|
||||
},
|
||||
OnPressed = element => ElementHelper.ShowInfoBox(this.UiSystem, Anchor.Center, 100, "This is an easy info box that you can open with just one line of code! It automatically closes when you press the button below as well."),
|
||||
PositionOffset = new Vector2(0, 1)
|
||||
});
|
||||
|
||||
|
|
|
@ -29,11 +29,7 @@ namespace MLEM.Ui.Elements {
|
|||
|
||||
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
|
||||
this.checced = defaultChecked;
|
||||
|
||||
this.OnClicked += (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
this.Checked = !this.Checked;
|
||||
};
|
||||
this.OnPressed += element => this.Checked = !this.Checked;
|
||||
|
||||
if (label != null) {
|
||||
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
|
||||
|
|
|
@ -79,9 +79,12 @@ namespace MLEM.Ui.Elements {
|
|||
public Vector2 AddedDisplayScale;
|
||||
public Point ScaledDisplayScale => (this.AddedDisplayScale * this.Scale).ToPoint();
|
||||
|
||||
public MouseClickCallback OnClicked;
|
||||
public GenericCallback OnPressed;
|
||||
public GenericCallback OnSecondaryPressed;
|
||||
public GenericCallback OnSelected;
|
||||
public GenericCallback OnDeselected;
|
||||
|
||||
public MouseClickCallback OnClicked;
|
||||
public MouseCallback OnMouseEnter;
|
||||
public MouseCallback OnMouseExit;
|
||||
public TextInputCallback OnTextInput;
|
||||
|
|
|
@ -8,10 +8,7 @@ namespace MLEM.Ui.Elements {
|
|||
var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true);
|
||||
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));
|
||||
box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, buttonHeight), okText) {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
system.Remove("InfoBox");
|
||||
},
|
||||
OnPressed = element => system.Remove("InfoBox"),
|
||||
PositionOffset = new Vector2(0, 1)
|
||||
});
|
||||
system.Add("InfoBox", box);
|
||||
|
@ -38,25 +35,21 @@ namespace MLEM.Ui.Elements {
|
|||
group.OnAreaUpdated += e => field.Size = new Vector2((e.Area.Width - e.Area.Height / 2) / e.Scale, 1);
|
||||
|
||||
var upButton = new Button(Anchor.TopRight, Vector2.One, "+") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left) {
|
||||
OnPressed = element => {
|
||||
var text = field.Text.ToString();
|
||||
if (int.TryParse(text, out var val))
|
||||
field.SetText(val + stepPerClick);
|
||||
}
|
||||
}
|
||||
};
|
||||
group.AddChild(upButton);
|
||||
group.OnAreaUpdated += e => upButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
||||
|
||||
var downButton = new Button(Anchor.BottomRight, Vector2.One, "-") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left) {
|
||||
OnPressed = element => {
|
||||
var text = field.Text.ToString();
|
||||
if (int.TryParse(text, out var val))
|
||||
field.SetText(val - stepPerClick);
|
||||
}
|
||||
}
|
||||
};
|
||||
group.AddChild(downButton);
|
||||
group.OnAreaUpdated += e => downButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
||||
|
|
|
@ -12,14 +12,12 @@ namespace MLEM.Ui.Elements {
|
|||
this.Group = group;
|
||||
|
||||
// don't += because we want to override the checking + unchecking behavior of Checkbox
|
||||
this.OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left) {
|
||||
this.OnPressed = element => {
|
||||
this.Checked = true;
|
||||
foreach (var sib in this.GetSiblings(true)) {
|
||||
if (sib is RadioButton radio && radio.Group == this.Group)
|
||||
radio.Checked = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,9 @@ namespace MLEM.Ui {
|
|||
public BlendState BlendState;
|
||||
public SamplerState SamplerState = SamplerState.PointClamp;
|
||||
|
||||
public MouseButton MainButton = MouseButton.Left;
|
||||
public MouseButton SecondaryButton = MouseButton.Right;
|
||||
|
||||
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
|
||||
this.GraphicsDevice = device;
|
||||
this.InputHandler = inputHandler ?? new InputHandler();
|
||||
|
@ -74,6 +77,7 @@ namespace MLEM.Ui {
|
|||
this.InputHandler.Update();
|
||||
|
||||
var mousedNow = this.GetMousedElement();
|
||||
// mouse new element
|
||||
if (mousedNow != this.MousedElement) {
|
||||
if (this.MousedElement != null)
|
||||
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement);
|
||||
|
@ -82,7 +86,9 @@ namespace MLEM.Ui {
|
|||
this.MousedElement = mousedNow;
|
||||
}
|
||||
|
||||
if (this.SelectedElement != mousedNow && this.InputHandler.IsMouseButtonPressed(MouseButton.Left)) {
|
||||
if (this.InputHandler.IsMouseButtonPressed(this.MainButton)) {
|
||||
// select element
|
||||
if (this.SelectedElement != mousedNow) {
|
||||
if (this.SelectedElement != null)
|
||||
this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement);
|
||||
if (mousedNow != null)
|
||||
|
@ -90,6 +96,14 @@ namespace MLEM.Ui {
|
|||
this.SelectedElement = mousedNow;
|
||||
}
|
||||
|
||||
// first action on element
|
||||
mousedNow?.OnPressed?.Invoke(mousedNow);
|
||||
} else if (this.InputHandler.IsMouseButtonPressed(this.SecondaryButton)) {
|
||||
// secondary action on element
|
||||
mousedNow?.OnSecondaryPressed?.Invoke(mousedNow);
|
||||
}
|
||||
|
||||
// generic element click
|
||||
if (mousedNow?.OnClicked != null) {
|
||||
foreach (var button in InputHandler.MouseButtons) {
|
||||
if (this.InputHandler.IsMouseButtonPressed(button))
|
||||
|
|
Loading…
Reference in a new issue