-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
226 lines (200 loc) · 6.44 KB
/
lib.rs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#![deny(clippy::all)]
use log::error;
use pixels::{Pixels, SurfaceTexture};
use winit::dpi::LogicalSize;
use winit::event::{Event, TouchPhase, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
const WIDTH: u32 = 320;
const HEIGHT: u32 = 240;
const BOX_SIZE: i16 = 64;
/// Representation of the application state. In this example, a box will bounce around the screen.
struct World {
box_x: i16,
box_y: i16,
velocity_x: i16,
velocity_y: i16,
}
#[cfg_attr(
target_os = "android",
ndk_glue::main(backtrace = "on", logger(tag = "pixels-android", level = "info"))
)]
fn main() {
run().unwrap();
}
fn show_soft_input(show: bool) -> bool {
let ctx = ndk_glue::native_activity();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }.unwrap();
let env = vm.attach_current_thread().unwrap();
let class_ctxt = env.find_class("android/content/Context").unwrap();
let ime = env
.get_static_field(class_ctxt, "INPUT_METHOD_SERVICE", "Ljava/lang/String;")
.unwrap();
let ime_manager = env
.call_method(
ctx.activity(),
"getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;",
&[ime],
)
.unwrap()
.l()
.unwrap();
let jni_window = env
.call_method(ctx.activity(), "getWindow", "()Landroid/view/Window;", &[])
.unwrap()
.l()
.unwrap();
let view = env
.call_method(jni_window, "getDecorView", "()Landroid/view/View;", &[])
.unwrap()
.l()
.unwrap();
if show {
let result = env
.call_method(
ime_manager,
"showSoftInput",
"(Landroid/view/View;I)Z",
&[view.into(), 0i32.into()],
)
.unwrap()
.z()
.unwrap();
log::info!("show input: {}", result);
result
} else {
let window_token = env
.call_method(view, "getWindowToken", "()Landroid/os/IBinder;", &[])
.unwrap()
.l()
.unwrap();
let result = env
.call_method(
ime_manager,
"hideSoftInputFromWindow",
"(Landroid/os/IBinder;I)Z",
&[window_token.into(), 0i32.into()],
)
.unwrap()
.z()
.unwrap();
log::info!("hide input: {}", result);
result
}
}
fn run() -> anyhow::Result<()> {
let event_loop = EventLoop::new();
let window = {
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
WindowBuilder::new()
.with_title("Hello Pixels")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
let mut pixels: Option<Pixels> = None;
let mut world = World::new();
let mut soft_keyboard = false;
event_loop.run(move |event, _, control_flow| {
control_flow.set_poll();
if let Event::Resumed = event {
log::info!("resumed");
pixels = Some({
let window_size = window.inner_size();
let surface_texture =
SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(WIDTH, HEIGHT, surface_texture).unwrap()
});
}
if let Event::Suspended = event {
pixels = None;
}
if let Some(pixels) = pixels.as_mut() {
// Draw the current frame
match event {
Event::RedrawRequested(_) => {
world.draw(pixels.get_frame());
if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
}
}
Event::MainEventsCleared => {
// Update internal state and request a redraw
world.update();
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
}
Event::WindowEvent {
event: WindowEvent::Touch(touch),
..
} => {
if touch.phase == TouchPhase::Started {
// toggle software keyboard
soft_keyboard = !soft_keyboard;
show_soft_input(soft_keyboard);
}
}
Event::WindowEvent {
event: WindowEvent::KeyboardInput { input, .. },
..
} => {
log::info!("input: {:?}", input);
}
_ => (),
}
}
});
}
impl World {
/// Create a new `World` instance that can draw a moving box.
fn new() -> Self {
Self {
box_x: 24,
box_y: 16,
velocity_x: 1,
velocity_y: 1,
}
}
/// Update the `World` internal state; bounce the box around the screen.
fn update(&mut self) {
if self.box_x <= 0 || self.box_x + BOX_SIZE > WIDTH as i16 {
self.velocity_x *= -1;
}
if self.box_y <= 0 || self.box_y + BOX_SIZE > HEIGHT as i16 {
self.velocity_y *= -1;
}
self.box_x += self.velocity_x;
self.box_y += self.velocity_y;
}
/// Draw the `World` state to the frame buffer.
///
/// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb`
fn draw(&self, frame: &mut [u8]) {
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
let x = (i % WIDTH as usize) as i16;
let y = (i / WIDTH as usize) as i16;
let inside_the_box = x >= self.box_x
&& x < self.box_x + BOX_SIZE
&& y >= self.box_y
&& y < self.box_y + BOX_SIZE;
let rgba = if inside_the_box {
[0x5e, 0x48, 0xe8, 0xff]
} else {
[0x48, 0xb2, 0xe8, 0xff]
};
pixel.copy_from_slice(&rgba);
}
}
}