#
###
# Application: Pycadia
# File:        vector_text.py
# Description: pycadia vector text message class
# Copyright (c) 2002-2003   Andy Balcombe <http://www.anti-particle.com>
###
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import string
import pygame
from pygame.locals import *

from game_state import Point
from actor import Actor
import gd_letters
import gd_numbers

class Char(pygame.sprite.Sprite, Actor):
    """a vector character"""
    def __init__(self, consts, verts, lines, scale, surf_size, x, y):
        Actor.__init__(self, consts, surf_size)
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.verts = verts
        self.lines = lines
        self.scale = scale
        self.visible=1
        self.spin_deg = 0
        self.speed = Point((0.0,0.0))
        self.mass = 0
        self.brightness = consts.MAX_BRIGHT
        self.pos = Point((x, y))
        self.rect.centerx = self.pos.x
        self.rect.centery = self.pos.y
        #redraw
        self.draw_obj()
        

class Message:
    """a message made up of Letter() classes"""
    def __init__(self, consts, message, scale, msg_x, msg_y, centerx_msg=0):
        self.consts = consts
        self.chars = [] #pygame.Sprite.Group()
        self.scale = scale
        self.char_width = gd_letters.letterRectSize[0] * scale
        self.char_height = gd_letters.letterRectSize[1] * scale
        self.surf_size = (self.char_width, self.char_height)
        if centerx_msg:
            #calc centre of message, first find longest line
            iMaxLen = max([len(s) for s in message.split('\n')]) 
            self.msg_x = (consts.SCREENRECT.width / 2) - ((iMaxLen * self.char_width)/2)
        else:
            self.msg_x = msg_x
        self.msg_y = msg_y
        self.message = str(message)
        #and generate the char sprites
        self._generate_message()
        
    def _generate_message(self):
        """generate a list of vector char classes"""
        let_num = 0
        y = self.msg_y
        #for each char in the message
        for char in self.message:
            let_num += 1
            #new line
            if char == '\n':
                let_num = 0
                x = self.msg_x
                self.msg_y += self.char_height + 5 
            #calc scale & y pos
            if char in string.ascii_lowercase:
                scale = self.scale * 0.8
                y = self.msg_y + (self.char_height * 0.1)
            else:
                scale = self.scale
                y = self.msg_y
            #char or digit
            if char in string.ascii_letters or char in string.digits:
                #pick module and select verts 
                if char in string.ascii_letters:
                    gd_module = 'gd_letters.'
                    verts = eval(gd_module + char.lower() + 'Verts')
                elif char in string.digits:
                    gd_module = 'gd_numbers.n'
                    verts = eval(gd_module + 'Verts')
                #set lines
                lines = eval(gd_module + char.lower() + 'Lines')
                #calc x pos
                x = self.msg_x + (self.char_width * let_num) 
                #create vector char
                self.chars.append(Char(self.consts, verts, lines, scale, self.surf_size, x, y))

    def set_visible(self, visible, all):
        """show/hide message"""
        if visible:
            all.add(self.chars)
        else:
            all.remove(self.chars)
            
    def update_message(self, message):
        """update the message text"""
        #first, delete the original message
        for char in self.chars:
            char.kill()
        #now recreate with new chars
        self.chars = []
        self.message = str(message)
        self._generate_message()

