Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use crate::solver::{Solution};
- use gtk::{prelude::*, Inhibit, Orientation::{Horizontal, Vertical} };
- use relm::{Relm, Widget};
- use relm_derive::{Msg, widget};
- #[derive(Msg)]
- pub enum TilesMsg {
- Update(usize, u32),
- }
- pub struct TilesModel {
- }
- #[widget]
- impl Widget for TilesComp {
- fn init_view(&mut self) {
- for i in 0..6 {
- self.set_model_in_tile(i);
- self.connect_changed_event_in_tile(i);
- }
- }
- fn get_tile_component(&self, index: usize) -> >k::ComboBoxText {
- match index {
- 0 => &self.tile_0,
- 1 => &self.tile_1,
- 2 => &self.tile_2,
- 3 => &self.tile_3,
- 4 => &self.tile_4,
- 5 => &self.tile_5,
- _ => panic!("Incorrect tile index")
- }
- }
- fn set_model_in_tile(&mut self, tile_component_index: usize) {
- let tile_component = self.get_tile_component(tile_component_index);
- let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100];
- for current in values.iter() {
- tile_component.append_text(¤t.to_string().to_owned());
- }
- tile_component.set_active(Some(0));
- }
- fn connect_changed_event_in_tile(&mut self, tile_component_index: usize) {
- let tile_component = self.get_tile_component(tile_component_index);
- tile_component.connect_changed(move |comp| {
- if let Some(id) = comp.get_active_id() {
- if let Ok(value) = id.parse::<u32>() {
- TilesMsg::Update(tile_component_index, value);
- }
- }
- });
- }
- fn model(relm: &Relm<Self>, _: ()) -> TilesModel {
- TilesModel {
- }
- }
- fn update(&mut self, event: TilesMsg) {
- match event {
- TilesMsg::Update(index, new_val) => {},
- }
- }
- view! {
- gtk::Box {
- orientation: Horizontal,
- spacing: 4,
- #[name="tile_0"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- #[name="tile_1"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- #[name="tile_2"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- #[name="tile_3"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- #[name="tile_4"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- #[name="tile_5"]
- gtk::ComboBoxText {
- id_column: 0,
- },
- }
- }
- }
- #[derive(Msg)]
- pub enum TargetNumberMsg {
- Update(u32),
- }
- pub struct TargetNumberModel {
- value: u32,
- }
- #[widget]
- impl Widget for TargetNumberComp {
- fn model() -> TargetNumberModel {
- TargetNumberModel {
- value: 0,
- }
- }
- fn update(&mut self, event: TargetNumberMsg) {
- match event {
- TargetNumberMsg::Update(new_val) => self.model.value = new_val,
- }
- }
- view! {
- gtk::Box {
- orientation: Horizontal,
- gtk::Label {
- justify: gtk::Justification::Center,
- text: &self.model.value.to_string(),
- hexpand: true,
- },
- }
- }
- }
- pub struct SolutionModel {
- }
- #[derive(Msg)]
- pub enum SolutionMsg {
- }
- #[widget]
- impl Widget for SolutionComponent {
- fn init_view(&mut self) {
- self.current_solution.get_buffer().unwrap().set_text("\n\n\n\n");
- }
- fn model() -> SolutionModel {
- SolutionModel {}
- }
- fn update(&mut self, msg: SolutionMsg) {
- }
- view! {
- gtk::Box {
- orientation: Vertical,
- gtk::Box {
- orientation: Horizontal,
- spacing: 4,
- homogeneous: true,
- gtk::Button {
- label: "|<"
- },
- gtk::Button {
- label: "<"
- },
- gtk::Button {
- label: ">"
- },
- gtk::Button {
- label: ">|"
- },
- },
- #[name="current_solution"]
- gtk::TextView {
- editable: false,
- }
- }
- }
- }
- pub struct AppModel {
- tiles: [u32; 6],
- }
- #[derive(Msg)]
- pub enum AppMsg {
- Quit,
- Solve,
- UpdateTile(usize, u32),
- }
- #[widget]
- impl Widget for Win {
- fn model() -> AppModel {
- AppModel {
- tiles: [0; 6],
- }
- }
- fn update(&mut self, event: AppMsg) {
- match event {
- AppMsg::Quit => gtk::main_quit(),
- AppMsg::Solve => self.solve(),
- AppMsg::UpdateTile(index, new_val) => self.model.tiles[index] = new_val,
- }
- }
- fn solve(&mut self) {
- /////////////////////////////////////
- println!("{}", self.model.tiles);
- /////////////////////////////////////
- }
- view! {
- gtk::Window {
- gtk::Box {
- orientation: Vertical,
- spacing: 4,
- #[name="tiles"]
- TilesComp {
- TilesMsg::Update(index, new_val) => AppMsg::UpdateTile(index, new_val), /// HERE !!!
- },
- #[name="target"]
- TargetNumberComp {},
- gtk::Button {
- label: "Solve",
- hexpand: true,
- clicked(_) => AppMsg::Solve,
- },
- SolutionComponent {},
- },
- delete_event(_, _) => (AppMsg::Quit, Inhibit(false)),
- }
- }
- }
- pub fn run() {
- Win::run(()).expect("Failed to build window.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement