Created
December 16, 2018 10:56
-
-
Save toshimasa-nanaki/76f33305a2b9167ca254f9e2b7a79521 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, Positionable, Widget, Labelable, Sizeable}; | |
use conrod::backend::glium::glium::{self, Surface}; | |
//publicなmain関数 | |
pub fn main() { | |
//画面の横縦サイズ宣言 | |
const WIDTH: u32 = 400; | |
const HEIGHT: u32 = 600; | |
//ループイベントオブジェクトを生成 | |
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, button4, button5, button6, button7, button8, button9, button0, button_point, button_eq, button_plus, button_minus, button_multi, button_divide, textbox }); | |
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(); | |
let mut num = vec![""]; | |
let mut str_num:String = "".to_string(); | |
//無限ループ(イベントを待つ人) | |
'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(); | |
//計算表示 | |
widget::TextBox::new(&str_num).w_h(400.0, 40.0).top_left_of(ui.window).set(ids.textbox, ui); | |
//ボタン1 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
//.top_left_with_margin_on(ui.window, 30.0) | |
.down_from(ids.button4, 30.0) | |
.label("1") | |
.label_font_size(16) | |
.set(ids.button1, ui) | |
.was_clicked() { | |
num.push("1"); | |
str_num.push_str("1"); | |
} | |
//ボタン2 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button5, 30.0) | |
// .middle_of(ui.window) | |
.label("2") | |
.label_font_size(16) | |
.set(ids.button2, ui) | |
.was_clicked() { | |
num.push("2"); | |
str_num.push_str("2"); | |
} | |
//ボタン3 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button6, 30.0) | |
// .bottom_right_of(ui.window) | |
.label("3") | |
.label_font_size(16) | |
.set(ids.button3, ui) | |
.was_clicked() { | |
num.push("3"); | |
str_num.push_str("3"); | |
} | |
//ボタン4 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
//.top_left_with_margin_on(ui.window, 30.0) | |
.down_from(ids.button7, 30.0) | |
.label("4") | |
.label_font_size(16) | |
.set(ids.button4, ui) | |
.was_clicked() { | |
num.push("4"); | |
str_num.push_str("4"); | |
} | |
//ボタン5 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button8, 30.0) | |
.label("5") | |
.label_font_size(16) | |
.set(ids.button5, ui) | |
.was_clicked() { | |
num.push("5"); | |
str_num.push_str("5"); | |
} | |
//ボタン6 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button9, 30.0) | |
.label("6") | |
.label_font_size(16) | |
.set(ids.button6, ui) | |
.was_clicked() { | |
num.push("6"); | |
str_num.push_str("6"); | |
} | |
//ボタン7 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
//.top_left_with_margin_on(ui.window, 30.0) | |
.down_from(ids.textbox, 30.0) | |
.label("7") | |
.label_font_size(16) | |
.set(ids.button7, ui) | |
.was_clicked() { | |
num.push("7"); | |
str_num.push_str("7"); | |
} | |
//ボタン8 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.right_from(ids.button7, 30.0) | |
.label("8") | |
.label_font_size(16) | |
.set(ids.button8, ui) | |
.was_clicked() { | |
num.push("8"); | |
str_num.push_str("8"); | |
} | |
//ボタン9 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.right_from(ids.button8, 30.0) | |
.label("9") | |
.label_font_size(16) | |
.set(ids.button9, ui) | |
.was_clicked() { | |
num.push("9"); | |
str_num.push_str("9"); | |
} | |
//ボタン0 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
//.top_left_with_margin_on(ui.window, 30.0) | |
.down_from(ids.button1, 30.0) | |
.label("0") | |
.label_font_size(16) | |
.set(ids.button0, ui) | |
.was_clicked() { | |
num.push("0"); | |
str_num.push_str("0"); | |
} | |
//ボタン小数点 | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button2, 30.0) | |
// .middle_of(ui.window) | |
.label(".") | |
.label_font_size(16) | |
.set(ids.button_point, ui) | |
.was_clicked() { | |
num.push("."); | |
str_num.push_str("."); | |
} | |
//ボタンイコール | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button3, 30.0) | |
// .bottom_right_of(ui.window) | |
.label("=") | |
.label_font_size(16) | |
.set(ids.button_eq, ui) | |
.was_clicked() { | |
// num.push("="); | |
// str_num.push_str("="); | |
str_num.clear(); | |
str_num.push_str("Error"); | |
} | |
//ボタン+ | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button_minus, 30.0) | |
.label("+") | |
.label_font_size(16) | |
.set(ids.button_plus, ui) | |
.was_clicked() { | |
num.push("+"); | |
str_num.push_str("+"); | |
} | |
//ボタンマイナス | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button_multi, 30.0) | |
// .bottom_right_of(ui.window) | |
.label("ー") | |
.label_font_size(16) | |
.set(ids.button_minus, ui) | |
.was_clicked() { | |
num.push("ー"); | |
str_num.push_str("ー"); | |
} | |
//ボタン× | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.down_from(ids.button_divide, 30.0) | |
// .bottom_right_of(ui.window) | |
.label("×") | |
.label_font_size(16) | |
.set(ids.button_multi, ui) | |
.was_clicked() { | |
num.push("×"); | |
str_num.push_str("×"); | |
} | |
//ボタン÷ | |
if widget::Button::new() | |
.w_h(40.0, 40.0) | |
.right_from(ids.button9, 30.0) | |
// .bottom_right_of(ui.window) | |
.label("÷") | |
.label_font_size(16) | |
.set(ids.button_divide, ui) | |
.was_clicked() { | |
num.push("÷"); | |
str_num.push_str("÷"); | |
} | |
} | |
// 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