Código:
#include "StdAfx.h"
#include "imgui.hpp"
#include "SEASON3B.h"
#include "NewUIGui.h"
CNewUIGui::CNewUIGui(void)
{
show_in_game_browser_window = true;
show_demo_window = true;
show_another_window = true;
}
CNewUIGui::~CNewUIGui(void)
{
this->Release();
}
void CNewUIGui::Create()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplWin32_Init(g_hWnd);
ImGui_ImplOpenGL2_Init();
}
void CNewUIGui::Release()
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui_ImplOpenGL2_Shutdown();
ImGui::DestroyContext();
}
IMGUI_IMPL_API LRESULT CNewUIGui::UpdateProcHandelr(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (context == NULL) return 0;
if (context->HoveredWindow == NULL) return 0;
ImGuiIO& io = ImGui::GetIO();
switch (msg)
{
case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
{
int button = 0;
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
::SetCapture(hwnd);
io.MouseDown[button] = true;
return 1;
}
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
{
int button = 0;
if (msg == WM_LBUTTONUP) { button = 0; }
if (msg == WM_RBUTTONUP) { button = 1; }
if (msg == WM_MBUTTONUP) { button = 2; }
if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
io.MouseDown[button] = false;
if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
::ReleaseCapture();
return 1;
}
case WM_MOUSEWHEEL:
io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
return 1;
case WM_MOUSEHWHEEL:
io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
return 1;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (wParam < 256)
{
io.KeysDown[wParam] = 1;
}
return 1;
case WM_KEYUP:
case WM_SYSKEYUP:
if (wParam < 256)
io.KeysDown[wParam] = 0;
return 1;
case WM_KILLFOCUS:
memset(io.KeysDown, 0, sizeof(io.KeysDown));
return 1;
case WM_CHAR:
if (wParam > 0 && wParam < 0x10000)
io.AddInputCharacterUTF16((unsigned short)wParam);
return 1;
}
return 0;
}
bool CNewUIGui::UpdateMouseEvent()
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (context == NULL) return 0;
if (context->HoveredWindow == NULL) return 0;
return true;
}
void CNewUIGui::RenderDemo(bool arc1, bool arc2)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::Checkbox("In Game Browser", &show_in_game_browser_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
}
int CNewUIGui::CreateColorEdit(char* label, float* clcolor)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return 0;
ImGui::ColorEdit3("clear color", clcolor);
ImVec4 color = ImVec4(clcolor[0], clcolor[1], clcolor[2], 1.0f);
ImU32 colorInt = ImGui::ColorConvertFloat4ToU32(color);
return colorInt;
}
void CNewUIGui::WindowsEmtyp(bool no_titlebar, bool no_scrollbar, bool no_menu, bool no_move, bool no_resize, bool no_collapse, bool no_close, bool no_nav, bool no_background, bool no_bring_to_front)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGuiWindowFlags window_flags = 0;
if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar;
if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar;
if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar;
if (no_move) window_flags |= ImGuiWindowFlags_NoMove;
if (no_resize) window_flags |= ImGuiWindowFlags_NoResize;
if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse;
if (no_nav) window_flags |= ImGuiWindowFlags_NoNav;
if (no_background) window_flags |= ImGuiWindowFlags_NoBackground;
if (no_bring_to_front) window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus;
//if (no_close) p_open = NULL;
bool p_open;
ImGui::SetNextWindowSize(ImVec2(1, 1), ImGuiCond_FirstUseEver);
ImGui::Begin(":", NULL, window_flags);
}
void CNewUIGui::ComboBoxWithIntStringPair(const char* label, int& selectedIndex, std::vector<std::pair<int, std::string>>& items)
{
ImGui::SameLine();
static const char* currentItem = NULL;
static int currentIndex = 0;
int itemCount = (int)items.size();
std::vector<const char*> itemsAsCharArray;
for (int i = 0; i < itemCount; i++)
{
itemsAsCharArray.push_back(items[i].second.c_str());
}
for (int i = 0; i < itemCount; i++)
{
if (selectedIndex == items[i].first && currentIndex != i || currentItem == NULL)
{
currentItem = items[i].second.c_str();
currentIndex = i;
selectedIndex = items[i].first;
i = itemCount;
}
}
if (ImGui::BeginCombo(label, currentItem))
{
for (int i = 0; i < itemCount; i++)
{
const bool isSelected = (currentIndex == i);
if (ImGui::Selectable(items[i].second.c_str(), isSelected))
{
currentItem = items[i].second.c_str();
currentIndex = i;
selectedIndex = items[i].first;
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
}
void CNewUIGui::AddImGuiInputfloat(char* id, float& value)
{
ImGui::InputFloat(id, &value, 0, 0, "%.5f");
}
void CNewUIGui::AddImGuiInputInt(char* id, int& value)
{
ImGui::InputInt(id, &value);
}
void CNewUIGui::AddGroupRadioButton(int Numeador, int& Selected)
{
for (int i = 0; i < Numeador; i++)
{
char Name[50] = {'\0',};
sprintf(Name, "Opcion %d", 1 + i);
if (i > 0) ImGui::SameLine();
if (ImGui::RadioButton(Name, Selected == i))
{
Selected = i;
}
}
}
void CNewUIGui::InitChild(char* id, int x, int y, float width, float height)
{
if (x > 0)
{
ImGui::SetCursorPosX(x);
}
if (y > 0)
{
ImGui::SetCursorPosY(y);
}
ImGui::BeginChild(id, ImVec2(width, height), true);
}
void CNewUIGui::EndChild()
{
ImGui::EndChild();
}
void CNewUIGui::CreateChild(char * id, int x, int y, float width, float height)
{
ImGui::SetCursorPosX(x);
ImGui::SetCursorPosY(y);
ImGui::BeginChild(id, ImVec2(width, height), true);
ImGui::EndChild();
}
void CNewUIGui::AddWindows(const char* Title, POINT m_Pos, POINT m_Size)
{
ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoResize;
window_flags |= ImGuiWindowFlags_NoCollapse;
ImGui::SetNextWindowSize(ImVec2(m_Size.x, m_Size.y), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(m_Pos.x, m_Pos.y), ImGuiCond_Always);
ImGui::Begin(Title, 0, window_flags);
}
bool CNewUIGui::InitWindows(const char* Title, POINT& m_Pos, POINT& m_Size, bool& p_open)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return 0;
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoResize;
window_flags |= ImGuiWindowFlags_NoCollapse;
ImGui::SetNextWindowSize(ImVec2(m_Size.x, m_Size.y), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(m_Pos.x, m_Pos.y), ImGuiCond_FirstUseEver);
ImGui::Begin(Title, &p_open, window_flags);
// Obtener posicion y tamaño de la ventana
ImVec2 window_pos = ImGui::GetWindowPos();
m_Pos.x = static_cast<int>(window_pos.x);
m_Pos.y = static_cast<int>(window_pos.y);
ImVec2 window_size = ImGui::GetWindowSize();
m_Size.x = static_cast<int>(window_size.x);
m_Size.y = static_cast<int>(window_size.y);
return true;
}
POINT CNewUIGui::InitWindows(int iPos_x, int iPos_y, int Width, int Height, bool& p_open, char* szText)
{
POINT m_Pos;
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return m_Pos;
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoResize;
window_flags |= ImGuiWindowFlags_NoCollapse;
ImGui::SetNextWindowSize(ImVec2(Width, Height), ImGuiCond_FirstUseEver);
ImGui::Begin(szText, &p_open, window_flags);
//ImGui::Spacing();
//
//ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
//ImGui::Spacing();
//
//
//ImGui::Text("Window position: (%.0f, %.0f)\n", windowPos.x, windowPos.y);
ImGui::Spacing();
ImVec2 windowPos = ImGui::GetWindowPos();
m_Pos.x = windowPos.x;
m_Pos.y = windowPos.y;
return m_Pos;
}
void CNewUIGui::RenderTabBar(char* Identificador, int &sTab, std::list<std::string>& textlist)
{
bool new_tab_selected = false;
static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown;
if (ImGui::BeginTabBar(Identificador, tab_bar_flags))
{
std::list<std::string>::iterator it = textlist.begin();
for (int i = 0; it != textlist.end(); ++it, i++)
{
if (ImGui::BeginTabItem(it->c_str()))
{
if (ImGui::IsItemActive())
{
sTab = i;
}
ImGui::EndTabItem();
}
}
ImGui::EndTabBar();
ImGui::Spacing();
}
}
void CNewUIGui::SameLine()
{
ImGui::SameLine();
}
void CNewUIGui::AddSpacing(int Number)
{
for (int i = 0; i < Number; i++) ImGui::Spacing();
}
void CNewUIGui::NextWidth(int Width, int Height)
{
ImGui::SetNextWindowSize(ImVec2(Width, Height));
}
float* CNewUIGui::SizeText(char* szText)
{
float m_Pos[2];
m_Pos[0] = ImGui::CalcTextSize(szText).x;
m_Pos[1] = ImGui::CalcTextSize(szText).y;
return m_Pos;
}
void CNewUIGui::AddImGuiTextLabel(bool SameLine, char* label, ...)
{
if (SameLine) ImGui::SameLine();
va_list args;
va_start(args, label);
char buffer[1024];
vsnprintf(buffer, 1024, label, args);
va_end(args);
ImGui::Text("%s", buffer);
}
void CNewUIGui::AddImGuiText(char* label, bool SameLine, bool NextLine)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
if (SameLine) ImGui::SameLine();
ImGui::Text(label);
if (NextLine) ImGui::Spacing();
}
void CNewUIGui::AddImGuiTextColor(char* label, float* vColor, bool SameLine, bool NextLine, int Aling, int window_width)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
if (SameLine) ImGui::SameLine();
if (Aling == 3)
{
ImGui::SetCursorPosX((window_width - ImGui::CalcTextSize(label).x) / 2.0f);
}
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor(vColor[0], vColor[1], vColor[2], 1.0f));
ImGui::Text(label);
ImGui::PopStyleColor();
if (NextLine) ImGui::Spacing();
}
void CNewUIGui::AddImgGuiSlider(char* label, int& control, int min, int max)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui::SliderInt(label, &control, min, max);
ImGui::Spacing();
}
void CNewUIGui::AddImgGuiSlider(char* label, float& control, float min, float max)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui::SliderFloat(label, &control, min, max);
ImGui::Spacing();
}
bool CNewUIGui::AddImGuiCheckbox(char* label, bool& pCheck, bool SameLine, float Marginx, float spacing_w)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return 0;
if (SameLine) ImGui::SameLine(Marginx);
return ImGui::Checkbox(label, &pCheck);
}
bool CNewUIGui::AddImGuiButton(char* szText, float iPos_x, float iPos_y, int Width, int Height)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return 0;
if (iPos_x > 0)
{
ImVec2 lastPos = ImGui::GetCursorPos();
float x = lastPos.x + iPos_x;
float y = lastPos.y + iPos_y;
ImGui::SetCursorPos(ImVec2(x, y));
}
return ImGui::Button(szText);
}
void CNewUIGui::RenderBitmaps(GLuint uiBitmapIndex)
{
BITMAP_t* pText = FindTexture(uiBitmapIndex);
if (pText)
{
BindTexture(uiBitmapIndex);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Image(reinterpret_cast<void*>(pText->TextureNumber), ImVec2(pText->output_width, pText->output_height));
ImGui::EndTooltip();
}
}
}
void CNewUIGui::CrearTooltip(float x, float y)
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
if (x > 0 && y > 0)
{
ImVec2 pos = ImVec2(x, y);
ImGui::SetNextWindowPos(pos, ImGuiCond_Always, ImVec2(0, 0));
}
else if (x == 0 && y != 0)
{
ImVec2 pos = ImVec2(ImGui::GetIO().DisplaySize.x, y);
ImGui::SetNextWindowPos(pos, ImGuiCond_Always, ImVec2(0, 0));
}
ImGui::BeginTooltip();
}
void CNewUIGui::FinishTooltip()
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui::EndTooltip();
}
void CNewUIGui::FinishWindows()
{
ImGuiContext* context = ImGui::GetCurrentContext();
if (!context) return;
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
}
ComboBox::ComboBox()
{
currentItem = NULL;
currentIndex = 0;
}
ComboBox::~ComboBox()
{
}
void ComboBox::Render(const char* label, int& selectedIndex, std::vector<std::pair<int, std::string>>& items)
{
ImGui::SameLine();
int itemCount = (int)items.size();
for (int i = 0; i < itemCount; i++)
{
if (selectedIndex == items[i].first && currentIndex != i || currentItem == NULL)
{
currentItem = items[i].second.c_str();
currentIndex = i;
selectedIndex = items[i].first;
i = itemCount;
}
}
if (ImGui::BeginCombo(label, currentItem))
{
for (int i = 0; i < itemCount; i++)
{
const bool isSelected = (currentIndex == i);
if (ImGui::Selectable(items[i].second.c_str(), isSelected))
{
currentItem = items[i].second.c_str();
currentIndex = i;
selectedIndex = items[i].first;
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
}
GUITextInput::GUITextInput()
{
hasFocus = 0;
}
GUITextInput::~GUITextInput()
{
}
void GUITextInput::RenderInt(char* id, int& value)
{
ImGui::InputInt(id, &value);
hasFocus = ImGui::IsItemActive();
}
void GUITextInput::RenderFloat(char* id, float& value)
{
ImGui::InputFloat(id, &value, 0.0003, 0.001, "%.5f");
hasFocus = ImGui::IsItemActive();
}