def set_construction_mode(self, resource_source_instance, build_costs):
        """Show resources relevant to construction and build costs
		@param resource_source_instance: object with StorageComponent
		@param build_costs: dict, { res : amount }
		"""
        if resource_source_instance is None:
            # Build moved out of settlement. This is usually not sane and an interaction error.
            # Use this heuristically computed settlement to fix preconditions.
            resource_source_instance = LastActivePlayerSettlementManager().get()
        if (
            self.construction_mode
            and resource_source_instance == self.current_instance()
            and build_costs == self._last_build_costs
        ):
            return  # now that's not an update

        self._last_build_costs = build_costs

        self.construction_mode = True
        self.set_inventory_instance(resource_source_instance, keep_construction_mode=True)

        # label background icons
        cost_icon_gold = "content/gui/images/background/widgets/resbar_stats_bottom.png"
        cost_icon_res = "content/gui/images/background/widgets/res_extra_bg.png"

        res_list = self._get_current_resources()

        # remove old one before, avoids duplicates
        self._drop_cost_labels()

        for res, amount in build_costs.iteritems():
            assert res in res_list or res == RES.GOLD

            cost_label = Label(text=u"-" + unicode(amount))
            cost_label.stylize(self.__class__.STYLE)
            # add icon below end of background icon
            if res in res_list:
                entry = res_list.index(res)
                cur_gui = self.gui[entry]
                reference_icon = cur_gui.findChild(name="background_icon")
                below = reference_icon.size[1]
                cost_icon = Icon(image=cost_icon_res, position=(0, below))
                cost_label.position = (15, below)  # TODO: centering

                cur_gui.addChild(cost_icon)
                cur_gui.addChild(cost_label)
                cur_gui.cost_gui = [cost_label, cost_icon]

                cur_gui.resizeToContent()  # container needs to be bigger now
            else:  # must be gold
                # there is an icon with scales there, use its positioning
                reference_icon = self.gold_gui.child_finder("balance_background")
                cost_icon = Icon(image=cost_icon_gold, position=(reference_icon.x, reference_icon.y))
                cost_label.position = (23, 74)  # TODO: centering

                self.gold_gui.addChild(cost_icon)
                self.gold_gui.addChild(cost_label)
                self.gold_gui.cost_gui = [cost_label, cost_icon]

                self.gold_gui.resizeToContent()
Пример #2
0
	def set_construction_mode(self, resource_source_instance, build_costs):
		"""Show resources relevant to construction and build costs
		@param resource_source_instance: object with StorageComponent
		@param build_costs: dict, { res : amount }
		"""
		if resource_source_instance is None:
			# Build moved out of settlement. This is usually not sane and an interaction error.
			# Use this heuristically computed settlement to fix preconditions.
			resource_source_instance = LastActivePlayerSettlementManager().get()
		if self.construction_mode and \
		   resource_source_instance == self.current_instance() and \
		   build_costs == self._last_build_costs:
			return # now that's not an update

		self._last_build_costs = build_costs

		self.construction_mode = True
		self.set_inventory_instance(resource_source_instance, keep_construction_mode=True)

		# label background icons
		cost_icon_gold = "content/gui/images/background/widgets/resbar_stats_bottom.png"
		cost_icon_res = "content/gui/images/background/widgets/res_extra_bg.png"

		res_list = self._get_current_resources()

		# remove old one before, avoids duplicates
		self._drop_cost_labels()

		for res, amount in build_costs.iteritems():
			assert res in res_list or res == RES.GOLD

			cost_label = Label(text=u"-"+unicode(amount))
			cost_label.stylize( self.__class__.STYLE )
			# add icon below end of background icon
			if res in res_list:
				entry = res_list.index(res)
				cur_gui = self.gui[ entry ]
				reference_icon = cur_gui.findChild(name="background_icon")
				below = reference_icon.size[1]
				cost_icon = Icon(image=cost_icon_res, position=(0, below))
				cost_label.position = (15, below) # TODO: centering

				cur_gui.addChild(cost_icon)
				cur_gui.addChild(cost_label)
				cur_gui.cost_gui = [cost_label, cost_icon]

				cur_gui.resizeToContent() # container needs to be bigger now
			else: # must be gold
				# there is an icon with scales there, use its positioning
				reference_icon = self.gold_gui.child_finder("balance_background")
				cost_icon = Icon(image=cost_icon_gold, position=(reference_icon.x, reference_icon.y))
				cost_label.position = (23, 74) # TODO: centering

				self.gold_gui.addChild(cost_icon)
				self.gold_gui.addChild(cost_label)
				self.gold_gui.cost_gui = [cost_label, cost_icon]

				self.gold_gui.resizeToContent()
Пример #3
0
	def _draw(self, vbox, current_hbox, index=0):
		"""Draws the inventory. """
		for resid, limit in sorted(self._limits.iteritems()):
			if self._selling:
				amount = max(0, self._inventory[resid] - limit)
			else:
				amount = max(0, limit - self._inventory[resid])

			# check if this res should be displayed
			button = ImageFillStatusButton.init_for_res(self.db, resid, amount,
			                                            filled=0, uncached=self.uncached)
			button.button.name = "buy_sell_inventory_%s_entry_%s" % (self._selling, index) # for tests
			current_hbox.addChild(button)

			if index % self.items_per_line == self.items_per_line - 1:
				vbox.addChild(current_hbox)
				current_hbox = HBox(padding = 0)
			index += 1
		vbox.addChild(current_hbox)
		self.addChild(vbox)

		label = Label()
		#xgettext:python-format
		label.text = _('Limit: {amount}t per slot').format(amount=self._inventory.get_limit(None))
		label.position = (110, 150)
		self.__icon.position = (90, 150)
		self.addChildren(label, self.__icon)
Пример #4
0
    def build_ship_info(self, index, ship, prodline):
        size = (260, 90)
        widget = Container(name='showcase_%s' % index,
                           position=(0, 20 + index * 90),
                           min_size=size,
                           max_size=size,
                           size=size)
        bg_icon = Icon(image='content/gui/images/background/square_80.png',
                       name='bg_%s' % index)
        widget.addChild(bg_icon)

        image = 'content/gui/images/objects/ships/76/{unit_id}.png'.format(
            unit_id=ship)
        helptext = self.instance.session.db.get_ship_tooltip(ship)
        unit_icon = Icon(image=image,
                         name='icon_%s' % index,
                         position=(2, 2),
                         helptext=helptext)
        widget.addChild(unit_icon)

        # if not buildable, this returns string with reason why to be displayed as helptext
        #ship_unbuildable = self.is_ship_unbuildable(ship)
        ship_unbuildable = False
        if not ship_unbuildable:
            button = OkButton(position=(60, 50),
                              name='ok_%s' % index,
                              helptext=_('Build this ship!'))
            button.capture(Callback(self.start_production, prodline))
        else:
            button = CancelButton(position=(60, 50),
                                  name='ok_%s' % index,
                                  helptext=ship_unbuildable)

        widget.addChild(button)

        # Get production line info
        production = self.producer.create_production_line(prodline)
        # consumed == negative, reverse to sort in *ascending* order:
        costs = sorted(production.consumed_res.iteritems(), key=itemgetter(1))
        for i, (res, amount) in enumerate(costs):
            xoffset = 103 + (i % 2) * 55
            yoffset = 20 + (i // 2) * 20
            icon = create_resource_icon(res, self.instance.session.db)
            icon.max_size = icon.min_size = icon.size = (16, 16)
            icon.position = (xoffset, yoffset)
            label = Label(name='cost_%s_%s' % (index, i))
            if res == RES.GOLD:
                label.text = unicode(-amount)
            else:
                label.text = u'{amount:02}t'.format(amount=-amount)
            label.position = (22 + xoffset, yoffset)
            widget.addChild(icon)
            widget.addChild(label)
        return widget
Пример #5
0
	def build_ship_info(self, index, ship, prodline):
		size = (260, 90)
		widget = Container(name='showcase_%s' % index, position=(0, 20 + index*90),
		                   min_size=size, max_size=size, size=size)
		bg_icon = Icon(image='content/gui/images/background/square_80.png', name='bg_%s'%index)
		widget.addChild(bg_icon)

		icon_path = 'content/gui/images/objects/ships/76/{unit_id}.png'.format(unit_id=ship)
		helptext = self.instance.session.db.get_ship_tooltip(ship)
		unit_icon = Icon(image=icon_path, name='icon_%s'%index, position=(2, 2),
		                 helptext=helptext)
		widget.addChild(unit_icon)

		# if not buildable, this returns string with reason why to be displayed as helptext
		#ship_unbuildable = self.is_ship_unbuildable(ship)
		ship_unbuildable = False
		if not ship_unbuildable:
			button = OkButton(position=(60, 50), name='ok_%s'%index, helptext=_('Build this ship!'))
			button.capture(Callback(self.start_production, prodline))
		else:
			button = CancelButton(position=(60, 50), name='ok_%s'%index,
			helptext=ship_unbuildable)

		widget.addChild(button)

		#TODO since this code uses the boat builder as producer, the
		# gold cost of ships is in consumed res is always 0 since it
		# is paid from player inventory, not from the boat builder one.
		production = self.producer.create_production(prodline)
		# consumed == negative, reverse to sort in *ascending* order:
		costs = sorted(production.get_consumed_resources().iteritems(), key=itemgetter(1))
		for i, (res, amount) in enumerate(costs):
			xoffset = 103 + (i  % 2) * 55
			yoffset =  20 + (i // 2) * 20
			icon = create_resource_icon(res, self.instance.session.db)
			icon.max_size = icon.min_size = icon.size = (16, 16)
			icon.position = (xoffset, yoffset)
			label = Label(name='cost_%s_%s' % (index, i))
			if res == RES.GOLD:
				label.text = unicode(-amount)
			else:
				label.text = u'{amount:02}t'.format(amount=-amount)
			label.position = (22 + xoffset, yoffset)
			widget.addChild(icon)
			widget.addChild(label)
		return widget
Пример #6
0
	def build_groundunit_info(self, index, groundunit, prodline):
		size = (260, 90)
		widget = Container(name='showcase_%s' % index, position=(0, 20 + index*90),
		                   min_size=size, max_size=size, size=size)
		bg_icon = Icon(image='content/gui/images/background/square_80.png', name='bg_%s'%index)
		widget.addChild(bg_icon)

		image = 'content/gui/images/objects/groundunit/76/{unit_id}.png'.format(unit_id=groundunit)
		helptext = self.instance.session.db.get_unit_tooltip(groundunit)
		unit_icon = Icon(image=image, name='icon_%s'%index, position=(2, 2),
		                 helptext=helptext)
		widget.addChild(unit_icon)

		# if not buildable, this returns string with reason why to be displayed as helptext
		#groundunit_unbuildable = self.is_groundunit_unbuildable(groundunit)
		groundunit_unbuildable = False
		if not groundunit_unbuildable:
			button = OkButton(position=(60, 50), name='ok_%s'%index, helptext=_('Build this groundunit!'))
			button.capture(Callback(self.start_production, prodline))
		else:
			button = CancelButton(position=(60, 50), name='ok_%s'%index,
			helptext=groundunit_unbuildable)

		widget.addChild(button)

		# Get production line info
		production = self.producer.create_production_line(prodline)
		# consumed == negative, reverse to sort in *ascending* order:
		costs = sorted(production.consumed_res.iteritems(), key=itemgetter(1))
		for i, (res, amount) in enumerate(costs):
			xoffset = 103 + (i  % 2) * 55
			yoffset =  20 + (i // 2) * 20
			icon = create_resource_icon(res, self.instance.session.db)
			icon.max_size = icon.min_size = icon.size = (16, 16)
			icon.position = (xoffset, yoffset)
			label = Label(name='cost_%s_%s' % (index, i))
			if res == RES.GOLD:
				label.text = unicode(-amount)
			else:
				label.text = u'{amount:02}t'.format(amount=-amount)
			label.position = (22 + xoffset, yoffset)
			widget.addChild(icon)
			widget.addChild(label)
		return widget
Пример #7
0
    def _draw(self, vbox, current_hbox, index=0):
        """Draws the inventory."""
        # add res to res order in case there are new ones
        # (never remove old ones for consistent positioning)
        new_res = sorted(resid for resid in self._inventory.iterslots() if resid not in self._res_order)

        if isinstance(self._inventory, PositiveTotalNumSlotsStorage):
            # limited number of slots. We have to switch unused slots with newly added ones on overflow

            while len(self._res_order) + len(new_res) > self._inventory.slotnum:
                for i in xrange(self._inventory.slotnum):
                    # search empty slot
                    if not self._inventory[self._res_order[i]]:
                        # insert new res here
                        self._res_order[i] = new_res.pop(0)
                        if not new_res:
                            break  # all done

                            # add remaining slots for slotstorage or just add it without consideration for other storage kinds
        self._res_order += new_res

        for resid in self._res_order:
            # check if this res should be displayed
            if not self.db.cached_query("SELECT shown_in_inventory FROM resource WHERE id = ?", resid)[0][0]:
                continue

            amount = self._inventory[resid]

            if self.ordinal:
                lower, upper = self.ordinal.get(resid, (0, 100))
                filled = (100 * (amount - lower)) // (upper - lower)
                amount = ""  # do not display exact information for resource deposits
            elif isinstance(self._inventory, TotalStorage):
                filled = 0
            else:
                filled = (100 * amount) // self._inventory.get_limit(resid)

            button = ImageFillStatusButton.init_for_res(self.db, resid, amount, filled=filled, uncached=self.uncached)
            button.button.name = "inventory_entry_%s" % index  # required for gui tests
            current_hbox.addChild(button)

            if index % self.items_per_line == self.items_per_line - 1:
                vbox.addChild(current_hbox)
                current_hbox = HBox(padding=0)
            index += 1
        if index <= self.items_per_line:  # Hide/Remove second line
            icons = self.parent.findChildren(name="slot")
            if len(icons) > self.items_per_line:
                self.parent.removeChildren(icons[self.items_per_line - 1 :])
        vbox.addChild(current_hbox)
        self.addChild(vbox)
        height = ImageFillStatusButton.CELL_SIZE[1] * len(self._res_order) // self.items_per_line
        self.min_size = (self.min_size[0], height)

        if isinstance(self._inventory, TotalStorage):
            # if it's full, the additional slots have to be marked as unusable (#1686)
            # check for any res, the res type doesn't matter here
            if not self._inventory.get_free_space_for(0):
                for i in xrange(index, self.items_per_line):
                    button = Icon(image=self.__class__.UNUSABLE_SLOT_IMAGE)
                    current_hbox.addChild(button)

        if self.display_legend:
            if isinstance(self._inventory, TotalStorage):
                # Add total storage indicator
                sum_stored_res = self._inventory.get_sum_of_stored_resources()
                label = Label()
                label.text = unicode(sum_stored_res) + u"/" + unicode(self._inventory.get_limit(None))
                label.position = (150, 53)
                self.__icon.position = (130, 53)
                self.addChildren(label, self.__icon)
            elif isinstance(self._inventory, PositiveSizedSlotStorage):
                label = Label()
                # xgettext:python-format
                label.text = _("Limit: {amount}t per slot").format(amount=self._inventory.get_limit(None))
                label.position = (20, 203)
                self.__icon.position = (0, 203)
                self.addChildren(label, self.__icon)