Nuklear/Readme.md

410 lines
16 KiB
Markdown
Raw Normal View History

2015-03-11 16:00:59 +03:00
# GUI
2015-05-21 14:06:29 +03:00
This is a bloat free minimal state immediate mode graphical user interface toolkit
2015-04-26 12:33:31 +03:00
written in ANSI C. It was designed as a embeddable user interface for graphical
application and does not have any direct dependencies.
2015-03-11 16:00:59 +03:00
2015-03-14 19:05:30 +03:00
## Features
2015-04-16 17:20:00 +03:00
- Immediate mode graphical user interface toolkit
2015-03-14 19:05:30 +03:00
- Written in C89 (ANSI C)
2015-05-03 12:45:29 +03:00
- Small codebase (~3kLOC)
2015-05-17 15:39:02 +03:00
- Focus on portability, efficiency, simplicity and minimal internal state
- Suited for embedding into graphical applications
2015-05-17 15:39:02 +03:00
- No global or hidden state
- No direct dependencies (not even libc!)
- Full memory management control
2015-03-25 16:32:43 +03:00
- Renderer and platform independent
2015-05-11 13:45:22 +03:00
- Configurable style and colors
2015-04-30 17:12:21 +03:00
- UTF-8 support
2015-03-24 15:08:42 +03:00
2015-04-16 21:05:43 +03:00
## Limitations
2015-04-28 23:08:07 +03:00
- Does NOT provide os window/input management
2015-04-16 21:05:43 +03:00
- Does NOT provide a renderer backend
2015-04-24 16:17:10 +03:00
- Does NOT implement a font library
2015-04-16 21:05:43 +03:00
Summary: It is only responsible for the actual user interface
2015-04-16 17:20:00 +03:00
2015-05-13 01:28:06 +03:00
## Target applications
- Graphical tools/editors
- Library testbeds
- Game engine debugging UI
- Graphical overlay
2015-05-09 16:22:23 +03:00
## Gallery
2015-05-09 21:28:36 +03:00
![gui screenshot](/screen/demo.png?raw=true)
2015-05-09 21:26:23 +03:00
![gui screenshot](/screen/config.png?raw=true)
2015-05-19 19:50:11 +03:00
![gui screenshot](/screen/screenshot.png?raw=true)
2015-05-09 16:22:23 +03:00
2015-04-25 17:44:43 +03:00
## Example
```c
2015-05-30 22:20:04 +03:00
/* allocate memory to hold the draw commands */
2015-05-29 22:36:54 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
void *memory = malloc(MEMORY_SIZE)
2015-05-29 22:36:54 +03:00
gui_command_buffer_init_fixed(buffer, memory, MEMORY_SIZE);
2015-05-11 14:25:05 +03:00
2015-05-29 15:47:42 +03:00
/* setup configuration */
struct gui_config config;
struct gui_font font = {...};
2015-05-31 23:30:56 +03:00
gui_config_default(&config, GUI_DEFAULT_ALL, &font);
2015-05-29 15:47:42 +03:00
/* initialize panel */
struct gui_panel panel;
2015-04-25 17:55:27 +03:00
gui_panel_init(&panel, 50, 50, 220, 170,
2015-04-25 17:44:43 +03:00
GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE|
GUI_PANEL_CLOSEABLE|GUI_PANEL_SCALEABLE|
2015-05-29 15:47:42 +03:00
GUI_PANEL_MINIMIZABLE, &config, &buffer);
2015-04-25 17:44:43 +03:00
2015-05-29 15:47:42 +03:00
struct gui_input input = {0};
2015-04-25 17:44:43 +03:00
while (1) {
gui_input_begin(&input);
/* record input */
gui_input_end(&input);
/* GUI */
2015-05-29 15:47:42 +03:00
struct gui_panel_layout layout;
gui_panel_begin(&layout, &panel, "Demo", &input);
2015-04-25 17:44:43 +03:00
gui_panel_row(&layout, 30, 1);
if (gui_panel_button_text(&layout, "button", GUI_BUTTON_DEFAULT)) {
/* event handling */
}
gui_panel_row(&layout, 30, 2);
2015-04-25 17:49:40 +03:00
if (gui_panel_option(&layout, "easy", option == 0)) option = 0;
if (gui_panel_option(&layout, "hard", option == 1)) option = 1;
gui_panel_label(&layout, "input:", GUI_TEXT_LEFT);
len = gui_panel_edit(&layout, buffer, len, 256, &active, GUI_INPUT_DEFAULT);
2015-04-25 17:44:43 +03:00
gui_panel_end(&layout, &panel);
/* draw */
2015-05-16 13:27:51 +03:00
const struct gui_command *cmd;
2015-05-29 15:47:42 +03:00
gui_foreach_command(cmd, buffer) {
/* execute draw call command */
2015-04-25 17:44:43 +03:00
}
}
```
2015-05-09 21:26:23 +03:00
![gui screenshot](/screen/screen.png?raw=true)
2015-04-25 17:44:43 +03:00
## IMGUIs
Immediate mode in contrast to classical retained mode GUIs store as little state as possible
by using procedural function calls as "widgets" instead of storing objects.
Each "widget" function call takes hereby all its necessary data and immediately returns
the through the user modified state back to the caller. Immediate mode graphical
user interfaces therefore combine drawing and input handling into one unit
instead of separating them like retain mode GUIs.
Since there is no to minimal internal state in immediate mode user interfaces,
updates have to occur every frame which on one hand is more drawing expensive than classic
retained GUI implementations but on the other hand grants a lot more flexibility and
support for overall layout changes. In addition without any state there is no
duplicated state between your program, the gui and the user which greatly
simplifies code. Further traits of immediate mode graphic user interfaces are a
code driven style, centralized flow control, easy extensibility and
understandability.
2015-04-20 22:14:58 +03:00
2015-04-25 17:44:43 +03:00
### Input
2015-05-03 12:45:29 +03:00
The `gui_input` struct holds the user input over the course of the frame and
manages the complete modification of widget and panel state. To fill the
structure with data over the frame there are a number of functions provided for
key, motion, button and text input. The input is hereby completly independent of
the underlying platform or way of input so even touch or other ways of input are
possible.
Like the panel and the buffer, input is based on an immediate mode API and
consist of an begin sequence with `gui_input_begin` and a end sequence point
with `gui_input_end`. All modifications can only occur between both of these
sequence points while all outside modification provoke undefined behavior.
2015-04-25 17:44:43 +03:00
```c
struct gui_input input = {0};
while (1) {
gui_input_begin(&input);
/* record input */
gui_input_end(&input);
}
```
2015-04-24 16:17:10 +03:00
### Configuration
The gui toolkit provides a number of different attributes that can be
configured, like spacing, padding, size and color.
While the widget API even expects you to provide the configuration
for each and every widget the panel layer provides you with a set of
attributes in the `gui_config` structure. The structure either needs to be
filled by the user or can be setup with some default values by the function
2015-05-20 18:27:21 +03:00
`gui_config_default`. Modification on the fly to the `gui_config` struct is in
2015-05-03 13:22:43 +03:00
true immediate mode fashion possible and supported.
2015-04-24 16:17:10 +03:00
```c
struct gui_config {
2015-05-20 18:33:41 +03:00
struct gui_vec2 properties[GUI_PROPERTY_MAX];
struct gui_color colors[GUI_COLOR_COUNT];
};
```
2015-05-20 18:33:41 +03:00
In addition to modifing the `gui_config` struct directly the configration API
2015-05-20 19:11:46 +03:00
enables you to temporarily change a property or color and revert back directly
2015-05-29 15:47:42 +03:00
after the change is no longer needed. The number of temporary changes are
limited but can be changed with constants `GUI_MAX_COLOR_STACK` and
`GUI_MAX_ATTRIB_STACK`.
2015-05-20 18:33:41 +03:00
```c
gui_config_push_color(config, GUI_COLORS_PANEL, 255, 0, 0, 255);
gui_config_push_attribute(config, GUI_ATTRIBUTE_PADDING, 10.0f, 5.0f);
2015-05-20 18:37:06 +03:00
/* use the configuration data */
2015-05-20 18:33:41 +03:00
gui_config_pop_attribute(config);
gui_config_pop_color(config);
```
2015-05-29 15:47:42 +03:00
Since there is no direct font implementation in the toolkit but font handling is
still an aspect of a gui implementation, the `gui_font` struct was introduced. It only
contains the bare minimum of what is needed for font handling.
For widgets the `gui_font` data has to be persistent while the
panel hold the font internally. Important to node is that the font does not hold
your font data but merely references it so you have to make sure that the font
always points to a valid object.
2015-05-03 12:45:29 +03:00
```c
2015-05-29 15:47:42 +03:00
struct gui_font {
void *userdata;
2015-05-29 15:47:42 +03:00
gui_float height;
gui_text_width_f width;
};
```
2015-05-29 15:47:42 +03:00
### Buffer
2015-05-13 01:28:06 +03:00
Almost all memory as well as object management for the toolkit
is left to the user for maximum control. In fact a big subset of the toolkit can
2015-05-13 01:28:06 +03:00
be used without any heap allocation at all. The only place where heap allocation
2015-05-29 15:47:42 +03:00
is needed at all is for buffering draw calls. While the standart way of
memory allocation in that case for libraries is to just provide allocator callbacks
which is implemented aswell with the `gui_allocator`
2015-05-13 01:28:06 +03:00
structure, there are two addition ways to provided memory. The
first one is to just providing a static fixed size memory block to fill up which
is handy for UIs with roughly known memory requirements. The other way of memory
2015-05-13 01:28:06 +03:00
managment is to extend the fixed size block with the abiltiy to resize your block
at the end of the frame if there is not enough memory.
For the purpose of resizable fixed size memory blocks and for general
2015-05-29 15:47:42 +03:00
information about memory consumption the `gui_memory_info` structure was
2015-05-13 01:28:06 +03:00
added. It contains information about the allocated amount of data in the current
frame as well as the needed amount if not enough memory was provided.
```c
2015-05-29 15:47:42 +03:00
void *memory = malloc(size);
2015-05-29 22:36:54 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
gui_command_buffer_init_fixed(&buffer, memory, size);
```
2015-05-03 12:45:29 +03:00
```c
2015-05-29 15:47:42 +03:00
struct gui_allocator alloc;
alloc.userdata = your_allocator;
alloc.alloc = your_allocation_callback;
alloc.relloac = your_reallocation_callback;
alloc.free = your_free_callback;
2015-05-03 12:45:29 +03:00
2015-05-29 22:36:54 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
const gui_size initial_size = 4*1024;
const gui_float grow_factor = 2.0f;
gui_command_buffer_init(&buffer, &alloc, initial_size, grow_factor);
```
2015-04-20 22:14:58 +03:00
### Widgets
2015-04-26 12:33:31 +03:00
The minimal widget API provides a number of basic widgets and is designed for
2015-05-03 13:22:43 +03:00
uses cases where no complex widget layouts or grouping is needed.
2015-04-26 12:33:31 +03:00
In order for the GUI to work each widget needs a canvas to
2015-04-20 22:14:58 +03:00
draw to, positional and widgets specific data as well as user input
and returns the from the user input modified state of the widget.
2015-04-17 23:28:00 +03:00
```c
2015-05-29 15:47:42 +03:00
2015-05-30 12:52:02 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
void *memory = malloc(MEMORY_SIZE)
gui_buffer_init_fixed(buffer, memory, MEMORY_SIZE);
struct gui_font font = {...};
2015-05-24 00:25:40 +03:00
const struct gui_slider slider = {...};
const struct gui_progress progress = {...};
gui_float value = 5.0f
gui_size prog = 20;
2015-04-17 23:28:00 +03:00
2015-05-29 15:47:42 +03:00
struct gui_input input = {0};
2015-04-17 23:28:00 +03:00
while (1) {
2015-05-16 13:27:51 +03:00
gui_input_begin(&input);
/* record input */
gui_input_end(&input);
2015-05-29 15:47:42 +03:00
gui_command_buffer_reset(&buffer);
value = gui_slider(&buffer, 50, 50, 100, 30, 0, value, 10, 1, &slider, &input);
prog = gui_progress(&buffer, 50, 100, 100, 30, prog, 100, gui_false, &progress, &input);
const struct gui_command *cmd;
gui_foreach_command(cmd, buffer) {
/* execute draw call command */
}
2015-04-17 23:28:00 +03:00
}
```
2015-04-20 22:14:58 +03:00
### Panels
To further extend the basic widget layer and remove some of the boilerplate
code the panel was introduced. The panel groups together a number of
2015-04-26 00:39:46 +03:00
widgets but in true immediate mode fashion does not save any state from
2015-04-20 22:14:58 +03:00
widgets that have been added to the panel. In addition the panel enables a
2015-04-26 00:39:46 +03:00
number of nice features on a group of widgets like movement, scaling,
2015-05-10 12:19:28 +03:00
hidding and minimizing. An additional use for panel is to further extend the
2015-04-26 00:39:46 +03:00
grouping of widgets into tabs, groups and shelfs.
2015-04-26 12:33:31 +03:00
The panel is divided into a `struct gui_panel` with persistent life time and
the `struct gui_panel_layout` structure with a temporary life time.
2015-05-03 13:22:43 +03:00
While the layout state is constantly modified over the course of
2015-04-26 12:33:31 +03:00
the frame, the panel struct is only modified at the immediate mode sequence points
2015-04-26 00:39:46 +03:00
`gui_panel_begin` and `gui_panel_end`. Therefore all changes to the panel struct inside of both
sequence points have no effect in the current frame and are only visible in the
next frame.
2015-04-20 22:14:58 +03:00
2015-05-07 16:52:35 +03:00
### Stack
While using basic panels is fine for a single movable panel or a big number of
static panels, it has rather limited support for overlapping movable panels. For
that to change the panel stack was introduced. The panel stack holds the basic
drawing order of each panel so instead of drawing each panel individually they
have to be drawn in a certain order.
2015-05-07 16:52:35 +03:00
```c
2015-05-29 15:47:42 +03:00
/* allocate buffer to hold output */
2015-05-29 22:36:54 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
gui_buffer_init_fixed(buffer, memory, size);
2015-05-12 18:08:39 +03:00
2015-05-29 15:47:42 +03:00
/* setup configuration data */
2015-05-07 16:52:35 +03:00
struct gui_config config;
struct gui_font font = {...}
2015-05-31 23:30:56 +03:00
gui_config_default(&config, GUI_DEFAULT_ALL, &font);
2015-05-12 18:08:39 +03:00
2015-05-29 15:47:42 +03:00
/* setup panel */
2015-05-30 12:52:02 +03:00
struct gui_panel panel;
gui_panel_init(&panel, 50, 50, 300, 200, 0, &config, &buffer);
2015-05-29 15:47:42 +03:00
/* setup stack */
2015-05-12 18:08:39 +03:00
struct gui_stack stack;
2015-05-07 16:52:35 +03:00
gui_stack_clear(&stack);
2015-05-30 12:52:02 +03:00
gui_stack_push(&stack, &panel);
2015-05-07 16:52:35 +03:00
2015-05-29 15:47:42 +03:00
struct gui_input input = {0};
2015-05-07 16:52:35 +03:00
while (1) {
2015-05-16 13:27:51 +03:00
gui_input_begin(&input);
/* record input */
gui_input_end(&input);
2015-05-30 12:52:02 +03:00
struct gui_panel_layout layout;
gui_panel_begin_stacked(&layout, &panel, &stack, "Demo", &input);
2015-05-07 16:52:35 +03:00
gui_panel_row(&layout, 30, 1);
if (gui_panel_button_text(&layout, "button", GUI_BUTTON_DEFAULT))
fprintf(stdout, "button pressed!\n");
2015-05-29 15:47:42 +03:00
gui_panel_end(&layout, &win.hook);
2015-05-17 15:39:02 +03:00
/* draw each panel */
2015-05-29 15:47:42 +03:00
struct gui_panel *iter;
gui_foreach_panel(iter, &stack) {
2015-05-17 15:39:02 +03:00
const struct gui_command *cmd
2015-05-29 15:47:42 +03:00
gui_foreach_command(cmd, panel->buffer)) {
2015-05-17 15:39:02 +03:00
/* execute command */
}
}
}
```
### Tiling
Stacked windows are only one side of the coin for panel layouts while
a tiled layout is the other. Tiled layouts divide the screen into regions called
slots in this case the top, left, center, right and bottom slot. Each slot occupies a
certain percentage on the screen and can be filled with panels either
horizontally or vertically. The combination of slots, ratio and multiple panels
per slots support a rich set of vertical, horizontal and mixed layouts.
2015-05-17 15:39:02 +03:00
```c
2015-05-30 12:52:02 +03:00
gui_command_buffer buffer;
2015-05-29 15:47:42 +03:00
gui_buffer_init_fixed(buffer, memory, size);
2015-05-17 15:39:02 +03:00
struct gui_config config;
struct gui_font font = {...}
2015-05-31 23:30:56 +03:00
gui_config_default(&config, GUI_DEFAULT_ALL, &font);
2015-05-29 15:47:42 +03:00
2015-05-30 12:52:02 +03:00
struct gui_panel panel;
2015-05-29 15:47:42 +03:00
struct gui_input input = {0};
2015-05-30 12:52:02 +03:00
gui_panel_init(&panel, 0, 0, 0, 0, 0, &config, &buffer);
2015-05-17 15:39:02 +03:00
struct gui_layout tiled;
struct gui_layout_config ratio = {...};
2015-05-30 22:20:04 +03:00
gui_layout_init(&tiled, &ratio, window_width, window_height);
gui_layout_slot(&tiled, GUI_SLOT_LEFT, GUI_LAYOUT_VERTICAL, 1);
2015-05-17 15:39:02 +03:00
while (1) {
gui_input_begin(&input);
/* record input */
gui_input_end(&input);
2015-06-01 01:56:09 +03:00
struct gui_panel_layout layout;
2015-05-30 12:52:02 +03:00
gui_panel_begin_tiled(&layout, &panel, &tiled, GUI_SLOT_LEFT, 0, "Demo", &input);
2015-05-17 15:39:02 +03:00
gui_panel_row(&layout, 30, 1);
if (gui_panel_button_text(&layout, "button", GUI_BUTTON_DEFAULT))
fprintf(stdout, "button pressed!\n");
2015-05-30 12:52:02 +03:00
gui_panel_end(&layout, &panel);
2015-05-07 16:52:35 +03:00
/* draw each panel */
2015-05-29 15:47:42 +03:00
struct gui_panel *iter;
gui_foreach_panel(iter, &layout.stack) {
2015-05-16 13:27:51 +03:00
const struct gui_command *cmd
2015-05-29 15:47:42 +03:00
gui_foreach_command(cmd, iter->buffer) {
/* execute draw call command */
2015-05-07 16:52:35 +03:00
}
}
}
```
2015-04-12 16:40:42 +03:00
## FAQ
2015-04-20 23:29:31 +03:00
#### Where is the demo/example code?
2015-04-30 17:12:21 +03:00
The demo and example code can be found in the demo folder.
There is demo code for Linux(X11), Windows(win32) and OpenGL(SDL2, freetype).
2015-04-30 17:46:11 +03:00
As for now there will be no DirectX demo since I don't have experience
programming using DirectX but you are more than welcome to provide one.
2015-04-12 16:40:42 +03:00
2015-04-16 21:05:43 +03:00
#### Why did you use ANSI C and not C99 or C++?
2015-04-12 16:40:42 +03:00
Personally I stay out of all "discussions" about C vs C++ since they are totally
worthless and never brought anything good with it. The simple answer is I
2015-05-03 13:22:43 +03:00
personally love C and have nothing against people using C++ especially the new
2015-04-16 17:20:00 +03:00
iterations with C++11 and C++14.
2015-04-12 16:40:42 +03:00
While this hopefully settles my view on C vs C++ there is still ANSI C vs C99.
2015-04-16 21:05:43 +03:00
While for personal projects I only use C99 with all its niceties, libraries are
2015-04-16 17:20:00 +03:00
a little bit different. Libraries are designed to reach the highest number of
2015-04-17 23:28:00 +03:00
users possible which brings me to ANSI C as the most portable version.
2015-04-16 21:05:43 +03:00
In addition not all C compiler like the MSVC
2015-04-16 17:20:00 +03:00
compiler fully support C99, which finalized my decision to use ANSI C.
2015-04-12 16:40:42 +03:00
2015-04-20 23:29:31 +03:00
#### Why do you typedef your own types instead of using the standard types?
2015-04-16 21:05:43 +03:00
This Project uses ANSI C which does not have the header file `<stdint.h>`
and therefore does not provide the fixed sized types that I need. Therefore
2015-04-12 16:40:42 +03:00
I defined my own types which need to be set to the correct size for each
2015-05-03 13:22:43 +03:00
platform. But if your development environment provides the header file you can define
2015-04-12 16:40:42 +03:00
`GUI_USE_FIXED_SIZE_TYPES` to directly use the correct types.
2015-04-20 23:29:31 +03:00
#### Why is font/input/window management not provided?
2015-04-17 23:28:00 +03:00
As for window and input management it is a ton of work to abstract over
all possible platforms and there are already libraries like SDL or SFML or even
the platform itself which provide you with the functionality.
So instead of reinventing the wheel and trying to do everything the project tries
2015-05-03 13:22:43 +03:00
to be as independent and out of the users way as possible.
This means in practice a little bit more work on the users behalf but grants a
2015-04-17 23:28:00 +03:00
lot more freedom especially because the toolkit is designed to be embeddable.
2015-04-24 16:17:10 +03:00
The font management on the other hand is litte bit more tricky. In the beginning
2015-04-20 12:13:27 +03:00
the toolkit had some basic font handling but I removed it later. This is mainly
2015-04-17 23:28:00 +03:00
a question of if font handling should be part of a gui toolkit or not. As for a
framework the question would definitely be yes but for a toolkit library the
question is not as easy. In the end the project does not have font handling
since there are already a number of font handling libraries in existence or even the
platform (Xlib, Win32) itself already provides a solution.
2015-03-14 19:05:30 +03:00
## References
2015-04-12 16:40:42 +03:00
- [Tutorial from Jari Komppa about imgui libraries](http://www.johno.se/book/imgui.html)
2015-03-25 16:32:43 +03:00
- [Johannes 'johno' Norneby's article](http://iki.fi/sol/imgui/)
2015-04-12 16:40:42 +03:00
- [Casey Muratori's original introduction to imgui's](http:://mollyrocket.com/861?node=861)
- [Casey Muratori's imgui panel design (1/2)](http://mollyrocket.com/casey/stream_0019.html)
- [Casey Muratori's imgui panel design (2/2)](http://mollyrocket.com/casey/stream_0020.html)
2015-04-24 16:17:10 +03:00
- [Casey Muratori: Designing and Evaluation Reusable Components](http://mollyrocket.com/casey/stream_0028.html)
2015-04-12 16:40:42 +03:00
- [ImGui: The inspiration for this project](https://github.com/ocornut/imgui)
2015-04-20 22:36:16 +03:00
- [Nvidia's imgui toolkit](https://code.google.com/p/nvidia-widgets/)
2015-03-14 19:05:30 +03:00
2015-03-03 19:24:02 +03:00
# License
(The MIT License)