Skip to content

Commit

Permalink
nanovg: work on python (demo + bindings)
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 13, 2024
1 parent 4a4d15a commit 6f653eb
Show file tree
Hide file tree
Showing 10 changed files with 1,523 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "immapp/immapp.h"
#include "hello_imgui/hello_imgui.h"
#include "demo_utils/api_demos.h"

#include "imgui.h"
#include "nanovg.h"
#include "nvg_imgui/nvg_imgui.h"

#include "demo_nanovg_full/demo_nanovg_full_impl.h"
Expand All @@ -12,7 +10,7 @@
struct MyNvgDemo
{
bool Blowup = false;
DemoData nvgDemoData;
DemoData nvgDemoData = {};
NVGcontext* vg;

MyNvgDemo(NVGcontext* _vg)
Expand All @@ -39,7 +37,7 @@ struct MyNvgDemo
struct AppState
{
std::unique_ptr<MyNvgDemo> myNvgDemo;
NVGcontext * vg;
NVGcontext * vg = nullptr;

std::unique_ptr<NvgImgui::NvgFramebuffer> myFramebuffer;

Expand All @@ -58,7 +56,6 @@ int main(int, char**)
HelloImGui::RunnerParams runnerParams;
runnerParams.imGuiWindowParams.defaultImGuiWindowType = HelloImGui::DefaultImGuiWindowType::NoDefaultWindow;
runnerParams.appWindowParams.windowGeometry.size = {1200, 900};
ImmApp::AddOnsParams addons;

runnerParams.callbacks.EnqueuePostInit([&]()
{
Expand All @@ -78,7 +75,10 @@ int main(int, char**)
auto nvgDrawingFunction = [&](NVGcontext *vg, float width, float height)
{
double now = ImGui::GetTime();
auto mousePos = ImGui::GetMousePos() - ImGui::GetMainViewport()->Pos;
ImVec2 mousePos(
ImGui::GetIO().MousePos.x - ImGui::GetMainViewport()->Pos.x,
ImGui::GetIO().MousePos.y - ImGui::GetMainViewport()->Pos.y
);
appState.myNvgDemo->Render(width, height, (int)mousePos.x, (int)mousePos.y, (float)now);
};

Expand Down Expand Up @@ -106,6 +106,6 @@ int main(int, char**)

runnerParams.fpsIdling.enableIdling = false;

ImmApp::Run(runnerParams, addons);
HelloImGui::Run(runnerParams);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,
const char* hoverText = "Hover your mouse over the text to see calculated caret position.";
float gx,gy;
int gutter = 0;
const char* boxText = "Testing\nsome multiline\ntext.";
// const char* boxText = "Testing\nsome multiline\ntext.";
NVG_NOTUSED(height);

nvgSave(vg);
Expand Down
105 changes: 105 additions & 0 deletions bindings/imgui_bundle/demos_python/demos_nanovg/demo_nanovg_full.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# port of bindings/imgui_bundle/demos_cpp/demos_nanovg/demo_nanovg_full.cpp
from imgui_bundle import imgui, nanovg as nvg, hello_imgui, ImVec2, ImVec4
from imgui_bundle.demos_python import demo_utils
import demo_nanovg_full_impl
from typing import List


nvg_imgui = nvg.nvg_imgui


class MyNvgDemo:
blowup: bool
nvgDemoData: demo_nanovg_full_impl.DemoData
vg: nvg.Context

def init(self, vg: nvg.Context):
self.vg = vg
self.blowup = False

self.nvgDemoData = demo_nanovg_full_impl.DemoData()
status = demo_nanovg_full_impl.load_demo_data(vg, self.nvgDemoData)
if status != 0:
print("Could not load demo data.")
return

def reset(self):
demo_nanovg_full_impl.free_demo_data(self.vg, self.nvgDemoData)

def render(self, width: float, height: float, mouse_x: float, mouse_y: float, t: float):
demo_nanovg_full_impl.render_demo(self.vg, mouse_x, mouse_y, width, height, t, self.blowup, self.nvgDemoData)


class AppState:
myNvgDemo: MyNvgDemo
vg: nvg.Context
myFrameBuffer: nvg_imgui.NvgFramebuffer
clear_color: List[float]
display_in_frame_buffer: bool = False

def __init__(self):
self.clear_color = [0.3, 0.3, 0.32, 1.0]


def main():
# This call is specific to the ImGui Bundle interactive manual. In a standard application, you could write:
# hello_imgui.set_assets_folder("my_assets") # (By default, HelloImGui will search inside "assets")
demo_utils.set_hello_imgui_demo_assets_folder()

app_state = AppState()

runner_params = hello_imgui.RunnerParams()
runner_params.imgui_window_params.default_imgui_window_type = hello_imgui.DefaultImGuiWindowType.no_default_window
runner_params.app_window_params.window_geometry.size = (1200, 900)

def post_init():
app_state.vg = nvg_imgui.create_nvg_context_gl(nvg_imgui.NvgCreateFlags.antialias.value | nvg_imgui.NvgCreateFlags.stencil_strokes.value)
app_state.myNvgDemo = MyNvgDemo()
app_state.myNvgDemo.init(app_state.vg)
nvg_image_flags = 0
app_state.myFrameBuffer = nvg_imgui.NvgFramebuffer(app_state.vg, 1000, 600, nvg_image_flags)

def before_exit():
app_state.myNvgDemo.reset()
app_state.myFrameBuffer = None
nvg_imgui.delete_nvg_context_gl(app_state.vg)

runner_params.callbacks.enqueue_post_init(post_init)
runner_params.callbacks.enqueue_before_exit(before_exit)

def nvg_drawing_function(_: nvg.Context, width: float, height: float):
now = imgui.get_time()
mouse_pos = ImVec2(
imgui.get_mouse_pos().x - imgui.get_main_viewport().pos.x,
imgui.get_mouse_pos().y - imgui.get_main_viewport().pos.y)
app_state.myNvgDemo.render(width, height, mouse_pos.x, mouse_pos.y, now)

def custom_background():
clear_color_vec4 = ImVec4(*app_state.clear_color)
nvg_imgui.render_nvg_to_background(app_state.vg, nvg_drawing_function, clear_color_vec4)

runner_params.callbacks.custom_background = custom_background

def gui():
imgui.begin("My Window!", None, imgui.WindowFlags_.always_auto_resize.value)
_, app_state.display_in_frame_buffer = imgui.checkbox("Display in framebuffer", app_state.display_in_frame_buffer)
_, app_state.myNvgDemo.blowup = imgui.checkbox("Blowup", app_state.myNvgDemo.blowup)
imgui.set_next_item_width(hello_imgui.em_size(15))
_, app_state.clear_color = imgui.color_edit4("Clear Color", app_state.clear_color)

if app_state.display_in_frame_buffer:
clear_color_vec4 = ImVec4(*app_state.clear_color)
nvg_imgui.render_nvg_to_frame_buffer(app_state.vg, app_state.myFrameBuffer, nvg_drawing_function, clear_color_vec4)
imgui.image(app_state.myFrameBuffer.texture_id, ImVec2(1000, 600))

imgui.end()

runner_params.callbacks.show_gui = gui

runner_params.fps_idling.enable_idling = False

hello_imgui.run(runner_params)


if __name__ == "__main__":
main()
Loading

0 comments on commit 6f653eb

Please sign in to comment.