#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# Description: Abacum, programming language
# Copyright (C) 2007 by Rafael Treviño Menéndez
# Author: Rafael Treviño Menéndez <skasi.7@gmail.com>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import pygame, random

HORIZONTAL = 0
VERTICAL = 1

def relativePos (pos1, pos2):
	return pos1[0] - pos2[0], pos1[1] - pos2[1]

def renderBar (surface, col1, col2, text = None):
	surfaceRect = surface.get_rect ()
	midHeight = int (.78 * surfaceRect.height)
	gradient (surface, col1, col2, direction = VERTICAL)
	glow = pygame.Surface ((surfaceRect.width - 2, int (.5 * surfaceRect.height))).convert_alpha ()
	gradient (glow, (255, 255, 255, 200), (255, 255, 255, 65), direction = VERTICAL)
	surface.blit (glow, (1, 1))
	if text:
		surface.blit (text[0], text[1])

def poolEffect (surface, beginCol, endCol, rect = None, size = 5):
	if not rect:
		rect = surface.get_rect ()
	
	surface.fill ((0, 0, 0), rect)

	diff = endCol[0] - beginCol[0], endCol[1] - beginCol[1], endCol[2] - beginCol[2]
	step = 1.0 * diff[0] / 100, 1.0 * diff[1] / 100, 1.0 * diff[2] / 100

	rectRect = pygame.Rect (0, 0, 4, 4)

	for x in xrange (0, rect.width, size):
		rectRect.left = x
		for y in xrange (0, rect.height, size):
			rectRect.top = y
			
			i = random.randrange (100)
			color = (int (beginCol [0] + i * step [0]), int (beginCol [1] + i * step [1]), int (beginCol [2] + i * step [2]))
			pygame.draw.rect (surface, color, rectRect)

def textRender (text, font, col1, col2, rect = None):
	textSurface = font.render (text, True, (255, 255, 255))
	if rect == None:
		pos = 0, 0
		rect = textSurface.get_rect ()
	else:
		pos = (rect.width - textSurface.get_rect ().width) / 2, (rect.height - textSurface.get_rect ().height) / 2
	surface = pygame.Surface (rect.size)
	renderBar (surface, col1, col2, (textSurface, pos))
	return surface

def gradient (surface, beginCol, endCol, rect = None, direction = HORIZONTAL):
	if not rect:
		rect = surface.get_rect ()
	
	if len (beginCol) < 4:
		beginCol = beginCol[0], beginCol[1], beginCol[2], 255
	if len (endCol) < 4:
		endCol = endCol[0], endCol[1], endCol[2], 255

	diff = endCol[0] - beginCol[0], endCol[1] - beginCol[1], endCol[2] - beginCol[2], endCol[3] - beginCol[3] 

	if direction == HORIZONTAL:
		width = rect.width - 1
		step = 1.0 * diff[0] / width, 1.0 * diff[1] / width, 1.0 * diff[2] / width, 1.0 * diff[3] / width
		for i in xrange (rect.width):
			color = (int (beginCol [0] + i * step [0]), int (beginCol [1] + i * step [1]), int (beginCol [2] + i * step [2]), int (beginCol[3] + i * step [3]))
			pygame.draw.line (surface, color, (rect.left + i, rect.top), (rect.left + i, rect.bottom))

	elif direction == VERTICAL:
		height = rect.height - 1
		step = 1.0 * diff[0] / height, 1.0 * diff[1] / height, 1.0 * diff[2] / height, 1.0 * diff[3] / height
		for i in xrange (rect.height):
			color = (int (beginCol [0] + i * step [0]), int (beginCol [1] + i * step [1]), int (beginCol [2] + i * step [2]), int (beginCol[3] + i * step[3]))
			pygame.draw.line (surface, color, (rect.left, rect.top + i), (rect.right, rect.top + i))
