Skip to main content

simple/
simple.rs

1// SPDX-License-Identifier: MIT
2
3use orbclient::{Color, EventOption, GraphicsPath, Mode, Renderer, Window, WindowFlag};
4
5fn main() {
6    let (width, height) = orbclient::get_display_size().unwrap();
7
8    let mut window = Window::new_flags(
9        (width as i32) / 4,
10        (height as i32) / 4,
11        width / 2,
12        height / 2,
13        "TITLE",
14        &[WindowFlag::Transparent],
15    )
16    .unwrap();
17
18    let (win_w, win_h) = (width / 2, height / 2);
19
20    // top left -> bottom rigth
21    window.linear_gradient(
22        0,
23        0,
24        win_w / 3,
25        win_h,
26        0,
27        0,
28        (win_w / 3) as i32,
29        (win_h / 2) as i32,
30        Color::rgb(128, 128, 128),
31        Color::rgb(255, 255, 255),
32    );
33    // horizontal gradient
34    window.linear_gradient(
35        (win_w / 3) as i32,
36        0,
37        win_w / 3,
38        win_h,
39        (win_w / 3) as i32,
40        0,
41        (2 * win_w / 3) as i32,
42        0,
43        Color::rgb(128, 255, 255),
44        Color::rgb(255, 255, 255),
45    );
46    // vertical gradient
47    window.linear_gradient(
48        (2 * win_w / 3) as i32,
49        0,
50        win_w / 3,
51        win_h,
52        (2 * win_w / 3) as i32,
53        0,
54        (2 * win_w / 3) as i32,
55        win_h as i32,
56        Color::rgb(0, 128, 0),
57        Color::rgb(255, 255, 255),
58    );
59    window.arc(100, 100, -25, 1 << 0 | 1 << 2, Color::rgb(0, 0, 255));
60    window.arc(100, 100, -25, 1 << 1 | 1 << 3, Color::rgb(0, 255, 255));
61    window.arc(100, 100, -25, 1 << 4 | 1 << 6, Color::rgb(255, 0, 255));
62    window.arc(100, 100, -25, 1 << 5 | 1 << 7, Color::rgb(255, 255, 0));
63    window.circle(100, 100, 25, Color::rgb(0, 0, 0));
64    window.circle(100, 101, -25, Color::rgb(0, 255, 0));
65    window.circle(220, 220, -100, Color::rgba(128, 128, 128, 80));
66    window.wu_circle(150, 220, 100, Color::rgba(255, 0, 0, 255));
67    window.line(0, 0, 200, 200, Color::rgb(255, 0, 0));
68    window.line(0, 200, 200, 0, Color::rgb(128, 255, 0));
69    // vertical and horizontal line test
70    window.line(100, 0, 100, 200, Color::rgb(0, 0, 255));
71    window.line(0, 100, 200, 100, Color::rgb(255, 255, 0));
72    window.wu_line(100, 220, 400, 250, Color::rgba(255, 0, 0, 255));
73    window.line(100, 230, 400, 260, Color::rgba(255, 0, 0, 255));
74
75    // path and bezier curve example draw a cloud
76    let mut cloud_path = GraphicsPath::new();
77    cloud_path.move_to(170, 80);
78    cloud_path.bezier_curve_to(130, 100, 130, 150, 230, 150);
79    cloud_path.bezier_curve_to(250, 180, 320, 180, 340, 150);
80    cloud_path.bezier_curve_to(420, 150, 420, 120, 390, 100);
81    cloud_path.bezier_curve_to(430, 40, 370, 30, 340, 50);
82    cloud_path.bezier_curve_to(320, 5, 250, 20, 250, 50);
83    cloud_path.bezier_curve_to(200, 5, 150, 20, 170, 80);
84    window.draw_path_stroke(cloud_path, Color::rgb(0, 0, 255));
85
86    // path and quadratic curve example draw a balloon
87    let mut balloon_path = GraphicsPath::new();
88    balloon_path.move_to(75, 25);
89    balloon_path.quadratic_curve_to(25, 25, 25, 62);
90    balloon_path.quadratic_curve_to(25, 100, 50, 100);
91    balloon_path.quadratic_curve_to(50, 120, 30, 125);
92    balloon_path.quadratic_curve_to(60, 120, 65, 100);
93    balloon_path.quadratic_curve_to(125, 100, 125, 62);
94    balloon_path.quadratic_curve_to(125, 25, 75, 25);
95    window.draw_path_stroke(balloon_path, Color::rgb(0, 0, 255));
96
97    window.char(200, 200, '═', Color::rgb(0, 0, 0));
98    window.char(208, 200, '═', Color::rgb(0, 0, 0));
99
100    // testing for non existent x,y position : does not panic but returns Color(0,0,0,0)
101    let _non_existent_pixel = window.getpixel(width as i32 + 10, height as i32 + 10);
102
103    // testing PartialEq for Color
104    if Color::rgb(11, 2, 3) == Color::rgba(1, 2, 3, 100) {
105        println!("Testing colors: they are the same!")
106    } else {
107        println!("Testing colors: they are NOT the same!")
108    }
109
110    //Draw a transparent rectangle over window content
111    // default mode is Blend
112    window.rect(250, 200, 80, 80, Color::rgba(100, 100, 100, 100));
113
114    //Draw an opaque rectangle replacing window content
115    window.mode().set(Mode::Overwrite); // set window drawing mode to Overwrite from now on
116    window.rect(300, 220, 80, 80, Color::rgb(100, 100, 100));
117
118    //Draw a hole in the window replacing alpha channel (Only in Orbital, not in SDL2)
119    window.rect(300, 100, 80, 80, Color::rgba(10, 10, 10, 1));
120
121    //Draw a transparent rectangle over window content
122    window.mode().set(Mode::Blend); //set mode to Blend fron now on
123    window.rect(200, 230, 80, 80, Color::rgba(100, 100, 100, 100));
124
125    //Draw a blured box over window content
126    window.box_blur(170, 100, 150, 150, 10);
127
128    //Draw a shadow around a box
129    window.box_shadow(170, 100, 150, 150, 0, 0, 20, Color::rgba(0, 0, 0, 255));
130
131    window.sync();
132
133    'events: loop {
134        for event in window.events() {
135            match event.to_option() {
136                EventOption::Quit(_quit_event) => break 'events,
137                EventOption::Mouse(evt) => println!(
138                    "At position {:?} pixel color is : {:?}",
139                    (evt.x, evt.y),
140                    window.getpixel(evt.x, evt.y)
141                ),
142                event_option => println!("{:?}", event_option),
143            }
144        }
145    }
146}