<commit_before># Colonizer is not intended for use yet
#!/usr/bin/python3

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from tkinter import messagebox
import json
import math
import os

from terralib.pixelgrid import *

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.pack()

        self.tilegrid = TileGrid()

        self.createWidgets()

    def createWidgets(self):
        # Create menu
        menubar = tk.Menu(self)
        self.master.config(menu=menubar)

        # Create loading buttons
        loadframe = tk.Frame(self)
        loadframe.grid(row=0, column=0, sticky=tk.W)
        openbutton = tk.Button(loadframe, text="Open", command=self.open)
        openbutton.grid(row=0, column=0)
        self.loadergrid = tk.StringVar()
        self.loadergrid.trace("w", self.changegrid)
        self.loader = tk.OptionMenu(
                loadframe, self.loadergrid, "Nothing loaded")
        self.loader.grid(row=0, column=1)
        self.pageflip = Flipper(self, onchange=self.changepage)
        self.pageflip.grid(row=0, column=1, sticky=tk.E)

        # Create panels
        self.tilepalette = tk.Canvas(self, width=512, height=512)
        self.tilepalette.grid(row=1, column=0, columnspan=2)
        self.tilepaletteimage = self.tilepalette.create_image(
                0, 0, anchor=tk.NW)
        
        self.tileimg = tk.Canvas(self, width=512, height=512)
        self.tileimg.grid(row=1, column=2)
        self.tileimgimage = self.tileimg.create_image(
                0, 0, anchor=tk.NW)
        self.tileimg.bind("<Button-1>", self.clicktileimg)

    def open(self):
        filen = filedialog.askopenfilename(
                filetypes=(("Terraformer images", "*.terra"),
                           ("All files", "*")),
                title="Open paletted image")
        if filen != () and filen != "":
            with open(filen, "r") as fileo:
                pixelgrid = PixelGrid([(0,0,0)])
                pixelgrid.load(json.load(fileo))
                name = os.path.basename(filen)
                self.tilegrid.add_dependency(filen, pixelgrid)
                self.resetgrids()

    def changegrid(self, *args):
        if self.loadergrid.get() == "Nothing loaded":
            return
        self.currentgrid = self.tilegrid.get_grid(self.loadergrid.get())
        self.tilepalettetkimg = self.currentgrid.getTkImage(2)
        self.tilepalette.itemconfig(self.tilepaletteimage, 
                                    image=self.tilepalettetkimg)
        self.pageflip.top = self.currentgrid.pages - 1
        self.pageflip.value = 0
        self.pageflip.reset()
        self.currentgrid.changepage(0)

    def resetgrids(self):
        self.loader["menu"].delete(0, "end")
        for key in self.tilegrid.dependencies:
            self.loader["menu"].add_command(label=key, command=tk._setit(
                        self.loadergrid, key))

    def changepage(self):
        self.currentgrid.changepage(self.pageflip.value)
        self.tilepalettetkimg = self.currentgrid.getTkImage(2)
        self.tilepalette.itemconfig(self.tilepaletteimage, 
                                    image=self.tilepalettetkimg)

    def redraw(self):
        self.tileimgtkimg = self.tilegrid.draw(2)
        self.tileimg.itemconfig(self.tileimgimage,
                                image = self.tileimgtkimg)

    def clicktileimg(self, event):
        x = math.floor(self.tileimg.canvasx(event.x)/(8*2))
        y = math.floor(self.tileimg.canvasy(event.y)/(8*2))
        self.tilegrid.set(x, y, 0, self.loadergrid.get(), 
                          self.pageflip.value, 0, 0)
        self.redraw()

class TileGrid:
    def __init__(self):
        self.width = 32
        self.height = 32
        self.layers = []
        self.layers.append({})
        self.grids = {}

    def set(self, x, y, l, grid, gp, gx, gy):
        self.layers[l][(x,y)] = (grid, gp, gx, gy)

    def draw(self, zoom):
        photo = tk.PhotoImage(width=8*self.width*zoom,
                              height=8*self.height*zoom)
        for layer in self.layers:
            for loc in layer:
                gset = layer[loc]
                x = loc[0]*zoom*8
                y = loc[1]*zoom*8
                self.get_grid(gset[0]).drawTkSubset(
                        photo, zoom, gset[2], gset[3], 1, 1, x, y,
                        layer == self.layers[0], gset[1])
        return photo<commit_msg>Colonizer layers and selections<commit_after># Colonizer is not intended for use yet
#!/usr/bin/python3

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from tkinter import messagebox
import json
import math
import os

from terralib.pixelgrid import *

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.pack()

        self.tilegrid = TileGrid()
        self.selection = (0, 0, 1, 1)
        self.createWidgets()
        self.currentlayer = 0
        
        self.config = {
            "lastdir" : "./"}
        
        try:
            with open("./config.json", "r") as fileo:
                self.config = json.load(fileo)
        except IOError:
            pass # Just use default

        self.redraw()

    def createWidgets(self):
        # Create menu
        menubar = tk.Menu(self)
        self.master.config(menu=menubar)

        # Create loading buttons
        loadframe = tk.Frame(self)
        loadframe.grid(row=0, column=0, sticky=tk.W)
        openbutton = tk.Button(loadframe, text="Open", command=self.open)
        openbutton.grid(row=0, column=0)
        self.loadergrid = tk.StringVar()
        self.loadergrid.trace("w", self.changegrid)
        self.loader = tk.OptionMenu(
                loadframe, self.loadergrid, "Nothing loaded")
        self.loader.grid(row=0, column=1)
        self.pageflip = Flipper(self, onchange=self.changepage)
        self.pageflip.grid(row=0, column=1, sticky=tk.E)
        self.layerflip = Flipper(self, onchange=self.changelayer, top=8)
        self.layerflip.grid(row=0, column=2, sticky=tk.E)

        # Create panels
        self.tilepalette = tk.Canvas(self, width=512, height=512)
        self.tilepalette.grid(row=1, column=0, columnspan=2)
        self.tilepaletteimage = self.tilepalette.create_image(
                0, 0, anchor=tk.NW)
        self.tilepaletteselection = self.tilepalette.create_rectangle(
                1, 1, 16, 16, outline="grey")
        self.tilepalette.bind("<Button-1>", self.clicktilepalette)
        self.tilepalette.bind("<B1-Motion>", self.motiontilepalette)

        self.tileimg = tk.Canvas(self, width=512, height=512)
        self.tileimg.grid(row=1, column=2)
        self.tileimgimage = self.tileimg.create_image(
                0, 0, anchor=tk.NW)
        self.tileimg.bind("<Button-1>", self.clicktileimg)
        self.tileimg.bind("<B1-Motion>", self.motiontileimg)

    def open(self):
        filen = filedialog.askopenfilename(
                filetypes=(("Terraformer images", "*.terra"),
                           ("All files", "*")),
                title="Open paletted image",
                initialdir=self.config["lastdir"])
        if filen != () and filen != "":
            with open(filen, "r") as fileo:
                pixelgrid = PixelGrid([(0,0,0)])
                pixelgrid.load(json.load(fileo))
                name = os.path.basename(filen)
                self.tilegrid.add_dependency(filen, pixelgrid)
                self.resetgrids()

    def changegrid(self, *args):
        if self.loadergrid.get() == "Nothing loaded":
            return
        self.currentgrid = self.tilegrid.get_grid(self.loadergrid.get())
        self.tilepalettetkimg = self.currentgrid.getTkImage(2)
        self.tilepalette.itemconfig(self.tilepaletteimage, 
                                    image=self.tilepalettetkimg)
        self.pageflip.top = self.currentgrid.pages - 1
        self.pageflip.value = 0
        self.pageflip.reset()
        self.currentgrid.changepage(0)

    def resetgrids(self):
        self.loader["menu"].delete(0, "end")
        for key in self.tilegrid.dependencies:
            self.loader["menu"].add_command(label=key, command=tk._setit(
                        self.loadergrid, key))

    def changepage(self):
        self.currentgrid.changepage(self.pageflip.value)
        self.tilepalettetkimg = self.currentgrid.getTkImage(2)
        self.tilepalette.itemconfig(self.tilepaletteimage, 
                                    image=self.tilepalettetkimg)

    def changelayer(self):
        self.currentlayer = self.layerflip.value

    def redraw(self):
        self.tileimgtkimg = self.tilegrid.draw(2)
        self.tileimg.itemconfig(self.tileimgimage,
                                image = self.tileimgtkimg)

    def clicktileimg(self, event):
        if (self.loadergrid.get() == "" or
                self.loadergrid.get() == "Nothing loaded"):
            return

        x = math.floor(self.tileimg.canvasx(event.x)/(8*2))
        y = math.floor(self.tileimg.canvasy(event.y)/(8*2))

        self.settileimgarea(x, y)
        self.lastclick = (x, y)
       
    def motiontileimg(self, event):
        if (self.loadergrid.get() == "" or
                self.loadergrid.get() == "Nothing loaded"):
            return

        x = math.floor(self.tileimg.canvasx(event.x)/(8*2))
        y = math.floor(self.tileimg.canvasy(event.y)/(8*2))
        width = abs(self.selection[0] - self.selection[2])
        height = abs(self.selection[1] - self.selection[3])
        
        if ((x - self.lastclick[0]) % width == 0 and
            (y - self.lastclick[1]) % height == 0):
            self.settileimgarea(x,y)

    def settileimgarea(self, x, y):
        selection = (min(self.selection[0], self.selection[2]),
                     min(self.selection[1], self.selection[3]),
                     max(self.selection[0], self.selection[2]),
                     max(self.selection[1], self.selection[3]))
        
        for i in range(0, selection[2]-selection[0]):
            for j in range(0, selection[3]-selection[1]):
                self.tilegrid.set(
                        x + i, y + j, self.currentlayer, 
                        self.loadergrid.get(), 
                        self.pageflip.value, 
                        selection[0] + i, 
                        selection[1] + j)
        self.redraw()

    def clicktilepalette(self, event):
        if (self.loadergrid.get() == "" or
                self.loadergrid.get() == "Nothing loaded"):
            return

        x = math.floor(self.tileimg.canvasx(event.x)/(8*2))
        y = math.floor(self.tileimg.canvasy(event.y)/(8*2))
        self.selection = (x, y, x+1, y+1)
        self.resetselection()
    
    def motiontilepalette(self, event):
        if (self.loadergrid.get() == "" or
                self.loadergrid.get() == "Nothing loaded"):
            return

        x = math.floor(self.tileimg.canvasx(event.x)/(8*2))
        y = math.floor(self.tileimg.canvasy(event.y)/(8*2))
        
        if (x,y) is not (self.selection[0], self.selection[1]):
            self.selection = (self.selection[0],
                              self.selection[1],
                              x,
                              y)
        self.resetselection()
    
    def resetselection(self):
        selection = (min(self.selection[0], self.selection[2]),
                     min(self.selection[1], self.selection[3]),
                     max(self.selection[0], self.selection[2]),
                     max(self.selection[1], self.selection[3]))
        self.tilepalette.coords(self.tilepaletteselection, 
                                selection[0] * 2 * 8,
                                selection[1] * 2 * 8,
                                selection[2] * 2 * 8,
                                selection[3] * 2 * 8)

class TileGrid:
    def __init__(self):
        self.width = 32
        self.height = 32
        self.layers = []
        self.layers.append({})
        self.grids = {}

    def set(self, x, y, l, grid, gp, gx, gy):
        while(len(self.layers) < l+1):
            self.layers.append({})
        self.layers[l][(x,y)] = (grid, gp, gx, gy)

    def draw(self, zoom):
        photo = tk.PhotoImage(width=8*self.width*zoom,
                              height=8*self.height*zoom)
        photo.put("#000000", 
                  to=(0,0,8*self.width*zoom,8*self.height*zoom))
        for layer in self.layers:
            for loc in layer:
                gset = layer[loc]
                x = loc[0]*zoom*8
                y = loc[1]*zoom*8
                self.get_grid(gset[0]).drawTkSubset(
                        photo, zoom, gset[2], gset[3], 1, 1, x, y,
                        layer == self.layers[0], gset[1])
        return photo
{'fraction_non_alphanumeric': 0.12848774472020966, 'fraction_numerical': 0.01672575921072915, 'mean_word_length': 1.8617115130127921, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1913423', 'n_tokens_mistral': 4190, 'n_tokens_neox': 3963, 'n_words': 723}