"""Tile menu for map editor"""
"""Adventures on Planet Zephulor by Aaron Hachez. 

   All code, artwork, and sound copyright 2003, Aaron Hachez.

   ----------------------------------------------------------------------------------------

   This file is part of "Adventures on Planet Zephulor".

   A copy of the GNU General Public License should be included with any
   redistribution of this program in a file named "COPYING.txt".

   "Adventures on Planet Zephulor" 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.

   "Adventures on Planet Zephulor" 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with "Adventures on Planet Zephulor"; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   ----------------------------------------------------------------------------------------

   "Adventures on Planet Zephulor" uses the following libraries:

   PyGame
   SDL

   Both are at the time of this writing, licensed under the GNU LGPL. Please see
   the included file LGPL for details 
"""

import pygame
import myimg

class tilemenu_base:
    """Main tilemenu class"""

    # The display rect for the tile menu
    viewport = []

    # The width and height of the viewport
    viewportwidth = 0
    viewportheight = 0

    # The starting offset for drawing
    startoffset = 0

    # The value to scroll by each cycle, if not 0, menu will scroll
    scrollvalue = 0

    # The number of tiles wide and high of the viewport. Unlike the map, we don't need a wider than the viewport list of tiles.
    tileswide = 0
    tileshigh = 0

    # The width and height of tiles
    tilewidth = 32
    tileheight = 32

    # The tile data
    tiles = []

    # Pad is the distance between tiles
    pad = 2

    def __init__(self, viewrect=[ 0,0,0,0 ], wh=[32, 32]):
        """Initialize the tile menu"""

        self.viewport = viewrect[:]

        self.tilewidth = wh[0]
        self.tileheight = wh[1]

        self.viewportwidth = self.viewport[2] - self.viewport[0]
        self.viewportheight = self.viewport[3] - self.viewport[1]

        self.tileswide = self.viewportwidth / (self.tilewidth + self.pad)
        self.tileshigh = self.viewportheight / (self.tileheight + self.pad)

        self.background = pygame.Surface((self.viewportwidth, self.viewportheight))
        self.background = self.background.convert()
        self.background.fill((0, 0, 0))

    def draw(self, screen):
        """Draw the tile menu to the screen"""
        
        screen.blit(self.background, (self.viewport[0], self.viewport[1]))

        x = 0
        y = 0

        # Set our start position to the offset
        n = self.startoffset

        # For each row and column
        for i in range(self.tileshigh):
            y = self.viewport[1]
            y += i * self.tileheight
            y += i * self.pad

            # If our index is greater than the total number of tiles
            if n >= len(self.tiles):
                break

            for j in range(self.tileswide):
                x = self.viewport[0]
                x += j * self.tilewidth
                x += j * self.pad
               
                # If our index is greater than the total number of tiles
                if n >= len(self.tiles):
                    break

                # Blit the tile to the screen
                screen.blit(self.tiles[ n ], (x, y))
                
                n += 1

    def loadtiles(self, filename, borderwidth=1):
        """Load a set of tiles from filename. Each tile is wh wide/high"""
   
        self.tiles = myimg.loadtiles(filename, tilewidth=self.tilewidth, tileheight=self.tileheight, border=borderwidth, colorkey=0) 

    def getclick(self, object):
        """Check to see if click is within our viewport"""

        # If the object is not less than right side, greater than left side,
        # Less than top, or greater than bottom
        if object.screenrect[0] > self.viewport[2]:
            return
        elif object.screenrect[1] > self.viewport[3]:
            return
        elif object.screenrect[0] < self.viewport[0]:
            return
        elif object.screenrect[1] < self.viewport[1]:
            return
        else:
            # If we get to this point, the object is onscreen
            return 1

    def returntile(self, object):
        """Return what tile has been clicked"""

        # Get x/y values from the distance of the mouse pointer (object) from the upper x/y of the viewport
        x = object.screenrect[0] - self.viewport[0]
        y = object.screenrect[1] - self.viewport[1]

        # As long as our x/y values are within the proper range
        if x < self.viewportwidth and y < self.viewportheight and x > 0 and y > 0:

            wherex = x
            wherey = y

            tile = wherex / (self.tilewidth + self.pad)
            tile += (wherey / (self.tileheight + self.pad)) * self.tileswide

            # Add offset (used for draw too) to the tile number
            tile += self.startoffset

            return tile
        else:
            return

    def scroll(self, value=0):
        """Scroll the tile menu up and down"""

        # Scroll value number of rows.
        self.startoffset += value * self.tileswide

        if self.startoffset < 0:
            self.startoffset = 0

        if self.startoffset > len(self.tiles):
            self.startoffset = len(self.tiles)

        self.scrollvalue = value

    def update(self):
        """Make updates for each cycle"""

        if self.scrollvalue != 0:
            self.scroll(self.scrollvalue)
