main.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #![feature(nll)]
  2. #![feature(never_type)]
  3. extern crate sdl2;
  4. extern crate rand;
  5. extern crate weak_table;
  6. pub mod geom;
  7. pub mod frag;
  8. pub mod diff;
  9. pub mod tree;
  10. pub mod gui;
  11. pub mod signal;
  12. mod app {
  13. use sdl2::pixels::{Color};
  14. use geom;
  15. use gui;
  16. use gui::{GUI, Component, PureComponent};
  17. use frag;
  18. use frag::AlignH::*;
  19. use frag::AlignV::*;
  20. #[derive(Eq, PartialEq)]
  21. struct Square { size: u32 }
  22. impl gui::Component for Square {
  23. type State = u32;
  24. fn init(&self) -> u32 { self.size }
  25. fn render(&self, ctx: gui::Ctx<Self>) -> GUI {
  26. let color = match self.size {
  27. 0...40 => Color::RGB(255, 0, 40),
  28. 40...50 => Color::RGB(0, 120, 240),
  29. _ => Color::RGB(255, 240, 0),
  30. };
  31. let area = frag::fill(color)
  32. .on_click(ctx.callback(|path, &()| {
  33. print!("click! path={:?}\n", path);
  34. }));
  35. area.container()
  36. .width(self.size)
  37. .height(self.size)
  38. .into()
  39. }
  40. }
  41. #[derive(Eq, PartialEq)]
  42. struct Window { elems: Vec<gui::GUI> }
  43. impl PureComponent for Window {
  44. fn render(&self) -> GUI {
  45. gui::pack_lr(self.elems.clone())
  46. .align(Left, Top)
  47. .underlay(gui::fill(Color::RGB(255, 255, 255)))
  48. .container()
  49. .bounds(geom::Dim(600, 400))
  50. .align(Center, Middle)
  51. }
  52. }
  53. pub fn create() -> GUI {
  54. Window {
  55. elems: [40, 50, 60]
  56. .iter()
  57. .map(|&size| Square { size }.into_gui())
  58. .collect()
  59. }.into_gui()
  60. }
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////
  63. ////////////////////////////////////////////////////////////////////////////////////////////////////
  64. fn main() {
  65. let sdl = sdl2::init().unwrap();
  66. let mut events = sdl.event_pump().unwrap();
  67. let vid = sdl.video().unwrap();
  68. let mut canvas: sdl2::render::Canvas<_> =
  69. vid.window("R", 800, 600)
  70. .build()
  71. .unwrap()
  72. .into_canvas()
  73. .build()
  74. .unwrap();
  75. let mut ui = gui::LiveGUI::new(
  76. geom::Dim(800, 600),
  77. app::create(),
  78. );
  79. 'renderloop: loop {
  80. for evt in events.poll_iter() {
  81. use sdl2::event::{Event, WindowEvent};
  82. match evt {
  83. Event::Quit { .. } =>
  84. break 'renderloop,
  85. Event::Window { win_event: WindowEvent::Resized(w, h), .. } =>
  86. ui.set_size(geom::Dim(w as u32, h as u32)),
  87. Event::MouseButtonDown { x, y, .. } => {
  88. let mouse_pos = geom::Pos(x as _, y as _);
  89. ui.fire_click_evt(mouse_pos);
  90. }
  91. _ =>
  92. (),
  93. }
  94. }
  95. {
  96. use frag::{DrawFrag};
  97. canvas.set_draw_color(sdl2::pixels::Color::RGB(0, 0, 0));
  98. canvas.clear();
  99. canvas.draw_frag(ui.root_frag());
  100. canvas.present();
  101. std::thread::sleep(
  102. std::time::Duration::from_micros(1000000 / 64)
  103. );
  104. }
  105. }
  106. }
  107. /*
  108. fn timed<F, T>(f: F) -> T where F: FnOnce() -> T {
  109. use std::time::{Instant};
  110. let start = Instant::now();
  111. let ret = f();
  112. print!("{:?}\n", Instant::now() - start);
  113. ret
  114. }
  115. */