mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
more easily allow controls to be changed
This commit is contained in:
parent
ed7865379d
commit
cc20682d47
4 changed files with 24 additions and 23 deletions
|
@ -84,9 +84,8 @@ namespace MLEM.Ui.Elements {
|
||||||
public GenericCallback OnSelected;
|
public GenericCallback OnSelected;
|
||||||
public GenericCallback OnDeselected;
|
public GenericCallback OnDeselected;
|
||||||
|
|
||||||
public MouseClickCallback OnClicked;
|
public GenericCallback OnMouseEnter;
|
||||||
public MouseCallback OnMouseEnter;
|
public GenericCallback OnMouseExit;
|
||||||
public MouseCallback OnMouseExit;
|
|
||||||
public TextInputCallback OnTextInput;
|
public TextInputCallback OnTextInput;
|
||||||
public GenericCallback OnAreaUpdated;
|
public GenericCallback OnAreaUpdated;
|
||||||
|
|
||||||
|
@ -100,6 +99,7 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected InputHandler Input => this.System.InputHandler;
|
protected InputHandler Input => this.System.InputHandler;
|
||||||
|
protected UiControls Controls => this.System.Controls;
|
||||||
public RootElement Root { get; private set; }
|
public RootElement Root { get; private set; }
|
||||||
public float Scale => this.Root.ActualScale;
|
public float Scale => this.Root.ActualScale;
|
||||||
public Point MousePos => this.Input.MousePosition;
|
public Point MousePos => this.Input.MousePosition;
|
||||||
|
@ -445,10 +445,6 @@ namespace MLEM.Ui.Elements {
|
||||||
protected virtual void InitStyle(UiStyle style) {
|
protected virtual void InitStyle(UiStyle style) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void MouseClickCallback(Element element, MouseButton button);
|
|
||||||
|
|
||||||
public delegate void MouseCallback(Element element);
|
|
||||||
|
|
||||||
public delegate void TextInputCallback(Element element, Keys key, char character);
|
public delegate void TextInputCallback(Element element, Keys key, char character);
|
||||||
|
|
||||||
public delegate void GenericCallback(Element element);
|
public delegate void GenericCallback(Element element);
|
||||||
|
|
|
@ -48,9 +48,9 @@ namespace MLEM.Ui.Elements {
|
||||||
public override void Update(GameTime time) {
|
public override void Update(GameTime time) {
|
||||||
base.Update(time);
|
base.Update(time);
|
||||||
var moused = this.System.MousedElement;
|
var moused = this.System.MousedElement;
|
||||||
if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) {
|
if (moused == this && this.Controls.MainButton(this.Input)) {
|
||||||
this.isMouseHeld = true;
|
this.isMouseHeld = true;
|
||||||
} else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) {
|
} else if (this.isMouseHeld && this.Controls.MainButton(this.Input)) {
|
||||||
this.isMouseHeld = false;
|
this.isMouseHeld = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
|
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
|
||||||
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
|
var scroll = this.Controls.Scroll(this.Input);
|
||||||
if (scroll != 0)
|
if (scroll != 0)
|
||||||
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
||||||
}
|
}
|
||||||
|
|
15
MLEM.Ui/UiControls.cs
Normal file
15
MLEM.Ui/UiControls.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
using MLEM.Input;
|
||||||
|
|
||||||
|
namespace MLEM.Ui {
|
||||||
|
public class UiControls {
|
||||||
|
|
||||||
|
public BoolQuery MainButton = h => h.IsMouseButtonPressed(MouseButton.Left);
|
||||||
|
public BoolQuery SecondaryButton = h => h.IsMouseButtonPressed(MouseButton.Right);
|
||||||
|
public FloatQuery Scroll = h => h.LastScrollWheel - h.ScrollWheel;
|
||||||
|
|
||||||
|
public delegate bool BoolQuery(InputHandler handler);
|
||||||
|
|
||||||
|
public delegate float FloatQuery(InputHandler handler);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -49,9 +49,7 @@ namespace MLEM.Ui {
|
||||||
public float DrawAlpha = 1;
|
public float DrawAlpha = 1;
|
||||||
public BlendState BlendState;
|
public BlendState BlendState;
|
||||||
public SamplerState SamplerState = SamplerState.PointClamp;
|
public SamplerState SamplerState = SamplerState.PointClamp;
|
||||||
|
public UiControls Controls = new UiControls();
|
||||||
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;
|
||||||
|
@ -86,7 +84,7 @@ namespace MLEM.Ui {
|
||||||
this.MousedElement = mousedNow;
|
this.MousedElement = mousedNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.InputHandler.IsMouseButtonPressed(this.MainButton)) {
|
if (this.Controls.MainButton(this.InputHandler)) {
|
||||||
// select element
|
// select element
|
||||||
if (this.SelectedElement != mousedNow) {
|
if (this.SelectedElement != mousedNow) {
|
||||||
if (this.SelectedElement != null)
|
if (this.SelectedElement != null)
|
||||||
|
@ -99,20 +97,12 @@ namespace MLEM.Ui {
|
||||||
// first action on element
|
// first action on element
|
||||||
if (mousedNow != null)
|
if (mousedNow != null)
|
||||||
mousedNow.OnPressed?.Invoke(mousedNow);
|
mousedNow.OnPressed?.Invoke(mousedNow);
|
||||||
} else if (this.InputHandler.IsMouseButtonPressed(this.SecondaryButton)) {
|
} else if (this.Controls.SecondaryButton(this.InputHandler)) {
|
||||||
// secondary action on element
|
// secondary action on element
|
||||||
if (mousedNow != null)
|
if (mousedNow != null)
|
||||||
mousedNow.OnSecondaryPressed?.Invoke(mousedNow);
|
mousedNow.OnSecondaryPressed?.Invoke(mousedNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
// generic element click
|
|
||||||
if (mousedNow?.OnClicked != null) {
|
|
||||||
foreach (var button in InputHandler.MouseButtons) {
|
|
||||||
if (this.InputHandler.IsMouseButtonPressed(button))
|
|
||||||
mousedNow.OnClicked(mousedNow, button);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
root.Element.Update(time);
|
root.Element.Update(time);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue