Repository navigation
Expand file tree
/
Copy pathAbstractMenu.cs
More file actions
77 lines (66 loc) · 3.34 KB
/
Copy pathAbstractMenu.cs
File metadata and controls
77 lines (66 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Gehört zu Johannes bzw. Arvid (Grafik)
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System;
namespace Info2021 {
abstract class AbstractMenu<A> {
// A represents the possible menu items without any further restrictions
protected SpriteBatch spriteBatch;
protected ResourceAccessor resourceAccessor;
protected MenuRenderer menuRenderer;
// selected menu option number
protected int activeIndex = 0;
// selected menu option
public A ActiveItem { get { return AllItems[activeIndex]; } }
// list of displayed Texts for each menu
public abstract string[] Texts { get; }
protected bool pressedJump = false;
// was it pressed in the last Frame
protected bool oldJump = false;
protected bool pressedUp = false;
// was it pressed in the last Frame
protected bool oldUp = false;
protected bool pressedDown = false;
// was it pressed in the last Frame
protected bool oldDown = false;
// all options of one specific menustate
public abstract A[] AllItems { get; }
public AbstractMenu(SpriteBatch spriteBatch, ResourceAccessor resourceAccessor) {
this.spriteBatch = spriteBatch;
this.resourceAccessor = resourceAccessor;
menuRenderer = new MenuRenderer(spriteBatch);
}
// gets called in Game1.cs
// item becomes ActiveItem, which is determined by acticeIndex(public A ActiveItem { get { return AllItems[activeIndex]; } }), which is updated by Update()
public bool HasBeenSelected(out A item) {
item = ActiveItem;
// return true if "x" is pressed and was not pressed in privious frame (if true game state changes accordingly)
return !oldJump && pressedJump;
}
public void Update() {
// check whether button is now pressed and was not pressed on last frame
// oldJump takes value of previous frame, oldUp takes ...
oldJump = pressedJump;
// current value gets checked
pressedJump = InputManager.IsActive(InputEvent.Jump);
oldUp = pressedUp;
pressedUp = InputManager.IsActive(InputEvent.Up);
oldDown = pressedDown;
pressedDown = InputManager.IsActive(InputEvent.Down);
// when down button is pressed and was not pressed in previous frame and the next index would not exceed the number possible Items
if (pressedDown && !oldDown && activeIndex < AllItems.Length - 1) activeIndex++;
if (pressedUp && !oldUp && activeIndex > 0) activeIndex--;
}
public void Draw(MenuRenderer renderer, ResourceAccessor accessor, Vector2 camPos) {
for (int i = 0; i < AllItems.Length; i++) {
// Draw selected text in black, unselected in blue
renderer.DrawText(new Vector2(100, i * 50 + 20), accessor.Font, Texts[i], i == activeIndex ? Color.Black : Color.Blue);
}
}
public void Draw(float dt) {
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
Draw(menuRenderer, resourceAccessor, Vector2.Zero);
spriteBatch.End();
}
}
}