def _update_xaxis(self):
        """
        updates camera and modelview of the x axis
        """
        if self._render_axis[0]:
            self._xaxis.size = self.get_xaxis_size()
            self._xaxis.update_camera(self.camera)
            self._xaxis.modelview.set_position(self._plotplane_margin[3], self.plotframe_size[1]-self._axis_translation[0]+self._plotplane_margin[0])
            self._xaxis.update_modelview()
            labels      = self._xaxis.labels
            curr_length = len(self._axis_texts[0])
            margin      = self._plotplane_margin[3]

            for i in range(0, len(labels)-curr_length):
                x, label = labels[i+curr_length]
                text = self._fntrenderer.create_text(label,
                    size=self.color_scheme['axis-fontsize'],
                    position=(x+margin,0),
                    color=hex_to_rgba(self.color_scheme['axis-fontcolor']))
                self._axis_texts[0].append(text)
                self._fntlayout.add_text(text, ('bottom', self._plotplane_margin[2]-text.size-10))
                text.position = (text.position[0]-text.get_boxsize()[0]/2.0, text.position[1])

            for i in range(0, min(len(labels), curr_length)):
                text = self._axis_texts[0][i]
                x, label = labels[i]
                if label != text.chars:
                    text.chars = label
                text.position = (x+margin-text.get_boxsize()[0]/2.0, text.position[1])
                text.color = hex_to_rgba(self.color_scheme['axis-fontcolor'])
            # hide unwanted texts
            for i in range(len(labels), curr_length):
                self._axis_texts[0][i].color = [0,0,0,0]
    def init_graphs(self):
        """
        initializes the graphs if neccessary and
        updates graph matricies
        """
        colors = self.color_scheme['graph-colors']
        colors_length = len(colors)
        graph_color_index = 0
        initial_scaling = [
            self._plotframe.inner_camera.get_matrix()[0],
            self._plotframe.inner_camera.get_matrix()[5]
        ]
        initial_plane_scaling = [
            self._plotframe.camera.get_matrix()[0],
            self._plotframe.camera.get_matrix()[5]
        ]
        for graph in [g for g in self.graphs.values() if not g.initialized]:
            if hasattr(graph, 'color') and graph.color is None:
                graph.color = hex_to_rgba(colors[graph_color_index%colors_length])
                graph_color_index+=1

            graph.init()

        self._update_graph_matricies()
        self._graphs_initialized = True
    def init_labels(self):
        """
        initializes plot labels.
        """
        axis_space = list(self._plotplane_margin)

        if self._xlabel is not None:
            axis_space[2] += self.color_scheme['xlabel-boxheight']
            text = self._fntrenderer.create_text(self._xlabel,
                position=(0,0),
                size=self.color_scheme['xlabel-fontsize'],
                color=hex_to_rgba(self.color_scheme['font-color']),
                enable_simple_tex=True)

            self.xlabel_text = text

            self._fntlayout.add_text(text, 'x-center', ('bottom', 5))

        if self._ylabel is not None:
            text = self._fntrenderer.create_text(self._ylabel,
                position=(5,0),
                size=self.color_scheme['ylabel-fontsize'],
                rotation=np.pi/2,
                color=hex_to_rgba(self.color_scheme['font-color']),
                enable_simple_tex=True)
            axis_space[3] += self.color_scheme['ylabel-boxheight']

            self.ylabel_text = text

            self._fntlayout.add_text(text, 'y-center')

        if self._title is not None:
            text = self._fntrenderer.create_text(self._title,
                position=(0,5),
                size=self.color_scheme['title-fontsize'],
                color=hex_to_rgba(self.color_scheme['font-color']),
                enable_simple_tex=True)
            axis_space[0] += self.color_scheme['title-boxheight']

            self.title_text = text

            self._fntlayout.add_text(text, 'x-center')

        self._plotplane_margin = axis_space
        self._fntlayout.min_screensize = (
            self._plot_plane_min_size[0] + axis_space[1] + axis_space[3],
            self._plot_plane_min_size[1] + axis_space[0] + axis_space[2])
        def area_pending():
            self.state = self.STATE_SELECT_AREA_PENDING
            self._select_area_rectangle.color = hex_to_rgba(self.color_scheme['select-area-pending-bgcolor'])
            self._select_area = self._select_area[0:2] + list(self.cursor)

            # if user opens the box in the wrong direction
            # we need to fix the coordinates so that the upper
            # left corner stays in upper left corner and so
            # for the lower right corner...
            if self._select_area[0] > self._select_area[2]:
                tmp = self._select_area[2]
                self._select_area[2] = self._select_area[0]
                self._select_area[0] = tmp
            if self._select_area[1] > self._select_area[3]:
                tmp = self._select_area[3]
                self._select_area[3] = self._select_area[1]
                self._select_area[1] = tmp
 def pre_render(self):
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glClearColor(*hex_to_rgba(self.color_scheme['bgcolor']))
    def _update_yaxis(self):
        """
        updates camera and modelview of the y axis
        """
        if self._render_axis[1]:
            #translation = self._plotframe.inner_camera.get_position()[1]
            self._yaxis.size = self.get_yaxis_size()
            self._yaxis.capture_size = self.get_yaxis_size()

            self._yaxis.modelview.set_position(self._plotplane_margin[3]-self._axis_translation[1],self._plotplane_margin[0])
            self._yaxis.update_modelview()
            self._yaxis.update_camera(self.camera)
            labels      = self._yaxis.labels
            curr_length = len(self._axis_texts[1])
            margin      = self._plotplane_margin[0]
            active_length = 0
            for i in range(0, len(labels)-curr_length):
                y, label = labels[i+curr_length]
                text = self._fntrenderer.create_text(label,
                    size=self.color_scheme['axis-fontsize'],
                    position=(0,margin+y),
                    color=hex_to_rgba(self.color_scheme['axis-fontcolor']))
                self._axis_texts[1].append(text)
                # XXX how to do this better?
                boxsize = text.get_boxsize()
                self._yboxwidths.append(boxsize[0])
                text.position = (text.position[0], text.position[1]-boxsize[1]/2.0)
                active_length += 1
            for i in range(0, min(len(labels), curr_length)):
                text = self._axis_texts[1][i]
                y, label = labels[i]
                if label != text.chars:
                    text.chars = label
                boxsize = text.get_boxsize()
                self._yboxwidths[i] = boxsize[0]
                text.position = (text.position[0]-text.get_boxsize()[0]/2.0, y+margin-text.get_boxsize()[1]/2.0)
                text.color = hex_to_rgba(self.color_scheme['axis-fontcolor'])
                active_length += 1
            # hide unwanted texts
            for i in range(len(labels), curr_length):
                self._axis_texts[1][i].color = [0,0,0,0]
                active_length -= 1
                self._yboxwidths[i] = 0

            maxboxwidth = max(self.min_label_space[1], np.max(self._yboxwidths))
            old =  self._plotplane_margin[3]
            if self._ylabel is not None:
                maxboxwidth += self.color_scheme['ylabel-fontsize']+10

            self._plotplane_margin[3] = max(self.min_label_space[1], int(maxboxwidth)+5)
            if  self._plotplane_margin[3] != old:
                self._margin_changed = True

            for i in range(len(self._yboxwidths)):
                self._axis_texts[1][i].position = (
                    maxboxwidth - self._axis_texts[1][i].get_boxsize()[0]+5,
                    self._axis_texts[1][i].position[1]
                )
            if self._margin_changed:
                self._yaxis.modelview.set_position(self._plotplane_margin[3]-self._axis_translation[1],self._plotplane_margin[0])
                self._yaxis.update_modelview()
                self._yaxis.update_camera(self.camera)
                self._update_xaxis()
                self._plotplane.position = self.plotframe_position
    def init_colorlegend(self):
        """
        initializes color legend.
        XXX
        - Refactor me as a kind of widget or so ...
        """
        if self.colorlegend is not None:
            self._colorlegend_frame = window.Framebuffer(
                self.camera,
                screensize = self.colorlegend_size,
                blit_texture=True,
                clear_color = hex_to_rgba(self.color_scheme['plotplane-bgcolor']),
                multisampling=2
            )
            self._colorlegend_frame.init()

            colorrange = self.colorlegend.colorrange
            colorrange_length = colorrange[1]-colorrange[0]

            factor = 0.1
            self._colorlegend_frame.inner_camera.set_scaling([1,colorrange_length*(1+2*factor)])
            self._colorlegend_frame.inner_camera.set_position(0,-colorrange_length*factor+colorrange[0])
            self._colorlegend_frame.update_camera(self.camera)

            if self._colorlegend is None:
                self._colorlegend = ShapeInstance('default_rectangle', **{
                    'size': self.colorlegend_size,
                    'position': self.colorlegend_position,
                    'border': {'size': self.color_scheme['plotframe-border-size'],'color': hex_to_rgba(self.color_scheme['plotframe-border-color'])},
                    'color': [0,0,0,0],
                    'texture': self._colorlegend_frame
                })

                self.shaperenderer.draw_instance(self._colorlegend)
            else:
                self._colorlegend.position = self.colorlegend_position
                self._colorlegend.size = self.colorlegend_size

            self._colorlegend_graph = Field(
                top_left=(0,colorrange_length*(1+factor)+colorrange[0]),
                bottom_right=(1,colorrange[0]-colorrange_length*factor),
                color_scheme=self.colorlegend,
                data_kernel='fragment_color = vec4({a}+({l})*(1+2*({f}))*x.y-({l})*({f}),0,0,1)'.format(a=colorrange[0],l=colorrange_length,f=factor)
            )
            self._colorlegend_graph.init()
            plot_camera = self._colorlegend_frame.inner_camera;
            self._colorlegend_graph.program.uniform('mat_camera', plot_camera.get_matrix())
            self._colorlegend_graph.program.uniform('mat_outer_camera', self._plotframe.camera.get_matrix())
            self._colorlegend_graph.program.uniform('mat_domain', np.identity(3))

            self._colorlegend_axis = axis.Fixed(
                camera       = self.camera,
                measurements = self.colorlegend.range,
                bgcolor      = hex_to_rgba(self.color_scheme['yaxis-bgcolor']),
                size         = (self.get_yaxis_size()[0], self.get_yaxis_size()[1]),
                scale_camera = self._colorlegend_frame.inner_camera,
                linecolor    = hex_to_rgba(self.color_scheme['yaxis-linecolor']),
                modelview    = ModelView()
            )
            self._colorlegend_axis.init()
            self._update_measure_axis()
            self._colorlegend_frame.use()
            self._colorlegend_graph.render(self)
            self._colorlegend_frame.unuse()
            length = colorrange_length*(1+2*factor)
            shift = colorrange_length*factor
            y_viewspace = lambda y: (1-(float(y-colorrange[0]+shift)/length))*self.colorlegend_size[1]+self.colorlegend_margin[0]
            x_viewspace = self.colorlegend_position[0]+self.colorlegend_size[0]+5
            curr_length = len(self._colorlegend_texts)
            labels = self._colorlegend_axis.labels
            for i in range(len(labels) - curr_length):
                yplot, label = labels[i+curr_length]
                text = self._fntrenderer.create_text(label,
                    size=self.color_scheme['axis-fontsize'],
                    position=(x_viewspace,y_viewspace(yplot)-10),
                    color=hex_to_rgba(self.color_scheme['axis-fontcolor']))
                self._colorlegend_texts.append(text)

            for i in range(0, min(len(labels), curr_length)):
                text = self._colorlegend_texts[i]
                yplot, label = labels[i]
                if label != text.chars:
                    text.chars = label
                text.position = (x_viewspace,y_viewspace(yplot)-10)
                text.color = hex_to_rgba(self.color_scheme['axis-fontcolor'])
    def init(self):
        """
        initializes plot2d
        """
        self.shaperenderer = ShapeRenderer(self.camera)
        self.shaperenderer.shapes['default_rectangle'] = Rectangle()
        self.shaperenderer.gl_init()

        # setup axis
        self.init_labels()

        # setup plotplane
        plotframe = window.Framebuffer(
            camera      = self.camera,
            screensize  = self.plotframe_size,
            screen_mode = window.Framebuffer.SCREEN_MODE_STRECH,
            record_mode = self.plotmode.record_mode if self.plotmode is not None else window.Framebuffer.RECORD_CLEAR,
            clear_color = hex_to_rgba(self.color_scheme['plotplane-bgcolor']),
            multisampling = 8,
            blit_texture=True,
        )
        self._fntrenderer.set_camera(self.camera)
        self._fntrenderer.init()

        self._fntlayout.camera = self.camera
        if self.plotmode is not None:
            plotframe.record_program = self.plotmode.get_shader()

        plotframe.init()
        plotframe.modelview.set_position(self._plotplane_margin[3], self._plotplane_margin[0])
        plotframe.update_modelview()

        # setup plotplane camera
        plotframe.inner_camera.set_base_matrix(np.array([
            1, 0, 0, 0,
            0, -1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1,
        ], dtype=np.float32))
        plotframe.inner_camera.set_scaling(self._axis)
        plotframe.inner_camera.set_position(*self._origin)
        self._plotframe = plotframe




        # parent controller initialization
        Controller.init(self)
        self.state = self.STATE_IDLE

        self._select_area_rectangle = ShapeInstance('default_rectangle', **{
            'size': (0,0),
            'position': (0,0),
            'border': {
                'size': self.color_scheme['select-area-border-size'],
                'color': hex_to_rgba(self.color_scheme['select-area-border-color']),
            },
            'color': hex_to_rgba(self.color_scheme['select-area-bgcolor']),
        })

        self._plotplane = ShapeInstance('default_rectangle', **{
            'size': self.plotframe_size,
            'position': self.plotframe_position,
            'border': {
                'size': self.color_scheme['plotframe-border-size'],
                'color': hex_to_rgba(self.color_scheme['plotframe-border-color']),
            },
            'color': [0,0,0,0],
            'texture': self._plotframe
        })
        self.shaperenderer.draw_instance(self._plotplane)

        # setup axis
        if self._render_axis[0]:
            self._xaxis = axis.Scale(
                camera       = self.camera,
                scale_camera = self._plotframe.inner_camera,
                size         = self.get_xaxis_size(),
                unit         = self._axis_units[0],
                subunits     = self._axis_subunits[0],
                font         = None,
                axis         = axis.XAXIS,
                unit_symbol  = self._axis_unit_symbols[0],
                bgcolor      = hex_to_rgba(self.color_scheme['xaxis-bgcolor']),
                linecolor    = hex_to_rgba(self.color_scheme['xaxis-linecolor']),
                fontcolor    = hex_to_rgba(self.color_scheme['xaxis-fontcolor']),
            )
            self._xaxis.density = self._axis_density[0]

            self._xaxis.init()
            self._update_xaxis()

        if self._render_axis[1]:
            self._yaxis = axis.Scale(
                camera       = self.camera,
                scale_camera = self._plotframe.inner_camera,
                size         = self.get_yaxis_size(),
                unit         = self._axis_units[1],
                subunits     = self._axis_subunits[1],
                font         = None,
                axis         = axis.YAXIS,
                unit_symbol  = self._axis_unit_symbols[1],
                bgcolor      = hex_to_rgba(self.color_scheme['yaxis-bgcolor']),
                linecolor    = hex_to_rgba(self.color_scheme['yaxis-linecolor']),
                fontcolor    = hex_to_rgba(self.color_scheme['yaxis-fontcolor']),
            )
            self._yaxis.density = self._axis_density[1]
            self._yaxis.init()
            self._update_yaxis()
        self.init_colorlegend()
        self._plotplane.size = self.plotframe_size
        self._update_plotframe_camera()
        self._update_graph_matricies()
        glfwWindowHint(GLFW_SAMPLES, 4);
        glEnable(GL_MULTISAMPLE)

        if self.widget is not None:
            plot_info('Plotter2d', 'init widget...')
            self.widget.gl_init()

        plot_info('Plotter2d', 'inizialized!')
        def area_selecting():
            self.state = self.STATE_SELECT_AREA
            self._select_area_rectangle.color = hex_to_rgba(self.color_scheme['select-area-bgcolor'])

            self._select_area = list(self.cursor) + [0,0]
            self.shaperenderer.draw_instance(self._select_area_rectangle)