Skip to content

Commit

Permalink
Exposed TextComponent to C#
Browse files Browse the repository at this point in the history
- Added Vector4 and some script utils
  • Loading branch information
TheCherno committed Mar 20, 2023
1 parent be419f6 commit 5e20b23
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 6 deletions.
30 changes: 28 additions & 2 deletions Hazel-ScriptCore/Source/Hazel/InternalCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ namespace Hazel
{
public static class InternalCalls
{
#region Entity
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static bool Entity_HasComponent(ulong entityID, Type componentType);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static ulong Entity_FindEntityByName(string name);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static object GetScriptInstance(ulong entityID);
#endregion

#region TransformComponent
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TransformComponent_GetTranslation(ulong entityID, out Vector3 translation);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TransformComponent_SetTranslation(ulong entityID, ref Vector3 translation);
#endregion

#region Rigidbody2DComponent
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void Rigidbody2DComponent_ApplyLinearImpulse(ulong entityID, ref Vector2 impulse, ref Vector2 point, bool wake);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand All @@ -26,10 +30,32 @@ public static class InternalCalls
internal extern static Rigidbody2DComponent.BodyType Rigidbody2DComponent_GetType(ulong entityID);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void Rigidbody2DComponent_SetType(ulong entityID, Rigidbody2DComponent.BodyType type);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void Rigidbody2DComponent_ApplyLinearImpulseToCenter(ulong entityID, ref Vector2 impulse, bool wake);
#endregion

#region TextComponent
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static string TextComponent_GetText(ulong entityID);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TextComponent_SetText(ulong entityID, string text);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TextComponent_GetColor(ulong entityID, out Vector4 color);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TextComponent_SetColor(ulong entityID, ref Vector4 color);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static float TextComponent_GetKerning(ulong entityID);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TextComponent_SetKerning(ulong entityID, float kerning);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static float TextComponent_GetLineSpacing(ulong entityID);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void TextComponent_SetLineSpacing(ulong entityID, float lineSpacing);
#endregion

#region Rigidbody2DComponent
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static bool Input_IsKeyDown(KeyCode keycode);
#endregion
}
}
40 changes: 39 additions & 1 deletion Hazel-ScriptCore/Source/Hazel/Scene/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BodyType Type
get => InternalCalls.Rigidbody2DComponent_GetType(Entity.ID);
set => InternalCalls.Rigidbody2DComponent_SetType(Entity.ID, value);
}

public void ApplyLinearImpulse(Vector2 impulse, Vector2 worldPosition, bool wake)
{
InternalCalls.Rigidbody2DComponent_ApplyLinearImpulse(Entity.ID, ref impulse, ref worldPosition, wake);
Expand All @@ -57,4 +57,42 @@ public void ApplyLinearImpulse(Vector2 impulse, bool wake)
}

}

public class TextComponent : Component
{

public string Text
{
get => InternalCalls.TextComponent_GetText(Entity.ID);
set => InternalCalls.TextComponent_SetText(Entity.ID, value);
}

public Vector4 Color
{
get
{
InternalCalls.TextComponent_GetColor(Entity.ID, out Vector4 color);
return color;
}

set
{
InternalCalls.TextComponent_SetColor(Entity.ID, ref value);
}
}

public float Kerning
{
get => InternalCalls.TextComponent_GetKerning(Entity.ID);
set => InternalCalls.TextComponent_SetKerning(Entity.ID, value);
}

public float LineSpacing
{
get => InternalCalls.TextComponent_GetLineSpacing(Entity.ID);
set => InternalCalls.TextComponent_SetLineSpacing(Entity.ID, value);
}

}

}
65 changes: 65 additions & 0 deletions Hazel-ScriptCore/Source/Hazel/Vector4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Hazel
{
public struct Vector4
{
public float X, Y, Z, W;

public static Vector4 Zero => new Vector4(0.0f);

public Vector4(float scalar)
{
X = scalar;
Y = scalar;
Z = scalar;
W = scalar;
}

public Vector4(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}

public Vector4(Vector3 xyz, float w)
{
X = xyz.X;
Y = xyz.Y;
Z = xyz.Z;
W = w;
}

public Vector2 XY
{
get => new Vector2(X, Y);
set
{
X = value.X;
Y = value.Y;
}
}

public Vector3 XYZ
{
get => new Vector3(X, Y, Z);
set
{
X = value.X;
Y = value.Y;
Z = value.Z;
}
}

public static Vector4 operator +(Vector4 a, Vector4 b)
{
return new Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
}

public static Vector4 operator *(Vector4 vector, float scalar)
{
return new Vector4(vector.X * scalar, vector.Y * scalar, vector.Z * scalar, vector.W * scalar);
}

}
}
5 changes: 5 additions & 0 deletions Hazel/src/Hazel/Scripting/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ namespace Hazel {
return s_Data->EntityInstances.at(uuid)->GetManagedObject();
}

MonoString* ScriptEngine::CreateString(const char* string)
{
return mono_string_new(s_Data->AppDomain, string);
}

MonoObject* ScriptEngine::InstantiateClass(MonoClass* monoClass)
{
MonoObject* instance = mono_object_new(s_Data->AppDomain, monoClass);
Expand Down
3 changes: 3 additions & 0 deletions Hazel/src/Hazel/Scripting/ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
typedef struct _MonoAssembly MonoAssembly;
typedef struct _MonoImage MonoImage;
typedef struct _MonoClassField MonoClassField;
typedef struct _MonoString MonoString;
}

namespace Hazel {
Expand Down Expand Up @@ -166,6 +167,8 @@ namespace Hazel {
static MonoImage* GetCoreAssemblyImage();

static MonoObject* GetManagedInstance(UUID uuid);

static MonoString* CreateString(const char* string);
private:
static void InitMono();
static void ShutdownMono();
Expand Down
121 changes: 118 additions & 3 deletions Hazel/src/Hazel/Scripting/ScriptGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@

namespace Hazel {

namespace Utils {

std::string MonoStringToString(MonoString* string)
{
char* cStr = mono_string_to_utf8(string);
std::string str(cStr);
mono_free(cStr);
return str;
}

}

static std::unordered_map<MonoType*, std::function<bool(Entity)>> s_EntityHasComponentFuncs;

#define HZ_ADD_INTERNAL_CALL(Name) mono_add_internal_call("Hazel.InternalCalls::" #Name, Name)

static void NativeLog(MonoString* string, int parameter)
{
char* cStr = mono_string_to_utf8(string);
std::string str(cStr);
mono_free(cStr);
std::string str = Utils::MonoStringToString(string);
std::cout << str << ", " << parameter << std::endl;
}

Expand Down Expand Up @@ -155,6 +165,102 @@ namespace Hazel {
body->SetType(Utils::Rigidbody2DTypeToBox2DBody(bodyType));
}

static MonoString* TextComponent_GetText(UUID entityID)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
return ScriptEngine::CreateString(tc.TextString.c_str());
}

static void TextComponent_SetText(UUID entityID, MonoString* textString)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
tc.TextString = Utils::MonoStringToString(textString);
}

static void TextComponent_GetColor(UUID entityID, glm::vec4* color)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
*color = tc.Color;
}

static void TextComponent_SetColor(UUID entityID, glm::vec4* color)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
tc.Color = *color;
}

static float TextComponent_GetKerning(UUID entityID)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
return tc.Kerning;
}

static void TextComponent_SetKerning(UUID entityID, float kerning)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
tc.Kerning = kerning;
}

static float TextComponent_GetLineSpacing(UUID entityID)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
return tc.LineSpacing;
}

static void TextComponent_SetLineSpacing(UUID entityID, float lineSpacing)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene);
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity);
HZ_CORE_ASSERT(entity.HasComponent<TextComponent>());

auto& tc = entity.GetComponent<TextComponent>();
tc.LineSpacing = lineSpacing;
}

static bool Input_IsKeyDown(KeyCode keycode)
{
return Input::IsKeyPressed(keycode);
Expand Down Expand Up @@ -211,6 +317,15 @@ namespace Hazel {
HZ_ADD_INTERNAL_CALL(Rigidbody2DComponent_GetLinearVelocity);
HZ_ADD_INTERNAL_CALL(Rigidbody2DComponent_GetType);
HZ_ADD_INTERNAL_CALL(Rigidbody2DComponent_SetType);

HZ_ADD_INTERNAL_CALL(TextComponent_GetText);
HZ_ADD_INTERNAL_CALL(TextComponent_SetText);
HZ_ADD_INTERNAL_CALL(TextComponent_GetColor);
HZ_ADD_INTERNAL_CALL(TextComponent_SetColor);
HZ_ADD_INTERNAL_CALL(TextComponent_GetKerning);
HZ_ADD_INTERNAL_CALL(TextComponent_SetKerning);
HZ_ADD_INTERNAL_CALL(TextComponent_GetLineSpacing);
HZ_ADD_INTERNAL_CALL(TextComponent_SetLineSpacing);

HZ_ADD_INTERNAL_CALL(Input_IsKeyDown);
}
Expand Down

0 comments on commit 5e20b23

Please sign in to comment.