Skip to content

Commit

Permalink
Code gen export in imgui window
Browse files Browse the repository at this point in the history
  • Loading branch information
serivesmejia committed Sep 22, 2024
1 parent 78ece97 commit f28bdc8
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 32 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import io.github.deltacv.papervision.engine.bridge.LocalPaperVisionEngineBridge
import io.github.deltacv.papervision.engine.client.message.*
import io.github.deltacv.papervision.engine.client.response.BooleanResponse
import io.github.deltacv.papervision.engine.client.response.OkResponse
import io.github.deltacv.papervision.engine.client.response.StringResponse
import io.github.deltacv.papervision.platform.lwjgl.PaperVisionApp
import io.github.deltacv.papervision.plugin.eocvsim.PaperVisionDefaultPipeline
import io.github.deltacv.papervision.plugin.eocvsim.PrevizSession
import io.github.deltacv.papervision.plugin.gui.CloseConfirmWindow
import io.github.deltacv.papervision.plugin.gui.eocvsim.PaperVisionTabPanel
import io.github.deltacv.papervision.plugin.gui.eocvsim.dialog.PaperVisionDialogFactory
import io.github.deltacv.papervision.plugin.project.PaperVisionProjectManager
import io.github.deltacv.papervision.util.replaceLast
import org.opencv.core.Size
import java.awt.print.Paper
import javax.swing.JOptionPane
Expand Down Expand Up @@ -90,10 +92,13 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() {
CloseConfirmWindow.Action.YES -> {
paperVisionProjectManager.saveCurrentProject()
PaperVisionDaemon.hidePaperVision()

paperVisionProjectManager.closeCurrentProject()
}
CloseConfirmWindow.Action.NO -> {
PaperVisionDaemon.hidePaperVision()
paperVisionProjectManager.discardCurrentRecovery()
paperVisionProjectManager.closeCurrentProject()
}
else -> { /* NO-OP */ }
}
Expand All @@ -118,6 +123,12 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() {
respond(OkResponse())
}

engine.setMessageHandlerOf<PrevizAskNameMessage> {
respond(StringResponse(
paperVisionProjectManager.currentProject?.name?.replaceLast(".paperproj", "") ?: "Mack"
))
}

engine.setMessageHandlerOf<PrevizPingPongMessage> {
respond(BooleanResponse(
currentPrevizSession?.sessionName == message.previzName && currentPrevizSession?.previzRunning ?: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ class PaperVisionProjectManager(
refresh()
}

fun closeCurrentProject() {
currentProject = null
PaperVisionDaemon.paperVision.previzManager.stopPreviz()
}

fun deleteAllRecoveredProjects() {
for(file in recoveryFolder.listFiles() ?: arrayOf()) {
if(file.extension == "recoverypaperproj") {
Expand Down
2 changes: 1 addition & 1 deletion EOCVSimPlugin/src/main/resources/plugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "1.0.0"
api-version = "3.6.0"

super-access = true
super-access-reason = "gg ez"
super-access-reason = "PaperVision requires your permision to fully support all features related to rendering user interaction and filesystem access."

main = "io.github.deltacv.papervision.plugin.PaperVisionEOCVSimPlugin"
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import io.github.deltacv.papervision.util.loggerForThis
import io.github.deltacv.mai18n.Language
import io.github.deltacv.papervision.engine.bridge.NoOpPaperVisionEngineBridge
import io.github.deltacv.papervision.engine.client.PaperVisionEngineClient
import io.github.deltacv.papervision.engine.client.message.PrevizAskNameMessage
import io.github.deltacv.papervision.engine.client.response.StringResponse
import io.github.deltacv.papervision.engine.previz.ClientPrevizManager
import io.github.deltacv.papervision.gui.style.CurrentStyles
import io.github.deltacv.papervision.gui.util.Popup.Companion.WARN
Expand Down Expand Up @@ -209,4 +211,13 @@ class PaperVision(
fun destroy() {
nodeEditor.destroy()
}

fun startPrevizAsk() {
engineClient.sendMessage(PrevizAskNameMessage().onResponseWith<StringResponse> { response ->
logger.info("Engine responded with previz name ${response.value}")
onUpdate.doOnce {
previzManager.startPreviz(response.value)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CodeGenManager(val paperVision: PaperVision) {

codeGen.stage = CodeGen.Stage.ENDED_SUCCESS

return result
return result.trim()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import io.github.deltacv.papervision.codegen.CodeGenManager
import io.github.deltacv.papervision.codegen.language.Language
import io.github.deltacv.papervision.codegen.language.jvm.JavaLanguage
import io.github.deltacv.papervision.engine.client.PaperVisionEngineClient
import io.github.deltacv.papervision.engine.client.message.PrevizAskNameMessage
import io.github.deltacv.papervision.engine.client.message.PrevizPingPongMessage
import io.github.deltacv.papervision.engine.client.message.PrevizSourceCodeMessage
import io.github.deltacv.papervision.engine.client.message.PrevizSetStreamResolutionMessage
import io.github.deltacv.papervision.engine.client.response.BooleanResponse
import io.github.deltacv.papervision.engine.client.response.OkResponse
import io.github.deltacv.papervision.engine.client.response.StringResponse
import io.github.deltacv.papervision.io.PipelineStream
import io.github.deltacv.papervision.io.TextureProcessorQueue
import io.github.deltacv.papervision.io.bufferedImageFromResource
Expand Down Expand Up @@ -57,7 +59,7 @@ class ClientPrevizManager(

logger.info("Starting previz session $previzName")

client.sendMessage(PrevizSourceCodeMessage(previzName, sourceCode).onResponse<OkResponse> {
client.sendMessage(PrevizSourceCodeMessage(previzName, sourceCode).onResponseWith<OkResponse> {
client.onProcess.doOnce {
logger.info("Previz session $previzName running")

Expand All @@ -68,7 +70,7 @@ class ClientPrevizManager(

client.sendMessage(PrevizSetStreamResolutionMessage(
previzName, previzStreamWidth, previzStreamHeight
).onResponse<OkResponse> {
).onResponseWith<OkResponse> {
client.onProcess.doOnce {
stream = PipelineStream(
previzName, client, textureProcessorQueue,
Expand Down Expand Up @@ -102,7 +104,7 @@ class ClientPrevizManager(
// send every 200 ms
if(pingPongTimer.seconds > 0.2) {
if(previzRunning) {
client.sendMessage(PrevizPingPongMessage(previzName!!).onResponse<BooleanResponse> {
client.sendMessage(PrevizPingPongMessage(previzName!!).onResponseWith<BooleanResponse> {
previzRunning = it.value
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.deltacv.papervision.gui

import imgui.ImGui
import imgui.ImVec2
import imgui.extension.texteditor.TextEditor
import imgui.extension.texteditor.TextEditorLanguageDefinition
import imgui.flag.ImGuiWindowFlags
import io.github.deltacv.papervision.gui.util.Window
import io.github.deltacv.papervision.util.flags
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection

class CodeDisplayWindow(
val code: String,
val language: TextEditorLanguageDefinition
) : Window() {
override var title = "Code"

override val windowFlags = flags(
ImGuiWindowFlags.NoResize,
ImGuiWindowFlags.NoCollapse,
ImGuiWindowFlags.NoMove,
ImGuiWindowFlags.HorizontalScrollbar
)

override val isModal = true

val EDITOR = TextEditor()

override fun onEnable() {
focus = true

EDITOR.isReadOnly

EDITOR.setLanguageDefinition(language)
EDITOR.textLines = code.lines().toTypedArray()

size = ImVec2(500f, 400f)
}

override fun drawContents() {
EDITOR.render("Code")

ImGui.newLine()

if(ImGui.button("Copy Code")) {
Toolkit.getDefaultToolkit().systemClipboard.setContents(
StringSelection(EDITOR.text), null
)
}

ImGui.sameLine()

if(ImGui.button("Export to File")) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import imgui.ImGui
import imgui.ImVec2
import imgui.extension.imnodes.ImNodes
import imgui.extension.imnodes.ImNodesContext
import imgui.extension.texteditor.TextEditorLanguageDefinition
import imgui.flag.ImGuiMouseButton
import imgui.flag.ImGuiWindowFlags
import imgui.type.ImInt
import io.github.deltacv.papervision.PaperVision
import io.github.deltacv.papervision.attribute.Attribute
import io.github.deltacv.papervision.attribute.AttributeMode
import io.github.deltacv.papervision.codegen.CodeGenManager
import io.github.deltacv.papervision.codegen.language.jvm.JavaLanguage
import io.github.deltacv.papervision.engine.client.PaperVisionEngineClient
import io.github.deltacv.papervision.engine.previz.ClientPrevizManager
import io.github.deltacv.papervision.util.eocvsim.EOCVSimPrevizState.*
Expand Down Expand Up @@ -59,6 +62,9 @@ class NodeEditor(val paperVision: PaperVision, val keyManager: KeyManager) : Win
lateinit var playButton: EOCVSimPlayButtonWindow
private set

lateinit var sourceCodeExportButton: SourceCodeExportButtonWindow
private set

lateinit var eyeFont: Font
private set

Expand Down Expand Up @@ -100,11 +106,21 @@ class NodeEditor(val paperVision: PaperVision, val keyManager: KeyManager) : Win
inputNode.enable()
outputNode.enable()

playButton = EOCVSimPlayButtonWindow(
sourceCodeExportButton = SourceCodeExportButtonWindow(
{ paperVision.nodeList.floatingButton },
paperVision.previzManager,
{ size },
paperVision.fontManager,
paperVision.codeGenManager
)

sourceCodeExportButton.enable()

playButton = EOCVSimPlayButtonWindow(
sourceCodeExportButton,
paperVision,
paperVision.fontManager
)

playButton.enable()
}

Expand Down Expand Up @@ -339,13 +355,50 @@ class NodeEditor(val paperVision: PaperVision, val keyManager: KeyManager) : Win
ImNodes.destroyContext()
}

fun recreateContext() {
context = ImNodesContext()
class SourceCodeExportButtonWindow(
val floatingButtonSupplier: () -> NodeList.FloatingButton,
val nodeEditorSizeSupplier: () -> ImVec2,
fontManager: FontManager,
val codeGenManager: CodeGenManager
) : Window() {
override var title = ""
override val windowFlags = flags(
ImGuiWindowFlags.NoBackground, ImGuiWindowFlags.NoTitleBar,
ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove,
ImGuiWindowFlags.AlwaysAutoResize
)

val frameWidth get() = floatingButtonSupplier().frameWidth

val sourceFont = fontManager.makeFont("/fonts/icons/Source.ttf", NodeList.plusFontSize * 0.65f)

override fun preDrawContents() {
position = ImVec2(
floatingButtonSupplier().position.x - NodeList.plusFontSize * 1.5f,
floatingButtonSupplier().position.y,
)
}

override fun drawContents() {
ImGui.pushFont(sourceFont.imfont)

if(ImGui.button("S", floatingButtonSupplier().frameWidth, floatingButtonSupplier().frameWidth)) {
CodeDisplayWindow(
codeGenManager.build("pp", JavaLanguage),
TextEditorLanguageDefinition.cPlusPlus()
).apply {
enable()
size = ImVec2(nodeEditorSizeSupplier().x * 0.8f, nodeEditorSizeSupplier().y * 0.8f)
}
}

ImGui.popFont()
}
}

class EOCVSimPlayButtonWindow(
val floatingButtonSupplier: () -> FrameWidthWindow,
val previzManager: ClientPrevizManager,
val sourceCodeExportButton: SourceCodeExportButtonWindow,
val paperVision: PaperVision,
fontManager: FontManager
) : Window() {

Expand All @@ -361,7 +414,7 @@ class NodeEditor(val paperVision: PaperVision, val keyManager: KeyManager) : Win
val playPauseDotsFont = fontManager.makeFont("/fonts/icons/Play-Pause-Dots.ttf", NodeList.plusFontSize * 0.65f)

override fun preDrawContents() {
val floatingButton = floatingButtonSupplier()
val floatingButton = sourceCodeExportButton

position = ImVec2(
floatingButton.position.x - NodeList.plusFontSize * 1.5f,
Expand All @@ -370,21 +423,21 @@ class NodeEditor(val paperVision: PaperVision, val keyManager: KeyManager) : Win
}

override fun drawContents() {
val floatingButton = floatingButtonSupplier()
val floatingButton = sourceCodeExportButton

ImGui.pushFont(playPauseDotsFont.imfont)

val text = if(previzManager.previzRunning) {
val text = if(paperVision.previzManager.previzRunning) {
"-"
} else "+"

val button = ImGui.button(text, floatingButton.frameWidth, floatingButton.frameWidth)

if (lastButton != button && button) {
if(!previzManager.previzRunning) {
previzManager.startPreviz("epico")
if(!paperVision.previzManager.previzRunning) {
paperVision.startPrevizAsk()
} else {
previzManager.stopPreviz()
paperVision.previzManager.stopPreviz()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ abstract class Window(
}
get() = realFocus

private var firstDraw = true

override fun enable() {
super.enable()

if(isModal)
ImGui.openPopup("${tr(title)}###$id")
firstDraw = true
}

override fun draw() {
Expand All @@ -72,16 +72,25 @@ abstract class Window(
}

if(isModal) {
if(firstDraw)
ImGui.openPopup("${tr(title)}###$id")

if(ImGui.beginPopupModal("${tr(title)}###$id", modalPOpen, windowFlags)) {
contents()
ImGui.endPopup()
} else {
delete()
}
} else {
if(ImGui.begin("${tr(title)}###$id", windowFlags)) {
contents()
ImGui.end()
} else {
delete()
}
}

firstDraw = false
}

private fun contents() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.deltacv.papervision.util

fun String.replaceLast(oldValue: String, newValue: String): String {
val lastIndex = lastIndexOf(oldValue)
if (lastIndex == -1) {
return this
}
val prefix = substring(0, lastIndex)
val suffix = substring(lastIndex + oldValue.length)
return "$prefix$newValue$suffix"
}
Binary file not shown.
Loading

0 comments on commit f28bdc8

Please sign in to comment.