Explorar o código

scroller button size can now be proportional to content/view (default)

danaugrs %!s(int64=7) %!d(string=hai) anos
pai
achega
0325130e71
Modificáronse 2 ficheiros con 55 adicións e 10 borrados
  1. 24 10
      gui/scrollbar.go
  2. 31 0
      gui/scroller.go

+ 24 - 10
gui/scrollbar.go

@@ -30,11 +30,12 @@ type ScrollBar struct {
 }
 
 type scrollBarButton struct {
-	Panel              // Embedded panel
-	sb      *ScrollBar // pointer to parent scroll bar
-	pressed bool       // mouse button pressed flag
-	mouseX  float32    // last mouse click x position
-	mouseY  float32    // last mouse click y position
+	Panel               // Embedded panel
+	sb       *ScrollBar // pointer to parent scroll bar
+	pressed  bool       // mouse button pressed flag
+	mouseX   float32    // last mouse click x position
+	mouseY   float32    // last mouse click y position
+	Size     float32    // button size
 }
 
 type ScrollBarStyle struct {
@@ -49,7 +50,7 @@ type ScrollBarButtonStyle struct {
 	Borders      BorderSizes
 	BordersColor math32.Color4
 	Color        math32.Color
-	Size         float32
+	Size         float32 	   // This is the default/minimum button size
 }
 
 // NewVScrollBar creates and returns a pointer to a new vertical scroll bar
@@ -89,6 +90,7 @@ func (sb *ScrollBar) initialize(width, height float32, vertical bool) {
 	sb.button.Panel.Subscribe(OnMouseUp, sb.button.onMouse)
 	sb.button.Panel.Subscribe(OnCursor, sb.button.onCursor)
 	sb.button.SetMargins(1, 1, 1, 1)
+	sb.button.Size = sb.style.Button.Size
 	sb.button.sb = sb
 	sb.Add(&sb.button)
 
@@ -96,6 +98,18 @@ func (sb *ScrollBar) initialize(width, height float32, vertical bool) {
 	sb.recalc()
 }
 
+// SetButtonSize sets the button size
+func (sb *ScrollBar) SetButtonSize(size float32) {
+
+	// Clamp to minimum size if requested size smaller than minimum
+	if size > sb.style.Button.Size {
+		sb.button.Size = size
+	} else {
+		sb.button.Size = sb.style.Button.Size
+	}
+	sb.recalc()
+}
+
 // Value returns the current position of the button in the scrollbar
 // The returned value is between 0.0 and 1.0
 func (sb *ScrollBar) Value() float64 {
@@ -145,9 +159,9 @@ func (sb *ScrollBar) onMouse(evname string, ev interface{}) {
 func (sb *ScrollBar) recalc() {
 
 	if sb.vertical {
-		sb.button.SetSize(sb.content.Width, sb.style.Button.Size)
+		sb.button.SetSize(sb.content.Width, sb.button.Size)
 	} else {
-		sb.button.SetSize(sb.style.Button.Size, sb.content.Height)
+		sb.button.SetSize(sb.button.Size, sb.content.Height)
 	}
 }
 
@@ -196,11 +210,11 @@ func (button *scrollBarButton) onCursor(evname string, ev interface{}) {
 	if button.sb.vertical {
 		dy := button.mouseY - e.Ypos
 		py := button.Position().Y
-		button.SetPositionY(math32.Clamp(py-dy, 0, button.sb.content.Height-button.sb.style.Button.Size))
+		button.SetPositionY(math32.Clamp(py-dy, 0, button.sb.content.Height-button.Size))
 	} else {
 		dx := button.mouseX - e.Xpos
 		px := button.Position().X
-		button.SetPositionX(math32.Clamp(px-dx, 0, button.sb.content.Width-button.sb.style.Button.Size))
+		button.SetPositionX(math32.Clamp(px-dx, 0, button.sb.content.Width-button.Size))
 	}
 	button.mouseX = e.Xpos
 	button.mouseY = e.Ypos

+ 31 - 0
gui/scroller.go

@@ -23,6 +23,7 @@ type Scroller struct {
 	adjustItem     bool            // adjust item to width or height
 	focus          bool            // has keyboard focus
 	cursorOver     bool            // mouse is over the list
+	autoButtonSize bool            // scroll button size is adjusted relative to content/view
 	scrollBarEvent bool
 }
 
@@ -279,10 +280,16 @@ func (s *Scroller) SetAutoHeight(maxHeight float32) {
 	s.maxAutoHeight = maxHeight
 }
 
+func (s *Scroller) SetAutoButtonSize(autoButtonSize bool) {
+
+	s.autoButtonSize = autoButtonSize
+}
+
 // initialize initializes this scroller and is normally used by other types which contains a scroller
 func (s *Scroller) initialize(vert bool, width, height float32) {
 
 	s.vert = vert
+	s.autoButtonSize = true
 	s.Panel.Initialize(width, height)
 	s.styles = &StyleDefault().Scroller
 
@@ -389,6 +396,18 @@ func (s *Scroller) vRecalc() {
 		}
 	}
 	s.setVScrollBar(scroll)
+
+	// Compute size of scroll button
+	if scroll && s.autoButtonSize {
+		var totalHeight float32 = 0
+		for _, item := range s.items {
+			// TODO OPTIMIZATION
+			// Break when the view/content proportion becomes smaller than the minimum button size
+			totalHeight += item.TotalHeight()
+		}
+		s.vscroll.SetButtonSize(s.height * s.height/totalHeight)
+	}
+
 	// Items width
 	width := s.ContentWidth()
 	if scroll {
@@ -442,6 +461,18 @@ func (s *Scroller) hRecalc() {
 		}
 	}
 	s.setHScrollBar(scroll)
+
+	// Compute size of scroll button
+	if scroll && s.autoButtonSize {
+		var totalWidth float32 = 0
+		for _, item := range s.items {
+			// TODO OPTIMIZATION
+			// Break when the view/content proportion becomes smaller than the minimum button size
+			totalWidth += item.GetPanel().Width()
+		}
+		s.hscroll.SetButtonSize(s.width * s.width/totalWidth)
+	}
+
 	// Items height
 	height := s.ContentHeight()
 	if scroll {