Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-dev-guy authored Apr 29, 2023
1 parent e03d8c6 commit 4f42209
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 0 deletions.
122 changes: 122 additions & 0 deletions canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
from globals import *
import time
import threading
from OpenGL import GL
import cv2
import numpy as np
import base64
WIDTH, HEIGHT = 900, 600
@contextmanager
def opengl_context(width, height):
pygame.init()
context = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL | HIDDEN)
yield context
pygame.quit()
@contextmanager
def skia_surface(window):
context = skia.GrDirectContext.MakeGL()
(fb_width, fb_height) = pygame.display.get_surface().get_size()
backend_render_target = skia.GrBackendRenderTarget(
fb_width,
fb_height,
0, # sampleCnt
0, # stencilBits
skia.GrGLFramebufferInfo(0, GL.GL_RGBA8))
surface = skia.Surface.MakeFromBackendRenderTarget(
context, backend_render_target, skia.kBottomLeft_GrSurfaceOrigin,
skia.kRGBA_8888_ColorType, skia.ColorSpace.MakeSRGB())
assert surface is not None
yield surface
context.abandonContext()
def createCanvas(canvasSurf,width,height):
PIMPcanvas = Canvas(canvasSurf,width,height)
return PIMPcanvas
def create_circle(canvas):
radius = random.randint(10, 50)
x = random.randint(radius, WIDTH - radius)
y = random.randint(radius, HEIGHT - radius)
color = skia.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255)
canvas.drawCircle(x, y, radius, skia.Paint(Color=color))


class Canvas:
def __init__(self, canvasSurf, width, height):
self.canvasSurf = canvasSurf
self.width = width
self.height = height
self.font = skia.Font(skia.Typeface('Arial'), 24)
self.text = 'Type something'
self.text_color = skia.ColorWHITE
self.text_position = skia.Point(100, 100)
# self.MakeContext('')

# self.action = {
# 'AddText':self.AddText()
# }

def AddText(self):

self.canvas.clear(skia.ColorWHITE)
self.canvas.drawImage(self.image, 0, 0)
paint = skia.Paint(self.text_color)
self.canvas.drawString(self.text, self.text_position.x(), self.text_position.y(), self.font, paint)
def AddCircle(self):
pass
# def updateCanvas(self,data):
# # print(data)
# data = data.reshape(-1)
# data = tuple(data)
# # print(data)
# self.pil_image.putdata(data)
# return self.pil_image
def MakeContext(self):
with opengl_context(self.width,self.height) as window:
GL.glClear(GL.GL_COLOR_BUFFER_BIT)
with skia_surface(window) as surface:
with surface as canvas:
canvas.clear(skia.ColorWHITE)
self.image = surface.makeImageSnapshot()
self.data = self.image.toarray()
self.pil_image = Image.fromarray(self.data)
cv2_image = cv2.cvtColor(np.array(self.pil_image), cv2.COLOR_RGB2BGR)
retval, buffer = cv2.imencode(".png", cv2_image)
image_data_base64_encoded_string = base64.b64encode(buffer).decode("utf-8")
# cv2.imshow('image', cv2_image)

# # Wait for a key press and then close the window
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# self.pil_image = Image.frombuffer("RGBA", (self.width, self.height), self.data, "raw", "RGBA", 0, 1)
surface.flushAndSubmit()
pygame.display.flip()
#print(len(self.data),self.data.shape[1])
self.canvasSurf.initMainCanvas(image_data_base64_encoded_string)
while self.canvasSurf.AppON:
create_circle(canvas)
surface.flushAndSubmit()
pygame.display.flip()
#self.image = surface.makeImageSnapshot()
assert self.image is not None
self.image = surface.makeImageSnapshot()
self.data = self.image.toarray()
self.pil_image = Image.fromarray(self.data)
cv2_image = cv2.cvtColor(np.array(self.pil_image), cv2.COLOR_RGB2BGR)
retval, buffer = cv2.imencode(".png", cv2_image)
image_data_base64_encoded_string = base64.b64encode(buffer).decode("utf-8")
# self.data = self.image.toarray()
#img = cv2.cvtColor(self.data, cv2.COLOR_RGB2BGR)
# self.pil_image.putdata([tuple(pixel) for row in self.data for pixel in row])
# print(self.pil_image)
#self.pil_image = Image.frombuffer("RGBA", (self.width, self.height), self.data, "raw", "RGBA", 0, 1)
# self.photo_image = ImageTk.PhotoImage(self.pil_image)
# print(self.photo_image,self.pil_image,self.canvasSurf.AppON)
self.canvasSurf.renderCanvas(image_data_base64_encoded_string)
time.sleep(1) # wait for one second before creating the next circle
def Render(self):
# self.MakeContext(action)
self.thread = threading.Thread(target=self.MakeContext)
self.thread.start()
# self.thread.join()



124 changes: 124 additions & 0 deletions editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from globals import *
import canvas
class editor(tk.Canvas):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#111',bd=0,highlightthickness=0)
self.bind('<Motion>', self.RulerPointer)
self.bind('<Configure>', self.PaintOnConfig)

self.canvasState = False
self.RulerMarkerX = []
self.RulerMarkerY = []
self.RulerText = []
const = 20
self.const = const


def PaintOnConfig(self,event):
# self.delete("all")


self.DrawRulers()
def DrawRulersOnInit(self):
const = 20
self.const = const
self.create_line(20, 0, 20, self.winfo_height(), width=1,fill='#444')
self.create_line(0, 20, self.winfo_width(), 20, width=1,fill='#444')

for i in range(0, self.winfo_width(), 10):
if i % 50 == 0:
self.RulerMarkerX.append(self.create_line(i+const,20, i+const,10, width=1,fill='#444'))
self.RulerText.append(self.create_text(i+const, 10, text=str(i), font=("Arial", 7),anchor=tk.SW,fill='#555'))
else:
self.RulerMarkerX.append(self.create_line(i+const, 20, i+const, 15, width=1,fill='#444'))

for i in range(0, self.winfo_height(), 10):
if i % 50 == 0:
# if i == 0:continue;
self.RulerMarkerY.append(self.create_line(20, i+const, 10, i+const, width=1,fill='#444'))
self.RulerText.append(self.create_text(0, i+const, text=str(i), font=("Arial", 7),anchor=tk.SW,fill='#555'))
else:
self.RulerMarkerY.append(self.create_line(20, i+const, 15, i+const, width=1,fill='#444'))
print(self.RulerMarkerX)
def DrawRulers(self):
if self.canvasState:
print(int(self.winfo_width()/10),len(self.RulerMarkerX))
if not(int(self.winfo_width()/10)<=len(self.RulerMarkerX) or int(self.winfo_height()/10)<=len(self.RulerMarkerY)):

print(int(self.winfo_width()/10),len(self.RulerMarkerX),"jjjj")
# pass
else:

self.AddXlist = int(self.winfo_width()/10)-len(self.RulerMarkerX)
self.RulerMarkerX = self.RulerMarkerX + [None] * self.AddXlist

self.AddYlist = int(self.winfo_height()/10)-len(self.RulerMarkerY)
self.RulerMarkerY = self.RulerMarkerY + [None] * self.AddYlist
const = 20
print(self.AddXlist,self.AddYlist,len(self.RulerMarkerX),len(self.RulerMarkerY))
self.const = const
self.create_line(20, 0, 20, self.winfo_height(), width=1,fill='#444')
self.create_line(0, 20, self.winfo_width(), 20, width=1,fill='#444')

for i,j in zip(range(0, self.winfo_width(), 10),range(len(self.RulerMarkerX))):
if self.RulerMarkerX[j] == None:
if i % 50 == 0:

self.RulerMarkerX[j] = self.create_line(i+const,20, i+const,10, width=1,fill='#444')
self.RulerText.append(self.create_text(i+const, 10, text=str(i), font=("Arial", 7),anchor=tk.SW,fill='#555'))
else:
self.RulerMarkerX[j] = self.create_line(i+const, 20, i+const, 15, width=1,fill='#444')
else:
continue
for i,j in zip(range(0, self.winfo_height(), 10),range(len(self.RulerMarkerY))):
if self.RulerMarkerY[j] ==None:
if i % 50 == 0:
# if i == 0:continue;
self.RulerMarkerY[j] = self.create_line(20, i+const, 10, i+const, width=1,fill='#444')
self.RulerText.append(self.create_text(0, i+const, text=str(i), font=("Arial", 7),anchor=tk.SW,fill='#555'))
else:
self.RulerMarkerY[j] = self.create_line(20, i+const, 15, i+const, width=1,fill='#444')
else:
continue
print(self.RulerMarkerX,self.RulerMarkerY)
else:
pass
def __initCanvas__(self):
self.RulerPointerTriangleCordsX = [15, 15, 20, 20, 25, 15]
self.RulerPointerTriangleCordsY = [15, 15, 20, 20, 15, 25]
self.RulerPointerTriangleX = self.create_polygon(self.RulerPointerTriangleCordsX, fill='red')

self.RulerPointerTriangleY = self.create_polygon(self.RulerPointerTriangleCordsY, fill='red')
self.canvasState = True
self.DrawRulersOnInit()

self.canvasContext = canvas.createCanvas(self,900,600)
self.canvasContext.Render()
# self.RulerPointer('j')
def removePreviusCache(self):
self.delete(self.self.photo_image)
del self.self.photo_image
def renderCanvas(self,Render):
print('h')
# self.delete(self.self.photo_image)
# del self.self.photo_image
# self.create_image(20, 20, anchor=tk.NW, image=Render)
#global self.photo_image
self.photo_image.config(data=Render)
print(self.photo_image)
# print(self.self.photo_image)
self.itemconfig(self.canvas_img, image=self.photo_image)
# self.update()

def RulerPointer(self,event):
self.RulerPointerTriangleCordsX = [15+event.x-self.const, 15, 20+event.x-self.const, 20, 25+event.x-self.const, 15]
self.RulerPointerTriangleCordsY = [15, 15+event.y-self.const, 20, 20+event.y-self.const, 15, 25+event.y-self.const]
self.coords(self.RulerPointerTriangleX, self.RulerPointerTriangleCordsX)
self.coords(self.RulerPointerTriangleY, self.RulerPointerTriangleCordsY)
def initMainCanvas(self,Data):
print('g')
#global self.photo_image
self.photo_image = tk.PhotoImage(data=Data)
self.canvas_img = self.create_image(20, 20, anchor=tk.NW, image=self.photo_image)
self.update()
21 changes: 21 additions & 0 deletions globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import tkinter as tk
from tkinter import ttk

#canvas imports
import skia
import pygame
from numpy import *
from pygame.locals import *
from contextlib import contextmanager
from PIL import Image, ImageTk

photo_image = None
APPDATA={
"version":"0.0.1",
"name":"Piraside",
"geometry":"1080x700"
}
CANVASX = 0
CANVASY = 0
global App
App = ''
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from window import window

if __name__ == '__main__':
App = window()
App.__initCanvas__()
App.mainloop()
56 changes: 56 additions & 0 deletions panes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from globals import *
from editor import editor
class ToolsPane(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#222', bd=0,highlightthickness=0,relief=tk.FLAT)

self.spacer = tk.Frame(self,bg='#222', bd=0,highlightthickness=0,relief=tk.FLAT,height=26)
self.spacer.pack(padx=0,pady=0)
self.label = tk.Button(self,bg="#333",bd=0,fg="#DDD",highlightthickness=0, text="=",width=1,height=1,relief=tk.FLAT)
self.label.pack(padx=0,pady=0)
class CanvasPane(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#111', bd=0,highlightthickness=0, relief=tk.FLAT)

self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=0)
self.rowconfigure(1,weight=1)

self.TAB = CanvasTabPane(self)
self.TAB.grid(row=0,column=0,sticky=tk.NSEW)

self.AREA = CanvasAreaPane(self)
self.AREA.grid(row=1,column=0,sticky=tk.NSEW)
class CanvasTabPane(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#444', bd=0,highlightthickness=0,relief=tk.FLAT,height=30)

self.tab1 = TabBtn(self)
self.tab1.pack(padx=0,pady=0,side=tk.LEFT)
self.tab2 = TabBtn(self)
self.tab2.pack(padx=0,pady=0,side=tk.LEFT)
class TabBtn(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#111', bd=0,highlightthickness=0,relief=tk.FLAT,height=40)
self.tab = tk.Button(self,bg="#111",bd=0,fg="#AAA",highlightthickness=0, text="wonderfool.jpg",height=0,relief=tk.FLAT,font=("Arial", 10))
self.close = tk.Button(self,bg="#111",bd=0,fg="#AAA",highlightthickness=0, text="x",width=1,height=0,relief=tk.FLAT, font=("Arial", 10))
self.tab.pack(side=tk.LEFT)
self.close.pack(side=tk.RIGHT)

class CanvasAreaPane(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#111', bd=0,highlightthickness=0,relief=tk.FLAT)
self.CANVAS = editor(self)
self.CANVAS.pack(fill=tk.BOTH,expand=True)
class LayersPane(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#222', bd=0,highlightthickness=0, relief=tk.FLAT)

self.label = tk.Label(self, text="Hello, World!")
self.label.pack()
43 changes: 43 additions & 0 deletions widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from globals import *
from panes import ToolsPane,CanvasPane,LayersPane

class MainPanedWindow(tk.PanedWindow):
def __init__(self, master):
super().__init__(master)
self.configure(self,bg="#333", orient=tk.HORIZONTAL,bd=0,borderwidth=0)

self.CanvasPaneWidget = CanvasPane(self)
self.add(self.CanvasPaneWidget)
self.paneconfigure(self.CanvasPaneWidget, minsize=800)

self.LayersPaneWidget = LayersPane(self)
self.add(self.LayersPaneWidget)
class SubWidget(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#222', relief=tk.FLAT)
self.ToolsPaneWidget = ToolsPane(self)
self.ToolsPaneWidget.pack(fill=tk.BOTH,expand=True)
class TopWidget(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#333', relief=tk.FLAT,height=40)

#Main widget
class widget(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='#222', bd=1, relief=tk.FLAT)
self.columnconfigure(0,weight=0)
self.columnconfigure(1,weight=1)
self.rowconfigure(0,weight=0)
self.rowconfigure(1,weight=1)

self.TopWidgetGroup = TopWidget(self)
self.TopWidgetGroup.grid(row=0,column=0,columnspan=2,sticky=tk.NSEW)

self.SubWidgetGroup = SubWidget(self)
self.SubWidgetGroup.grid(row=1,column=0,sticky=tk.NSEW)

self.MainPanedWindowWidgetGroup = MainPanedWindow(self)
self.MainPanedWindowWidgetGroup.grid(row=1,column=1,sticky=tk.NSEW)
Loading

0 comments on commit 4f42209

Please sign in to comment.