├── .gitignore ├── calc.PNG ├── assets └── fonts │ ├── FiraSans-Bold.ttf │ └── FiraMono-Medium.ttf ├── Cargo.toml ├── README.md ├── .vscode └── launch.json ├── .cargo └── config ├── LICENSE ├── src ├── calc.rs ├── button.rs └── main.rs └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /calc.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PravinKumar95/simple-calc/HEAD/calc.PNG -------------------------------------------------------------------------------- /assets/fonts/FiraSans-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PravinKumar95/simple-calc/HEAD/assets/fonts/FiraSans-Bold.ttf -------------------------------------------------------------------------------- /assets/fonts/FiraMono-Medium.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PravinKumar95/simple-calc/HEAD/assets/fonts/FiraMono-Medium.ttf -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bevy-test" 3 | version = "0.1.0" 4 | authors = ["Pravin Kumar "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bevy = '0.5.0' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-calc 2 | Calculator created using bevyengine following their examples. 3 | Note: It's not feature complete. 4 | 5 | ![image of simple calculator](/calc.PNG "bevy calc") 6 | 7 | # Install 8 | 1. git clone https://github.com/PravinKumar95/simple-calc.git 9 | 2. cd simple-calc 10 | 3. cargo run --release 11 | 12 | ## Note 13 | In case of compile error, remove config file under .cargo to disable fast builds 14 | and beware of use and throw code 15 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(Windows) Launch", 9 | "type": "cppvsdbg", 10 | "request": "launch", 11 | "program": "${workspaceFolder}/target/debug/bevy-test.exe", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${workspaceFolder}", 15 | "environment": [], 16 | "externalConsole": false 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /.cargo/config: -------------------------------------------------------------------------------- 1 | # Rename this file to `config` to enable "fast build" configuration. Please read the notes below. 2 | 3 | # NOTE: For maximum performance, build using a nightly compiler 4 | # If you are using rust stable, remove the "-Zshare-generics=y" below. 5 | 6 | # [target.x86_64-unknown-linux-gnu] 7 | # linker = "/usr/bin/clang" 8 | # rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"] 9 | 10 | # [target.x86_64-apple-darwin] 11 | # rustflags = ["-Zshare-generics=y"] 12 | 13 | # NOTE: you must manually install lld on windows. you can easily do this with the "scoop" package manager: 14 | # `scoop install llvm` 15 | # [target.x86_64-pc-windows-msvc] 16 | # linker = "lld-link.exe" 17 | # rustflags = ["-Clinker=lld", "-Zshare-generics=y"] 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Pravin kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/calc.rs: -------------------------------------------------------------------------------- 1 | use std::option::Option; 2 | pub struct Calc { 3 | left: f32, 4 | right:Option, 5 | symbol:String, 6 | is_evaluated:bool, 7 | } 8 | 9 | impl Calc { 10 | pub fn new() -> Self { 11 | Calc { left: 0.0,right:Option::None,symbol : "".to_string(),is_evaluated:false } 12 | } 13 | pub fn add(&mut self) { 14 | self.left = self.left+self.right.unwrap_or(0.0); 15 | self.right = Option::None; 16 | self.symbol = "".to_string(); 17 | self.is_evaluated = true; 18 | } 19 | pub fn sub(&mut self) { 20 | self.left = self.left-self.right.unwrap_or(0.0); 21 | self.right = Option::None; 22 | self.symbol = "".to_string(); 23 | self.is_evaluated = true; 24 | } 25 | pub fn mult(&mut self) { 26 | self.left = self.left*self.right.unwrap_or(1.0); 27 | self.right = Option::None; 28 | self.symbol = "".to_string(); 29 | self.is_evaluated = true; 30 | } 31 | pub fn div(&mut self) { 32 | self.left = self.left/self.right.unwrap_or(1.0); 33 | self.right = Option::None; 34 | self.symbol = "".to_string(); 35 | self.is_evaluated = true; 36 | } 37 | pub fn display(&self) -> String { 38 | if let Some(right) = &self.right { 39 | format!("{} {} {}",&self.left,&self.symbol,right) 40 | } 41 | else { 42 | format!("{} {}",&self.left,&self.symbol) 43 | } 44 | } 45 | pub fn symbol(&self) -> String{ 46 | self.symbol.clone() 47 | } 48 | pub fn set_display(&mut self,val:f32) { 49 | self.left = val; 50 | } 51 | pub fn add_display(&mut self,val:f32){ 52 | if self.is_evaluated && 53 | (self.symbol != "+".to_string() 54 | &&self.symbol != "-".to_string() 55 | &&self.symbol != "/".to_string() 56 | &&self.symbol != "*".to_string()){ 57 | self.left = 0.0; 58 | self.right = None; 59 | self.is_evaluated = false; 60 | } 61 | 62 | if self.symbol != "".to_string() { 63 | let new_val = format!("{}{}",self.right.unwrap_or(0.0),val); 64 | self.right = Some(new_val.parse::().unwrap()); 65 | } 66 | else { 67 | let new_val = format!("{}{}",self.left,val); 68 | self.left = new_val.parse::().unwrap(); 69 | } 70 | 71 | } 72 | pub fn add_symbol(&mut self,val:String){ 73 | self.symbol = val; 74 | } 75 | pub fn reset(&mut self){ 76 | self.left = 0.0; 77 | self.right = None; 78 | self.symbol = "".to_string(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/button.rs: -------------------------------------------------------------------------------- 1 | use bevy::prelude::*; 2 | 3 | pub struct BtnPlugin{ 4 | pub text:String, 5 | } 6 | 7 | impl Plugin for BtnPlugin{ 8 | fn build(&self, app: &mut AppBuilder) { 9 | app 10 | .init_resource::() 11 | .add_startup_system(setup.system()) 12 | .add_system(button_system.system()); 13 | } 14 | } 15 | /// This example illustrates how to create a button that changes color and text based on its interaction state. 16 | 17 | pub struct ButtonMaterials { 18 | normal: Handle, 19 | hovered: Handle, 20 | pressed: Handle, 21 | } 22 | 23 | impl FromResources for ButtonMaterials { 24 | fn from_resources(resources: &Resources) -> Self { 25 | let mut materials = resources.get_mut::>().unwrap(); 26 | ButtonMaterials { 27 | normal: materials.add(Color::rgb(0.02, 0.02, 0.02).into()), 28 | hovered: materials.add(Color::rgb(0.05, 0.05, 0.05).into()), 29 | pressed: materials.add(Color::rgb(0.1, 0.5, 0.1).into()), 30 | } 31 | } 32 | } 33 | 34 | fn button_system( 35 | button_materials: Res, 36 | mut interaction_query: Query<( 37 | &Button, 38 | Mutated, 39 | &mut Handle, 40 | &Children, 41 | )>, 42 | text_query: Query<&mut Text>, 43 | ) { 44 | for (_button, interaction, mut material, children) in &mut interaction_query.iter() { 45 | let mut text = text_query.get_mut::(children[0]).unwrap(); 46 | match *interaction { 47 | Interaction::Clicked => { 48 | *material = button_materials.pressed; 49 | } 50 | Interaction::Hovered => { 51 | *material = button_materials.hovered; 52 | } 53 | Interaction::None => { 54 | *material = button_materials.normal; 55 | } 56 | } 57 | } 58 | } 59 | 60 | fn setup( 61 | mut commands: Commands, 62 | asset_server: Res, 63 | button_materials: Res, 64 | ) { 65 | commands 66 | // ui camera 67 | .spawn(ButtonComponents { 68 | style: Style { 69 | size: Size::new(Val::Px(65.0), Val::Px(65.0)), 70 | // center button 71 | margin: Rect::all(Val::Auto), 72 | // horizontally center child text 73 | justify_content: JustifyContent::Center, 74 | // vertically center child text 75 | align_items: AlignItems::Center, 76 | ..Default::default() 77 | }, 78 | material: button_materials.normal, 79 | ..Default::default() 80 | }) 81 | .with_children(|parent| { 82 | parent.spawn(TextComponents { 83 | text: Text { 84 | value: state.text.clone(), 85 | font: asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap(), 86 | style: TextStyle { 87 | font_size: 40.0, 88 | color: Color::rgb(0.8, 0.8, 0.8), 89 | }, 90 | }, 91 | ..Default::default() 92 | }); 93 | }); 94 | } 95 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use bevy::prelude::*; 2 | mod calc; 3 | 4 | fn main() { 5 | App::build() 6 | .insert_resource(WindowDescriptor { 7 | title: "bevy calculator".to_string(), 8 | width: 450.0, 9 | height: 600.0, 10 | vsync: true, 11 | resizable: true, 12 | ..Default::default() 13 | }) 14 | .add_plugins(DefaultPlugins) 15 | .init_resource::() 16 | .insert_resource(calc::Calc::new()) 17 | .add_startup_system(setup_calc_ui.system()) 18 | .add_system(button_system.system()) 19 | .add_system(display_system.system()) 20 | .run(); 21 | } 22 | 23 | struct ButtonMaterials { 24 | normal: Handle, 25 | hovered: Handle, 26 | pressed: Handle, 27 | cc: Handle, 28 | } 29 | 30 | impl FromWorld for ButtonMaterials { 31 | fn from_world(world: &mut World) -> Self { 32 | let mut materials = world.get_resource_mut::>().unwrap(); 33 | ButtonMaterials { 34 | normal: materials.add(Color::rgb(0.02, 0.02, 0.02).into()), 35 | hovered: materials.add(Color::rgb(0.09, 0.09, 0.09).into()), 36 | pressed: materials.add(Color::rgb(0.7, 0.4, 0.0).into()), 37 | cc: materials.add(Color::rgb(0.7, 0.0, 0.0).into()) 38 | } 39 | } 40 | } 41 | 42 | fn button_system( 43 | mut calc:ResMut, 44 | button_materials: Res, 45 | mut interaction_query: Query< 46 | (&Interaction, &mut Handle, &Children), 47 | (Changed, With