Updated Imgui and ImguiSFML
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#include "imgui/imgui-SFML.h"
|
||||
#include "imgui/imgui.h"
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
#include <SFML/OpenGL.hpp>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
@@ -10,8 +10,102 @@
|
||||
#include <SFML/Window/Event.hpp>
|
||||
#include <SFML/Window/Window.hpp>
|
||||
|
||||
#include <cmath> // abs
|
||||
#include <cstddef> // offsetof, NULL
|
||||
#include <cassert>
|
||||
#include <SFML/Window/Touch.hpp>
|
||||
|
||||
#ifdef ANDROID
|
||||
#ifdef USE_JNI
|
||||
|
||||
#include <jni.h>
|
||||
#include <android/native_activity.h>
|
||||
#include <SFML/System/NativeActivity.hpp>
|
||||
|
||||
static bool s_wantTextInput = false;
|
||||
|
||||
int openKeyboardIME()
|
||||
{
|
||||
ANativeActivity *activity = sf::getNativeActivity();
|
||||
JavaVM* vm = activity->vm;
|
||||
JNIEnv* env = activity->env;
|
||||
JavaVMAttachArgs attachargs;
|
||||
attachargs.version = JNI_VERSION_1_6;
|
||||
attachargs.name = "NativeThread";
|
||||
attachargs.group = NULL;
|
||||
jint res = vm->AttachCurrentThread(&env, &attachargs);
|
||||
if (res == JNI_ERR)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
jclass natact = env->FindClass("android/app/NativeActivity");
|
||||
jclass context = env->FindClass("android/content/Context");
|
||||
|
||||
jfieldID fid = env->GetStaticFieldID(context, "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
|
||||
jobject svcstr = env->GetStaticObjectField(context, fid);
|
||||
|
||||
|
||||
jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
|
||||
jobject imm_obj = env->CallObjectMethod(activity->clazz, getss, svcstr);
|
||||
|
||||
jclass imm_cls = env->GetObjectClass(imm_obj);
|
||||
jmethodID toggleSoftInput = env->GetMethodID(imm_cls, "toggleSoftInput", "(II)V");
|
||||
|
||||
env->CallVoidMethod(imm_obj, toggleSoftInput, 2, 0);
|
||||
|
||||
env->DeleteLocalRef(imm_obj);
|
||||
env->DeleteLocalRef(imm_cls);
|
||||
env->DeleteLocalRef(svcstr);
|
||||
env->DeleteLocalRef(context);
|
||||
env->DeleteLocalRef(natact);
|
||||
|
||||
vm->DetachCurrentThread();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
int closeKeyboardIME()
|
||||
{
|
||||
ANativeActivity *activity = sf::getNativeActivity();
|
||||
JavaVM* vm = activity->vm;
|
||||
JNIEnv* env = activity->env;
|
||||
JavaVMAttachArgs attachargs;
|
||||
attachargs.version = JNI_VERSION_1_6;
|
||||
attachargs.name = "NativeThread";
|
||||
attachargs.group = NULL;
|
||||
jint res = vm->AttachCurrentThread(&env, &attachargs);
|
||||
if (res == JNI_ERR)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
jclass natact = env->FindClass("android/app/NativeActivity");
|
||||
jclass context = env->FindClass("android/content/Context");
|
||||
|
||||
jfieldID fid = env->GetStaticFieldID(context, "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
|
||||
jobject svcstr = env->GetStaticObjectField(context, fid);
|
||||
|
||||
|
||||
jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
|
||||
jobject imm_obj = env->CallObjectMethod(activity->clazz, getss, svcstr);
|
||||
|
||||
jclass imm_cls = env->GetObjectClass(imm_obj);
|
||||
jmethodID toggleSoftInput = env->GetMethodID(imm_cls, "toggleSoftInput", "(II)V");
|
||||
|
||||
env->CallVoidMethod(imm_obj, toggleSoftInput, 1, 0);
|
||||
|
||||
env->DeleteLocalRef(imm_obj);
|
||||
env->DeleteLocalRef(imm_cls);
|
||||
env->DeleteLocalRef(svcstr);
|
||||
env->DeleteLocalRef(context);
|
||||
env->DeleteLocalRef(natact);
|
||||
|
||||
vm->DetachCurrentThread();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Supress warnings caused by converting from uint to void* in pCmd->TextureID
|
||||
#ifdef __clang__
|
||||
@@ -22,6 +116,9 @@
|
||||
|
||||
static bool s_windowHasFocus = true;
|
||||
static bool s_mousePressed[3] = { false, false, false };
|
||||
static bool s_touchDown[3] = { false, false, false };
|
||||
static bool s_mouseMoved = false;
|
||||
static sf::Vector2i s_touchPos;
|
||||
static sf::Texture* s_fontTexture = NULL; // owning pointer to internal font atlas which is used if user doesn't set custom sf::Texture.
|
||||
namespace
|
||||
{
|
||||
@@ -29,7 +126,7 @@ namespace
|
||||
ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect);
|
||||
ImVec2 getDownRightAbsolute(const sf::FloatRect& rect);
|
||||
|
||||
inline void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prototype
|
||||
void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prototype
|
||||
|
||||
// Implementation of ImageButton overload
|
||||
bool imageButtonImpl(const sf::Texture& texture, const sf::FloatRect& textureRect, const sf::Vector2f& size, const int framePadding,
|
||||
@@ -42,7 +139,7 @@ namespace ImGui
|
||||
namespace SFML
|
||||
{
|
||||
|
||||
void Init(sf::RenderTarget& target, sf::Texture* fontTexture)
|
||||
void Init(sf::RenderTarget& target, bool loadDefaultFont)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
@@ -56,8 +153,12 @@ void Init(sf::RenderTarget& target, sf::Texture* fontTexture)
|
||||
io.KeyMap[ImGuiKey_PageDown] = sf::Keyboard::PageDown;
|
||||
io.KeyMap[ImGuiKey_Home] = sf::Keyboard::Home;
|
||||
io.KeyMap[ImGuiKey_End] = sf::Keyboard::End;
|
||||
#ifdef ANDROID
|
||||
io.KeyMap[ImGuiKey_Backspace] = sf::Keyboard::Delete;
|
||||
#else
|
||||
io.KeyMap[ImGuiKey_Delete] = sf::Keyboard::Delete;
|
||||
io.KeyMap[ImGuiKey_Backspace] = sf::Keyboard::BackSpace;
|
||||
#endif
|
||||
io.KeyMap[ImGuiKey_Enter] = sf::Keyboard::Return;
|
||||
io.KeyMap[ImGuiKey_Escape] = sf::Keyboard::Escape;
|
||||
io.KeyMap[ImGuiKey_A] = sf::Keyboard::A;
|
||||
@@ -71,16 +172,15 @@ void Init(sf::RenderTarget& target, sf::Texture* fontTexture)
|
||||
io.DisplaySize = static_cast<sf::Vector2f>(target.getSize());
|
||||
io.RenderDrawListsFn = RenderDrawLists; // set render callback
|
||||
|
||||
if (fontTexture == NULL) {
|
||||
if (s_fontTexture) { // delete previously created texture
|
||||
delete s_fontTexture;
|
||||
}
|
||||
if (s_fontTexture) { // delete previously created texture
|
||||
delete s_fontTexture;
|
||||
}
|
||||
s_fontTexture = new sf::Texture;
|
||||
|
||||
s_fontTexture = new sf::Texture;
|
||||
createFontTexture(*s_fontTexture);
|
||||
setFontTexture(*s_fontTexture);
|
||||
} else {
|
||||
setFontTexture(*fontTexture);
|
||||
if (loadDefaultFont) {
|
||||
// this will load default font automatically
|
||||
// No need to call AddDefaultFont
|
||||
UpdateFontTexture();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +190,9 @@ void ProcessEvent(const sf::Event& event)
|
||||
if (s_windowHasFocus) {
|
||||
switch (event.type)
|
||||
{
|
||||
case sf::Event::MouseMoved:
|
||||
s_mouseMoved = true;
|
||||
break;
|
||||
case sf::Event::MouseButtonPressed: // fall-through
|
||||
case sf::Event::MouseButtonReleased:
|
||||
{
|
||||
@@ -100,6 +203,17 @@ void ProcessEvent(const sf::Event& event)
|
||||
}
|
||||
}
|
||||
break;
|
||||
case sf::Event::TouchBegan: // fall-through
|
||||
case sf::Event::TouchEnded:
|
||||
{
|
||||
s_mouseMoved = false;
|
||||
int button = event.touch.finger;
|
||||
if (event.type == sf::Event::TouchBegan &&
|
||||
button >= 0 && button < 3) {
|
||||
s_touchDown[event.touch.finger] = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case sf::Event::MouseWheelMoved:
|
||||
io.MouseWheel += static_cast<float>(event.mouseWheel.delta);
|
||||
break;
|
||||
@@ -140,7 +254,16 @@ void Update(sf::RenderWindow& window, sf::Time dt)
|
||||
|
||||
void Update(sf::Window& window, sf::RenderTarget& target, sf::Time dt)
|
||||
{
|
||||
Update(sf::Mouse::getPosition(window), static_cast<sf::Vector2f>(target.getSize()), dt);
|
||||
|
||||
if (!s_mouseMoved)
|
||||
{
|
||||
if (sf::Touch::isDown(0))
|
||||
s_touchPos = sf::Touch::getPosition(0, window);
|
||||
|
||||
Update(s_touchPos, static_cast<sf::Vector2f>(target.getSize()), dt);
|
||||
} else {
|
||||
Update(sf::Mouse::getPosition(window), static_cast<sf::Vector2f>(target.getSize()), dt);
|
||||
}
|
||||
window.setMouseCursorVisible(!ImGui::GetIO().MouseDrawCursor); // don't draw mouse cursor if ImGui draws it
|
||||
}
|
||||
|
||||
@@ -152,16 +275,38 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
|
||||
|
||||
if (s_windowHasFocus) {
|
||||
io.MousePos = mousePos;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
io.MouseDown[i] = s_mousePressed[i] || sf::Mouse::isButtonPressed((sf::Mouse::Button)i);
|
||||
for (unsigned int i = 0; i < 3; i++) {
|
||||
io.MouseDown[i] = s_touchDown[i] || sf::Touch::isDown(i) || s_mousePressed[i] || sf::Mouse::isButtonPressed((sf::Mouse::Button)i);
|
||||
s_mousePressed[i] = false;
|
||||
s_touchDown[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ANDROID
|
||||
#ifdef USE_JNI
|
||||
if (io.WantTextInput && !s_wantTextInput)
|
||||
{
|
||||
openKeyboardIME();
|
||||
s_wantTextInput = true;
|
||||
}
|
||||
if (!io.WantTextInput && s_wantTextInput)
|
||||
{
|
||||
closeKeyboardIME();
|
||||
s_wantTextInput = false;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
assert(io.Fonts->Fonts.Size > 0); // You forgot to create and set up font atlas (see createFontTexture)
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
void Render(sf::RenderTarget& target)
|
||||
{
|
||||
target.resetGLStates();
|
||||
ImGui::Render();
|
||||
}
|
||||
|
||||
void Shutdown()
|
||||
{
|
||||
ImGui::GetIO().Fonts->TexID = NULL;
|
||||
@@ -171,31 +316,31 @@ void Shutdown()
|
||||
s_fontTexture = NULL;
|
||||
}
|
||||
|
||||
ImGui::Shutdown(); // need to specify namespace here, otherwise ImGui::SFML::Shutdown would be called
|
||||
//ImGui::Shutdown(); // need to specify namespace here, otherwise ImGui::SFML::Shutdown would be called
|
||||
}
|
||||
|
||||
void createFontTexture(sf::Texture& texture)
|
||||
void UpdateFontTexture()
|
||||
{
|
||||
sf::Texture& texture = *s_fontTexture;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
|
||||
|
||||
texture.create(width, height);
|
||||
texture.update(pixels);
|
||||
|
||||
io.Fonts->TexID = (void*)texture.getNativeHandle();
|
||||
|
||||
io.Fonts->ClearInputData();
|
||||
io.Fonts->ClearTexData();
|
||||
}
|
||||
|
||||
void setFontTexture(sf::Texture& texture)
|
||||
sf::Texture& GetFontTexture()
|
||||
{
|
||||
ImGui::GetIO().Fonts->TexID = (void*)texture.getNativeHandle();
|
||||
if (&texture != s_fontTexture) { // internal texture is not needed anymore
|
||||
delete s_fontTexture;
|
||||
s_fontTexture = NULL;
|
||||
}
|
||||
return *s_fontTexture;
|
||||
}
|
||||
|
||||
} // end of namespace SFML
|
||||
@@ -218,7 +363,7 @@ void Image(const sf::Texture& texture, const sf::Vector2f& size,
|
||||
void Image(const sf::Texture& texture, const sf::FloatRect& textureRect,
|
||||
const sf::Color& tintColor, const sf::Color& borderColor)
|
||||
{
|
||||
Image(texture, sf::Vector2f(textureRect.width, textureRect.height), textureRect, tintColor, borderColor);
|
||||
Image(texture, sf::Vector2f(std::abs(textureRect.width), std::abs(textureRect.height)), textureRect, tintColor, borderColor);
|
||||
}
|
||||
|
||||
void Image(const sf::Texture& texture, const sf::Vector2f& size, const sf::FloatRect& textureRect,
|
||||
@@ -302,7 +447,7 @@ void DrawRectFilled(const sf::FloatRect& rect, const sf::Color& color,
|
||||
float rounding, int rounding_corners)
|
||||
{
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
draw_list->AddRect(
|
||||
draw_list->AddRectFilled(
|
||||
getTopLeftAbsolute(rect),
|
||||
getDownRightAbsolute(rect),
|
||||
ColorConvertFloat4ToU32(color), rounding, rounding_corners);
|
||||
@@ -312,6 +457,7 @@ void DrawRectFilled(const sf::FloatRect& rect, const sf::Color& color,
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
ImVec2 getTopLeftAbsolute(const sf::FloatRect & rect)
|
||||
{
|
||||
ImVec2 pos = ImGui::GetCursorScreenPos();
|
||||
@@ -326,6 +472,7 @@ ImVec2 getDownRightAbsolute(const sf::FloatRect & rect)
|
||||
// Rendering callback
|
||||
void RenderDrawLists(ImDrawData* draw_data)
|
||||
{
|
||||
|
||||
if (draw_data->CmdListsCount == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -339,32 +486,43 @@ void RenderDrawLists(ImDrawData* draw_data)
|
||||
if (fb_width == 0 || fb_height == 0) { return; }
|
||||
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
|
||||
|
||||
// Save OpenGL state
|
||||
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
|
||||
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
|
||||
|
||||
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
||||
|
||||
#ifdef GL_VERSION_ES_CL_1_1
|
||||
GLint last_program, last_texture, last_array_buffer, last_element_array_buffer;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
|
||||
#else
|
||||
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
||||
#endif
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glDisable(GL_LIGHTING);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
|
||||
|
||||
glMatrixMode(GL_TEXTURE);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
|
||||
|
||||
#ifdef GL_VERSION_ES_CL_1_1
|
||||
glOrthof(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
|
||||
#else
|
||||
glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
|
||||
#endif
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
for (int n = 0; n < draw_data->CmdListsCount; ++n) {
|
||||
@@ -390,18 +548,14 @@ void RenderDrawLists(ImDrawData* draw_data)
|
||||
idx_buffer += pcmd->ElemCount;
|
||||
}
|
||||
}
|
||||
|
||||
// Restore modified state
|
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)last_texture);
|
||||
glMatrixMode(GL_TEXTURE);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
glPopAttrib();
|
||||
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
|
||||
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
|
||||
#ifdef GL_VERSION_ES_CL_1_1
|
||||
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
#else
|
||||
glPopAttrib();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool imageButtonImpl(const sf::Texture& texture, const sf::FloatRect& textureRect, const sf::Vector2f& size, const int framePadding,
|
||||
|
||||
5910
src/imgui/imgui.cpp
5910
src/imgui/imgui.cpp
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user