Created
December 15, 2018 02:32
-
-
Save toshimasa-nanaki/bb5d0c2ef7cd455754361dfedcca4070 to your computer and use it in GitHub Desktop.
Rust 電卓アプリ ボタン出してみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//クレートとしてconrodを読み込む | |
#[macro_use] extern crate conrod; | |
fn main() { | |
//featureモジュールのmain関数呼び出し | |
feature::main(); | |
} | |
//モジュール | |
mod feature { | |
//conrodで利用するものを宣言 | |
use conrod::{self, widget, Colorable, Positionable, Widget, Labelable, Sizeable}; | |
use conrod::backend::glium::glium::{self, Surface}; | |
//publicなmain関数 | |
pub fn main() { | |
//画面の横縦サイズ宣言 | |
const WIDTH: u32 = 400; | |
const HEIGHT: u32 = 200; | |
//ループイベントオブジェクトを生成 | |
let mut events_loop = glium::glutin::EventsLoop::new(); | |
//gliumを使ってディスプレイ用のオブジェクトを作る | |
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(); | |
// UIの組み立て | |
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build(); | |
// ウィジェットIDの生成 | |
widget_ids!(struct Ids { text, button1, button2, button3 }); | |
let ids = Ids::new(ui.widget_id_generator()); | |
// フォントファイルの読み込み | |
const FONT_PATH: &'static str = | |
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/fonts/ipag.ttf"); | |
ui.fonts.insert_from_file(FONT_PATH).unwrap(); | |
// gliumで描くためのオブジェクト | |
let mut renderer = conrod::backend::glium::Renderer::new(&display).unwrap(); | |
// ウィジェットと画像をマッピングさせたマップ(今は使わない) | |
let image_map = conrod::image::Map::<glium::texture::Texture2d>::new(); | |
let mut events = Vec::new(); | |
//無限ループ(イベントを待つ人) | |
'render: loop { | |
events.clear(); | |
// 最新のフレームからイベントをすべて取得する | |
events_loop.poll_events(|event| { events.push(event); }); | |
// イベントが取得できるまで待つ。 | |
if events.is_empty() { | |
events_loop.run_forever(|event| { | |
events.push(event); | |
glium::glutin::ControlFlow::Break | |
}); | |
} | |
// 取得したイベントを処理する | |
for event in events.drain(..) { | |
//表示された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, | |
_ => (), | |
} | |
} | |
_ => (), | |
}; | |
// winitイベントをconrodイベントに変換する。 | |
let input = match conrod::backend::winit::convert_event(event, &display) { | |
None => continue, | |
Some(input) => input, | |
}; | |
// UIでハンドリング(これでButtonをクリックしたときのイベントとかを検知できる) | |
ui.handle_event(input); | |
// ウィジェットをUIにセットする | |
let ui = &mut ui.set_widgets(); | |
// 画面中央にCalcを表示 | |
widget::Text::new("Calc!") | |
.middle_of(ui.window) | |
.color(conrod::color::WHITE) | |
.font_size(64) | |
.set(ids.text, ui); | |
//ボタン1 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.top_left_of(ui.window) | |
.label("1") | |
.label_font_size(16) | |
.set(ids.button1, ui) | |
.was_clicked() { | |
println!("1"); | |
} | |
//ボタン2 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.middle_of(ui.window) | |
.label("2") | |
.label_font_size(16) | |
.set(ids.button2, ui) | |
.was_clicked() { | |
println!("2"); | |
} | |
//ボタン3 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.bottom_right_of(ui.window) | |
.label("3") | |
.label_font_size(16) | |
.set(ids.button3, ui) | |
.was_clicked() { | |
println!("3"); | |
} | |
} | |
// uiが変わった場合に再描画する | |
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
gliumの日本語のサンプルコードが少ないので、助かります!