1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![allow(clippy::type_complexity, dead_code)]

mod pass;

pub use imgui;
pub use pass::DrawImguiDesc;

use amethyst::{
	core::SystemDesc,
	ecs::{DispatcherBuilder, Read, ReadExpect, System, SystemData, World, Write},
	error::Error,
	input::{BindingTypes, InputEvent},
	renderer::{
		bundle::{RenderOrder, RenderPlan, RenderPlugin, Target},
		rendy::{factory::Factory, graph::render::RenderGroupDesc},
		types::Backend,
	},
	shrev::{EventChannel, ReaderId},
	window::Window,
	winit::Event,
};
use derivative::Derivative;
use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::sync::{Arc, Mutex};

pub struct ImguiContextWrapper(pub imgui::Context);
unsafe impl Send for ImguiContextWrapper {}

pub struct FilteredInputEvent<T: BindingTypes>(pub InputEvent<T>);

pub struct ImguiInputSystem<T: BindingTypes> {
	input_reader: ReaderId<InputEvent<T>>,
	winit_reader: ReaderId<Event>,
}
impl<'s, T: BindingTypes> System<'s> for ImguiInputSystem<T> {
	type SystemData = (
		ReadExpect<'s, Arc<Mutex<ImguiContextWrapper>>>,
		Read<'s, EventChannel<InputEvent<T>>>,
		Read<'s, EventChannel<Event>>,
		Write<'s, EventChannel<FilteredInputEvent<T>>>,
	);

	fn run(&mut self, (context, input_events, winit_events, mut filtered_events): Self::SystemData) {
		let state = &mut context.lock().unwrap().0;

		for _ in winit_events.read(&mut self.winit_reader) {
			//platform.handle_event(state.io_mut(), &window, &event);
		}
		for input in input_events.read(&mut self.input_reader) {
			match input {
				InputEvent::MouseMoved { .. } |
				InputEvent::MouseButtonPressed(_) |
				InputEvent::MouseButtonReleased(_) |
				InputEvent::MouseWheelMoved(_) => {
					if !state.io().want_capture_mouse {
						filtered_events.single_write(FilteredInputEvent(input.clone()));
					}
				},
				InputEvent::KeyPressed { .. } | InputEvent::KeyReleased { .. } => {
					if !state.io().want_capture_keyboard {
						filtered_events.single_write(FilteredInputEvent(input.clone()));
					}
				},
				_ => filtered_events.single_write(FilteredInputEvent(input.clone())),
			}
		}
	}
}

pub struct ImguiInputSystemDesc<T: BindingTypes> {
	_marker: std::marker::PhantomData<T>,
	config_flags: imgui::ConfigFlags,
}
impl<T: BindingTypes> ImguiInputSystemDesc<T> {
	pub fn new(config_flags: imgui::ConfigFlags) -> Self {
		Self {
			_marker: Default::default(),
			config_flags,
		}
	}
}

impl<'a, 'b, T: BindingTypes> SystemDesc<'a, 'b, ImguiInputSystem<T>> for ImguiInputSystemDesc<T> {
	fn build(self, world: &mut World) -> ImguiInputSystem<T> {
		<ImguiInputSystem<T> as System<'_>>::SystemData::setup(world);

		let input_reader = Write::<EventChannel<InputEvent<T>>>::fetch(world).register_reader();
		let winit_reader = Write::<EventChannel<Event>>::fetch(world).register_reader();

		// Setup Imgui
		let mut context = imgui::Context::create();

		context.fonts().add_font(&[imgui::FontSource::DefaultFontData {
			config: Some(imgui::FontConfig {
				size_pixels: 13.,
				..imgui::FontConfig::default()
			}),
		}]);

		context.io_mut().config_flags |= self.config_flags;

		let mut platform = WinitPlatform::init(&mut context);
		platform.attach_window(context.io_mut(), &world.fetch::<Window>(), HiDpiMode::Default);

		world.insert(Arc::new(Mutex::new(ImguiContextWrapper(context))));
		world.insert(platform);

		ImguiInputSystem {
			input_reader,
			winit_reader,
		}
	}
}

/// Ui is actually Ui<'a>
/// This implies 'static
static mut CURRENT_UI: Option<imgui::Ui<'static>> = None;

pub fn with(f: impl FnOnce(&imgui::Ui)) {
	unsafe {
		if let Some(ui) = current_ui() {
			(f)(ui);
		}
	}
}

// what lifeimtes go here? how do I use transmute here?
pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> { CURRENT_UI.as_ref() }

/// A [RenderPlugin] for rendering Imgui elements.
#[derive(Derivative)]
#[derivative(Debug(bound = ""))]
pub struct RenderImgui<T: BindingTypes> {
	target: Target,
	config_flags: imgui::ConfigFlags,
	_marker: std::marker::PhantomData<T>,
}
impl<T: BindingTypes> Default for RenderImgui<T> {
	#[cfg(feature = "docking")]
	fn default() -> Self {
		Self {
			target: Default::default(),
			_marker: Default::default(),
			config_flags: imgui::ConfigFlags::ENABLE_DOCKING,
		}
	}

	#[cfg(not(feature = "docking"))]
	fn default() -> Self {
		Self {
			target: Default::default(),
			_marker: Default::default(),
			config_flags: imgui::ConfigFlags::empty(),
		}
	}
}

impl<T: BindingTypes> RenderImgui<T> {
	pub fn with_imgui_config(mut self, config_flags: imgui::ConfigFlags) -> Self {
		self.config_flags = config_flags;
		self
	}

	/// Select render target on which UI should be rendered.
	pub fn with_target(mut self, target: Target) -> Self {
		self.target = target;
		self
	}
}

impl<B: Backend, T: BindingTypes> RenderPlugin<B> for RenderImgui<T> {
	fn on_build<'a, 'b>(&mut self, world: &mut World, dispatcher: &mut DispatcherBuilder<'a, 'b>) -> Result<(), Error> {
		dispatcher.add(
			ImguiInputSystemDesc::<T>::new(self.config_flags).build(world),
			"imgui_input_system",
			&["input_system", "window"],
		);

		Ok(())
	}

	fn on_plan(&mut self, plan: &mut RenderPlan<B>, _factory: &mut Factory<B>, _: &World) -> Result<(), Error> {
		plan.extend_target(self.target, |ctx| {
			ctx.add(RenderOrder::Overlay, DrawImguiDesc::new().builder())?;
			Ok(())
		});
		Ok(())
	}
}