1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-24 17:43:36 +02:00

more easily allow controls to be changed

This commit is contained in:
Ellpeck 2019-08-28 16:38:58 +02:00
parent ed7865379d
commit cc20682d47
4 changed files with 24 additions and 23 deletions

View file

@ -84,9 +84,8 @@ namespace MLEM.Ui.Elements {
public GenericCallback OnSelected;
public GenericCallback OnDeselected;
public MouseClickCallback OnClicked;
public MouseCallback OnMouseEnter;
public MouseCallback OnMouseExit;
public GenericCallback OnMouseEnter;
public GenericCallback OnMouseExit;
public TextInputCallback OnTextInput;
public GenericCallback OnAreaUpdated;
@ -100,6 +99,7 @@ namespace MLEM.Ui.Elements {
}
}
protected InputHandler Input => this.System.InputHandler;
protected UiControls Controls => this.System.Controls;
public RootElement Root { get; private set; }
public float Scale => this.Root.ActualScale;
public Point MousePos => this.Input.MousePosition;
@ -445,10 +445,6 @@ namespace MLEM.Ui.Elements {
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 GenericCallback(Element element);

View file

@ -48,9 +48,9 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime time) {
base.Update(time);
var moused = this.System.MousedElement;
if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) {
if (moused == this && this.Controls.MainButton(this.Input)) {
this.isMouseHeld = true;
} else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) {
} else if (this.isMouseHeld && this.Controls.MainButton(this.Input)) {
this.isMouseHeld = false;
}
@ -65,7 +65,7 @@ namespace MLEM.Ui.Elements {
}
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)
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
}

15
MLEM.Ui/UiControls.cs Normal file
View 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);
}
}

View file

@ -49,9 +49,7 @@ namespace MLEM.Ui {
public float DrawAlpha = 1;
public BlendState BlendState;
public SamplerState SamplerState = SamplerState.PointClamp;
public MouseButton MainButton = MouseButton.Left;
public MouseButton SecondaryButton = MouseButton.Right;
public UiControls Controls = new UiControls();
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
this.GraphicsDevice = device;
@ -86,7 +84,7 @@ namespace MLEM.Ui {
this.MousedElement = mousedNow;
}
if (this.InputHandler.IsMouseButtonPressed(this.MainButton)) {
if (this.Controls.MainButton(this.InputHandler)) {
// select element
if (this.SelectedElement != mousedNow) {
if (this.SelectedElement != null)
@ -99,20 +97,12 @@ namespace MLEM.Ui {
// first action on element
if (mousedNow != null)
mousedNow.OnPressed?.Invoke(mousedNow);
} else if (this.InputHandler.IsMouseButtonPressed(this.SecondaryButton)) {
} else if (this.Controls.SecondaryButton(this.InputHandler)) {
// secondary action on element
if (mousedNow != null)
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)
root.Element.Update(time);
}