Skip to content

Commit 675f550

Browse files
authored
Pause menu 3 (#3663)
* Add TextureFlags * Add TextureFlags.PremultipliedAlpha in text block and webpanel textures * Add BlendMode.PremultipliedAlpha * Add panel to PauseModal
1 parent 677282f commit 675f550

11 files changed

Lines changed: 157 additions & 34 deletions

File tree

engine/Sandbox.Engine/Resources/Textures/Texture.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public partial class Texture : Resource, IDisposable
3232

3333
public override bool IsValid => native.IsValid;
3434

35+
/// <summary>
36+
/// Flags providing hints about this texture
37+
/// </summary>
38+
public TextureFlags Flags { get; set; } = TextureFlags.None;
39+
3540
/// <summary>
3641
/// Private constructor, use <see cref="FromNative(ITexture)"/>
3742
/// </summary>
@@ -304,3 +309,16 @@ internal async Task ReplacementAsync( Task<Texture> task )
304309
IsLoaded = true;
305310
}
306311
}
312+
313+
/// <summary>
314+
/// Flags providing hints about a texture
315+
/// </summary>
316+
public enum TextureFlags
317+
{
318+
None = 0,
319+
320+
/// <summary>
321+
/// Hint that this texture has pre-multiplied alpha
322+
/// </summary>
323+
PremultipliedAlpha = 1 << 0,
324+
}

engine/Sandbox.Engine/Systems/Render/BlendMode.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ public enum BlendMode
1313
// Screen = 2,
1414
// Overlay = 3,
1515
// Darken = 4,
16-
Lighten = 2, // 5
17-
// ColorDodge = 6,
18-
// ColorBurn = 7,
19-
// HardLight = 8,
20-
// SoftLight = 9,
21-
// Difference = 10,
22-
// Exclusion = 11,
16+
Lighten = 2,
17+
PremultipliedAlpha = 3,
18+
// 5
19+
// ColorDodge = 6,
20+
// ColorBurn = 7,
21+
// HardLight = 8,
22+
// SoftLight = 9,
23+
// Difference = 10,
24+
// Exclusion = 11,
25+
26+
27+
2328

2429
}
2530
}

engine/Sandbox.Engine/Systems/UI/Controls/WebPanel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Web;
1+
using Microsoft.AspNetCore.Components;
2+
using System.Web;
23

34
namespace Sandbox.UI;
45

@@ -12,7 +13,7 @@ public class WebPanel : Panel
1213
/// </summary>
1314
public WebSurface Surface { get; private set; }
1415

15-
16+
[Parameter]
1617
public string Url
1718
{
1819
get => Surface.Url;
@@ -52,6 +53,8 @@ private void BrowserDataChanged( ReadOnlySpan<byte> span, Vector2 size )
5253
.WithName( "WebPanel" )
5354
.Finish();
5455

56+
sufaceTexture.Flags |= TextureFlags.PremultipliedAlpha;
57+
5558
Style.SetBackgroundImage( sufaceTexture );
5659
}
5760

engine/Sandbox.Engine/Systems/UI/Engine/TextBlock.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,17 @@ internal void Render( PanelRenderer renderer, ref RenderState state, Styles curr
157157

158158
if ( color.a <= 0 ) return;
159159

160+
var bm = renderer.OverrideBlendMode;
161+
162+
if ( bm == BlendMode.Normal && Texture.Flags.Contains( TextureFlags.PremultipliedAlpha ) )
163+
bm = BlendMode.PremultipliedAlpha;
164+
160165
textAttr.Set( "BoxPosition", textrect.Position );
161166
textAttr.Set( "BoxSize", textrect.Size );
162167

163168
textAttr.Set( "TextureIndex", Texture.Index );
164169
textAttr.Set( "SamplerIndex", SamplerState.GetBindlessIndex( new SamplerState() { Filter = TextFilter } ) );
165-
textAttr.SetComboEnum( "D_BLENDMODE", renderer.OverrideBlendMode );
170+
textAttr.SetComboEnum( "D_BLENDMODE", bm );
166171

167172
Graphics.DrawQuad( textrect.Floor(), Material.UI.Text, color, textAttr );
168173
}
@@ -543,7 +548,7 @@ unsafe void RebuildTexture( float maxwidth, float maxheight )
543548

544549
using var perfScope = Performance.Scope( "TextBlock.RebuildTexture" );
545550

546-
using ( var bitmap = new SKBitmap( width, height, SKColorType.Bgra8888, SKAlphaType.Unpremul ) )
551+
using ( var bitmap = new SKBitmap( width, height, SKColorType.Bgra8888, SKAlphaType.Premul ) )
547552
using ( var canvas = new SKCanvas( bitmap ) )
548553
{
549554
var o = new Topten.RichTextKit.TextPaintOptions
@@ -599,6 +604,8 @@ unsafe void RebuildTexture( float maxwidth, float maxheight )
599604
.WithData( bitmap.GetPixels(), width * height * bitmap.BytesPerPixel )
600605
.WithDynamicUsage()
601606
.Finish();
607+
608+
Texture.Flags |= TextureFlags.PremultipliedAlpha;
602609
}
603610
}
604611

engine/Sandbox.Engine/Systems/UI/Panel/Panel.Render.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Sandbox.Rendering;
2-
using System.Runtime.InteropServices;
32

43
namespace Sandbox.UI;
54

@@ -70,7 +69,12 @@ internal virtual void DrawBackground( PanelRenderer renderer, ref RenderState st
7069

7170
if ( bgBlendMode == BlendMode.Normal || ComputedStyle.BackgroundImage == null )
7271
{
73-
bgAttribs.SetComboEnum( "D_BLENDMODE", renderer.OverrideBlendMode );
72+
BlendMode bm = bgBlendMode;
73+
74+
if ( bm == BlendMode.Normal && (ComputedStyle.BackgroundImage?.Flags.Contains( TextureFlags.PremultipliedAlpha ) ?? false) )
75+
bm = BlendMode.PremultipliedAlpha;
76+
77+
bgAttribs.SetComboEnum( "D_BLENDMODE", bm );
7478
bgAttribs.Set( "Texture", ComputedStyle.BackgroundImage );
7579
Graphics.DrawQuad( rect, Material.UI.Box, color, bgAttribs );
7680
}

game/addons/base/Assets/shaders/common/blendmode.hlsl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// Blend Modes (https://web.dev/learn/css/blend-modes/)
55
// I only filled in what I needed. A job for someone else - garry
66
//
7-
DynamicCombo( D_BLENDMODE, 0..2, Sys( ALL ) );
7+
DynamicCombo( D_BLENDMODE, 0..3, Sys( ALL ) );
88

9-
// Alpha Blend
9+
// Alpha Blend (standard)
1010
#if D_BLENDMODE == 0
1111
RenderState( BlendEnable, true );
1212
RenderState( SrcBlend, SRC_ALPHA );
@@ -15,18 +15,34 @@ DynamicCombo( D_BLENDMODE, 0..2, Sys( ALL ) );
1515
RenderState( SrcBlendAlpha, ONE );
1616
RenderState( DstBlendAlpha, INV_SRC_ALPHA );
1717
RenderState( BlendOpAlpha, ADD );
18+
1819
// Multiply
1920
#elif D_BLENDMODE == 1
2021
RenderState( BlendEnable, true );
2122
RenderState( SrcBlend, DEST_COLOR );
22-
RenderState( DstBlend, INV_SRC_ALPHA );
23+
RenderState( DstBlend, ZERO );
24+
RenderState( BlendOp, ADD );
2325
RenderState( SrcBlendAlpha, ONE );
24-
RenderState( DstBlendAlpha, ONE );
25-
// Lighten
26+
RenderState( DstBlendAlpha, ZERO );
27+
RenderState( BlendOpAlpha, ADD );
28+
29+
// Lighten / Additive
2630
#elif D_BLENDMODE == 2
2731
RenderState( BlendEnable, true );
2832
RenderState( SrcBlend, SRC_ALPHA );
2933
RenderState( DstBlend, ONE );
34+
RenderState( BlendOp, ADD );
3035
RenderState( SrcBlendAlpha, ONE );
3136
RenderState( DstBlendAlpha, ONE );
32-
#endif
37+
RenderState( BlendOpAlpha, ADD );
38+
39+
// Premultiplied Alpha
40+
#elif D_BLENDMODE == 3
41+
RenderState( BlendEnable, true );
42+
RenderState( SrcBlend, ONE );
43+
RenderState( DstBlend, INV_SRC_ALPHA );
44+
RenderState( BlendOp, ADD );
45+
RenderState( SrcBlendAlpha, ONE );
46+
RenderState( DstBlendAlpha, INV_SRC_ALPHA );
47+
RenderState( BlendOpAlpha, ADD );
48+
#endif

game/addons/base/Assets/shaders/ui_cssbox.shader

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,18 @@ PS
329329
}
330330

331331
vImage.xyz = SrgbGammaToLinear( vImage.xyz );
332-
vImage *= bgTint;
332+
333+
#if ( D_BLENDMODE == 3 ) // PREMULIPLIED
334+
vImage.rgb *= bgTint.rgb;
335+
vImage *= bgTint.a;
336+
#else
337+
vImage *= bgTint;
338+
#endif
333339

334340
vBox.rgb = lerp( vBox.rgb, vImage.rgb, saturate( vImage.a + ( 1 - vBox.a ) ) );
335341
vBox.a = max( vBox.a, vImage.a );
342+
343+
336344
}
337345

338346
o.vColor = vBox;

game/addons/base/Assets/shaders/ui_text.shader

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,13 @@ PS
9696
Texture2D tex = GetBindlessTexture2D( TextureIndex + 1 );
9797
float4 vColor = tex.SampleBias( sampler, vTexCoord, mipBias );
9898

99-
float flAlphaScale = 1.0f;
100-
101-
float lum = saturate(dot( float3(0.30, 0.59, 0.11), vColor.rgb ) - 0.2);
102-
float alpha = vColor.a;
103-
alpha = pow(alpha, 0.6 + (lum) * 3 );
104-
105-
vColor.a = alpha * flAlphaScale;
106-
10799
o.vColor = vColor;
108-
o.vColor.a *= i.vColor.a;
100+
101+
#if ( D_BLENDMODE == 3 )
102+
o.vColor *= i.vColor.a;
103+
#else
104+
o.vColor.a *= i.vColor.a;
105+
#endif
109106

110107
// Apply fog only on world panels
111108
#if D_WORLDPANEL

game/addons/menu/Code/ModalSystem.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ public class ModalSystem : IModalSystem
1010

1111
List<BaseModal> OpenModals = new List<BaseModal>();
1212

13+
PauseModal _pauseModal;
14+
1315
public ModalSystem()
1416
{
1517
Instance = this;
1618
}
1719

1820
public bool HasModalsOpen()
1921
{
22+
if ( IsPauseMenuOpen )
23+
return true;
24+
2025
return OpenModals.Any( x => x.WantsMouseInput() );
2126
}
2227

@@ -167,8 +172,16 @@ public void PauseMenu()
167172
return;
168173
}
169174

170-
var modal = new PauseModal();
171-
Push( modal );
175+
_pauseModal = MenuOverlay.Instance.Children.OfType<PauseModal>().FirstOrDefault();
176+
177+
if ( _pauseModal != null )
178+
{
179+
_pauseModal.ToggleClass( "hidden" );
180+
return;
181+
}
182+
183+
_pauseModal = new PauseModal();
184+
MenuOverlay.Instance.AddChild( _pauseModal );
172185
}
173186

174187
public void Player( SteamId steamid, string page = "" )
@@ -190,6 +203,6 @@ public void WorkshopPublish( in WorkshopPublishOptions options )
190203
Push( new WorkshopPublishModal { Options = options } );
191204
}
192205

193-
public bool IsModalOpen => OpenModals.Any();
194-
public bool IsPauseMenuOpen => OpenModals.OfType<PauseModal>().Any();
206+
public bool IsModalOpen => HasModalsOpen();
207+
public bool IsPauseMenuOpen => _pauseModal.IsValid() && _pauseModal.IsPauseMenuOpen();
195208
}

game/addons/menu/code/Modals/PauseMenuModal/PauseModal.razor

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
</div>
2121

2222
<div class="right">
23-
23+
24+
@{
25+
var ident = game?.FullIdent.Split('#')[0] ?? "unknown/unknown";
26+
var url = $"https://sbox.game/game/pause/{ident}";
27+
//var url = $"https://localhost:44338/game/pause/{ident}";
28+
<WebPanel class="browser" Url="@url"></WebPanel>
29+
}
30+
2431
</div>
2532

2633
</div>
@@ -108,4 +115,36 @@
108115
StateHasChanged();
109116
}
110117
}
118+
119+
public override void Tick()
120+
{
121+
base.Tick();
122+
123+
CheckForDelete();
124+
}
125+
126+
bool CheckForDelete()
127+
{
128+
if (MenuUtility.GamePackage == null)
129+
{
130+
Delete();
131+
return true;
132+
}
133+
134+
if (MenuUtility.GamePackage?.Ident != game?.Ident)
135+
{
136+
Delete();
137+
return true;
138+
}
139+
140+
return false;
141+
}
142+
143+
public bool IsPauseMenuOpen()
144+
{
145+
if (CheckForDelete() )
146+
return false;
147+
148+
return !HasClass( "hidden" );
149+
}
111150
}

0 commit comments

Comments
 (0)