Init
This commit is contained in:
commit
4265c306b9
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__
|
41
dariuino-ide.py
Executable file
41
dariuino-ide.py
Executable file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from src.gui import *
|
||||
|
||||
|
||||
class Argument:
|
||||
INTEGER = 1
|
||||
STRING = 2
|
||||
|
||||
def __init__(self, name, type):
|
||||
self.name = name
|
||||
self.type = type
|
||||
|
||||
|
||||
class Statement:
|
||||
def __init__(self, name):
|
||||
self.arguments = []
|
||||
|
||||
def add_argument(self, argument):
|
||||
self.arguments.append(argument)
|
||||
|
||||
|
||||
def main():
|
||||
resources = ResourceContainer.from_json_file('res/resources.json')
|
||||
|
||||
container = Container(Position(0, 0), Dimensions(600, 600))
|
||||
container.set_background_image(resources.get_image('background'))
|
||||
|
||||
v = Visualization(resources.get_image('sprite'), Position(100, 300))
|
||||
|
||||
container.add_visualization(v)
|
||||
|
||||
ui = UI('Dariuino IDE', Dimensions(1920, 1080))
|
||||
ui.add_container(container)
|
||||
ui.init()
|
||||
ui.update()
|
||||
ui.main()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
6
res/resources.json
Normal file
6
res/resources.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"images": {
|
||||
"background": "/home/mal/Bilder/65a671cb20fb3d0c5774aafc5e5971c4.jpg",
|
||||
"sprite": "/home/mal/Bilder/Icons/wow-moka.png"
|
||||
}
|
||||
}
|
233
src/gui.py
Executable file
233
src/gui.py
Executable file
@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import pygame
|
||||
import json
|
||||
|
||||
|
||||
class Position:
|
||||
def __init__(self, x = 0, y = 0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def as_tuple(self):
|
||||
return (self.x, self.y)
|
||||
|
||||
@staticmethod
|
||||
def from_tuple(tuple):
|
||||
return Position(tuple[0], tuple[1])
|
||||
|
||||
|
||||
class Color:
|
||||
def __init__(self, red, green, blue):
|
||||
self.red = red
|
||||
self.green = green
|
||||
self.blue = blue
|
||||
|
||||
def as_tuple(self):
|
||||
return (self.red, self.green, self.blue)
|
||||
|
||||
|
||||
class Dimensions:
|
||||
def __init__(self, width, height):
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
def as_tuple(self):
|
||||
return (self.width, self.height)
|
||||
|
||||
@staticmethod
|
||||
def from_tuple(tuple):
|
||||
return Dimensions(tuple[0], tuple[1])
|
||||
|
||||
|
||||
class Image:
|
||||
def __init__(self, path = None):
|
||||
self.surface = None
|
||||
|
||||
if path is not None:
|
||||
self.surface = pygame.image.load(path)
|
||||
|
||||
def get_dimensions(self) -> Dimensions:
|
||||
return Dimensions.from_tuple(self.surface.get_size())
|
||||
|
||||
|
||||
class Canvas:
|
||||
def __init__(self, size: Dimensions):
|
||||
self.surface = pygame.Surface(size.as_tuple(), pygame.SRCALPHA)
|
||||
|
||||
def draw_image(self, image: Image, position: Position):
|
||||
self.surface.blit(image.surface, position.as_tuple())
|
||||
|
||||
def clear(self, color = Color(0, 0, 0)):
|
||||
self.surface.fill(color.as_tuple())
|
||||
|
||||
def get_image(self):
|
||||
image = Image()
|
||||
image.surface = self.surface
|
||||
|
||||
return image
|
||||
|
||||
class Screen:
|
||||
def __init__(self, title: str, resolution: Dimensions):
|
||||
self.resolution = resolution
|
||||
self.screen = pygame.display.set_mode(resolution.as_tuple(), pygame.NOFRAME | pygame.FULLSCREEN | pygame.DOUBLEBUF)
|
||||
pygame.display.set_caption(title)
|
||||
|
||||
def get_canvas(self):
|
||||
canvas = Canvas(self.resolution)
|
||||
canvas.surface = pygame.display.get_surface()
|
||||
|
||||
return canvas
|
||||
|
||||
def get_image(self):
|
||||
image = Image()
|
||||
|
||||
def update(self):
|
||||
pygame.dispay.flip()
|
||||
|
||||
@staticmethod
|
||||
def get_screen_resolutions():
|
||||
resolutions = []
|
||||
|
||||
for mode in pygame.display.list_modes():
|
||||
resolutions.append(Dimensions.from_tuple(mode))
|
||||
|
||||
return resolutions
|
||||
|
||||
|
||||
class Visualization():
|
||||
def __init__(self, surface: Canvas, position: Position):
|
||||
self.surface = surface
|
||||
self.position = position
|
||||
|
||||
def draw(self, canvas: Canvas):
|
||||
canvas.draw_image(self.surface, self.position)
|
||||
|
||||
def is_inside(self, position: Position):
|
||||
if position.x < self.position.x:
|
||||
return False
|
||||
|
||||
if position.y < self.position.y:
|
||||
return False
|
||||
|
||||
dimensions = self.surface.get_dimensions()
|
||||
|
||||
if position.x > self.position.x + dimensions.width:
|
||||
return False
|
||||
|
||||
if position.y > self.position.y + dimensions.height:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class Clickable(Visualization):
|
||||
def __init__(self, surface: Canvas, position: Position, on_click_callback: callable):
|
||||
Visualization.__init__(self, surface)
|
||||
self.on_click = on_click_callback
|
||||
|
||||
def click(self):
|
||||
self.on_click()
|
||||
|
||||
|
||||
class Container(Visualization):
|
||||
ROWS = 0
|
||||
COLUMNS = 1
|
||||
|
||||
def __init__(self, position: Position, dimensions: Dimensions):
|
||||
Visualization.__init__(self, Canvas(dimensions), position)
|
||||
self.background_image = None
|
||||
self.background_color = Color(0, 0, 0)
|
||||
self.visualizations = []
|
||||
self.clickables = []
|
||||
|
||||
def set_background_image(self, image: Image):
|
||||
self.background_image = image
|
||||
|
||||
def render(self):
|
||||
self.surface.clear(self.background_color)
|
||||
|
||||
if self.background_image is not None:
|
||||
self.surface.draw_image(self.background_image, Dimensions(0, 0))
|
||||
|
||||
for visualization in self.visualizations:
|
||||
visualization.draw(self.surface)
|
||||
|
||||
def add_visualization(self, visualization: Visualization):
|
||||
self.visualizations.append(visualization)
|
||||
|
||||
def add_clickable(self, clickable: Clickable):
|
||||
self.add_visualization(clickable)
|
||||
self.clickables.append(clickable)
|
||||
|
||||
|
||||
class UI:
|
||||
def __init__(self, title: str, resolution: Dimensions):
|
||||
pygame.init()
|
||||
|
||||
self.is_running = True
|
||||
self.clock = pygame.time.Clock()
|
||||
self.screen = Screen(title, resolution)
|
||||
self.canvas = self.screen.get_canvas()
|
||||
|
||||
self.containers = []
|
||||
|
||||
def init(self):
|
||||
pass
|
||||
|
||||
def update(self):
|
||||
self.canvas.clear()
|
||||
|
||||
for container in self.containers:
|
||||
container.render()
|
||||
container.draw(self.canvas)
|
||||
|
||||
def add_container(self, container: Container):
|
||||
self.containers.append(container)
|
||||
|
||||
def exit(self):
|
||||
pygame.quit()
|
||||
self.is_running = False
|
||||
|
||||
def main(self):
|
||||
while self.is_running:
|
||||
self.clock.tick()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.exit()
|
||||
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
self.exit()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
class IconButton(Clickable):
|
||||
def __init__(self, icon: pygame.Surface, icon_hover = None):
|
||||
self.icon = icon
|
||||
self.icon_hover = icon_hover
|
||||
|
||||
|
||||
class ResourceContainer:
|
||||
def __init__(self):
|
||||
self.images = {}
|
||||
|
||||
def add_image(self, name, url):
|
||||
self.images[name] = Image(url)
|
||||
|
||||
def get_image(self, name):
|
||||
return self.images[name]
|
||||
|
||||
@staticmethod
|
||||
def from_json_file(json_file):
|
||||
with open(json_file, 'r') as file:
|
||||
data = json.loads(file.read())
|
||||
|
||||
resources = ResourceContainer()
|
||||
|
||||
for image in data['images']:
|
||||
resources.add_image(image, data['images'][image])
|
||||
|
||||
return resources
|
Loading…
Reference in New Issue
Block a user