-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlPanel.cs
147 lines (117 loc) · 4.77 KB
/
ControlPanel.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Based on this guide: https://stardewvalleywiki.com/User:Dem1se#Define_your_UI
// 1. Create your new UI class
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.Menus;
using System.Linq;
using StardewModdingAPI;
namespace ScriptedHand
{
public class ControlPanel : IClickableMenu
{
static int UIWidth = 800;
static int UIHeight = 800;
static int XPos = (int)(Game1.viewport.Width * Game1.options.zoomLevel * (1 / Game1.options.uiScale)) / 2 - (UIWidth / 2);
static int YPos = (int)(Game1.viewport.Height * Game1.options.zoomLevel * (1 / Game1.options.uiScale)) / 2 - (UIHeight / 2);
private static System.Collections.Generic.List<ControlDrawData> ScriptControls = new();
// 3. Declare all the UI elements
ClickableComponent TitleLabel;
public ControlPanel()
{
initialize(XPos, YPos, UIWidth, UIHeight);
// 4. initialize and lay out all the declared UI components
TitleLabel = new ClickableComponent(new Rectangle(XPos + 60, YPos + 116, 0, 64), "ScriptedHand Control Panel");
}
// 5. The method invoked when the player left-clicks on the menu.
public override void receiveLeftClick(int x, int y, bool playSound = true)
{
if (TitleLabel.containsPoint(x, y))
{
// handle user clicking on the title. More practical use-case would be with buttons
}
foreach (ControlDrawData control in ScriptControls)
{
if (control.data.controlType == ControlType.Button)
{
// TODO: trigger button action
}
}
}
public static explicit operator bool(ControlPanel v)
{
return true;
}
// 6. Render the UI that has been set up to the screen.
// Gets called automatically every render tick when this UI is active
public override void draw(SpriteBatch b)
{
//draw screen fade
b.Draw(Game1.fadeToBlackRect, Game1.graphics.GraphicsDevice.Viewport.Bounds, Color.Black * 0.75f);
// draw menu dialogue box
Game1.drawDialogueBox(XPos, YPos, UIWidth, UIHeight, false, true);
// draw the TitleLabel
Utility.drawTextWithShadow(b, TitleLabel.name, Game1.dialogueFont, new Vector2(TitleLabel.bounds.X, TitleLabel.bounds.Y), Color.Black);
// draw every script control (pain)
if (ScriptControls.Count > 0)
foreach (ControlDrawData control in ScriptControls)
{
// Draw label
Utility.drawTextWithShadow(b, control.data.label, Game1.dialogueFont, new Vector2(control.x, control.y), Color.Black);
//TODO: Draw control
//switch (control.data.controltype)
//{
// case controltype.button:
// break;
// default:
// throw new exception($"lua error: invalid control type " +
// $"{control.data.controltype}. must be one of button," +
// $"checkbox, numberinput, textinput, or slider");
//}
}
// draw cursor at last
drawMouse(b);
}
// TODO: Implement Control Panel scripting API
public enum ControlType
{
Button, Checkbox, NumberInput, TextInput, Slider
}
static public void ListLayout(params ControlData[] controls)
{
const int PAGE_MAX_CONTROLS = 5;
int i = 0;
if (controls.Length < PAGE_MAX_CONTROLS && controls.Length > 0)
{
// Non-paged layout
foreach (ControlData control in controls)
{
ControlDrawData drawData = new()
{
data = control,
// the actual layout part lmao
index = i,
x = XPos + 60,
y = (YPos + 116) + (30 * (i + 1)),
width = UIWidth - 60,
height = 29
};
ScriptControls.Add(drawData);
}
}
}
struct ControlDrawData
{
public ControlData data;
public int index;
// Dimensions
public int x;
public int y;
public int width;
public int height;
}
}
}