-
Notifications
You must be signed in to change notification settings - Fork 3
/
glfw_opengl3_imnodes.nim
205 lines (171 loc) · 6.2 KB
/
glfw_opengl3_imnodes.nim
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import std/[strutils]
import nimgl/[opengl,glfw]
import imguin/[glfw_opengl]
import imguin/lang/imgui_ja_gryph_ranges
include ../utils/setupFonts
include imguin/simple
import imnodeDemo
const MainWinWidth = 1024
const MainWinHeight = 800
#--------------
# Configration
#--------------
# .--------------------------------------------..---------.-----------------------.------------
# | Combination of flags || | Viewport |
# |--------------------------------------------||---------|-----------------------|------------
# | fViewport | fDocking | TransparentViewport || Docking | Transparent | Outside | Description
# |:---------:|:--------:|:-------------------:||:-------:|:-----------:|:-------:| -----------
# | false | false | false || - | - | - |
# | false | true | false || v | - | - | (Default): Only docking
# | true | - | false || v | - | v | Docking and outside of viewport
# | - | - | true || v | v | - | Transparent Viewport and docking
# `-----------'----------'---------------------'`---------'-------------'---------'-------------
var
fDocking = true
fViewport = false
TransparentViewport = false
#
block:
if TransparentViewport:
fViewport = true
if fViewport:
fDocking = true
#---------------------
# Forward definitions
#---------------------
proc winMain(hWin: glfw.GLFWWindow)
#------
# main
#------
proc main() =
doAssert glfwInit()
defer: glfwTerminate()
if TransparentViewport:
glfwWindowHint(GLFWVisible, GLFW_FALSE)
glfwWindowHint(GLFWContextVersionMajor, 3)
glfwWindowHint(GLFWContextVersionMinor, 3)
glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE)
glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFWResizable, GLFW_TRUE)
#
glfwWindowHint(GLFWVisible, GLFW_FALSE)
var glfwWin = glfwCreateWindow(MainWinWidth, MainWinHeight, "ImNodes")
if glfwWin.isNil:
quit(-1)
glfwWin.makeContextCurrent()
defer: glfwWin.destroyWindow()
glfwSwapInterval(1) # Enable vsync
doAssert glInit() # OpenGL init
# Setup ImGui
let context = igCreateContext(nil)
defer: context.igDestroyContext()
# setup ImNodes
imnodes_CreateContext()
defer:imnodes_DestroyContext(nil)
if fDocking:
var pio = igGetIO()
pio.ConfigFlags = pio.ConfigFlags or ImGui_ConfigFlags_DockingEnable.cint
if fViewport:
pio.ConfigFlags = pio.ConfigFlags or ImGui_ConfigFlags_ViewportsEnable.cint
pio.ConfigViewports_NoAutomerge = true
# GLFW + OpenGL
const glsl_version = "#version 130" # GL 3.0 + GLSL 130
doAssert ImGui_ImplGlfw_InitForOpenGL(cast[ptr GLFWwindow](glfwwin), true)
defer: ImGui_ImplGlfw_Shutdown()
doAssert ImGui_ImplOpenGL3_Init(glsl_version)
defer: ImGui_ImplOpenGL3_Shutdown()
glfwWin.winMain()
#---------
# winMain
#---------
proc winMain(hWin: glfw.GLFWWindow) =
var
showDemoWindow = true
showAnotherWindow = false
showFirstWindow = true
fval = 0.5f
counter = 0
sBuf = newString(200)
clearColor:ccolor
showWindowDelay = 1 # TODO
if TransparentViewport:
clearColor = ccolor(elm:(x:0f, y:0f, z:0f, w:0.0f)) # Transparent
else:
clearColor = ccolor(elm:(x:0.25f, y:0.65f, z:0.85f, w:1.0f))
igStyleColorsClassic(nil)
# Add multibytes font
discard setupFonts()
# ImNode demo init
NodeEditorInitialize()
defer: NodeEditorShutdown()
var pio = igGetIO()
while not hwin.windowShouldClose:
glfwPollEvents()
# start imgui frame
ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplGlfw_NewFrame()
igNewFrame()
if showDemoWindow:
igShowDemoWindow(addr showDemoWindow)
# ImNodes demo
NodeEditorShow()
# show a simple window that we created ourselves.
if showFirstWindow:
igBegin("Nim: Dear ImGui test with Futhark", addr showFirstWindow, 0)
defer: igEnd()
var s = "GLFW v" & $glfwGetVersionString()
igText(s.cstring)
s = "OpenGL v" & ($cast[cstring](glGetString(GL_VERSION))).split[0]
igText(s.cstring)
igInputTextWithHint("InputText" ,"Input text here" ,sBuf)
s = "Input result:" & sBuf
igText(s.cstring)
igCheckbox("Demo window", addr showDemoWindow)
igCheckbox("Another window", addr showAnotherWindow)
igSliderFloat("Float", addr fval, 0.0f, 1.0f, "%.3f", 0)
igColorEdit3("Background color", clearColor.array3, 0.ImGuiColorEditFlags)
if igButton("Button", ImVec2(x: 0.0f, y: 0.0f)):
inc counter
igSameLine(0.0f, -1.0f)
igText("counter = %d", counter)
igText("Application average %.3f ms/frame (%.1f FPS)".cstring, (1000.0f / igGetIO().Framerate.float).cfloat, (igGetIO().Framerate).cfloat)
igSeparatorText(ICON_FA_WRENCH & " Icon font test ")
igText(ICON_FA_TRASH_CAN & " Trash")
igText(ICON_FA_MAGNIFYING_GLASS_PLUS &
" " & ICON_FA_POWER_OFF &
" " & ICON_FA_MICROPHONE &
" " & ICON_FA_MICROCHIP &
" " & ICON_FA_VOLUME_HIGH &
" " & ICON_FA_SCISSORS &
" " & ICON_FA_SCREWDRIVER_WRENCH &
" " & ICON_FA_BLOG)
# show further samll window
if showAnotherWindow:
igBegin("imgui Another Window", addr showAnotherWindow, 0)
igText("Hello from imgui")
if igButton("Close me", ImVec2(x: 0.0f, y: 0.0f)):
showAnotherWindow = false
igEnd()
# render
igRender()
glClearColor(clearColor.elm.x, clearColor.elm.y, clearColor.elm.z, clearColor.elm.w)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData())
if 0 != (pio.ConfigFlags and ImGui_ConfigFlags_ViewportsEnable.cint):
var backup_current_window = glfwGetCurrentContext()
igUpdatePlatformWindows()
igRenderPlatformWindowsDefault(nil, nil)
backup_current_window.makeContextCurrent()
hwin.swapBuffers()
if not showFirstWindow and not showDemoWindow and not showAnotherWindow:
hwin.setWindowShouldClose(true) # End program
if showWindowDelay > 0:
dec showWindowDelay
else:
once: # Avoid flickering screen at startup.
hWin.showWindow()
#### end while
#------
# main
#------
main()