Skip to content

Commit

Permalink
added clearance of trap audio
Browse files Browse the repository at this point in the history
  • Loading branch information
dertseha committed Jul 21, 2018
1 parent 307ea10 commit 746f21c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
34 changes: 34 additions & 0 deletions editor/texts/SetAudioCommand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package texts

import (
"github.com/inkyblackness/hacked/editor/cmd"
"github.com/inkyblackness/hacked/ss1/resource"
)

type setAudioCommand struct {
model *viewModel
restoreKey resource.Key

dataKey resource.Key
oldData [][]byte
newData [][]byte
}

func (cmd setAudioCommand) Do(trans cmd.Transaction) error {
return cmd.perform(trans, cmd.newData)
}

func (cmd setAudioCommand) Undo(trans cmd.Transaction) error {
return cmd.perform(trans, cmd.oldData)
}

func (cmd setAudioCommand) perform(trans cmd.Transaction, data [][]byte) error {
if len(data) > 0 {
trans.SetResourceBlocks(cmd.dataKey.Lang, cmd.dataKey.ID, data)
} else {
trans.DelResource(cmd.dataKey.Lang, cmd.dataKey.ID)
}
cmd.model.restoreFocus = true
cmd.model.currentKey = cmd.restoreKey
return nil
}
39 changes: 28 additions & 11 deletions editor/texts/View.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (view *View) setTextFromClipboard() {
oldData, isList := view.currentModification()
if isList {
newData := view.cp.Encode(blockedValue[0])
view.requestSetTextLine(oldData[0], newData)
view.requestSetTextLine(oldData[0], newData, false)
} else {
newData := make([][]byte, len(blockedValue))
for index, blockLine := range blockedValue {
Expand All @@ -247,7 +247,7 @@ func (view *View) clearText() {
emptyLine := view.cp.Encode("")
currentModification, isList := view.currentModification()
if isList && !bytes.Equal(currentModification[0], emptyLine) {
view.requestSetTextLine(currentModification[0], emptyLine)
view.requestSetTextLine(currentModification[0], emptyLine, true)
} else if !isList && ((len(currentModification) != 1) || !bytes.Equal(currentModification[0], []byte{0x00})) {
view.requestSetTextPage(currentModification, [][]byte{emptyLine})
}
Expand All @@ -256,7 +256,7 @@ func (view *View) clearText() {
func (view *View) removeText() {
currentModification, isList := view.currentModification()
if isList && (len(currentModification[0]) > 0) {
view.requestSetTextLine(currentModification[0], nil)
view.requestSetTextLine(currentModification[0], nil, true)
} else if !isList && (len(currentModification) > 0) {
view.requestSetTextPage(currentModification, nil)
}
Expand All @@ -277,18 +277,35 @@ func (view *View) requestImportAudio() {
})
}

func (view *View) requestSetAudio(data []byte) {
func (view *View) setAudioCommand(data []byte) cmd.Command {
dataKey := resource.KeyOf(textToAudio[view.model.currentKey.ID].Plus(view.model.currentKey.Index), view.model.currentKey.Lang, 0)
return setAudioCommand{
restoreKey: view.model.currentKey,
model: &view.model,

dataKey: dataKey,
oldData: view.mod.ModifiedBlocks(dataKey.Lang, dataKey.ID),
newData: [][]byte{data},
}
}

func (view *View) requestSetTextLine(oldData []byte, newData []byte) {
command := setTextLineCommand{
key: view.model.currentKey,
model: &view.model,
oldData: oldData,
newData: newData,
func (view *View) requestSetAudio(data []byte) {
view.commander.Queue(view.setAudioCommand(data))
}

func (view *View) requestSetTextLine(oldData []byte, newData []byte, clearAudio bool) {
commands := cmd.List{
setTextLineCommand{
key: view.model.currentKey,
model: &view.model,
oldData: oldData,
newData: newData,
},
}
view.commander.Queue(command)
if clearAudio && view.hasAudio() {
commands = append(commands, view.setAudioCommand(nil))
}
view.commander.Queue(commands)
}

func (view *View) requestSetTextPage(oldData [][]byte, newData [][]byte) {
Expand Down

0 comments on commit 746f21c

Please sign in to comment.