Skip to content

Instantly share code, notes, and snippets.

@Ronald-TR
Created March 16, 2018 15:49
Show Gist options
  • Save Ronald-TR/8fe7736b0c1e5581b91f62dd8a490cee to your computer and use it in GitHub Desktop.
Save Ronald-TR/8fe7736b0c1e5581b91f62dd8a490cee to your computer and use it in GitHub Desktop.
import os
import shutil
class Constantes:
def __init__(self, name):
self.name = name
def __add__(self, other):
aux = self.name + '+' + str(other)
return aux
def __sub__(self, other):
aux = self.name + '-' + str(other)
return aux
def __mul__(self, other):
aux = self.name + '*' + str(other)
return aux
def __div__(self, other):
aux = self.name + '/' + str(other)
return aux
def __str__(self):
return self.name
# CONSTANTES DE OBJETOS BAIXO
RECT = 'display.newRect'
EMITER = 'display.newEmiter'
IMAGE = 'display.newImage'
IMAGE_RECT = 'display.newImageRect'
TEXT = 'display.newText'
# DIMENSOES
CENTER_X = Constantes('display.contentCenterX')
CENTER_Y = Constantes('display.contentCenterY')
CONTENT_HEIGHT = Constantes('display.contentHeight')
CONTENT_WIDTH = Constantes('display.contentWidth')
DEFAULT_FONT = Constantes('native.SystemFont')
# setattr(CENTER_X, '__add__', add)
# setattr(CENTER_Y, '__add__', add)
# setattr(DEFAULT_FONT, '__add__', add)
class Text:
def __init__(self, text='', x=CENTER_X, y=CENTER_Y, width='', height='', font=DEFAULT_FONT):
self.text = f'"{text}"'
self.x = str(x)
self.y = str(y)
self.width_height = f'({width}, {height})' if width != '' and height != '' else ''
self.font = str(font)
def __str__(self):
args = ','.join([v for i, v in self.__dict__.items() if v != ''])
return f'{TEXT}({args})'
image_r_words = ['varname', 'filename']
image_args = ['filename', 'width', 'height']
class ImageRect:
def __init__(self, varname, image, width='', height=''):
shutil.copy(image.filename, os.path.dirname(display.path))
filename = image.filename.split('\\')[-1]
self.filename = f'"{filename}"'
self.width = str(width) if str(width) != '' else str(image.width)
self.height = str(height) if str(height) != '' else str(image.height)
self.varname = varname
self.x = ''
self.y = ''
def __str__(self):
def validate(i, v): return (v != '') and not (i in image_r_words)
code = '\n'.join([f'{self.varname}.{i}={v}' for i, v in self.__dict__.items() if validate(i, v)])
args = ','.join([v for i, v in self.__dict__.items() if (v != '') and (i in image_args)])
definition = f'local {self.varname} = {IMAGE_RECT}({args})\n'
# implementation = '\n'.join([v for i, v in self.__dict__.items() if i not in args])
return definition + code
class Display:
def __init__(self):
self.elements = []
self.path = ''
def add(self, element):
self.elements.append(element)
def compile(self):
with open(self.path, 'wb') as fp:
content = '\n'.join([e.__str__() for e in self.elements])
fp.write(content.encode())
if not hasattr(globals(), 'display'):
display = Display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment