Skip to content

Instantly share code, notes, and snippets.

@toshimasa-nanaki
Last active December 14, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshimasa-nanaki/46f5425bb6546c5f18f532bddf3cc17e to your computer and use it in GitHub Desktop.
Save toshimasa-nanaki/46f5425bb6546c5f18f532bddf3cc17e to your computer and use it in GitHub Desktop.
Rust 電卓アプリ ハローワールドだけ
[package]
name = "calculator"
version = "0.1.0"
authors = ["自分の名前とアドレス"]
[dependencies.conrod]
version = "0.61.1"
features = ["glium", "winit"]
[dependencies]
find_folder = "*"
#[macro_use] extern crate conrod;
fn main() {
feature::main();
}
mod feature {
use conrod::{self, widget, Colorable, Positionable, Widget};
use conrod::backend::glium::glium::{self, Surface};
pub fn main() {
const WIDTH: u32 = 400;
const HEIGHT: u32 = 200;
// Build the window.
let mut events_loop = glium::glutin::EventsLoop::new();
let window = glium::glutin::WindowBuilder::new()
.with_title("Calculator")
.with_dimensions((WIDTH, HEIGHT).into());
let context = glium::glutin::ContextBuilder::new()
.with_vsync(true)
.with_multisampling(4);
let display = glium::Display::new(window, context, &events_loop).unwrap();
// construct our `Ui`.
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();
// Generate the widget identifiers.
widget_ids!(struct Ids { text });
let ids = Ids::new(ui.widget_id_generator());
// Add a `Font` to the `Ui`'s `font::Map` from file.
const FONT_PATH: &'static str =
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/fonts/ipag.ttf");
ui.fonts.insert_from_file(FONT_PATH).unwrap();
// A type used for converting `conrod::render::Primitives` into `Command`s that can be used
// for drawing to the glium `Surface`.
let mut renderer = conrod::backend::glium::Renderer::new(&display).unwrap();
// The image map describing each of our widget->image mappings (in our case, none).
let image_map = conrod::image::Map::<glium::texture::Texture2d>::new();
let mut events = Vec::new();
'render: loop {
events.clear();
// Get all the new events since the last frame.
events_loop.poll_events(|event| { events.push(event); });
// If there are no new events, wait for one.
if events.is_empty() {
events_loop.run_forever(|event| {
events.push(event);
glium::glutin::ControlFlow::Break
});
}
// Process the events.
for event in events.drain(..) {
// Break from the loop upon `Escape` or closed window.
match event.clone() {
glium::glutin::Event::WindowEvent { event, .. } => {
match event {
glium::glutin::WindowEvent::CloseRequested |
glium::glutin::WindowEvent::KeyboardInput {
input: glium::glutin::KeyboardInput {
virtual_keycode: Some(glium::glutin::VirtualKeyCode::Escape),
..
},
..
} => break 'render,
_ => (),
}
}
_ => (),
};
// Use the `winit` backend feature to convert the winit event to a conrod input.
let input = match conrod::backend::winit::convert_event(event, &display) {
None => continue,
Some(input) => input,
};
// Handle the input with the `Ui`.
ui.handle_event(input);
// Set the widgets.
let ui = &mut ui.set_widgets();
// "Calc" in the middle of the screen.
widget::Text::new("Calc")
.middle_of(ui.window)
.color(conrod::color::WHITE)
.font_size(32)
.set(ids.text, ui);
}
// Draw the `Ui` if it has changed.
if let Some(primitives) = ui.draw_if_changed() {
renderer.fill(&display, primitives, &image_map);
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 1.0);
renderer.draw(&display, &mut target, &image_map).unwrap();
target.finish().unwrap();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment