Skip to content

Commit ba5a41b

Browse files
committed
draft of the "avoid virtual member calls" refactor
1 parent eda1a89 commit ba5a41b

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

MLEM.Ui/Elements/Button.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public bool TruncateTextIfLong {
8181
}
8282
/// <summary>
8383
/// Whether this button should be able to be selected even if it <see cref="IsDisabled"/>.
84-
/// If this is true, <see cref="CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
84+
/// If this is true, <see cref="Element.CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
8585
/// </summary>
8686
public bool CanSelectDisabled;
8787
/// <summary>
@@ -90,11 +90,6 @@ public bool TruncateTextIfLong {
9090
/// </summary>
9191
public Func<Button, bool> AutoDisableCondition;
9292

93-
/// <inheritdoc />
94-
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
95-
/// <inheritdoc />
96-
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
97-
9893
private bool isDisabled;
9994

10095
/// <summary>
@@ -167,5 +162,15 @@ protected override void InitStyle(UiStyle style) {
167162
this.DisabledColor = this.DisabledColor.OrStyle(style.ButtonDisabledColor);
168163
}
169164

165+
/// <inheritdoc />
166+
protected override bool GetCanBeSelected(bool value) {
167+
return value && (this.CanSelectDisabled || !this.IsDisabled);
168+
}
169+
170+
/// <inheritdoc />
171+
protected override bool GetCanBePressed(bool value) {
172+
return value && !this.IsDisabled;
173+
}
174+
170175
}
171176
}

MLEM.Ui/Elements/Checkbox.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,10 @@ public bool Checked {
9090
public CheckStateChange OnCheckStateChange;
9191
/// <summary>
9292
/// Whether this checkbox should be able to be selected even if it <see cref="IsDisabled"/>.
93-
/// If this is true, <see cref="CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
93+
/// If this is true, <see cref="Element.CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
9494
/// </summary>
9595
public bool CanSelectDisabled;
9696

97-
/// <inheritdoc />
98-
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
99-
/// <inheritdoc />
100-
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
101-
10297
private bool isChecked;
10398
private StyleProp<float> textOffsetX;
10499

@@ -145,7 +140,7 @@ public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteB
145140
var uncheckedColor = this.UncheckColor.OrDefault(Color.Transparent);
146141
if (this.Checked)
147142
batch.Draw(this.Checkmark, boxDisplayArea, this.CheckColor.OrDefault(Color.White) * alpha);
148-
else if(uncheckedColor.A != 0) {
143+
else if (uncheckedColor.A != 0) {
149144
batch.Draw(this.Checkmark, boxDisplayArea, uncheckedColor * alpha);
150145
}
151146
base.Draw(time, batch, alpha, context);
@@ -166,6 +161,16 @@ protected override void InitStyle(UiStyle style) {
166161
this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX);
167162
}
168163

164+
/// <inheritdoc />
165+
protected override bool GetCanBeSelected(bool value) {
166+
return value && (this.CanSelectDisabled || !this.IsDisabled);
167+
}
168+
169+
/// <inheritdoc />
170+
protected override bool GetCanBePressed(bool value) {
171+
return value && !this.IsDisabled;
172+
}
173+
169174
/// <summary>
170175
/// A delegate used for <see cref="Checkbox.OnCheckStateChange"/>
171176
/// </summary>

MLEM.Ui/Elements/Element.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public RectangleF UnscrolledArea {
178178
/// Set this property to <c>true</c> to cause this element to be hidden.
179179
/// Hidden elements don't receive input events, aren't rendered and don't factor into auto-anchoring.
180180
/// </summary>
181-
public virtual bool IsHidden {
182-
get => this.isHidden;
181+
public bool IsHidden {
182+
get => this.GetIsHidden(this.isHidden);
183183
set {
184184
if (this.isHidden == value)
185185
return;
@@ -212,8 +212,8 @@ public int Priority {
212212
/// Set this field to false to disallow the element from being selected.
213213
/// An unselectable element is skipped by automatic navigation and its <see cref="OnSelected"/> callback will never be called.
214214
/// </summary>
215-
public virtual bool CanBeSelected {
216-
get => this.canBeSelected;
215+
public bool CanBeSelected {
216+
get => this.GetCanBeSelected(this.canBeSelected);
217217
set {
218218
this.canBeSelected = value;
219219
if (!this.canBeSelected && this.Root?.SelectedElement == this)
@@ -223,15 +223,18 @@ public virtual bool CanBeSelected {
223223
/// <summary>
224224
/// Set this field to false to disallow the element from reacting to being moused over.
225225
/// </summary>
226-
public virtual bool CanBeMoused { get; set; } = true;
226+
public bool CanBeMoused { get; set; } = true;
227227
/// <summary>
228228
/// Set this field to false to disallow this element's <see cref="OnPressed"/> and <see cref="OnSecondaryPressed"/> events to be called.
229229
/// </summary>
230-
public virtual bool CanBePressed { get; set; } = true;
230+
public bool CanBePressed {
231+
get => this.GetCanBePressed(this.canBePressed);
232+
set => this.canBePressed = value;
233+
}
231234
/// <summary>
232235
/// Set this field to false to cause auto-anchored siblings to ignore this element as a possible anchor point.
233236
/// </summary>
234-
public virtual bool CanAutoAnchorsAttach {
237+
public bool CanAutoAnchorsAttach {
235238
get => this.canAutoAnchorsAttach;
236239
set {
237240
if (this.canAutoAnchorsAttach != value) {
@@ -244,7 +247,7 @@ public virtual bool CanAutoAnchorsAttach {
244247
/// Set this field to true to cause this element's width to be automatically calculated based on the area that its <see cref="Children"/> take up.
245248
/// To use this element's <see cref="Size"/>'s X coordinate as a minimum or maximum width rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
246249
/// </summary>
247-
public virtual bool SetWidthBasedOnChildren {
250+
public bool SetWidthBasedOnChildren {
248251
get => this.setWidthBasedOnChildren;
249252
set {
250253
if (this.setWidthBasedOnChildren != value) {
@@ -257,7 +260,7 @@ public virtual bool SetWidthBasedOnChildren {
257260
/// Set this field to true to cause this element's height to be automatically calculated based on the area that its <see cref="Children"/> take up.
258261
/// To use this element's <see cref="Size"/>'s Y coordinate as a minimum or maximum height rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
259262
/// </summary>
260-
public virtual bool SetHeightBasedOnChildren {
263+
public bool SetHeightBasedOnChildren {
261264
get => this.setHeightBasedOnChildren;
262265
set {
263266
if (this.setHeightBasedOnChildren != value) {
@@ -271,7 +274,7 @@ public virtual bool SetHeightBasedOnChildren {
271274
/// For example, if an element's <see cref="Size"/>'s Y coordinate is set to 20, but there is only one child with a height of 10 in it, the element's height would be shrunk to 10 if this value was false, but would remain at 20 if it was true.
272275
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
273276
/// </summary>
274-
public virtual bool TreatSizeAsMinimum {
277+
public bool TreatSizeAsMinimum {
275278
get => this.treatSizeAsMinimum;
276279
set {
277280
if (this.treatSizeAsMinimum != value) {
@@ -284,7 +287,7 @@ public virtual bool TreatSizeAsMinimum {
284287
/// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/>are enabled, the resulting width or height weill always be less than or equal to this element's <see cref="Size"/>.
285288
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
286289
/// </summary>
287-
public virtual bool TreatSizeAsMaximum {
290+
public bool TreatSizeAsMaximum {
288291
get => this.treatSizeAsMaximum;
289292
set {
290293
if (this.treatSizeAsMaximum != value) {
@@ -298,7 +301,7 @@ public virtual bool TreatSizeAsMaximum {
298301
/// If the resulting area is too large, the size of this element is shrunk to fit the target area.
299302
/// This can be useful if an element should fill the remaining area of a parent exactly.
300303
/// </summary>
301-
public virtual bool PreventParentSpill {
304+
public bool PreventParentSpill {
302305
get => this.preventParentSpill;
303306
set {
304307
if (this.preventParentSpill != value) {
@@ -311,7 +314,7 @@ public virtual bool PreventParentSpill {
311314
/// The transparency (alpha value) that this element is rendered with.
312315
/// Note that, when <see cref="Draw(Microsoft.Xna.Framework.GameTime,Microsoft.Xna.Framework.Graphics.SpriteBatch,float,MLEM.Graphics.SpriteBatchContext)"/> is called, this alpha value is multiplied with the <see cref="Parent"/>'s alpha value and passed down to this element's <see cref="Children"/>.
313316
/// </summary>
314-
public virtual float DrawAlpha { get; set; } = 1;
317+
public float DrawAlpha { get; set; } = 1;
315318
/// <summary>
316319
/// Stores whether this element is currently being moused over or touched.
317320
/// </summary>
@@ -335,7 +338,7 @@ public virtual bool PreventParentSpill {
335338
/// All elements that share the same auto-nav group will be able to be navigated between, and all other elements will not be reachable from elements of other groups.
336339
/// Note that, if no element is previously selected and auto-navigation is invoked, this element cannot be chosen as the first element to navigate to if its auto-nav group is non-null.
337340
/// </summary>
338-
public virtual string AutoNavGroup { get; set; }
341+
public string AutoNavGroup { get; set; }
339342

340343
/// <summary>
341344
/// This Element's current <see cref="UiStyle"/>.
@@ -551,6 +554,7 @@ protected IList<Element> SortedChildren {
551554
private bool treatSizeAsMaximum;
552555
private bool preventParentSpill;
553556
private int layoutRecursion;
557+
private bool canBePressed = true;
554558

555559
/// <summary>
556560
/// Creates a new element with the given anchor and size and sets up some default event reactions.
@@ -1073,6 +1077,33 @@ public Vector2 TransformInverseAll(Vector2 position) {
10731077
return this.TransformInverse(position);
10741078
}
10751079

1080+
/// <summary>
1081+
/// A method that allows derived classes to modify the return value of the <see cref="IsHidden"/> property.
1082+
/// </summary>
1083+
/// <param name="value">The <see cref="IsHidden"/> property's actual current value.</param>
1084+
/// <returns>The value that <see cref="IsHidden"/> should return.</returns>
1085+
protected virtual bool GetIsHidden(bool value) {
1086+
return value;
1087+
}
1088+
1089+
/// <summary>
1090+
/// A method that allows derived classes to modify the return value of the <see cref="CanBeSelected"/> property.
1091+
/// </summary>
1092+
/// <param name="value">The <see cref="CanBeSelected"/> property's actual current value.</param>
1093+
/// <returns>The value that <see cref="CanBeSelected"/> should return.</returns>
1094+
protected virtual bool GetCanBeSelected(bool value) {
1095+
return value;
1096+
}
1097+
1098+
/// <summary>
1099+
/// A method that allows derived classes to modify the return value of the <see cref="CanBePressed"/> property.
1100+
/// </summary>
1101+
/// <param name="value">The <see cref="CanBePressed"/> property's actual current value.</param>
1102+
/// <returns>The value that <see cref="CanBePressed"/> should return.</returns>
1103+
protected virtual bool GetCanBePressed(bool value) {
1104+
return value;
1105+
}
1106+
10761107
/// <summary>
10771108
/// Called when this element is added to a <see cref="UiSystem"/> and, optionally, a given <see cref="RootElement"/>.
10781109
/// This method is called in <see cref="AddChild{T}"/> for a parent whose <see cref="System"/> is set, as well as <see cref="UiSystem.Add"/>.

MLEM.Ui/Elements/Image.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ public bool SetHeightBasedOnAspect {
103103
/// </summary>
104104
public SamplerState SamplerState;
105105

106-
/// <inheritdoc />
107-
public override bool IsHidden => base.IsHidden || this.Texture == null;
108-
109106
private bool scaleToImage;
110107
private bool setWidthBasedOnAspect;
111108
private bool setHeightBasedOnAspect;
@@ -198,6 +195,11 @@ public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteB
198195
base.Draw(time, batch, alpha, context);
199196
}
200197

198+
/// <inheritdoc />
199+
protected override bool GetIsHidden(bool value) {
200+
return value || this.Texture == null;
201+
}
202+
201203
private void CheckTextureChange() {
202204
var newTexture = this.GetTextureCallback?.Invoke(this) ?? this.explicitlySetTexture;
203205
if (this.displayedTexture == newTexture)

MLEM.Ui/Elements/Paragraph.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ public StyleProp<TextAlignment> Alignment {
161161
/// </summary>
162162
public int? DrawEndIndex;
163163

164-
/// <inheritdoc />
165-
public override bool IsHidden => base.IsHidden || string.IsNullOrWhiteSpace(this.Text);
166-
167164
private string displayedText;
168165
private string explicitlySetText;
169166
private StyleProp<TextAlignment> alignment;
@@ -278,6 +275,11 @@ protected override void InitStyle(UiStyle style) {
278275
this.LinkColor = this.LinkColor.OrStyle(style.LinkColor);
279276
}
280277

278+
/// <inheritdoc />
279+
protected override bool GetIsHidden(bool value) {
280+
return value || string.IsNullOrWhiteSpace(this.Text);
281+
}
282+
281283
private void SetTextDirty() {
282284
this.tokenizedText = null;
283285
// only set our area dirty if our size changed as a result of this action

MLEM.Ui/Elements/Tooltip.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ public StyleProp<float> ParagraphWidth {
106106
/// </summary>
107107
public virtual Rectangle? Viewport { get; set; }
108108

109-
/// <inheritdoc />
110-
public override bool IsHidden => this.autoHidden || base.IsHidden;
111-
112109
private TimeSpan delayCountdown;
113110
private bool autoHidden;
114111
private Element autoNavSnapElement;
@@ -315,6 +312,11 @@ public void AddToElement(Element elementToHover) {
315312
};
316313
}
317314

315+
/// <inheritdoc />
316+
protected override bool GetIsHidden(bool value) {
317+
return value || this.autoHidden;
318+
}
319+
318320
private void Init(Element elementToHover) {
319321
this.SetWidthBasedOnChildren = true;
320322
this.SetHeightBasedOnChildren = true;

0 commit comments

Comments
 (0)