Przeglądaj źródła

Added ability to click menu options.

Panel helpers: HasBorder(), Length(), Within().
Steve Thielemann 2 lat temu
rodzic
commit
c54742098b
2 zmienionych plików z 79 dodań i 25 usunięć
  1. 19 2
      door/menu.go
  2. 60 23
      door/panel.go

+ 19 - 2
door/menu.go

@@ -172,8 +172,25 @@ func (m *Menu) Choose(d *Door) int {
 				}
 				// Look at Mouse X/Y to determine where they clicked
 				if mouse.Button == 1 || mouse.Button == 2 {
-					// TODO:
-					log.Println("Mouse (", mouse.X, ",", mouse.Y, ")")
+					// TODO: log.Println("Mouse (", mouse.X, ",", mouse.Y, ")")
+					clicked, cx, cy := m.Panel.Within(int(mouse.X), int(mouse.Y))
+					log.Printf("%t : (%d, %d)\n", clicked, cx, cy)
+
+					if clicked {
+						if m.Panel.HasBorder() {
+							// Did they click the border?
+							if cy > 0 && cy <= m.Panel.Length() {
+								updated = true
+								m.Chosen = cy - 1
+								update_and_exit = true
+							}
+						} else {
+							// Borderless -- yes, they clicked something!
+							updated = true
+							m.Chosen = cy
+							update_and_exit = true
+						}
+					}
 				}
 			}
 		}

+ 60 - 23
door/panel.go

@@ -27,6 +27,7 @@ type Panel struct {
 	TitleOffset int
 }
 
+/*
 func (p *Panel) Range(withBorder bool) (sx, sy, ex, ey int) {
 	sx = p.X
 	sy = p.Y
@@ -43,6 +44,65 @@ func (p *Panel) Range(withBorder bool) (sx, sy, ex, ey int) {
 	}
 	return
 }
+*/
+
+func (p *Panel) Length() int {
+	return len(p.Lines)
+}
+
+func (p *Panel) RightBottomPos() (rx, by int) {
+	rx = p.X + p.Width
+	by = p.Y + len(p.Lines)
+	if p.HasBorder() {
+		rx += 2
+		by += 2
+	}
+	return
+}
+
+func (p *Panel) HasBorder() bool {
+	return int(p.Style) > 0
+}
+
+/*
+Is X,Y within the Panel?
+*/
+func (p *Panel) Within(x, y int) (bool, int, int) {
+	if x < p.X || y < p.Y {
+		return false, 0, 0
+	}
+	mx, my := p.RightBottomPos()
+	if x > mx || y > my {
+		return false, 0, 0
+	}
+	// Ok, it must be within:
+	return true, x - p.X, y - p.Y
+}
+
+/*
+// Panel Clicked?
+// If onBorder == true, returns true, 0 for border click.
+// Otherwise true, line clicked on (starting with 1)/
+func (p *Panel) Clicked(m Mouse, onBorder bool) (bool, int) {
+	var sx, sy, ex, ey = p.Range(onBorder)
+	var mx = int(m.X)
+	var my = int(m.Y)
+
+	if (mx >= sx) && (mx <= ex) {
+		if (my >= sy) && (my <= ey) {
+			if onBorder {
+				if my == sy || my == ey {
+					return true, 0
+				}
+				return true, my - sy
+			} else {
+				return true, (my - sy) + 1
+			}
+		}
+	}
+	return false, 0
+}
+*/
 
 // Clear out the panel
 func (p *Panel) Clear() string {
@@ -225,26 +285,3 @@ func (p *Panel) CenterX() {
 func (p *Panel) CenterY() {
 	p.Y = (Height - len(p.Lines)) / 2
 }
-
-// Panel Clicked?
-// If onBorder == true, returns true, 0 for border click.
-// Otherwise true, line clicked on (starting with 1)/
-func (p *Panel) Clicked(m Mouse, onBorder bool) (bool, int) {
-	var sx, sy, ex, ey = p.Range(onBorder)
-	var mx = int(m.X)
-	var my = int(m.Y)
-
-	if (mx >= sx) && (mx <= ex) {
-		if (my >= sy) && (my <= ey) {
-			if onBorder {
-				if my == sy || my == ey {
-					return true, 0
-				}
-				return true, my - sy
-			} else {
-				return true, (my - sy) + 1
-			}
-		}
-	}
-	return false, 0
-}