Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ScrollPanelWidget.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
7
8 public float m_dragSpeed;
9
10 public float m_scrollBarAlpha;
11
12 public float m_scrollAreaLength;
13
14 public virtual LayoutDirection Direction { get; set; }
15
16 public virtual float ScrollPosition { get; set; }
17
18 public virtual float ScrollSpeed { get; set; }
19
21 ClampToBounds = true;
23 }
24
25 public void StartInitialScroll() {
26 ScrollPosition = 12f;
27 ScrollSpeed = -70f;
28 }
29
30 public virtual float CalculateScrollAreaLength() {
31 float num = 0f;
32 foreach (Widget child in Children) {
33 if (child.IsVisible) {
37 }
38 }
39 return num;
40 }
41
42 public override void MeasureOverride(Vector2 parentAvailableSize) {
43 IsDrawRequired = true;
44 foreach (Widget child in Children) {
45 if (child.IsVisible) {
46 if (Direction == LayoutDirection.Horizontal) {
47 child.Measure(new Vector2(float.MaxValue, MathUtils.Max(parentAvailableSize.Y - child.MarginVerticalSum, 0f)));
48 }
49 else {
50 child.Measure(new Vector2(MathUtils.Max(parentAvailableSize.X - child.MarginHorizontalSum, 0f), float.MaxValue));
51 }
52 }
53 }
54 }
55
56 public override void ArrangeOverride() {
57 foreach (Widget child in Children) {
58 Vector2 zero = Vector2.Zero;
59 Vector2 actualSize = ActualSize;
60 if (Direction == LayoutDirection.Horizontal) {
61 zero.X -= ScrollPosition;
62 actualSize.X = zero.X + child.ParentDesiredSize.X;
63 }
64 else {
65 zero.Y -= ScrollPosition;
66 actualSize.Y = zero.Y + child.ParentDesiredSize.Y;
67 }
68 ArrangeChildWidgetInCell(zero, actualSize, child);
69 }
70 }
71
72 public override void Update() {
73 float num = 50f;
76 if (Input.Tap.HasValue
77 && HitTestPanel(Input.Tap.Value)) {
79 }
80 if (m_lastDragPosition.HasValue) {
81 if (Input.Press.HasValue) {
82 float num2;
83 Vector2 vector = ScreenToWidget(Input.Press.Value);
84 Vector2 vector2 = vector - m_lastDragPosition.Value;
85 if (Direction == LayoutDirection.Horizontal) {
86 ScrollPosition += 0f - vector2.X;
87 num2 = vector2.X / Time.FrameDuration;
88 }
89 else {
90 ScrollPosition += 0f - vector2.Y;
91 num2 = vector2.Y / Time.FrameDuration;
92 }
93 float num3 = MathF.Abs(num2) < MathF.Abs(m_dragSpeed) ? 20f : 16f;
96 m_lastDragPosition = vector;
97 ScrollSpeed = 0f;
98 }
99 else {
101 m_dragSpeed = 0f;
102 m_lastDragPosition = null;
103 }
104 }
105 if (ScrollSpeed != 0f) {
106 ScrollSpeed *= MathF.Pow(0.33f, Time.FrameDuration);
107 if (MathF.Abs(ScrollSpeed) < 40f) {
108 ScrollSpeed = 0f;
109 }
111 m_scrollBarAlpha = 3f;
112 }
113 if (Input.Scroll.HasValue
114 && HitTestPanel(Input.Scroll.Value.XY)) {
115 ScrollPosition -= 40f * Input.Scroll.Value.Z;
116 ScrollSpeed = 0f;
117 num = 0f;
118 m_scrollBarAlpha = 3f;
119 }
120 float num4 = MathUtils.Max(m_scrollAreaLength - ((Direction == LayoutDirection.Horizontal) ? ActualSize.X : ActualSize.Y), 0f);
121 if (ScrollPosition < 0f) {
122 if (!m_lastDragPosition.HasValue) {
124 }
126 ScrollSpeed = 0f;
127 }
128 if (ScrollPosition > num4) {
129 if (!m_lastDragPosition.HasValue) {
131 }
133 ScrollSpeed = 0f;
134 }
135 if (m_lastDragPosition.HasValue
136 && (Input.Drag.HasValue || Input.Hold.HasValue)) {
137 Input.Clear();
138 }
139 }
140
141 public override void Draw(DrawContext dc) {
142 Color color = new Color((byte)80, (byte)80, (byte)80) * GlobalColorTransform * MathUtils.Saturate(m_scrollBarAlpha);
143 if (color.A > 0
144 && m_scrollAreaLength > 0f) {
146 int count = flatBatch2D.TriangleVertices.Count;
147 if (Direction == LayoutDirection.Horizontal) {
148 float scrollPosition = ScrollPosition;
149 float x = ActualSize.X;
150 Vector2 corner = new(scrollPosition / m_scrollAreaLength * x, ActualSize.Y - 5f);
151 Vector2 corner2 = new((scrollPosition + x) / m_scrollAreaLength * x, ActualSize.Y - 1f);
152 flatBatch2D.QueueQuad(corner, corner2, 0f, color);
153 }
154 else {
155 float scrollPosition2 = ScrollPosition;
156 float y = ActualSize.Y;
157 Vector2 corner3 = new(ActualSize.X - 5f, scrollPosition2 / m_scrollAreaLength * y);
158 Vector2 corner4 = new(ActualSize.X - 1f, (scrollPosition2 + y) / m_scrollAreaLength * y);
159 flatBatch2D.QueueQuad(corner3, corner4, 0f, color);
160 }
161 flatBatch2D.TransformTriangles(GlobalTransform, count);
162 }
163 }
164
165 public bool HitTestPanel(Vector2 position) {
166 bool found = false;
168 position,
169 delegate(Widget widget) {
170 found = widget.IsChildWidgetOf(this) || widget == this;
171 return true;
172 }
173 );
174 return found;
175 }
176 }
177}
Engine.Color Color
readonly DynamicArray< VertexPositionColor > TriangleVertices
void TransformTriangles(Matrix matrix, int start=0, int end=-1)
static readonly DepthStencilState None
void QueueQuad(Vector2 corner1, Vector2 corner2, float depth, Color color)
FlatBatch2D FlatBatch(int layer=0, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, BlendState blendState=null)
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static float FrameDuration
定义 Time.cs:46
readonly WidgetsList Children
static void ArrangeChildWidgetInCell(Vector2 c1, Vector2 c2, Widget widget)
bool HitTestPanel(Vector2 position)
override void Draw(DrawContext dc)
virtual LayoutDirection Direction
override void MeasureOverride(Vector2 parentAvailableSize)
virtual float CalculateScrollAreaLength()
readonly PrimitivesRenderer2D PrimitivesRenderer2D
virtual float MarginVerticalSum
Vector2 ParentDesiredSize
virtual Widget HitTestGlobal(Vector2 point, Func< Widget, bool > predicate=null)
virtual bool IsVisible
virtual float MarginHorizontalSum
WidgetInput Input
Color GlobalColorTransform
bool IsDrawRequired
Vector2 ActualSize
virtual Vector2 ScreenToWidget(Vector2 p)
Matrix GlobalTransform
virtual void Measure(Vector2 parentAvailableSize)
virtual bool IsChildWidgetOf(ContainerWidget containerWidget)
static readonly Vector2 Zero