Skip to content

Commit

Permalink
add hover on pet. working on add pet functionality and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
SeakMengs committed Aug 25, 2023
1 parent f6d29f5 commit d16138e
Show file tree
Hide file tree
Showing 29 changed files with 898 additions and 87 deletions.
12 changes: 12 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tauri-build = { version = "1.4", features = [] }
tauri = { version = "1.4.0", features = [ "window-all", "dialog-all", "fs-all", "path-all", "macos-private-api", "system-tray", "shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
mouse_position = "0.1.3"
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
Expand Down
23 changes: 23 additions & 0 deletions src-tauri/src/app/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use mouse_position::mouse_position::Mouse;
use serde_json::json;

#[tauri::command]
pub fn get_mouse_position() -> serde_json::Value {
/*
* because we set the window to ignore cursor events, we cannot use
* javascript to get the mouse position, so we use get mouse position manually
*/
let position = Mouse::get_mouse_position();
match position {
Mouse::Position { x, y } => {
json!({
"clientX": x,
"clientY": y
})
}
Mouse::Error => {
println!("Error getting mouse position");
json!(null)
}
}
}
3 changes: 2 additions & 1 deletion src-tauri/src/app/default/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"theme": "dark",
"language": "en",
"isPetAboveTaskbar": false
"isPetAboveTaskbar": false,
"isAllowHoverOnPet": true
}
3 changes: 2 additions & 1 deletion src-tauri/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod conf;
pub mod tray;
pub mod utils;
pub mod utils;
pub mod cmd;
7 changes: 6 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod app;
use app::cmd::get_mouse_position;
use app::conf::{combine_config_path, convert_path, if_app_config_does_not_exist_create_default};
use app::tray::{handle_tray_event, init_system_tray};
use tauri::Manager;
Expand Down Expand Up @@ -39,7 +40,11 @@ fn main() {
})
.system_tray(init_system_tray())
.on_system_tray_event(handle_tray_event)
.invoke_handler(tauri::generate_handler![convert_path, combine_config_path])
.invoke_handler(tauri::generate_handler![
convert_path,
combine_config_path,
get_mouse_position
])
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_app_handle, event| {
Expand Down
16 changes: 13 additions & 3 deletions src/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { usePetStore } from "./hooks/usePetStore";
import { listen } from '@tauri-apps/api/event';
import { clonePetsFromSettings } from "./utils/clonePetsFromSettings";
import { useSettingStore } from "./hooks/useSettingStore";
import { TRenderEventListener } from "./types/IEvents";

function Canvas() {
// credit: https://stackoverflow.com/questions/16277383/javascript-screen-height-and-screen-width-returns-incorrect-values
Expand All @@ -14,16 +15,25 @@ function Canvas() {
const requestAnimateFrameId = useRef<number>(0);

const { pets, isPetsInitialized, setIsPetsInitialized } = usePetStore();
const { setIsPetAboveTaskbar } = useSettingStore();
const { setIsPetAboveTaskbar, setIsAllowHoverOnPet } = useSettingStore();

// disable right click (context menu) for build version only. uncomment for development
// credit: https://github.com/tauri-apps/wry/issues/30
document.addEventListener('contextmenu', event => event.preventDefault());

useEffect(() => {
let unListen: () => void;
listen<any>('render', (event) => {
setIsPetAboveTaskbar(event.payload!.isPetAboveTaskbar);
listen<any>('render', (event: TRenderEventListener) => {
switch (event.payload.dispatchType) {
case 'switchPetAboveTaskBar':
setIsPetAboveTaskbar(event.payload!.value as boolean);
break;
case 'switchAllowHoverOnPet':
setIsAllowHoverOnPet(event.payload!.value as boolean);
break;
default:
return;
}
clonePetsFromSettings();
}).then((unListenFn) => {
unListen = unListenFn;
Expand Down
4 changes: 2 additions & 2 deletions src/SettingWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function SettingWindow() {
// disable right click (context menu) for build version only. uncomment for development
// credit: https://github.com/tauri-apps/wry/issues/30
document.addEventListener('contextmenu', event => event.preventDefault());

// get object theme change it name to colorScheme for readability
const { theme: colorScheme, language } = useSettingStore();
const { t, i18n } = useTranslation();
Expand All @@ -50,6 +49,7 @@ function SettingWindow() {
}

let CurrentSettingTab = SettingTabComponent[page];

if (!CurrentSettingTab) {
CurrentSettingTab = memo(() => <Text component='h1'>{t("Seem like the content of this page doesn't exist or has not been updated")}</Text>);
}
Expand Down Expand Up @@ -102,4 +102,4 @@ function SettingWindow() {
);
}

export default SettingWindow;
export default memo(SettingWindow);
2 changes: 1 addition & 1 deletion src/__tests__/functions/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { mockIPC, mockWindows, clearMocks } from '@tauri-apps/api/mocks'
import { getAppSettings } from "../../utils/settingsHelper";
import { getAppSettings } from "../../utils/settings";
import { expect, test, afterEach } from "vitest";

afterEach(() => {
Expand Down
44 changes: 34 additions & 10 deletions src/class/Pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* A good resource to learn about canvas, the project is based on this tutorial.
* credit: https://www.youtube.com/watch?v=vyqbNFMDRGQ&t=8593s&ab_channel=ChrisCourses
*/
import { invoke } from "@tauri-apps/api";
import { useSettingStore } from "../hooks/useSettingStore";
import { IPetParams, TStates, TCurrentPetState } from "../types/IPet";

import { IMousePosition } from "../types/IMousePosition";
export default class Pet {
position: { x: number; y: number };
name: string;
Expand Down Expand Up @@ -130,16 +131,39 @@ export default class Pet {

}

if (this.isBeingSelected) {
context.strokeStyle = "red";
context.lineWidth = 1;
context.strokeRect(
dx_start_draw_position,
dy_start_draw_position - this.offset.y * this.scale,
dWidth_draw_width,
dHeight_draw_height
)
const isAllowHoverOnPet = useSettingStore.getState().isAllowHoverOnPet;
if (isAllowHoverOnPet) {
invoke<IMousePosition>('get_mouse_position').then((mousePosition) => {
// if hover on the pet set isBeingSelected to true
if (
mousePosition.clientX >= dx_start_draw_position &&
mousePosition.clientX <= dx_start_draw_position + dWidth_draw_width &&
mousePosition.clientY >= dy_start_draw_position - this.offset.y * this.scale &&
mousePosition.clientY <= dy_start_draw_position + dHeight_draw_height - this.offset.y * this.scale
) {
this.isBeingSelected = true;
// check state before switch because not all pets have the same states
if (this.states.meow) this.switchState('meow');
else if (this.states.attack) this.switchState('attack');
return
}

this.isBeingSelected = false;
if (this.states.walk) this.switchState('walk');
else if (this.states.run) this.switchState('run');
});
}

// if (this.isBeingSelected) {
// context.strokeStyle = "red";
// context.lineWidth = 1;
// context.strokeRect(
// dx_start_draw_position,
// dy_start_draw_position - this.offset.y * this.scale,
// dWidth_draw_width,
// dHeight_draw_height
// )
// }
}

// used to control the animation speed,
Expand Down
4 changes: 4 additions & 0 deletions src/default/defaultPetExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IPetParams } from '../types/IPet';
import defaultPetConfig from './pets.json';

export const defaultPetOptions: IPetParams[] = JSON.parse(JSON.stringify(defaultPetConfig));
Loading

0 comments on commit d16138e

Please sign in to comment.