mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +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
|
// 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)
|
// (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.") {
|
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) => {
|
OnPressed = element => image.IsHidden = !image.IsHidden
|
||||||
if (button == MouseButton.Left)
|
|
||||||
image.IsHidden = !image.IsHidden;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
root.AddChild(new VerticalSpace(3));
|
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 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") {
|
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle,
|
||||||
if (button == MouseButton.Left)
|
|
||||||
this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle;
|
|
||||||
},
|
|
||||||
PositionOffset = new Vector2(0, 1),
|
PositionOffset = new Vector2(0, 1),
|
||||||
// set HasCustomStyle to true before changing style information so that, when changing the style globally
|
// set HasCustomStyle to true before changing style information so that, when changing the style globally
|
||||||
// (like above), these custom values don't get undone
|
// (like above), these custom values don't get undone
|
||||||
|
@ -113,13 +107,13 @@ namespace Demos {
|
||||||
root.AddChild(new VerticalSpace(3));
|
root.AddChild(new VerticalSpace(3));
|
||||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:"));
|
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:"));
|
||||||
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") {
|
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => {
|
||||||
if (element.Root.Scale < 2)
|
if (element.Root.Scale < 2)
|
||||||
element.Root.Scale += 0.1F;
|
element.Root.Scale += 0.1F;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
root.AddChild(new Button(Anchor.AutoInline, new Vector2(10), "-") {
|
root.AddChild(new Button(Anchor.AutoInline, new Vector2(10), "-") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => {
|
||||||
if (element.Root.Scale > 0.5F)
|
if (element.Root.Scale > 0.5F)
|
||||||
element.Root.Scale -= 0.1F;
|
element.Root.Scale -= 0.1F;
|
||||||
},
|
},
|
||||||
|
@ -138,10 +132,7 @@ namespace Demos {
|
||||||
this.UiSystem.Add("TestTooltip", tooltip);
|
this.UiSystem.Add("TestTooltip", tooltip);
|
||||||
root.AddChild(new VerticalSpace(3));
|
root.AddChild(new VerticalSpace(3));
|
||||||
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Test Tooltip") {
|
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Test Tooltip") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => tooltip.IsHidden = !tooltip.IsHidden
|
||||||
if (button == MouseButton.Left)
|
|
||||||
tooltip.IsHidden = !tooltip.IsHidden;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1);
|
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)
|
// 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)
|
// 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") {
|
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)
|
PositionOffset = new Vector2(0, 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -166,10 +157,7 @@ namespace Demos {
|
||||||
cols[2].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the third column"));
|
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") {
|
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Show Info Box") {
|
||||||
OnClicked = (element, button) => {
|
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."),
|
||||||
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.");
|
|
||||||
},
|
|
||||||
PositionOffset = new Vector2(0, 1)
|
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) {
|
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
|
||||||
this.checced = defaultChecked;
|
this.checced = defaultChecked;
|
||||||
|
this.OnPressed += element => this.Checked = !this.Checked;
|
||||||
this.OnClicked += (element, button) => {
|
|
||||||
if (button == MouseButton.Left)
|
|
||||||
this.Checked = !this.Checked;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (label != null) {
|
if (label != null) {
|
||||||
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
|
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
|
||||||
|
|
|
@ -79,9 +79,12 @@ namespace MLEM.Ui.Elements {
|
||||||
public Vector2 AddedDisplayScale;
|
public Vector2 AddedDisplayScale;
|
||||||
public Point ScaledDisplayScale => (this.AddedDisplayScale * this.Scale).ToPoint();
|
public Point ScaledDisplayScale => (this.AddedDisplayScale * this.Scale).ToPoint();
|
||||||
|
|
||||||
public MouseClickCallback OnClicked;
|
public GenericCallback OnPressed;
|
||||||
|
public GenericCallback OnSecondaryPressed;
|
||||||
public GenericCallback OnSelected;
|
public GenericCallback OnSelected;
|
||||||
public GenericCallback OnDeselected;
|
public GenericCallback OnDeselected;
|
||||||
|
|
||||||
|
public MouseClickCallback OnClicked;
|
||||||
public MouseCallback OnMouseEnter;
|
public MouseCallback OnMouseEnter;
|
||||||
public MouseCallback OnMouseExit;
|
public MouseCallback OnMouseExit;
|
||||||
public TextInputCallback OnTextInput;
|
public TextInputCallback OnTextInput;
|
||||||
|
|
|
@ -8,10 +8,7 @@ namespace MLEM.Ui.Elements {
|
||||||
var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true);
|
var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true);
|
||||||
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));
|
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));
|
||||||
box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, buttonHeight), okText) {
|
box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, buttonHeight), okText) {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => system.Remove("InfoBox"),
|
||||||
if (button == MouseButton.Left)
|
|
||||||
system.Remove("InfoBox");
|
|
||||||
},
|
|
||||||
PositionOffset = new Vector2(0, 1)
|
PositionOffset = new Vector2(0, 1)
|
||||||
});
|
});
|
||||||
system.Add("InfoBox", box);
|
system.Add("InfoBox", box);
|
||||||
|
@ -38,24 +35,20 @@ namespace MLEM.Ui.Elements {
|
||||||
group.OnAreaUpdated += e => field.Size = new Vector2((e.Area.Width - e.Area.Height / 2) / e.Scale, 1);
|
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, "+") {
|
var upButton = new Button(Anchor.TopRight, Vector2.One, "+") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => {
|
||||||
if (button == MouseButton.Left) {
|
var text = field.Text.ToString();
|
||||||
var text = field.Text.ToString();
|
if (int.TryParse(text, out var val))
|
||||||
if (int.TryParse(text, out var val))
|
field.SetText(val + stepPerClick);
|
||||||
field.SetText(val + stepPerClick);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
group.AddChild(upButton);
|
group.AddChild(upButton);
|
||||||
group.OnAreaUpdated += e => upButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
group.OnAreaUpdated += e => upButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
||||||
|
|
||||||
var downButton = new Button(Anchor.BottomRight, Vector2.One, "-") {
|
var downButton = new Button(Anchor.BottomRight, Vector2.One, "-") {
|
||||||
OnClicked = (element, button) => {
|
OnPressed = element => {
|
||||||
if (button == MouseButton.Left) {
|
var text = field.Text.ToString();
|
||||||
var text = field.Text.ToString();
|
if (int.TryParse(text, out var val))
|
||||||
if (int.TryParse(text, out var val))
|
field.SetText(val - stepPerClick);
|
||||||
field.SetText(val - stepPerClick);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
group.AddChild(downButton);
|
group.AddChild(downButton);
|
||||||
|
|
|
@ -12,13 +12,11 @@ namespace MLEM.Ui.Elements {
|
||||||
this.Group = group;
|
this.Group = group;
|
||||||
|
|
||||||
// don't += because we want to override the checking + unchecking behavior of Checkbox
|
// don't += because we want to override the checking + unchecking behavior of Checkbox
|
||||||
this.OnClicked = (element, button) => {
|
this.OnPressed = element => {
|
||||||
if (button == MouseButton.Left) {
|
this.Checked = true;
|
||||||
this.Checked = true;
|
foreach (var sib in this.GetSiblings(true)) {
|
||||||
foreach (var sib in this.GetSiblings(true)) {
|
if (sib is RadioButton radio && radio.Group == this.Group)
|
||||||
if (sib is RadioButton radio && radio.Group == this.Group)
|
radio.Checked = false;
|
||||||
radio.Checked = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,9 @@ namespace MLEM.Ui {
|
||||||
public BlendState BlendState;
|
public BlendState BlendState;
|
||||||
public SamplerState SamplerState = SamplerState.PointClamp;
|
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) {
|
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
|
||||||
this.GraphicsDevice = device;
|
this.GraphicsDevice = device;
|
||||||
this.InputHandler = inputHandler ?? new InputHandler();
|
this.InputHandler = inputHandler ?? new InputHandler();
|
||||||
|
@ -74,6 +77,7 @@ namespace MLEM.Ui {
|
||||||
this.InputHandler.Update();
|
this.InputHandler.Update();
|
||||||
|
|
||||||
var mousedNow = this.GetMousedElement();
|
var mousedNow = this.GetMousedElement();
|
||||||
|
// mouse new element
|
||||||
if (mousedNow != this.MousedElement) {
|
if (mousedNow != this.MousedElement) {
|
||||||
if (this.MousedElement != null)
|
if (this.MousedElement != null)
|
||||||
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement);
|
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement);
|
||||||
|
@ -82,14 +86,24 @@ namespace MLEM.Ui {
|
||||||
this.MousedElement = mousedNow;
|
this.MousedElement = mousedNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.SelectedElement != mousedNow && this.InputHandler.IsMouseButtonPressed(MouseButton.Left)) {
|
if (this.InputHandler.IsMouseButtonPressed(this.MainButton)) {
|
||||||
if (this.SelectedElement != null)
|
// select element
|
||||||
this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement);
|
if (this.SelectedElement != mousedNow) {
|
||||||
if (mousedNow != null)
|
if (this.SelectedElement != null)
|
||||||
mousedNow.OnSelected?.Invoke(mousedNow);
|
this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement);
|
||||||
this.SelectedElement = mousedNow;
|
if (mousedNow != null)
|
||||||
|
mousedNow.OnSelected?.Invoke(mousedNow);
|
||||||
|
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) {
|
if (mousedNow?.OnClicked != null) {
|
||||||
foreach (var button in InputHandler.MouseButtons) {
|
foreach (var button in InputHandler.MouseButtons) {
|
||||||
if (this.InputHandler.IsMouseButtonPressed(button))
|
if (this.InputHandler.IsMouseButtonPressed(button))
|
||||||
|
|
Loading…
Reference in a new issue