-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jpooleycodes/logging_dock_bar
Add a LogBar in the bottom dock
- Loading branch information
Showing
5 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
editor/src/main/com/mbrlabs/mundus/editor/events/LogEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mbrlabs.mundus.editor.events | ||
|
||
/** | ||
* An Event for posting new log entries in the log bar | ||
*/ | ||
class LogEvent(val logMessage: String) { | ||
|
||
interface LogEventListener { | ||
@Subscribe | ||
fun onLogEvent(event: LogEvent) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
editor/src/main/com/mbrlabs/mundus/editor/ui/modules/dock/LogBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.mbrlabs.mundus.editor.ui.modules.dock | ||
|
||
import com.badlogic.gdx.Gdx | ||
import com.badlogic.gdx.Input | ||
import com.badlogic.gdx.scenes.scene2d.Actor | ||
import com.badlogic.gdx.scenes.scene2d.InputEvent | ||
import com.badlogic.gdx.scenes.scene2d.InputListener | ||
import com.badlogic.gdx.scenes.scene2d.ui.Table | ||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener | ||
import com.kotcrab.vis.ui.widget.* | ||
import com.kotcrab.vis.ui.widget.tabbedpane.Tab | ||
import com.mbrlabs.mundus.editor.Mundus | ||
import com.mbrlabs.mundus.editor.events.LogEvent | ||
import com.mbrlabs.mundus.editor.ui.UI | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
/** | ||
* Docked log bar for displaying LogEvents with timestamps | ||
*/ | ||
class LogBar : Tab(false, false), LogEvent.LogEventListener { | ||
|
||
private val root = VisTable() | ||
private val logTable = VisTable() | ||
private val pane = VisScrollPane(logTable) | ||
|
||
private val logOpsMenu = PopupMenu() | ||
private val clearLogsButton = MenuItem("Clear Logs") | ||
|
||
private val maxLogSize = 75 | ||
private val dateFormat = SimpleDateFormat("HH:mm:ss") | ||
|
||
// True when new entries are in the log and log is not the active tab | ||
var newEntries = false | ||
|
||
init { | ||
Mundus.registerEventListener(this) | ||
initUi() | ||
} | ||
|
||
private fun initUi() { | ||
root.setBackground("window-bg") | ||
root.left().top() | ||
root.add(pane).top().fillX().expandX() | ||
|
||
pane.fadeScrollBars = false | ||
|
||
logOpsMenu.addItem(clearLogsButton) | ||
registerListeners() | ||
} | ||
|
||
private fun registerListeners() { | ||
// Pop up menu on right click | ||
root.addListener(object : InputListener() { | ||
override fun touchDown(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int): Boolean { | ||
return true | ||
} | ||
|
||
override fun touchUp(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int) { | ||
if (event!!.button == Input.Buttons.RIGHT) { | ||
logOpsMenu.showMenu(UI, Gdx.input.x.toFloat(), | ||
(Gdx.graphics.height - Gdx.input.y).toFloat()) | ||
} | ||
} | ||
|
||
override fun enter(event: InputEvent, x: Float, y: Float, pointer: Int, fromActor: Actor?) { | ||
// Give scroll focus to pane automatically when mouse enters | ||
UI.scrollFocus = pane | ||
} | ||
|
||
override fun exit(event: InputEvent, x: Float, y: Float, pointer: Int, toActor: Actor?) { | ||
// Only clear focus if the exit to another actor is NOT an actor within the LogBars root | ||
if (toActor?.isDescendantOf(root) != true) | ||
UI.scrollFocus = null | ||
} | ||
}) | ||
|
||
clearLogsButton.addListener(object : ClickListener() { | ||
override fun clicked(event: InputEvent, x: Float, y: Float) { | ||
logTable.clearChildren() | ||
} | ||
}) | ||
} | ||
|
||
override fun onShow() { | ||
super.onShow() | ||
newEntries = false | ||
} | ||
|
||
override fun getTabTitle(): String { | ||
if (newEntries) | ||
return "Log*" | ||
|
||
return "Log" | ||
} | ||
|
||
override fun getContentTable(): Table { | ||
return root | ||
} | ||
|
||
override fun onLogEvent(event: LogEvent) { | ||
addLogMessage(event.logMessage) | ||
} | ||
|
||
/** | ||
* Appends new log message with a time stamp to the log table, then scrolls to most recent entry and | ||
* removes old entries. | ||
*/ | ||
private fun addLogMessage(message : String) { | ||
if (!isActiveTab) | ||
newEntries = true | ||
|
||
val timeStamp = dateFormat.format(Date()) | ||
|
||
val logString = buildString { | ||
append("[") | ||
append(timeStamp) | ||
append("] ") | ||
append(message) | ||
} | ||
|
||
logTable.add(VisLabel(logString)).left().pad(4f).expand().row() | ||
|
||
// Remove oldest entry | ||
if (logTable.cells.size > maxLogSize) | ||
logTable.removeActorAt(0, true) | ||
|
||
scrollToBottom() | ||
} | ||
|
||
private fun scrollToBottom() { | ||
// Update layout and scroll to the latest (bottom) log message | ||
pane.layout() | ||
pane.scrollTo(0f, 0f, 0f, 0f) | ||
} | ||
} |