1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- ########################################################################
- # Hello Worlds - Libre 3D RPG game.
- # Copyright (C) 2020 CYBERDEViL
- #
- # This file is part of Hello Worlds.
- #
- # Hello Worlds is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # Hello Worlds is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- #
- ########################################################################
- class Signal:
- def __init__(self, *argTypes):
- self._argTypes = argTypes
- self._cbFuncs = [] # callbacks
- def connect(self, cbFunc):
- if cbFunc not in self._cbFuncs:
- self._cbFuncs.append(cbFunc)
- return 0
- else:
- print("Signal.connect : Callback function {0} connection already exists.".format(cbFunc))
- return 1
- def disconnect(self, cbFunc=None):
- if not cbFunc:
- self._cbFuncs.clear()
- return 0
- elif cbFunc in self._cbFuncs:
- self._cbFuncs.remove(cbFunc)
- return 0
- else:
- print("Signal.disconnect : Callback function {0} not found.".format(cbFunc))
- return 1
- def emit(self, *args):
- _args = []
- index = 0
- for argType in self._argTypes:
- if type(args[index]) != argType:
- print("Signal.emit : Expected a {0} type instead got a {1} type for argument {2}".format(argType, type(args[index]), index))
- return 1
- _args.append(args[index])
- index += 1
- for cbFunc in self._cbFuncs: cbFunc(*_args)
- return 0
|