From 6c2f784cf19fc4adbe13e94f7ca3f457bfcca0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsur=C3=B3=20Tibor?= Date: Sun, 26 Feb 2023 18:07:53 +0000 Subject: [PATCH 1/2] Rename .java to .kt --- .../mundus/editor/tools/{ToolManager.java => ToolManager.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename editor/src/main/com/mbrlabs/mundus/editor/tools/{ToolManager.java => ToolManager.kt} (100%) diff --git a/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.java b/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt similarity index 100% rename from editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.java rename to editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt From e60de1e27b9e6971831a7b38b1f9dea31f409be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsur=C3=B3=20Tibor?= Date: Sun, 26 Feb 2023 18:07:53 +0000 Subject: [PATCH 2/2] Fix game object selection --- .../editor/events/ToolDeactivatedEvent.kt | 9 + .../com/mbrlabs/mundus/editor/tools/Tool.kt | 2 +- .../mundus/editor/tools/ToolManager.kt | 184 ++++++++---------- .../mundus/editor/ui/modules/MundusToolbar.kt | 2 +- .../components/terrain/BaseBrushTab.kt | 6 +- .../components/terrain/TerrainBrushGrid.kt | 25 ++- .../modules/outline/OutlineRightClickMenu.kt | 4 +- 7 files changed, 120 insertions(+), 112 deletions(-) create mode 100644 editor/src/main/com/mbrlabs/mundus/editor/events/ToolDeactivatedEvent.kt diff --git a/editor/src/main/com/mbrlabs/mundus/editor/events/ToolDeactivatedEvent.kt b/editor/src/main/com/mbrlabs/mundus/editor/events/ToolDeactivatedEvent.kt new file mode 100644 index 000000000..dd62ad179 --- /dev/null +++ b/editor/src/main/com/mbrlabs/mundus/editor/events/ToolDeactivatedEvent.kt @@ -0,0 +1,9 @@ +package com.mbrlabs.mundus.editor.events + +class ToolDeactivatedEvent { + + interface ToolDeactivatedEventListener { + @Subscribe + fun onToolDeactivatedEvent(event: ToolDeactivatedEvent) + } +} diff --git a/editor/src/main/com/mbrlabs/mundus/editor/tools/Tool.kt b/editor/src/main/com/mbrlabs/mundus/editor/tools/Tool.kt index 30afbbade..fb7f7ee9d 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/tools/Tool.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/tools/Tool.kt @@ -29,7 +29,7 @@ import com.mbrlabs.mundus.editor.shader.Shaders * @author Marcus Brummer * @version 25-12-2015 */ -abstract class Tool(protected var projectManager: ProjectManager, +abstract class Tool(var projectManager: ProjectManager, protected var history: CommandHistory) : InputAdapter(), Disposable { protected var shader: Shader = Shaders.wireframeShader diff --git a/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt b/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt index 35891d0c6..7d993ba23 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt @@ -13,146 +13,124 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mbrlabs.mundus.editor.tools; - -import com.badlogic.gdx.Input; -import com.badlogic.gdx.InputAdapter; -import com.badlogic.gdx.graphics.g3d.ModelBatch; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.utils.Array; -import com.badlogic.gdx.utils.Disposable; -import com.mbrlabs.mundus.commons.scene3d.GameObject; -import com.mbrlabs.mundus.editor.core.EditorScene; -import com.mbrlabs.mundus.editor.core.project.ProjectManager; -import com.mbrlabs.mundus.editor.history.CommandHistory; -import com.mbrlabs.mundus.editor.input.InputManager; -import com.mbrlabs.mundus.editor.tools.brushes.*; -import com.mbrlabs.mundus.editor.tools.picker.GameObjectPicker; -import com.mbrlabs.mundus.editor.tools.picker.ToolHandlePicker; +package com.mbrlabs.mundus.editor.tools + +import com.badlogic.gdx.Input +import com.badlogic.gdx.InputAdapter +import com.badlogic.gdx.graphics.glutils.ShapeRenderer +import com.badlogic.gdx.utils.Array +import com.badlogic.gdx.utils.Disposable +import com.mbrlabs.mundus.commons.scene3d.GameObject +import com.mbrlabs.mundus.editor.Mundus +import com.mbrlabs.mundus.editor.core.project.ProjectManager +import com.mbrlabs.mundus.editor.events.ToolDeactivatedEvent +import com.mbrlabs.mundus.editor.history.CommandHistory +import com.mbrlabs.mundus.editor.input.InputManager +import com.mbrlabs.mundus.editor.tools.brushes.* +import com.mbrlabs.mundus.editor.tools.picker.GameObjectPicker +import com.mbrlabs.mundus.editor.tools.picker.ToolHandlePicker /** * @author Marcus Brummer * @version 25-12-2015 */ -public class ToolManager extends InputAdapter implements Disposable { - - private static final int KEY_DEACTIVATE = Input.Keys.ESCAPE; - - private Tool activeTool; - - public Array terrainBrushes; - - public ModelPlacementTool modelPlacementTool; - public SelectionTool selectionTool; - public TranslateTool translateTool; - public RotateTool rotateTool; - public ScaleTool scaleTool; - - private InputManager inputManager; - - public ToolManager(InputManager inputManager, ProjectManager projectManager, GameObjectPicker goPicker, - ToolHandlePicker toolHandlePicker, ShapeRenderer shapeRenderer, - CommandHistory history) { - this.inputManager = inputManager; - this.activeTool = null; - - terrainBrushes = new Array<>(); - terrainBrushes.add(new SmoothCircleBrush(projectManager, history)); - terrainBrushes.add(new CircleBrush(projectManager, history)); - terrainBrushes.add(new StarBrush(projectManager, history)); - terrainBrushes.add(new ConfettiBrush(projectManager, history)); - - modelPlacementTool = new ModelPlacementTool(projectManager, history); - selectionTool = new SelectionTool(projectManager, goPicker, history); - translateTool = new TranslateTool(projectManager, goPicker, toolHandlePicker, history); - rotateTool = new RotateTool(projectManager, goPicker, toolHandlePicker, shapeRenderer, history); - scaleTool = new ScaleTool(projectManager, goPicker, toolHandlePicker, shapeRenderer, history); +class ToolManager(private val inputManager: InputManager, projectManager: ProjectManager?, goPicker: GameObjectPicker?, + toolHandlePicker: ToolHandlePicker?, shapeRenderer: ShapeRenderer?, + history: CommandHistory?) : InputAdapter(), Disposable { + var activeTool: Tool? = null + private set + var terrainBrushes: Array + var modelPlacementTool: ModelPlacementTool + var selectionTool: SelectionTool + var translateTool: TranslateTool + var rotateTool: RotateTool + var scaleTool: ScaleTool + + init { + terrainBrushes = Array() + terrainBrushes.add(SmoothCircleBrush(projectManager, history)) + terrainBrushes.add(CircleBrush(projectManager, history)) + terrainBrushes.add(StarBrush(projectManager, history)) + terrainBrushes.add(ConfettiBrush(projectManager, history)) + modelPlacementTool = ModelPlacementTool(projectManager, history) + selectionTool = SelectionTool(projectManager, goPicker, history) + translateTool = TranslateTool(projectManager, goPicker, toolHandlePicker, history) + rotateTool = RotateTool(projectManager, goPicker, toolHandlePicker, shapeRenderer, history) + scaleTool = ScaleTool(projectManager, goPicker, toolHandlePicker, shapeRenderer, history) } - public void activateTool(Tool tool) { - boolean shouldKeepSelection = activeTool != null && activeTool instanceof SelectionTool && tool instanceof SelectionTool; - GameObject selected = getSelectedObject(); - - deactivateTool(); - activeTool = tool; - inputManager.addProcessor(activeTool); - activeTool.onActivated(); - + fun activateTool(tool: Tool?) { + val shouldKeepSelection = activeTool != null && activeTool is SelectionTool && tool is SelectionTool + val selected = getSelectedObject() + deactivateTool() + activeTool = tool + inputManager.addProcessor(activeTool) + activeTool!!.onActivated() if (shouldKeepSelection && selected != null) { - ((SelectionTool)activeTool).gameObjectSelected(selected); + (activeTool as SelectionTool?)!!.gameObjectSelected(selected) } } - public void deactivateTool() { + fun deactivateTool() { if (activeTool != null) { - activeTool.onDisabled(); - inputManager.removeProcessor(activeTool); - activeTool = null; + activeTool!!.onDisabled() + inputManager.removeProcessor(activeTool) + activeTool = null } } - public void setDefaultTool() { - if (activeTool == null || activeTool == modelPlacementTool || activeTool instanceof TerrainBrush) - activateTool(translateTool); - else - activeTool.onDisabled(); - + fun setDefaultTool() { + if (activeTool == null || activeTool === modelPlacementTool || activeTool is TerrainBrush) activateTool(translateTool) else activeTool!!.onDisabled() } - public void render() { + fun render() { if (activeTool != null) { - activeTool.render(); + activeTool!!.render() } } - public void act() { + fun act() { if (activeTool != null) { - activeTool.act(); + activeTool!!.act() } } - public Tool getActiveTool() { - return activeTool; - } - - public boolean isSelected(final GameObject go) { - return go.equals(getSelectedObject()); + fun isSelected(go: GameObject): Boolean { + return go == getSelectedObject() } - @Override - public boolean keyUp(int keycode) { + override fun keyUp(keycode: Int): Boolean { if (keycode == KEY_DEACTIVATE) { if (activeTool != null) { - activeTool.onDisabled(); + activeTool!!.onDisabled() + Mundus.postEvent(ToolDeactivatedEvent()) } - setDefaultTool(); - return true; + setDefaultTool() + return true } - return false; + return false } - @Override - public void dispose() { - for (TerrainBrush brush : terrainBrushes) { - brush.dispose(); + override fun dispose() { + for (brush in terrainBrushes) { + brush.dispose() } - translateTool.dispose(); - modelPlacementTool.dispose(); - selectionTool.dispose(); - rotateTool.dispose(); - scaleTool.dispose(); + translateTool.dispose() + modelPlacementTool.dispose() + selectionTool.dispose() + rotateTool.dispose() + scaleTool.dispose() } - private GameObject getSelectedObject() { + private fun getSelectedObject() : GameObject? { if (activeTool == null) { - return null; - } - EditorScene scene = getActiveTool().getProjectManager().current().currScene; - - if (scene == null) { - return null; + return null } - return scene.currentSelection; + val scene = activeTool!!.projectManager.current().currScene ?: return null + return scene.currentSelection } -} + companion object { + private const val KEY_DEACTIVATE = Input.Keys.ESCAPE + } +} \ No newline at end of file diff --git a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/MundusToolbar.kt b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/MundusToolbar.kt index 7f8384e80..f0e9ea4ab 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/MundusToolbar.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/MundusToolbar.kt @@ -253,7 +253,7 @@ class MundusToolbar : Toolbar(), FullScreenEvent.FullScreenEventListener { * Refresh the active button in UI with the currently active tool. */ fun updateActiveToolButton() { - when (toolManager.activeTool.name) { + when (toolManager.activeTool?.name) { SelectionTool.NAME -> setActive(selectBtn) TranslateTool.NAME -> setActive(translateBtn) RotateTool.NAME -> setActive(rotateBtn) diff --git a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/BaseBrushTab.kt b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/BaseBrushTab.kt index b3117c9f4..2923e6b70 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/BaseBrushTab.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/BaseBrushTab.kt @@ -9,12 +9,16 @@ abstract class BaseBrushTab(private val parent: TerrainComponentWidget, protected val terrainBrushGrid: TerrainBrushGrid = TerrainBrushGrid(parent, mode); + override fun onShow() { + terrainBrushGrid.setupButtonStyleForSelectedBrush() + } + /** * Clears selection. */ override fun onHide() { super.onHide() - terrainBrushGrid.clearSelection() + terrainBrushGrid.clearSelectedButtonStyle() } } diff --git a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/TerrainBrushGrid.kt b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/TerrainBrushGrid.kt index d1c742583..b0546bed1 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/TerrainBrushGrid.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/inspector/components/terrain/TerrainBrushGrid.kt @@ -27,6 +27,7 @@ import com.kotcrab.vis.ui.widget.VisLabel import com.kotcrab.vis.ui.widget.VisTable import com.mbrlabs.mundus.editor.Mundus import com.mbrlabs.mundus.editor.events.GlobalBrushSettingsChangedEvent +import com.mbrlabs.mundus.editor.events.ToolDeactivatedEvent import com.mbrlabs.mundus.editor.tools.ToolManager import com.mbrlabs.mundus.editor.tools.brushes.TerrainBrush import com.mbrlabs.mundus.editor.ui.UI @@ -39,7 +40,7 @@ import com.mbrlabs.mundus.editor.ui.widgets.ImprovedSlider */ class TerrainBrushGrid(private val parent: TerrainComponentWidget, private val brushMode: TerrainBrush.BrushMode) - : VisTable(), GlobalBrushSettingsChangedEvent.GlobalBrushSettingsChangedListener { + : VisTable(), GlobalBrushSettingsChangedEvent.GlobalBrushSettingsChangedListener, ToolDeactivatedEvent.ToolDeactivatedEventListener { private val grid = GridGroup(40f, 0f) private val strengthSlider = ImprovedSlider(0f, 1f, 0.1f) @@ -76,8 +77,19 @@ class TerrainBrushGrid(private val parent: TerrainComponentWidget, add(settingsTable).expand().fill().padLeft(5f).padRight(5f).padTop(5f).row() } - fun clearSelection() { - toolManager.deactivateTool() + fun setupButtonStyleForSelectedBrush() { + val activeTool = toolManager.activeTool + + if (activeTool is TerrainBrush && activeTool.mode.equals(brushMode)) { + for (button in buttons) { + if (button.name == activeTool.name) { + button.style = FaTextButton.styleActive + } + } + } + } + + fun clearSelectedButtonStyle() { buttons.forEach { it.style = FaTextButton.styleNoBg } } @@ -97,16 +109,21 @@ class TerrainBrushGrid(private val parent: TerrainComponentWidget, strengthSlider.value = TerrainBrush.getStrength() } + override fun onToolDeactivatedEvent(event: ToolDeactivatedEvent) { + clearSelectedButtonStyle() + } + /** */ private inner class BrushItem(brush: TerrainBrush) : VisTable() { init { val button = FaTextButton(brush.iconFont) + button.name = brush.name add(button) buttons.add(button) addListener(object : ClickListener() { override fun clicked(event: InputEvent?, x: Float, y: Float) { - clearSelection() + clearSelectedButtonStyle() activateBrush(brush) button.style = FaTextButton.styleActive } diff --git a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/outline/OutlineRightClickMenu.kt b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/outline/OutlineRightClickMenu.kt index 547132a81..495219737 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/outline/OutlineRightClickMenu.kt +++ b/editor/src/main/com/mbrlabs/mundus/editor/ui/modules/outline/OutlineRightClickMenu.kt @@ -82,8 +82,8 @@ class OutlineRightClickMenu(outline: Outline) : PopupMenu() { override fun clicked(event: InputEvent?, x: Float, y: Float) { if (selectedGO != null) { outline.removeGo(selectedGO!!) - if (toolManager.isSelected(selectedGO)) { - toolManager.activeTool.onDisabled() + if (toolManager.isSelected(selectedGO!!)) { + toolManager.activeTool!!.onDisabled() } } }