Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add web imgui support #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ workspace(name = "imgui-info-center")
load("//third_party/bazel:iminfocenter.bzl", "iminfocenter_deps")

iminfocenter_deps()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "emsdk",
strip_prefix = "emsdk-428746457845f0224346dc36164f84829094117b/bazel",
url = "https://github.com/glazfu/emsdk/archive/428746457845f0224346dc36164f84829094117b.tar.gz",
sha256 = "061f518242d8c4996516675d5efa5e7c4d969336c70cf1322be9629e5a37d5a6",
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")
emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
emsdk_emscripten_deps()
21 changes: 21 additions & 0 deletions example/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ cc_binary(
"@imgui",
],
)


load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

cc_binary(
name = "web_example",
srcs = ["test_web_main.cpp"],
linkopts = [
"-s USE_WEBGL2=1 -s USE_GLFW=3 -s FULL_ES3=1",
],
deps = [
"//src:web_imgui",
":font_deps",
"//src:imgui_info_center_web",
"@imgui//:imgui_web",
],
)
wasm_cc_binary(
name = "web_example-wasm",
cc_target = ":web_example",
)
75 changes: 75 additions & 0 deletions example/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!doctype html>
<html lang="en-us">

<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGui Demo</title>
<style>
body {
font-family: arial;
margin: 0;
padding: none;
}

.emscripten {
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}

div.emscripten {
text-align: center;
}

div.emscripten_border {
border: none;
}
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */

canvas.emscripten {
border: 0px none;
background-color: black;
}
</style>
</head>

<body>

<div class="emscripten_border">
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
</div>

<script type='text/javascript'>
var Module = {
preRun: [],
postRun: [],
print: (function() {})(),
printErr: function(text) {},
canvas: (function() {
var canvas = document.getElementById('canvas');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) {
alert('WebGL context lost. You will need to reload the page.');
e.preventDefault();
}, false);
return canvas;
})(),
setStatus: function(text) {},
totalDependencies: 0,
monitorRunDependencies: function(left) {}
};
window.addEventListener('resize', js_resizeCanvas, false);

function js_resizeCanvas() {
document.getElementById('canvas').width = window.innerWidth;
document.getElementById('canvas').height = window.innerHeight;
}
</script>
<script async type="text/javascript" src="../bazel-bin/example/web_example-wasm/web_example.js"></script>
</body>

</html>
200 changes: 200 additions & 0 deletions example/test_web_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#include "GLFW/glfw3.h"
#include "fa_solid_900.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include "imgui/imgui_stdlib.h"
#include "src/imgui_info_center.h"
#include "tahoma.h"

#include "src/web_imgui.h"

#include <map>
#include <string>
#include <vector>

std::string card_title;
std::string card_content;
ImInfo::ImInfoCardType card_type = ImInfo::ImInfoCardType::UNKNOWN;
int32_t card_lifetime = 3000;
float card_progress = 0.0f;
std::vector<int32_t> progress_index;
std::map<int32_t, int32_t> stage_index;
void SetStyle() {
auto &style = ImGui::GetStyle();

style.FrameRounding = 4.0f;
style.GrabRounding = 4.0f;
style.ItemSpacing.x = 5.0f;
style.ItemSpacing.y = 8.0f;
style.IndentSpacing = 6.0f;

ImVec4 *colors = ImGui::GetStyle().Colors;

colors[ImGuiCol_Text] = ImVec4(0.95f, 0.96f, 0.98f, 1.00f);
colors[ImGuiCol_TextDisabled] = ImVec4(0.36f, 0.42f, 0.47f, 1.00f);
colors[ImGuiCol_WindowBg] = ImVec4(0.11f, 0.15f, 0.17f, 1.00f);
colors[ImGuiCol_ChildBg] = ImVec4(0.15f, 0.18f, 0.22f, 1.00f);
colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);
colors[ImGuiCol_Border] = ImVec4(0.08f, 0.10f, 0.12f, 1.00f);
colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.25f, 0.29f, 1.00f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.12f, 0.20f, 0.28f, 1.00f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.09f, 0.12f, 0.14f, 1.00f);
colors[ImGuiCol_TitleBg] = ImVec4(0.09f, 0.12f, 0.14f, 0.65f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.08f, 0.10f, 0.12f, 1.00f);
colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);
colors[ImGuiCol_MenuBarBg] = ImVec4(0.15f, 0.18f, 0.22f, 1.00f);
colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.39f);
colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.20f, 0.25f, 0.29f, 1.00f);
colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.18f, 0.22f, 0.25f, 1.00f);
colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.09f, 0.21f, 0.31f, 1.00f);
colors[ImGuiCol_CheckMark] = ImVec4(0.28f, 0.56f, 0.74f, 1.00f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.28f, 0.56f, 0.74f, 1.00f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.37f, 0.61f, 1.00f, 1.00f);
colors[ImGuiCol_Button] = ImVec4(0.20f, 0.25f, 0.29f, 1.00f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.28f, 0.56f, 0.74f, 1.00f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.70f, 1.00f);
colors[ImGuiCol_Header] = ImVec4(0.33f, 0.38f, 0.42f, 0.55f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.74f, 0.80f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.74f, 1.00f);
colors[ImGuiCol_Separator] = ImVec4(0.20f, 0.25f, 0.29f, 1.00f);
colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f);
colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f);
colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.74f, 0.25f);
colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.74f, 0.67f);
colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.74f, 0.95f);
colors[ImGuiCol_Tab] = ImVec4(0.11f, 0.15f, 0.17f, 1.00f);
colors[ImGuiCol_TabHovered] = ImVec4(0.26f, 0.59f, 0.72f, 0.80f);
colors[ImGuiCol_TabActive] = ImVec4(0.20f, 0.35f, 0.47f, 1.00f);
colors[ImGuiCol_TabUnfocused] = ImVec4(0.11f, 0.15f, 0.17f, 1.00f);
colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.11f, 0.15f, 0.17f, 1.00f);
colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.74f, 0.35f);
colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.78f, 1.00f);
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
}
auto loop = [](GLFWwindow *main_window, int display_w, int display_h) {
ImGui::SetNextWindowPos(ImVec2(display_w, display_h), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(display_w, display_h), ImGuiCond_Always);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::Begin("Background", nullptr,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGui::End();

ImGui::SetNextWindowSize(ImVec2(500, 500), ImGuiCond_Always);
ImGui::Begin("demo window", nullptr);
{
auto &info_center = ImInfo::ImInfoCenter::Get();
ImGui::InputText("title", &card_title);
ImGui::InputText("content", &card_content);
ImGui::InputInt("lifetime", &card_lifetime);
ImGui::InputFloat("progress", &card_progress);

if (ImGui::RadioButton("Unknown",
card_type == ImInfo::ImInfoCardType::UNKNOWN)) {
card_type = ImInfo::ImInfoCardType::UNKNOWN;
}
ImGui::SameLine();
if (ImGui::RadioButton("Success",
card_type == ImInfo::ImInfoCardType::SUCCESS)) {
card_type = ImInfo::ImInfoCardType::SUCCESS;
}
ImGui::SameLine();
if (ImGui::RadioButton("Info", card_type == ImInfo::ImInfoCardType::INFO)) {
card_type = ImInfo::ImInfoCardType::INFO;
}
ImGui::SameLine();
if (ImGui::RadioButton("Warning",
card_type == ImInfo::ImInfoCardType::WARNING)) {
card_type = ImInfo::ImInfoCardType::WARNING;
}
ImGui::SameLine();
if (ImGui::RadioButton("Error",
card_type == ImInfo::ImInfoCardType::ERROR)) {
card_type = ImInfo::ImInfoCardType::ERROR;
}

ImGui::Separator();

ImGui::Text("Create a card with: ");
if (ImGui::Button("current parameters")) {
info_center.AddCardBasic(card_type, card_title, card_content,
card_lifetime);
}
if (ImGui::Button("no title")) {
info_center.AddCardBasic(card_content, card_lifetime);
}
if (ImGui::Button("content only")) {
info_center.AddCardBasic(card_content);
}
if (ImGui::Button("with increasing progress bar")) {
int32_t ind = info_center.AddCardProgressBar(
card_progress, card_type, card_title, card_content, card_lifetime);
progress_index.emplace_back(ind);
}
if (ImGui::Button("with stage")) {
int32_t ind = info_center.AddCardStage(card_type, card_title,
card_content, card_lifetime);
stage_index.emplace(ind, 0);
}

for (const auto &ind : progress_index) {
info_center.IncreaseCardProgress(ind, 0.5f);
}

for (auto &ind_pair : stage_index) {
++ind_pair.second;
if (ind_pair.second == 100) {
info_center.SetCardStage(ind_pair.first,
ImInfo::ImInfoCardStageCode::ERROR);
}
if (ind_pair.second == 500) {
info_center.SetCardStage(ind_pair.first,
ImInfo::ImInfoCardStageCode::SUCCESS);
}
}
}
ImGui::End();

ImInfo::ImInfoCenter::Get().Show();
};
auto init_func = []() {
ImGuiIO *io = &ImGui::GetIO();

ImFontConfig font_cfg;
font_cfg.FontDataOwnedByAtlas = false;
io->Fonts->AddFontFromMemoryTTF((void *)tahoma, sizeof(tahoma), 17.f,
&font_cfg);
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};

ImFontConfig icons_config;
icons_config.MergeMode = true;
icons_config.PixelSnapH = true;
icons_config.FontDataOwnedByAtlas = false;

io->Fonts->AddFontFromMemoryTTF((void *)fa_solid_900, sizeof(fa_solid_900),
16.0f, &icons_config, icons_ranges);
};

int main(int argc, char **argv) {

WebImGui &wig = WebImGui::Get();
wig.set_loop(loop);
wig.set_ImGui_init(init_func);
if (wig.init() != 0)
return 1;
wig.loop();

glfwTerminate();

return 0;
}
4 changes: 4 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ is_in_bazel_repo() {

run_example() {
is_in_bazel_repo && bazel run @imgui-info-center//example:info_center_example
}

build_example_web() {
is_in_bazel_repo && bazel build //example:web_example-wasm
}
17 changes: 17 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,20 @@ cc_library(
],
deps = ["@imgui"],
)

cc_library(
name = "imgui_info_center_web",
srcs = ["imgui_info_center.cpp"],
hdrs = [
"font_awesome_5.h",
"imgui_info_center.h",
],
deps = ["@imgui//:imgui_web"],
)

cc_library(
name = "web_imgui",
srcs = ["web_imgui.cpp"],
hdrs = ["web_imgui.h",],
deps = ["@imgui//:imgui_web",],
)
Loading