예제 #1
0
class TreeGen(GeneratePlugin):

    def __init__(self, *args, **kwargs):
        super(TreeGen, self).__init__(*args, **kwargs)
        self.optionsWidget = None
        self.displayName = self.tr("Tree")

    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()
        self.trunkSlider = SpinSlider()
        self.trunkSlider.setValue(1)
        widget.setLayout(Column(self.trunkSlider, None))
        self.optionsWidget = widget
        return widget

    def generateInSchematic(self, dim):
        bounds = dim.bounds
        blocktypes = dim.blocktypes

        trunkX = int(bounds.width / 2)
        trunkZ = int(bounds.length / 2)

        crownBottom = int(bounds.height * 2 / 3)

        wood = blocktypes["log[axis=y,variant=birch]"]
        leaves = blocktypes["leaves[check_decay=false,decayable=true,variant=birch]"]

        dim.setBlocks(range(0, bounds.width), [range(crownBottom, bounds.height)], [[range(0, bounds.length)]], leaves)
        dim.setBlocks(trunkX, range(0, bounds.height), trunkZ, wood)
예제 #2
0
    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)
예제 #3
0
    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()
        self.trunkSlider = SpinSlider()
        self.trunkSlider.setValue(1)
        widget.setLayout(Column(self.trunkSlider, None))
        self.optionsWidget = widget
        return widget
예제 #4
0
    def __init__(self, dimension, textureAtlas, geometryCache, shareGLWidget,
                 *args, **kwargs):
        super(CameraWorldViewFrame, self).__init__(*args, **kwargs)

        self.worldView = view = CameraWorldView(dimension, textureAtlas,
                                                geometryCache, shareGLWidget)

        auxControlWidget = QtGui.QWidget()

        StickyMouselookSetting.connectAndCall(view.setStickyMouselook)

        stickyCheckbox = QtGui.QCheckBox(self.tr("Sticky Mouselook"))
        stickyCheckbox.setChecked(StickyMouselookSetting.value())
        stickyCheckbox.toggled.connect(StickyMouselookSetting.setValue)

        auxControlWidget.setLayout(Column(stickyCheckbox))

        self.viewControls = ViewControls(view, auxControlWidget)

        ViewDistanceSetting.connectAndCall(view.setViewDistance)

        viewDistanceInput = QtGui.QSpinBox(minimum=2, maximum=64, singleStep=2)
        viewDistanceInput.setValue(self.worldView.viewDistance)
        viewDistanceInput.valueChanged.connect(ViewDistanceSetting.setValue)

        MaxViewDistanceSetting.connectAndCall(viewDistanceInput.setMaximum)

        PerspectiveSetting.connectAndCall(view.setPerspective)

        perspectiveInput = QtGui.QCheckBox(self.tr("Perspective"))
        perspectiveInput.setChecked(view.perspective)
        perspectiveInput.toggled.connect(PerspectiveSetting.setValue)

        showButton = QtGui.QPushButton(self.tr("Show..."))
        showButton.setMenu(view.layerToggleGroup.menu)

        workplaneCheckbox = QtGui.QCheckBox(self.tr("Work Plane"))
        workplaneSpinSlider = SpinSlider()
        workplaneSpinSlider.setValue(64)
        workplaneSpinSlider.setMinimum(dimension.bounds.miny)
        workplaneSpinSlider.setMaximum(dimension.bounds.maxy)

        workplaneCheckbox.toggled.connect(view.toggleWorkplane)

        workplaneSpinSlider.valueChanged.connect(view.setWorkplaneLevel)

        self.setLayout(
            Column(Row(None,
                       workplaneCheckbox,
                       workplaneSpinSlider,
                       showButton,
                       perspectiveInput,
                       QtGui.QLabel(self.tr("View Distance:")),
                       viewDistanceInput,
                       self.viewControls.getShowHideButton(),
                       margin=0),
                   view,
                   margin=0))
예제 #5
0
    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()

        self.systemsBox = QtGui.QComboBox()
        self.systemsBox.addItem("Koch Snowflake")

        self.blocktypeButton = BlockTypeButton()
        self.blocktypeButton.editorSession = self.editorSession
        self.blocktypeButton.block = "minecraft:stone"
        self.blocktypeButton.blocksChanged.connect(self.updatePreview)

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(100)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

        widget.setLayout(Column(self.systemsBox,
                                self.iterationsSlider,
                                self.blocktypeButton, # xxx from systemsBox
                                None))

        self.optionsWidget = widget
        return widget
예제 #6
0
    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)
예제 #7
0
    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()
        self.trunkSlider = SpinSlider()
        self.trunkSlider.setValue(1)
        widget.setLayout(Column(self.trunkSlider, None))
        self.optionsWidget = widget
        return widget
예제 #8
0
파일: camera.py 프로젝트: mcedit/mcedit2
    def __init__(self, dimension, textureAtlas, geometryCache, shareGLWidget, *args, **kwargs):
        super(CameraWorldViewFrame, self).__init__(*args, **kwargs)

        self.worldView = view = CameraWorldView(dimension, textureAtlas, geometryCache, shareGLWidget)

        auxControlWidget = QtGui.QWidget()
        
        StickyMouselookSetting.connectAndCall(view.setStickyMouselook)
        
        stickyCheckbox = QtGui.QCheckBox(self.tr("Sticky Mouselook"))
        stickyCheckbox.setChecked(StickyMouselookSetting.value())
        stickyCheckbox.toggled.connect(StickyMouselookSetting.setValue)

        auxControlWidget.setLayout(Column(stickyCheckbox))

        self.viewControls = ViewControls(view, auxControlWidget)

        ViewDistanceSetting.connectAndCall(view.setViewDistance)

        viewDistanceInput = QtGui.QSpinBox(minimum=2, maximum=64, singleStep=2)
        viewDistanceInput.setValue(self.worldView.viewDistance)
        viewDistanceInput.valueChanged.connect(ViewDistanceSetting.setValue)
        
        MaxViewDistanceSetting.connectAndCall(viewDistanceInput.setMaximum)
        
        PerspectiveSetting.connectAndCall(view.setPerspective)

        perspectiveInput = QtGui.QCheckBox(self.tr("Perspective"))
        perspectiveInput.setChecked(view.perspective)
        perspectiveInput.toggled.connect(PerspectiveSetting.setValue)

        showButton = QtGui.QPushButton(self.tr("Show..."))
        showButton.setMenu(view.layerToggleGroup.menu)

        workplaneCheckbox = QtGui.QCheckBox(self.tr("Work Plane"))
        workplaneSpinSlider = SpinSlider()
        workplaneSpinSlider.setValue(64)
        workplaneSpinSlider.setMinimum(dimension.bounds.miny)
        workplaneSpinSlider.setMaximum(dimension.bounds.maxy)

        workplaneCheckbox.toggled.connect(view.toggleWorkplane)

        workplaneSpinSlider.valueChanged.connect(view.setWorkplaneLevel)

        self.setLayout(Column(Row(None,
                                  workplaneCheckbox,
                                  workplaneSpinSlider,
                                  showButton,
                                  perspectiveInput,
                                  QtGui.QLabel(self.tr("View Distance:")),
                                  viewDistanceInput,
                                  self.viewControls.getShowHideButton(), margin=0),
                              view, margin=0))
예제 #9
0
class TreeGen(GeneratePlugin):
    def __init__(self, *args, **kwargs):
        super(TreeGen, self).__init__(*args, **kwargs)
        self.optionsWidget = None
        self.displayName = self.tr("Tree")

    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()
        self.trunkSlider = SpinSlider()
        self.trunkSlider.setValue(1)
        widget.setLayout(Column(self.trunkSlider, None))
        self.optionsWidget = widget
        return widget

    def generate(self, bounds, blocktypes):
        schematic = createSchematic(bounds.size, blocktypes)
        dim = schematic.getDimension()

        trunkX = int(bounds.width / 2)
        trunkZ = int(bounds.length / 2)

        crownBottom = int(bounds.height * 2 / 3)

        wood = blocktypes["log[axis=y,variant=birch]"]
        leaves = blocktypes[
            "leaves[check_decay=false,decayable=true,variant=birch]"]

        dim.setBlocks(range(0,
                            bounds.width), [range(crownBottom, bounds.height)],
                      [[range(0, bounds.length)]], leaves)
        dim.setBlocks(trunkX, range(0, bounds.height), trunkZ, wood)

        return schematic
예제 #10
0
파일: command.py 프로젝트: wcpe/mcedit2
    def widgetFromOptDict(self, optDict):
        self.optIdx += 1

        type = optDict.get('type')
        if type is None or not isinstance(type, basestring):
            raise ValueError("Option dict must have 'type' key")

        if type in ('int', 'float'):
            minimum = optDict.get('min', None)
            maximum = optDict.get('max', None)

            value = optDict.get('value', 0)
            increment = optDict.get('increment', None)

            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            if minimum is None or maximum is None:
                if type == 'float':
                    widget = QtGui.QDoubleSpinBox(value=value)
                else:
                    widget = QtGui.QSpinBox(value=value)
                if minimum is not None:
                    widget.setMinimum(minimum)
                else:
                    widget.setMinimum(-2000000000)
                if maximum is not None:
                    widget.setMaximum(maximum)
                else:
                    widget.setMaximum(2000000000)

                if increment is not None:
                    widget.setSingleStep(increment)
            else:
                widget = SpinSlider(double=(type == 'float'),
                                    minimum=minimum,
                                    maximum=maximum,
                                    value=value,
                                    increment=increment)

            self.widgets.append(widget)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.value

        elif type == 'bool':
            value = optDict.get('value', False)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QCheckBox()
            widget.setChecked(value)
            self.widgets.append(widget)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.isChecked

        elif type == 'text':
            value = optDict.get('value', '')
            name = optDict.get('name', None)
            placeholder = optDict.get('placeholder', None)

            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QLineEdit()
            self.widgets.append(widget)
            if placeholder:
                widget.setPlaceholderText(placeholder)
            if value:
                widget.setText(value)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.text

        elif type == 'choice':
            value = optDict.get('value', None)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            choices = optDict.get('choices', [])

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QComboBox()
            self.widgets.append(widget)

            for label, key in choices:
                widget.addItem(label, key)
                if key == value:
                    widget.setCurrentIndex(widget.count() - 1)

            def getChoiceKey():
                return widget.itemData(widget.currentIndex())

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = getChoiceKey

        elif type == 'blocktype':
            value = optDict.get('value', None)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = BlockTypeButton()
            widget.editorSession = self.editorSession
            self.widgets.append(widget)

            if value is not None:
                if not isinstance(value, BlockType):
                    value = self.editorSession.worldEditor.blocktypes[value]
                widget.block = value

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = lambda: widget.block

        elif type == 'label':
            text = optDict.get('text', None)
            if not text:
                raise ValueError(
                    "Option dict for type 'label' must have 'text' key.")
            widget = QtGui.QLabel(text, wordWrap=True)
            self.widgets.append(widget)

            self.formLayout.addRow("", widget)

        elif type == 'nbt':
            widget = QtGui.QLabel("Not Implemented")
            self.widgets.append(widget)

            self.formLayout.addRow("NBT Option: ", widget)
        else:
            raise ValueError("Unknown type %s for option dict" % type)
예제 #11
0
class LSystemPlugin(GeneratePlugin):
    """
    A GeneratePlugin subclass intended for driving an L-system.

    Most of the GeneratePlugin methods are already implemented. To use an LSystemPlugin,
    you need to implement `getOptionsWidget` and `createInitialSymbol` - after that,
    previewing and generating the L-System is taken care of by LSystemPlugin.

    In your implementation of `getOptionsWidget`, you should add `self.iterationsSlider`
     to your widget to control the iteration depth.

    A `recursive` attribute is also available. If your L-System is not recursively defined - that
    is, a finite number of iterations will result in a symbol list that has no further `replace`
    methods defined, you may set the `recursive` attribute of the LSystemPlugin to False. Setting
    `recursive` to False will cause the block and schematic renderer to run the replacement rules
    until no further replacements occur (or until MAX_ITERATIONS iterations), and the value of
    `iterationsSlider` will be ignored for the final generation. The `iterationsSlider` will
    still affect the GL rendering, which is useful for inspecting the system's state after every
    iteration.

    """

    recursive = True
    MAX_ITERATIONS = 50

    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

    def createInitialSymbol(self, bounds):
        """
        Create and return the initial Symbol for the L-System. The symbol is typically initialized
        using values input by the user via the options widget.

        :param bounds: The bounding box selected with the Generate tool, in world coordinates.
        :type bounds: BoundingBox
        :return: The initial Symbol for this L-System
        :rtype: Symbol
        """
        raise NotImplementedError

    def createSymbolList(self, bounds, indefinite=False):
        system = self.createInitialSymbol(bounds)

        symbol_list = [system]

        if indefinite:
            max_iterations = self.MAX_ITERATIONS
        else:
            max_iterations = self.iterationsSlider.value()

        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(
                    _symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...",
                                   process(symbol_list),
                                   cancel=True)
        if symbol_list is False:
            return None

        return symbol_list

    def getPreviewNode(self, bounds):
        symbol_list = self.createSymbolList(bounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to OpenGL")

        sceneNodes = self.renderSceneNodes(symbol_list)
        return sceneNodes

    def renderSceneNodes(self, symbol_list):
        return renderSceneNodes(symbol_list)

    def generateInSchematic(self, dimension, originalBounds):
        symbol_list = self.createSymbolList(originalBounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to blocks")

        rendering = self.renderBlocks(symbol_list)

        log.info("Editing %d blocks" % len(rendering))
        for x, y, z, blockType in rendering:
            x -= originalBounds.minx
            y -= originalBounds.miny
            z -= originalBounds.minz

            dimension.setBlock(x, y, z, blockType)

    def renderBlocks(self, symbol_list):
        return renderBlocks(symbol_list)
예제 #12
0
    def widgetFromOptDict(self, optDict):
        self.optIdx += 1

        type = optDict.get('type')
        if type is None or not isinstance(type, basestring):
            raise ValueError("Option dict must have 'type' key")

        if type in ('int', 'float'):
            minimum = optDict.get('min', None)
            maximum = optDict.get('max', None)

            value = optDict.get('value', 0)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            if minimum is None or maximum is None:
                if type == 'float':
                    widget = QtGui.QDoubleSpinBox(value=value)
                else:
                    widget = QtGui.QSpinBox(value=value)
                if minimum is not None:
                    widget.setMinimum(minimum)
                if maximum is not None:
                    widget.setMaximum(maximum)

            else:
                widget = SpinSlider(double=(type == 'float'), minimum=minimum, maximum=maximum, value=value)

            self.widgets.append(widget)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.value

        elif type == 'bool':
            value = optDict.get('value', False)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QCheckBox()
            widget.setChecked(value)
            self.widgets.append(widget)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.isChecked

        elif type == 'text':
            value = optDict.get('value', '')
            name = optDict.get('name', None)
            placeholder = optDict.get('placeholder', None)

            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QLineEdit()
            self.widgets.append(widget)
            if placeholder:
                widget.setPlaceholderText(placeholder)
            if value:
                widget.setText(value)

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = widget.text

        elif type == 'choice':
            value = optDict.get('value', None)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            choices = optDict.get('choices', [])

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = QtGui.QComboBox()
            self.widgets.append(widget)

            for label, key in choices:
                widget.addItem(label, key)
                if key == value:
                    widget.setCurrentIndex(widget.count() - 1)

            def getChoiceKey():
                return widget.itemData(widget.currentIndex())

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = getChoiceKey

        elif type == 'blocktype':
            value = optDict.get('value', None)
            name = optDict.get('name', None)
            if name is None:
                raise ValueError("Option dict must have 'name' key")

            text = optDict.get('text', "Option %d" % self.optIdx)
            widget = BlockTypeButton()
            widget.editorSession = self.editorSession
            self.widgets.append(widget)

            if value is not None:
                if not isinstance(value, BlockType):
                    value = self.editorSession.worldEditor.blocktypes[value]
                widget.block = value

            self.formLayout.addRow(text, widget)
            self.valueGetters[name] = lambda: widget.block

        elif type == 'label':
            text = optDict.get('text', None)
            if not text:
                raise ValueError("Option dict for type 'label' must have 'text' key.")
            widget = QtGui.QLabel(text, wordWrap=True)
            self.widgets.append(widget)

            self.formLayout.addRow("", widget)

        elif type == 'nbt':
            widget = QtGui.QLabel("Not Implemented")
            self.widgets.append(widget)

            self.formLayout.addRow("NBT Option: ", widget)
        else:
            raise ValueError("Unknown type %s for option dict" % type)
예제 #13
0
class LSystemPlugin(GeneratePlugin):
    """
    A GeneratePlugin subclass intended for driving an L-system.

    Most of the GeneratePlugin methods are already implemented. To use an LSystemPlugin,
    you need to implement `getInputsList` and `createInitialSymbol` - after that,
    previewing and generating the L-System is taken care of by LSystemPlugin.

    If `getInputsList` is not capable enough for your needs, you may implement `getOptionsWidget`
    from GeneratePlugin instead. If you do, you should add `self.iterationsSlider` to your widget
    to control the iteration depth.

    A `recursive` attribute is also available. If your L-System is not recursively defined - that
    is, a finite number of iterations will result in a symbol list that has no further `replace`
    methods defined, you may set the `recursive` attribute of the LSystemPlugin to False. Setting
    `recursive` to False will cause the block and schematic renderer to run the replacement rules
    until no further replacements occur (or until MAX_ITERATIONS iterations), and the value of
    `iterationsSlider` will be ignored for the final generation. The `iterationsSlider` will
    still affect the GL rendering, which is useful for inspecting the system's state after every
    iteration.

    """

    recursive = True
    MAX_ITERATIONS = 50

    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

    def getInputsList(self):
        """
        .... on second thought, not sure if I want to do this.

        Return a list of (name, label, inputType, defaultValue, inputOptions) tuples to configure options
        that are presented in the plugin's options widget.

        This may be implemented by subclasses to easily present common types of inputs and ensure
        the previews will be updated when the option's value is changed. Values will also be saved
        to the user's preferences, keyed on the class name of this LSystemPlugin.

        The values set by the user will be available in the LSystemPlugin's `inputValues`
        attribute as a `dict`.

        `name` is a string used as a key in the `optionValues` dict.
        `label` is a string shown to the user in the options widget. `label` should be translated
            using `self.tr("Label Text")`
        `inputType` is one of the strings listed below for the different input types.
        `defaultValue` is the initial value for the input.
        `inputOptions` is a dict providing additional options for the input widget. See the input
            types below for additional options for each input type.

        Input types:

            `SpinSlider`
                An input for entering an integer using a text field, a pair of +/- buttons,
                and a horizontal slider. The default value should be an integer.

                `inputOptions`:
                    `min`: Minimum value.
                    `max`: Maximum value.

            `BlockTypeButton`
                An input for selecting a block type from the currently edited world.

                `inputOptions`: None

            `ChoiceButton`
                An input for selecting one option from a list of possible options.

            `TextField`
                An input for entering arbitrary text. The default value should be a `str`, or
                preferably a `unicode`.

                `inputOptions`:
                    `placeholder`: Placeholder text to show in grey whenever the field is empty.

        :return:
        :rtype:
        """

    def createInitialSymbol(self, bounds):
        """
        Create and return the initial Symbol for the L-System. The symbol is typically initialized
        using values input by the user via the options widget.

        :param bounds: The bounding box selected with the Generate tool, in world coordinates.
        :type bounds: BoundingBox
        :return: The initial Symbol for this L-System
        :rtype: Symbol
        """
        raise NotImplementedError

    def createSymbolList(self, bounds, indefinite=False):
        system = self.createInitialSymbol(bounds)

        symbol_list = [system]

        if indefinite:
            max_iterations = self.MAX_ITERATIONS
        else:
            max_iterations = self.iterationsSlider.value()

        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(_symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...", process(symbol_list), cancel=True)
        if symbol_list is False:
            return None

        return symbol_list

    def getPreviewNode(self, bounds):
        symbol_list = self.createSymbolList(bounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to OpenGL")

        sceneNodes = self.renderSceneNodes(symbol_list)
        return sceneNodes

    def renderSceneNodes(self, symbol_list):
        return renderSceneNodes(symbol_list)

    def generateInSchematic(self, dimension, originalBounds):
        symbol_list = self.createSymbolList(originalBounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to blocks")

        rendering = self.renderBlocks(symbol_list)

        log.info("Editing %d blocks" % len(rendering))
        for x, y, z, blockType in rendering:
            x -= originalBounds.minx
            y -= originalBounds.miny
            z -= originalBounds.minz

            dimension.setBlock(x, y, z, blockType)

    def renderBlocks(self, symbol_list):
        return renderBlocks(symbol_list)
예제 #14
0
class LSystemPlugin(GeneratePlugin):
    """
    A GeneratePlugin subclass intended for driving an L-system.

    Most of the GeneratePlugin methods are already implemented. To use an LSystemPlugin,
    you need to implement `getInputsList` and `createInitialSymbol` - after that,
    previewing and generating the L-System is taken care of by LSystemPlugin.

    If `getInputsList` is not capable enough for your needs, you may implement `getOptionsWidget`
    from GeneratePlugin instead. If you do, you should add `self.iterationsSlider` to your widget
    to control the iteration depth.

    A `recursive` attribute is also available. If your L-System is not recursively defined - that
    is, a finite number of iterations will result in a symbol list that has no further `replace`
    methods defined, you may set the `recursive` attribute of the LSystemPlugin to False. Setting
    `recursive` to False will cause the block and schematic renderer to run the replacement rules
    until no further replacements occur (or until MAX_ITERATIONS iterations), and the value of
    `iterationsSlider` will be ignored for the final generation. The `iterationsSlider` will
    still affect the GL rendering, which is useful for inspecting the system's state after every
    iteration.

    """

    recursive = True
    MAX_ITERATIONS = 50

    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

    def getInputsList(self):
        """
        .... on second thought, not sure if I want to do this.

        Return a list of (name, label, inputType, defaultValue, inputOptions) tuples to configure options
        that are presented in the plugin's options widget.

        This may be implemented by subclasses to easily present common types of inputs and ensure
        the previews will be updated when the option's value is changed. Values will also be saved
        to the user's preferences, keyed on the class name of this LSystemPlugin.

        The values set by the user will be available in the LSystemPlugin's `inputValues`
        attribute as a `dict`.

        `name` is a string used as a key in the `optionValues` dict.
        `label` is a string shown to the user in the options widget. `label` should be translated
            using `self.tr("Label Text")`
        `inputType` is one of the strings listed below for the different input types.
        `defaultValue` is the initial value for the input.
        `inputOptions` is a dict providing additional options for the input widget. See the input
            types below for additional options for each input type.

        Input types:

            `SpinSlider`
                An input for entering an integer using a text field, a pair of +/- buttons,
                and a horizontal slider. The default value should be an integer.

                `inputOptions`:
                    `min`: Minimum value.
                    `max`: Maximum value.

            `BlockTypeButton`
                An input for selecting a block type from the currently edited world.

                `inputOptions`: None

            `ChoiceButton`
                An input for selecting one option from a list of possible options.

            `TextField`
                An input for entering arbitrary text. The default value should be a `str`, or
                preferably a `unicode`.

                `inputOptions`:
                    `placeholder`: Placeholder text to show in grey whenever the field is empty.

        :return:
        :rtype:
        """

    def createInitialSymbol(self, bounds):
        """
        Create and return the initial Symbol for the L-System. The symbol is typically initialized
        using values input by the user via the options widget.

        :param bounds: The bounding box selected with the Generate tool, in world coordinates.
        :type bounds: BoundingBox
        :return: The initial Symbol for this L-System
        :rtype: Symbol
        """
        raise NotImplementedError

    def createSymbolList(self, bounds, indefinite=False):
        system = self.createInitialSymbol(bounds)

        symbol_list = [system]

        if indefinite:
            max_iterations = self.MAX_ITERATIONS
        else:
            max_iterations = self.iterationsSlider.value()

        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(
                    _symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...",
                                   process(symbol_list),
                                   cancel=True)
        if symbol_list is False:
            return None

        return symbol_list

    def getPreviewNode(self, bounds):
        symbol_list = self.createSymbolList(bounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to OpenGL")

        sceneNodes = self.renderSceneNodes(symbol_list)
        return sceneNodes

    def renderSceneNodes(self, symbol_list):
        return renderSceneNodes(symbol_list)

    def generateInSchematic(self, dimension, originalBounds):
        symbol_list = self.createSymbolList(originalBounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to blocks")

        rendering = self.renderBlocks(symbol_list)

        log.info("Editing %d blocks" % len(rendering))
        for x, y, z, blockType in rendering:
            x -= originalBounds.minx
            y -= originalBounds.miny
            z -= originalBounds.minz

            dimension.setBlock(x, y, z, blockType)

    def renderBlocks(self, symbol_list):
        return renderBlocks(symbol_list)
예제 #15
0
class LSystemPlugin(GeneratePlugin):

    def __init__(self, editorSession):
        log.warn("type(LSystemPlugin): %s, type(self): %s (IS? %s)",
                 LSystemPlugin, type(self),
                 LSystemPlugin is type(self))
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None
        self.displayName = self.tr("L-System Test")

    def getOptionsWidget(self):
        if self.optionsWidget:
            return self.optionsWidget

        widget = QtGui.QWidget()

        self.systemsBox = QtGui.QComboBox()
        self.systemsBox.addItem("Koch Snowflake")

        self.blocktypeButton = BlockTypeButton()
        self.blocktypeButton.editorSession = self.editorSession
        self.blocktypeButton.block = "minecraft:stone"
        self.blocktypeButton.blocksChanged.connect(self.updatePreview)

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(100)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

        widget.setLayout(Column(self.systemsBox,
                                self.iterationsSlider,
                                self.blocktypeButton, # xxx from systemsBox
                                None))

        self.optionsWidget = widget
        return widget

    def getPreviewNode(self, bounds):
        system = koch.Snowflake(bounds, blocktype=self.blocktypeButton.block)
        symbol_list = [system]

        max_iterations = self.iterationsSlider.value()

        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(_symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...", process(symbol_list), cancel=True)
        if symbol_list is False:
            return

        sceneNodes = renderSceneNodes(symbol_list)
        return sceneNodes

    def generate(self, bounds, blocktypes):
        # self.systemsBox.value()
        schematic = createSchematic(bounds.size, blocktypes)
        dim = schematic.getDimension()
        system = koch.Snowflake(dim.bounds, blocktype=self.blocktypeButton.block)
        symbol_list = [system]

        max_iterations = self.iterationsSlider.value()
        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(_symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...", process(symbol_list), cancel=True)
        if symbol_list is False:
            return

        import pprint
        pprint.pprint(symbol_list)
        rendering = renderBlocks(symbol_list)

        print("Rendering %d blocks" % len(rendering))
        for x, y, z, blockType in rendering:
            dim.setBlock(x, y, z, blockType)

        return schematic
예제 #16
0
class LSystemPlugin(GeneratePlugin):
    """
    A GeneratePlugin subclass intended for driving an L-system.

    Most of the GeneratePlugin methods are already implemented. To use an LSystemPlugin,
    you need to implement `getOptionsWidget` and `createInitialSymbol` - after that,
    previewing and generating the L-System is taken care of by LSystemPlugin.

    In your implementation of `getOptionsWidget`, you should add `self.iterationsSlider`
     to your widget to control the iteration depth.

    A `recursive` attribute is also available. If your L-System is not recursively defined - that
    is, a finite number of iterations will result in a symbol list that has no further `replace`
    methods defined, you may set the `recursive` attribute of the LSystemPlugin to False. Setting
    `recursive` to False will cause the block and schematic renderer to run the replacement rules
    until no further replacements occur (or until MAX_ITERATIONS iterations), and the value of
    `iterationsSlider` will be ignored for the final generation. The `iterationsSlider` will
    still affect the GL rendering, which is useful for inspecting the system's state after every
    iteration.

    """

    recursive = True
    MAX_ITERATIONS = 50

    def __init__(self, editorSession):
        super(LSystemPlugin, self).__init__(editorSession)
        self.optionsWidget = None

        self.iterationsSlider = SpinSlider()
        self.iterationsSlider.setMinimum(1)
        self.iterationsSlider.setMaximum(50)
        self.iterationsSlider.setValue(3)
        self.iterationsSlider.valueChanged.connect(self.updatePreview)

    def createInitialSymbol(self, bounds):
        """
        Create and return the initial Symbol for the L-System. The symbol is typically initialized
        using values input by the user via the options widget.

        :param bounds: The bounding box selected with the Generate tool, in world coordinates.
        :type bounds: BoundingBox
        :return: The initial Symbol for this L-System
        :rtype: Symbol
        """
        raise NotImplementedError

    def createSymbolList(self, bounds, indefinite=False):
        system = self.createInitialSymbol(bounds)

        symbol_list = [system]

        if indefinite:
            max_iterations = self.MAX_ITERATIONS
        else:
            max_iterations = self.iterationsSlider.value()

        def process(_symbol_list):
            for iteration, _symbol_list in applyReplacementsIterated(_symbol_list, max_iterations):
                yield iteration, max_iterations

            yield _symbol_list

        symbol_list = showProgress("Generating...", process(symbol_list), cancel=True)
        if symbol_list is False:
            return None

        return symbol_list

    def getPreviewNode(self, bounds):
        symbol_list = self.createSymbolList(bounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to OpenGL")

        sceneNodes = self.renderSceneNodes(symbol_list)
        return sceneNodes

    def renderSceneNodes(self, symbol_list):
        return renderSceneNodes(symbol_list)

    def generateInSchematic(self, dimension, originalBounds):
        symbol_list = self.createSymbolList(originalBounds)
        if symbol_list is None:
            return None

        log.info("Rendering symbols to blocks")

        rendering = self.renderBlocks(symbol_list)

        log.info("Editing %d blocks" % len(rendering))
        for x, y, z, blockType in rendering:
            x -= originalBounds.minx
            y -= originalBounds.miny
            z -= originalBounds.minz

            dimension.setBlock(x, y, z, blockType)

    def renderBlocks(self, symbol_list):
        return renderBlocks(symbol_list)