Ejemplo n.º 1
0
 def is_mouse_in_window(self) -> bool:
     if not ig.global_mouse_capture():
         return False
     window_x, window_y = cast(Iterable[float], ig.get_window_position())
     window_w, window_h = cast(Iterable[float], ig.get_window_size())
     return self.mouse_pos[0] >= window_x and self.mouse_pos[0] < window_x + window_w and \
       self.mouse_pos[1] >= window_y and self.mouse_pos[1] < window_y + window_h
Ejemplo n.º 2
0
def get_viewport(framebuffer_size: Tuple[int, int]) -> core.Viewport:
    window_pos = tuple(map(int, ig.get_window_position()))
    window_size = tuple(map(int, ig.get_window_size()))

    viewport = core.Viewport()
    viewport.x = window_pos[0]
    viewport.y = window_pos[1]
    viewport.width = window_size[0]
    viewport.height = window_size[1]

    return viewport
Ejemplo n.º 3
0
def get_mouse_world_pos_birds_eye(
        camera: core.BirdsEyeCamera) -> Optional[Tuple[float, float]]:
    window_size = tuple(map(int, ig.get_window_size()))
    mouse_pos = get_normalized_mouse_pos()
    if mouse_pos is None:
        return None

    world_span_x = camera.span_y
    world_span_z = camera.span_y * window_size[0] / window_size[1]
    return (
        camera.pos[0] + mouse_pos[1] * world_span_x / 2,
        camera.pos[2] + mouse_pos[0] * world_span_z / 2,
    )
Ejemplo n.º 4
0
def get_normalized_mouse_pos() -> Optional[Tuple[float, float]]:
    if not ig.global_mouse_capture():
        return None
    window_pos = tuple(map(int, ig.get_window_position()))
    window_size = tuple(map(int, ig.get_window_size()))
    mouse_pos = tuple(map(float, ig.get_mouse_pos()))
    mouse_pos = (
        mouse_pos[0] - window_pos[0],
        window_size[1] - mouse_pos[1] + window_pos[1],
    )
    mouse_pos = (
        2 * mouse_pos[0] / window_size[0] - 1,
        2 * mouse_pos[1] / window_size[1] - 1,
    )
    if any(c < -1 or c > 1 for c in mouse_pos):
        return None
    return mouse_pos
Ejemplo n.º 5
0
def get_mouse_ray(camera: core.RotateCamera) -> Optional[Tuple[Vec3f, Vec3f]]:
    window_size = tuple(map(int, ig.get_window_size()))
    mouse_pos = get_normalized_mouse_pos()
    if mouse_pos is None:
        return None

    forward_dir = angle_to_direction(camera.pitch, camera.yaw)
    up_dir = angle_to_direction(camera.pitch + math.pi / 2, camera.yaw)
    right_dir = angle_to_direction(0, camera.yaw - math.pi / 2)

    top = math.tan(camera.fov_y / 2)
    right = top * window_size[0] / window_size[1]

    mouse_dir = tuple(forward_dir[i] + top * mouse_pos[1] * up_dir[i] +
                      right * mouse_pos[0] * right_dir[i] for i in range(3))
    mag = math.sqrt(sum(c**2 for c in mouse_dir))
    mouse_dir = (mouse_dir[0] / mag, mouse_dir[1] / mag, mouse_dir[2] / mag)

    return (camera.pos, mouse_dir)
Ejemplo n.º 6
0
    def get_drag_amount(self) -> Tuple[float, float]:
        mouse_was_down = self.mouse_down
        last_mouse_pos = self.mouse_pos
        self.mouse_down = ig.is_mouse_down() and ig.global_mouse_capture()
        self.mouse_pos = ig.get_mouse_pos()

        if self.dragging:
            if not self.mouse_down:
                self.dragging = False
            return (
                self.mouse_pos[0] - last_mouse_pos[0],
                self.mouse_pos[1] - last_mouse_pos[1],
            )

        elif not mouse_was_down and self.mouse_down and not ig.is_any_item_hovered(
        ):
            window_x, window_y = ig.get_window_position()
            window_w, window_h = ig.get_window_size()
            if self.mouse_pos[0] >= window_x and self.mouse_pos[0] < window_x + window_w and \
                self.mouse_pos[1] >= window_y and self.mouse_pos[1] < window_y + window_h:
                self.dragging = True

        return (0, 0)
Ejemplo n.º 7
0
  def render(self) -> None:
    ig.push_id(str(epoch))

    # if ig.is_key_pressed(ord('`')):
    #   self.show_debug_pane = not self.show_debug_pane

    self.handle_controller()

    prev_frame_time = use_state_with('prev-frame-time', time.time)
    accum_time = use_state('accum-time', 0.0)
    now = time.time()
    accum_time.value += now - prev_frame_time.value
    prev_frame_time.value = now

    play_speed = self.model.play_speed
    if play_speed == 0.0:
      accum_time.value = 0
    else:
      target_fps = 30 * abs(play_speed)
      target_dt = 1 / target_fps
      updates = 0
      while accum_time.value >= target_dt and updates < 20:
        accum_time.value -= target_dt
        self.model.selected_frame += 1 if play_speed > 0 else -1
        self.handle_controller()
        updates += 1

    ig_window_size = ig.get_window_size()
    window_size = (int(ig_window_size.x), int(ig_window_size.y))

    ig.columns(2)
    self.render_left_column(window_size)
    ig.next_column()
    self.render_right_column()
    ig.columns(1)

    ig.pop_id()
Ejemplo n.º 8
0
def render_tabs(
    id: str,
    tabs: List[TabInfo],
    open_tab_index: Optional[int] = None,
    allow_windowing=False,
) -> Tuple[Optional[int], Optional[int]]:
    ig.push_id(id)
    root_id = get_local_state_id_stack()
    ig.columns(2)

    closed_tab = None

    rendered = use_state('rendered', False)
    if not rendered.value:
        rendered.value = True
        ig.set_column_width(-1, 120)

    if len(tabs) == 0:
        ig.pop_id()
        return None, closed_tab

    selected_tab_index = use_state_with('selected-tab-index',
                                        lambda: open_tab_index or 0)
    selected_tab_id = use_state_with('selected-tab',
                                     lambda: tabs[selected_tab_index.value].id)

    if open_tab_index is not None:
        selected_tab_index.value = open_tab_index
        selected_tab_id.value = tabs[open_tab_index].id

    windowed_tabs = use_state('windowed-tabs', cast(Set[str], set())).value

    # TODO: Change selected tab if windowed

    # Handle deletion/insertion
    if selected_tab_index.value >= len(tabs):
        selected_tab_index.value = len(tabs) - 1
    if tabs[selected_tab_index.value].id != selected_tab_id.value:
        matching_indices = [
            i for i in range(len(tabs)) if tabs[i].id == selected_tab_id.value
        ]
        if len(matching_indices) > 0:
            selected_tab_index.value = matching_indices[0]
        else:
            selected_tab_id.value = tabs[selected_tab_index.value].id

    ig.begin_child('tabs')
    for i, tab in enumerate(tabs):
        if tab.id in windowed_tabs:
            continue

        _, selected = ig.selectable(
            tab.label + '##tab-' + tab.id,
            selected_tab_id.value == tab.id,
        )
        if selected:
            selected_tab_index.value = i
            selected_tab_id.value = tab.id

        if tab.closable and ig.is_item_hovered() and ig.is_mouse_clicked(2):
            closed_tab = i

        if allow_windowing or tab.closable:
            if ig.begin_popup_context_item(f'##ctx-{tab.id}'):
                if allow_windowing and ig.selectable('Pop out')[0]:
                    windowed_tabs.add(tab.id)
                if tab.closable and ig.selectable('Close')[0]:
                    closed_tab = i
                ig.end_popup_context_item()

    ig.end_child()

    ig.next_column()

    ig.begin_child('content', flags=ig.WINDOW_HORIZONTAL_SCROLLING_BAR)
    tab = tabs[selected_tab_index.value]
    if tab.id not in windowed_tabs:
        push_local_state_rebase(('rebase-tabs', ) + root_id)
        tab.render(tab.id)  # type: ignore
        pop_local_state_rebase()
    ig.end_child()

    ig.columns(1)

    for tab_id in set(windowed_tabs):
        matching = [tab for tab in tabs if tab.id == tab_id]
        if len(matching) == 0:
            windowed_tabs.remove(tab.id)
            continue
        tab = matching[0]

        ig.set_next_window_size(*ig.get_window_size(), ig.ONCE)
        ig.set_next_window_position(*ig.get_window_position(), ig.ONCE)

        ig.push_style_color(ig.COLOR_WINDOW_BACKGROUND, 0.06, 0.06, 0.06, 0.94)
        _, opened = ig.begin(
            tab.label + '##window-' + tab.id,
            closable=True,
            flags=ig.WINDOW_HORIZONTAL_SCROLLING_BAR,
        )
        push_local_state_rebase(('rebase-tabs', ) + root_id)
        tab.render(tab.id)  # type: ignore
        pop_local_state_rebase()
        ig.end()
        ig.pop_style_color()

        if not opened:
            windowed_tabs.remove(tab.id)

    ig.pop_id()
    return (
        None if open_tab_index == selected_tab_index.value else
        selected_tab_index.value,
        closed_tab,
    )