All pastes #969936 Raw Edit

Miles

public text v1 · immutable
·published 2008-04-03 19:53 UTC
rendered paste body
alias test {
  if (!$window(@sb)) {
    ;Window isn't open, create it
    window -ak0hdBpfo +d @sb -1 -1 220 50
  }
  else {
    ;Clear it
    clear @sb
  }
  ;Scroll bar coordinates
  set %sb.x 10 
  set %sb.y 10
  set %sb.w 200
  set %sb.h 30
  ;Scroll bar handle coordinates
  set %sb.h.x 10
  set %sb.h.y 10
  set %sb.h.w 30
  set %sb.h.h 30
  ;Draw it
  draw.scroll.bar
} 

alias draw.scroll.bar {
  ;Draw some rectangles
  drawrect -rfn @sb $rgb(100,100,100) 1 %sb.x %sb.y %sb.w %sb.h
  drawrect -rfn @sb $rgb(50,50,50) 1 %sb.h.x %sb.h.y %sb.h.w %sb.h.h
  drawdot @sb
}

menu @sb {
  sclick {
    if ($inrect($mouse.x,$mouse.y,%sb.x,%sb.y,%sb.w,%sb.h)) { 
      ;Mouse is in scrollbar coords.     
      if ($inrect($mouse.x,$mouse.y,%sb.h.x,%sb.h.y,%sb.h.w,%sb.h.h)) { 
        ;Mouse is in scrollbar handle coords.
        var %x,%scrollbar.percent,%scrollbar.handle.percent
        while ($mouse.key & 1) && ($whilefix) {
          %x = $calc($mouse.x - %sb.x) 
          %scrollbar.percent = $int($calc((%x / %sb.w) * 100))
          %x = $calc($mouse.x - %sb.h.x) 
          %scrollbar.handle.percent = $int($calc((%x / %sb.h.w) * 100))
          if (%scrollbar.percent isnum 0-100) && (%scrollbar.handle.percent isnum 0-100) {
            ;No errors.
            scroll.bar.position %scrollbar.handle.percent %scrollbar.percent
            draw.scroll.bar
          }
        }
      }
      else { 
        ;We need to step in the direction of $mouse.y, worry about this later.
      }
    }
  }
}

alias scroll.bar.position {
  var %pos = $calc(($2 / 100) * %sb.w)
  if (%pos isnum $+(%sb.x, -, $calc(%sb.x + %sb.w - %sb.h.w))) {
    ;It's a valid position.
    %sb.h.x = %pos
  }
}