Skip to content

Commit

Permalink
Scene deleting (#114)
Browse files Browse the repository at this point in the history
* Added submenus to scene menus

* Screen delete method

* Disable current delete buttton

* Delete dialog

* Update delete button enable on scene added

* Log at scene delete

* Open button instead of Change to

* Save project after scene delete

* Updated CHANGES
  • Loading branch information
Dgzt authored Oct 21, 2022
1 parent be97e91 commit 3ff31e2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
1 change: 1 addition & 0 deletions editor/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Convert Water Shader to an Uber Shader
- Water Shader Enhancements (visible depth, toggle reflection/refraction)
- Skybox rendering performance tweak
- Added scene delete button

[0.4.1]
- Fix incorrect bone counts causing 1024 shader register error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,17 @@ public void changeScene(ProjectContext projectContext, String sceneName) {
}
}

/**
* Deletes scene
*
* @param project The project context
* @param sceneName The screen name
*/
public void deleteScene(final ProjectContext project, final String sceneName) {
project.scenes.removeValue(sceneName, false);
SceneManager.deleteScene(project, sceneName);
}

private void initGameObject(ProjectContext context, GameObject root) {
initComponents(context, root);
if (root.getChildren() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public static SceneDTO loadScene(ProjectContext context, String sceneName) throw
return json.fromJson(SceneDTO.class, new FileInputStream(sceneDir));
}

/**
* Deletes scene.
*
* @param context project context of the scene
* @param sceneName name of the scene to remove
*/
public static void deleteScene(final ProjectContext context, final String sceneName) {
final String sceneDir = getScenePath(context, sceneName);
FileHandle sceneFile = Gdx.files.absolute(sceneDir);
sceneFile.delete();
}

private static String getScenePath(ProjectContext context, String sceneName) {
return FilenameUtils.concat(context.path + "/" + ProjectManager.PROJECT_SCENES_DIR,
sceneName + "." + ProjectManager.PROJECT_SCENE_EXTENSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FileMenu : Menu("File") {
saveProject.setShortcut(Input.Keys.CONTROL_LEFT, Input.Keys.S)

// setup recent projects
val recentPrjectsPopup = PopupMenu()
val recentProjectsPopup = PopupMenu()
for (ref in registry.projects) {
val pro = MenuItem(ref.name + " - [" + ref.path + "]")
pro.addListener(object : ClickListener() {
Expand All @@ -70,9 +70,9 @@ class FileMenu : Menu("File") {

}
})
recentPrjectsPopup.addItem(pro)
recentProjectsPopup.addItem(pro)
}
recentProjects.subMenu = recentPrjectsPopup
recentProjects.subMenu = recentProjectsPopup

addItem(newProject)
addItem(importProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.mbrlabs.mundus.editor.ui.modules.menu

import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.utils.Array
import com.kotcrab.vis.ui.util.InputValidator
import com.kotcrab.vis.ui.util.dialog.Dialogs
import com.kotcrab.vis.ui.util.dialog.InputDialogAdapter
import com.kotcrab.vis.ui.util.dialog.OptionDialogAdapter
import com.kotcrab.vis.ui.widget.Menu
import com.kotcrab.vis.ui.widget.MenuItem
import com.kotcrab.vis.ui.widget.PopupMenu
import com.mbrlabs.mundus.editor.Mundus
import com.mbrlabs.mundus.editor.core.kryo.KryoManager
import com.mbrlabs.mundus.editor.core.project.ProjectManager
Expand All @@ -42,6 +45,7 @@ class SceneMenu : Menu("Scenes"),

companion object {
private val TAG = SceneMenu::class.java.simpleName
private const val DELETE_BUTTON_NAME = "delete_scene_submenu"
}

private val sceneItems = Array<MenuItem>()
Expand Down Expand Up @@ -86,26 +90,94 @@ class SceneMenu : Menu("Scenes"),

private fun buildMenuItem(sceneName: String): MenuItem {
val menuItem = MenuItem(sceneName)
menuItem.addListener(object : ClickListener() {

val subMenus = PopupMenu()
subMenus.addItem(buildOpenSubMenuItem(sceneName))
subMenus.addItem(buildDeleteSubMenuItem(sceneName, menuItem))
menuItem.subMenu = subMenus

addItem(menuItem)
sceneItems.add(menuItem)

return menuItem
}

private fun buildOpenSubMenuItem(sceneName: String): MenuItem {
val menuItem = MenuItem("Open")
menuItem.addListener(object: ClickListener() {
override fun clicked(event: InputEvent?, x: Float, y: Float) {
projectManager.changeScene(projectManager.current(), sceneName)
updateDeleteButtonEnable(sceneName)
}
})
addItem(menuItem)
sceneItems.add(menuItem)
return menuItem
}

private fun buildDeleteSubMenuItem(sceneName: String, screenMenu: MenuItem): MenuItem {
val menuItem = MenuItem("Delete")
menuItem.name = DELETE_BUTTON_NAME
menuItem.addListener(object: ClickListener() {
override fun clicked(event: InputEvent?, x: Float, y: Float) {
Dialogs.showOptionDialog(UI, "Deleting scene", "Are you sure you want to delete '$sceneName' scene?", Dialogs.OptionDialogType.YES_CANCEL, object : OptionDialogAdapter() {
override fun yes() {
// Delete scene file
projectManager.deleteScene(projectManager.current(), sceneName)

// Delete scene from project
kryoManager.saveProjectContext(projectManager.current())

// Delete scene from UI
sceneItems.removeValue(screenMenu, true)
removeActor(screenMenu)
pack()

Log.trace(TAG, "SceneMenu", "Scene [{}] deleted.", sceneName)
}
})
}
})

// The current scene's delete button will be disabled
if (projectManager.current().activeSceneName.equals(sceneName)) {
disableMenuItem(menuItem)
}

return menuItem
}

private fun updateDeleteButtonEnable(currentSceneName: String) {
for (mi in sceneItems) {
val deleteMenuItem = mi.subMenu.findActor<MenuItem>(DELETE_BUTTON_NAME)

// Enable other delete buttons and disable current delete button
if (mi.text.contentEquals(currentSceneName)) {
disableMenuItem(deleteMenuItem)
} else {
enableMenuItem(deleteMenuItem)
}
}
}

private fun disableMenuItem(menuItem: MenuItem) {
menuItem.touchable = Touchable.disabled
menuItem.isDisabled = true
}

private fun enableMenuItem(menuItem: MenuItem) {
menuItem.touchable = Touchable.enabled
menuItem.isDisabled = false
}

override fun onProjectChanged(event: ProjectChangedEvent) {
buildSceneUi()
}

override fun onSceneAdded(event: SceneAddedEvent) {
val sceneName = event.scene!!.name
buildMenuItem(sceneName)
updateDeleteButtonEnable(sceneName)

// Save context here so that the ID above is persisted in .pro file
// Save context here so that the scene name above is persisted in .pro file
kryoManager.saveProjectContext(projectManager.current())

Log.trace(TAG, "SceneMenu", "New scene [{}] added.", sceneName)
Expand Down

0 comments on commit 3ff31e2

Please sign in to comment.