2. Widgets and state
The setup panel of a solver: its parameters are ordinary Fortran variables, and the widgets edit them in place.
!< tut02_widgets, tutorial chapter 2: widgets bound to Fortran variables.
program tut02_widgets
!< tut02_widgets, tutorial chapter 2: widgets bound to Fortran variables.
use, intrinsic :: iso_fortran_env, only : int32, real32, real64
use figure, only : app_object, ig_begin, ig_button, ig_checkbox, ig_end, ig_input, ig_same_line, ig_separator, &
ig_set_next_window, ig_show_demo_window, ig_slider, ig_text
implicit none
type(app_object) :: app !< Application.
integer(int32) :: cells !< Grid cells.
real(real32) :: relax !< Relaxation factor.
real(real64) :: cfl !< CFL number.
real(real64) :: t_end !< Final time [s].
logical :: viscous !< Viscous terms on.
logical :: show_tools !< Tools window open.
logical :: show_demo !< Dear ImGui demo open.
integer(int32) :: runs !< Times "Run" was pressed.
character(len=80) :: line !< Text buffer.
cells = 100_int32
relax = 0.8_real32
cfl = 0.5_real64
t_end = 1.0_real64
viscous = .true.
show_tools = .true.
show_demo = .false.
runs = 0_int32
call app%init('FIGURE: widgets', 900_int32, 600_int32)
do while (app%next_frame())
call ig_set_next_window(20.0_real64, 20.0_real64, 420.0_real64, 320.0_real64)
if (ig_begin('Setup')) then
if (ig_slider('cells', cells, 10_int32, 1000_int32)) continue
if (ig_slider('relaxation', relax, 0.1_real32, 1.0_real32)) continue
if (ig_slider('CFL', cfl, 0.1_real64, 2.0_real64)) continue
if (ig_input('final time', t_end, step=0.1_real64, format='%.3f')) then
t_end = max(t_end, 0.0_real64)
endif
if (ig_checkbox('viscous', viscous)) continue
call ig_separator()
if (ig_button('Run')) runs = runs + 1_int32
call ig_same_line()
write(line, '(A,I0,A,I0,A,F5.2)') 'runs: ', runs, ', cells: ', cells, ', CFL: ', cfl
call ig_text(trim(line))
if (ig_button('Reset##cfl')) cfl = 0.5_real64
call ig_same_line()
if (ig_button('Reset##time')) t_end = 1.0_real64
if (ig_checkbox('tools window', show_tools)) continue
if (ig_checkbox('Dear ImGui demo', show_demo)) continue
endif
call ig_end()
if (show_tools) then
call ig_set_next_window(460.0_real64, 20.0_real64, 300.0_real64, 120.0_real64)
if (ig_begin('Tools', show_tools)) then
call ig_text('Close me with the x in the title bar.')
endif
call ig_end()
endif
if (show_demo) call ig_show_demo_window(show_demo)
enddo
call app%destroy()
endprogram tut02_widgetsWidgets edit your variables
if (ig_slider('cells', cells, 10_int32, 1000_int32)) continue
if (ig_slider('relaxation', relax, 0.1_real32, 1.0_real32)) continue
if (ig_slider('CFL', cfl, 0.1_real64, 2.0_real64)) continue
if (ig_input('final time', t_end, step=0.1_real64, format='%.3f')) then
t_end = max(t_end, 0.0_real64)
endif
if (ig_checkbox('viscous', viscous)) continue
call ig_separator()
if (ig_button('Run')) runs = runs + 1_int32
call ig_same_line()
write(line, '(A,I0,A,I0,A,F5.2)') 'runs: ', runs, ', cells: ', cells, ', CFL: ', cfl
call ig_text(trim(line))ig_slideris generic:cellsisint32,relaxisreal32,cflisreal64; bounds have the same kind.ig_inputedits areal64with-/+buttons (step) and a Cprintfformat.- Each widget returns
.true.in the frame the user changed the value.if (...) continuediscards it; theig_inputline uses it to clamp the value as soon as it is edited. ig_buttonreturns.true.on the frame it is clicked: the counter grows by one per click.ig_textshows plain text: numbers are composed with an internalwrite, andtrimdrops the trailing blanks of the buffer.ig_same_lineputs the next item on the same line;ig_separatordraws a line.
None of these variables needs target, and nothing is registered: the next frame, the widgets read the variables again. Change cfl in the program, and the slider moves.
Same text, different widgets
if (ig_button('Reset##cfl')) cfl = 0.5_real64
call ig_same_line()
if (ig_button('Reset##time')) t_end = 1.0_real64Two buttons labelled "Reset" would be one widget to Dear ImGui, which identifies widgets by label. The text after ## is part of the identifier but is not shown.
A window the user can close
if (show_tools) then
call ig_set_next_window(460.0_real64, 20.0_real64, 300.0_real64, 120.0_real64)
if (ig_begin('Tools', show_tools)) then
call ig_text('Close me with the x in the title bar.')
endif
call ig_end()
endif
if (show_demo) call ig_show_demo_window(show_demo)With the open argument, the window gets a close button that sets show_tools to .false.; guarding the window with the same variable makes it disappear, and the check box in "Setup" brings it back. ig_show_demo_window is the Dear ImGui demo, with the same convention.
ig_set_next_window places a window the first time it appears; afterwards the user moves it. The place is not remembered across runs unless the program names a layout file (app%init(..., ini_file='hello.ini'), see Frame loop).
What you learned
Widgets bound to variables, their return values, identifiers, closable windows. Reference: Widgets.
Next: 3. Plots.