module Adjutant
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
using Observables, UUIDs
using Pkg
const DEBUG = get(ENV, "ADJUTANT_DEBUG", "0") == "1"
include("processing.jl")
include("widgets.jl")
include("node_editor.jl")
Base.@kwdef mutable struct AdjutantState
widgets = WidgetContainer()
end
function gui_main(func)
adj = AdjutantState()
func(adj)
# OpenGL 3.2 + GLSL 150
glsl_version = 150
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE)
# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)
# create window
window = GLFW.CreateWindow(1280, 720, "Adjutant")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1) # enable vsync
# setup Dear ImGui context
ctx = CImGui.CreateContext()
# setup Dear ImGui style
CImGui.StyleColorsDark()
# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)
binds = Dict(
#name == "c" && mods == 2 && exit(0)
"Q" => () -> exit(0),
"C" => () -> clear_all!(adj),
"t" => Dict(
"c" => () -> clear_task!(adj),
),
"m" => Dict(
"q" => () -> query_models!(adj),
"c" => () -> clear_model!(adj),
"f" => () -> fit!(adj),
),
)
current_dict = Ref(binds)
function dispatch_keybinding(key, scancode, actions, mods)
name = GLFW.GetKeyName(key, scancode)
if name isa String && mods == 2 # Ctrl
name = uppercase(name)
end
if haskey(current_dict[], name)
next_obj = current_dict[][name]
if next_obj isa Function
next_obj()
current_dict[] = binds
else
@assert next_obj isa Dict
current_dict[] = next_obj
end
else
current_dict[] = binds
end
end
# setup keyboard callbacks
SetCustomKeyCallback() do window, key, scancode, action, mods
if action == GLFW.RELEASE
dispatch_keybinding(key, scancode, action, mods)
end
if DEBUG
name = GLFW.GetKeyName(key, scancode)
if name === nothing
println("GLFW: scancode $scancode ", action, " ", mods)
else
println("GLFW: key $name ", action, " ", mods)
end
end
end
push!(adj.widgets, AddWidgetButton())
clear_color = Cfloat[0.0, 0.0, 0.0, 1.0]
while !GLFW.WindowShouldClose(window)
GLFW.PollEvents()
# start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplGlfw_NewFrame()
CImGui.NewFrame()
CImGui.Begin("Adjutant")
adj.widgets(adj)
CImGui.End()
# rendering
CImGui.Render()
GLFW.MakeContextCurrent(window)
display_w, display_h = GLFW.GetFramebufferSize(window)
glViewport(0, 0, display_w, display_h)
glClearColor(clear_color...)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())
GLFW.MakeContextCurrent(window)
GLFW.SwapBuffers(window)
end
# cleanup
ImGui_ImplOpenGL3_Shutdown()
ImGui_ImplGlfw_Shutdown()
CImGui.DestroyContext(ctx)
GLFW.DestroyWindow(window)
end
end