Platformer with Level Editor
import QtQuick 2.0
import Felgo 3.0
import "../common"
MultiResolutionImage {
id: sidebar
property string activeTool: "hand"
property var bgImage
property var grid
property var undoHandler
property var buttons: [groundButton, ground2Button, platformButton, spikeballButton, spikesButton, opponentJumperButton, opponentWalkerButton, coinButton, mushroomButton, starButton, finishButton]
property alias gridSizeButton: gridSizeButton
z: 100
width: 100
height: editorOverlay.height
anchors.top: editorOverlay.top
anchors.left: editorOverlay.left
source: "../../assets/ui/sidebar.png"
Item {
anchors.fill: parent
anchors.margins: 4
Row {
id: undoRedo
width: parent.width
height: 30
anchors.top: parent.top
anchors.left: parent.left
spacing: 2
property int buttonWidth: width / 2 - spacing / 2
PlatformerImageButton {
width: parent.buttonWidth
image.source: undoHandler.pointer >= 0 ? "../../assets/ui/undo.png" : "../../assets/ui/undo_grey.png"
hoverRectangle.visible: undoHandler.pointer >= 0 ? true : false
onClicked: undoHandler.undo()
}
PlatformerImageButton {
width: parent.buttonWidth
image.source: undoHandler.pointer < undoHandler.undoArray.length - 1 ? "../../assets/ui/redo.png" : "../../assets/ui/redo_grey.png"
hoverRectangle.visible: undoHandler.pointer < undoHandler.undoArray.length - 1 ? true : false
onClicked: undoHandler.redo()
}
}
Row {
id: tools
width: parent.width
height: 30
anchors.top: undoRedo.bottom
anchors.left: parent.left
anchors.topMargin: 4
spacing: 2
property int buttonWidth: width / 2 - spacing / 2
PlatformerSelectableImageButton {
id: drawEraseButton
property bool drawActive: true
width: parent.buttonWidth
height: parent.height
image.source: drawActive ? "../../assets/ui/drawActive.png" : "../../assets/ui/eraseActive.png"
onClicked: {
if(isSelected) {
drawActive = !drawActive
}
else {
handButton.isSelected = false
isSelected = true
}
updateActiveTool()
if(drawActive && editorOverlay.selectedButton == null) {
changeActiveEntityGroup(1)
selectBuildEntityButton(groundButton)
}
}
}
PlatformerSelectableImageButton {
id: handButton
width: parent.buttonWidth
height: parent.height
image.source: "../../assets/ui/hand.png"
isSelected: true
onClicked: {
if(!isSelected) {
drawEraseButton.isSelected = false
isSelected = true
}
updateActiveTool()
}
}
}
Row {
id: entityGroups
width: parent.width
height: 30
anchors.top: tools.bottom
anchors.left: parent.left
anchors.topMargin: 4
spacing: 2
property int activeGroup: 1
property int buttonWidth: width / 3 - spacing * 2 / 3
ItemGroupButton {
image.source: "../../assets/ui/entityGroups/group1.png"
selected: entityGroups.activeGroup == 1
onClicked: changeActiveEntityGroup(1)
}
ItemGroupButton {
image.source: "../../assets/ui/entityGroups/group2.png"
selected: entityGroups.activeGroup == 2
onClicked: changeActiveEntityGroup(2)
}
ItemGroupButton {
image.source: "../../assets/ui/entityGroups/group3.png"
selected: entityGroups.activeGroup == 3
onClicked: changeActiveEntityGroup(3)
}
}
Flickable {
id: buildFlickable
width: parent.width
anchors.top: entityGroups.bottom
anchors.bottom: optionsButton.top
anchors.left: parent.left
anchors.topMargin: 4
anchors.bottomMargin: 4
contentWidth: width
contentHeight: buildColumn.height
topMargin: 5
bottomMargin: 5
clip: true
flickableDirection: Flickable.VerticalFlick
pressDelay: 100
Column {
id: buildColumn
width: parent.width
spacing: 5
Item {
id: levelNameItem
width: parent.width
height: 30
visible: entityGroups.activeGroup == 0
Text {
text: levelEditor.currentLevelName ? levelEditor.currentLevelName : ""
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: nameLevelButton.left
anchors.rightMargin: 4
verticalAlignment: Text.AlignVCenter
fontSizeMode: Text.Fit
font.pixelSize: 13
minimumPixelSize: 7
}
PlatformerImageButton {
id: nameLevelButton
image.source: "../../assets/ui/edit_black.png"
width: 30
height: parent.height
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
onClicked: {
nativeUtils.displayTextInput("Enter levelName", "Enter a level name. (max 15 characters)", "", levelEditor.currentLevelName)
}
Connections {
target: nativeUtils
onTextInputFinished: {
if(accepted) {
if(enteredText.length > 15) {
nativeUtils.displayMessageBox("Invalid level name", "A maximum of 15 characters is allowed!")
return
}
levelEditor.currentLevelName = enteredText
}
}
}
}
}
Row {
id: backgroundButtons
width: parent.width
height: 30
visible: entityGroups.activeGroup == 0
spacing: 2
property int buttonWidth: width / 3 - spacing * 2 / 3
PlatformerImageButton {
width: parent.buttonWidth
image.source: bgImage.bg0
image.fillMode: Image.PreserveAspectCrop
onClicked: bgImage.bg = 0
}
PlatformerImageButton {
width: parent.buttonWidth
image.source: bgImage.bg1
image.fillMode: Image.PreserveAspectCrop
onClicked: bgImage.bg = 1
}
PlatformerImageButton {
width: parent.buttonWidth
image.source: bgImage.bg2
image.fillMode: Image.PreserveAspectCrop
onClicked: bgImage.bg = 2
}
}
Item {
id: gridOptions
width: parent.width
height: 30
visible: entityGroups.activeGroup == 0
Text {
height: parent.height
anchors.top: parent.top
anchors.left: parent.left
text: "GridSize:"
font.pixelSize: 12
verticalAlignment: Text.AlignVCenter
}
PlatformerTextButton {
id: gridSizeButton
screenText: "32"
width: 30
height: parent.height
anchors.top: parent.top
anchors.right: parent.right
onClicked: {
if(screenText == "32") {
screenText = "16"
editorOverlay.scene.gridSize = 16
}
else {
screenText = "32"
editorOverlay.scene.gridSize = 32
}
}
}
}
PlatformerTextButton {
id: publishButton
screenText: "Publish"
width: parent.width
height: 30
visible: entityGroups.activeGroup == 0
onClicked: {
publishDialog.opacity = 1
}
}
PlatformerBuildEntityButton {
id: groundButton
visible: entityGroups.activeGroup == 1
toCreateEntityTypeUrl: "../entities/GroundGrass.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: ground2Button
visible: entityGroups.activeGroup == 1
toCreateEntityTypeUrl: "../entities/GroundDirt.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: platformButton
visible: entityGroups.activeGroup == 1
toCreateEntityTypeUrl: "../entities/Platform.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: spikeballButton
visible: entityGroups.activeGroup == 1
toCreateEntityTypeUrl: "../entities/Spikeball.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: spikesButton
visible: entityGroups.activeGroup == 1
toCreateEntityTypeUrl: "../entities/Spikes.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: opponentJumperButton
visible: entityGroups.activeGroup == 2
toCreateEntityTypeUrl: "../entities/OpponentJumper.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: opponentWalkerButton
visible: entityGroups.activeGroup == 2
toCreateEntityTypeUrl: "../entities/OpponentWalker.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: coinButton
visible: entityGroups.activeGroup == 3
toCreateEntityTypeUrl: "../entities/Coin.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: mushroomButton
visible: entityGroups.activeGroup == 3
toCreateEntityTypeUrl: "../entities/Mushroom.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: starButton
visible: entityGroups.activeGroup == 3
toCreateEntityTypeUrl: "../entities/Star.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
PlatformerBuildEntityButton {
id: finishButton
visible: entityGroups.activeGroup == 3
toCreateEntityTypeUrl: "../entities/Finish.qml"
onSelected: selectBuildEntityButton(this)
onUnselected: unselectBuildEntityButton()
}
}
}
ItemGroupButton {
id: optionsButton
width: 30
height: 25
anchors.bottom: parent.bottom
anchors.right: parent.right
image.source: "../../assets/ui/options.png"
selected: entityGroups.activeGroup == 0
onClicked: changeActiveEntityGroup(0)
}
}
function selectBuildEntityButton(button) {
if(activeTool == "erase") {
setActiveTool("draw")
}
unselectAllButtonsButOne(button)
}
function unselectBuildEntityButton() {
if(activeTool == "draw" || activeTool == "erase") {
setActiveTool("hand")
}
editorOverlay.selectedButton = null
}
function unselectAllButtons() {
for(var i=0; i<buttons.length; i++) {
buttons[i].isSelected = false
}
editorOverlay.selectedButton = null
}
function unselectAllButtonsButOne(button) {
unselectAllButtons()
if(button) {
button.isSelected = true
editorOverlay.selectedButton = button
}
}
function changeActiveEntityGroup(newGroup) {
if(entityGroups.activeGroup !== newGroup) {
entityGroups.activeGroup = newGroup
}
}
function updateActiveTool() {
if(drawEraseButton.isSelected) {
if(drawEraseButton.drawActive) {
activeTool = "draw"
}
else {
activeTool = "erase"
}
}
else if(handButton.isSelected) {
activeTool = "hand"
}
else {
activeTool = ""
}
}
function setActiveTool(tool) {
if(tool === "draw") {
handButton.isSelected = false
drawEraseButton.isSelected = true
drawEraseButton.drawActive = true
}
else if(tool === "erase") {
handButton.isSelected = false
drawEraseButton.isSelected = true
drawEraseButton.drawActive = false
}
else if(tool === "hand") {
drawEraseButton.isSelected = false
handButton.isSelected = true
}
updateActiveTool()
}
function reset() {
unselectAllButtons()
changeActiveEntityGroup(1)