~jpsamaroo/Adjutant.jl

ref: c93000a0359728120848e82a6dc0208c83f48338 Adjutant.jl/src/Adjutant.jl -rw-r--r-- 3.3 KiB
c93000a0Julian P Samaroo Remove extra deps, make things work again 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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