# ui/uiscale (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uiscale/

UI scale. (vanilla.js ui/uiscale.js, UIScale)
- The fixed side of the view (height for portrait, width for landscape) always has the same length, so on a small screen (a portal's PC iframe, etc.)
  one view unit becomes much smaller than 1 CSS pixel on screen and text becomes unreadable. In that case it computes a scale that enlarges only the UI layer on screen. (the game's graphics stay as they are)
- If one unit is smaller than minimum_css_scale CSS pixels, it enlarges by that much. (1 ~ maximum_scale)
  - On a short screen, it does not enlarge so far that the enlarged UI layer's height falls below minimum_layer_height. (0 means not checked)
  - On a portrait screen (width at most portrait_ratio times the height), it enlarges without an upper limit so that one unit becomes minimum_css_scale CSS pixels.
- Differences from the original:
  - It takes the CSS pixels of one unit (ViewManager::get_css_pixel_scale) as an argument. (the original reads it from the engine if options.cssPixelScale is absent)
  - The options object is the UIScaleOptions struct.
- Usage: let ui_scale = UIScale::read(view_width, view_height, view_manager.get_css_pixel_scale(), &UIScaleOptions::default());

## UIScaleOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

UI scale settings. (original options = { minimumCssScale = 0.8, maximumScale = 1.8, minimumLayerHeight = 0, portraitRatio = 1.1 })

Example: Keeping the UI layer height on a short screen

```rust
use vanilla::{UIScale, UIScaleOptions};

let options = UIScaleOptions {
	minimum_layer_height: 900.0,
	..UIScaleOptions::default()
};
assert_eq!(options.maximum_scale, 1.8);
let ui_scale = UIScale::read(1920.0, 1080.0, 0.4, &options);
assert!((ui_scale - 1.2).abs() < 0.0001);
```

- `pub minimum_css_scale: f64` (field, since 0.4.0): Enlarges if one unit is smaller than this many CSS pixels.
  - returns f64
- `pub maximum_scale: f64` (field, since 0.4.0): Upper limit of enlargement. (portrait screens are not bound by it)
  - returns f64
- `pub minimum_layer_height: f64` (field, since 0.4.0): Lowest height the enlarged UI layer keeps. (0 means not checked)
  - returns f64
- `pub portrait_ratio: f64` (field, since 0.4.0): Portrait screen if the width is at most this many times the height.
  - returns f64

## UIScale (struct)

UI scale.

Example: Scale up a UI layer node to fit the screen

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, UIScale, UIScaleOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let layer_id = node_tree.create_child(root_id, "UILayer");
let view_manager = engine.get_view_manager();
let view_size = view_manager.get_view_size();
let ui_scale = UIScale::read(view_size.x, view_size.y, view_manager.get_css_pixel_scale(), &UIScaleOptions::default());
node_tree[layer_id].set_local_scale(Vector2::create(ui_scale, ui_scale));
assert!(ui_scale >= 1.0);
```

- `pub fn read(view_width: f64, view_height: f64, css_pixel_scale: f64, options: &UIScaleOptions) -> f64` (static, since 0.4.0): Reads the UI scale. (view_width, view_height are the area the UI layer covers, css_pixel_scale is the CSS pixels of one view unit)
  - `view_width` f64: Width of the area the UI layer covers. (view units)
  - `view_height` f64: Height of the area the UI layer covers. (view units)
  - `css_pixel_scale` f64: CSS pixels one view unit takes on screen. (ViewManager::get_css_pixel_scale, not computed if not greater than 0)
  - `options` &UIScaleOptions: Scale settings.
  - returns f64: Scale to multiply the UI layer by. (1 or more, 1 if css_pixel_scale is not greater than 0, may exceed maximum_scale on a portrait screen)

  Example: Scale by the CSS pixels of one unit

  ```rust
  use vanilla::{UIScale, UIScaleOptions};
  
  let options = UIScaleOptions::default();
  assert_eq!(UIScale::read(1920.0, 1080.0, 1.0, &options), 1.0);
  assert!((UIScale::read(1920.0, 1080.0, 0.5, &options) - 1.6).abs() < 0.0001);
  assert!((UIScale::read(1920.0, 1080.0, 0.2, &options) - 1.8).abs() < 0.0001);
  assert!((UIScale::read(1080.0, 1920.0, 0.2, &options) - 4.0).abs() < 0.0001);
  assert_eq!(UIScale::read(1920.0, 1080.0, 0.0, &options), 1.0);
  ```

- `pub fn is_portrait(view_width: f64, view_height: f64, portrait_ratio: f64) -> bool` (static, since 0.4.0): Whether it is a portrait screen. (width at most portrait_ratio times the height)
  - `view_width` f64: Width of the area. (view units)
  - `view_height` f64: Height of the area. (view units)
  - `portrait_ratio` f64: Upper limit of the width / height ratio seen as a portrait screen. (1.1 in the default settings)
  - returns bool: true if the width is at most portrait_ratio times the height.
