-
Notifications
You must be signed in to change notification settings - Fork 7
/
ObjectTypeControlRenderer.go
71 lines (66 loc) · 2.08 KB
/
ObjectTypeControlRenderer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package values
import (
"github.com/inkyblackness/imgui-go"
"github.com/inkyblackness/hacked/ss1/content/object"
"github.com/inkyblackness/hacked/ss1/content/text"
"github.com/inkyblackness/hacked/ss1/resource"
"github.com/inkyblackness/hacked/ss1/world/ids"
)
// ObjectTypeControlRenderer renders a control to select an object.
type ObjectTypeControlRenderer struct {
// Meta describes the layout of classes, subclasses, and types.
Meta object.PropertiesTable
// TextCache is used to retrieve the name of a type.
TextCache *text.Cache
}
// Render creates the controls according to the given parameters.
func (renderer ObjectTypeControlRenderer) Render(readOnly, multiple bool, label string,
classUnifier Unifier, typeUnifier Unifier,
tripleResolver func(Unifier) object.Triple,
changeHandler func(object.Triple)) {
switch {
case classUnifier.IsUnique():
class := tripleResolver(classUnifier).Class
triples := renderer.Meta.TriplesInClass(class)
selectedIndex := -1
if typeUnifier.IsUnique() {
triple := tripleResolver(typeUnifier)
for index, availableTriple := range triples {
if availableTriple == triple {
selectedIndex = index
}
}
if selectedIndex < 0 {
selectedIndex = len(triples)
triples = append(triples, triple)
}
}
RenderUnifiedCombo(readOnly, multiple, label, typeUnifier,
func(u Unifier) int { return selectedIndex },
func(value int) string {
triple := triples[value]
return renderer.tripleName(triple)
},
len(triples),
func(newValue int) {
triple := triples[newValue]
changeHandler(triple)
})
case multiple:
imgui.LabelText(label, "(multiple classes)")
default:
imgui.LabelText(label, "")
}
}
func (renderer ObjectTypeControlRenderer) tripleName(triple object.Triple) string {
suffix := "???"
linearIndex := renderer.Meta.TripleIndex(triple)
if (linearIndex >= 0) && (renderer.TextCache != nil) {
key := resource.KeyOf(ids.ObjectLongNames, resource.LangDefault, linearIndex)
objName, err := renderer.TextCache.Text(key)
if err == nil {
suffix = objName
}
}
return triple.String() + ": " + suffix
}