shithub: wildlife

Download patch

ref: eca59fe0c9f2e78eca5246e626950ee5b408ca12
parent: 7084ab4489ce1ddb6595a8f2624e8fdf23a73d8f
author: Philip Silva <[email protected]>
date: Tue Mar 29 11:10:08 EDT 2022

More items, fix data structures, rect. mouse targets

--- a/README
+++ b/README
@@ -4,7 +4,7 @@
 
 Compile:
 
-go build .
+go build '-buildvcs=false' .
 
 Usage:
 
--- a/main.go
+++ b/main.go
@@ -26,63 +26,91 @@
 )
 
 type Folder struct {
-	*duit.Image
-
 	Pre string
 	Suf string
 	Name string
-	Items map[string]string
-	Targets []Target
+	Items map[string]*Item
 }
 
 func getFolder(root, pre string) (ff *Folder) {
-	//log.Printf("getFolder(%v, %v)", root, pre)
-	var rf *Folder
-	if root != "" {
-		for _, f := range folders {
-			if f.Pre == root {
-				rf = f
-			}
-		}
-	}
 	for _, f := range folders {
 		if f.Pre == pre {
 			ff = f
 		}
 	}
-	var fn string
-	if ff != nil {
-		fn = ff.Main()
-	} else {
-		ff = rf
-		fn = ff.getItem(pre)
-	}
-	ff.Image = &duit.Image{
-		Image: readImagePath(fn),
-	}
 	return
 }
 
-func (f Folder) Main() string {
+func (f Folder) Main() *Item {
 	fnSuf := f.Suf
 	if _, ok := f.Items[f.Suf]; !ok {
 		fnSuf = "0001"
 	}
-	return fmt.Sprintf("./data/about/%v%v/%v%v.dib", f.Pre, f.Suf, f.Pre, fnSuf)
+	return f.getItem(fnSuf)
 }
 
-func (f Folder) getItem(k string) string {
+func (f *Folder) Decode(k string) (fName, itName string) {
+	if _, ok := f.Items[k]; ok {
+		return f.Pre, k
+	}
+	var ff *Folder
+	fName = k
+	for _, fff := range folders {
+		if fff.Pre == k {
+			ff = fff
+		}
+	}
+	if ff == nil {
+		return f.Pre, k
+	}
+	itName = ff.Suf
+	if _, ok := ff.Items[itName]; !ok {
+		itName = "0001"
+	}
+	return
+}
+
+func (f Folder) getItem(k string) *Item {
+	for key, it := range f.Items {
+		if key == k {
+			if it.Image == nil {
+				fn := fmt.Sprintf("./data/about/%v%v/%v%v.dib", f.Pre, f.Suf, f.Pre, k)
+				it.Image = &duit.Image{
+					Image: readImagePath(fn),
+				}
+			}
+			return it
+		}
+	}
+	return nil
+}
+
+/*func (f Folder) getItem(k string) string {
 	fnSuf := k
 	return fmt.Sprintf("./data/about/%v%v/%v%v.dib", f.Pre, f.Suf, f.Pre, fnSuf)
+}*/
+
+type Item struct {
+	*duit.Image
+
+	f *Folder
+	name string
+	Targets map[string]Target
 }
 
-func (f *Folder) Mouse(dui *duit.DUI, self *duit.Kid, m draw.Mouse, origM draw.Mouse, orig image.Point) (r duit.Result) {
+func (it *Item) Mouse(dui *duit.DUI, self *duit.Kid, m draw.Mouse, origM draw.Mouse, orig image.Point) (r duit.Result) {
 	if m.Buttons&1 == 1 {
 		//log.Printf("m=%+v, origM=%+v", m, origM)
-		for _, t := range f.Targets {
+		for k, t := range it.Targets {
 			if t.has(m.Point) {
-				tf := getFolder(f.Pre, t.key)
-				dui.Top.UI = tf
+				fName, itName := it.f.Decode(k)
+				_=fName
+				tf := getFolder(it.f.Pre, k)
+				if tf == nil {
+					tf = it.f
+				}
+				it := tf.getItem(itName)
+				dui.Top.UI = it
 				dui.MarkLayout(dui.Top.UI)
 				dui.MarkDraw(dui.Top.UI)
 				dui.Render()
@@ -93,34 +121,64 @@
 		}
 	} else if m.Buttons&4 == 4 {
 		tf := getFolder("", "cont")
-		dui.Top.UI = tf
+		it := tf.Main()
+		dui.Top.UI = it
 		dui.MarkLayout(dui.Top.UI)
 		dui.MarkDraw(dui.Top.UI)
 		dui.Render()
 	}
-	return f.Image.Mouse(dui, self, m, origM, orig)
+	return it.Image.Mouse(dui, self, m, origM, orig)
 }
 
-type Target struct {
-	key string
-	xy draw.Point
+type Target interface {
+	has(draw.Point) bool
+}
+
+type Circle struct {
+	x int
+	y int
 	r int
 }
 
-func (t Target) has(p draw.Point) bool {
+func C(x, y, r int) Circle {
+	return Circle{x: x, y: y, r: r}
+}
+
+func (c Circle) has(p draw.Point) bool {
 	x := p.X * 2 / scale
 	y := p.Y * 2 / scale
-	return math.Abs(float64(x)-float64(t.xy.X)) < float64(t.r) && math.Abs(float64(y)-float64(t.xy.Y)) < float64(t.r)
+	return math.Abs(float64(x)-float64(c.x)) < float64(c.r) && math.Abs(float64(y)-float64(c.y)) < float64(c.r)
 }
 
+type Rectangle struct {
+	x int
+	y int
+	x2 int
+	y2 int
+}
+
+func R(x, y, x2, y2 int) Rectangle {
+	return Rectangle{x: x, y: y, x2: x2, y2: y2}
+}
+
+func (r Rectangle) has(p draw.Point) bool {
+	x := p.X * 2 / scale
+	y := p.Y * 2 / scale
+	return r.x <= x && x <= r.x2 && r.y <= y && y <= r.y2
+}
+
 var folders = []*Folder{
 	&Folder{
 		Pre: "afri",
 		Suf: "00aa",
 		Name: "Africa",
-		Items: map[string]string{
-			"00aa": "Waterhole",
-			"00fb": "Art",
+		Items: map[string]*Item{
+			"00aa": &Item{
+				name: "Waterhole",
+			},
+			"00fb": &Item{
+				name: "Art",
+			},
 		},
 	},
 	&Folder{
@@ -127,66 +185,42 @@
 		Pre: "atls",
 		Suf: "00nv",
 		Name: "Atlas",
-		Items: map[string]string{
-			"00nv": "Continents",
-			"01p1": "African reptiles",
-			"01p2": "African herbivores",
-			"01pu": "Africa",
-			"02pu": "Asia",
-			"03pu": "Australia",
-			"04pu": "Europe",
-			"05pu": "North America",
-			"06pu": "South America",
-		},
-		Targets: []Target{
-			Target{
-				key: "01pu",
-				xy: draw.Point{
-					X: 659,
-					Y: 461,
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Continents",
+				Targets: map[string]Target{
+					"01pu": C(659, 461, 100),
+					"02pu": C(931, 275, 200),
+					"03pu": C(1117, 603, 50),
+					"04pu": C(600, 262, 50),
+					"05pu": C(222, 270, 140),
+					"06pu": C(350, 552, 120),
 				},
-				r: 100,
 			},
-			Target{
-				key: "02pu",
-				xy: draw.Point{
-					X: 931,
-					Y: 275,
-				},
-				r: 200,
+			"01p1": &Item{
+				name: "African reptiles",
 			},
-			Target{
-				key: "03pu",
-				xy: draw.Point{
-					X: 1117,
-					Y: 603,
-				},
-				r: 50,
+			"01p2": &Item{
+				name: "African herbivores",
 			},
-			Target{
-				key: "04pu",
-				xy: draw.Point{
-					X: 600,
-					Y: 262,
-				},
-				r: 50,
+			"01pu": &Item{
+				name: "Africa",
 			},
-			Target{
-				key: "05pu",
-				xy: draw.Point{
-					X: 222,
-					Y: 270,
-				},
-				r: 140,
+			"02pu": &Item{
+				name: "Asia",
 			},
-			Target{
-				key: "06pu",
-				xy: draw.Point{
-					X: 350,
-					Y: 552,
-				},
-				r: 120,
+			"03pu": &Item{
+				name: "Australia",
 			},
+			"04pu": &Item{
+				name: "Europe",
+			},
+			"05pu": &Item{
+				name: "North America",
+			},
+			"06pu": &Item{
+				name: "South America",
+			},
 		},
 	},
 	&Folder{
@@ -193,8 +227,10 @@
 		Pre: "aust",
 		Suf: "00aa",
 		Name: "Australia",
-		Items: map[string]string{
-			"00aa": "Oddities",
+		Items: map[string]*Item{
+			"00aa": &Item{
+				name: "Oddities",
+			},
 		},
 	},
 	&Folder{
@@ -201,8 +237,10 @@
 		Pre: "beet",
 		Suf: "00aa",
 		Name: "Beetles",
-		Items: map[string]string{
-			"00aa": "Beetles",
+		Items: map[string]*Item{
+			"00aa": &Item{
+				name: "Beetles",
+			},
 		},
 	},
 	&Folder{
@@ -209,8 +247,10 @@
 		Pre: "frst",
 		Suf: "00aa",
 		Name: "Forest",
-		Items: map[string]string{
-			"00aa": "Forest Environments",
+		Items: map[string]*Item{
+			"00aa": &Item{
+				name: "Forest Environments",
+			},
 		},
 	},
 	&Folder{
@@ -217,42 +257,17 @@
 		Pre: "cont",
 		Suf: "00nv",
 		Name: "Contents",
-		Items: map[string]string{
-			"00nv": "Contents",
-		},
-		Targets: []Target{
-			Target{
-				key: "atls",
-				xy: draw.Point{
-					X: 242,
-					Y: 190,
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Contents",
+				Targets: map[string]Target{
+					"atls": C(242, 190, 150),
+					"habt": C(256, 588, 200),
+					"indx": C(920, 614, 200),
+					"weap": C(997, 200, 200),
+					"wwfd": C(604, 384, 200),
 				},
-				r: 150,
 			},
-			Target{
-				key: "habt",
-				xy: draw.Point{
-					X: 256,
-					Y: 588,
-				},
-				r: 200,
-			},
-			Target{
-				key: "indx",
-				xy: draw.Point{
-					X: 920,
-					Y: 614,
-				},
-				r: 200,
-			},
-			Target{
-				key: "weap",
-				xy: draw.Point{
-					X: 997,
-					Y: 200,
-				},
-				r: 200,
-			},
 		},
 	},
 	&Folder{
@@ -259,8 +274,10 @@
 		Pre: "guid",
 		Suf: "00nv",
 		Name: "Guides",
-		Items: map[string]string{
-			"00nv": "Guides",
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Guides",
+			},
 		},
 	},
 	&Folder{
@@ -267,8 +284,10 @@
 		Pre: "habt",
 		Suf: "00nv",
 		Name: "Habitats",
-		Items: map[string]string{
-			"00nv": "Habitats",
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Habitats",
+			},
 		},
 	},
 	&Folder{
@@ -275,8 +294,10 @@
 		Pre: "help",
 		Suf: "00nv",
 		Name: "Help",
-		Items: map[string]string{
-			"00nv": "Help",
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Help",
+			},
 		},
 	},
 	&Folder{
@@ -283,106 +304,33 @@
 		Pre: "indx",
 		Suf: "00nv",
 		Name: "Index",
-		Items: map[string]string{
-			"0001": "Index",
-		},
-		Targets: []Target{
-			Target{
-				key: "0001",
-				xy: draw.Point{
-					X: 48,
-					Y: 90,
+		Items: map[string]*Item{
+			"0001": &Item{
+				name: "Index",
+				Targets: map[string]Target{
+					"0001": R(25,66, 72,114),
+					"0002": R(75,70, 119,115),
+					"0003": R(122,68, 163,114),
+					"0004": R(166,69, 295,115),
+					"0005": R(297,66, 389,115),
+					"0006": R(394,69, 502,117),
+					"0007": R(504,66, 703,115),
+					"0008": R(702,66, 791,116),
+					"0009": R(794,67, 880,117),
+					"0010": R(879,68, 1018,114),
+					"0011": R(1013,66, 1210,116),
 				},
-				r: 25,
 			},
-			Target{
-				key: "0002",
-				xy: draw.Point{
-					X: 97,
-					Y: 90,
-				},
-				r: 25,
-			},
-			Target{
-				key: "0003",
-				xy: draw.Point{
-					X: 144,
-					Y: 90,
-				},
-				r: 25,
-			},
-			Target{
-				key: "0003",
-				xy: draw.Point{
-					X: 186,
-					Y: 90,
-				},
-				r: 25,
-			},
-			Target{
-				key: "0004",
-				xy: draw.Point{
-					X: 236,
-					Y: 90,
-				},
-				r: 25,
-			},
-			Target{
-				key: "0005",
-				xy: draw.Point{
-					X: 340,
-					Y: 90,
-				},
-				r: 40,
-			},
-			Target{
-				key: "0006",
-				xy: draw.Point{
-					X: 440,
-					Y: 90,
-				},
-				r: 40,
-			},
-			Target{
-				key: "0007",
-				xy: draw.Point{
-					X: 600,
-					Y: 90,
-				},
-				r: 90,
-			},
-			Target{
-				key: "0008",
-				xy: draw.Point{
-					X: 748,
-					Y: 90,
-				},
-				r: 40,
-			},
-			Target{
-				key: "0009",
-				xy: draw.Point{
-					X: 843,
-					Y: 90,
-				},
-				r: 38,
-			},
-			Target{
-				key: "0010",
-				xy: draw.Point{
-					X: 947,
-					Y: 90,
-				},
-				r: 60,
-			},
-			Target{
-				key: "0011",
-				xy: draw.Point{
-					X: 1112,
-					Y: 90,
-				},
-				r: 85,
-			},
+			"0002": &Item{},
+			"0003": &Item{},
+			"0004": &Item{},
+			"0005": &Item{},
+			"0006": &Item{},
+			"0007": &Item{},
+			"0008": &Item{},
+			"0009": &Item{},
+			"0010": &Item{},
+			"0011": &Item{},
 		},
 	},
 	/*Folder{
@@ -389,7 +337,7 @@
 		Pre: "titl",
 		Suf: "00nv",
 		Name: "Title",
-		Items: map[string]string{
+		Items: map[string]*Item{
 			"00nv": "Title",
 		},
 	},*/
@@ -397,38 +345,63 @@
 		Pre: "weap",
 		Suf: "00nv",
 		Name: "Index",
-		Items: map[string]string{
-			"00nv": "Index",
-		},
-		Targets: []Target{
-			Target{
-				key: "01pu",
-				xy: draw.Point{
-					X: 250,
-					Y: 400,
+		Items: map[string]*Item{
+			"00nv": &Item{
+				name: "Index",
+				Targets: map[string]Target{
+					"01pu": C(250, 400, 150),
+					"03pu": C(654, 375, 150),
+					"02pu": C(1047, 400, 150),
 				},
-				r: 150,
 			},
-			Target{
-				key: "03pu",
-				xy: draw.Point{
-					X: 654,
-					Y: 375,
+			"01pu": &Item{},
+			"03pu": &Item{},
+			"02pu": &Item{},
+		},
+	},
+	&Folder{
+		Pre: "wwfd",
+		Suf: "00aa",
+		Name: "WWF",
+		Items: map[string]*Item{
+			"00aa": &Item{
+				name: "WWF",
+				Targets: map[string]Target{
+					"03pu": C(302, 176, 80),
+					"04pu": C(518, 255, 80),
+					"02pu": C(685, 415, 80),
+					"06pu": C(952, 147, 80),
+					"05pu": C(917, 324, 80),
+					"01pu": C(1102, 313, 80),
+					"07tv": C(202, 465, 80),
+					"00fb": C(89, 55, 80),
+					"01fb": C(1208, 69, 80),
 				},
-				r: 150,
 			},
-			Target{
-				key: "02pu",
-				xy: draw.Point{
-					X: 1047,
-					Y: 400,
-				},
-				r: 150,
-			},
+			"03pu": &Item{},
+			"04pu": &Item{},
+			"02pu": &Item{},
+			"06pu": &Item{},
+			"05pu": &Item{},
+			"01pu": &Item{},
+			"07tv": &Item{},
+			"00fb": &Item{},
+			"01fb": &Item{},
 		},
 	},
 }
 
+func init() {
+	for _, f := range folders {
+		for _, it := range f.Items {
+			it.f = f
+			if f.Pre == "indx" {
+				it.Targets = f.Items["0001"].Targets
+			}
+		}
+	}
+}
+
 func check(err error, msg string) {
 	if err != nil {
 		log.Fatalf("%s: %s\n", msg, err)
@@ -437,8 +410,8 @@
 
 func resize(img image.Image, scale int) image.Image {
 	bounds := img.Bounds()
-	newX := bounds.Dx() * 2
-	newY:= bounds.Dy() * 2
+	newX := bounds.Dx() * scale
+	newY:= bounds.Dy() * scale
 	dst := image.NewRGBA(image.Rect(0, 0, newX, newY))
 	xdraw.NearestNeighbor.Scale(dst, dst.Rect, img, img.Bounds(), xdraw.Over, nil)
 	return dst
@@ -488,11 +461,9 @@
 		}
 	}
 
-	_=readImagePath
-
 	f := getFolder("", "cont")
 
-	dui.Top.UI = f
+	dui.Top.UI = f.Main()
 	dui.Render()
 
 	for {