# Path: flexlay/gui/tile_selection.py
# class TileSelection:
# 
#     def __init__(self):
#         self.tilemap = None
#         self.start_pos = None
#         self.selection = None
#         self.active = False
# 
#     def start(self, tilemap, pos):
#         self.tilemap = tilemap
#         self.active = True
#         self.start_pos = pos
#         self.update(self.start_pos)
# 
#     def update(self, pos):
#         self.selection = Rect(min(self.start_pos.x, pos.x),
#                               min(self.start_pos.y, pos.y),
#                               max(self.start_pos.x, pos.x) + 1,
#                               max(self.start_pos.y, pos.y) + 1)
# 
#     def is_active(self):
#         return self.active
# 
#     def clear(self):
#         self.selection = Rect()
#         self.active = False
# 
#     def draw(self, gc, color=Color(255, 255, 255, 100)):
#         tile_size = self.tilemap.get_tileset().get_tile_size()
# 
#         gc.fill_rect(Rect(self.selection.left * tile_size,
#                           self.selection.top * tile_size,
#                           self.selection.right * tile_size,
#                           self.selection.bottom * tile_size),
#                      color)
# 
#     def get_brush(self, field):
#         sel = self.selection.copy()
# 
#         sel.normalize()
# 
#         if sel.left > field.width - 1 or \
#            sel.top > field.height - 1 or \
#            sel.right <= 0 or \
#            sel.bottom <= 0:
# 
#             # Selection is empty
#             logging.error("Error: Invalid selection")
#             brush = TileBrush(1, 1)
#             brush.put(0, 0, 0)
#             brush.set_opaque()
#             return brush
#         else:
#             # Selection is valid
#             # Cut the selection to the field size
#             sel.left = max(0, sel.left)
#             sel.top = max(0, sel.top)
# 
#             sel.right = min(sel.right, field.width)
#             sel.bottom = min(sel.bottom, field.height)
# 
#             brush = TileBrush(sel.width, sel.height)
# 
#             for y in range(sel.top, sel.bottom):
#                 for x in range(sel.left, sel.right):
#                     brush.put(x - sel.left, y - sel.top,
#                               field.at(x, y))
# 
#             return brush
# 
#     def get_rect(self):
#         sel = self.selection.copy()
#         sel.normalize()
#         return sel
# 
# Path: flexlay/tile_brush.py
# class TileBrush:
# 
#     @staticmethod
#     def from_field(field, w, h, pos_x, pos_y):
#         return TileBrush(w, h, field.copy_region(pos_x, pos_y, w, h))
# 
#     def __init__(self, w, h, field=None):
#         self.opaque = False
#         if field is None:
#             self.field = Field.from_size(w, h)
#         else:
#             self.field = field
# 
#     @property
#     def width(self):
#         return self.field.width
# 
#     @property
#     def height(self):
#         return self.field.height
# 
#     def set_data(self, data):
#         self.field.set_data(data)
# 
#     def get_data(self, data):
#         return self.field.get_data(data)
# 
#     def at(self, x, y):
#         return self.field.at(x, y)
# 
#     def put(self, x, y, value):
#         assert isinstance(value, int)
#         return self.field.put(x, y, value)
# 
#     def resize(self, w, h, pos_x=0, pos_y=0):
#         self.field.resize(w, h, pos_x, pos_y)
# 
#     def set_opaque(self):
#         self.opaque = True
# 
#     def set_transparent(self):
#         self.opaque = False
# 
#     def is_opaque(self):
#         return self.opaque
# 
#     def auto_crop(self):
#         rect = Rect(0, 0, 0, 0)
# 
#         for y, x in itertools.product(range(0, self.field.height),
#                                       range(0, self.field.width)):
#             if self.field.at(x, y) != 0:
#                 rect.top = y
#                 break
# 
#         for y, x in itertools.product(reversed(range(0, self.field.height)),
#                                       range(0, self.field.width)):
#             if self.field.at(x, y) != 0:
#                 rect.bottom = y + 1
#                 break
# 
#         for x, y in itertools.product(range(0, self.field.width),
#                                       range(0, self.field.height)):
#             if self.field.at(x, y) != 0:
#                 rect.left = x
#                 break
# 
#         for x, y in itertools.product(reversed(range(0, self.field.width)),
#                                       range(0, self.field.height)):
#             if self.field.at(x, y) != 0:
#                 rect.right = x + 1
#                 break
# 
#         if rect.width != 0:
#             self.resize(rect.width, rect.height,
#                         -rect.left, -rect.top)
#         else:
#             self.field = Field.from_size(1, 1)
#             self.field.put(0, 0, 0)
#             self.set_opaque()


from flexlay.gui.tile_selection import TileSelection
from flexlay.tile_brush import TileBrush
# Flexlay - A Generic 2D Game Editor
# Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.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 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




class ToolContext:

    current = None

    def __init__(self):
        ToolContext.current = self

# Path: flexlay/gui/tile_selection.py
# class TileSelection:
# 
#     def __init__(self):
#         self.tilemap = None
#         self.start_pos = None
#         self.selection = None
#         self.active = False
# 
#     def start(self, tilemap, pos):
#         self.tilemap = tilemap
#         self.active = True
#         self.start_pos = pos
#         self.update(self.start_pos)
# 
#     def update(self, pos):
#         self.selection = Rect(min(self.start_pos.x, pos.x),
#                               min(self.start_pos.y, pos.y),
#                               max(self.start_pos.x, pos.x) + 1,
#                               max(self.start_pos.y, pos.y) + 1)
# 
#     def is_active(self):
#         return self.active
# 
#     def clear(self):
#         self.selection = Rect()
#         self.active = False
# 
#     def draw(self, gc, color=Color(255, 255, 255, 100)):
#         tile_size = self.tilemap.get_tileset().get_tile_size()
# 
#         gc.fill_rect(Rect(self.selection.left * tile_size,
#                           self.selection.top * tile_size,
#                           self.selection.right * tile_size,
#                           self.selection.bottom * tile_size),
#                      color)
# 
#     def get_brush(self, field):
#         sel = self.selection.copy()
# 
#         sel.normalize()
# 
#         if sel.left > field.width - 1 or \
#            sel.top > field.height - 1 or \
#            sel.right <= 0 or \
#            sel.bottom <= 0:
# 
#             # Selection is empty
#             logging.error("Error: Invalid selection")
#             brush = TileBrush(1, 1)
#             brush.put(0, 0, 0)
#             brush.set_opaque()
#             return brush
#         else:
#             # Selection is valid
#             # Cut the selection to the field size
#             sel.left = max(0, sel.left)
#             sel.top = max(0, sel.top)
# 
#             sel.right = min(sel.right, field.width)
#             sel.bottom = min(sel.bottom, field.height)
# 
#             brush = TileBrush(sel.width, sel.height)
# 
#             for y in range(sel.top, sel.bottom):
#                 for x in range(sel.left, sel.right):
#                     brush.put(x - sel.left, y - sel.top,
#                               field.at(x, y))
# 
#             return brush
# 
#     def get_rect(self):
#         sel = self.selection.copy()
#         sel.normalize()
#         return sel
# 
# Path: flexlay/tile_brush.py
# class TileBrush:
# 
#     @staticmethod
#     def from_field(field, w, h, pos_x, pos_y):
#         return TileBrush(w, h, field.copy_region(pos_x, pos_y, w, h))
# 
#     def __init__(self, w, h, field=None):
#         self.opaque = False
#         if field is None:
#             self.field = Field.from_size(w, h)
#         else:
#             self.field = field
# 
#     @property
#     def width(self):
#         return self.field.width
# 
#     @property
#     def height(self):
#         return self.field.height
# 
#     def set_data(self, data):
#         self.field.set_data(data)
# 
#     def get_data(self, data):
#         return self.field.get_data(data)
# 
#     def at(self, x, y):
#         return self.field.at(x, y)
# 
#     def put(self, x, y, value):
#         assert isinstance(value, int)
#         return self.field.put(x, y, value)
# 
#     def resize(self, w, h, pos_x=0, pos_y=0):
#         self.field.resize(w, h, pos_x, pos_y)
# 
#     def set_opaque(self):
#         self.opaque = True
# 
#     def set_transparent(self):
#         self.opaque = False
# 
#     def is_opaque(self):
#         return self.opaque
# 
#     def auto_crop(self):
#         rect = Rect(0, 0, 0, 0)
# 
#         for y, x in itertools.product(range(0, self.field.height),
#                                       range(0, self.field.width)):
#             if self.field.at(x, y) != 0:
#                 rect.top = y
#                 break
# 
#         for y, x in itertools.product(reversed(range(0, self.field.height)),
#                                       range(0, self.field.width)):
#             if self.field.at(x, y) != 0:
#                 rect.bottom = y + 1
#                 break
# 
#         for x, y in itertools.product(range(0, self.field.width),
#                                       range(0, self.field.height)):
#             if self.field.at(x, y) != 0:
#                 rect.left = x
#                 break
# 
#         for x, y in itertools.product(reversed(range(0, self.field.width)),
#                                       range(0, self.field.height)):
#             if self.field.at(x, y) != 0:
#                 rect.right = x + 1
#                 break
# 
#         if rect.width != 0:
#             self.resize(rect.width, rect.height,
#                         -rect.left, -rect.top)
#         else:
#             self.field = Field.from_size(1, 1)
#             self.field.put(0, 0, 0)
#             self.set_opaque()


# Path: flexlay/tool_context.py
from flexlay.gui.tile_selection import TileSelection
from flexlay.tile_brush import TileBrush

# Flexlay - A Generic 2D Game Editor
# Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.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 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




class ToolContext:

    current = None

    def __init__(self):
        ToolContext.current = self

        self.tile_brush = TileBrush(1, 1)