Skip to content

Tutorial EzLang Slider

ddbnl edited this page Aug 29, 2022 · 1 revision

The slider allows a user to choose a numerical value by dragging the slider left or right (using keyboard or mouse). A slider has a value, a minimum value, a maximum value, and a step value. The step value determines the minimum amount by which the value can be adjusted. A slider with min 0, max 20, and step 5, has 5 possible values (0, 5, 10 , 15, 20). If you manually set the slider value, make sure the new value is a multiple of the step value.

The slider becomes useful by binding an 'on_value_change' callback to it. We will discuss callbacks in detail in the scheduler chapter, but we'll show an example here.

Here are a few cropped examples with screenshots (see 'examples' for the full functional EzLang/code):

Regular slider

// This is a regular slider
- Label:
    text: Regular:
    auto_scale: true, true
    padding_right: 1
- Slider:
    min: 0
    max: 100
    step: 5
    value: 50
    padding_bottom: 1
    selection_order: 1
    auto_scale: true, true

slider_1

Disabled slider

// This is a disabled slider
- Label:
    text: Disabled:
    auto_scale: true, true
    padding_right: 1
- Slider:
    min: 0
    max: 100
    step: 5
    disabled: true
    value: 50
    padding_bottom: 1
    selection_order: 2
    auto_scale: true, true

slider_2

Slider with hovering value label

// This is a slider with a hovering value label
- Label:
    text: With label:
    auto_scale: true, true
    padding_right: 1
- Layout:
    mode: float
    auto_scale: true, true

    - Label:
        id: my_label
        text: parent.label_slider.value
        x: ( parent.label_slider.width - 1 ) * (parent.label_slider.value / (parent.label_slider.max - parent.label_slider.min))
        y: 0
        auto_scale: true, true
    - Slider:
        id: label_slider
        pos: 0, 1
        min: 0
        max: 100
        step: 5
        value: 50
        padding_bottom: 1
        selection_order: 3
        auto_scale: true, true

slider_3

Slider with synced text input

// This is a slider with a synced text input; the user can use either one to set the value
- Label:
    text: With text input:
    auto_scale: true, true
    padding_right: 1
- Layout:
    mode: box
    orientation: horizontal
    auto_scale: true, true
    - Slider:
        id: input_slider
        min: 0
        max: 100
        step: 5
        value: 50
        padding_bottom: 1
        selection_order: 4
        auto_scale: true, true
        padding_right: 1

    - TextInput:
        id: my_input
        text: parent.input_slider.value
        size_hint_x: none
        width: 4
        max_length: 3
let sync_slider_callback = |context: Context| {

    let input_state = context.state_tree.get_mut("my_input")
        .as_text_input_mut();
    let mut value = input_state.get_text();
    if value.is_empty() {
        return false
    }

    let slider_state = context.state_tree.get_mut("input_slider")
        .as_slider_mut();
    match value.trim().parse() {
        Ok(i) => {
            if i != slider_state.get_value() {
                slider_state.set_value(i);
                slider_state.update(context.scheduler);
            }
        },
        Err(_) => {
            let old_value = slider_state.get_value();
            let input_state = context.state_tree.get_mut("my_input")
                .as_text_input_mut();
            input_state.set_text(old_value.to_string());
            input_state.update(context.scheduler);
        }
    }
    false
};

slider_4

Continue

The general tutorial continues with: EzLang Canvas.

Tutorial Tutorial-Project-Structure
  Minimal example
EzLang
  EzLang basics
  EzLang Templates
  Ezlang Layout modes
   EzLang Box mode layouts
   EzLang Stack mode layouts
   EzLang Table mode layouts
   EzLang Float mode layouts
   EzLang Tab mode layouts
   EzLang Screen mode layouts
   EzLang Layout Scrolling
   EzLang Layout Views
  EzLang Widget overview
   EzLang Label
   EzLang Text Input
   EzLang Button
   EzLang Checkbox
   EzLang Radio button
   EzLang Dropdown
   EzLang Slider
   EzLang Canvas
  EzLang Property Binding
  EzLang Sizing
   EzLang Size hints
   EzLang Auto scaling
   EzLang Maths Sizing
   EzLang Manual Sizing
  EzLang Positioning
   EzLang Layout Mode Positioning
   EzLang Position Hints
   EzLang Position Maths
   EzLang Manual Position
   EzLang Adjusting Position
  EzLang Keyboard Selection
Scheduler
  Widget States and the State Tree
  The Scheduler Object
  Managing callbacks
   Callback Structure
   Callback Configs
   Callback: On keyboard enter
   Callback: On Left Mouse Click
   Callback: On Press
   Callback: On Select
   Callback: On Deselect
   Callback: On Right Mouse Click
   Callback: On Hover
   Callback: On Drag
   Callback: On Scroll Up
   Callback: On Scroll Down
   Callback: On Value Change
   Callback: Custom Key Binds
   Callback: Global Key Binds
   Callback: Property Binds
  Tasks
   Scheduled Single Exectution Tasks
   Scheduled Recurring Tasks
   Threaded Tasks
  Custom Properties
  Modals
  Programmatic Widgets
  Updating widgets
  Managing selection
Default global (key)binds
Performance
ExamplesLayout: Box Mode Nested
Layout: Box Mode Size Hints
Layout: Stack Mode
Layout: Table Mode Dynamic
Layout: Table Mode Static
Layout: Float Mode Manual
Layout: Float Mode Position hints
Layout: Screen Mode
Layout: Tab Mode
Layout: Scrolling
Layout: Views
Widget: Label
Widget: Text input
Widget: Button
Widget: Checkbox
Widget: Radio Button
Widget: Dropdown
Widget: Slider
Widget: Progress Bar
Widget: Canvas
Scheduler: Schedule Once
Scheduler: Schedule Once Callback
Scheduler: Schedule Recurring
Scheduler: Schedule Recurring Callback
Scheduler: Threaded Task State Tree
Scheduler: Threaded Task Custom Property
Scheduler: Create Widgets
Scheduler: Modal Popup
Reference Widgets
  Common Properties
  Label
  Text Input
  Button
  Checkbox
  Radio button
  Dropdown
  Slider
  Canvas
Scheduler
  Schedule once
  Schedule Recurring
  Schedule Threaded
  Cancel Task
  Cancel Recurring Task
  Create Widget
  Remove Widget
  Select Widget
  Deselect Widget
  Update Widget
  Force Redraw
  Open Modal
  Dismiss Modal
  Bind Global Key
  Remove Global Key
  Clear Global Keys
  Bind Property
  Create Custom Properties
  Get Property
  Get Property Mut
  Overwrite Callback Config
  Update Callback Config
  Exit
Clone this wiki locally