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

Added buttons to select display type #5

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/main/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@
</article>
</div>
</div>
<div class="tile is-parent">
<article class="tile is-child">
<label>Display Settings</label>
<div class="tabs is-toggle">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be better stylistically as a bulma select?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and I think it looks better as a toggle switch actually, but you are welcome to switch it to the select if you think it is better

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's keep it as a toggle switch for now.

<ul>
<li id="hex-opt" class="is-active"><a onclick="driver.setDisplay('hex')">Hex</a></li>
<li id="dec-opt"><a onclick="driver.setDisplay('dec')">Decimal</a></li>
<li id="unsign-opt"><a onclick="driver.setDisplay('unsign')">Unsigned</a></li>
</ul>
</div>
</article>
</div>
</section>
<script type="text/javascript" src="js/codemirror.js"></script>
<script type="text/javascript" src="venus.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/venus/glue/Driver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,8 @@ import kotlin.browser.window
}
Renderer.updateRegister(id, sim.getReg(id))
}

@JsName("setDisplay") fun render_setDisplay(dis_type: String) {
Copy link
Owner

@kvakil kvakil Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to camelCase? Also maybe a better name would be setRegisterDisplay.

Renderer.setDisplay(dis_type)
}
}
61 changes: 60 additions & 1 deletion src/main/kotlin/venus/glue/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal object Renderer {
private var activeMemoryAddress: Int = 0
/** The simulator being rendered */
private lateinit var sim: Simulator
/* The way the information in the registers is displayed*/
private var display_type = "hex"

/**
* Shows the simulator tab and hides other tabs
Expand Down Expand Up @@ -166,7 +168,17 @@ internal object Renderer {
*/
fun updateRegister(id: Int, value: Int, setActive: Boolean = false) {
val register = getElement("reg-$id-val") as HTMLInputElement
register.value = toHex(value)
var shown_val: String
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kotlin supports the when block, so this could be more cleanly written as:

register.value = when (displayType) {
    "hex" -> toHex(value)
    "dec" -> value.toString()
    "unsign" -> toUnsigned(value)
    else -> toHex(value)
}

if (display_type.equals("hex")) {
shown_val = toHex(value)
} else if (display_type.equals("dec")) {
shown_val = value.toString()
} else if (display_type.equals("unsign")) {
shown_val = toUnsigned(value)
} else {
shown_val = toHex(value)
}
register.value = shown_val
if (setActive) {
activeRegister?.classList?.remove("is-modified")
register.classList.add("is-modified")
Expand Down Expand Up @@ -389,4 +401,51 @@ internal object Renderer {

return "0x" + suffix
}

private fun toUnsigned(value: Int): String {
var remainder = value.toLong()
var suffix = ""

// output as two's complement
if (remainder < 0) {
remainder += 0x1_0000_0000L
}

// convert to hex
while (remainder > 0) {
val hexDigit = hexMap[(remainder % 10).toInt()]
suffix = hexDigit + suffix
remainder /= 10
}

if (suffix.length < 1) {
suffix = "0"
}
return suffix
}

/**
* Sets the display type for all of the registers and memory
* Rerenders after
*/
fun setDisplay(dis_type: String) {
display_type = dis_type
optSetVisibily(dis_type)
updateAll()
}

private val opts = listOf("hex", "dec", "unsign")

private fun optSetVisibily(opt: String) {
var tabDisplay: HTMLElement?
for (option in opts) {
if (opt.equals(option)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need .equals for strings in Kotlin, just == is good. (If you need Object.equals from Java, that's ===.)

tabDisplay = document.getElementById("$option-opt") as HTMLElement
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this line out of the if statement?

tabDisplay.className = "is-active"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this a single line using the if statement:

tabDisplay.className = if (opt == option) "is-active" else ""

} else {
tabDisplay = document.getElementById("$option-opt") as HTMLElement
tabDisplay.className = ""
}
}
}
}