[][src]Trait glerminal::menu_systems::InterfaceItem

pub trait InterfaceItem: InterfaceItemClone {
    fn get_base(&self) -> &InterfaceItemBase;
fn get_mut_base(&mut self) -> &mut InterfaceItemBase;
fn get_total_width(&self) -> u32;
fn get_total_height(&self) -> u32;
fn draw(&mut self, text_buffer: &mut TextBuffer);
fn handle_events(&mut self, events: &Events) -> bool;
fn update(&mut self, delta: f32, processor: &dyn TextProcessor); }

Represents a single menu item: an item that is somewhere, can handle events and can be drawn.

Current pre-implemented items to use in Menus are

  • TextItem, functions as a text label and a button.
  • TextInput, can accept text input that can be get with get_text.
  • Dialog, can be used to display large volumes of text compactly.
  • Checkbox, can be checked (and unchecked), like a check- or radiobox (if using CheckboxGroup).

You can make your own InterfaceItems that you can create, draw, and use for Menus by implementing InterfaceItem. To implement InterfaceItem, you need to derive Clone too though.

A simple example of how to make an InterfaceItem that you can use for Menus

use glerminal::menu_systems::{InterfaceItem, InterfaceItemBase};
use glerminal::{with_base, Events, TextBuffer, TextStyle};
use glerminal::text_processing::TextProcessor;

#[derive(Clone)]
struct TextLabel {
    base: InterfaceItemBase,
    text: String,
}

impl TextLabel {
    pub fn new(text: String) -> TextLabel {
        TextLabel {
            base: InterfaceItemBase::new(false),
            text: text,
        }
    }

    with_base!(TextLabel);
}

impl InterfaceItem for TextLabel {
    fn get_base(&self) -> &InterfaceItemBase {
        &self.base
    }

    fn get_mut_base(&mut self) -> &mut InterfaceItemBase {
        &mut self.base
    }

    fn get_total_width(&self) -> u32 {
        self.text.len() as u32
    }

    fn get_total_height(&self) -> u32 {
        1
    }

    fn draw(&mut self, text_buffer: &mut TextBuffer) {
        self.base.dirty = false;
        let pos = self.base.get_pos();

        text_buffer.cursor.style = TextStyle {
            fg_color: [0.8, 0.8, 0.8, 1.0],
            bg_color: [0.0, 0.0, 0.0, 0.0],
            ..Default::default()
        };
        text_buffer.cursor.move_to(pos.0, pos.1);
        text_buffer.write(self.text.clone());
    }

    fn handle_events(&mut self, _: &Events) -> bool {
        false
    }

    // If you want TextProcessor support, ie. Parser support, implement the processing here.
    // Also remember to not process every frame, but only when necessary.
    fn update(&mut self, _: f32, processor: &TextProcessor) {}
}

Required methods

fn get_base(&self) -> &InterfaceItemBase

Get the InterfaceItemBase

fn get_mut_base(&mut self) -> &mut InterfaceItemBase

Get the InterfaceItemBase as mutable

fn get_total_width(&self) -> u32

Get the width this InterfaceItem can take up

This should ideally never change

fn get_total_height(&self) -> u32

Get the height this InterfaceItem can take up

This should ideally never change

fn draw(&mut self, text_buffer: &mut TextBuffer)

Draw the InterfaceItem

fn handle_events(&mut self, events: &Events) -> bool

Handle events for this InterfaceItem.

Returns whether it handled any events.

fn update(&mut self, delta: f32, processor: &dyn TextProcessor)

Update this InterfaceItem; delta is given in seconds. (see Terminal.delta_time()). Also process any text that has changed since last update.

Loading content...

Implementors

impl InterfaceItem for Checkbox
[src]

impl InterfaceItem for Dialog
[src]

impl InterfaceItem for TextInput
[src]

impl InterfaceItem for TextItem
[src]

Loading content...