Files
kryon/runtime/input.kry
T

232 lines
7.0 KiB
Plaintext

#module "input"
#import "input_props"
InputContinuousStep :: struct {
value: float
changed: bool
}
InputDiscreteStep :: struct {
value: i32
changed: bool
}
InputStep :: struct {
value: f64
changed: bool
}
InputPointerInteraction :: struct {
active: bool
hovered: bool
disabled_marker: bool
activated: bool
consume_release: bool
}
InputCellLayout :: struct {
cell: Rectangle
field: Rectangle
minus: Rectangle
plus: Rectangle
has_step_buttons: bool
}
InputScaledMetric :: (value: float, scale: float) -> i32 {
if scale <= 0.0 { scale = 1.0 }
metric: i32 = (i32)(value * scale)
if metric < 0 { return 0 }
return metric
}
InputDefaultStepButtonWidth :: (scale: float) -> i32 #export {
return InputScaledMetric(24.0, scale)
}
InputDoubleClickSlopFor :: (scale: float) -> i32 #export {
return InputScaledMetric(6.0, scale)
}
InputPointerDragThresholdFor :: (scale: float) -> i32 #export {
return InputScaledMetric(5.0, scale)
}
InputPointerDragShouldStart :: (dx: i32, dy: i32, threshold: i32) -> bool #export {
if threshold < 0 { threshold = 0 }
return dx > threshold || dx < -threshold ||
dy > threshold || dy < -threshold
}
InputPointerDragIsHorizontal :: (dx: i32, dy: i32) -> bool #export {
if dx < 0 { dx = -dx }
if dy < 0 { dy = -dy }
return dx >= dy
}
InputPointerInteractionFor :: (inside: bool, captured: bool, disabled: bool,
hover_effects: bool, released: bool, release_consumed: bool,
press_started_inside: bool) -> InputPointerInteraction #export {
interaction: InputPointerInteraction
interaction.active = inside && !captured && !disabled
interaction.hovered = interaction.active && hover_effects
interaction.disabled_marker = inside && !captured && disabled
interaction.activated = interaction.active && released &&
!release_consumed && press_started_inside
interaction.consume_release = interaction.activated
return interaction
}
InputTempEditActivationFor :: (pressed: bool, control: bool,
same_kind: bool, same_widget: bool, same_component: bool,
elapsed_seconds: float, dx: float, dy: float, slop: i32) -> bool #export {
if !pressed {
return false
}
if control {
return true
}
if slop < 0 { slop = 0 }
if dx < (float)-slop || dx > (float)slop {
return false
}
if dy < (float)-slop || dy > (float)slop {
return false
}
return same_kind && same_widget && same_component &&
elapsed_seconds <= 0.30
}
InputCellLayoutFor :: (bounds: Rectangle, count: i32, index: i32,
requested_button_width: i32, has_step: bool) -> InputCellLayout #export {
layout: InputCellLayout
if count <= 0 { return layout }
if index < 0 { index = 0 }
if index >= count { index = count - 1 }
layout.cell.x = bounds.x + bounds.width * (float)index / (float)count
layout.cell.y = bounds.y
layout.cell.width = bounds.width / (float)count
layout.cell.height = bounds.height
layout.field = layout.cell
layout.minus = layout.cell
layout.plus = layout.cell
if has_step {
button_width: i32 = requested_button_width
if button_width < 0 { button_width = 0 }
max_button: i32 = (i32)(layout.cell.width / 2.0)
if button_width > max_button { button_width = max_button }
layout.field.width = layout.field.width - (float)(button_width * 2)
if layout.field.width < 0.0 { layout.field.width = 0.0 }
layout.minus.x = layout.field.x + layout.field.width
layout.minus.width = (float)button_width
layout.plus.x = layout.minus.x + layout.minus.width
layout.plus.width = (float)button_width
layout.has_step_buttons = button_width > 0
}
return layout
}
InputContinuousEffectiveStep :: (step: float, step_fast: float, fast: bool) -> float #export {
if fast && step_fast != 0.0 { return step_fast }
return step
}
InputDiscreteEffectiveStep :: (step: i32, step_fast: i32, fast: bool) -> i32 #export {
if fast && step_fast != 0 { return step_fast }
return step
}
InputEffectiveStep :: (step: f64, step_fast: f64, fast: bool) -> f64 #export {
if fast && step_fast != 0.0 { return step_fast }
return step
}
InputKindIsFloat :: (kind: NumericValueKind) -> bool #export {
return (i32)kind == (i32)NumericFloat
}
InputKindIsInt :: (kind: NumericValueKind) -> bool #export {
return (i32)kind == (i32)NumericInt
}
InputKindIsDouble :: (kind: NumericValueKind) -> bool #export {
return (i32)kind == (i32)NumericDouble
}
InputDefaultFormat :: (kind: NumericValueKind) -> const char* #export {
if InputKindIsInt(kind) { return "%d" }
if InputKindIsDouble(kind) { return "%.6f" }
return "%.3f"
}
InputRoundValueForKind :: (kind: NumericValueKind, value: f64) -> f64 #export {
if InputKindIsInt(kind) {
if value < 0.0 { return (f64)((i32)(value - 0.5)) }
return (f64)((i32)(value + 0.5))
}
return value
}
InputContinuousStepValue :: (value: float, step: float, step_fast: float,
direction: i32, fast: bool) -> InputContinuousStep #export {
result: InputContinuousStep
result.value = value
result.changed = false
if direction == 0 || step == 0.0 { return result }
next: float = value + (float)direction *
InputContinuousEffectiveStep(step, step_fast, fast)
result.value = next
result.changed = next != value
return result
}
InputDiscreteStepValue :: (value: i32, step: i32, step_fast: i32,
direction: i32, fast: bool) -> InputDiscreteStep #export {
result: InputDiscreteStep
result.value = value
result.changed = false
if direction == 0 || step == 0 { return result }
next: i32 = value + direction * InputDiscreteEffectiveStep(step, step_fast, fast)
result.value = next
result.changed = next != value
return result
}
InputStepValue :: (value: f64, step: f64, step_fast: f64,
direction: i32, fast: bool) -> InputStep #export {
result: InputStep
result.value = value
result.changed = false
if direction == 0 || step == 0.0 { return result }
next: f64 = value + (f64)direction *
InputEffectiveStep(step, step_fast, fast)
result.value = next
result.changed = next != value
return result
}
InputStepValueForKind :: (kind: NumericValueKind, value: f64, step: f64,
step_fast: f64, direction: i32, fast: bool) -> InputStep #export {
result: InputStep
result.value = value
result.changed = false
if InputKindIsInt(kind) {
stepped: InputDiscreteStep = InputDiscreteStepValue((i32)value,
(i32)step, (i32)step_fast, direction, fast)
result.value = (f64)stepped.value
result.changed = stepped.changed
return result
}
if InputKindIsFloat(kind) {
continuous: InputContinuousStep = InputContinuousStepValue(
(float)value, (float)step, (float)step_fast, direction, fast)
result.value = (f64)continuous.value
result.changed = continuous.changed
return result
}
return InputStepValue(value, step, step_fast, direction, fast)
}