Files
kryon/runtime/scroll.kry
T

529 lines
18 KiB
Plaintext

#module "scroll"
#import "control_props"
#import "drawing_props"
#import "style"
ScrollMetrics :: struct {
scrollbar_width: i32
reserved_width: i32
safe_gap: i32
default_wheel_step: i32
drag_threshold: i32
visual_bleed: i32
thumb_min_height: i32
thumb_inset: i32
}
ScrollPolicyView :: struct {
content_x: i32
content_y: i32
content_w: i32
viewport_h: i32
content_h: i32
max_scroll: i32
scrollbar_x: i32
}
ScrollBarPaint :: struct {
track_bounds: Rectangle
thumb_bounds: Rectangle
track_span: i32
scroll_per_pixel: float
}
ScrollBarDragDecision :: struct {
start_drag: bool
continue_drag: bool
cancel_drag: bool
claim_scroll_owner: bool
scroll_offset: i32
}
ScrollBarDragReleaseDecision :: struct {
clear_drag: bool
consume_release: bool
}
ScrollContentDragDecision :: struct {
start_drag: bool
active: bool
dragging: bool
gesture_pending: bool
claim_scroll_owner: bool
capture_input: bool
scroll_offset: i32
}
ScrollClipGeometry :: struct {
screen_bounds: Rectangle
clipped_world_bounds: Rectangle
visual_screen_bounds: Rectangle
visual_bleed: i32
}
ScrollMetric :: (fields: u32, field: u32, value: float, fallback: float,
scale: float) -> i32 {
if scale <= 0.0 { scale = 1.0 }
if (fields & field) != (u32)0 && value >= 0.0 {
return (i32)(value * scale)
}
return (i32)(fallback * scale)
}
ScrollMetricsFor :: (scale: float, track: StyleFrame, thumb: StyleFrame) -> ScrollMetrics #export {
if scale <= 0.0 { scale = 1.0 }
metrics: ScrollMetrics
metrics.scrollbar_width = ScrollMetric(track.value.fields,
(u32)StyleIconSize, track.value.icon_size, 10.0, scale)
metrics.reserved_width = ScrollMetric(track.value.fields,
(u32)StylePaddingX, track.value.padding_x, 16.0, scale)
metrics.safe_gap = ScrollMetric(track.value.fields,
(u32)StyleGap, track.value.gap, 20.0, scale)
metrics.default_wheel_step = ScrollMetric(track.value.fields,
(u32)StyleContentOffset, track.value.offset_y, 42.0, scale)
metrics.drag_threshold = ScrollMetric(track.value.fields,
(u32)StyleContentOffset, track.value.offset_x, 5.0, scale)
metrics.visual_bleed = ScrollMetric(track.value.fields,
(u32)StylePaddingY, track.value.padding_y, 8.0, scale)
metrics.thumb_min_height = ScrollMetric(thumb.value.fields,
(u32)StyleIconSize, thumb.value.icon_size, 16.0, scale)
metrics.thumb_inset = ScrollMetric(thumb.value.fields,
(u32)StylePaddingX, thumb.value.padding_x, 2.0, scale)
return metrics
}
ScrollClamp :: (value: i32, maximum: i32) -> i32 #export {
if value < 0 { return 0 }
if value > maximum { return maximum }
return value
}
ScrollMax :: (content_height: i32, viewport_height: i32) -> i32 #export {
maximum: i32 = content_height - viewport_height
if maximum < 0 { maximum = 0 }
return maximum
}
ScrollReservedWidth :: (max_scroll: i32, metrics: ScrollMetrics) -> i32 #export {
if max_scroll > 0 { return metrics.reserved_width }
return 0
}
ScrollContentWidth :: (content_width: i32, max_scroll: i32,
metrics: ScrollMetrics) -> i32 #export {
reserved: i32 = ScrollReservedWidth(max_scroll, metrics)
if reserved <= 0 { return content_width }
if content_width <= reserved { return 0 }
return content_width - reserved
}
ScrollPageContentWidthFor :: (view_width: i32, max_content_width: i32,
min_content_width: i32, side_padding: i32) -> i32 #export {
if max_content_width <= 0 { max_content_width = view_width }
available: i32 = view_width - side_padding * 2
if max_content_width > available { max_content_width = available }
if min_content_width > 0 && max_content_width < min_content_width {
max_content_width = min_content_width
}
if max_content_width < 0 { max_content_width = 0 }
return max_content_width
}
ScrollSafeContentWidth :: (content_x: i32, content_width: i32,
scrollbar_x: i32, max_scroll: i32, metrics: ScrollMetrics) -> i32 #export {
safe_width: i32 = content_width
if max_scroll <= 0 || scrollbar_x <= 0 { return content_width }
safe_width = scrollbar_x - content_x - metrics.safe_gap
if safe_width > content_width { return content_width }
if safe_width < 0 { return 0 }
return safe_width
}
ScrollScopeContentBounds :: (bounds: Rectangle, has_scroll: bool,
metrics: ScrollMetrics) -> Rectangle #export {
content: Rectangle = bounds
if has_scroll {
content.width = bounds.width - (float)metrics.scrollbar_width
if content.width < 0.0 { content.width = 0.0 }
}
return content
}
ScrollScreenBoundsFor :: (bounds: Rectangle, camera_offset: Vector2,
zoom: float) -> Rectangle #export {
if zoom <= 0.0 { zoom = 1.0 }
screen: Rectangle
screen.x = camera_offset.x + bounds.x * zoom
screen.y = camera_offset.y + bounds.y * zoom
screen.width = bounds.width * zoom
screen.height = bounds.height * zoom
return screen
}
ScrollWorldBoundsFor :: (screen_bounds: Rectangle, camera_offset: Vector2,
zoom: float) -> Rectangle #export {
if zoom <= 0.0 { zoom = 1.0 }
world: Rectangle
world.x = (screen_bounds.x - camera_offset.x) / zoom
world.y = (screen_bounds.y - camera_offset.y) / zoom
world.width = screen_bounds.width / zoom
world.height = screen_bounds.height / zoom
return world
}
ScrollVisualBoundsFor :: (screen_bounds: Rectangle, visual_bleed: i32,
zoom: float, screen_width: i32, screen_height: i32, view_width: i32,
view_height: i32, camera_offset: Vector2) -> Rectangle #export {
if zoom <= 0.0 { zoom = 1.0 }
bleed: i32 = (i32)((float)visual_bleed * zoom)
if bleed < 1 { bleed = 1 }
visual: Rectangle
visual.x = screen_bounds.x - (float)bleed
visual.y = screen_bounds.y - (float)bleed
visual.width = screen_bounds.width + (float)(bleed * 2)
visual.height = screen_bounds.height + (float)(bleed * 2)
if visual.x < 0.0 {
visual.width += visual.x
visual.x = 0.0
}
if visual.y < 0.0 {
visual.height += visual.y
visual.y = 0.0
}
if screen_width <= 0 {
screen_width = (i32)(camera_offset.x + (float)view_width * zoom)
}
if screen_height <= 0 {
screen_height = (i32)(camera_offset.y + (float)view_height * zoom)
}
if visual.x + visual.width > (float)screen_width {
visual.width = (float)screen_width - visual.x
}
if visual.y + visual.height > (float)screen_height {
visual.height = (float)screen_height - visual.y
}
if visual.width < 0.0 { visual.width = 0.0 }
if visual.height < 0.0 { visual.height = 0.0 }
return visual
}
ScrollClipGeometryFor :: (bounds: Rectangle, clipped_screen_bounds: Rectangle,
visual_bleed: i32, camera_offset: Vector2, zoom: float, screen_width: i32,
screen_height: i32, view_width: i32, view_height: i32) -> ScrollClipGeometry #export {
if zoom <= 0.0 { zoom = 1.0 }
geometry: ScrollClipGeometry
geometry.screen_bounds = ScrollScreenBoundsFor(bounds, camera_offset, zoom)
geometry.clipped_world_bounds = ScrollWorldBoundsFor(clipped_screen_bounds,
camera_offset, zoom)
geometry.visual_screen_bounds = ScrollVisualBoundsFor(geometry.screen_bounds,
visual_bleed, zoom, screen_width, screen_height, view_width,
view_height, camera_offset)
geometry.visual_bleed = (i32)((float)visual_bleed * zoom)
if geometry.visual_bleed < 1 { geometry.visual_bleed = 1 }
return geometry
}
ScrollWheelOffsetFor :: (scroll_offset: i32, wheel_move: float,
max_scroll: i32, wheel_step: i32) -> i32 #export {
delta: i32 = (i32)(wheel_move * (float)wheel_step)
return ScrollClamp(scroll_offset - delta, max_scroll)
}
ScrollRowWheelStepFor :: (row_height: i32, scale: float) -> i32 #export {
if row_height > 0 { return row_height * 3 }
if scale <= 0.0 { scale = 1.0 }
return (i32)(90.0 * scale)
}
ScrollDragOffsetFor :: (mouse_y: float, track_y: float, grab_y: float,
max_scroll: i32, paint: ScrollBarPaint) -> i32 #export {
if paint.scroll_per_pixel <= 0.0 {
return ScrollClamp(0, max_scroll)
}
offset: i32 = (i32)((mouse_y - track_y - grab_y) * paint.scroll_per_pixel)
return ScrollClamp(offset, max_scroll)
}
ScrollDragDeltaOffsetFor :: (start_scroll: i32, delta_y: i32,
max_scroll: i32) -> i32 #export {
return ScrollClamp(start_scroll - delta_y, max_scroll)
}
ScrollThumbDragDeltaOffsetFor :: (start_scroll: i32, delta_y: i32,
max_scroll: i32, paint: ScrollBarPaint) -> i32 #export {
offset: i32 = start_scroll
if paint.scroll_per_pixel > 0.0 {
offset += (i32)((float)delta_y * paint.scroll_per_pixel)
}
return ScrollClamp(offset, max_scroll)
}
ScrollBarDragFor :: (mouse_down: bool, input_captured: bool,
thumb_active: bool, pointer_owner_none: bool, drag_active: bool,
owns_drag: bool, current_scroll: i32, start_scroll: i32,
delta_y: i32, max_scroll: i32,
paint: ScrollBarPaint) -> ScrollBarDragDecision #export {
decision: ScrollBarDragDecision
decision.scroll_offset = ScrollClamp(current_scroll, max_scroll)
if mouse_down && (!input_captured || owns_drag) {
if !drag_active {
if thumb_active && pointer_owner_none {
decision.start_drag = true
decision.claim_scroll_owner = true
}
return decision
}
if owns_drag {
decision.continue_drag = true
decision.claim_scroll_owner = true
decision.scroll_offset = ScrollThumbDragDeltaOffsetFor(
start_scroll, delta_y, max_scroll, paint)
}
return decision
}
if owns_drag {
decision.cancel_drag = true
}
return decision
}
ScrollBarDragReleaseFor :: (drag_active: bool, mouse_released: bool,
content_disabled: bool) -> ScrollBarDragReleaseDecision #export {
decision: ScrollBarDragReleaseDecision
decision.clear_drag = drag_active && (mouse_released || content_disabled)
decision.consume_release = drag_active && mouse_released
return decision
}
ScrollContentDragFor :: (max_scroll: i32, mouse_pressed: bool,
mouse_down: bool, inside: bool, input_captured: bool,
on_scrollbar: bool, pointer_owner_none: bool, pointer_owner_scroll: bool,
blocked_by_other_drag: bool, active: bool, dragging: bool,
current_scroll: i32, start_scroll: i32, delta_y: i32,
drag_threshold: i32) -> ScrollContentDragDecision #export {
decision: ScrollContentDragDecision
decision.scroll_offset = ScrollClamp(current_scroll, max_scroll)
if blocked_by_other_drag {
return decision
}
if max_scroll > 0 && pointer_owner_none && mouse_pressed && inside &&
!input_captured && !on_scrollbar {
decision.start_drag = true
decision.active = true
decision.gesture_pending = true
return decision
}
if active && mouse_down && (pointer_owner_none || pointer_owner_scroll) {
decision.active = true
if dragging || delta_y > drag_threshold || delta_y < -drag_threshold {
decision.dragging = true
decision.claim_scroll_owner = true
decision.capture_input = true
decision.scroll_offset = ScrollDragDeltaOffsetFor(
start_scroll, delta_y, max_scroll)
}
return decision
}
if active && dragging {
decision.capture_input = true
}
return decision
}
ScrollRectVisibleOffsetFor :: (scroll_offset: i32, viewport_y: i32,
viewport_height: i32, rect_y: i32, rect_height: i32, margin: i32,
max_scroll: i32) -> i32 #export {
if margin < 0 { margin = 0 }
next_scroll: i32 = ScrollClamp(scroll_offset, max_scroll)
viewport_top: i32 = viewport_y + margin
viewport_bottom: i32 = viewport_y + viewport_height - margin
rect_top: i32 = rect_y
rect_bottom: i32 = rect_y + rect_height
if viewport_bottom < viewport_top { viewport_bottom = viewport_top }
if rect_bottom > viewport_bottom {
next_scroll += rect_bottom - viewport_bottom
}
if rect_top < viewport_top {
next_scroll -= viewport_top - rect_top
}
return ScrollClamp(next_scroll, max_scroll)
}
ScrollMeasure :: (bounds: Rectangle, content_height: i32, content_x: i32,
content_width: i32, scroll_offset: i32, scrollbar_x: i32,
metrics: ScrollMetrics) -> ScrollPolicyView #export {
view: ScrollPolicyView
x: i32 = (i32)bounds.x
y: i32 = (i32)bounds.y
w: i32 = (i32)bounds.width
h: i32 = (i32)bounds.height
if content_height < 0 { content_height = 0 }
if content_x <= 0 { content_x = x }
if content_width <= 0 { content_width = w }
if scrollbar_x <= 0 { scrollbar_x = x + w - metrics.scrollbar_width }
view.content_x = content_x
view.viewport_h = h
view.content_h = content_height
view.max_scroll = ScrollMax(content_height, h)
scroll_offset = ScrollClamp(scroll_offset, view.max_scroll)
view.content_y = y - scroll_offset
view.scrollbar_x = scrollbar_x
view.content_w = ScrollSafeContentWidth(content_x, content_width,
scrollbar_x, view.max_scroll, metrics)
return view
}
ScrollBarPaintFor :: (x: i32, y: i32, viewport_h: i32, content_h: i32,
scroll_offset: i32, max_scroll: i32, metrics: ScrollMetrics) -> ScrollBarPaint #export {
paint: ScrollBarPaint
scrollbar_w: i32 = metrics.scrollbar_width
if scrollbar_w < 0 { scrollbar_w = 0 }
if viewport_h < 0 { viewport_h = 0 }
if content_h < 0 { content_h = 0 }
thumb_h: i32 = viewport_h
if content_h > 0 {
thumb_h = (i32)((float)viewport_h * ((float)viewport_h / (float)content_h))
}
if thumb_h < metrics.thumb_min_height { thumb_h = metrics.thumb_min_height }
max_thumb_h: i32 = (i32)((float)viewport_h * 0.28)
if max_thumb_h < metrics.thumb_min_height { max_thumb_h = metrics.thumb_min_height }
if thumb_h > max_thumb_h { thumb_h = max_thumb_h }
if thumb_h > viewport_h { thumb_h = viewport_h }
if thumb_h < 0 { thumb_h = 0 }
clamped_scroll: i32 = ScrollClamp(scroll_offset, max_scroll)
scroll_ratio: float = max_scroll > 0 ? (float)clamped_scroll / (float)max_scroll : 0.0
if scroll_ratio < 0.0 { scroll_ratio = 0.0 }
if scroll_ratio > 1.0 { scroll_ratio = 1.0 }
track_span: i32 = viewport_h - thumb_h
if track_span < 0 { track_span = 0 }
thumb_y: i32 = y + (i32)(scroll_ratio * (float)track_span)
if thumb_y < y { thumb_y = y }
if thumb_y + thumb_h > y + viewport_h {
thumb_y = y + viewport_h - thumb_h
}
inset: i32 = metrics.thumb_inset
if inset < 0 { inset = 0 }
thumb_w: i32 = scrollbar_w - inset * 2
if thumb_w < 0 { thumb_w = 0 }
paint.track_bounds.x = (float)x
paint.track_bounds.y = (float)y
paint.track_bounds.width = (float)scrollbar_w
paint.track_bounds.height = (float)viewport_h
paint.thumb_bounds.x = (float)(x + inset)
paint.thumb_bounds.y = (float)thumb_y
paint.thumb_bounds.width = (float)thumb_w
paint.thumb_bounds.height = (float)thumb_h
paint.track_span = track_span
paint.scroll_per_pixel = track_span > 0 ? (float)max_scroll / (float)track_span : 0.0
return paint
}
ScrollScopeInput :: struct {
bounds: Rectangle
content_height: i32
has_offset: bool
offset: i32
pointer_allowed: bool
disabled: bool
mouse: Vector2
pressed: bool
down: bool
released: bool
wheel: float
owns_drag: bool
owner_captured: bool
grab: float
}
ScrollScopeFrame :: struct {
clip: Rectangle
content: Rectangle
paint: ScrollBarPaint
offset: i32
grab: float
scrollbar: bool
start_drag: bool
clear_drag: bool
consume_release: bool
consume_wheel: bool
thumb_state: i32
}
ScrollPointInside :: (point: Vector2, bounds: Rectangle) -> bool {
return point.x >= bounds.x && point.y >= bounds.y &&
point.x < bounds.x + bounds.width && point.y < bounds.y + bounds.height
}
ScrollScopeFrameFor :: (input: ScrollScopeInput, metrics: ScrollMetrics) -> ScrollScopeFrame #export {
frame: ScrollScopeFrame
frame.clip = input.bounds
frame.grab = input.grab
frame.thumb_state = (i32)ButtonStateNormal
maximum: i32 = ScrollMax(input.content_height, (i32)input.bounds.height)
active: bool = input.owns_drag
if input.has_offset {
frame.offset = ScrollClamp(input.offset, maximum)
if input.pointer_allowed && !input.disabled && input.wheel != 0.0 {
frame.offset = ScrollWheelOffsetFor(frame.offset, input.wheel,
maximum, metrics.default_wheel_step)
frame.consume_wheel = true
}
frame.scrollbar = maximum > 0 &&
input.bounds.width > (float)metrics.scrollbar_width && input.bounds.height > 0.0
}
if !frame.scrollbar || input.disabled || input.owner_captured {
frame.clear_drag = active
frame.consume_release = active && input.released
active = false
}
if frame.scrollbar {
x: i32 = (i32)(input.bounds.x + input.bounds.width) - metrics.scrollbar_width
frame.paint = ScrollBarPaintFor(x, (i32)input.bounds.y, (i32)input.bounds.height,
input.content_height, frame.offset, maximum, metrics)
if !input.disabled && !input.owner_captured && input.pointer_allowed &&
input.pressed && ScrollPointInside(input.mouse, frame.paint.track_bounds) {
frame.start_drag = true
frame.clear_drag = false
active = true
frame.grab = frame.paint.thumb_bounds.height / 2.0
if ScrollPointInside(input.mouse, frame.paint.thumb_bounds) {
frame.grab = input.mouse.y - frame.paint.thumb_bounds.y
}
}
if active {
if frame.paint.track_span > 0 && (input.down || input.pressed) {
frame.offset = ScrollDragOffsetFor(input.mouse.y,
frame.paint.track_bounds.y, frame.grab, maximum, frame.paint)
}
if input.released {
frame.consume_release = true
frame.clear_drag = true
active = false
}
}
frame.paint = ScrollBarPaintFor(x, (i32)input.bounds.y, (i32)input.bounds.height,
input.content_height, frame.offset, maximum, metrics)
if active {
frame.thumb_state = (i32)ButtonStatePressed
} else if !input.disabled && input.pointer_allowed &&
ScrollPointInside(input.mouse, frame.paint.thumb_bounds) {
frame.thumb_state = (i32)ButtonStateHover
}
frame.clip = ScrollScopeContentBounds(input.bounds, true, metrics)
}
frame.content = frame.clip
frame.content.y -= (float)frame.offset
frame.content.height = (float)input.content_height
if frame.content.height < 0.0 { frame.content.height = 0.0 }
return frame
}