1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-20 12:09:10 +02:00
This commit is contained in:
Ellpeck 2020-02-06 01:33:24 +01:00
parent 64749c31ce
commit ee5111372b
15 changed files with 83 additions and 67 deletions

View file

@ -17,21 +17,21 @@
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/processorParam:TextureFormat=Color
/build:Fonts/TestFont.spritefont
#begin Fonts/TestFontBold.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/processorParam:TextureFormat=Color
/build:Fonts/TestFontBold.spritefont
#begin Fonts/TestFontItalic.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/processorParam:TextureFormat=Color
/build:Fonts/TestFontItalic.spritefont
#begin Textures/Anim.png

View file

@ -85,16 +85,16 @@ namespace MLEM.Ui.Elements {
public TabNextElementCallback GetTabNextElement;
public GamepadNextElementCallback GetGamepadNextElement;
private UiSystem system;
public UiSystem System {
get => this.system;
private UiSystem uiSystem;
public UiSystem UiSystem {
get => this.uiSystem;
internal set {
this.system = value;
if (this.system != null)
this.InitStyle(this.system.Style);
this.uiSystem = value;
if (this.uiSystem != null)
this.InitStyle(this.uiSystem.Style);
}
}
protected UiControls Controls => this.System.Controls;
protected UiControls Controls => this.UiSystem.Controls;
protected InputHandler Input => this.Controls.Input;
public RootElement Root { get; internal set; }
public float Scale => this.Root.ActualScale;
@ -166,7 +166,7 @@ namespace MLEM.Ui.Elements {
element.Parent = this;
element.AndChildren(e => {
e.Root = this.Root;
e.System = this.System;
e.UiSystem = this.UiSystem;
if (this.Root != null)
this.Root.OnElementAdded(e);
});
@ -180,7 +180,7 @@ namespace MLEM.Ui.Elements {
element.Parent = null;
element.AndChildren(e => {
e.Root = null;
e.System = null;
e.UiSystem = null;
if (this.Root != null)
this.Root.OnElementRemoved(e);
});
@ -230,7 +230,7 @@ namespace MLEM.Ui.Elements {
if (this.IsHidden)
return;
var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport;
var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.uiSystem.Viewport;
var parentCenterX = parentArea.X + parentArea.Width / 2;
var parentCenterY = parentArea.Y + parentArea.Height / 2;
@ -314,7 +314,7 @@ namespace MLEM.Ui.Elements {
}
this.area = new RectangleF(pos, actualSize);
this.System.OnElementAreaUpdated?.Invoke(this);
this.UiSystem.OnElementAreaUpdated?.Invoke(this);
foreach (var child in this.Children)
child.ForceUpdateArea();
@ -451,15 +451,15 @@ namespace MLEM.Ui.Elements {
}
public virtual void Update(GameTime time) {
this.System.OnElementUpdated?.Invoke(this, time);
this.UiSystem.OnElementUpdated?.Invoke(this, time);
foreach (var child in this.GetRelevantChildren())
if (child.System != null)
if (child.UiSystem != null)
child.Update(time);
}
public virtual void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
this.System.OnElementDrawn?.Invoke(this, time, batch, alpha);
this.UiSystem.OnElementDrawn?.Invoke(this, time, batch, alpha);
foreach (var child in this.GetRelevantChildren()) {
if (!child.IsHidden)

View file

@ -85,7 +85,7 @@ namespace MLEM.Ui.Elements {
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
if (this.renderTarget != null)
this.renderTarget.Dispose();
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.GraphicsDevice, targetArea.Width, targetArea.Height);
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.UiSystem.GraphicsDevice, targetArea.Width, targetArea.Height);
}
}
}
@ -132,8 +132,8 @@ namespace MLEM.Ui.Elements {
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
if (this.Texture.Value != null)
batch.Draw(this.Texture, this.DisplayArea, Color.White * alpha, this.Scale);
if (this.Texture != null)
batch.Draw(this.Texture.Value, this.DisplayArea, Color.White * alpha, this.Scale);
// if we handle overflow, draw using the render target in DrawUnbound
if (!this.scrollOverflow || this.renderTarget == null) {
base.Draw(time, batch, alpha, blendState, samplerState, matrix);

View file

@ -36,9 +36,8 @@ namespace MLEM.Ui.Elements {
batch.Draw(this.Texture, this.DisplayArea, (Color) this.Color * alpha, this.Scale);
var percentage = this.CurrentValue / this.MaxValue;
var tex = this.ProgressTexture.Value;
var padHor = tex != null ? tex.Padding.Width * this.Scale : 0;
var padVer = tex != null ? tex.Padding.Height * this.Scale : 0;
var padHor = this.ProgressTexture != null ? this.ProgressTexture.Value.Padding.Width * this.Scale : 0;
var padVer = this.ProgressTexture != null ? this.ProgressTexture.Value.Padding.Height * this.Scale : 0;
var width = percentage * (this.DisplayArea.Width - padHor) + padHor;
var height = percentage * (this.DisplayArea.Height - padVer) + padVer;
RectangleF progressArea;
@ -61,7 +60,7 @@ namespace MLEM.Ui.Elements {
break;
}
var offsetArea = progressArea.Shrink(this.ProgressPadding.Value * this.Scale);
if (this.ProgressTexture.Value != null) {
if (this.ProgressTexture != null) {
batch.Draw(this.ProgressTexture, offsetArea, (Color) this.ProgressColor * alpha, this.Scale);
} else {
batch.Draw(batch.GetBlankTexture(), offsetArea, (Color) this.ProgressColor * alpha);

View file

@ -73,7 +73,7 @@ namespace MLEM.Ui.Elements {
if (this.isMouseHeld)
this.ScrollToPos(this.Input.MousePosition.Transform(this.Root.InvTransform));
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
double scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
if (scroll != 0)
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
}

View file

@ -69,6 +69,7 @@ namespace MLEM.Ui.Elements {
}
};
} else {
#if !WEB
this.OnPressed += async e => {
if (!KeyboardInput.IsVisible) {
var title = this.MobileTitle ?? this.PlaceholderText;
@ -77,6 +78,7 @@ namespace MLEM.Ui.Elements {
this.SetText(result.Replace('\n', ' '), true);
}
};
#endif
}
this.OnDeselected += e => this.CaretPos = 0;
this.OnSelected += e => this.CaretPos = this.text.Length;
@ -84,7 +86,7 @@ namespace MLEM.Ui.Elements {
private void HandleTextChange(bool textChanged = true) {
// not initialized yet
if (this.Font.Value == null)
if (this.Font == null)
return;
var length = this.Font.Value.MeasureString(this.text).X * this.TextScale;
var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2;

View file

@ -23,12 +23,12 @@ namespace MLEM.Ui.Elements {
if (elementToHover != null) {
elementToHover.OnMouseEnter += element => {
element.System.Add(element.GetType().Name + "Tooltip", this);
element.UiSystem.Add(element.GetType().Name + "Tooltip", this);
this.SnapPositionToMouse();
};
elementToHover.OnMouseExit += element => {
if (this.System != null)
this.System.Remove(this.Root.Name);
if (this.UiSystem != null)
this.UiSystem.Remove(this.Root.Name);
};
}
}
@ -51,7 +51,7 @@ namespace MLEM.Ui.Elements {
}
public void SnapPositionToMouse() {
var viewport = this.System.Viewport.Size;
var viewport = this.UiSystem.Viewport.Size;
var offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset;
if (offset.X < 0)
offset.X = 0;

View file

@ -12,9 +12,8 @@ namespace MLEM.Ui.Style {
}
public void SetFromStyle(T value) {
if (!this.isCustom) {
if (!this.isCustom)
this.Value = value;
}
}
public void Set(T value) {
@ -23,7 +22,7 @@ namespace MLEM.Ui.Style {
}
public T OrDefault(T def) {
return EqualityComparer<T>.Default.Equals(this.Value, default) ? def : this.Value;
return EqualityComparer<T>.Default.Equals(this.Value, default(T)) ? def : this.Value;
}
public static implicit operator T(StyleProp<T> prop) {
@ -34,5 +33,13 @@ namespace MLEM.Ui.Style {
return new StyleProp<T>(prop);
}
public static bool operator ==(StyleProp<T> left, StyleProp<T> right) {
return EqualityComparer<T>.Default.Equals(left.Value, right.Value);
}
public static bool operator !=(StyleProp<T> left, StyleProp<T> right) {
return !(left == right);
}
}
}

View file

@ -13,7 +13,7 @@ namespace MLEM.Ui {
public readonly InputHandler Input;
protected readonly bool IsInputOurs;
protected readonly UiSystem System;
protected readonly UiSystem UiSystem;
public RootElement ActiveRoot { get; private set; }
public Element MousedElement { get; private set; }
@ -34,8 +34,8 @@ namespace MLEM.Ui {
public bool IsAutoNavMode { get; private set; }
public UiControls(UiSystem system, InputHandler inputHandler = null) {
this.System = system;
public UiControls(UiSystem uiSystem, InputHandler inputHandler = null) {
this.UiSystem = uiSystem;
this.Input = inputHandler ?? new InputHandler();
this.IsInputOurs = inputHandler == null;
@ -46,18 +46,18 @@ namespace MLEM.Ui {
public virtual void Update() {
if (this.IsInputOurs)
this.Input.Update();
this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanSelectContent && !root.Element.IsHidden);
this.ActiveRoot = this.UiSystem.GetRootElements().FirstOrDefault(root => root.CanSelectContent && !root.Element.IsHidden);
// MOUSE INPUT
if (this.HandleMouse) {
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2());
if (mousedNow != this.MousedElement) {
if (this.MousedElement != null)
this.System.OnElementMouseExit?.Invoke(this.MousedElement);
this.UiSystem.OnElementMouseExit?.Invoke(this.MousedElement);
if (mousedNow != null)
this.System.OnElementMouseEnter?.Invoke(mousedNow);
this.UiSystem.OnElementMouseEnter?.Invoke(mousedNow);
this.MousedElement = mousedNow;
this.System.OnMousedElementChanged?.Invoke(mousedNow);
this.UiSystem.OnMousedElementChanged?.Invoke(mousedNow);
}
if (this.Input.IsMouseButtonPressed(MouseButton.Left)) {
@ -65,11 +65,11 @@ namespace MLEM.Ui {
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
this.ActiveRoot?.SelectElement(selectedNow);
if (mousedNow != null && mousedNow.CanBePressed)
this.System.OnElementPressed?.Invoke(mousedNow);
this.UiSystem.OnElementPressed?.Invoke(mousedNow);
} else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) {
this.IsAutoNavMode = false;
if (mousedNow != null && mousedNow.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(mousedNow);
this.UiSystem.OnElementSecondaryPressed?.Invoke(mousedNow);
}
}
@ -79,10 +79,10 @@ namespace MLEM.Ui {
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) {
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
// secondary action on element using space or enter
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
this.UiSystem.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else {
// first action on element using space or enter
this.System.OnElementPressed?.Invoke(this.SelectedElement);
this.UiSystem.OnElementPressed?.Invoke(this.SelectedElement);
}
}
} else if (this.Input.IsKeyPressed(Keys.Tab)) {
@ -103,13 +103,13 @@ namespace MLEM.Ui {
var tapped = this.GetElementUnderPos(tap.Position);
this.ActiveRoot?.SelectElement(tapped);
if (tapped != null && tapped.CanBePressed)
this.System.OnElementPressed?.Invoke(tapped);
this.UiSystem.OnElementPressed?.Invoke(tapped);
} else if (this.Input.GetGesture(GestureType.Hold, out var hold)) {
this.IsAutoNavMode = false;
var held = this.GetElementUnderPos(hold.Position);
this.ActiveRoot?.SelectElement(held);
if (held != null && held.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(held);
this.UiSystem.OnElementSecondaryPressed?.Invoke(held);
}
}
@ -117,10 +117,10 @@ namespace MLEM.Ui {
if (this.HandleGamepad) {
if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed)
this.System.OnElementPressed?.Invoke(this.SelectedElement);
this.UiSystem.OnElementPressed?.Invoke(this.SelectedElement);
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
this.UiSystem.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else if (this.DownButtons.Any(this.IsGamepadPressed)) {
this.HandleGamepadNextElement(Direction2.Down);
} else if (this.LeftButtons.Any(this.IsGamepadPressed)) {
@ -134,7 +134,7 @@ namespace MLEM.Ui {
}
public virtual Element GetElementUnderPos(Vector2 position, bool transform = true) {
foreach (var root in this.System.GetRootElements()) {
foreach (var root in this.UiSystem.GetRootElements()) {
var pos = transform ? Vector2.Transform(position, root.InvTransform) : position;
var moused = root.Element.GetElementUnderPos(pos);
if (moused != null)
@ -146,7 +146,7 @@ namespace MLEM.Ui {
protected virtual Element GetTabNextElement(bool backward) {
if (this.ActiveRoot == null)
return null;
var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Append(this.ActiveRoot.Element);
var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Concat(new[] {this.ActiveRoot.Element});
if (this.SelectedElement?.Root != this.ActiveRoot) {
return backward ? children.LastOrDefault(c => c.CanBeSelected) : children.FirstOrDefault(c => c.CanBeSelected);
} else {
@ -174,7 +174,7 @@ namespace MLEM.Ui {
protected virtual Element GetGamepadNextElement(RectangleF searchArea) {
if (this.ActiveRoot == null)
return null;
var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Append(this.ActiveRoot.Element);
var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Concat(new[] {this.ActiveRoot.Element});
if (this.SelectedElement?.Root != this.ActiveRoot) {
return children.FirstOrDefault(c => c.CanBeSelected);
} else {
@ -202,7 +202,7 @@ namespace MLEM.Ui {
RectangleF searchArea = default;
if (this.SelectedElement?.Root != null) {
searchArea = this.SelectedElement.Area;
var (_, _, width, height) = this.System.Viewport;
var (_, _, width, height) = this.UiSystem.Viewport;
switch (dir) {
case Direction2.Down:
searchArea.Height += height;

View file

@ -42,7 +42,7 @@ namespace MLEM.Ui {
set {
this.style = value;
foreach (var root in this.rootElements) {
root.Element.AndChildren(e => e.System = this);
root.Element.AndChildren(e => e.UiSystem = this);
root.Element.SetAreaDirty();
}
}
@ -85,7 +85,7 @@ namespace MLEM.Ui {
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));
this.OnSelectedElementDrawn = (element, time, batch, alpha) => {
if (this.Controls.IsAutoNavMode && element.SelectionIndicator.Value != null) {
if (this.Controls.IsAutoNavMode && element.SelectionIndicator != null) {
batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2);
}
};
@ -134,7 +134,7 @@ namespace MLEM.Ui {
this.rootElements.Add(root);
root.Element.AndChildren(e => {
e.Root = root;
e.System = this;
e.UiSystem = this;
root.OnElementAdded(e);
e.SetAreaDirty();
});
@ -151,7 +151,7 @@ namespace MLEM.Ui {
root.SelectElement(null);
root.Element.AndChildren(e => {
e.Root = null;
e.System = null;
e.UiSystem = null;
root.OnElementRemoved(e);
e.SetAreaDirty();
});
@ -192,7 +192,7 @@ namespace MLEM.Ui {
public readonly string Name;
public readonly Element Element;
public readonly UiSystem System;
public readonly UiSystem UiSystem;
private float scale = 1;
public float Scale {
get => this.scale;
@ -208,10 +208,10 @@ namespace MLEM.Ui {
get => this.priority;
set {
this.priority = value;
this.System.SortRoots();
this.UiSystem.SortRoots();
}
}
public float ActualScale => this.System.GlobalScale * this.Scale;
public float ActualScale => this.UiSystem.GlobalScale * this.Scale;
public Matrix Transform = Matrix.Identity;
public Matrix InvTransform => Matrix.Invert(this.Transform);
@ -222,10 +222,10 @@ namespace MLEM.Ui {
public Element.GenericCallback OnElementAdded;
public Element.GenericCallback OnElementRemoved;
public RootElement(string name, Element element, UiSystem system) {
public RootElement(string name, Element element, UiSystem uiSystem) {
this.Name = name;
this.Element = element;
this.System = system;
this.UiSystem = uiSystem;
this.OnElementAdded += e => {
if (e.CanBeSelected)
@ -242,11 +242,11 @@ namespace MLEM.Ui {
return;
if (this.SelectedElement != null)
this.System.OnElementDeselected?.Invoke(this.SelectedElement);
this.UiSystem.OnElementDeselected?.Invoke(this.SelectedElement);
if (element != null)
this.System.OnElementSelected?.Invoke(element);
this.UiSystem.OnElementSelected?.Invoke(element);
this.SelectedElement = element;
this.System.OnSelectedElementChanged?.Invoke(element);
this.UiSystem.OnSelectedElementChanged?.Invoke(element);
}
}

View file

@ -24,7 +24,9 @@ namespace MLEM.Extensions {
private static class TextInputAdder {
public static void Add(GameWindow window, TextInputCallback callback) {
#if !WEB
window.TextInput += (sender, args) => callback(sender, args.Key, args.Character);
#endif
}
}

View file

@ -27,8 +27,13 @@ namespace MLEM.Formatting {
// color codes
var colors = typeof(Color).GetProperties();
foreach (var color in colors) {
#if !WEB
if (color.GetGetMethod().IsStatic)
FormattingCodes[color.Name.ToLowerInvariant()] = new FormattingCode((Color) color.GetValue(null));
#else
if (color.GetMethod.IsStatic)
FormattingCodes[color.Name.ToLower()] = new FormattingCode((Color) color.GetValue(null));
#endif
}
// animations
@ -139,7 +144,7 @@ namespace MLEM.Formatting {
}
private static FormattingCode FromMatch(Capture match) {
var rawCode = match.Value.Substring(1, match.Value.Length - 2).ToLowerInvariant();
var rawCode = match.Value.Substring(1, match.Value.Length - 2).ToLower();
FormattingCodes.TryGetValue(rawCode, out var val);
return val;
}

View file

@ -270,6 +270,7 @@ namespace MLEM.Input {
return true;
}
}
sample = default(GestureSample);
return false;
}

View file

@ -7,7 +7,7 @@ namespace MLEM.Misc {
[DataContract]
public struct RectangleF : IEquatable<RectangleF> {
public static RectangleF Empty => default;
public static RectangleF Empty => default(RectangleF);
[DataMember]
public float X;

View file

@ -40,7 +40,7 @@ namespace MLEM.Pathfinding {
}
protected override float GetManhattanDistance(Vector3 first, Vector3 second) {
return Math.Abs(second.X - first.X) + Math.Abs(second.Y - first.Y) + Math.Abs(second.Z - first.Z);
return (float)(Math.Abs(second.X - first.X) + Math.Abs(second.Y - first.Y) + Math.Abs(second.Z - first.Z));
}
}