Exemplo n.º 1
0
class CalendarGuiMonth(DirectFrame):
    __module__ = __name__
    notify = directNotify.newCategory('CalendarGuiMonth')

    def __init__(self,
                 parent,
                 startingDateTime,
                 scale=1.0,
                 pos=(0, 0, -0.1),
                 dayClickCallback=None,
                 onlyFutureDaysClickable=False,
                 onlyFutureMonthsClickable=False):
        self.startDate = startingDateTime
        self.curDate = startingDateTime
        self.dayClickCallback = dayClickCallback
        self.onlyFutureDaysClickable = onlyFutureDaysClickable
        self.onlyFutureMonthsClickable = onlyFutureMonthsClickable
        if self.onlyFutureDaysClickable:
            self.onlyFutureMonthsClickable = True
        DirectFrame.__init__(self, parent=parent, scale=scale, pos=pos)
        self.showMarkers = base.config.GetBool('show-calendar-markers', 0)
        self.load()
        self.createGuiObjects()
        self.lastSelectedDate = None
        self.accept('clickedOnDay', self.clickedOnDay)
        return

    def createDummyLocators(self):
        self.monthLocator = self.attachNewNode('monthLocator')
        self.monthLocator.setZ(0.6)
        self.weekDayLocators = []
        for i in xrange(7):
            self.weekDayLocators.append(
                self.attachNewNode('weekDayLocator-%d' % i))
            self.weekDayLocators[i].setZ(0.5)
            self.weekDayLocators[i].setX(i * 0.24 + -0.75)

        dayTopLeftX = -0.8
        dayTopLeftZ = 0.4
        self.dayLocators = []
        for row in xrange(6):
            oneWeek = []
            for col in xrange(7):
                newDayLoc = self.attachNewNode('dayLocator-row-%d-col-%d' %
                                               (row, col))
                newDayLoc.setX(col * 0.24 + dayTopLeftX)
                newDayLoc.setZ(row * -0.18 + dayTopLeftZ)
                oneWeek.append(newDayLoc)

            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.attachNewNode('monthLeft')
        self.monthLeftLocator.setPos(-0.3, 0, 0.65)
        self.monthRightLocator = self.attachNewNode('monthRight')
        self.monthRightLocator.setPos(0.3, 0, 0.65)

    def attachMarker(self, parent, scale=0.01, color=(1, 0, 0)):
        if self.showMarkers:
            marker = loader.loadModel('phase_3/models/misc/sphere')
            marker.reparentTo(parent)
            marker.setScale(scale)
            marker.setColor(*color)

    def load(self):
        monthAsset = loader.loadModel(
            'phase_4/models/parties/tt_m_gui_sbk_calendar')
        monthAsset.reparentTo(self)
        self.monthLocator = self.find('**/locator_month/locator_month')
        self.attachMarker(self.monthLocator)
        self.weekDayLocators = []
        for weekday in ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'):
            weekDayLoc = self.find('**/loc_%s' % weekday)
            self.weekDayLocators.append(weekDayLoc)
            self.attachMarker(weekDayLoc)

        self.dayLocators = []
        for row in xrange(6):
            oneWeek = []
            for col in xrange(7):
                newDayLoc = self.find('**/loc_box_%s_%s' % (row, col))
                oneWeek.append(newDayLoc)

            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.find('**/locator_month_arrowL')
        self.monthRightLocator = self.find('**/locator_month_arrowR')
        self.filterLocator = self.find('**/locator_filter')
        self.filterLocatorArrowUp = self.find('**/locator_filter_arrowTop')
        self.filterLocatorArrowDown = self.find(
            '**/locator_filter_arrowBottom')
        self.yearLocator = self.attachNewNode('yearLocator')
        self.yearLocator.setPos(self.monthLocator, 0, 0, -0.03)

    def createGuiObjects(self):
        self.monthLabel = DirectLabel(
            parent=self.monthLocator,
            relief=None,
            text=TTLocalizer.Months[self.startDate.month],
            text_scale=0.075,
            text_font=ToontownGlobals.getMinnieFont(),
            text_fg=(40 / 255.0, 140 / 255.0, 246 / 255.0, 1.0))
        self.yearLabel = DirectLabel(parent=self.yearLocator,
                                     relief=None,
                                     text=str(self.startDate.year),
                                     text_scale=0.03,
                                     text_font=ToontownGlobals.getMinnieFont(),
                                     text_fg=(140 / 255.0, 140 / 255.0,
                                              246 / 255.0, 1.0))
        self.weekdayLabels = []
        for posIndex in xrange(7):
            adjustedNameIndex = (posIndex - 1) % 7
            self.weekdayLabels.append(
                DirectLabel(parent=self.weekDayLocators[posIndex],
                            relief=None,
                            text=TTLocalizer.DayNamesAbbrev[adjustedNameIndex],
                            text_font=ToontownGlobals.getInterfaceFont(),
                            text_fg=(255 / 255.0, 146 / 255.0, 113 / 255.0,
                                     1.0),
                            text_scale=0.05))

        self.createGuiDays()
        arrowUp = self.find('**/month_arrowR_up')
        arrowDown = self.find('**/month_arrowR_down')
        arrowHover = self.find('**/month_arrowR_hover')
        self.monthLeftArrow = DirectButton(parent=self.monthLeftLocator,
                                           relief=None,
                                           image=(arrowUp, arrowDown,
                                                  arrowHover, arrowUp),
                                           image3_color=Vec4(1, 1, 1, 0.5),
                                           scale=(-1.0, 1.0, 1.0),
                                           command=self.__doMonthLeft)
        if self.onlyFutureMonthsClickable:
            self.monthLeftArrow.hide()
        self.monthRightArrow = DirectButton(parent=self.monthRightLocator,
                                            relief=None,
                                            image=(arrowUp, arrowDown,
                                                   arrowHover, arrowUp),
                                            image3_color=Vec4(1, 1, 1, 0.5),
                                            command=self.__doMonthRight)

        def makeLabel(itemName, itemNum, *extraArgs):
            return DirectLabel(text=itemName,
                               frameColor=(0, 0, 0, 0),
                               text_scale=0.04)

        gui = loader.loadModel(
            'phase_4/models/parties/tt_m_gui_sbk_calendar_box')
        arrowUp = gui.find('**/downScroll_up')
        arrowDown = gui.find('**/downScroll_down')
        arrowHover = gui.find('**/downScroll_hover')
        filterLocatorUpPos = self.filterLocatorArrowUp.getPos(
            self.filterLocator)
        filterLocatorDownPos = self.filterLocatorArrowDown.getPos(
            self.filterLocator)
        self.filterList = DirectScrolledList(
            parent=self.filterLocator,
            relief=None,
            pos=(0, 0, 0),
            image=None,
            text_scale=0.025,
            incButton_image=(arrowUp, arrowDown, arrowHover, arrowUp),
            incButton_relief=None,
            incButton_pos=filterLocatorDownPos,
            incButton_image3_color=Vec4(1, 1, 1, 0.2),
            incButtonCallback=self.filterChanged,
            decButton_image=(arrowUp, arrowDown, arrowHover, arrowUp),
            decButton_relief=None,
            decButton_pos=filterLocatorUpPos,
            decButton_scale=(1, 1, -1),
            decButton_image3_color=Vec4(1, 1, 1, 0.2),
            decButtonCallback=self.filterChanged,
            numItemsVisible=1,
            itemMakeFunction=makeLabel,
            items=[
                TTLocalizer.CalendarShowAll,
                TTLocalizer.CalendarShowOnlyHolidays,
                TTLocalizer.CalendarShowOnlyParties
            ],
            itemFrame_frameSize=(-0.2, 0.2, -0.02, 0.05),
            itemFrame_frameColor=(0, 0, 0, 0))
        gui.removeNode()
        return

    def getTopLeftDate(self):
        firstOfTheMonth = self.curDate.replace(day=1)
        daysAwayFromSunday = (firstOfTheMonth.weekday() - 6) % 7
        topLeftDate = firstOfTheMonth + timedelta(days=-daysAwayFromSunday)
        return topLeftDate

    def createGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        curDate = topLeftDate
        self.guiDays = []
        for row in self.dayLocators:
            for oneLocator in row:
                self.guiDays.append(
                    CalendarGuiDay(oneLocator, curDate, self.curDate,
                                   self.dayClickCallback,
                                   self.onlyFutureDaysClickable))
                curDate += timedelta(days=1)

    def changeDateForGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        guiDayDate = topLeftDate
        for guiDay in self.guiDays:
            guiDay.changeDate(self.curDate, guiDayDate)
            guiDayDate += timedelta(days=1)

    def changeMonth(self, monthChange):
        if monthChange != 0:
            newMonth = self.curDate.month + monthChange
            newYear = self.curDate.year
            while newMonth > 12:
                newYear += 1
                newMonth -= 12

            while newMonth < 1:
                if newYear - 1 > 1899:
                    newMonth += 12
                    newYear -= 1
                else:
                    newMonth += 1

            self.curDate = datetime(newYear, newMonth, 1,
                                    self.curDate.time().hour,
                                    self.curDate.time().minute,
                                    self.curDate.time().second,
                                    self.curDate.time().microsecond,
                                    self.curDate.tzinfo)
        self.monthLabel['text'] = (TTLocalizer.Months[self.curDate.month], )
        self.yearLabel['text'] = (str(self.curDate.year), )
        startTime = globalClock.getRealTime()
        self.changeDateForGuiDays()
        endTime = globalClock.getRealTime()
        self.notify.debug('changeDate took %f seconds' % (endTime - startTime))
        self.updateSelectedDate()
        if monthChange != 0:
            if self.onlyFutureMonthsClickable and newMonth == self.startDate.month and newYear == self.startDate.year:
                self.monthLeftArrow.hide()

    def __doMonthLeft(self):
        self.changeMonth(-1)

    def __doMonthRight(self):
        self.monthLeftArrow.show()
        self.changeMonth(1)

    def destroy(self):
        self.ignoreAll()
        self.dayClickCallback = None
        self.monthLeftArrow.destroy()
        self.monthRightArrow.destroy()
        for day in self.guiDays:
            if day is not None:
                day.destroy()
            day = None

        self.filterList.destroy()
        DirectFrame.destroy(self)
        return

    def clickedOnDay(self, dayDate):
        self.lastSelectedDate = dayDate
        self.updateSelectedDate()

    def updateSelectedDate(self):
        if self.lastSelectedDate:
            for oneGuiDay in self.guiDays:
                if oneGuiDay.myDate.date() == self.lastSelectedDate:
                    oneGuiDay.updateSelected(True)
                else:
                    oneGuiDay.updateSelected(False)

    def clearSelectedDay(self):
        for oneGuiDay in self.guiDays:
            oneGuiDay.updateSelected(False)

    def filterChanged(self):
        newFilter = self.filterList.getSelectedIndex()
        for guiDay in self.guiDays:
            guiDay.changeFilter(newFilter)
Exemplo n.º 2
0
class PartyEditor(DirectObject, FSM):
    """
    This class creates the grid and scrolled list needed for players to
    drag and drop activities and decorations onto their party grounds.
    """
    notify = directNotify.newCategory("PartyEditor")

    def __init__(self, partyPlanner, parent):
        FSM.__init__(self, self.__class__.__name__)
        self.partyPlanner = partyPlanner
        self.parent = parent
        self.partyEditorGrid = PartyEditorGrid(self)
        self.currentElement = None

        self.defaultTransitions = {
            "Hidden": ["Idle", "Cleanup"],
            "Idle": ["DraggingElement", "Hidden", "Cleanup"],
            "DraggingElement":
            ["Idle", "DraggingElement", "Hidden", "Cleanup"],
            "Cleanup": [],
        }
        self.initElementList()
        self.initPartyClock()
        self.initTrashCan()

    def initElementList(self):
        self.activityIconsModel = loader.loadModel(
            "phase_4/models/parties/eventSignIcons")
        self.decorationModels = loader.loadModel(
            "phase_4/models/parties/partyDecorations")
        pos = self.partyPlanner.gui.find(
            "**/step_05_activitiesIcon_locator").getPos()
        self.elementList = DirectScrolledList(
            parent=self.parent,
            relief=None,
            # inc and dec are DirectButtons
            decButton_image=(
                self.partyPlanner.gui.find("**/activitiesButtonUp_up"),
                self.partyPlanner.gui.find("**/activitiesButtonUp_down"),
                self.partyPlanner.gui.find("**/activitiesButtonUp_rollover"),
                self.partyPlanner.gui.find("**/activitiesButtonUp_inactive"),
            ),
            decButton_relief=None,
            decButton_pos=(-0.05, 0.0, -0.38),
            incButton_image=(
                self.partyPlanner.gui.find("**/activitiesButtonDown_up"),
                self.partyPlanner.gui.find("**/activitiesButtonDown_down"),
                self.partyPlanner.gui.find("**/activitiesButtonDown_rollover"),
                self.partyPlanner.gui.find("**/activitiesButtonDown_inactive"),
            ),
            incButton_relief=None,
            incButton_pos=(-0.05, 0.0, -0.94),

            # itemFrame is a DirectFrame
            itemFrame_pos=(pos[0], pos[1], pos[2] + 0.04),
            itemFrame_relief=None,
            # each item is a button with text on it
            numItemsVisible=1,
            items=[],
        )

        for activityId in PartyGlobals.PartyEditorActivityOrder:
            if activityId in PartyGlobals.VictoryPartyActivityIds:
                holidayIds = base.cr.newsManager.getHolidayIdList()
                if ToontownGlobals.VICTORY_PARTY_HOLIDAY in holidayIds:
                    pele = PartyEditorListElement(self, activityId)
                    self.elementList.addItem(pele)
            elif activityId in PartyGlobals.VictoryPartyReplacementActivityIds:
                holidayIds = base.cr.newsManager.getHolidayIdList()
                if not ToontownGlobals.VICTORY_PARTY_HOLIDAY in holidayIds:
                    pele = PartyEditorListElement(self, activityId)
                    self.elementList.addItem(pele)
            else:
                pele = PartyEditorListElement(self, activityId)
                self.elementList.addItem(pele)
                if activityId == PartyGlobals.ActivityIds.PartyClock:
                    self.partyClockElement = pele

        for decorationId in PartyGlobals.DecorationIds:
            decorName = PartyGlobals.DecorationIds.getString(decorationId)
            if (decorName == "HeartTarget") \
            or (decorName == "HeartBanner") \
            or (decorName == "FlyingHeart"):
                holidayIds = base.cr.newsManager.getHolidayIdList()
                if ToontownGlobals.VALENTINES_DAY in holidayIds:
                    pele = PartyEditorListElement(self,
                                                  decorationId,
                                                  isDecoration=True)
                    self.elementList.addItem(pele)
            elif decorationId in PartyGlobals.VictoryPartyDecorationIds:
                holidayIds = base.cr.newsManager.getHolidayIdList()
                if ToontownGlobals.VICTORY_PARTY_HOLIDAY in holidayIds:
                    pele = PartyEditorListElement(self,
                                                  decorationId,
                                                  isDecoration=True)
                    self.elementList.addItem(pele)
            elif decorationId in PartyGlobals.VictoryPartyReplacementDecorationIds:
                holidayIds = base.cr.newsManager.getHolidayIdList()
                if not ToontownGlobals.VICTORY_PARTY_HOLIDAY in holidayIds:
                    pele = PartyEditorListElement(self,
                                                  decorationId,
                                                  isDecoration=True)
                    self.elementList.addItem(pele)
            else:
                pele = PartyEditorListElement(self,
                                              decorationId,
                                              isDecoration=True)
                self.elementList.addItem(pele)
        self.elementList.refresh()
        self.elementList['command'] = self.scrollItemChanged

    def initPartyClock(self):
        self.partyClockElement.buyButtonClicked((8, 7))

    def initTrashCan(self):
        trashcanGui = loader.loadModel("phase_3/models/gui/trashcan_gui")
        self.trashCanButton = DirectButton(
            parent=self.parent,
            relief=None,
            pos=Point3(*PartyGlobals.TrashCanPosition),
            scale=PartyGlobals.TrashCanScale,
            geom=(
                trashcanGui.find("**/TrashCan_CLSD"),
                trashcanGui.find("**/TrashCan_OPEN"),
                trashcanGui.find("**/TrashCan_RLVR"),
                trashcanGui.find("**/TrashCan_RLVR"),
            ),
            command=self.trashCanClicked,
        )
        self.trashCanButton.bind(DirectGuiGlobals.ENTER, self.mouseEnterTrash)
        self.trashCanButton.bind(DirectGuiGlobals.EXIT, self.mouseExitTrash)
        self.mouseOverTrash = False
        self.oldInstructionText = ""
        self.trashCanLastClickedTime = 0

    def scrollItemChanged(self):
        if not self.elementList["items"]:
            # we are probably closing the gui, do nothing
            return
        self.currentElement = self.elementList["items"][
            self.elementList.getSelectedIndex()]
        self.elementList["items"][
            self.elementList.getSelectedIndex()].elementSelectedFromList()
        if self.elementList["items"][
                self.elementList.getSelectedIndex()].isDecoration:
            self.partyPlanner.instructionLabel[
                "text"] = TTLocalizer.PartyPlannerEditorInstructionsClickedElementDecoration
        else:
            self.partyPlanner.instructionLabel[
                "text"] = TTLocalizer.PartyPlannerEditorInstructionsClickedElementActivity

    def listElementClicked(self):
        self.request("DraggingElement")

    def listElementReleased(self):
        self.request("Idle", True)

    def trashCanClicked(self):
        currentTime = time.time()
        # Check for double click, if so, clear the party grounds
        if currentTime - self.trashCanLastClickedTime < 0.2:
            self.clearPartyGrounds()
        self.trashCanLastClickedTime = time.time()

    def clearPartyGrounds(self):
        for item in self.elementList["items"]:
            item.clearPartyGrounds()
        self.initPartyClock()
        if self.currentElement:
            self.currentElement.checkSoldOutAndPaidStatusAndAffordability()

    def buyCurrentElement(self):
        if self.currentElement:
            purchaseSuccessful = self.currentElement.buyButtonClicked()
            if purchaseSuccessful:
                # The buying and placement of the item was successful
                self.handleMutuallyExclusiveActivities()
                pass
            else:
                # The buying and placement of the item was not successful
                self.partyPlanner.instructionLabel[
                    "text"] = TTLocalizer.PartyPlannerEditorInstructionsNoRoom

    def mouseEnterTrash(self, mouseEvent):
        self.mouseOverTrash = True
        self.oldInstructionText = self.partyPlanner.instructionLabel["text"]
        self.partyPlanner.instructionLabel[
            "text"] = TTLocalizer.PartyPlannerEditorInstructionsTrash

    def mouseExitTrash(self, mouseEvent):
        self.mouseOverTrash = False
        self.partyPlanner.instructionLabel["text"] = self.oldInstructionText

    ### FSM Methods ###

    def enterHidden(self):
        PartyEditor.notify.debug("Enter Hidden")

    def exitHidden(self):
        PartyEditor.notify.debug("Exit Hidden")

    def enterIdle(self, fromDragging=False):
        PartyEditor.notify.debug("Enter Idle")
        if not fromDragging:
            self.elementList.scrollTo(0)
            self.elementList["items"][0].elementSelectedFromList()
            self.currentElement = self.elementList["items"][
                self.elementList.getSelectedIndex()]
            self.currentElement.checkSoldOutAndPaidStatusAndAffordability()
        self.partyPlanner.instructionLabel[
            "text"] = TTLocalizer.PartyPlannerEditorInstructionsIdle
        self.updateCostsAndBank()
        self.handleMutuallyExclusiveActivities()

    def handleMutuallyExclusiveActivities(self):
        """Smartly removed the older activity and inform the user."""
        mutSet = self.getMutuallyExclusiveActivities()
        if not mutSet:
            return
        # our mutset doesn't tell us which one is older
        currentActivities = self.partyEditorGrid.getActivitiesElementsOnGrid()
        lastActivity = self.partyEditorGrid.lastActivityIdPlaced
        for act in currentActivities:
            if (act.id in mutSet) and not (lastActivity == act.id):
                act.removeFromGrid()
                removedName = TTLocalizer.PartyActivityNameDict[
                    act.id]["editor"]
                addedName = TTLocalizer.PartyActivityNameDict[lastActivity][
                    "editor"]
                instr = TTLocalizer.PartyPlannerEditorInstructionsRemoved % \
                        {"removed" : removedName, "added" : addedName}
                self.partyPlanner.instructionLabel["text"] = instr
                self.updateCostsAndBank()
                # deliberately no break here, in case they manage to
                # get 3 jukeboxes into the editor somehow

    def getMutuallyExclusiveActivities(self):
        """Return the set of activities on the grid that are mutually exclusive, None otherwise."""
        # create a set of activity Ids
        currentActivities = self.partyEditorGrid.getActivitiesOnGrid()
        actSet = Set([])
        for act in currentActivities:
            actSet.add(act[0])
        result = None
        for mutuallyExclusiveTuples in PartyGlobals.MutuallyExclusiveActivities:
            mutSet = Set(mutuallyExclusiveTuples)
            inter = mutSet.intersection(actSet)
            if len(inter) > 1:
                result = inter
                break
        return result

    def updateCostsAndBank(self):
        """
        We need to update the total cost of the party and what they will have
        left in their bank.
        """
        currentActivities = self.partyEditorGrid.getActivitiesOnGrid()
        currentDecorations = self.partyEditorGrid.getDecorationsOnGrid()
        newCost = 0
        for elementTuple in currentActivities:
            newCost += PartyGlobals.ActivityInformationDict[
                elementTuple[0]]["cost"]
        for elementTuple in currentDecorations:
            newCost += PartyGlobals.DecorationInformationDict[
                elementTuple[0]]["cost"]
        self.partyPlanner.costLabel[
            "text"] = TTLocalizer.PartyPlannerTotalCost % newCost
        if len(currentActivities) > 0 or len(currentDecorations) > 0:
            self.partyPlanner.setNextButtonState(enabled=True)
        else:
            self.partyPlanner.setNextButtonState(enabled=False)
        self.partyPlanner.totalCost = newCost
        self.partyPlanner.beanBank["text"] = str(
            int(self.partyPlanner.totalMoney - self.partyPlanner.totalCost))

    def exitIdle(self):
        PartyEditor.notify.debug("Exit Idle")

    def enterDraggingElement(self):
        PartyEditor.notify.debug("Enter DraggingElement")
        if self.currentElement.isDecoration:
            self.partyPlanner.instructionLabel[
                "text"] = TTLocalizer.PartyPlannerEditorInstructionsDraggingDecoration
        else:
            self.partyPlanner.instructionLabel[
                "text"] = TTLocalizer.PartyPlannerEditorInstructionsDraggingActivity

    def exitDraggingElement(self):
        PartyEditor.notify.debug("Exit DraggingElement")

    def enterCleanup(self):
        PartyEditor.notify.debug("Enter Cleanup")
        self.partyEditorGrid.destroy()
        self.elementList.removeAndDestroyAllItems()
        self.elementList.destroy()
        self.trashCanButton.unbind(DirectGuiGlobals.ENTER)
        self.trashCanButton.unbind(DirectGuiGlobals.EXIT)
        self.trashCanButton.destroy()

    def exitCleanup(self):
        PartyEditor.notify.debug("Exit Cleanup")
Exemplo n.º 3
0
class PartyEditor(DirectObject, FSM):
    notify = directNotify.newCategory('PartyEditor')

    def __init__(self, partyPlanner, parent):
        FSM.__init__(self, self.__class__.__name__)
        self.partyPlanner = partyPlanner
        self.parent = parent
        self.partyEditorGrid = PartyEditorGrid(self)
        self.currentElement = None
        self.defaultTransitions = {'Hidden': ['Idle', 'Cleanup'],
         'Idle': ['DraggingElement', 'Hidden', 'Cleanup'],
         'DraggingElement': ['Idle',
                             'DraggingElement',
                             'Hidden',
                             'Cleanup'],
         'Cleanup': []}
        self.initElementList()
        self.initPartyClock()
        self.initTrashCan()
        return

    def initElementList(self):
        self.activityIconsModel = loader.loadModel('phase_4/models/parties/eventSignIcons')
        self.decorationModels = loader.loadModel('phase_4/models/parties/partyDecorations')
        pos = self.partyPlanner.gui.find('**/step_05_activitiesIcon_locator').getPos()
        self.elementList = DirectScrolledList(parent=self.parent, relief=None, decButton_image=(self.partyPlanner.gui.find('**/activitiesButtonUp_up'),
         self.partyPlanner.gui.find('**/activitiesButtonUp_down'),
         self.partyPlanner.gui.find('**/activitiesButtonUp_rollover'),
         self.partyPlanner.gui.find('**/activitiesButtonUp_inactive')), decButton_relief=None, decButton_pos=(-0.05, 0.0, -0.38), incButton_image=(self.partyPlanner.gui.find('**/activitiesButtonDown_up'),
         self.partyPlanner.gui.find('**/activitiesButtonDown_down'),
         self.partyPlanner.gui.find('**/activitiesButtonDown_rollover'),
         self.partyPlanner.gui.find('**/activitiesButtonDown_inactive')), incButton_relief=None, incButton_pos=(-0.05, 0.0, -0.94), itemFrame_pos=(pos[0], pos[1], pos[2] + 0.04), itemFrame_relief=None, numItemsVisible=1, items=[])
        holidayIds = base.cr.newsManager.getHolidayIdList()
        isWinter = ToontownGlobals.WINTER_DECORATIONS in holidayIds or ToontownGlobals.WACKY_WINTER_DECORATIONS in holidayIds
        isVictory = ToontownGlobals.VICTORY_PARTY_HOLIDAY in holidayIds
        isValentine = ToontownGlobals.VALENTINES_DAY in holidayIds
        for activityId in PartyGlobals.PartyEditorActivityOrder:
            if not isVictory and activityId in PartyGlobals.VictoryPartyActivityIds or not isWinter and activityId in PartyGlobals.WinterPartyActivityIds or not isValentine and activityId in PartyGlobals.ValentinePartyActivityIds:
                pass
            elif isVictory and activityId in PartyGlobals.VictoryPartyReplacementActivityIds or isWinter and activityId in PartyGlobals.WinterPartyReplacementActivityIds or isValentine and activityId in PartyGlobals.ValentinePartyReplacementActivityIds:
                pass
            else:
                pele = PartyEditorListElement(self, activityId)
                self.elementList.addItem(pele)
                if activityId == PartyGlobals.ActivityIds.PartyClock:
                    self.partyClockElement = pele

        for decorationId in PartyGlobals.DecorationIds:
            if not isVictory and decorationId in PartyGlobals.VictoryPartyDecorationIds or not isWinter and decorationId in PartyGlobals.WinterPartyDecorationIds or not isValentine and decorationId in PartyGlobals.ValentinePartyDecorationIds:
                pass
            elif isVictory and decorationId in PartyGlobals.VictoryPartyReplacementDecorationIds or isValentine and decorationId in PartyGlobals.ValentinePartyReplacementDecorationIds:
                pass
            elif decorationId in PartyGlobals.TTIUnreleasedDecor:
                pass
            else:
                pele = PartyEditorListElement(self, decorationId, isDecoration=True)
                self.elementList.addItem(pele)

        self.elementList.refresh()
        self.elementList['command'] = self.scrollItemChanged
        return

    def initPartyClock(self):
        self.partyClockElement.buyButtonClicked((8, 7))

    def initTrashCan(self):
        trashcanGui = loader.loadModel('phase_3/models/gui/trashcan_gui')
        self.trashCanButton = DirectButton(parent=self.parent, relief=None, pos=Point3(*PartyGlobals.TrashCanPosition), scale=PartyGlobals.TrashCanScale, geom=(trashcanGui.find('**/TrashCan_CLSD'),
         trashcanGui.find('**/TrashCan_OPEN'),
         trashcanGui.find('**/TrashCan_RLVR'),
         trashcanGui.find('**/TrashCan_RLVR')), command=self.trashCanClicked)
        self.trashCanButton.bind(DirectGuiGlobals.ENTER, self.mouseEnterTrash)
        self.trashCanButton.bind(DirectGuiGlobals.EXIT, self.mouseExitTrash)
        self.mouseOverTrash = False
        self.oldInstructionText = ''
        self.trashCanLastClickedTime = 0
        return

    def scrollItemChanged(self):
        if not self.elementList['items']:
            return
        self.currentElement = self.elementList['items'][self.elementList.getSelectedIndex()]
        self.elementList['items'][self.elementList.getSelectedIndex()].elementSelectedFromList()
        if self.elementList['items'][self.elementList.getSelectedIndex()].isDecoration:
            self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsClickedElementDecoration
        else:
            self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsClickedElementActivity

    def listElementClicked(self):
        self.request('DraggingElement')

    def listElementReleased(self):
        self.request('Idle', True)

    def trashCanClicked(self):
        currentTime = time.time()
        if currentTime - self.trashCanLastClickedTime < 0.2:
            self.clearPartyGrounds()
        self.trashCanLastClickedTime = time.time()

    def clearPartyGrounds(self):
        for item in self.elementList['items']:
            item.clearPartyGrounds()

        self.initPartyClock()
        if self.currentElement:
            self.currentElement.checkSoldOutAndPaidStatusAndAffordability()

    def buyCurrentElement(self):
        if self.currentElement:
            purchaseSuccessful = self.currentElement.buyButtonClicked()
            if purchaseSuccessful:
                self.handleMutuallyExclusiveActivities()
            else:
                self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsNoRoom

    def mouseEnterTrash(self, mouseEvent):
        self.mouseOverTrash = True
        self.oldInstructionText = self.partyPlanner.instructionLabel['text']
        self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsTrash

    def mouseExitTrash(self, mouseEvent):
        self.mouseOverTrash = False
        self.partyPlanner.instructionLabel['text'] = self.oldInstructionText

    def enterHidden(self):
        PartyEditor.notify.debug('Enter Hidden')

    def exitHidden(self):
        PartyEditor.notify.debug('Exit Hidden')

    def enterIdle(self, fromDragging = False):
        PartyEditor.notify.debug('Enter Idle')
        if not fromDragging:
            self.elementList.scrollTo(0)
            self.elementList['items'][0].elementSelectedFromList()
            self.currentElement = self.elementList['items'][self.elementList.getSelectedIndex()]
            self.currentElement.checkSoldOutAndPaidStatusAndAffordability()
        self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsIdle
        self.updateCostsAndBank()
        self.handleMutuallyExclusiveActivities()

    def handleMutuallyExclusiveActivities(self):
        mutSet = self.getMutuallyExclusiveActivities()
        if not mutSet:
            return
        currentActivities = self.partyEditorGrid.getActivitiesElementsOnGrid()
        lastActivity = self.partyEditorGrid.lastActivityIdPlaced
        for act in currentActivities:
            if act.id in mutSet and not lastActivity == act.id:
                act.removeFromGrid()
                removedName = TTLocalizer.PartyActivityNameDict[act.id]['editor']
                addedName = TTLocalizer.PartyActivityNameDict[lastActivity]['editor']
                instr = TTLocalizer.PartyPlannerEditorInstructionsRemoved % {'removed': removedName,
                 'added': addedName}
                self.partyPlanner.instructionLabel['text'] = instr
                self.updateCostsAndBank()

    def getMutuallyExclusiveActivities(self):
        currentActivities = self.partyEditorGrid.getActivitiesOnGrid()
        actSet = Set([])
        for act in currentActivities:
            actSet.add(act[0])

        result = None
        for mutuallyExclusiveTuples in PartyGlobals.MutuallyExclusiveActivities:
            mutSet = Set(mutuallyExclusiveTuples)
            inter = mutSet.intersection(actSet)
            if len(inter) > 1:
                result = inter
                break

        return result

    def updateCostsAndBank(self):
        currentActivities = self.partyEditorGrid.getActivitiesOnGrid()
        currentDecorations = self.partyEditorGrid.getDecorationsOnGrid()
        newCost = 0
        for elementTuple in currentActivities:
            newCost += PartyGlobals.ActivityInformationDict[elementTuple[0]]['cost']

        for elementTuple in currentDecorations:
            newCost += PartyGlobals.DecorationInformationDict[elementTuple[0]]['cost']

        self.partyPlanner.costLabel['text'] = TTLocalizer.PartyPlannerTotalCost % newCost
        if len(currentActivities) > 0 or len(currentDecorations) > 0:
            self.partyPlanner.setNextButtonState(enabled=True)
        else:
            self.partyPlanner.setNextButtonState(enabled=False)
        self.partyPlanner.totalCost = newCost
        self.partyPlanner.beanBank['text'] = str(int(self.partyPlanner.totalMoney - self.partyPlanner.totalCost))

    def exitIdle(self):
        PartyEditor.notify.debug('Exit Idle')

    def enterDraggingElement(self):
        PartyEditor.notify.debug('Enter DraggingElement')
        if self.currentElement.isDecoration:
            self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsDraggingDecoration
        else:
            self.partyPlanner.instructionLabel['text'] = TTLocalizer.PartyPlannerEditorInstructionsDraggingActivity

    def exitDraggingElement(self):
        PartyEditor.notify.debug('Exit DraggingElement')

    def enterCleanup(self):
        PartyEditor.notify.debug('Enter Cleanup')
        self.partyEditorGrid.destroy()
        self.elementList.removeAndDestroyAllItems()
        self.elementList.destroy()
        self.trashCanButton.unbind(DirectGuiGlobals.ENTER)
        self.trashCanButton.unbind(DirectGuiGlobals.EXIT)
        self.trashCanButton.destroy()

    def exitCleanup(self):
        PartyEditor.notify.debug('Exit Cleanup')
Exemplo n.º 4
0
class CalendarGuiMonth(DirectFrame):
    notify = directNotify.newCategory("CalendarGuiMonth")

    def __init__(
        self,
        parent,
        startingDateTime,
        scale=1.0,
        pos=(0, 0, -0.1),
        dayClickCallback=None,
        onlyFutureDaysClickable=False,
        onlyFutureMonthsClickable=False,
    ):
        self.startDate = startingDateTime
        self.curDate = startingDateTime
        self.dayClickCallback = dayClickCallback
        self.onlyFutureDaysClickable = onlyFutureDaysClickable
        self.onlyFutureMonthsClickable = onlyFutureMonthsClickable
        if self.onlyFutureDaysClickable:
            self.onlyFutureMonthsClickable = True
        DirectFrame.__init__(self, parent=parent, scale=scale, pos=pos)
        self.load()
        self.createGuiObjects()
        self.lastSelectedDate = None
        self.accept("clickedOnDay", self.clickedOnDay)

    def load(self):
        monthAsset = loader.loadModel("phase_4/models/parties/tt_m_gui_sbk_calendar")
        monthAsset.reparentTo(self)
        self.monthLocator = self.find("**/locator_month/locator_month")
        self.weekDayLocators = []
        for weekday in ("sun", "mon", "tue", "wed", "thu", "fri", "sat"):
            weekDayLoc = self.find("**/loc_%s" % weekday)
            self.weekDayLocators.append(weekDayLoc)

        self.dayLocators = []
        for row in xrange(6):
            oneWeek = []
            for col in xrange(7):
                newDayLoc = self.find("**/loc_box_%s_%s" % (row, col))
                oneWeek.append(newDayLoc)

            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.find("**/locator_month_arrowL")
        self.monthRightLocator = self.find("**/locator_month_arrowR")
        self.filterLocator = self.find("**/locator_filter")
        self.filterLocatorArrowUp = self.find("**/locator_filter_arrowTop")
        self.filterLocatorArrowDown = self.find("**/locator_filter_arrowBottom")
        self.yearLocator = self.attachNewNode("yearLocator")
        self.yearLocator.setPos(self.monthLocator, 0, 0, -0.03)

    def createGuiObjects(self):
        self.monthLabel = DirectLabel(
            parent=self.monthLocator,
            relief=None,
            text=TTLocalizer.Months[self.startDate.month],
            text_scale=0.075,
            text_font=ToontownGlobals.getMinnieFont(),
            text_fg=(40 / 255.0, 140 / 255.0, 246 / 255.0, 1.0),
        )
        self.yearLabel = DirectLabel(
            parent=self.yearLocator,
            relief=None,
            text=str(self.startDate.year),
            text_scale=0.03,
            text_font=ToontownGlobals.getMinnieFont(),
            text_fg=(140 / 255.0, 140 / 255.0, 246 / 255.0, 1.0),
        )
        self.weekdayLabels = []
        for posIndex in xrange(7):
            adjustedNameIndex = (posIndex - 1) % 7
            self.weekdayLabels.append(
                DirectLabel(
                    parent=self.weekDayLocators[posIndex],
                    relief=None,
                    text=TTLocalizer.DayNamesAbbrev[adjustedNameIndex],
                    text_font=ToontownGlobals.getInterfaceFont(),
                    text_fg=(255 / 255.0, 146 / 255.0, 113 / 255.0, 1.0),
                    text_scale=0.05,
                )
            )

        self.createGuiDays()
        arrowUp = self.find("**/month_arrowR_up")
        arrowDown = self.find("**/month_arrowR_down")
        arrowHover = self.find("**/month_arrowR_hover")
        self.monthLeftArrow = DirectButton(
            parent=self.monthLeftLocator,
            relief=None,
            image=(arrowUp, arrowDown, arrowHover, arrowUp),
            image3_color=(1, 1, 1, 0.5),
            scale=(-1.0, 1.0, 1.0),
            command=self.__doMonthLeft,
        )
        if self.onlyFutureMonthsClickable:
            self.monthLeftArrow.hide()
        self.monthRightArrow = DirectButton(
            parent=self.monthRightLocator,
            relief=None,
            image=(arrowUp, arrowDown, arrowHover, arrowUp),
            image3_color=(1, 1, 1, 0.5),
            command=self.__doMonthRight,
        )

        def makeLabel(itemName, itemNum, *extraArgs):
            return DirectLabel(text=itemName, frameColor=(0, 0, 0, 0), text_scale=0.04)

        gui = loader.loadModel("phase_4/models/parties/tt_m_gui_sbk_calendar_box")
        arrowUp = gui.find("**/downScroll_up")
        arrowDown = gui.find("**/downScroll_down")
        arrowHover = gui.find("**/downScroll_hover")
        filterLocatorUpPos = self.filterLocatorArrowUp.getPos(self.filterLocator)
        filterLocatorDownPos = self.filterLocatorArrowDown.getPos(self.filterLocator)
        self.filterList = DirectScrolledList(
            parent=self.filterLocator,
            relief=None,
            pos=(0, 0, 0),
            image=None,
            text_scale=0.025,
            incButton_image=(arrowUp, arrowDown, arrowHover, arrowUp),
            incButton_relief=None,
            incButton_pos=filterLocatorDownPos,
            incButton_image3_color=(1, 1, 1, 0.2),
            incButtonCallback=self.filterChanged,
            decButton_image=(arrowUp, arrowDown, arrowHover, arrowUp),
            decButton_relief=None,
            decButton_pos=filterLocatorUpPos,
            decButton_scale=(1, 1, -1),
            decButton_image3_color=(1, 1, 1, 0.2),
            decButtonCallback=self.filterChanged,
            numItemsVisible=1,
            itemMakeFunction=makeLabel,
            items=[
                TTLocalizer.CalendarShowAll,
                TTLocalizer.CalendarShowOnlyHolidays,
                TTLocalizer.CalendarShowOnlyParties,
            ],
            itemFrame_frameSize=(-0.2, 0.2, -0.02, 0.05),
            itemFrame_frameColor=(0, 0, 0, 0),
        )
        gui.removeNode()

    def getTopLeftDate(self):
        firstOfTheMonth = self.curDate.replace(day=1)
        daysAwayFromSunday = (firstOfTheMonth.weekday() - 6) % 7
        topLeftDate = firstOfTheMonth + timedelta(days=-daysAwayFromSunday)
        return topLeftDate

    def createGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        curDate = topLeftDate
        self.guiDays = []
        for row in self.dayLocators:
            for oneLocator in row:
                self.guiDays.append(
                    CalendarGuiDay(
                        oneLocator, curDate, self.curDate, self.dayClickCallback, self.onlyFutureDaysClickable
                    )
                )
                curDate += timedelta(days=1)

    def changeDateForGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        guiDayDate = topLeftDate
        for guiDay in self.guiDays:
            guiDay.changeDate(self.curDate, guiDayDate)
            guiDayDate += timedelta(days=1)

    def changeMonth(self, monthChange):
        if monthChange != 0:
            newMonth = self.curDate.month + monthChange
            newYear = self.curDate.year
            while newMonth > 12:
                newYear += 1
                newMonth -= 12

            while newMonth < 1:
                if newYear - 1 > 2002:
                    newMonth += 12
                    newYear -= 1
                else:
                    newMonth += 1

            self.curDate = datetime(
                newYear,
                newMonth,
                1,
                self.curDate.time().hour,
                self.curDate.time().minute,
                self.curDate.time().second,
                self.curDate.time().microsecond,
                self.curDate.tzinfo,
            )
        self.monthLabel["text"] = (TTLocalizer.Months[self.curDate.month],)
        self.yearLabel["text"] = (str(self.curDate.year),)
        startTime = globalClock.getRealTime()
        self.changeDateForGuiDays()
        endTime = globalClock.getRealTime()
        self.notify.debug("changeDate took %f seconds" % (endTime - startTime))
        self.updateSelectedDate()
        if monthChange != 0:
            if self.onlyFutureMonthsClickable and newMonth == self.startDate.month and newYear == self.startDate.year:
                self.monthLeftArrow.hide()

    def __doMonthLeft(self):
        self.changeMonth(-1)

    def __doMonthRight(self):
        self.monthLeftArrow.show()
        self.changeMonth(1)

    def destroy(self):
        self.ignoreAll()
        self.dayClickCallback = None
        self.monthLeftArrow.destroy()
        self.monthRightArrow.destroy()
        for day in self.guiDays:
            if day is not None:
                day.destroy()
            day = None

        self.filterList.destroy()
        DirectFrame.destroy(self)

    def clickedOnDay(self, dayDate):
        self.lastSelectedDate = dayDate
        self.updateSelectedDate()

    def updateSelectedDate(self):
        if self.lastSelectedDate:
            for oneGuiDay in self.guiDays:
                oneGuiDay.updateSelected(oneGuiDay.myDate.date() == self.lastSelectedDate)

    def clearSelectedDay(self):
        for oneGuiDay in self.guiDays:
            oneGuiDay.updateSelected(False)

    def filterChanged(self):
        newFilter = self.filterList.getSelectedIndex()
        for guiDay in self.guiDays:
            guiDay.changeFilter(newFilter)
class CalendarGuiMonth(DirectFrame):
    notify = directNotify.newCategory('CalendarGuiMonth')

    def __init__(self, parent, startingDateTime, scale = 1.0, pos = (0, 0, -0.1), dayClickCallback = None, onlyFutureDaysClickable = False, onlyFutureMonthsClickable = False):
        self.startDate = startingDateTime
        self.curDate = startingDateTime
        self.dayClickCallback = dayClickCallback
        self.onlyFutureDaysClickable = onlyFutureDaysClickable
        self.onlyFutureMonthsClickable = onlyFutureMonthsClickable
        if self.onlyFutureDaysClickable:
            self.onlyFutureMonthsClickable = True
        DirectFrame.__init__(self, parent=parent, scale=scale, pos=pos)
        self.showMarkers = config.GetBool('show-calendar-markers', 0)
        self.load()
        self.createGuiObjects()
        self.lastSelectedDate = None
        self.accept('clickedOnDay', self.clickedOnDay)

    def createDummyLocators(self):
        self.monthLocator = self.attachNewNode('monthLocator')
        self.monthLocator.setZ(0.6)
        self.weekDayLocators = []
        for i in xrange(7):
            self.weekDayLocators.append(self.attachNewNode('weekDayLocator-%d' % i))
            self.weekDayLocators[i].setZ(0.5)
            self.weekDayLocators[i].setX(i * 0.24 + -0.75)

        dayTopLeftX = -0.8
        dayTopLeftZ = 0.4
        self.dayLocators = []
        for row in xrange(6):
            oneWeek = []
            for col in xrange(7):
                newDayLoc = self.attachNewNode('dayLocator-row-%d-col-%d' % (row, col))
                newDayLoc.setX(col * 0.24 + dayTopLeftX)
                newDayLoc.setZ(row * -0.18 + dayTopLeftZ)
                oneWeek.append(newDayLoc)

            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.attachNewNode('monthLeft')
        self.monthLeftLocator.setPos(-0.3, 0, 0.65)
        self.monthRightLocator = self.attachNewNode('monthRight')
        self.monthRightLocator.setPos(0.3, 0, 0.65)

    def attachMarker(self, parent, scale = 0.01, color = (1, 0, 0)):
        if self.showMarkers:
            marker = loader.loadModel('phase_3/models/misc/sphere')
            marker.reparentTo(parent)
            marker.setScale(scale)
            marker.setColor(*color)

    def load(self):
        monthAsset = loader.loadModel('phase_4/models/parties/tt_m_gui_sbk_calendar')
        monthAsset.reparentTo(self)
        self.monthLocator = self.find('**/locator_month/locator_month')
        self.attachMarker(self.monthLocator)
        self.weekDayLocators = []
        for weekday in ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'):
            weekDayLoc = self.find('**/loc_%s' % weekday)
            self.weekDayLocators.append(weekDayLoc)
            self.attachMarker(weekDayLoc)

        self.dayLocators = []
        for row in xrange(6):
            oneWeek = []
            for col in xrange(7):
                newDayLoc = self.find('**/loc_box_%s_%s' % (row, col))
                oneWeek.append(newDayLoc)

            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.find('**/locator_month_arrowL')
        self.monthRightLocator = self.find('**/locator_month_arrowR')
        self.filterLocator = self.find('**/locator_filter')
        self.filterLocatorArrowUp = self.find('**/locator_filter_arrowTop')
        self.filterLocatorArrowDown = self.find('**/locator_filter_arrowBottom')
        self.yearLocator = self.attachNewNode('yearLocator')
        self.yearLocator.setPos(self.monthLocator, 0, 0, -0.03)

    def createGuiObjects(self):
        self.monthLabel = DirectLabel(parent=self.monthLocator, relief=None, text=TTLocalizer.Months[self.startDate.month], text_scale=0.075, text_font=ToontownGlobals.getMinnieFont(), text_fg=(40 / 255.0,
         140 / 255.0,
         246 / 255.0,
         1.0))
        self.yearLabel = DirectLabel(parent=self.yearLocator, relief=None, text=str(self.startDate.year), text_scale=0.03, text_font=ToontownGlobals.getMinnieFont(), text_fg=(140 / 255.0,
         140 / 255.0,
         246 / 255.0,
         1.0))
        self.weekdayLabels = []
        for posIndex in xrange(7):
            adjustedNameIndex = (posIndex - 1) % 7
            self.weekdayLabels.append(DirectLabel(parent=self.weekDayLocators[posIndex], relief=None, text=TTLocalizer.DayNamesAbbrev[adjustedNameIndex], text_font=ToontownGlobals.getInterfaceFont(), text_fg=(255 / 255.0,
             146 / 255.0,
             113 / 255.0,
             1.0), text_scale=0.05))

        self.createGuiDays()
        arrowUp = self.find('**/month_arrowR_up')
        arrowDown = self.find('**/month_arrowR_down')
        arrowHover = self.find('**/month_arrowR_hover')
        self.monthLeftArrow = DirectButton(parent=self.monthLeftLocator, relief=None, image=(arrowUp,
         arrowDown,
         arrowHover,
         arrowUp), image3_color=Vec4(1, 1, 1, 0.5), scale=(-1.0, 1.0, 1.0), command=self.__doMonthLeft)
        if self.onlyFutureMonthsClickable:
            self.monthLeftArrow.hide()
        self.monthRightArrow = DirectButton(parent=self.monthRightLocator, relief=None, image=(arrowUp,
         arrowDown,
         arrowHover,
         arrowUp), image3_color=Vec4(1, 1, 1, 0.5), command=self.__doMonthRight)

        def makeLabel(itemName, itemNum, *extraArgs):
            return DirectLabel(text=itemName, frameColor=(0, 0, 0, 0), text_scale=0.04)

        gui = loader.loadModel('phase_4/models/parties/tt_m_gui_sbk_calendar_box')
        arrowUp = gui.find('**/downScroll_up')
        arrowDown = gui.find('**/downScroll_down')
        arrowHover = gui.find('**/downScroll_hover')
        filterLocatorUpPos = self.filterLocatorArrowUp.getPos(self.filterLocator)
        filterLocatorDownPos = self.filterLocatorArrowDown.getPos(self.filterLocator)
        self.filterList = DirectScrolledList(parent=self.filterLocator, relief=None, pos=(0, 0, 0), image=None, text_scale=0.025, incButton_image=(arrowUp,
         arrowDown,
         arrowHover,
         arrowUp), incButton_relief=None, incButton_pos=filterLocatorDownPos, incButton_image3_color=Vec4(1, 1, 1, 0.2), incButtonCallback=self.filterChanged, decButton_image=(arrowUp,
         arrowDown,
         arrowHover,
         arrowUp), decButton_relief=None, decButton_pos=filterLocatorUpPos, decButton_scale=(1, 1, -1), decButton_image3_color=Vec4(1, 1, 1, 0.2), decButtonCallback=self.filterChanged, numItemsVisible=1, itemMakeFunction=makeLabel, items=[TTLocalizer.CalendarShowAll, TTLocalizer.CalendarShowOnlyHolidays, TTLocalizer.CalendarShowOnlyParties], itemFrame_frameSize=(-0.2, 0.2, -0.02, 0.05), itemFrame_frameColor=(0, 0, 0, 0))
        gui.removeNode()

    def getTopLeftDate(self):
        firstOfTheMonth = self.curDate.replace(day=1)
        daysAwayFromSunday = (firstOfTheMonth.weekday() - 6) % 7
        topLeftDate = firstOfTheMonth + timedelta(days=-daysAwayFromSunday)
        return topLeftDate

    def createGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        curDate = topLeftDate
        self.guiDays = []
        for row in self.dayLocators:
            for oneLocator in row:
                self.guiDays.append(CalendarGuiDay(oneLocator, curDate, self.curDate, self.dayClickCallback, self.onlyFutureDaysClickable))
                curDate += timedelta(days=1)

    def changeDateForGuiDays(self):
        topLeftDate = self.getTopLeftDate()
        guiDayDate = topLeftDate
        for guiDay in self.guiDays:
            guiDay.changeDate(self.curDate, guiDayDate)
            guiDayDate += timedelta(days=1)

    def changeMonth(self, monthChange):
        if monthChange != 0:
            newMonth = self.curDate.month + monthChange
            newYear = self.curDate.year
            while newMonth > 12:
                newYear += 1
                newMonth -= 12

            while newMonth < 1:
                if newYear - 1 > 1899:
                    newMonth += 12
                    newYear -= 1
                else:
                    newMonth += 1

            self.curDate = datetime(newYear, newMonth, 1, self.curDate.time().hour, self.curDate.time().minute, self.curDate.time().second, self.curDate.time().microsecond, self.curDate.tzinfo)
        self.monthLabel['text'] = (TTLocalizer.Months[self.curDate.month],)
        self.yearLabel['text'] = (str(self.curDate.year),)
        startTime = globalClock.getRealTime()
        self.changeDateForGuiDays()
        endTime = globalClock.getRealTime()
        self.notify.debug('changeDate took %f seconds' % (endTime - startTime))
        self.updateSelectedDate()
        if monthChange != 0:
            if self.onlyFutureMonthsClickable and newMonth == self.startDate.month and newYear == self.startDate.year:
                self.monthLeftArrow.hide()

    def __doMonthLeft(self):
        self.changeMonth(-1)

    def __doMonthRight(self):
        self.monthLeftArrow.show()
        self.changeMonth(1)

    def destroy(self):
        self.ignoreAll()
        self.dayClickCallback = None
        self.monthLeftArrow.destroy()
        self.monthRightArrow.destroy()
        for day in self.guiDays:
            if day is not None:
                day.destroy()
            day = None

        self.filterList.destroy()
        DirectFrame.destroy(self)

    def clickedOnDay(self, dayDate):
        self.lastSelectedDate = dayDate
        self.updateSelectedDate()

    def updateSelectedDate(self):
        if self.lastSelectedDate:
            for oneGuiDay in self.guiDays:
                if oneGuiDay.myDate.date() == self.lastSelectedDate:
                    oneGuiDay.updateSelected(True)
                else:
                    oneGuiDay.updateSelected(False)

    def clearSelectedDay(self):
        for oneGuiDay in self.guiDays:
            oneGuiDay.updateSelected(False)

    def filterChanged(self):
        newFilter = self.filterList.getSelectedIndex()
        for guiDay in self.guiDays:
            guiDay.changeFilter(newFilter)
Exemplo n.º 6
0
class CalendarGuiMonth(DirectFrame):
    """A class to represent the gui for one month.
    Do not put references to the shtiker page or book,  so this
    class may be extended later."""

    notify = directNotify.newCategory("CalendarGuiMonth")

    def __init__(self,
                 parent,
                 startingDateTime,
                 scale=1.0,
                 pos=(0, 0, -0.1),
                 dayClickCallback=None,
                 onlyFutureDaysClickable=False):
        """Construct ourself."""
        self.startDate = startingDateTime
        self.curDate = startingDateTime
        self.dayClickCallback = dayClickCallback
        self.onlyFutureDaysClickable = onlyFutureDaysClickable
        # sticker book is shifted up by 0.1, so default pos counteracts that
        # as the gui was made assuming 0,0 is center of the screen
        DirectFrame.__init__(self, parent=parent, scale=scale, pos=pos)

        # for debugging, show red dots for locators
        self.showMarkers = base.config.GetBool('show-calendar-markers', 0)

        # WARNING debug only, remove this or else we leak
        # base.cgm = self

        # self.createDummyLocators()
        self.load()
        self.createGuiObjects()
        self.lastSelectedDate = None
        self.accept('clickedOnDay', self.clickedOnDay)

    def createDummyLocators(self):
        """Put some programming locators until we get the real art assets."""
        self.monthLocator = self.attachNewNode('monthLocator')
        self.monthLocator.setZ(0.6)

        self.weekDayLocators = []
        for i in range(7):
            self.weekDayLocators.append(
                self.attachNewNode('weekDayLocator-%d' % i))
            self.weekDayLocators[i].setZ(0.5)
            self.weekDayLocators[i].setX(i * (0.24) + -0.75)

        dayTopLeftX = -0.8
        dayTopLeftZ = 0.4
        self.dayLocators = []
        for row in range(6):
            oneWeek = []
            for col in range(7):
                newDayLoc = self.attachNewNode('dayLocator-row-%d-col-%d' %
                                               (row, col))
                newDayLoc.setX(col * 0.24 + dayTopLeftX)
                newDayLoc.setZ(row * -0.18 + dayTopLeftZ)
                oneWeek.append(newDayLoc)
            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.attachNewNode('monthLeft')
        self.monthLeftLocator.setPos(-0.3, 0, 0.65)

        self.monthRightLocator = self.attachNewNode('monthRight')
        self.monthRightLocator.setPos(0.3, 0, 0.65)

    def attachMarker(self, parent, scale=0.01, color=(1, 0, 0)):
        """Attach a marker to the parent to aid in visual debugging."""
        if self.showMarkers:
            marker = loader.loadModel("phase_3/models/misc/sphere")
            marker.reparentTo(parent)
            marker.setScale(scale)
            marker.setColor(*color)

    def load(self):
        """Load our assets, make sure we have correct locators."""
        monthAsset = loader.loadModel(
            'phase_4/models/parties/tt_m_gui_sbk_calendar')
        monthAsset.reparentTo(self)
        self.monthLocator = self.find('**/locator_month/locator_month')
        self.attachMarker(self.monthLocator)

        self.weekDayLocators = []
        for weekday in ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'):
            weekDayLoc = self.find('**/loc_%s' % weekday)
            self.weekDayLocators.append(weekDayLoc)
            self.attachMarker(weekDayLoc)

        self.dayLocators = []
        for row in range(6):
            oneWeek = []
            for col in range(7):
                newDayLoc = self.find('**/loc_box_%s_%s' % (row, col))
                oneWeek.append(newDayLoc)
            self.dayLocators.append(oneWeek)

        self.monthLeftLocator = self.find('**/locator_month_arrowL')
        self.monthRightLocator = self.find('**/locator_month_arrowR')

        self.filterLocator = self.find('**/locator_filter')
        self.filterLocatorArrowUp = self.find('**/locator_filter_arrowTop')
        self.filterLocatorArrowDown = self.find(
            '**/locator_filter_arrowBottom')

        self.yearLocator = self.attachNewNode("yearLocator")
        self.yearLocator.setPos(self.monthLocator, 0, 0, -0.03)

    def createGuiObjects(self):
        """Create the other gui objects in the month, assumes we have proper locators."""
        self.monthLabel = DirectLabel(
            parent=self.monthLocator,
            relief=None,
            text=TTLocalizer.Months[self.startDate.month],
            text_scale=0.075,
            text_font=ToontownGlobals.getMinnieFont(),
            text_fg=(40 / 255.0, 140 / 255.0, 246 / 255.0, 1.0),
        )
        self.yearLabel = DirectLabel(
            parent=self.yearLocator,
            relief=None,
            text=str(self.startDate.year),
            text_scale=0.03,
            text_font=ToontownGlobals.getMinnieFont(),
            #text_fg = (40/255.0, 140/255.0, 246/255.0, 1.0),
            text_fg=(140 / 255.0, 140 / 255.0, 246 / 255.0, 1.0),
        )

        self.weekdayLabels = []
        for posIndex in range(7):
            # Sunday is the usual first day of the week, but
            # self.startDate.weekDay() reports 0 for Monday
            adjustedNameIndex = (posIndex - 1) % 7
            self.weekdayLabels.append(
                DirectLabel(parent=self.weekDayLocators[posIndex],
                            relief=None,
                            text=TTLocalizer.DayNamesAbbrev[adjustedNameIndex],
                            text_font=ToontownGlobals.getInterfaceFont(),
                            text_fg=(255 / 255.0, 146 / 255.0, 113 / 255.0,
                                     1.0),
                            text_scale=0.05))
        self.createGuiDays()

        arrowUp = self.find("**/month_arrowR_up")
        arrowDown = self.find("**/month_arrowR_down")
        arrowHover = self.find("**/month_arrowR_hover")
        self.monthLeftArrow = DirectButton(
            parent=self.monthLeftLocator,
            relief=None,
            image=(
                arrowUp,
                arrowDown,
                arrowHover,
                arrowUp,
            ),
            # make the disabled color more transparent
            image3_color=Vec4(1, 1, 1, 0.5),
            scale=(-1.0, 1.0, 1.0),  # make the arrow point left
            #pos = (0.25, 0, buttonbase_ycoord - textRowHeight * 4),
            command=self.__doMonthLeft,
        )
        if self.onlyFutureDaysClickable:
            self.monthLeftArrow.hide()
        self.monthRightArrow = DirectButton(
            parent=self.monthRightLocator,
            relief=None,
            image=(
                arrowUp,
                arrowDown,
                arrowHover,
                arrowUp,
            ),
            # make the disabled color more transparent
            image3_color=Vec4(1, 1, 1, 0.5),
            #pos = (0.65, 0, buttonbase_ycoord - textRowHeight * 4),
            command=self.__doMonthRight,
        )

        def makeLabel(itemName, itemNum, *extraArgs):

            return DirectLabel(
                text=itemName,
                frameColor=(0, 0, 0, 0),
                #scale = 0.1,
                #relief = DGG.RAISED,
                #frameSize = (-3.5, 3.5, -0.2, 0.8),
                text_scale=0.04)

        gui = loader.loadModel(
            'phase_4/models/parties/tt_m_gui_sbk_calendar_box')
        arrowUp = gui.find("**/downScroll_up")
        arrowDown = gui.find("**/downScroll_down")
        arrowHover = gui.find("**/downScroll_hover")

        filterLocatorUpPos = self.filterLocatorArrowUp.getPos(
            self.filterLocator)
        filterLocatorDownPos = self.filterLocatorArrowDown.getPos(
            self.filterLocator)
        self.filterList = DirectScrolledList(
            parent=self.filterLocator,
            relief=None,
            pos=(0, 0, 0),  #(0.65, 0, 0.7),
            image=None,  #DGG.getDefaultDialogGeom(),
            text_scale=0.025,
            incButton_image=(
                arrowUp,
                arrowDown,
                arrowHover,
                arrowUp,
            ),
            incButton_relief=None,
            incButton_pos=filterLocatorDownPos,  #(0.0, 0.0, -0.035),
            # Make the disabled button fade out
            incButton_image3_color=Vec4(1, 1, 1, 0.2),
            incButtonCallback=self.filterChanged,
            # Same for the decrement button
            decButton_image=(
                arrowUp,
                arrowDown,
                arrowHover,
                arrowUp,
            ),
            decButton_relief=None,
            decButton_pos=filterLocatorUpPos,  #(0.0, 0.0, 0.07),
            decButton_scale=(1, 1, -1),
            # Make the disabled button fade out
            decButton_image3_color=Vec4(1, 1, 1, 0.2),
            decButtonCallback=self.filterChanged,
            # each item is a button with text on it
            numItemsVisible=1,
            itemMakeFunction=makeLabel,
            # note ordering is very important, should match ToontownGlobals
            items=[
                TTLocalizer.CalendarShowAll,
                TTLocalizer.CalendarShowOnlyHolidays,
                TTLocalizer.CalendarShowOnlyParties,
            ],
            # itemFrame is a DirectFrame
            itemFrame_frameSize=(-.2, .2, -.02, .05),
            itemFrame_frameColor=(0, 0, 0, 0),
        )
        gui.removeNode()

    def getTopLeftDate(self):
        """Return the top left date. Will probably be a date in the previous month."""
        # for the current month figure out how many days
        # we subtract to get to Sunday
        firstOfTheMonth = self.curDate.replace(day=1)
        daysAwayFromSunday = (firstOfTheMonth.weekday() - 6) % 7
        topLeftDate = firstOfTheMonth + timedelta(days=-daysAwayFromSunday)
        return topLeftDate

    def createGuiDays(self):
        """Create the day guis for the whole month."""
        topLeftDate = self.getTopLeftDate()
        curDate = topLeftDate
        self.guiDays = []
        for row in self.dayLocators:
            for oneLocator in row:
                self.guiDays.append(
                    CalendarGuiDay(oneLocator, curDate, self.curDate,
                                   self.dayClickCallback,
                                   self.onlyFutureDaysClickable))
                curDate += timedelta(days=1)

    def changeDateForGuiDays(self):
        """Change the date for all our gui days.

        This should be much faster then our current tearing down and loading up again.
        """
        topLeftDate = self.getTopLeftDate()
        guiDayDate = topLeftDate
        for guiDay in self.guiDays:
            guiDay.changeDate(self.curDate, guiDayDate)
            guiDayDate += timedelta(days=1)

    def changeMonth(self, monthChange):
        """Change the month we are displaying."""
        # monthChange should be able to handle bigger values now
        if monthChange != 0:
            # if it's March 1, make sure we dont skip February going back 31 days
            newMonth = self.curDate.month + monthChange
            newYear = self.curDate.year
            # make sure we have a valid month 1..12
            while newMonth > 12:
                newYear += 1
                newMonth -= 12
            while newMonth < 1:
                if newYear - 1 > 1899:
                    newMonth += 12
                    newYear -= 1
                else:
                    newMonth += 1
            self.curDate = datetime(newYear, newMonth, 1,
                                    self.curDate.time().hour,
                                    self.curDate.time().minute,
                                    self.curDate.time().second,
                                    self.curDate.time().microsecond,
                                    self.curDate.tzinfo)

        self.monthLabel['text'] = TTLocalizer.Months[self.curDate.month],
        self.yearLabel['text'] = str(self.curDate.year),
        startTime = globalClock.getRealTime()
        self.changeDateForGuiDays()
        endTime = globalClock.getRealTime()
        self.notify.debug('changeDate took %f seconds' % (endTime - startTime))
        # if we have a selected date, it probably changed box
        self.updateSelectedDate()
        if self.onlyFutureDaysClickable and (newMonth == self.startDate.month
                                             and newYear
                                             == self.startDate.year):
            self.monthLeftArrow.hide()

    def __doMonthLeft(self):
        """Handle left month arrrow being pressed."""
        self.changeMonth(-1)

    def __doMonthRight(self):
        """Handle right month arrow being pressed."""
        self.monthLeftArrow.show()
        self.changeMonth(1)

    def destroy(self):
        """Clean ourself up."""
        self.ignoreAll()
        # these 2 lines get rid of party planner leaks
        self.dayClickCallback = None
        self.monthLeftArrow.destroy()
        self.monthRightArrow.destroy()
        for day in self.guiDays:
            if day is not None:
                day.destroy()
            day = None
        self.filterList.destroy()
        DirectFrame.destroy(self)

    def clickedOnDay(self, dayDate):
        """Handle one of our child day squares getting clicked on."""
        self.lastSelectedDate = dayDate
        self.updateSelectedDate()

    def updateSelectedDate(self):
        if self.lastSelectedDate:
            for oneGuiDay in self.guiDays:
                if oneGuiDay.myDate.date() == self.lastSelectedDate:
                    oneGuiDay.updateSelected(True)
                else:
                    oneGuiDay.updateSelected(False)

    def clearSelectedDay(self):
        for oneGuiDay in self.guiDays:
            oneGuiDay.updateSelected(False)

    def filterChanged(self):
        """Refresh our days since the filter has changed."""
        newFilter = self.filterList.getSelectedIndex()
        for guiDay in self.guiDays:
            guiDay.changeFilter(newFilter)