Ejemplo n.º 1
0
class WaitingSymbol(TQGraphicsNodePath):
    """ An animation that runs as long as done_function returns False. """
    def __init__(self, done_function, position, size=0.2, frequency=1):
        """
        Args:
            done_function: function which controls when the waiting symbol
                           animation should be stopped (when True)
            position: Vec3 of the upper left hand corner in p3d coordinates """

        # -- logical
        self.done_function = done_function

        self.position = position
        self.size = size
        self.frequency = frequency
        self.duration = 1.

        self.a = None

        TQGraphicsNodePath.__init__(self)

        # -- supporting graphics
        self.quad = Quad(thickness=3.0,
                         TQGraphicsNodePath_creation_parent_node=engine.
                         tq_graphics_basics.tq_aspect2d)
        self.quad.reparentTo(self)

        self.quad.set_pos_vec3(self.position)
        self.quad.set_height(self.size)
        self.quad.set_width(self.size)

        self.line = Line1dSolid(thickness=3.0,
                                TQGraphicsNodePath_creation_parent_node=engine.
                                tq_graphics_basics.tq_aspect2d)
        self.line.reparentTo(self)

        self.anim_seq = Sequence()

        self.anim_seq.set_sequence_params(
            duration=self.duration,
            extraArgs=[],
            update_function=self.update,
            on_finish_function=self.restart_animation)

        self.anim_seq.start()

    def restart_animation(self):
        self.anim_seq.set_t(0.)
        self.anim_seq.resume()

    def _get_center(self):
        return Vec3(self.position[0] + 0.5 * self.size, 0.,
                    self.position[2] - 0.5 * self.size)

    def set_position(self, vec3_pos):
        self.position = vec3_pos
        self.update(self.a)

    def update(self, a):
        """ a is in between 0 and 1 """

        self.a = a

        if self.done_function() == True:
            self.remove()
            return
        elif self.done_function() != False:
            raise ValueError("self.done_function() should return Boolean")

        t = a * self.duration
        omega = 2 * np.pi * self.frequency
        theta = omega * t

        r = self.size * 0.4
        p1 = self._get_center()
        p2 = p1 + Vec3(r * np.cos(theta), 0., r * np.sin(theta))

        self.line.setTailPoint(p1)
        self.line.setTipPoint(p2)

        self.quad.set_pos_vec3(self.position)

    def remove(self):
        """ release all resources """
        self.anim_seq.remove()
        self.line.remove()
        self.quad.remove()
Ejemplo n.º 2
0
class ProcessingBox(TQGraphicsNodePath):
    """ an individual box with text, position, loading symbol, elapsed time since start e"""

    def __init__(self, is_done_function, description):
        self.description = description

        self.x_pos_left_border = None
        self.y_pos_top_box = None

        self.is_done_function = is_done_function

        self.time_initial = time.time_ns()

        self.task_obj_update = None

        self.waiting_symbol = None

        self.text = None

        self.elapsed_time_text = None

        self.quad = None

        self.task_obj_update = None

        self.task_obj_update = taskMgr.add(self.update_task, 'update_task')

        self.height = None

        self.width = None

        if self.height is None:
            self.height = 0.2

        if self.width is None:
            self.width = 0.9

        TQGraphicsNodePath.__init__(self)

        pass

    def set_xy_pos(self, x_pos_left_border, y_pos_top_box):
        """ """
        self.x_pos_left_border = x_pos_left_border
        self.y_pos_top_box = y_pos_top_box


    def update(self):
        if self.is_done_function() == False:
            if self.x_pos_left_border is None:
                self.x_pos_left_border = -1.0

            x_pos_waiting_symbol = self.x_pos_left_border + 0.1

            x_pos_text = x_pos_waiting_symbol + 0.2

            if self.y_pos_top_box is None:
                self.y_pos_top_box = 0.

            y_pos_waiting_symbol = self.y_pos_top_box - 0.05

            y_pos_text = self.y_pos_top_box - 0.125

            x_pos_elapsed_time = x_pos_text + 0.4
            y_pos_elapsed_time = y_pos_text

            quad_thickness = 2.0

            if self.waiting_symbol is None:
                self.waiting_symbol = WaitingSymbol(self.is_done_function, Vec3(x_pos_waiting_symbol, 0., y_pos_waiting_symbol), size=0.1)
                self.waiting_symbol.reparentTo(self)
            else:
                self.waiting_symbol.set_position(Vec3(x_pos_waiting_symbol, 0., y_pos_waiting_symbol))

            if self.text is None:
                self.text = Fixed2dLabel(text=self.description, font="fonts/arial.egg", x=x_pos_text, y=y_pos_text)
                self.text.reparentTo(self)
            else:
                self.text.setPos(x_pos_text, y_pos_text)

            if self.elapsed_time_text is None:
                self.elapsed_time_text = Fixed2dLabel(text="elapsed time", font="fonts/arial.egg", x=x_pos_elapsed_time, y=y_pos_elapsed_time)
                self.elapsed_time_text.reparentTo(self)
            else:
                self.elapsed_time_text.setPos(x_pos_elapsed_time, y_pos_elapsed_time)

            elapsed_time = (time.time_ns() - self.time_initial) / 1.0e9
            self.elapsed_time_text.setText("{0:.0f} s".format(elapsed_time))

            if self.quad is None:
                self.quad = Quad(thickness=quad_thickness, TQGraphicsNodePath_creation_parent_node=engine.tq_graphics_basics.tq_aspect2d)
                self.quad.reparentTo(self)

                self.quad.set_pos_vec3(Vec3(self.x_pos_left_border, 0., self.y_pos_top_box))

                self.quad.set_height(self.height)
                self.quad.set_width(self.width)
            else:
                self.quad.set_pos_vec3(Vec3(self.x_pos_left_border, 0., self.y_pos_top_box))

            return True  # call task.cont

        return False  # call task.done

    def update_task(self, task):
        """ This updates it's position, checks if the task is done, and if it is, removes the Processing Box """

        ret = self.update()
        if ret == True:
            return task.cont

        self.remove()
        return task.done

    def _is_done(self):
        """ check the is_done_function """
        return self.is_done_function()

    def get_height(self):
        return self.height

    def get_width(self):
        return self.width

    def remove(self):
        taskMgr.remove(self.task_obj_update)

        if self.waiting_symbol:
            self.waiting_symbol.remove()

        if self.text:
            self.text.remove()

        if self.elapsed_time_text:
            self.elapsed_time_text.remove()

        if self.quad:
            self.quad.remove()