func (im *InventoryModel) SlotUpdated(index int) { // For now we ignore that a slot is updated and just reset the whole thing. Lazy, and slower, I know! qml.Lock() im.Size -= 1 qml.Changed(im, &im.Size) im.Size += 1 qml.Changed(im, &im.Size) qml.Unlock() build_model.Update(im.Entity.EntityID) }
func (c *Control) Search(searchstring string, mode string, active_dicts []string) { go func() { busy <- true c.SearchResult = "" spacing := " " if mode == "startswith" { spacing = "" } // log.Printf( // "Active dictionaries: %v\nSearch string: %s\nSearch mode: %s\n", // active_dicts, // searchstring, // mode, // ) for _, d := range active_dicts { data, err := ioutil.ReadFile(path.Join(DICT_PATH, d)) if err != nil { log.Fatal(err) } for _, entry := range strings.Split(string(data), "\n") { if strings.HasPrefix(strings.ToLower(entry), searchstring+spacing) { // log.Println(entry) c.SearchResult += fmt.Sprintf("<b>%s:</b><br />%s<br /><br />", d, entry) } } } qml.Changed(c, &c.SearchResult) busy <- false }() }
func (bm *BuildModel) Update(entity_id int) { qml.Lock() bm.Entity = w.Entities[entity_id] record := make(map[*Archetype]int) if bm.Entity != nil { // These first two lines ensure that a complete redraw is performed: bm.Size -= 1 qml.Changed(bm, &bm.Size) // Check how many inventory items are buildable: count := 0 for _, e := range bm.Entity.Inventory.Slots { if e != nil { a := w.Archetypes[e.SimpleName] if a.Buildable { record[a] += e.Quantity count++ } } } bm.Size = len(record) bm.Slots = make([]*BuildSlot, len(record)) count = 0 for a, q := range record { bm.Slots[count] = &BuildSlot{count, a, q} count++ } // for _, e := range bm.Entity.Inventory.Slots { // if e != nil && w.Archetypes[e.SimpleName].Buildable { // bm.Slots[count] = &BuildSlot{count, w.Archetypes[e.SimpleName], e.Quantity} // count++ // } // } } else { bm.Size = 0 bm.Slots = make([]*BuildSlot, 0) } qml.Changed(bm, &bm.Size) qml.Unlock() }
func (cm *CraftModel) AddArchetype(a *Archetype) { qml.Lock() cm.Craftables = append(cm.Craftables, &CraftElement{Archetype: a, Ready: false}) cm.Len = len(cm.Craftables) qml.Changed(cm, &cm.Len) qml.Unlock() }
func (ce *CraftElement) UpdateRequirements() { ce.reqs = make([]CraftReq, len(ce.Archetype.CraftRequirements)) count := 0 for na, quantity := range ce.Archetype.CraftRequirements { ce.reqs[count] = CraftReq{na, quantity} count++ } ce.Len = len(ce.reqs) qml.Changed(ce, &ce.Len) }
func (c *Control) Load(dictionary string) { go func() { // log.Println("LOADING", dictionary) c.SearchResult = "" busy <- true data, err := ioutil.ReadFile(path.Join(DICT_PATH, dictionary)) if err != nil { log.Fatal(err) } c.SearchResult = string(data) // log.Println("LOADED!") qml.Changed(c, &c.SearchResult) busy <- false }() }
func (im *InventoryModel) SetContainer(entity_id int) { // This function should only be called once whenever the vessel is set. Vessel is the entity which represents the player qml.Lock() im.Entity = w.Entities[entity_id] if im.Entity != nil { // These first two lines ensure that a complete redraw is performed: im.Size -= 1 qml.Changed(im, &im.Size) im.Size = len(im.Entity.Inventory.Slots) im.Slots = make([]*InventorySlot, im.Size) for i, _ := range im.Slots { im.Slots[i] = &InventorySlot{} } } else { im.Size = 0 im.Slots = make([]*InventorySlot, 0) } qml.Changed(im, &im.Size) qml.Unlock() build_model.Update(entity_id) }
func (jm *JobModel) Update() { qml.Lock() jm.Len = len(w.Jobs) jm.Jobs = make([]*kinglib.JobStatus, jm.Len) count := 0 for _, js := range w.Jobs { jm.Jobs[count] = js count++ } // fmt.Printf("JobModel Length: %d\n", jm.Len) qml.Unlock() qml.Changed(jm, &jm.Len) }
func (ctrl *Control) TextReleased(text qml.Object) { x := text.Int("x") y := text.Int("y") width := text.Int("width") height := text.Int("height") ctrl.Emit(x+15, y+height/2) ctrl.Emit(x+width/2, 1.0*y+height/2) ctrl.Emit(x+width-15, 1.0*y+height/2) go func() { time.Sleep(500 * time.Millisecond) messages := []string{"Hello", "Hello", "Hacks"} ctrl.Message = messages[rand.Intn(len(messages))] + " from Go!" qml.Changed(ctrl, &ctrl.Message) }() }
func main() { // Profiling --> // f, err := os.Create("cpu.prof") // if err != nil { // log.Fatal(err) // } // pprof.StartCPUProfile(f) // defer pprof.StopCPUProfile() // <-- Profiling // Watching busy status busy = make(chan bool) go func() { for { ctrl.Busy = <-busy qml.Changed(ctrl, &ctrl.Busy) } }() qml.Init(nil) engine := qml.NewEngine() component, err := engine.LoadFile("qmldict.qml") if err != nil { panic(err) } ctrl.DictionariesList = GetDictList(DICT_PATH) ctrl.Len = len(ctrl.DictionariesList) context := engine.Context() context.SetVar("ctrl", ctrl) // log.Println(ctrl.DictionariesList) window := component.CreateWindow(nil) ctrl.Root = window.Root() window.Show() window.Wait() }
func (ts *GoType) NotifyStringChanged() { qml.Changed(ts, &ts.StringValue) }
Done: func(c *TestData) { c.Assert(c.createdValue, HasLen, 1) c.Assert(c.createdValue[0].StringValue, Equals, "<newest>") }, }, { Summary: "Singleton type registration", QML: `Item { Component.onCompleted: console.log("String is", GoSingleton.stringValue) }`, QMLLog: "String is <initial>", }, { Summary: "qml.Changed on unknown value is okay", Value: GoType{StringValue: "<old>"}, Init: func(c *TestData) { value := &GoType{} qml.Changed(&value, &value.StringValue) }, QML: `Item{}`, }, { Summary: "qml.Changed triggers a QML slot", QML: ` GoType { stringValue: "<old>" onStringValueChanged: console.log("String is", stringValue) } `, QMLLog: "!String is", Done: func(c *TestData) { c.Assert(c.createdValue, HasLen, 1) value := c.createdValue[0]
func (colors *Colors) Add(c color.RGBA) { colors.list = append(colors.list, c) colors.Len = len(colors.list) qml.Changed(colors, &colors.Len) }