Files
kryon/runtime/input.kry
T

112 lines
3.3 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
}
InputCellLayout :: struct {
cell: Rectangle
field: Rectangle
minus: Rectangle
plus: Rectangle
has_step_buttons: bool
}
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
}
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
}