Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a view menu with zoom actions #16

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tarsila/src/gui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ impl MenuBar {
ui.close_menu();
}
});
ui.menu_button("View", |ui| {
if ui.button("Zoom in").clicked() {
events.push(Effect::UiEvent(UiEvent::ZoomIn));
ui.close_menu();
}
if ui.button("Zoom out").clicked() {
events.push(Effect::UiEvent(UiEvent::ZoomOut));
ui.close_menu();
}
if ui.button("Reset zoom to default").clicked() {
events.push(Effect::UiEvent(UiEvent::ResetZoom));
ui.close_menu();
}
if ui.button("Set zoom to 100%").clicked() {
events.push(Effect::UiEvent(UiEvent::SetZoom100));
ui.close_menu();
}
});
ui.menu_button("Canvas", |ui| {
/*
ui.menu_button("Category", |ui| {
Expand Down
11 changes: 10 additions & 1 deletion tarsila/src/ui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const CAMERA_SPEED: f32 = 12.;
const BG_COLOR: MqColor = MqColor::new(0.5, 0.5, 0.5, 1.);
const GUI_REST_MS: u64 = 100;
const FPS_INTERVAL: usize = 15;
const DEFAULT_ZOOM_LEVEL: f32 = 8.;

// Center on the space after the toolbar
const CANVAS_X: f32 = LEFT_TOOLBAR_W as f32 + ((WINDOW_W as u16 - LEFT_TOOLBAR_W) / 2) as f32
Expand Down Expand Up @@ -52,12 +53,14 @@ impl From<UiEvent> for Effect {
pub enum UiEvent {
ZoomIn,
ZoomOut,
ResetZoom,
MoveCamera(Direction),
MouseOverGui,
Paste,
Exit,
NewProject,
GuiInteraction,
SetZoom100,
}

impl UiEvent {
Expand Down Expand Up @@ -131,7 +134,7 @@ impl Default for UiState {
gui: Gui::new(),
camera: Position::ZERO_F32,
canvas_pos: (CANVAS_X, CANVAS_Y).into(),
zoom: 8.,
zoom: DEFAULT_ZOOM_LEVEL,
layer_textures: vec![drawing],
keyboard: KeyboardManager::new(),
mouse: MouseManager::new(),
Expand Down Expand Up @@ -308,6 +311,8 @@ impl UiState {
match event {
UiEvent::ZoomIn => self.zoom_in(),
UiEvent::ZoomOut => self.zoom_out(),
UiEvent::ResetZoom => self.reset_zoom(),
UiEvent::SetZoom100 => self.zoom = 1.,
UiEvent::MoveCamera(dir) => self.move_camera(dir),
UiEvent::MouseOverGui => self.mouse_over_gui = true,
UiEvent::GuiInteraction => (),
Expand Down Expand Up @@ -377,6 +382,10 @@ impl UiState {
self.camera.y /= 2.;
}

pub fn reset_zoom(&mut self) {
self.zoom = DEFAULT_ZOOM_LEVEL;
}

pub fn move_camera(&mut self, direction: Direction) {
let speed = CAMERA_SPEED;

Expand Down