示例#1
0
def init_pychan():
    """General pychan initiation for uh"""

    # register custom widgets

    from horizons.gui.widgets.inventory import Inventory
    from horizons.gui.widgets.buysellinventory import BuySellInventory
    from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
    from horizons.gui.widgets.progressbar import ProgressBar
    from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
    from horizons.gui.widgets.tooltip import TooltipIcon, TooltipButton, TooltipLabel, TooltipProgressBar
    from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
    from horizons.gui.widgets.icongroup import TabBG
    from horizons.gui.widgets.stepslider import StepSlider
    from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
    from horizons.gui.widgets.container import AutoResizeContainer

    widgets = [
        OkButton,
        CancelButton,
        DeleteButton,
        Inventory,
        BuySellInventory,
        ImageFillStatusButton,
        ProgressBar,
        StepSlider,
        TabBG,
        ToggleImageButton,
        TooltipIcon,
        TooltipButton,
        TooltipLabel,
        TooltipProgressBar,
        HealthWidget,
        StanceWidget,
        WeaponStorageWidget,
        AutoResizeContainer,
    ]

    for widget in widgets:
        pychan.widgets.registerWidget(widget)

        # style
    for name, stylepart in STYLES.iteritems():
        pychan.manager.addStyle(name, stylepart)
def init_pychan():
	"""General pychan initiation for uh"""
	global STYLES

	# quick hack to allow up_image/down_image values to be unicode
	# TODO solve this problem in a better way (e.g. passing str explicitly)
	# or waiting for a fix of http://github.com/fifengine/fifengine/issues/701
	from fife.extensions.pychan.properties import ImageProperty

	def patch_imageproperty(func):
		def wrapper(self, obj, image):
			if isinstance(image, unicode):
				image = str(image)
			return func(self, obj, image)
		return wrapper

	ImageProperty.__set__ = patch_imageproperty(ImageProperty.__set__)

	# register custom widgets
	from horizons.gui.widgets.inventory import Inventory
	from horizons.gui.widgets.buysellinventory import BuySellInventory
	from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
	from horizons.gui.widgets.progressbar import ProgressBar, TilingProgressBar
	# additionally, ImageButton is imported from widgets.imagebutton above
	from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, MainmenuButton, OkButton
	from horizons.gui.widgets.icongroup import TabBG, TilingHBox, hr
	from horizons.gui.widgets.stepslider import StepSlider
	from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
	from horizons.gui.widgets.container import AutoResizeContainer
	from horizons.gui.widgets.tooltip import _Tooltip

	widgets = [OkButton, CancelButton, DeleteButton, MainmenuButton,
	           Inventory, BuySellInventory, ImageFillStatusButton,
	           ProgressBar, StepSlider, TabBG,
	           HealthWidget, StanceWidget, WeaponStorageWidget,
	           AutoResizeContainer, RenameLabel, RenameImageButton,
	           TilingHBox, TilingProgressBar, hr,
			 # This overwrites the ImageButton provided by FIFE!
	           ImageButton,
	           ]

	for widget in widgets:
		pychan.widgets.registerWidget(widget)

	# add uh styles
	for name, stylepart in STYLES.iteritems():
		pychan.manager.addStyle(name, stylepart)

	# patch default widgets
	for name, widget in pychan.widgets.WIDGETS.items():

		def catch_gcn_exception_decorator(func):
			@functools.wraps(func)
			def wrapper(*args, **kwargs):
				try:
					# only apply usable args, else it would crash when called through fife timers
					pychan.tools.applyOnlySuitable(func, *args, **kwargs)
				except RuntimeError as e:
					handle_gcn_exception(e)
			return wrapper

		widget.hide = catch_gcn_exception_decorator(widget.hide)

	from fife.extensions.pychan import Label, Icon, VBox, HBox
	# this is white list of widgets with tooltip.
	widgets_with_tooltip = [Label, Icon, HBox, VBox,
	                        ImageButton, AutoResizeContainer]

	for widget in widgets_with_tooltip:
		# Copy everything we need from the tooltip class (manual mixin).
		# TODO: Figure out if it is safe to use this instead:
		# widget.__bases__ += (_Tooltip, )
		for key, value in _Tooltip.__dict__.iteritems():
			if not key.startswith("__"):
				setattr(widget, key, value)

		def add_tooltip_init(func):
			@functools.wraps(func)
			def wrapper(self, *args, **kwargs):
				func(self, *args, **kwargs)
				self.init_tooltip()
			return wrapper

		widget.__init__ = add_tooltip_init(widget.__init__)

		# these sometimes fail with "No focushandler set (did you add the widget to the gui?)."
		# see #1597 and #1647
		widget.requestFocus = catch_gcn_exception_decorator(widget.requestFocus)

	# FIXME hack pychan's text2gui function, it does an isinstance check that breaks
	# the lazy string from horizons.i18n. we should be passing unicode to
	# widgets all the time, therefore we don't need the additional check.
	def text2gui(text):
		unicodePolicy = horizons.globals.fife.pychan.manager.unicodePolicy
		return text.encode("utf8",*unicodePolicy).replace("\t"," "*4).replace("[br]","\n")

	pychan.widgets.textfield.text2gui = text2gui
	pychan.widgets.basictextwidget.text2gui = text2gui


	setup_cursor_change_on_hover()

	setup_trigger_signals_on_action()

	setup_trigger_signals_on_hover()
示例#3
0
def init_pychan():
    """General pychan initiation for uh"""
    global STYLES

    # quick hack to allow up_image/down_image values to be unicode
    # TODO solve this problem in a better way (e.g. passing str explicitly)
    # or waiting for a fix of http://github.com/fifengine/fifengine/issues/701
    from fife.extensions.pychan.properties import ImageProperty

    def patch_imageproperty(func):
        def wrapper(self, obj, image):
            if isinstance(image, unicode):
                image = str(image)
            return func(self, obj, image)

        return wrapper

    ImageProperty.__set__ = patch_imageproperty(ImageProperty.__set__)

    # register custom widgets
    from horizons.gui.widgets.inventory import Inventory
    from horizons.gui.widgets.buysellinventory import BuySellInventory
    from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
    from horizons.gui.widgets.progressbar import ProgressBar, TilingProgressBar
    # additionally, ImageButton is imported from widgets.imagebutton above
    from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, MainmenuButton, OkButton
    from horizons.gui.widgets.icongroup import TabBG, TilingHBox, hr
    from horizons.gui.widgets.stepslider import StepSlider
    from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
    from horizons.gui.widgets.container import AutoResizeContainer
    from horizons.gui.widgets.tooltip import _Tooltip

    widgets = [
        OkButton,
        CancelButton,
        DeleteButton,
        MainmenuButton,
        Inventory,
        BuySellInventory,
        ImageFillStatusButton,
        ProgressBar,
        StepSlider,
        TabBG,
        HealthWidget,
        StanceWidget,
        WeaponStorageWidget,
        AutoResizeContainer,
        RenameLabel,
        RenameImageButton,
        TilingHBox,
        TilingProgressBar,
        hr,
        # This overwrites the ImageButton provided by FIFE!
        ImageButton,
    ]

    for widget in widgets:
        pychan.widgets.registerWidget(widget)

    # add uh styles
    for name, stylepart in STYLES.iteritems():
        pychan.manager.addStyle(name, stylepart)

    # patch default widgets
    for name, widget in pychan.widgets.WIDGETS.items():

        def catch_gcn_exception_decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                try:
                    # only apply usable args, else it would crash when called through fife timers
                    pychan.tools.applyOnlySuitable(func, *args, **kwargs)
                except RuntimeError as e:
                    handle_gcn_exception(e)

            return wrapper

        widget.hide = catch_gcn_exception_decorator(widget.hide)

    from fife.extensions.pychan import Label, Icon, VBox, HBox
    # this is white list of widgets with tooltip.
    widgets_with_tooltip = [
        Label, Icon, HBox, VBox, ImageButton, AutoResizeContainer
    ]

    for widget in widgets_with_tooltip:
        # Copy everything we need from the tooltip class (manual mixin).
        # TODO: Figure out if it is safe to use this instead:
        # widget.__bases__ += (_Tooltip, )
        for key, value in _Tooltip.__dict__.iteritems():
            if not key.startswith("__"):
                setattr(widget, key, value)

        def add_tooltip_init(func):
            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                func(self, *args, **kwargs)
                self.init_tooltip()

            return wrapper

        widget.__init__ = add_tooltip_init(widget.__init__)

        # these sometimes fail with "No focushandler set (did you add the widget to the gui?)."
        # see #1597 and #1647
        widget.requestFocus = catch_gcn_exception_decorator(
            widget.requestFocus)

    # FIXME hack pychan's text2gui function, it does an isinstance check that breaks
    # the lazy string from horizons.i18n. we should be passing unicode to
    # widgets all the time, therefore we don't need the additional check.
    def text2gui(text):
        unicodePolicy = horizons.globals.fife.pychan.manager.unicodePolicy
        return text.encode("utf8", *unicodePolicy).replace("\t",
                                                           " " * 4).replace(
                                                               "[br]", "\n")

    pychan.widgets.textfield.text2gui = text2gui
    pychan.widgets.basictextwidget.text2gui = text2gui

    setup_cursor_change_on_hover()

    setup_trigger_signals_on_action()

    setup_trigger_signals_on_hover()
示例#4
0
def init_pychan():
	"""General pychan initiation for uh"""
	global STYLES

	# quick hack to allow up_image/down_image values to be unicode
	# TODO solve this problem in a better way (e.g. passing str explicitly)
	# or waiting for a fix of http://fife.trac.cvsdude.com/engine/ticket/701
	from fife.extensions.pychan.properties import ImageProperty

	def patch_imageproperty(func):
		def wrapper(self, obj, image):
			if isinstance(image, unicode):
				image = str(image)
			return func(self, obj, image)
		return wrapper

	ImageProperty.__set__ = patch_imageproperty(ImageProperty.__set__)

	# register custom widgets
	from horizons.gui.widgets.inventory import Inventory
	from horizons.gui.widgets.buysellinventory import BuySellInventory
	from horizons.gui.widgets.imagefillstatusbutton import  ImageFillStatusButton
	from horizons.gui.widgets.progressbar import ProgressBar
	from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
	from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
	from horizons.gui.widgets.icongroup import TabBG
	from horizons.gui.widgets.stepslider import StepSlider
	from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
	from horizons.gui.widgets.container import AutoResizeContainer
	from horizons.gui.widgets.tooltip import _Tooltip

	widgets = [OkButton, CancelButton, DeleteButton,
			   Inventory, BuySellInventory, ImageFillStatusButton,
			   ProgressBar, StepSlider, TabBG, ToggleImageButton,
			   HealthWidget, StanceWidget, WeaponStorageWidget,
	       AutoResizeContainer, RenameLabel, RenameImageButton]

	for widget in widgets:
		pychan.widgets.registerWidget(widget)

	# add uh styles
	# NOTE: do this before adding tooltip feature, because pychan has a design issue
	# where it sometimes uses the class hierarchy and sometimes treats each class differently.
	for name, stylepart in STYLES.iteritems():
		pychan.manager.addStyle(name, stylepart)

	# patch default widgets
	for name, widget in pychan.widgets.WIDGETS.items():

		def catch_gcn_exception_decorator(func):
			@functools.wraps(func)
			def wrapper(*args, **kwargs):
				try:
					# only apply usable args, else it would crash when called through fife timers
					pychan.tools.applyOnlySuitable(func, *args, **kwargs)
				except RuntimeError as e:
					handle_gcn_exception(e)
			return wrapper

		widget.hide = catch_gcn_exception_decorator(widget.hide)

		# support for tooltips via helptext attribute
		if any( attr.name == "helptext" for attr in widget.ATTRIBUTES ):
			# Copy everything we need from the tooltip class (manual mixin).
			# TODO Figure out if it is safe to use this instead:
			#widget.__bases__ += (_Tooltip, )
			for key, value in _Tooltip.__dict__.iteritems():
				if not key.startswith("__"):
					setattr(widget, key, value)

			def add_tooltip_init(func):
				@functools.wraps(func)
				def wrapper(self, *args, **kwargs):
					func(self, *args, **kwargs)
					self.init_tooltip()
				return wrapper

			widget.__init__ = add_tooltip_init(widget.__init__)

			# these sometimes fail with "No focushandler set (did you add the widget to the gui?)."
			# see #1597 and #1647
			widget.requestFocus = catch_gcn_exception_decorator(widget.requestFocus)


	setup_cursor_change_on_hover()

	setup_trigger_signals_on_action()


	# NOTE: there is a bug with the tuple notation: http://fife.trac.cvsdude.com/engine/ticket/656
	# work around this here for now:
	def conv(d):
		entries = []
		for k, v in d.iteritems():
			if isinstance(v, dict): # recurse
				v = conv(v)
			if isinstance(k, tuple): # resolve tuple-notation, add separate keys
				for k_i in k:
					entries.append( (k_i, v) )
			else:
				entries.append( (k, v) )

		return dict(entries)

	# patch uh styles
	STYLES = conv(STYLES)

	# patch fife default styles
	pychan.manager.styles = conv(pychan.manager.styles)
def init_pychan():
	"""General pychan initiation for uh"""
	global STYLES

	# quick hack to allow up_image/down_image values to be unicode
	# TODO solve this problem in a better way (e.g. passing str explicitly)
	# or waiting for a fix of http://fife.trac.cloudforge.com/engine/ticket/701
	from fife.extensions.pychan.properties import ImageProperty

	def patch_imageproperty(func):
		def wrapper(self, obj, image):
			if isinstance(image, unicode):
				image = str(image)
			return func(self, obj, image)
		return wrapper

	ImageProperty.__set__ = patch_imageproperty(ImageProperty.__set__)

	# register custom widgets
	from horizons.gui.widgets.inventory import Inventory
	from horizons.gui.widgets.buysellinventory import BuySellInventory
	from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
	from horizons.gui.widgets.progressbar import ProgressBar
	from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
	# additionally, ImageButton is imported from widgets.imagebutton above
	from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, MainmenuButton, OkButton
	from horizons.gui.widgets.icongroup import TabBG, TilingHBox
	from horizons.gui.widgets.stepslider import StepSlider
	from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
	from horizons.gui.widgets.container import AutoResizeContainer
	from horizons.gui.widgets.tooltip import _Tooltip

	widgets = [OkButton, CancelButton, DeleteButton, MainmenuButton,
	           Inventory, BuySellInventory, ImageFillStatusButton,
	           ProgressBar, StepSlider, TabBG, ToggleImageButton,
	           HealthWidget, StanceWidget, WeaponStorageWidget,
	           AutoResizeContainer, RenameLabel, RenameImageButton,
	           TilingHBox,
			 # This overwrites the ImageButton provided by FIFE!
	           ImageButton,
	           ]

	for widget in widgets:
		pychan.widgets.registerWidget(widget)

	# add uh styles
	for name, stylepart in STYLES.iteritems():
		pychan.manager.addStyle(name, stylepart)

	# patch default widgets
	for name, widget in pychan.widgets.WIDGETS.items():

		def catch_gcn_exception_decorator(func):
			@functools.wraps(func)
			def wrapper(*args, **kwargs):
				try:
					# only apply usable args, else it would crash when called through fife timers
					pychan.tools.applyOnlySuitable(func, *args, **kwargs)
				except RuntimeError as e:
					handle_gcn_exception(e)
			return wrapper

		widget.hide = catch_gcn_exception_decorator(widget.hide)

		# support for tooltips via helptext attribute
		if any( attr.name == "helptext" for attr in widget.ATTRIBUTES ):
			# Copy everything we need from the tooltip class (manual mixin).
			# TODO Figure out if it is safe to use this instead:
			#widget.__bases__ += (_Tooltip, )
			for key, value in _Tooltip.__dict__.iteritems():
				if not key.startswith("__"):
					setattr(widget, key, value)

			def add_tooltip_init(func):
				@functools.wraps(func)
				def wrapper(self, *args, **kwargs):
					func(self, *args, **kwargs)
					self.init_tooltip()
				return wrapper

			widget.__init__ = add_tooltip_init(widget.__init__)

			# these sometimes fail with "No focushandler set (did you add the widget to the gui?)."
			# see #1597 and #1647
			widget.requestFocus = catch_gcn_exception_decorator(widget.requestFocus)


	setup_cursor_change_on_hover()

	setup_trigger_signals_on_action()
示例#6
0
def init_pychan():
    """General pychan initiation for uh"""
    global STYLES

    # register custom widgets

    from horizons.gui.widgets.inventory import Inventory
    from horizons.gui.widgets.buysellinventory import BuySellInventory
    from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
    from horizons.gui.widgets.progressbar import ProgressBar
    from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
    from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
    from horizons.gui.widgets.icongroup import TabBG
    from horizons.gui.widgets.stepslider import StepSlider
    from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
    from horizons.gui.widgets.container import AutoResizeContainer
    from horizons.gui.widgets.tooltip import _Tooltip

    widgets = [
        OkButton,
        CancelButton,
        DeleteButton,
        Inventory,
        BuySellInventory,
        ImageFillStatusButton,
        ProgressBar,
        StepSlider,
        TabBG,
        ToggleImageButton,
        HealthWidget,
        StanceWidget,
        WeaponStorageWidget,
        AutoResizeContainer,
    ]

    for widget in widgets:
        pychan.widgets.registerWidget(widget)

        # add uh styles
        # NOTE: do this before adding tooltip feature, because pychan has a design issue
        # where it sometimes uses the class hierarchy and sometimes treats each class differently.
    for name, stylepart in STYLES.iteritems():
        pychan.manager.addStyle(name, stylepart)

        # patch default widgets
    for name, widget in pychan.widgets.WIDGETS.items():

        def patch_hide(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                try:
                    # only apply usable args, else it would crash when called through fife timers
                    pychan.tools.applyOnlySuitable(func, *args, **kwargs)
                except RuntimeError as e:
                    handle_gcn_exception(e)

            return wrapper

        widget.hide = patch_hide(widget.hide)

        # support for tooltips via helptext attribute
        if any(attr.name == "helptext" for attr in widget.ATTRIBUTES):
            # Copy everything we need from the tooltip class (manual mixin).
            # TODO Figure out if it is safe to use this instead:
            # widget.__bases__ += (_Tooltip, )
            for key, value in _Tooltip.__dict__.iteritems():
                if not key.startswith("__"):
                    setattr(widget, key, value)

            def patch(func):
                @functools.wraps(func)
                def wrapper(self, *args, **kwargs):
                    func(self, *args, **kwargs)
                    self.init_tooltip()

                return wrapper

            widget.__init__ = patch(widget.__init__)

            # NOTE: there is a bug with the tuple notation: http://fife.trac.cvsdude.com/engine/ticket/656
            # work around this here for now:

    def conv(d):
        entries = []
        for k, v in d.iteritems():
            if isinstance(v, dict):  # recurse
                v = conv(v)
            if isinstance(k, tuple):  # resolve tuple-notation, add separate keys
                for k_i in k:
                    entries.append((k_i, v))
            else:
                entries.append((k, v))

        return dict(entries)

        # patch uh styles

    STYLES = conv(STYLES)

    # patch fife default styles
    pychan.manager.styles = conv(pychan.manager.styles)
示例#7
0
def init_pychan():
    """General pychan initiation for uh"""
    global STYLES

    # quick hack to allow up_image/down_image values to be unicode
    # TODO solve this problem in a better way (e.g. passing str explicitly)
    # or waiting for a fix of http://fife.trac.cvsdude.com/engine/ticket/701
    from fife.extensions.pychan.properties import ImageProperty

    def patch_imageproperty(func):
        def wrapper(self, obj, image):
            if isinstance(image, unicode):
                image = str(image)
            return func(self, obj, image)

        return wrapper

    ImageProperty.__set__ = patch_imageproperty(ImageProperty.__set__)

    # register custom widgets
    from horizons.gui.widgets.inventory import Inventory
    from horizons.gui.widgets.buysellinventory import BuySellInventory
    from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton
    from horizons.gui.widgets.progressbar import ProgressBar
    from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
    from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
    from horizons.gui.widgets.icongroup import TabBG, TilingHBox
    from horizons.gui.widgets.stepslider import StepSlider
    from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
    from horizons.gui.widgets.container import AutoResizeContainer
    from horizons.gui.widgets.tooltip import _Tooltip

    widgets = [
        OkButton,
        CancelButton,
        DeleteButton,
        Inventory,
        BuySellInventory,
        ImageFillStatusButton,
        ProgressBar,
        StepSlider,
        TabBG,
        ToggleImageButton,
        HealthWidget,
        StanceWidget,
        WeaponStorageWidget,
        AutoResizeContainer,
        RenameLabel,
        RenameImageButton,
        TilingHBox,
    ]

    for widget in widgets:
        pychan.widgets.registerWidget(widget)

    # add uh styles
    # NOTE: do this before adding tooltip feature, because pychan has a design issue
    # where it sometimes uses the class hierarchy and sometimes treats each class differently.
    for name, stylepart in STYLES.iteritems():
        pychan.manager.addStyle(name, stylepart)

    # patch default widgets
    for name, widget in pychan.widgets.WIDGETS.items():

        def catch_gcn_exception_decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                try:
                    # only apply usable args, else it would crash when called through fife timers
                    pychan.tools.applyOnlySuitable(func, *args, **kwargs)
                except RuntimeError as e:
                    handle_gcn_exception(e)

            return wrapper

        widget.hide = catch_gcn_exception_decorator(widget.hide)

        # support for tooltips via helptext attribute
        if any(attr.name == "helptext" for attr in widget.ATTRIBUTES):
            # Copy everything we need from the tooltip class (manual mixin).
            # TODO Figure out if it is safe to use this instead:
            #widget.__bases__ += (_Tooltip, )
            for key, value in _Tooltip.__dict__.iteritems():
                if not key.startswith("__"):
                    setattr(widget, key, value)

            def add_tooltip_init(func):
                @functools.wraps(func)
                def wrapper(self, *args, **kwargs):
                    func(self, *args, **kwargs)
                    self.init_tooltip()

                return wrapper

            widget.__init__ = add_tooltip_init(widget.__init__)

            # these sometimes fail with "No focushandler set (did you add the widget to the gui?)."
            # see #1597 and #1647
            widget.requestFocus = catch_gcn_exception_decorator(
                widget.requestFocus)

    setup_cursor_change_on_hover()

    setup_trigger_signals_on_action()

    # NOTE: there is a bug with the tuple notation: http://fife.trac.cvsdude.com/engine/ticket/656
    # work around this here for now:
    def conv(d):
        entries = []
        for k, v in d.iteritems():
            if isinstance(v, dict):  # recurse
                v = conv(v)
            if isinstance(k,
                          tuple):  # resolve tuple-notation, add separate keys
                for k_i in k:
                    entries.append((k_i, v))
            else:
                entries.append((k, v))

        return dict(entries)

    # patch uh styles
    STYLES = conv(STYLES)

    # patch fife default styles
    pychan.manager.styles = conv(pychan.manager.styles)
示例#8
0
def init_pychan():
	"""General pychan initiation for uh"""
	global STYLES

	# register custom widgets

	from horizons.gui.widgets.inventory import Inventory
	from horizons.gui.widgets.buysellinventory import BuySellInventory
	from horizons.gui.widgets.imagefillstatusbutton import  ImageFillStatusButton
	from horizons.gui.widgets.progressbar import ProgressBar
	from horizons.gui.widgets.toggleimagebutton import ToggleImageButton
	from horizons.gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
	from horizons.gui.widgets.icongroup import TabBG
	from horizons.gui.widgets.stepslider import StepSlider
	from horizons.gui.widgets.unitoverview import HealthWidget, StanceWidget, WeaponStorageWidget
	from horizons.gui.widgets.container import AutoResizeContainer
	from horizons.gui.widgets.tooltip import _Tooltip

	widgets = [OkButton, CancelButton, DeleteButton,
			   Inventory, BuySellInventory, ImageFillStatusButton,
			   ProgressBar, StepSlider, TabBG, ToggleImageButton,
			   HealthWidget, StanceWidget, WeaponStorageWidget,
	       AutoResizeContainer]

	for widget in widgets:
		pychan.widgets.registerWidget(widget)

	# add uh styles
	# NOTE: do this before adding tooltip feature, because pychan has a design issue
	# where it sometimes uses the class hierarchy and sometimes treats each class differently.
	for name, stylepart in STYLES.iteritems():
		pychan.manager.addStyle(name, stylepart)

	# patch default widgets
	for name, widget in pychan.widgets.WIDGETS.items()[:]:

		# patch hide so that it doesn't crash
		def hide_new(self, *args, **kwargs):
			try:
				# call on self, we can't access the outer scope easily since we're in a loop
				# only apply usable args, else it would crash when called through fife timers
				pychan.tools.applyOnlySuitable(self._hide_orig, *args, **kwargs)
			except RuntimeError as e:
				import traceback
				traceback.print_exc()
				print 'Caught pychan RuntimeError on hide, assuming irrelevant gcn::exception.'

		if not hasattr(widget, "_hide_orig"):
			widget._hide_orig = widget.hide
		widget.hide = hide_new


		# support for tooltips via helptext attribute
		if any( attr.name == "helptext" for attr in widget.ATTRIBUTES ):

			# create a new class with a custom __init__, so tooltips are initalized

			klass_name =  str(widget)+" with tooltip hack (see horizons/engine/pychan_util.py"
			klass = type(klass_name, (widget, ), {})

			def __init__(self, *args, **kwargs):
				# this is going to look a bit weird

				# remove all traces of this code ever existing (would confuse pychan badly, don't try to create own widgets)
				self.__class__ = self.__class__.__mro__[1]
				# manually copy everything we need from the tooltip class
				for key, value in _Tooltip.__dict__.iteritems():
					if not key.startswith("__"): # not the internals
						if callable( value ):
							value =  new.instancemethod(value, self)

					# put it in the instance dict, not the class dict
					self.__dict__[key] = value

				# call real init (no super, since we are already in the super class
				self.__init__(*args, **kwargs)

				self.init_tooltip()

			klass.__init__ = __init__

			# register this new class in pychan
			pychan.widgets.WIDGETS[name] = klass


	# NOTE: there is a bug with the tuple notation: http://fife.trac.cvsdude.com/engine/ticket/656
	# work around this here for now:
	def conv(d):
		entries = []
		for k, v in d.iteritems():
			if isinstance(v, dict): # recurse
				v = conv(v)
			if isinstance(k, tuple): # resolve tuple-notation, add separate keys
				for k_i in k:
					entries.append( (k_i, v) )
			else:
				entries.append( (k, v) )

		return dict(entries)

	# patch uh styles
	STYLES = conv(STYLES)

	# patch fife default styles
	pychan.manager.styles = conv(pychan.manager.styles)