summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/GUI/Font.bind.cpp
blob: 8759fe1df9029d4232b0eff6e0ec36a95ef61db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Runtime/GUI/Font.h"
#include "Runtime/Graphics/Shader.h"
#include "Runtime/Debug/Log.h"
#include "Runtime/Graphics/GfxDevice.h"
#include "Runtime/Common/DataBuffer.h"
#include "Runtime/GUI/utf8.h"
#include "Runtime/Utilities/StaticInitiator.h"
#include "Runtime/GUI/UITextMesh.h"
#include "Runtime/Math/Math.h"
#include "Runtime/GUI/TextMeshGenerator.h"
#include "Runtime/Utilities/AutoInvoke.h"

LUA_BIND_REGISTRY(Font)
{
    LUA_BIND_REGISTER_METHODS(state,
        { "New", _New }
    );
}

LUA_BIND_POSTPROCESS(Font)
{
}

// Font.New(ttfPath, atlasSize, margin, padding)
// Font.New(ttfData, atlasSize, margin, padding)
LUA_BIND_IMPL_METHOD(Font, _New)
{
    LUA_BIND_STATE(L);
    LUA_BIND_CHECK(L, "STNN!|UTNN!");

    Font* font = NULL;
    if (state.CheckParams(1, "STNN!")) 
    {
        const char* path = state.GetValue<const char*>(1, "");
        TextGeneratingSettings setting;
        setting.atlasSize = state.GetValue<Vector2>(2, Vector2(128, 128));
        setting.margin = state.GetValue<int>(3, 0);
        setting.padding = state.GetValue<int>(4, 0);
        try {
            font = new Font(state.GetVM(), path, setting);
        }
        catch (FontException& e)
        {
            log_error(e.what());
            state.PushNil();
            return 1;
        }
    }
    else if (state.CheckParams(1, "UTNN!")) 
    {
        DataBuffer* buffer = state.GetUserdata<DataBuffer>(1);
        if (buffer == NULL)
        {
            log_error("Create font failed, DataBuffer is invalid.");
            state.PushNil();
            return 1;
        }
        TextGeneratingSettings setting;
        setting.atlasSize = state.GetValue<Vector2>(2, Vector2(128, 128));
        setting.margin = state.GetValue<int>(3, 0);
        setting.padding = state.GetValue<int>(4, 0);
        try {
            font = new Font(state.GetVM(), buffer, setting);
        }
        catch (FontException& e)
        {
            log_error(e.what());
            state.PushNil();
            return 1;
        }
    }

    font->PushUserdata(state);

    return 1;
}