def Move( self, newIndex ): #components can have their update function skipped on accident if ran in another comp.update self.__Throw("Object.Move") SetObjEdited(self) children = self.GetParentChildren() if (newIndex < 0): Functions.Err("Object.Move failed, index less than 0") elif (newIndex > len(children)): Functions.Warn( "Object.Move incorrect, index is greater than parent.children count" ) oldIndex = self.index self.__index = newIndex del children._ProtectedList__val[oldIndex] children._ProtectedList__val.insert(newIndex, self) i = oldIndex if (newIndex > oldIndex): while (i < newIndex): children[i]._Object__index = i i = i + 1 elif (newIndex < oldIndex): while (i >= newIndex): children[i]._Object__index = i i = i - 1
def __Throw(self, action=""): if (self.__destroyed): if (action == ""): Functions.Err("Object was destroyed") else: Functions.Err("Object was destroyed while " + action + "() was happening")
def SetColor(value, varName, className): Functions.TypeCheck(value, [tuple, type(None)], varName, className) output = None if (value != None): temp = [] max = -1 for i in value: if (i < 0): Functions.Err("color cant have a negative value") if (i > max): max = i temp.append(i) value = temp if (max > 255): for i in range(len(value)): value[i] = value[i] / max * 255 if (value == None or len(value) == 4): output = (float(value[0]), float(value[1]), float(value[2]), float(value[3])) elif (len(value) == 3): output = (float(value[0]), float(value[1]), float(value[2]), 255.0) else: Functions.Err( className + "." + varName + " must have a length of 3 or 4 in RGBA or RGB format") return output
def RectItemHelp(self, i): options = ["left", "top", "width", "height"] if (type(i) is not int): Functions.Err("in PythUnity.Rect[index], index must be a " + str(int) + " not " + str(type(i))) if (i > 4 or i < 0): Functions.Err( "in PythUnity.Rect[index], index must be between values 0 and " + str(len(options))) return options
def __setitem__(self, i, newvalue): if (type(newvalue) is not int and type(newvalue) is not float): Functions.Err( "in PythUnity.Rect[index] = newValue, newValue must be a " + str(int) + " or " + str(float) + " not " + str(type(newvalue))) options = RectItemHelp(self, i) attr = setattr(self, options[i], newvalue)
def DelComp(self, index): self.__Throw("Object.DelComp") if (len(self.__components) > index): del self.__components._ProtectedList__val[index] else: Functions.Err( "Object.DelComp failed, index greater than comp index")
def text(self, value): self.__edited = True Functions.TypeCheck(value, [String, type(None)], "text") self.__text = value self.text._String__owner = self self.text._String__imageChanged = True TransformText(self)
def height(self, value): Functions.TypeCheck(value, [float, int], "height", "Rect") self.__height = float(value) UpdateRects(self) if (self.__owner != None and not self.__global): self.__owner._Object__transformed = True TransformText(self.__owner) SetEdited(self)
def rect(self, value): SetObjEdited(self) Functions.TypeCheck(value, Rect, "rect") self.__rect = value self.__rect._Rect__owner = self self.__rect._Rect__global = False UpdateRects(self.__rect) self.__transformed = True TransformText(self)
def TestFuncLen(func, name, neededArgs=["self"], className="Button"): if (func != None): length = len(inspect.getargspec(func)[0]) if (length != len(neededArgs)): Functions.Err("PythUnity." + className + "." + name + " must have an argument length of " + str(len(neededArgs)) + " not " + str(length) + "\nfunction: " + str(func) + "\nneeded arguments: " + str(neededArgs) + "\nrecieved arguments: " + str(inspect.getargspec(func)[0]))
def __init__(self, rect, image, onClick=None, onDrag=None, onClickOff=None, onScroll=None, onHover=None, onHoverOff=None, onHoverOn=None, enableDragOff=False, clickGroup=0, fitImage=False, effect=None): self.__destroyed = False self.__parent = None self.__children = ProtectedList("children", "Object", False, False, False, False) self.__variables = {} self.__image = None self.__transformedImage = None self.__text = None self.__color = None self.__oldPos = (0, 0, 0, 0) self.__edited = True self.__components = ProtectedList("components", "Object", False, False, False, False) self.__clickGroup = None self.__velocity = (0, 0) self.__index = None self.__rect = None self.__transformed = False self.__transformedColor = False self.__updateLocal = False self.__updateGlobal = False self.renderOptions = RenderOptions(fitImage=fitImage, effect=effect) self.__globalRect = Rect(0, 0, 0, 0) self.__globalRect._Rect__owner = self self.__globalRect._Rect__global = True self.__enabled = True self.rect = rect self.clickGroup = clickGroup if (type(image) is tuple): self.color = image elif (type(image) is String): self.text = image.Copy() elif (type(image) is pygame.Surface): self.image = image if onClick != None or onDrag != None or onScroll != None: self.__button = Button(onClick, onDrag, onClickOff, onScroll, onHover, onHoverOff, onHoverOn, enableDragOff) else: self.__button = None Functions.AddObject(self)
def onClick(self, value): Functions.TypeCheck( value, [types.FunctionType, type(None)], "onClick", "Button") TestFuncLen(value, "onClick", ["self", "string_mouseButton"]) self.__onClick = value
def enableDragOff(self, value): Functions.TypeCheck(value, bool, "enableDragOff", "Button") self.__enableDragOff = value
def left(self, value): Functions.TypeCheck(value, [float, int], "left", "Rect") self.__left = float(value) UpdateRects(self) SetEdited(self)
def top(self, value): Functions.TypeCheck(value, [float, int], "top", "Rect") self.__top = float(value) UpdateRects(self) SetEdited(self)
def onHoverOff(self, value): Functions.TypeCheck( value, [types.FunctionType, type(None)], "onHoverOff", "Button") TestFuncLen(value, "onHoverOff", ["self", "string_reason"]) self.__onHoverOff = value
def font(self, value): Functions.TypeCheck(value, str, "font", "String") self.__font = value self.__rowChanged = True SetEdited(self)
def alignment(self, value): Functions.TypeCheck(value, int, "alignment", "String") self.__alignment = value self.__imageChanged = True SetEdited(self)
def button(self, value): Functions.TypeCheck(value, [Button, type(None)], "button") self.__button = value
def addDash(self, value): Functions.TypeCheck(value, bool, "addDash", "String") self.__addDash = value self.__rowChanged = True
def rows(self, value): Functions.Err("can't set PythUnity.rows, you can only get it")
def onDrag(self, value): Functions.TypeCheck( value, [types.FunctionType, type(None)], "onDrag", "Button") TestFuncLen(value, "onDrag") self.__onDrag = value
def sizer(self, value): Functions.Err("can't set PythUnity.sizer, you can only get it")
def onHoverOn(self, value): Functions.TypeCheck( value, [types.FunctionType, type(None)], "onHoverOn", "Button") TestFuncLen(value, "onHoverOn") self.__onHoverOn = value
def fitImage(self, value): Functions.TypeCheck(value, type(False), "fitImage", "RenderOptions") self.__fitImage = value if (self.__owner != None): self.__owner._Object__imageChanged = True SetEdited(self)
def fontSize(self, value): Functions.TypeCheck(value, [int, float], "fontSize", "String") self.__fontSize = value self.__rowChanged = True SetEdited(self)
def hovering(self, value): Functions.Err( "You cant set PythUnity.Button.hovering, you can only get it")
def text(self, value): Functions.TypeCheck(value, str, "text", "String") self.__text = value self.__rowChanged = True SetEdited(self)
def maxRows(self, value): Functions.TypeCheck(value, int, "maxRows", "String") self.__maxRows = value self.__rowChanged = True
def onScroll(self, value): Functions.TypeCheck( value, [types.FunctionType, type(None)], "onScroll", "Button") TestFuncLen(value, "onScroll", ["self", "int_direction"]) self.__onScroll = value