This commit is contained in:
Даниил Заикин
2026-06-17 20:44:53 +03:00
parent 9d153773c2
commit b133e7c656
1880 changed files with 244545 additions and 0 deletions
@@ -0,0 +1,108 @@
using Blocks.Common;
using System.Collections.Generic;
using Unity.Properties;
using UnityEngine;
using UnityEngine.UIElements;
namespace Blocks.Sessions.Common
{
[UxmlElement]
public partial class CopySessionCodeElement : VisualElement
{
const string k_CopySessionCodeButtonText = "COPY";
[CreateProperty, UxmlAttribute]
public string SessionType
{
get => m_SessionType;
set
{
if (m_SessionType == value)
return;
m_SessionType = value;
if (panel != null)
UpdateBindings();
}
}
string m_SessionType;
CopySessionCodeViewModel m_ViewModel;
readonly List<DataBinding> m_Bindings = new();
public CopySessionCodeElement()
{
AddToClassList(BlocksTheme.ContainerHorizontal);
var hasSessionCode = new DataBinding
{
dataSourcePath = new PropertyPath(nameof(CopySessionCodeViewModel.HasSessionCode)),
bindingMode = BindingMode.ToTarget
};
SetBinding(new BindingId(nameof(enabledSelf)), hasSessionCode);
var sessionCode = new TextField
{
textEdition =
{
isReadOnly = true
},
selectAllOnMouseUp = false,
selectAllOnFocus = false
};
sessionCode.AddToClassList(BlocksTheme.TextField);
sessionCode.AddToClassList(BlocksTheme.SpaceRight);
var sessionCodeBinding = new DataBinding
{
dataSourcePath = new PropertyPath(nameof(CopySessionCodeViewModel.SessionCode)),
bindingMode = BindingMode.ToTarget
};
sessionCode.SetBinding("value", sessionCodeBinding);
Add(sessionCode);
m_Bindings.Add(sessionCodeBinding);
var copySessionCodeButton = new Button
{
text = k_CopySessionCodeButtonText
};
copySessionCodeButton.AddToClassList(BlocksTheme.Button);
copySessionCodeButton.clicked += CopySessionCode;
Add(copySessionCodeButton);
m_Bindings.Add(hasSessionCode);
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
}
void CopySessionCode()
{
if (string.IsNullOrEmpty(m_ViewModel?.SessionCode))
return;
GUIUtility.systemCopyBuffer = m_ViewModel.SessionCode;
}
void UpdateBindings()
{
CleanupBindings();
m_ViewModel = new CopySessionCodeViewModel(SessionType);
foreach (var binding in m_Bindings)
{
binding.dataSource = m_ViewModel;
}
}
void CleanupBindings()
{
m_ViewModel?.Dispose();
m_ViewModel = null;
foreach (var binding in m_Bindings)
{
binding.dataSource = null;
}
}
}
}
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: de2ba267bbd77504ab9606732bf832e0
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/CommonSession/Runtime/CopySessionCode/CopySessionCodeElement.cs
uploadId: 814574
@@ -0,0 +1,109 @@
using System;
using System.Runtime.CompilerServices;
using Unity.Properties;
using Unity.Services.Multiplayer;
using UnityEngine.UIElements;
namespace Blocks.Sessions.Common
{
public class CopySessionCodeViewModel : IDisposable, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
{
const string k_NoSessionText = "No Session Code";
SessionObserver m_SessionObserver;
ISession m_Session;
long m_UpdateVersion;
[CreateProperty]
public bool HasSessionCode
{
get => m_HasSessionCode;
private set
{
if (m_HasSessionCode == value)
return;
m_HasSessionCode = value;
Notify();
}
}
bool m_HasSessionCode;
[CreateProperty]
public string SessionCode
{
get => m_SessionCode;
private set
{
if (m_SessionCode == value)
return;
m_SessionCode = value;
HasSessionCode = m_SessionCode != k_NoSessionText;
++m_UpdateVersion;
Notify();
}
}
string m_SessionCode = k_NoSessionText;
public CopySessionCodeViewModel(string sessionType)
{
m_SessionObserver = new SessionObserver(sessionType);
m_SessionObserver.SessionAdded += OnSessionAdded;
if (m_SessionObserver.Session != null)
{
OnSessionAdded(m_SessionObserver.Session);
}
}
void OnSessionAdded(ISession session)
{
m_Session = session;
SessionCode = session?.Code;
m_Session.RemovedFromSession += OnSessionRemoved;
m_Session.Deleted += OnSessionRemoved;
}
void OnSessionRemoved()
{
m_Session.RemovedFromSession -= OnSessionRemoved;
m_Session.Deleted -= OnSessionRemoved;
SessionCode = k_NoSessionText;
m_Session = null;
}
public void Dispose()
{
if (m_SessionObserver != null)
{
m_SessionObserver.SessionAdded -= OnSessionAdded;
m_SessionObserver.Dispose();
m_SessionObserver = null;
}
if (m_Session != null)
{
m_Session.RemovedFromSession -= OnSessionRemoved;
m_Session.Deleted -= OnSessionRemoved;
m_Session = null;
}
}
/// <summary>
/// This method is used by UIToolkit to determine if any data bound to the UI has changed.
/// Instead of hashing the data, an m_UpdateVersion counter is incremented when changes occur.
/// </summary>
public long GetViewHashCode() => m_UpdateVersion;
/// <summary>
/// Suggested implementation of INotifyBindablePropertyChanged from UIToolkit.
/// </summary>
public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;
void Notify([CallerMemberName] string property = null) =>
propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(property));
}
}
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 95e390a0b04dc0b428a3a458c263ebde
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/CommonSession/Runtime/CopySessionCode/CopySessionCodeViewModel.cs
uploadId: 814574