some inventory improvements
This commit is contained in:
@ -1,32 +1,79 @@
|
||||
package screens
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/items"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/types"
|
||||
blt "lab.zaar.be/thefish/bearlibterminal"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InventoryScreen struct {
|
||||
*MenuScreen
|
||||
items []ecs.Entity
|
||||
|
||||
cursor int
|
||||
offset int
|
||||
who ecs.Entity
|
||||
|
||||
cursor int
|
||||
offset int
|
||||
pageSize int
|
||||
selected []int
|
||||
|
||||
prepared preparedList
|
||||
}
|
||||
|
||||
func (is *InventoryScreen) MakeInverntory() *InventoryScreen {
|
||||
type displayItem struct {
|
||||
Entity ecs.Entity
|
||||
Name string
|
||||
Index string
|
||||
Selected bool
|
||||
}
|
||||
|
||||
type preparedList []displayItem
|
||||
|
||||
func (p *preparedList) Prepare(is *InventoryScreen) {
|
||||
//fixme add item class sorting
|
||||
//fixme text instead of list to rpint and use printinside()
|
||||
bp := items.Controller.GetComponent(is.who, ecs.BackpackComponent).(items.Backpack)
|
||||
is.prepared = make([]displayItem, len(bp.GetItems()))
|
||||
//todo prepare item list
|
||||
for i, ent := range bp.GetItems() {
|
||||
namec := items.Controller.GetComponent(ent, ecs.Named{}.Type()).(ecs.Named)
|
||||
is.prepared[i] = displayItem{
|
||||
Entity: ent,
|
||||
Name: namec.Name,
|
||||
Index: string(runeIndex[i]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (is *InventoryScreen) MakeInverntory(who ecs.Entity) *InventoryScreen {
|
||||
is.drawFunc = is.InventoryRender
|
||||
is.inputFunc = is.HandleInput
|
||||
is.who = who
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *InventoryScreen) SetItems(items []ecs.Entity) *InventoryScreen {
|
||||
is.items = items
|
||||
return is
|
||||
func (is *InventoryScreen) Enter() {
|
||||
is.redraw = true
|
||||
is.offset = 0
|
||||
is.cursor = 0
|
||||
|
||||
is.prepared = preparedList{}
|
||||
is.prepared.Prepare(is)
|
||||
}
|
||||
|
||||
func (is *InventoryScreen) HandleInput(input string) {
|
||||
if strings.Contains(string(runeIndex), strings.Replace(input, "Shift+", "", -1)) {
|
||||
if strings.Contains("Shift+", input) {
|
||||
input = strings.ToUpper(input)
|
||||
}
|
||||
for i, _ := range is.prepared {
|
||||
if is.prepared[i].Index == input {
|
||||
is.prepared[i].Selected = !is.prepared[i].Selected
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
switch input {
|
||||
case "PageUp":
|
||||
is.offset = is.offset - 1
|
||||
@ -36,16 +83,13 @@ func (is *InventoryScreen) HandleInput(input string) {
|
||||
break
|
||||
case "PageDown":
|
||||
is.offset = is.offset + 1
|
||||
if is.offset > len(is.items)-1 {
|
||||
is.offset = len(is.items) - 1
|
||||
if is.offset > len(is.prepared)-1 {
|
||||
is.offset = len(is.prepared) - 1
|
||||
}
|
||||
break
|
||||
case "enter":
|
||||
//select current under cursor
|
||||
break;
|
||||
case "a","b","c","d","e","f","g","h","i","j","k","l":
|
||||
//selct by letter
|
||||
break;
|
||||
case "Escape":
|
||||
fallthrough
|
||||
case "Space":
|
||||
@ -54,8 +98,6 @@ func (is *InventoryScreen) HandleInput(input string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (is *InventoryScreen) InventoryRender() {
|
||||
menuLayer := is.mw.GetLayer("menu")
|
||||
menuLayer.ClearRect(is.Rect)
|
||||
@ -72,26 +114,35 @@ func (is *InventoryScreen) InventoryRender() {
|
||||
_, headerHeight := menuLayer.PrintInside(is.Rect, is.header, blt.TK_ALIGN_LEFT)
|
||||
itemField := types.Rect{is.X, is.Y + headerHeight + 1, is.W, is.H - headerHeight - footerHeight}
|
||||
_ = itemField
|
||||
var ilw, ilh int
|
||||
if (len(is.items) > 0) {
|
||||
//fixme itemfield object, scroller, inputhandler, current selected item
|
||||
menuItems := make([]string, 0)
|
||||
for i := is.offset; i < len(is.items); i++ {
|
||||
if string(is.items[i].(string)) != "" {
|
||||
menuItems = append(menuItems, is.items[i].(string))
|
||||
|
||||
if (len(is.prepared) > 0) {
|
||||
|
||||
for i := is.offset; i < itemField.H; i++ {
|
||||
if i < len(is.prepared) {
|
||||
selectedColor := "blue"
|
||||
selectedDash := "-"
|
||||
if is.prepared[i].Selected {
|
||||
selectedColor = "green"
|
||||
selectedDash = "+"
|
||||
}
|
||||
if is.cursor == i {
|
||||
menuLayer.WithColor("yellow")
|
||||
} else {
|
||||
menuLayer.WithColor("white")
|
||||
}
|
||||
menuStr := fmt.Sprintf("[color=%s]%s[/color] %s %s", selectedColor, is.prepared[i].Index, selectedDash, is.prepared[i].Name)
|
||||
if len(menuStr) > itemField.W - 2 {
|
||||
menuStr = menuStr[0:itemField.W - 2]
|
||||
}
|
||||
menuLayer.PutStringInto(itemField,i,menuStr,1)
|
||||
}
|
||||
}
|
||||
ilw, ilh = menuLayer.PrintInside(itemField, strings.Join(menuItems, "\n"), blt.TK_ALIGN_LEFT)
|
||||
}
|
||||
if ilh < len(is.items) {
|
||||
if is.cursor < len(is.prepared) {
|
||||
is.drawScrollBar(menuLayer, itemField)
|
||||
}
|
||||
if ilw > itemField.W-4 {
|
||||
fmt.Printf("Excess width of item names found! Need h-scroll of certain names")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//func (ms *InventoryScreen) UseEcs() bool {return true}
|
||||
//func (ms *InventoryScreen) Enter() {}
|
||||
|
||||
|
Reference in New Issue
Block a user