Skip to content

Instantly share code, notes, and snippets.

@memononen
Created October 18, 2013 17:38
Show Gist options
  • Select an option

  • Save memononen/7045124 to your computer and use it in GitHub Desktop.

Select an option

Save memononen/7045124 to your computer and use it in GitHub Desktop.
Disposable mode UI: combining flexbox for layout, templates for scopes and IMGUI for logic.
* Material dialog we're creating
+----------------------+
| Materials |
| __________________ |
|?(__________________)x|
|----------------------|
| /¨\ #|
|( ) Gold #|
| \_/ #|
|.....................#|
| /¨\ #|
|( ) Matte Plastic #|
| \_/ |
|......................|
| /¨\ |
|( ) Glass |
|----------------------|
| (+Add) (Remove) |
+----------------------+
* UI Definition templates
material-window.txt:
<win width="300px" height="500px" resizable="1" dir="vert" align="justify">
<header>Materials</header>
<box height="auto" dir="horiz">
<icon img="looking-glass" /><input name="search" grow="1" /><icon name="clear" img="clear" />
</box>
<scroll name="materials" grow="1" dir="vert" align="justify">
</scroll>
<box height="auto" dir="horiz">
<box grow="1" />
<button name="add-item"><icon img="plus"/>Add</button>
<button name="remove">Remove</button>
</box>
</win>
material-item.txt:
<item dir="horiz" padding="4px" align="center">
<img name="thumbnail" width="25px" height="25px" />
<label name="name" grow="1" />
</item>
material-noitems.txt:
<item name="item" dir="horiz" padding="4px" pack="center" align="center">
<icon img="sad-face" /><label>Sorry, no items found.</label>
</item>
* IMGUI code to handle the dialog
Layout* win = parseLayout("material-window.txt");
Layout* item = parseLayout("material-item.txt");
Layout* noitems = parseLayout("material-noitems.txt");
win->begin(&m_windowPosition, &m_windowSize);
// Search box
if (win->inputText("search", m_searchString)) {
if (searchString == "")
items = m_materials;
else
items = searchItems(m_materials, searchString);
m_selectedMaterial = -1;
}
if (win->button("clear")) {
m_searchString = "";
items = m_materials;
}
// Material list
win->get("materials")->begin();
if (items.length == 0) {
// Show no-items prompt.
noitems->show(); //practically same as begin()/end()
} else {
for (int i = 0; i < items.length; i++) {
item->begin();
if (item->item("item", m_selectedMaterial == i))
m_selectedMaterial = i;
item->image("thumbnail", items[i].image);
item->image("name", items[i].name);
item->end();
}
}
win->get("materials")->end();
// Footer
if (win->button("add"))
m_materials.add();
m_selectedItem = m_materials.length() - 1;
if (win->button("remove")) {
if (selectedItem != -1)
m_materials.remove(m_selectedItem);
}
win->end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment