class TextBox(Sprite): def __init__(self, ID, wh, loc, font, imgFolder): Sprite.__init__(self) self.theFont = font self.width, self.height = wh self.identity = ID self.text = "++++" self.image = pygame.Surface(wh) self.image.fill((255,255,255)) self.rect = self.image.get_rect() self.doneButton = Button(imgFolder, "textButton", (self.rect.left, self.rect.bottom - 166), "done", -1) self.doneButton.addText("Add Entry") self.image.blit(self.doneButton.getImage(), self.doneButton.getRect()) x,y = loc self.rect.top += y self.rect.left += x def setLocation(self, loc): x,y = loc self.rect.top += y self.rect.left += x def getIdentity(self): return self.identity def isClicked(self, xy): # need to scale the xy value to be relative to the textBox window x,y = xy x -= self.rect.left y -= self.rect.top xy = x,y if self.doneButton.getRect().collidepoint(xy): return self.text else: return None def addText(self, string): self.text = string textBox = TextBlock("doesntMatter", string, self.theFont, 15,60, self.height - 200) self.image.blit(textBox.getImage(), textBox.getRect()) def getRect(self): return self.rect def getImage(self): return self.image def scroll(self, dy): pass
def createImage(self): if self.imageCreated: self.image.fill((0,0,0)) self.allTextBoxes = [] self.allButtons = [] totalHeight = 10 i = 0 doneCount = 0 #print "FIELDS:", self.fields[0] #print " ", self.fields[1] #tally the height for f in self.fields[0]: self.allTextBoxes.append(TextBlock(f + ":", f + ":", self.theFont, totalHeight)) if self.fields[1][i]: self.allButtons.append(Button(self.imgFolder, "textButton", (self.allTextBoxes[i].getImage().get_width() + 15, totalHeight), f, -1)) self.allButtons[i].addText("Add Data") else: self.allButtons.append(Button(self.imgFolder, "checkBox", (self.allTextBoxes[i].getImage().get_width() + 15, totalHeight), "nothing", -1)) doneCount += 1 totalHeight += self.allTextBoxes[i].getImage().get_height() + self.belowTextBorder i+=1 totalHeight += 120 self.image = pygame.Surface((400, totalHeight)) for button in self.allButtons: self.image.blit(button.getImage(), button.getRect()) for text in self.allTextBoxes: self.image.blit(text.getImage(), text.getRect()) if doneCount >= 5: doneButton = Button(self.imgFolder, "textButton", (150, totalHeight - 100), "submitRecipe", -1) doneButton.addText("Submit Recipe") self.allButtons.append(doneButton) self.image.blit(doneButton.getImage(), doneButton.getRect()) self.imageCreated = True
class Row(Sprite): def __init__(self, objectName, font, locY, imgdir, mode, ing=None): pygame.sprite.Sprite.__init__(self) col = 255, 255, 255 self.locationY = locY self.name = objectName self.theFont = font self.imgFolder = imgdir self.nameText = self.theFont.render(self.name, False, col) self.image = pygame.Surface((700, 30)) # self.nameText.get_height() + 2)) # this stores the ingredient if we are in "groceryListViewer" mode # should not be used by recipeViewer, but we store it here anyway so python doesn't complain # this will be useful later for scaling the amount stored self.ingredient = ing if mode == "recipeViewer": ## in this mode, we need the buttons to expand recipes and add them to the shopping cart self.expandButton = Button(self.imgFolder, "expandButton", (10, 0), "expand@" + self.name, -1) self.image.blit(self.expandButton.getImage(), self.expandButton.getRect()) self.groceryListButton = Button( self.imgFolder, "addToGroceryListButton", (self.nameText.get_width() + self.expandButton.getImage().get_width() + 25, 0), "addToGroceryList@" + self.name, -1, ) self.image.blit(self.groceryListButton.getImage(), self.groceryListButton.getRect()) self.writeToDesktopButton = Button( self.imgFolder, "writeToDesktop", (self.groceryListButton.getRect().right + 15, 0), "writeToDesktop@" + self.name, -1, ) self.image.blit(self.writeToDesktopButton.getImage(), self.writeToDesktopButton.getRect()) ## we put the name text in a different spot if we have the expand button self.image.blit(self.nameText, (self.expandButton.getImage().get_width() + 15, 0)) self.allButtons = [self.expandButton, self.groceryListButton, self.writeToDesktopButton] elif mode == "groceryListViewer": self.image.blit(self.nameText, (15, 0)) ## put remove item and scale quantity boxes in here... elif mode == "recipeDeleter": self.expandButton = Button(self.imgFolder, "expandButton", (10, 0), "expand@" + self.name, -1) self.image.blit(self.expandButton.getImage(), self.expandButton.getRect()) self.image.blit(self.nameText, (self.expandButton.getImage().get_width() + 15, 0)) self.deleteButton = Button( self.imgFolder, "delete", (self.nameText.get_width() + self.expandButton.getImage().get_width() + 25, 0), "delete@" + self.name, -1, ) self.image.blit(self.deleteButton.getImage(), self.deleteButton.getRect()) self.allButtons = [self.expandButton, self.deleteButton] self.image.convert() self.rect = self.image.get_rect() x, y = self.rect.topleft y += locY self.rect.topleft = x, y def toggleButton(self, buttonType): ## might have been smarter to just code in the toggling to the button # so we just call button.toggle # and within the button class it already knows how to toggle itself if buttonType == "expand": # pygame.draw.rect(self.image, (0,0,0), self.expandButton.getRect(), 0) buttonIDs = self.expandButton.getIdentity().split("@") self.allButtons.remove(self.expandButton) self.expandButton = Button( self.imgFolder, "unExpandButton", (self.expandButton.getRect().topleft), "unExpand@" + buttonIDs[1], -1 ) self.allButtons.append(self.expandButton) self.image.blit(self.expandButton.getImage(), self.expandButton.getRect()) elif buttonType == "unExpand": buttonIDs = self.expandButton.getIdentity().split("@") self.allButtons.remove(self.expandButton) self.expandButton = Button( self.imgFolder, "expandButton", (self.expandButton.getRect().topleft), "expand@" + buttonIDs[1], -1 ) self.allButtons.append(self.expandButton) self.image.blit(self.expandButton.getImage(), self.expandButton.getRect()) elif buttonType == "groceryButton": # pygame.draw.rect(self.image, (0,255,0), self.groceryListButton.getRect(), 0) self.allButtons.remove(self.groceryListButton) buttonIDs = self.groceryListButton.getIdentity().split("@") if buttonIDs[0] == "addToGroceryList": self.groceryListButton = Button( self.imgFolder, "removeFromGroceryListButton", (self.groceryListButton.getRect().topleft), "removeFromGroceryList@" + buttonIDs[1], -1, ) else: self.groceryListButton = Button( self.imgFolder, "addToGroceryListButton", (self.groceryListButton.getRect().topleft), "addToGroceryList@" + buttonIDs[1], -1, ) self.image.blit(self.groceryListButton.getImage(), self.groceryListButton.getRect().topleft) self.allButtons.append(self.groceryListButton) elif buttonType == "deleteButton": # pygame.draw.rect(self.image, (0,255,0), self.deleteButton.getRect(), 0) self.allButtons.remove(self.deleteButton) self.deleteButton = Button( self.imgFolder, "deleted", (self.deleteButton.getRect().topleft), "pointlessID", -1 ) self.image.blit(self.deleteButton.getImage(), self.deleteButton.getRect().topleft) self.allButtons.append(self.deleteButton) def isAddedToGroceryList(self): buttonIDs = self.groceryListButton.getIdentity().split("@") if buttonIDs[0] == "addToGroceryList": return False else: return True def getImage(self): return self.image def getRect(self): return self.rect def getBlitPos(self): x, y = self.rect.topleft x += self.getLeftBorder() return x, y def getName(self): return self.name def get_height(self): return self.image.get_height() def getLeftBorder(self): return 0 def updateY(self, dy): self.rect = self.rect.move(0, dy) def collideButtons(self, xy): # need to convert the xy location of the mouse click to be relative to the top left of the row x, y = xy y -= self.rect.top xy = x, y for button in self.allButtons: if button.getRect().collidepoint(xy): return button return None ## this method is only for groceryListViewer # all scaling done to the ingredient list will be stored in this rows "ingredient" object # we then return the object for printing to file or for storing the groceryList def getModifiedIngredient(self): # print "returning : ", self.ingredient return self.ingredient