Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix game object selection #140

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mbrlabs.mundus.editor.events

class ToolDeactivatedEvent {

interface ToolDeactivatedEventListener {
@Subscribe
fun onToolDeactivatedEvent(event: ToolDeactivatedEvent)
}
}
2 changes: 1 addition & 1 deletion editor/src/main/com/mbrlabs/mundus/editor/tools/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
158 changes: 0 additions & 158 deletions editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.java

This file was deleted.

136 changes: 136 additions & 0 deletions editor/src/main/com/mbrlabs/mundus/editor/tools/ToolManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2016. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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.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
*/
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<TerrainBrush>
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)
}

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) {
(activeTool as SelectionTool?)!!.gameObjectSelected(selected)
}
}

fun deactivateTool() {
if (activeTool != null) {
activeTool!!.onDisabled()
inputManager.removeProcessor(activeTool)
activeTool = null
}
}

fun setDefaultTool() {
if (activeTool == null || activeTool === modelPlacementTool || activeTool is TerrainBrush) activateTool(translateTool) else activeTool!!.onDisabled()
}

fun render() {
if (activeTool != null) {
activeTool!!.render()
}
}

fun act() {
if (activeTool != null) {
activeTool!!.act()
}
}

fun isSelected(go: GameObject): Boolean {
return go == getSelectedObject()
}

override fun keyUp(keycode: Int): Boolean {
if (keycode == KEY_DEACTIVATE) {
if (activeTool != null) {
activeTool!!.onDisabled()
Mundus.postEvent(ToolDeactivatedEvent())
}
setDefaultTool()
return true
}
return false
}

override fun dispose() {
for (brush in terrainBrushes) {
brush.dispose()
}
translateTool.dispose()
modelPlacementTool.dispose()
selectionTool.dispose()
rotateTool.dispose()
scaleTool.dispose()
}

private fun getSelectedObject() : GameObject? {
if (activeTool == null) {
return null
}
val scene = activeTool!!.projectManager.current().currScene ?: return null
return scene.currentSelection
}

companion object {
private const val KEY_DEACTIVATE = Input.Keys.ESCAPE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
Loading