|
| 1 | +// This file is part of Gopher2600. |
| 2 | +// |
| 3 | +// Gopher2600 is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// Gopher2600 is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +package sdlimgui |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strconv" |
| 21 | + |
| 22 | + "github.com/inkyblackness/imgui-go/v2" |
| 23 | + "github.com/jetsetilly/gopher2600/hardware/riot/ports/savekey" |
| 24 | +) |
| 25 | + |
| 26 | +const winSaveKeyEEPROMTitle = "SaveKey EEPROM" |
| 27 | +const menuSaveKeyEEPROMTitle = "EEPROM" |
| 28 | + |
| 29 | +type winSaveKeyEEPROM struct { |
| 30 | + windowManagement |
| 31 | + widgetDimensions |
| 32 | + |
| 33 | + img *SdlImgui |
| 34 | + |
| 35 | + // height of status line at bottom of window. valid after first frame |
| 36 | + statusHeight float32 |
| 37 | + |
| 38 | + // the X position of the grid header. based on the width of the column |
| 39 | + // headers (we know this value after the first pass) |
| 40 | + xPos float32 |
| 41 | +} |
| 42 | + |
| 43 | +func newWinSaveKeyEEPROM(img *SdlImgui) (managedWindow, error) { |
| 44 | + win := &winSaveKeyEEPROM{img: img} |
| 45 | + return win, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (win *winSaveKeyEEPROM) init() { |
| 49 | + win.widgetDimensions.init() |
| 50 | +} |
| 51 | + |
| 52 | +func (win *winSaveKeyEEPROM) destroy() { |
| 53 | +} |
| 54 | + |
| 55 | +func (win *winSaveKeyEEPROM) id() string { |
| 56 | + return winSaveKeyEEPROMTitle |
| 57 | +} |
| 58 | + |
| 59 | +func (win *winSaveKeyEEPROM) draw() { |
| 60 | + if !win.open { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if !win.img.lz.SaveKey.SaveKeyActive { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + imgui.SetNextWindowPosV(imgui.Vec2{469, 285}, imgui.ConditionFirstUseEver, imgui.Vec2{0, 0}) |
| 69 | + imgui.SetNextWindowSizeV(imgui.Vec2{394, 356}, imgui.ConditionFirstUseEver) |
| 70 | + imgui.BeginV(winSaveKeyEEPROMTitle, &win.open, 0) |
| 71 | + |
| 72 | + win.drawGrid(winSaveKeyEEPROMTitle, win.img.lz.SaveKey.EEPROMdata) |
| 73 | + win.drawStatusLine() |
| 74 | + |
| 75 | + imgui.End() |
| 76 | +} |
| 77 | + |
| 78 | +func (win *winSaveKeyEEPROM) drawStatusLine() { |
| 79 | + statusHeight := imgui.CursorPosY() |
| 80 | + |
| 81 | + imgui.Spacing() |
| 82 | + |
| 83 | + if imgui.Button("Save to disk") { |
| 84 | + win.img.lz.Dbg.PushRawEvent(func() { |
| 85 | + if sk, ok := win.img.lz.Dbg.VCS.RIOT.Ports.Player1.(*savekey.SaveKey); ok { |
| 86 | + sk.EEPROM.Write() |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + imgui.SameLine() |
| 91 | + |
| 92 | + imgui.AlignTextToFramePadding() |
| 93 | + if win.img.lz.SaveKey.Dirty { |
| 94 | + imgui.Text("Data is NOT saved") |
| 95 | + } else { |
| 96 | + imgui.Text("Data is saved") |
| 97 | + } |
| 98 | + |
| 99 | + win.statusHeight = imgui.CursorPosY() - statusHeight |
| 100 | +} |
| 101 | + |
| 102 | +func (win *winSaveKeyEEPROM) drawGrid(tag string, a []byte) { |
| 103 | + const numberOfColumns = 16 |
| 104 | + |
| 105 | + height := imguiRemainingWinHeight() - win.statusHeight |
| 106 | + imgui.BeginChildV(tag, imgui.Vec2{X: 0, Y: height}, false, 0) |
| 107 | + |
| 108 | + // no spacing between any of the drawEditByte() objects |
| 109 | + imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{}) |
| 110 | + |
| 111 | + // draw headers for each column. this relies on win.xPos, which requires |
| 112 | + // one frame before it is accurate. |
| 113 | + headerDim := imgui.Vec2{X: win.xPos, Y: imgui.CursorPosY()} |
| 114 | + for i := 0; i < numberOfColumns; i++ { |
| 115 | + imgui.SetCursorPos(headerDim) |
| 116 | + headerDim.X += win.twoDigitDim.X |
| 117 | + imgui.AlignTextToFramePadding() |
| 118 | + imgui.Text(fmt.Sprintf("-%x", i)) |
| 119 | + } |
| 120 | + |
| 121 | + // draw rows |
| 122 | + var clipper imgui.ListClipper |
| 123 | + clipper.Begin(len(a) / numberOfColumns) |
| 124 | + for clipper.Step() { |
| 125 | + for i := clipper.DisplayStart; i < clipper.DisplayEnd; i++ { |
| 126 | + offset := (i * numberOfColumns) |
| 127 | + imgui.AlignTextToFramePadding() |
| 128 | + imgui.Text(fmt.Sprintf("%03x- ", i)) |
| 129 | + imgui.SameLine() |
| 130 | + win.xPos = imgui.CursorPosX() |
| 131 | + |
| 132 | + imgui.PushItemWidth(win.twoDigitDim.X) |
| 133 | + for j := 0; j < numberOfColumns; j++ { |
| 134 | + imgui.SameLine() |
| 135 | + win.drawEditByte(tag, uint16(offset+j), a[offset+j]) |
| 136 | + } |
| 137 | + imgui.PopItemWidth() |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // finished with spacing setting |
| 142 | + imgui.PopStyleVar() |
| 143 | + |
| 144 | + imgui.EndChild() |
| 145 | +} |
| 146 | + |
| 147 | +func (win *winSaveKeyEEPROM) drawEditByte(tag string, address uint16, data byte) { |
| 148 | + l := fmt.Sprintf("##%d", address) |
| 149 | + content := fmt.Sprintf("%02x", data) |
| 150 | + |
| 151 | + if imguiHexInput(l, !win.img.paused, 2, &content) { |
| 152 | + if v, err := strconv.ParseUint(content, 16, 8); err == nil { |
| 153 | + win.img.lz.Dbg.PushRawEvent(func() { |
| 154 | + if sk, ok := win.img.lz.Dbg.VCS.RIOT.Ports.Player1.(*savekey.SaveKey); ok { |
| 155 | + sk.EEPROM.Poke(address, uint8(v)) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments