#!/usr/bin/env python

# Copyright (C) 2010-2012  Sam Bull

"""
FPS counter, display current FPS performance to the user.

"""

from _locals import *
from base_widget import Simple

class FPSCounter(Simple):

    """
    FPS counter

    """

    _default_size = (80, 30)
    _settings_default = {"label": "", "clock": None}

    def _config(self, **kwargs):
        """
          clock: ``pygame.time.Clock`` Clock used to time the game loop.
          label: ``str`` Text to display in front of the value.

        """
        if "clock" in kwargs:
            self._settings["clock"] = kwargs["clock"]
        if "label" in kwargs:
            self._settings["label"] = kwargs["label"]

    def toggle(self):
        """Toggle the FPS counter, adding or removing this widget."""
        if self.active():
            if self._fade is not None:
                if self._fade_up:
                    self.remove()
                else:
                    self.add()
            else:
                self.remove()
        else:
            self.add()

    def update(self, time):
        """Update counter each frame."""
        text = Simple(Font["widget"].render(
            self._settings["label"] +
            str(round(self._settings["clock"].get_fps(), 1)),
            True, Font.col))
        text.rect.center = (self.rect.w/2, self.rect.h/2)
        self.image.fill(0)
        self.image.blit(text.image, text.pos)