refactor: Converted message data to its own struct

This commit is contained in:
Gerard Gascón 2024-01-27 15:42:31 +01:00
parent 1fc809141c
commit 9dd340eeb7
10 changed files with 339 additions and 18 deletions

View file

@ -232,6 +232,7 @@ RectTransform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 2089551592} - {fileID: 2089551592}
- {fileID: 1152326132}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
@ -373,6 +374,140 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 468556174} m_GameObject: {fileID: 468556174}
m_CullTransparentMesh: 1 m_CullTransparentMesh: 1
--- !u!1 &502585124
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 502585125}
- component: {fileID: 502585127}
- component: {fileID: 502585126}
m_Layer: 5
m_Name: Text (TMP)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &502585125
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 502585124}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1152326132}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &502585126
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 502585124}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: Send
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4281479730
m_fontColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 24
m_fontSizeBase: 24
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 2
m_VerticalAlignment: 512
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!222 &502585127
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 502585124}
m_CullTransparentMesh: 1
--- !u!1001 &516388491 --- !u!1001 &516388491
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -894,6 +1029,153 @@ MonoBehaviour:
maxVisibleMessages: 5 maxVisibleMessages: 5
messageHeight: 16 messageHeight: 16
messagePrefab: {fileID: -6056302822753658535, guid: 6bf006df27e33a24088a28c9d46dbb43, type: 3} messagePrefab: {fileID: -6056302822753658535, guid: 6bf006df27e33a24088a28c9d46dbb43, type: 3}
--- !u!1 &1152326131
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1152326132}
- component: {fileID: 1152326136}
- component: {fileID: 1152326135}
- component: {fileID: 1152326134}
- component: {fileID: 1152326133}
m_Layer: 5
m_Name: SendButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1152326132
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152326131}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 502585125}
m_Father: {fileID: 23275630}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -80, y: -169.5}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1152326133
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152326131}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3f321cddc02443098ca224011b58aa9e, type: 3}
m_Name:
m_EditorClassIdentifier:
inputField: {fileID: 23275626}
--- !u!114 &1152326134
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152326131}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 1152326135}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1152326133}
m_TargetAssemblyTypeName: Messaging.Composer.SendButton, Assembly-CSharp
m_MethodName: SendEmojis
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &1152326135
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152326131}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &1152326136
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152326131}
m_CullTransparentMesh: 1
--- !u!1 &1284257431 --- !u!1 &1284257431
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -6,6 +6,8 @@ namespace Messaging.Composer {
public class InputField : MonoBehaviour { public class InputField : MonoBehaviour {
[SerializeField] private TMP_Text fieldText; [SerializeField] private TMP_Text fieldText;
public string Text => fieldText.text;
public void EmptyText() { public void EmptyText() {
fieldText.SetText(""); fieldText.SetText("");
} }

View file

@ -0,0 +1,25 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Messaging.Composer {
public class SendButton : MonoBehaviour {
[SerializeField] private InputField inputField;
private MessageManager _messageManager;
private void Awake() {
_messageManager = FindObjectOfType<MessageManager>();
}
public void SendEmojis() {
if (CanSendText()) {
_messageManager.CreateMessage(inputField.Text, true);
}
}
private bool CanSendText() {
return !string.IsNullOrEmpty(inputField.Text) && !string.IsNullOrWhiteSpace(inputField.Text);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3f321cddc02443098ca224011b58aa9e
timeCreated: 1706364183

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 25fe495ec1e84081bb966070ed1d4e10
timeCreated: 1706365431

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace Messaging.MessageData {
public struct MessageData {
public KeyValuePair<string, string> QuestionMessage;
public KeyValuePair<string, string> AnswerMessage;
public MessageData(string text) {
string[] jokeParts = text.Split(':');
string[] question = jokeParts[0].Split('|');
string[] answer = jokeParts[1].Split('|');
QuestionMessage = new KeyValuePair<string, string>(question[0], question[1]);
AnswerMessage = new KeyValuePair<string, string>(answer[0], answer[1]);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a53c45e3eeca441d81be59b5e03893bc
timeCreated: 1706365538

View file

@ -1,12 +0,0 @@
using System.Collections.Generic;
namespace Messaging {
public static class MessageParser {
private const char MessageDifferentiator = '|';
public static KeyValuePair<string, string> SplitMessage(string message) {
string[] result = message.Split(MessageDifferentiator);
return new KeyValuePair<string, string>(result[0], result[1]);
}
}
}

View file

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: d7e3a5f1cf5d487eab9b2b5e232efeda
timeCreated: 1706346013

View file

@ -16,14 +16,14 @@ namespace Messaging {
} }
private async void Start() { private async void Start() {
string[] fLines = textAsset.text.Split(':'); MessageData.MessageData data = new(textAsset.text);
int i = 0; int i = 0;
while (Application.isPlaying) { while (Application.isPlaying) {
bool isAnswer = i % 2 != 0; bool isAnswer = i % 2 != 0;
CreateMessage(MessageParser.SplitMessage(fLines[i % 2]).Key, isAnswer); CreateMessage(data.QuestionMessage.Key, isAnswer);
await Task.Delay(2000); await Task.Delay(2000);
CreateRealMessage(MessageParser.SplitMessage(fLines[i % 2]).Value, isAnswer); CreateRealMessage(data.QuestionMessage.Value, isAnswer);
await Task.Delay(2000); await Task.Delay(2000);
++i; ++i;
} }