def _init_tabs(self):
		"""Add enough tabbuttons for all widgets."""
		# Load buttons
		for index, tab in enumerate(self._tabs):
			container = pychan.Container()
			background = pychan.Icon()
			background.image = tab.button_background_image
			button = TooltipButton()
			button.name = index
			if self.current_tab is tab:
				button.up_image = tab.button_active_image
			else:
				button.up_image = tab.button_up_image
			button.down_image = tab.button_down_image
			button.hover_image = tab.button_hover_image
			button.size = (50, 50)
			button.capture(pychan.tools.callbackWithArguments(self._show_tab, index))
			if hasattr(tab, 'tooltip') and tab.tooltip is not None:
				button.tooltip = unicode(tab.tooltip)
			container.size = background.size
			container.addChild(background)
			container.addChild(button)
			self.content.addChild(container)
		self.widget.size = (50, 200*len(self._tabs))
		self.widget.adaptLayout()
Exemple #2
0
def create_resource_selection_dialog(on_click, inventory, db, widget='select_trade_resource.xml', res_filter=None):
	"""Returns a container containing resource icons
	@param on_click: called with resource id as parameter on clicks
	@param inventory: to determine fill status of resource slots
	@param db: main db instance
	@param widget: which xml file to use as a template. Default: tabwidget. Required
	               since the resource bar also uses this code (no tabs there though).
	@param res_filter: callback to decide which icons to use. Default: show all
	"""
	from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
	from horizons.gui.widgets.tooltip import TooltipButton
	dummy_icon_path = "content/gui/icons/resources/none_gray.png"

	dlg = load_uh_widget(widget)

	button_width = ImageFillStatusButton.DEFAULT_BUTTON_SIZE[0] # used for dummy button
	vbox = dlg.findChild(name="resources")
	amount_per_line = vbox.width / button_width
	current_hbox = pychan.widgets.HBox(name="hbox_0", padding=0)
	index = 1
	resources = db.get_res(True)

	# Add the zero element to the beginning that allows to remove the currently
	# sold/bought resource
	for res_id in [0] + list(resources):
		# don't show resources that are already in the list
		if res_filter is not None and not res_filter(res_id):
			continue
		# create button (dummy one or real one)
		if res_id == 0:
			button = TooltipButton( size=(button_width, button_width), name="resource_icon_00")
			button.up_image, button.down_image, button.hover_image = (dummy_icon_path,)*3
		else:
			amount = inventory[res_id]
			filled = int(float(inventory[res_id]) / float(inventory.get_limit(res_id)) * 100.0)
			button = ImageFillStatusButton.init_for_res(db, res_id,
			                                            amount=amount, filled=filled,
			                                            use_inactive_icon=False)

		# on click: add this res
		cb = Callback(on_click, res_id)
		if hasattr(button, "button"): # for imagefillstatusbuttons
			button.button.capture( cb )
		else:
			button.capture( cb )

		current_hbox.addChild(button)
		if index % amount_per_line == 0 and index is not 0:
			vbox.addChild(current_hbox)
			current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line),
			                                   padding=0)
		index += 1
	#	current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment
	vbox.addChild(current_hbox)
	vbox.adaptLayout()

	return dlg
	def _add_line_to_gui(self, ship, sequence_number):
		sequence_number_label = widgets.Label(name = 'sequence_number_%d' % ship.worldid)
		sequence_number_label.text = unicode(sequence_number)
		sequence_number_label.min_size = sequence_number_label.max_size = (15, 20)

		ship_name = widgets.Label(name = 'ship_name_%d' % ship.worldid)
		ship_name.text = unicode(ship.get_component(NamedComponent).name)
		ship_name.min_size = ship_name.max_size = (100, 20)

		rename_icon = TooltipButton(name = 'rename_%d' % ship.worldid)
		rename_icon.up_image = "content/gui/images/background/rename_feather_20.png"
		rename_icon.hover_image = "content/gui/images/background/rename_feather_20_h.png"
		rename_icon.tooltip = _("Click to change the name of this ship")

		ship_type = widgets.Label(name = 'ship_type_%d' % ship.worldid)
		ship_type.text = unicode(ship.classname)
		ship_type.min_size = ship_type.max_size = (60, 20)

		weapons = widgets.Label(name = 'weapons_%d' % ship.worldid)
		if isinstance(ship, FightingShip):
			weapon_list = []
			for weapon_id, amount in sorted(ship.get_weapon_storage()):
				weapon_list.append('%d %s' % (amount, self.session.db.get_res_name(weapon_id)))
			if weapon_list:
				weapons.text = unicode(', '.join(weapon_list))
			else:
				#i18n There are no weapons equipped at the moment.
				weapons.text = _('None')
		else:
			weapons.text = _('N/A')
		weapons.min_size = weapons.max_size = (60, 20)

		health = widgets.Label(name = 'health_%d' % ship.worldid)
		health_component = ship.get_component(HealthComponent)
		health.text = unicode('%d/%d' % (health_component.health, health_component.max_health))
		health.min_size = health.max_size = (65, 20)

		status = widgets.Label(name = 'status_%d' % ship.worldid)
		status.text, status_position = ship.get_status()
		status.min_size = status.max_size = (320, 20)

		hbox = widgets.HBox()
		hbox.addChild(sequence_number_label)
		hbox.addChild(ship_name)
		hbox.addChild(rename_icon)
		hbox.addChild(ship_type)
		hbox.addChild(weapons)
		hbox.addChild(health)
		hbox.addChild(status)
		self._content_vbox.addChild(hbox)
		return (ship_name, rename_icon, status, status_position)
	def _add_line_to_gui(self, settlement, sequence_number):
		sequence_number_label = widgets.Label(name = 'sequence_number_%d' % settlement.worldid)
		sequence_number_label.text = unicode(sequence_number)
		sequence_number_label.min_size = sequence_number_label.max_size = (15, 20)

		name = widgets.Label(name = 'name_%d' % settlement.worldid)
		name.text = unicode(settlement.get_component(NamedComponent).name)
		name.min_size = name.max_size = (175, 20)

		rename_icon = TooltipButton(name = 'rename_%d' % settlement.worldid)
		rename_icon.up_image = "content/gui/images/background/rename_feather_20.png"
		rename_icon.hover_image = "content/gui/images/background/rename_feather_20_h.png"
		rename_icon.tooltip = _("Click to change the name of your settlement")

		self._add_generic_line_to_gui(settlement.worldid, [sequence_number_label, name, rename_icon],
			settlement.inhabitants, settlement.cumulative_taxes, settlement.cumulative_running_costs)
		return name, rename_icon
	def show_resource_menu(self, slot_id):
		self.resources = load_xml_translated('select_trade_resource.xml')
		self.resources.position = self.widget.position
		button_width = 50
		vbox = self.resources.findChild(name="resources")
		current_hbox = pychan.widgets.HBox(padding = 2)
		index = 1
		resources = horizons.main.db.get_res_id_and_icon(True)
		# Add the zero element to the beginning that allows to remove the currently sold/bought resource
		for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources):
			if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list:
				continue # don't show resources that are already in the list
			button = TooltipButton(size=(50, 50))
			button.up_image, button.down_image, button.hover_image = icon, icon, icon
			if res_id == 0:
				button.tooltip = u""
			else:
				button.tooltip = horizons.main.db.get_res_name(res_id)
			button.capture(Callback(self.add_resource, res_id, slot_id))
			current_hbox.addChild(button)
			if index % (vbox.width/(button_width)) == 0 and index is not 0:
				vbox.addChild(current_hbox)
				current_hbox = pychan.widgets.HBox(padding=0)
			index += 1
		vbox.addChild(current_hbox)
		vbox.adaptLayout()
		self.resources.addChild(vbox)
		self.resources.stylize('headline')
		self.hide()
		self.resources.show()
		self.settlement.session.ingame_gui.minimap_to_front()
	def show_resource_menu(self, slot_id):
		self.resources = load_xml_translated('select_trade_resource.xml')
		self.resources.position = self.widget.position
		button_width = 50
		vbox = self.resources.findChild(name="resources")
		amount_per_line = vbox.width / button_width
		current_hbox = pychan.widgets.HBox(name="hbox_0")
		index = 1
		resources = horizons.main.db.get_res_id_and_icon(True)
		# Add the zero element to the beginning that allows to remove the currently sold/bought resource
		for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources):
			if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list:
				continue # don't show resources that are already in the list
			button = TooltipButton( size=(button_width, button_width), \
			                        name="resource_icon_%02d" % res_id )
			button.up_image, button.down_image, button.hover_image = icon, icon, icon
			if res_id == 0:
				button.tooltip = u""
			else:
				button.tooltip = horizons.main.db.get_res_name(res_id)
			button.capture(Callback(self.add_resource, res_id, slot_id))
			current_hbox.addChild(button)
			if index % amount_per_line == 0 and index is not 0:
				vbox.addChild(current_hbox)
				current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line) )
			index += 1
#		current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment
		vbox.addChild(current_hbox)
		vbox.adaptLayout()
		self.hide() # hides tab that invoked the selection widget
		self.resources.show() # show selection widget, still display old tab icons
		self.settlement.session.ingame_gui.minimap_to_front()
 def show_resource_menu(self, slot_id):
     self.resources = load_xml_translated("select_trade_resource.xml")
     self.resources.position = self.widget.position
     button_width = 50
     vbox = self.resources.findChild(name="resources")
     amount_per_line = vbox.width / button_width
     current_hbox = pychan.widgets.HBox(name="hbox_0")
     index = 1
     resources = horizons.main.db.get_res_id_and_icon(True)
     # Add the zero element to the beginning that allows to remove the currently sold/bought resource
     for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources):
         if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list:
             continue  # don't show resources that are already in the list
         button = TooltipButton(size=(button_width, button_width), name="resource_icon_%02d" % res_id)
         button.up_image, button.down_image, button.hover_image = icon, icon, icon
         if res_id == 0:
             button.tooltip = u""
         else:
             button.tooltip = horizons.main.db.get_res_name(res_id)
         button.capture(Callback(self.add_resource, res_id, slot_id))
         current_hbox.addChild(button)
         if index % amount_per_line == 0 and index is not 0:
             vbox.addChild(current_hbox)
             current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line))
         index += 1
     # 		current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment
     vbox.addChild(current_hbox)
     vbox.adaptLayout()
     self.hide()  # hides tab that invoked the selection widget
     self.resources.show()  # show selection widget, still display old tab icons
     self.settlement.session.ingame_gui.minimap_to_front()
Exemple #8
0
 def _draw(self):
     """Draws the icon + bar."""
     self.button = TooltipButton(up_image=self.up_image,
                                 down_image=self.down_image,
                                 hover_image=self.hover_image,
                                 tooltip=self.tooltip)
     label = pychan.widgets.Label(text=self.text)
     label.position = self.text_position
     fill_bar = pychan.widgets.Icon(
         "content/gui/images/tabwidget/green_line.png")
     fill_bar.position = (self.button.width-fill_bar.width-1, \
                          self.button.height-int(self.button.height/100.0*self.filled))
     self.addChildren(self.button, fill_bar, label)
	def show_resource_menu(self, slot, entry):

		position = self.widgets.index(entry)
		if self.resource_menu_shown:
			self.hide_resource_menu()
		self.resource_menu_shown = True
		vbox = self._gui.findChild(name="resources")
		label = self._gui.findChild(name="select_res_label")
		label.text = unicode("Select Resources")

		#hardcoded for 5 works better than vbox.width / button_width
		amount_per_line = 5

		current_hbox = widgets.HBox()
		index = 1

		for res_id in self.icon_for_resource:
			if res_id in self.instance.route.waypoints[position]['resource_list'] and\
			   slot.findChild(name='button').up_image.source is not self.icon_for_resource[res_id]:
				continue
			button = TooltipButton(size=(50,50))
			icon = self.icon_for_resource[res_id]
			button.up_image, button.down_image, button.hover_image = icon, icon, icon
			button.capture(Callback(self.add_resource, slot, res_id, entry))
			if res_id != 0:
				button.tooltip = horizons.main.db.get_res_name(res_id)
			current_hbox.addChild(button)
			if index > amount_per_line:
				index -= amount_per_line
				vbox.addChild(current_hbox)
				current_hbox = widgets.HBox()
			index += 1
		vbox.addChild(current_hbox)

		self.hide()
		self.show()
Exemple #10
0
 def _init_tabs(self):
     """Add enough tabbuttons for all widgets."""
     # Load buttons
     for index, tab in enumerate(self._tabs):
         tab.add_remove_listener(self.hide)
         container = pychan.Container()
         background = pychan.Icon()
         background.name = "bg_%s" % index
         button = TooltipButton()
         button.name = index
         if self.current_tab is tab:
             background.image = tab.button_background_image_active
             button.up_image = tab.button_active_image
         else:
             background.image = tab.button_background_image
             button.up_image = tab.button_up_image
         button.down_image = tab.button_down_image
         button.hover_image = tab.button_hover_image
         button.is_focusable = False
         button.size = (50, 50)
         button.capture(Callback(self._show_tab, index))
         if hasattr(tab, "tooltip") and tab.tooltip is not None:
             button.tooltip = unicode(tab.tooltip)
         container.size = background.size
         container.addChild(background)
         container.addChild(button)
         self.content.addChild(container)
     self.widget.size = (50, 55 * len(self._tabs))
     self.widget.adaptLayout()
 def _init_tabs(self):
     """Add enough tabbuttons for all widgets."""
     # Load buttons
     for index, tab in enumerate(self._tabs):
         container = pychan.Container()
         background = pychan.Icon()
         background.image = tab.button_background_image
         button = TooltipButton()
         button.name = index
         if self.current_tab is tab:
             button.up_image = tab.button_active_image
         else:
             button.up_image = tab.button_up_image
         button.down_image = tab.button_down_image
         button.hover_image = tab.button_hover_image
         button.is_focusable = False
         button.size = (50, 50)
         button.capture(Callback(self._show_tab, index))
         if hasattr(tab, 'tooltip') and tab.tooltip is not None:
             button.tooltip = unicode(tab.tooltip)
         container.size = background.size
         container.addChild(background)
         container.addChild(button)
         self.content.addChild(container)
     self.widget.size = (50, 200 * len(self._tabs))
     self.widget.adaptLayout()
	def show_resource_menu(self, slot, entry):
		position = self.widgets.index(entry)
		if self.resource_menu_shown:
			self.hide_resource_menu()
		self.resource_menu_shown = True

		vbox = self._gui.findChild(name="resources")
		lbl = widgets.Label(name='select_res_label', text=_('Select a resource:'))
		vbox.addChild( lbl )

		scrollarea = widgets.ScrollArea()
		res_box = widgets.VBox()
		scrollarea.addChild(res_box)
		vbox.addChild(scrollarea)

		# TODO: use create_resource_selection_dialog from util/gui.py

		#hardcoded for 5 works better than vbox.width / button_width
		amount_per_line = 5

		current_hbox = widgets.HBox(max_size="326,46")
		index = 1

		settlement = entry.settlement()
		inventory = settlement.get_component(StorageComponent).inventory if settlement else None
		from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton

		for res_id in sorted(self.icon_for_resource):
			if res_id in self.instance.route.waypoints[position]['resource_list'] and \
			   slot.findChild(name='button').up_image.source is not self.icon_for_resource[res_id]:
				continue
			cb = Callback(self.add_resource, slot, res_id, entry)
			if res_id == 0 or inventory is None: # no fillbar e.g. on dead settlement (shouldn't happen) or dummy slot
				button = TooltipButton(size=(46,46))
				icon = self.icon_for_resource[res_id]
				button.up_image, button.down_image, button.hover_image = icon, icon, icon
				button.capture(cb)
			else: # button with fillbar
				amount = inventory[res_id]
				filled = int(float(inventory[res_id]) / float(inventory.get_limit(res_id)) * 100.0)
				button = ImageFillStatusButton.init_for_res(self.session.db, res_id,
			                                            	amount=amount, filled=filled,
			                                            	use_inactive_icon=False)
				button.button.capture(cb)


			current_hbox.addChild(button)
			if index >= amount_per_line:
				index -= amount_per_line
				res_box.addChild(current_hbox)
				current_hbox = widgets.HBox(max_size="326,26")
			index += 1
		current_hbox.addSpacer(widgets.Spacer())
		#TODO this spacer does absolutely nothing.
		res_box.addChild(current_hbox)

		self._gui.adaptLayout()
		scrollarea.max_width = 316
		scrollarea.width = 316
		vbox.max_width = 316
		vbox.width = 316