def get_window_rect() -> IMGui[Rect]: """ To be used only in imgui code """ window_top_left = im.get_window_position() width, height = im.get_window_size() window_bottom_right = Vec2(window_top_left.x + width, window_top_left.y + height) return Rect(window_top_left, window_bottom_right)
def get_window_content_rect() -> IMGui[Rect]: TITLE_BAR_HEIGHT = 20 window_top_left = im.get_window_position() content_top_left = Vec2(window_top_left.x, window_top_left.y + TITLE_BAR_HEIGHT) width, height = im.get_window_size() window_bottom_right = Vec2(window_top_left.x + width, window_top_left.y + height) return Rect(content_top_left, window_bottom_right)
def begin_output(self, pin): x, y = imgui.get_cursor_screen_pos() wx, wy = imgui.get_window_position() ww, wh = imgui.get_window_size() x = wx + ww + 8 pos = (x, y) pin.set_position(pos) return pos
def zoomtip(imtex, imdim, mag=1.0): if imgui.is_item_hovered(): w, h = imgui.get_window_size() h = imdim[0] * w / imdim[1] rectmin = imgui.get_item_rect_min() mousepos = imgui.get_mouse_pos() u = float(mousepos[0] - rectmin[0]) / w v = float(mousepos[1] - rectmin[1]) / h imgui.begin_tooltip() tw = 32. / imdim[1] / mag th = 32. / imdim[0] / mag imgui.image(imtex, 64, 64, uv0=(u - tw, v - th), uv1=(u + tw, v + th)) dl = imgui.get_window_draw_list() rm = imgui.get_item_rect_min() col = imgui.get_color_u32_rgba(1, 1, 0, 1) dl.add_line(rm[0], rm[1] + 32, rm[0] + 64, rm[1] + 32, col) dl.add_line(rm[0] + 32, rm[1], rm[0] + 32, rm[1] + 64, col) imgui.end()
def Vplan(self): w, h = imgui.get_window_size() h = self.V.shape[1] * w / self.V.shape[2] _, self.lookstep = imgui.slider_float("lookahead step", self.lookstep, 0, 1, power=1.5) imgui.image(self.Vtex[int(48 * self.car.theta / np.pi % 96)], w, h) shots = [] c = self.car bestk = None for i in range(50): k, v, c = self.bestkv(c, self.lookstep) if bestk is None: bestk = k shots.append([c.x, c.y]) self.shots = np.array([shots]) return bestk
def draw_log_detail_info_panel(self, idx, log): if idx != self.detail_idx: return popup_id = "detail_log_%s" % self.detail_idx win_size = imgui.get_window_size() item_size = (win_size.x * 0.94, win_size.y * 0.5) imgui.set_next_window_size(item_size[0], item_size[1]) imgui.set_next_window_position((win_size.x - item_size[0]) * 0.5, (win_size.y - item_size[1]) * 0.5) if imgui.begin_popup(popup_id): msg = log.detail_info utils.get_win_size() btn_height = 22 padding = imgui.get_style().window_padding area_size = (item_size[0] * 0.98, item_size[1] * 0.94 - btn_height - padding.y * 0.6) imgui.input_text_multiline("##hidden", msg, len(msg) + 1, area_size[0], area_size[1], imgui.INPUT_TEXT_READ_ONLY) if imgui.button("复制", area_size[0], btn_height): print("复制") imgui.end_popup()
def render_plugins_ui(window): "Draw UI windows for all plugins active for the current drawing." if not window.drawing: return drawing = window.drawing deactivated = set() for name, args in window.drawing.active_plugins.items(): plugin, sig = window.plugins[name] _, opened = imgui.begin(f"{ name } ##{ drawing.path or drawing.uuid }", True) if not opened: deactivated.add(name) imgui.columns(2) for param_name, param_sig in islice(sig.items(), 4, None): if param_sig.annotation == inspect._empty: continue imgui.text(param_name) imgui.next_column() default_value = args.get(param_name) if default_value is not None: value = default_value else: value = param_sig.default label = f"##{param_name}_val" if param_sig.annotation == int: changed, args[param_name] = imgui.drag_int(label, value) elif param_sig.annotation == float: changed, args[param_name] = imgui.drag_float(label, value) elif param_sig.annotation == str: changed, args[param_name] = imgui.input_text(label, value, 20) elif param_sig.annotation == bool: changed, args[param_name] = imgui.checkbox(label, value) imgui.next_column() imgui.columns(1) texture_and_size = getattr(plugin, "texture", None) if texture_and_size: texture, size = texture_and_size w, h = size ww, wh = imgui.get_window_size() scale = max(1, (ww - 10) // w) imgui.image(texture.name, w * scale, h * scale, border_color=(1, 1, 1, 1)) if hasattr(plugin, "ui"): result = plugin.ui(oldpaint, imgui, window.drawing, window.brush, **args) if result: args.update(result) last_run = getattr(plugin, "last_run", 0) period = getattr(plugin, "period", None) t = time() # TODO I've seen more readable if-statements in my days... if callable(plugin) and ((period and t > last_run + period) or (not period and imgui.button("Execute"))): plugin.last_run = t try: result = plugin(oldpaint, imgui, window.drawing, window.brush, **args) if result: args.update(result) except Exception: # We don't want crappy plugins to ruin everything # Still probably probably possible to crash opengl though... logger.error(f"Plugin {name}: {format_exc()}") imgui.button("Help") if imgui.begin_popup_context_item("Help", mouse_button=0): if plugin.__doc__: imgui.text(inspect.cleandoc(plugin.__doc__)) else: imgui.text("No documentation available.") imgui.end_popup() imgui.end() for name in deactivated: window.drawing.active_plugins.pop(name, None)
def _max_image_size(self): win_size = imgui.get_window_size() max_image_size = imgui.Vec2(win_size.x - (self.listbox_width + 40), win_size.y - 150) return max_image_size
def render(self): videowindow, self.open = imgui.begin("Video window {}".format( self.filename), self.open, flags=imgui.WINDOW_NO_SCROLLBAR) w, h = imgui.get_window_size() if imgui.APPEARING: imgui.set_window_size(400, 300) w, h = imgui.core.get_content_region_available() w = int(max(w, 0)) h = int(max(h - 85, 0)) if not self.open: imgui.end() self.terminate() return if self.ctx.update() and w > 0 and h > 0: gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self.fbo) gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, w, h, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, None) gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) self.ctx.render(flip_y=False, opengl_fbo={ 'w': w, 'h': h, 'fbo': self.fbo }) try: imgui.text("Filename: {} fbo: {} tex: {}".format( self.mpv.filename, self.fbo, self.texture)) except: imgui.text("Filename: {} fbo: {} tex: {}".format( self.mpv.filename, self.fbo, self.texture)) try: imgui.text("{0:.2f}s/{1:.2f}s ({2:.2f}s remaining)".format( self.mpv.time_pos, self.mpv.duration, self.mpv.playtime_remaining)) except: imgui.text("Loading...") imgui.image(self.texture, w, h) imgui.push_item_width(-1) changed, values = imgui.slider_float("##Playback Percentage", *self.playbackPos, min_value=0.0, max_value=100.0, format="Playback Percentage %.0f", power=1.0) if changed and values: try: self.mpv.command('seek', values, 'absolute-percent') except: pass self.playbackPos = (values, ) elif self.mpv.percent_pos: self.playbackPos = (self.mpv.percent_pos, ) changed, values = imgui.slider_float("##Volume", *self.volume, min_value=0.0, max_value=100.0, format="Volume %.0f", power=1.0) if changed: self.mpv.volume = values self.volume = (values, ) elif self.mpv.volume: self.volume = (self.mpv.volume, ) imgui.end()
def render_editor(self): if imgui.button("optimize track"): self.start_optimization() imgui.same_line() _, ks = imgui.slider_float2("kcurv, kdist", self.kcurv, self.kdist, min_value=0, max_value=100.0, power=2) self.kcurv, self.kdist = ks _, self.lanewidth = imgui.slider_float("lane width", self.lanewidth, min_value=10, max_value=1000, power=1.5) # use a child region just to keep appearing/disappearing widgets at the # top from shifting stuff around imgui.begin_child("region", 0, 25, border=False) if (self.editmode == 'turn' and self.selectedpt is not None and self.selectedpt < len(self.pts['turn'])): i = self.selectedpt T = self.pts['turn'] _, T[i][2] = imgui.slider_float("turn %d radius" % (i + 1), T[i][2], min_value=-1000., max_value=1000., power=1.2) if self.editmode == 'home': _, self.homeangle = imgui.slider_float("home position angle", self.homeangle, -3.141, 3.141) imgui.end_child() w, h = imgui.get_window_size() h = self.mapim.shape[0] * w / self.mapim.shape[1] imgui.image_button(self.maptex, w, h, frame_padding=0) rectmin = imgui.get_item_rect_min() if imgui.is_item_clicked(0): mxy = imgui.get_mouse_pos() u = (mxy[0] - rectmin[0]) * self.mapim.shape[1] / w v = (mxy[1] - rectmin[1]) * self.mapim.shape[1] / w if imgui.is_mouse_double_clicked(0): self.add_point(u, v) else: self.select_point(u, v) if (imgui.is_item_hovered() and self.selectedpt is not None and imgui.is_mouse_dragging(0)): mxy = imgui.get_mouse_pos() u = (mxy[0] - rectmin[0]) * self.mapim.shape[1] / w v = (mxy[1] - rectmin[1]) * self.mapim.shape[1] / w self.move_point(u, v) scale = w / self.mapim.shape[1] dl = imgui.get_window_draw_list() self.render_ptlist(dl, self.pts['cone'], rectmin, scale, imgui.get_color_u32_rgba(1, 0.7, 0, 1), 4, self.editmode == 'cone') self.render_ptlist(dl, self.pts['turn'], rectmin, scale, imgui.get_color_u32_rgba(0, 0.3, 1, 1), 3, self.editmode == 'turn') self.render_ptlist(dl, self.pts['home'], rectmin, scale, imgui.get_color_u32_rgba(1, 1, 1, 1), 10, self.editmode == 'home') # render home position angle if len(self.pts['home']) > 0: h = self.pts['home'][0] S = 100 * np.sin(self.homeangle) C = 100 * np.cos(self.homeangle) dl.add_line(rectmin[0] + h[0] * scale, rectmin[1] + h[1] * scale, rectmin[0] + h[0] * scale + C, rectmin[1] + h[1] * scale - S, imgui.get_color_u32_rgba(1, 1, 1, 1)) self.render_turns(scale, rectmin) self.render_opttrack(scale, rectmin) if self.editmode == "cone": zoomtip(self.maptex, self.mapim.shape, 2)
def render_plugins_ui(drawing): "Draw UI windows for all plugins active for the current drawing." # TODO there's an imgui related crash here somewhere preventing (at least) the # voxel plugin from being used in more than one drawing. For now: avoid that. if not drawing: return deactivated = set() for name, (plugin, sig, args) in drawing.plugins.items(): _, opened = imgui.begin(f"{name} {id(drawing)}", True) if not opened: deactivated.add(name) imgui.end() continue imgui.columns(2) for param_name, param_sig in islice(sig.items(), 2, None): imgui.text(param_name) imgui.next_column() default_value = args.get(param_name) if default_value is not None: value = default_value else: value = param_sig.default label = f"##{param_name}_val" if param_sig.annotation == int: changed, args[param_name] = imgui.drag_int(label, value) elif param_sig.annotation == float: changed, args[param_name] = imgui.drag_float(label, value) elif param_sig.annotation == str: changed, args[param_name] = imgui.input_text(label, value, 20) elif param_sig.annotation == bool: changed, args[param_name] = imgui.checkbox(label, value) imgui.next_column() imgui.columns(1) texture_and_size = getattr(plugin, "texture", None) if texture_and_size: texture, size = texture_and_size w, h = size ww, wh = imgui.get_window_size() scale = max(1, (ww - 10) // w) imgui.image(texture.name, w * scale, h * scale, border_color=(1, 1, 1, 1)) last_run = getattr(plugin, "last_run", 0) period = getattr(plugin, "period", None) t = time() if period and t > last_run + period or imgui.button("Execute"): plugin.last_run = last_run try: result = plugin(voxpaint, drawing, **args) if result: args.update(result) except Exception: print_exc() imgui.button("Help") if imgui.begin_popup_context_item("Help", mouse_button=0): if plugin.__doc__: imgui.text(inspect.cleandoc(plugin.__doc__)) else: imgui.text("No documentation available.") imgui.end_popup() imgui.end() for name in deactivated: drawing.plugins.pop(name, None)