Skip to content

Commit

Permalink
Merge 55949a3 into 04dd780
Browse files Browse the repository at this point in the history
  • Loading branch information
swistakm committed May 19, 2019
2 parents 04dd780 + 55949a3 commit 873de59
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 3 deletions.
95 changes: 95 additions & 0 deletions doc/examples/plots.py
@@ -0,0 +1,95 @@
from array import array
from math import sin, pi
from random import random

import glfw
import OpenGL.GL as gl
import imgui
from imgui.integrations.glfw import GlfwRenderer
from time import time


C = .01
L = int(pi * 2 * 100)


def main():
window = impl_glfw_init()
imgui.create_context()
impl = GlfwRenderer(window)

plot_values = array('f', [sin(x * C) for x in range(L)])
histogram_values = array('f', [random() for _ in range(20)])

while not glfw.window_should_close(window):
glfw.poll_events()
impl.process_inputs()

imgui.new_frame()

imgui.begin("Plot example")
imgui.plot_lines(
"Sin(t)",
plot_values,
overlay_text="SIN() over time",
# offset by one item every milisecond, plot values
# buffer its end wraps around
values_offset=int(time() * 100) % L,
# 0=autoscale => (0, 50) = (autoscale width, 50px height)
graph_size=(0, 50),
)

imgui.plot_histogram(
"histogram(random())",
histogram_values,
overlay_text="random histogram",
# offset by one item every milisecond, plot values
# buffer its end wraps around
graph_size=(0, 50),
)


imgui.end()

gl.glClearColor(1., 1., 1., 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

imgui.render()
impl.render(imgui.get_draw_data())
glfw.swap_buffers(window)

impl.shutdown()
glfw.terminate()


def impl_glfw_init():
width, height = 1280, 720
window_name = "minimal ImGui/GLFW3 example"

if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)

# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

# Create a windowed mode window and its OpenGL context
window = glfw.create_window(
int(width), int(height), window_name, None, None
)
glfw.make_context_current(window)

if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)

return window


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion doc/source/gen_example.py
Expand Up @@ -181,7 +181,6 @@ def render_snippet(
pivot_x=0.5,
pivot_y=0.5
)

exec(frame_code, locals(), globals())

gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, offscreen_fb)
Expand Down
3 changes: 3 additions & 0 deletions doc/source/visual_examples/imgui.core.plot_histogram_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions doc/source/visual_examples/imgui.core.plot_lines_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion imgui/cimgui.pxd
Expand Up @@ -645,7 +645,7 @@ cdef extern from "imgui.h" namespace "ImGui":
int stride # = sizeof(float))
) except +

void PlotHistogram( #
void PlotHistogram( #
const char* label, const float* values, int values_count,
# note: optional
int values_offset, const char* overlay_text, float scale_min,
Expand Down
112 changes: 111 additions & 1 deletion imgui/core.pyx
Expand Up @@ -5224,7 +5224,7 @@ def plot_lines(
Args:
label (str): A plot label that will be displayed on the plot's right
side. If you want the label to be visible, add :code:`"##"`
side. If you want the label to be invisible, add :code:`"##"`
before the label's text: :code:`"my_label" -> "##my_label"`
values (array of floats): the y-values.
Expand All @@ -5250,6 +5250,23 @@ def plot_lines(
entire array.
* **stride** (*int*): Number of bytes to move to read next element.
.. visual-example::
:auto_layout:
:width: 400
:height: 130
from array import array
from math import sin
plot_values = array('f')
for x in range(100):
plot_values.append(sin(x * 0.1))
imgui.begin("Plot example")
imgui.plot_lines("Sin(t)", plot_values)
imgui.end()
.. wraps::
void PlotLines(
const char* label, const float* values, int values_count,
Expand Down Expand Up @@ -5284,6 +5301,99 @@ def plot_lines(
)


def plot_histogram(
str label not None,
const float[:] values not None,
int values_count = -1,
int values_offset = 0,
str overlay_text = None,
float scale_min = FLT_MAX,
float scale_max = FLT_MAX,
graph_size = (0, 0),
int stride = sizeof(float),
):
"""
Plot a histogram of float values.
Args:
label (str): A plot label that will be displayed on the plot's right
side. If you want the label to be invisible, add :code:`"##"`
before the label's text: :code:`"my_label" -> "##my_label"`
values (array of floats): the y-values.
It must be a type that supports Cython's Memoryviews,
(See: http://docs.cython.org/en/latest/src/userguide/memoryviews.html)
for example a numpy array.
overlay_text (str or None, optional): Overlay text.
scale_min (float, optional): y-value at the bottom of the plot.
scale_max (float, optional): y-value at the top of the plot.
graph_size (tuple of two floats, optional): plot size in pixels.
**Note:** In ImGui 1.49, (-1,-1) will NOT auto-size the plot.
To do that, use :func:`get_content_region_available` and pass
in the right size.
**Note:** These low-level parameters are exposed if needed for
performance:
* **values_offset** (*int*): Index of first element to display
* **values_count** (*int*): Number of values to display. -1 will use the
entire array.
* **stride** (*int*): Number of bytes to move to read next element.
.. visual-example::
:auto_layout:
:width: 400
:height: 130
from array import array
from random import random
histogram_values = array('f')
for _ in range(20):
histogram_values.append(random())
imgui.begin("Plot example")
imgui.plot_histogram("histogram(random())", histogram_values)
imgui.end()
.. wraps::
void PlotHistogram(
const char* label, const float* values, int values_count,
# note: optional
int values_offset,
const char* overlay_text,
float scale_min,
float scale_max,
ImVec2 graph_size,
int stride
)
"""
if values_count == -1:
values_count = values.shape[0]

# Would be nicer as something like
# _bytes(overlay_text) if overlay_text is not None else NULL
# but then Cython complains about either types or pointers to temporary references.
cdef const char* overlay_text_ptr = NULL
cdef bytes overlay_text_b

if overlay_text is not None:
overlay_text_b = _bytes(overlay_text) # must be assigned to a variable
overlay_text_ptr = overlay_text_b # auto-convert bytes to char*

cimgui.PlotHistogram(
_bytes(label), &values[0], values_count,
values_offset,
overlay_text_ptr,
scale_min, scale_max,
_cast_tuple_ImVec2(graph_size),
stride
)


def set_item_default_focus():
"""Make last item the default focused item of a window.
Please use instead of "if (is_window_appearing()) set_scroll_here()" to signify "default item".
Expand Down

0 comments on commit 873de59

Please sign in to comment.