---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by likan.
--- DateTime: 2019/12/23 10:05
---

ImguiDemo1 = ImguiDemo1 or class("imguiDemo1", nil);

function ImguiDemo1:ctor()
    self.m_size = ImVec2:new(100, 200);
    self.m_pos = ImVec2:new(200, 100);
    self.m_pos1 = ImVec2:new(100, 100);

    self.m_vec2Array = KVector:new();
end



function ImguiDemo1:dtor()
    if self.m_size ~= nil then
        self.m_size:delete();
        self.m_size = nil;
    end

    if self.m_pos ~= nil then
        self.m_pos:delete();
        self.m_pos = nil;
    end

    if self.m_pos1 ~= nil then
        self.m_pos1:delete();
        self.m_pos1 = nil;
    end
end

local bEnableTexture = true;

function ImguiDemo1:Init()
    local io = ImGui.GetIO();

    local fonts = io.Fonts;
    local glyp = fonts:GetGlyphRangesChineseFull();
    fonts:AddFontFromFileTTF("c:/windows/fonts/simhei.ttf", 13.0, nil, glyp);

    if bEnableTexture then
        local pTexturePool = X3D.GetTexturePool();
        -- self.m_pTexture = pTexturePool:Request("ui_source/ui/nv1.tga", true)
        self.m_pTexture = pTexturePool:Request("hbui/uiproject/build/images/loading_loading_bg.jpg", true)
        ---@type KTexture
        self.m_pTexture = tolua.cast(self.m_pTexture, "KLTexture");
        local pSRV = self.m_pTexture:GetRawShaderResourceView();
        self.m_TexutureId = ImGui.ToTextureId(pSRV);
        print("$$$$$$ImguiTextureId$$$$$$ = ", self.m_TexutureId);
    end
end

function ImguiDemo1:UnInit()
    if bEnableTexture then
        local pTexturePool = X3D.GetTexturePool();
        pTexturePool:Remove(self.m_pTexture);
        self.m_pTexture = nil;
    end
    self:ClearVec2Cache();
end

function bool2Int(v)
    if v then
        return 1;
    else
        return 0;
    end
end


---@param x number
---@param y number
function ImguiDemo1:AllocVec2(x, y)
    ---@type ImVec2
    local value = self.m_vec2Array:PopBack();
    if value == nil then
        value = ImVec2:new();
    end
    if x ~= nil then
        value.x = x;
    end
    if y~= nil then
        value.y = y;
    end
    return value;
end

---@param value ImVec2
function ImguiDemo1:FreeVec2(value)
    self.m_vec2Array:PushBack(value);
end

function ImguiDemo1:ClearVec2Cache()
    local size = self.m_vec2Array:Size();
    for i = 1, size do
        ---@type ImVec2
        local value = self.m_vec2Array:Get(i);
        value:delete();
    end
    self.m_vec2Array:Clear();
end

function calla()
  local b = 0;
end

szInputText = 'hi, Chinese boy'
function ImguiDemo1:Render()
    --如果一个函数用emmylua调试经常失效，那么在换个函数头加一个全局函数调用，那么这这个函数调试就不会再断点调试失效了
    --这是为什么，我也不知道，暂时这么绕吧
    --calla();

    local vec2 = self:AllocVec2(100, 200);
    ImGui.SetNextWindowContentSize(vec2);

    --vec2.x = 200; vec2.y = 100;
    --ImGui.SetNextWindowPos(vec2);

    local bOpen = false;
    _, bOpen = ImGui.Begin("Hi", bOpen);
    ImGui.InputText("a", szInputText, #szInputText);
    if ImGui.Button("click me") then
        LOG_I("clicked ...");
    end

    if bEnableTexture then
        vec2.x = 100; vec2.y = 100;
        if ImGui.ImageButton(self.m_TexutureId, vec2) then
            LOG_I("click image button..");
        end
    end

    ImGui.End();
    self:FreeVec2(vec2);
end
